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