Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 17 | #include <ChassisIntrusionSensor.hpp> |
| 18 | #include <Utils.hpp> |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 19 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 20 | #include <boost/asio/io_service.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 21 | #include <boost/container/flat_map.hpp> |
Patrick Williams | 0c42f40 | 2021-08-27 16:05:45 -0500 | [diff] [blame] | 22 | #include <phosphor-logging/lg2.hpp> |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 23 | #include <sdbusplus/asio/connection.hpp> |
| 24 | #include <sdbusplus/asio/object_server.hpp> |
| 25 | #include <sdbusplus/asio/sd_event.hpp> |
| 26 | #include <sdbusplus/bus.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 27 | #include <sdbusplus/bus/match.hpp> |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 28 | #include <sdbusplus/exception.hpp> |
| 29 | #include <sdbusplus/server.hpp> |
| 30 | #include <sdbusplus/timer.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 31 | |
| 32 | #include <array> |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 33 | #include <charconv> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 34 | #include <chrono> |
| 35 | #include <ctime> |
| 36 | #include <fstream> |
| 37 | #include <functional> |
| 38 | #include <iostream> |
| 39 | #include <memory> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 40 | #include <stdexcept> |
| 41 | #include <string> |
| 42 | #include <utility> |
| 43 | #include <vector> |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 44 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 45 | static constexpr bool debug = false; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 46 | |
| 47 | static constexpr const char* sensorType = |
| 48 | "xyz.openbmc_project.Configuration.ChassisIntrusionSensor"; |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 49 | static constexpr const char* nicType = "xyz.openbmc_project.Configuration.NIC"; |
Brandon Kim | 6655823 | 2021-11-09 16:53:08 -0800 | [diff] [blame^] | 50 | static constexpr auto nicTypes{std::to_array<const char*>({nicType})}; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 51 | |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 52 | namespace fs = std::filesystem; |
| 53 | |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 54 | static bool getIntrusionSensorConfig( |
Lei YU | ba63793 | 2021-03-17 10:35:00 +0800 | [diff] [blame] | 55 | const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 56 | IntrusionSensorType* pType, int* pBusId, int* pSlaveAddr, |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 57 | bool* pGpioInverted) |
| 58 | { |
| 59 | // find matched configuration according to sensor type |
| 60 | ManagedObjectType sensorConfigurations; |
| 61 | bool useCache = false; |
| 62 | |
| 63 | if (!getSensorConfiguration(sensorType, dbusConnection, |
| 64 | sensorConfigurations, useCache)) |
| 65 | { |
| 66 | std::cerr << "error communicating to entity manager\n"; |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | const SensorData* sensorData = nullptr; |
| 71 | const std::pair<std::string, |
| 72 | boost::container::flat_map<std::string, BasicVariantType>>* |
| 73 | baseConfiguration = nullptr; |
| 74 | |
| 75 | // Get bus and addr of matched configuration |
| 76 | for (const std::pair<sdbusplus::message::object_path, SensorData>& sensor : |
| 77 | sensorConfigurations) |
| 78 | { |
| 79 | baseConfiguration = nullptr; |
| 80 | sensorData = &(sensor.second); |
| 81 | |
| 82 | // match sensor type |
| 83 | auto sensorBase = sensorData->find(sensorType); |
| 84 | if (sensorBase == sensorData->end()) |
| 85 | { |
| 86 | std::cerr << "error finding base configuration \n"; |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | baseConfiguration = &(*sensorBase); |
| 91 | |
| 92 | // judge class, "Gpio" or "I2C" |
| 93 | auto findClass = baseConfiguration->second.find("Class"); |
| 94 | if (findClass != baseConfiguration->second.end() && |
Patrick Williams | 9473325 | 2020-05-13 11:44:58 -0500 | [diff] [blame] | 95 | std::get<std::string>(findClass->second) == "Gpio") |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 96 | { |
| 97 | *pType = IntrusionSensorType::gpio; |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | *pType = IntrusionSensorType::pch; |
| 102 | } |
| 103 | |
| 104 | // case to find GPIO info |
| 105 | if (*pType == IntrusionSensorType::gpio) |
| 106 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 107 | auto findGpioPolarity = |
| 108 | baseConfiguration->second.find("GpioPolarity"); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 109 | |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 110 | if (findGpioPolarity == baseConfiguration->second.end()) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 111 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 112 | std::cerr << "error finding gpio polarity in configuration \n"; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 113 | continue; |
| 114 | } |
| 115 | |
| 116 | try |
| 117 | { |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 118 | *pGpioInverted = |
Patrick Williams | 9473325 | 2020-05-13 11:44:58 -0500 | [diff] [blame] | 119 | (std::get<std::string>(findGpioPolarity->second) == "Low"); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 120 | } |
| 121 | catch (const std::bad_variant_access& e) |
| 122 | { |
| 123 | std::cerr << "invalid value for gpio info in config. \n"; |
| 124 | continue; |
| 125 | } |
| 126 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 127 | if (debug) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 128 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 129 | std::cout << "find chassis intrusion sensor polarity inverted " |
| 130 | "flag is " |
| 131 | << *pGpioInverted << "\n"; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | // case to find I2C info |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 138 | if (*pType == IntrusionSensorType::pch) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 139 | { |
| 140 | auto findBus = baseConfiguration->second.find("Bus"); |
| 141 | auto findAddress = baseConfiguration->second.find("Address"); |
| 142 | if (findBus == baseConfiguration->second.end() || |
| 143 | findAddress == baseConfiguration->second.end()) |
| 144 | { |
| 145 | std::cerr << "error finding bus or address in configuration \n"; |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | try |
| 150 | { |
Patrick Williams | 9473325 | 2020-05-13 11:44:58 -0500 | [diff] [blame] | 151 | *pBusId = std::get<uint64_t>(findBus->second); |
| 152 | *pSlaveAddr = std::get<uint64_t>(findAddress->second); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 153 | } |
| 154 | catch (const std::bad_variant_access& e) |
| 155 | { |
| 156 | std::cerr << "invalid value for bus or address in config. \n"; |
| 157 | continue; |
| 158 | } |
| 159 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 160 | if (debug) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 161 | { |
| 162 | std::cout << "find matched bus " << *pBusId |
| 163 | << ", matched slave addr " << *pSlaveAddr << "\n"; |
| 164 | } |
| 165 | return true; |
| 166 | } |
| 167 | } |
| 168 | |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 169 | std::cerr << "can't find matched I2C or GPIO configuration for intrusion " |
| 170 | "sensor. \n"; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 171 | *pBusId = -1; |
| 172 | *pSlaveAddr = -1; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 176 | static constexpr bool debugLanLeash = false; |
| 177 | boost::container::flat_map<int, bool> lanStatusMap; |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 178 | boost::container::flat_map<int, std::string> lanInfoMap; |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 179 | boost::container::flat_map<std::string, int> pathSuffixMap; |
| 180 | |
Lei YU | ba63793 | 2021-03-17 10:35:00 +0800 | [diff] [blame] | 181 | static void getNicNameInfo( |
| 182 | const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 183 | { |
| 184 | auto getter = std::make_shared<GetSensorConfiguration>( |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 185 | dbusConnection, [](const ManagedObjectType& sensorConfigurations) { |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 186 | // Get NIC name and save to map |
| 187 | lanInfoMap.clear(); |
| 188 | for (const std::pair<sdbusplus::message::object_path, SensorData>& |
| 189 | sensor : sensorConfigurations) |
| 190 | { |
| 191 | const std::pair< |
| 192 | std::string, |
| 193 | boost::container::flat_map<std::string, BasicVariantType>>* |
| 194 | baseConfiguration = nullptr; |
| 195 | |
| 196 | // find base configuration |
| 197 | auto sensorBase = sensor.second.find(nicType); |
| 198 | if (sensorBase == sensor.second.end()) |
| 199 | { |
| 200 | continue; |
| 201 | } |
| 202 | baseConfiguration = &(*sensorBase); |
| 203 | |
| 204 | auto findEthIndex = baseConfiguration->second.find("EthIndex"); |
| 205 | auto findName = baseConfiguration->second.find("Name"); |
| 206 | |
| 207 | if (findEthIndex != baseConfiguration->second.end() && |
| 208 | findName != baseConfiguration->second.end()) |
| 209 | { |
| 210 | auto* pEthIndex = |
| 211 | std::get_if<uint64_t>(&findEthIndex->second); |
| 212 | auto* pName = std::get_if<std::string>(&findName->second); |
| 213 | if (pEthIndex != nullptr && pName != nullptr) |
| 214 | { |
| 215 | lanInfoMap[*pEthIndex] = *pName; |
| 216 | if (debugLanLeash) |
| 217 | { |
| 218 | std::cout << "find name of eth" << *pEthIndex |
| 219 | << " is " << *pName << "\n"; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (lanInfoMap.size() == 0) |
| 226 | { |
| 227 | std::cerr << "can't find matched NIC name. \n"; |
| 228 | } |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 229 | }); |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 230 | |
| 231 | getter->getConfiguration( |
| 232 | std::vector<std::string>{nicTypes.begin(), nicTypes.end()}); |
| 233 | } |
| 234 | |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 235 | static void processLanStatusChange(sdbusplus::message::message& message) |
| 236 | { |
| 237 | const std::string& pathName = message.get_path(); |
| 238 | std::string interfaceName; |
| 239 | boost::container::flat_map<std::string, BasicVariantType> properties; |
| 240 | message.read(interfaceName, properties); |
| 241 | |
| 242 | auto findStateProperty = properties.find("OperationalState"); |
| 243 | if (findStateProperty == properties.end()) |
| 244 | { |
| 245 | return; |
| 246 | } |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 247 | std::string* pState = |
| 248 | std::get_if<std::string>(&(findStateProperty->second)); |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 249 | if (pState == nullptr) |
| 250 | { |
| 251 | std::cerr << "invalid OperationalState \n"; |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | bool newLanConnected = (*pState == "routable" || *pState == "carrier" || |
| 256 | *pState == "degraded"); |
| 257 | |
| 258 | // get ethNum from path. /org/freedesktop/network1/link/_32 for eth0 |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 259 | size_t pos = pathName.find("/_"); |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 260 | if (pos == std::string::npos || pathName.length() <= pos + 2) |
| 261 | { |
| 262 | std::cerr << "unexpected path name " << pathName << "\n"; |
| 263 | return; |
| 264 | } |
| 265 | std::string suffixStr = pathName.substr(pos + 2); |
| 266 | |
| 267 | auto findEthNum = pathSuffixMap.find(suffixStr); |
| 268 | if (findEthNum == pathSuffixMap.end()) |
| 269 | { |
| 270 | std::cerr << "unexpected eth for suffixStr " << suffixStr << "\n"; |
| 271 | return; |
| 272 | } |
| 273 | int ethNum = findEthNum->second; |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 274 | |
| 275 | // get lan status from map |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 276 | auto findLanStatus = lanStatusMap.find(ethNum); |
| 277 | if (findLanStatus == lanStatusMap.end()) |
| 278 | { |
| 279 | std::cerr << "unexpected eth " << ethNum << " in lanStatusMap \n"; |
| 280 | return; |
| 281 | } |
| 282 | bool oldLanConnected = findLanStatus->second; |
| 283 | |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 284 | // get lan info from map |
| 285 | std::string lanInfo = ""; |
| 286 | if (lanInfoMap.size() > 0) |
| 287 | { |
| 288 | auto findLanInfo = lanInfoMap.find(ethNum); |
| 289 | if (findLanInfo == lanInfoMap.end()) |
| 290 | { |
| 291 | std::cerr << "unexpected eth " << ethNum << " in lanInfoMap \n"; |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | lanInfo = "(" + findLanInfo->second + ")"; |
| 296 | } |
| 297 | } |
| 298 | |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 299 | if (debugLanLeash) |
| 300 | { |
| 301 | std::cout << "ethNum = " << ethNum << ", state = " << *pState |
| 302 | << ", oldLanConnected = " |
| 303 | << (oldLanConnected ? "true" : "false") |
| 304 | << ", newLanConnected = " |
| 305 | << (newLanConnected ? "true" : "false") << "\n"; |
| 306 | } |
| 307 | |
| 308 | if (oldLanConnected != newLanConnected) |
| 309 | { |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 310 | std::string strEthNum = "eth" + std::to_string(ethNum) + lanInfo; |
Patrick Williams | 0c42f40 | 2021-08-27 16:05:45 -0500 | [diff] [blame] | 311 | auto strState = newLanConnected ? "connected" : "lost"; |
| 312 | auto strMsgId = |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 313 | newLanConnected ? "OpenBMC.0.1.LanRegained" : "OpenBMC.0.1.LanLost"; |
Patrick Williams | 0c42f40 | 2021-08-27 16:05:45 -0500 | [diff] [blame] | 314 | |
| 315 | lg2::info("{ETHDEV} LAN leash {STATE}", "ETHDEV", strEthNum, "STATE", |
| 316 | strState, "REDFISH_MESSAGE_ID", strMsgId, |
| 317 | "REDFISH_MESSAGE_ARGS", strEthNum); |
| 318 | |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 319 | lanStatusMap[ethNum] = newLanConnected; |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Lei YU | dd68d4a | 2021-03-16 22:17:23 +0800 | [diff] [blame] | 323 | /** @brief Initialize the lan status. |
| 324 | * |
| 325 | * @return true on success and false on failure |
| 326 | */ |
Lei YU | ba63793 | 2021-03-17 10:35:00 +0800 | [diff] [blame] | 327 | static bool initializeLanStatus( |
| 328 | const std::shared_ptr<sdbusplus::asio::connection>& conn) |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 329 | { |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 330 | // init lan port name from configuration |
| 331 | getNicNameInfo(conn); |
| 332 | |
| 333 | // get eth info from sysfs |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 334 | std::vector<fs::path> files; |
| 335 | if (!findFiles(fs::path("/sys/class/net/"), R"(eth\d+/ifindex)", files)) |
| 336 | { |
| 337 | std::cerr << "No eth in system\n"; |
Lei YU | dd68d4a | 2021-03-16 22:17:23 +0800 | [diff] [blame] | 338 | return false; |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // iterate through all found eth files, and save ifindex |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 342 | for (const fs::path& fileName : files) |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 343 | { |
| 344 | if (debugLanLeash) |
| 345 | { |
| 346 | std::cout << "Reading " << fileName << "\n"; |
| 347 | } |
| 348 | std::ifstream sysFile(fileName); |
| 349 | if (!sysFile.good()) |
| 350 | { |
| 351 | std::cerr << "Failure reading " << fileName << "\n"; |
| 352 | continue; |
| 353 | } |
| 354 | std::string line; |
| 355 | getline(sysFile, line); |
| 356 | const uint8_t ifindex = std::stoi(line); |
| 357 | // pathSuffix is ASCII of ifindex |
| 358 | const std::string& pathSuffix = std::to_string(ifindex + 30); |
| 359 | |
| 360 | // extract ethNum |
| 361 | const std::string& fileStr = fileName.string(); |
| 362 | const int pos = fileStr.find("eth"); |
| 363 | const std::string& ethNumStr = fileStr.substr(pos + 3); |
| 364 | int ethNum = 0; |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 365 | std::from_chars_result r = std::from_chars( |
| 366 | ethNumStr.data(), ethNumStr.data() + ethNumStr.size(), ethNum); |
| 367 | if (r.ec != std::errc()) |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 368 | { |
| 369 | std::cerr << "invalid ethNum string: " << ethNumStr << "\n"; |
| 370 | continue; |
| 371 | } |
| 372 | |
| 373 | // save pathSuffix |
| 374 | pathSuffixMap[pathSuffix] = ethNum; |
| 375 | if (debugLanLeash) |
| 376 | { |
| 377 | std::cout << "ethNum = " << std::to_string(ethNum) |
| 378 | << ", ifindex = " << line |
| 379 | << ", pathSuffix = " << pathSuffix << "\n"; |
| 380 | } |
| 381 | |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 382 | // init lan connected status from networkd |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 383 | conn->async_method_call( |
| 384 | [ethNum](boost::system::error_code ec, |
| 385 | const std::variant<std::string>& property) { |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 386 | lanStatusMap[ethNum] = false; |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 387 | if (ec) |
| 388 | { |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 389 | std::cerr << "Error reading init status of eth" << ethNum |
| 390 | << "\n"; |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 391 | return; |
| 392 | } |
| 393 | const std::string* pState = std::get_if<std::string>(&property); |
| 394 | if (pState == nullptr) |
| 395 | { |
| 396 | std::cerr << "Unable to read lan status value\n"; |
| 397 | return; |
| 398 | } |
| 399 | bool isLanConnected = |
| 400 | (*pState == "routable" || *pState == "carrier" || |
| 401 | *pState == "degraded"); |
| 402 | if (debugLanLeash) |
| 403 | { |
| 404 | std::cout << "ethNum = " << std::to_string(ethNum) |
| 405 | << ", init LAN status = " |
| 406 | << (isLanConnected ? "true" : "false") << "\n"; |
| 407 | } |
| 408 | lanStatusMap[ethNum] = isLanConnected; |
| 409 | }, |
| 410 | "org.freedesktop.network1", |
| 411 | "/org/freedesktop/network1/link/_" + pathSuffix, |
| 412 | "org.freedesktop.DBus.Properties", "Get", |
| 413 | "org.freedesktop.network1.Link", "OperationalState"); |
| 414 | } |
Lei YU | dd68d4a | 2021-03-16 22:17:23 +0800 | [diff] [blame] | 415 | return true; |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 416 | } |
| 417 | |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 418 | int main() |
| 419 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 420 | int busId = -1; |
| 421 | int slaveAddr = -1; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 422 | bool gpioInverted = false; |
| 423 | IntrusionSensorType type = IntrusionSensorType::gpio; |
| 424 | |
| 425 | // setup connection to dbus |
| 426 | boost::asio::io_service io; |
| 427 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 428 | auto objServer = sdbusplus::asio::object_server(systemBus); |
| 429 | |
| 430 | // setup object server, define interface |
| 431 | systemBus->request_name("xyz.openbmc_project.IntrusionSensor"); |
| 432 | |
| 433 | std::shared_ptr<sdbusplus::asio::dbus_interface> ifaceChassis = |
| 434 | objServer.add_interface( |
| 435 | "/xyz/openbmc_project/Intrusion/Chassis_Intrusion", |
| 436 | "xyz.openbmc_project.Chassis.Intrusion"); |
| 437 | |
| 438 | ChassisIntrusionSensor chassisIntrusionSensor(io, ifaceChassis); |
| 439 | |
| 440 | if (getIntrusionSensorConfig(systemBus, &type, &busId, &slaveAddr, |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 441 | &gpioInverted)) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 442 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 443 | chassisIntrusionSensor.start(type, busId, slaveAddr, gpioInverted); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | // callback to handle configuration change |
| 447 | std::function<void(sdbusplus::message::message&)> eventHandler = |
| 448 | [&](sdbusplus::message::message& message) { |
| 449 | if (message.is_method_error()) |
| 450 | { |
| 451 | std::cerr << "callback method error\n"; |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | std::cout << "rescan due to configuration change \n"; |
| 456 | if (getIntrusionSensorConfig(systemBus, &type, &busId, &slaveAddr, |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 457 | &gpioInverted)) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 458 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 459 | chassisIntrusionSensor.start(type, busId, slaveAddr, |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 460 | gpioInverted); |
| 461 | } |
| 462 | }; |
| 463 | |
Lei YU | dd68d4a | 2021-03-16 22:17:23 +0800 | [diff] [blame] | 464 | auto eventMatch = std::make_unique<sdbusplus::bus::match::match>( |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 465 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 466 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 467 | std::string(inventoryPath) + "',arg0namespace='" + sensorType + "'", |
| 468 | eventHandler); |
| 469 | |
Lei YU | dd68d4a | 2021-03-16 22:17:23 +0800 | [diff] [blame] | 470 | if (initializeLanStatus(systemBus)) |
| 471 | { |
| 472 | // add match to monitor lan status change |
| 473 | sdbusplus::bus::match::match lanStatusMatch( |
| 474 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 475 | "type='signal', member='PropertiesChanged'," |
| 476 | "arg0namespace='org.freedesktop.network1.Link'", |
| 477 | [](sdbusplus::message::message& msg) { |
| 478 | processLanStatusChange(msg); |
| 479 | }); |
| 480 | |
| 481 | // add match to monitor entity manager signal about nic name config |
| 482 | // change |
| 483 | sdbusplus::bus::match::match lanConfigMatch( |
| 484 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 485 | "type='signal', member='PropertiesChanged',path_namespace='" + |
| 486 | std::string(inventoryPath) + "',arg0namespace='" + nicType + |
| 487 | "'", |
| 488 | [&systemBus](sdbusplus::message::message& msg) { |
| 489 | if (msg.is_method_error()) |
| 490 | { |
| 491 | std::cerr << "callback method error\n"; |
| 492 | return; |
| 493 | } |
| 494 | getNicNameInfo(systemBus); |
| 495 | }); |
| 496 | } |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 497 | |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 498 | io.run(); |
| 499 | |
| 500 | return 0; |
| 501 | } |