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