blob: c94727aaf16f7114f75fccaa302c9c59f196cb6d [file] [log] [blame]
Qiang XUe28d1fa2019-02-27 13:50:56 +08001/*
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 Jefferye73bd0a2023-01-25 10:39:57 +103017#include "ChassisIntrusionSensor.hpp"
18#include "Utils.hpp"
19
Ed Tanouseacbfdd2024-04-04 12:00:24 -070020#include <boost/asio/error.hpp>
Ed Tanous1f978632023-02-28 18:16:39 -080021#include <boost/asio/io_context.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070022#include <boost/asio/steady_timer.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <boost/container/flat_map.hpp>
Patrick Williams0c42f402021-08-27 16:05:45 -050024#include <phosphor-logging/lg2.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080025#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080027#include <sdbusplus/bus.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070029#include <sdbusplus/message.hpp>
James Feist38fb5982020-05-28 10:09:54 -070030
31#include <array>
Ed Tanous8a57ec02020-10-09 12:46:52 -070032#include <charconv>
James Feist38fb5982020-05-28 10:09:54 -070033#include <chrono>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070034#include <cstdint>
James Feist38fb5982020-05-28 10:09:54 -070035#include <ctime>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070036#include <exception>
37#include <filesystem>
James Feist38fb5982020-05-28 10:09:54 -070038#include <fstream>
39#include <functional>
40#include <iostream>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070041#include <map>
James Feist38fb5982020-05-28 10:09:54 -070042#include <memory>
Patrick Venture96e97db2019-10-31 13:44:38 -070043#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070044#include <system_error>
Patrick Venture96e97db2019-10-31 13:44:38 -070045#include <utility>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070046#include <variant>
Patrick Venture96e97db2019-10-31 13:44:38 -070047#include <vector>
Qiang XUe28d1fa2019-02-27 13:50:56 +080048
Ed Tanous8a57ec02020-10-09 12:46:52 -070049static constexpr bool debug = false;
Qiang XUe28d1fa2019-02-27 13:50:56 +080050
Zev Weiss054aad82022-08-18 01:37:34 -070051static constexpr const char* sensorType = "ChassisIntrusionSensor";
52static constexpr const char* nicType = "NIC";
Brandon Kim66558232021-11-09 16:53:08 -080053static constexpr auto nicTypes{std::to_array<const char*>({nicType})};
Qiang XUe28d1fa2019-02-27 13:50:56 +080054
Chau Ly95f49932023-04-19 09:44:55 +000055static const std::map<std::string, std::string> compatibleHwmonNames = {
56 {"Aspeed2600_Hwmon", "intrusion0_alarm"}
57 // Add compatible strings here for new hwmon intrusion detection
58 // drivers that have different hwmon names but would also like to
59 // use the available Hwmon class.
60};
61
Chau Lycebb28c2022-10-21 10:01:52 +000062static void createSensorsFromConfig(
63 boost::asio::io_context& io, sdbusplus::asio::object_server& objServer,
Lei YUba637932021-03-17 10:35:00 +080064 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Chau Lycebb28c2022-10-21 10:01:52 +000065 std::shared_ptr<ChassisIntrusionSensor>& pSensor)
Qiang XUe28d1fa2019-02-27 13:50:56 +080066{
67 // find matched configuration according to sensor type
68 ManagedObjectType sensorConfigurations;
69 bool useCache = false;
70
71 if (!getSensorConfiguration(sensorType, dbusConnection,
72 sensorConfigurations, useCache))
73 {
74 std::cerr << "error communicating to entity manager\n";
Chau Lycebb28c2022-10-21 10:01:52 +000075 return;
Qiang XUe28d1fa2019-02-27 13:50:56 +080076 }
77
78 const SensorData* sensorData = nullptr;
Zev Weissafd15042022-07-18 12:28:40 -070079 const std::pair<std::string, SensorBaseConfigMap>* baseConfiguration =
80 nullptr;
Qiang XUe28d1fa2019-02-27 13:50:56 +080081
Zev Weissf343b8a2022-08-12 18:21:01 -070082 for (const auto& [path, cfgData] : sensorConfigurations)
Qiang XUe28d1fa2019-02-27 13:50:56 +080083 {
84 baseConfiguration = nullptr;
Zev Weissf343b8a2022-08-12 18:21:01 -070085 sensorData = &cfgData;
Qiang XUe28d1fa2019-02-27 13:50:56 +080086
87 // match sensor type
Zev Weiss054aad82022-08-18 01:37:34 -070088 auto sensorBase = sensorData->find(configInterfaceName(sensorType));
Qiang XUe28d1fa2019-02-27 13:50:56 +080089 if (sensorBase == sensorData->end())
90 {
91 std::cerr << "error finding base configuration \n";
92 continue;
93 }
94
95 baseConfiguration = &(*sensorBase);
96
Chau Lyb318dca2022-10-26 04:12:52 +000097 // Rearm defaults to "Automatic" mode
98 bool autoRearm = true;
99 auto findRearm = baseConfiguration->second.find("Rearm");
100 if (findRearm != baseConfiguration->second.end())
101 {
102 std::string rearmStr = std::get<std::string>(findRearm->second);
103 if (rearmStr != "Automatic" && rearmStr != "Manual")
104 {
105 std::cerr << "Wrong input for Rearm parameter\n";
106 continue;
107 }
108 autoRearm = (rearmStr == "Automatic");
109 }
110
Chau Ly95f49932023-04-19 09:44:55 +0000111 // judge class, "Gpio", "Hwmon" or "I2C"
Qiang XUe28d1fa2019-02-27 13:50:56 +0800112 auto findClass = baseConfiguration->second.find("Class");
Chau Ly95f49932023-04-19 09:44:55 +0000113 if (findClass != baseConfiguration->second.end())
Qiang XUe28d1fa2019-02-27 13:50:56 +0800114 {
Chau Ly95f49932023-04-19 09:44:55 +0000115 auto classString = std::get<std::string>(findClass->second);
116 if (classString == "Gpio")
117 {
118 auto findGpioPolarity =
119 baseConfiguration->second.find("GpioPolarity");
Qiang XUe28d1fa2019-02-27 13:50:56 +0800120
Chau Ly95f49932023-04-19 09:44:55 +0000121 if (findGpioPolarity == baseConfiguration->second.end())
122 {
123 std::cerr
124 << "error finding gpio polarity in configuration \n";
125 continue;
126 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800127
Chau Ly95f49932023-04-19 09:44:55 +0000128 try
Chau Lycebb28c2022-10-21 10:01:52 +0000129 {
Chau Ly95f49932023-04-19 09:44:55 +0000130 bool gpioInverted =
131 (std::get<std::string>(findGpioPolarity->second) ==
132 "Low");
133 pSensor = std::make_shared<ChassisIntrusionGpioSensor>(
Chau Lyb318dca2022-10-26 04:12:52 +0000134 autoRearm, io, objServer, gpioInverted);
Chau Ly95f49932023-04-19 09:44:55 +0000135 pSensor->start();
136 if (debug)
137 {
138 std::cout
139 << "find chassis intrusion sensor polarity inverted "
140 "flag is "
141 << gpioInverted << "\n";
142 }
143 return;
Chau Lycebb28c2022-10-21 10:01:52 +0000144 }
Chau Ly95f49932023-04-19 09:44:55 +0000145 catch (const std::bad_variant_access& e)
Chau Lycebb28c2022-10-21 10:01:52 +0000146 {
Chau Ly95f49932023-04-19 09:44:55 +0000147 std::cerr << "invalid value for gpio info in config. \n";
148 continue;
Chau Lycebb28c2022-10-21 10:01:52 +0000149 }
Chau Ly95f49932023-04-19 09:44:55 +0000150 catch (const std::exception& e)
151 {
152 std::cerr << e.what() << std::endl;
153 continue;
154 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800155 }
Chau Ly95f49932023-04-19 09:44:55 +0000156 // If class string contains Hwmon string
157 else if (classString.find("Hwmon") != std::string::npos)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800158 {
Chau Ly95f49932023-04-19 09:44:55 +0000159 std::string hwmonName;
160 std::map<std::string, std::string>::const_iterator
161 compatIterator = compatibleHwmonNames.find(classString);
162
163 if (compatIterator == compatibleHwmonNames.end())
164 {
165 std::cerr << "Hwmon Class string is not supported\n";
166 continue;
167 }
168
169 hwmonName = compatIterator->second;
170
171 try
172 {
173 pSensor = std::make_shared<ChassisIntrusionHwmonSensor>(
Chau Lyb318dca2022-10-26 04:12:52 +0000174 autoRearm, io, objServer, hwmonName);
Chau Ly95f49932023-04-19 09:44:55 +0000175 pSensor->start();
176 return;
177 }
178 catch (const std::exception& e)
179 {
180 std::cerr << e.what() << std::endl;
181 continue;
182 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800183 }
Chau Ly95f49932023-04-19 09:44:55 +0000184 else
Qiang XUe28d1fa2019-02-27 13:50:56 +0800185 {
Chau Ly95f49932023-04-19 09:44:55 +0000186 auto findBus = baseConfiguration->second.find("Bus");
187 auto findAddress = baseConfiguration->second.find("Address");
188 if (findBus == baseConfiguration->second.end() ||
189 findAddress == baseConfiguration->second.end())
190 {
191 std::cerr
192 << "error finding bus or address in configuration \n";
193 continue;
194 }
195 try
196 {
197 int busId = std::get<uint64_t>(findBus->second);
198 int slaveAddr = std::get<uint64_t>(findAddress->second);
199 pSensor = std::make_shared<ChassisIntrusionPchSensor>(
Chau Lyb318dca2022-10-26 04:12:52 +0000200 autoRearm, io, objServer, busId, slaveAddr);
Chau Ly95f49932023-04-19 09:44:55 +0000201 pSensor->start();
202 if (debug)
203 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400204 std::cout
205 << "find matched bus " << busId
206 << ", matched slave addr " << slaveAddr << "\n";
Chau Ly95f49932023-04-19 09:44:55 +0000207 }
208 return;
209 }
210 catch (const std::bad_variant_access& e)
211 {
212 std::cerr
213 << "invalid value for bus or address in config. \n";
214 continue;
215 }
216 catch (const std::exception& e)
217 {
218 std::cerr << e.what() << std::endl;
219 continue;
220 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800221 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800222 }
223 }
224
Chau Ly95f49932023-04-19 09:44:55 +0000225 std::cerr << " Can't find matched I2C, GPIO or Hwmon configuration\n";
Chau Lycebb28c2022-10-21 10:01:52 +0000226
227 // Make sure nothing runs when there's failure in configuration for the
228 // sensor after rescan
229 if (pSensor)
230 {
231 std::cerr << " Reset the occupied sensor pointer\n";
232 pSensor = nullptr;
233 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800234}
235
Qiang XU88b7f282019-08-14 22:51:43 +0800236static constexpr bool debugLanLeash = false;
237boost::container::flat_map<int, bool> lanStatusMap;
Qiang XU74ddf862019-09-12 17:12:13 +0800238boost::container::flat_map<int, std::string> lanInfoMap;
Qiang XU88b7f282019-08-14 22:51:43 +0800239boost::container::flat_map<std::string, int> pathSuffixMap;
240
Lei YUba637932021-03-17 10:35:00 +0800241static void getNicNameInfo(
242 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
Qiang XU74ddf862019-09-12 17:12:13 +0800243{
244 auto getter = std::make_shared<GetSensorConfiguration>(
Patrick Williams597e8422023-10-20 11:19:01 -0500245 dbusConnection, [](const ManagedObjectType& sensorConfigurations) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400246 // Get NIC name and save to map
247 lanInfoMap.clear();
248 for (const auto& [path, cfgData] : sensorConfigurations)
Qiang XU74ddf862019-09-12 17:12:13 +0800249 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400250 const std::pair<std::string, SensorBaseConfigMap>*
251 baseConfiguration = nullptr;
Qiang XU74ddf862019-09-12 17:12:13 +0800252
Patrick Williams2aaf7172024-08-16 15:20:40 -0400253 // find base configuration
254 auto sensorBase = cfgData.find(configInterfaceName(nicType));
255 if (sensorBase == cfgData.end())
Qiang XU74ddf862019-09-12 17:12:13 +0800256 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400257 continue;
258 }
259 baseConfiguration = &(*sensorBase);
260
261 auto findEthIndex = baseConfiguration->second.find("EthIndex");
262 auto findName = baseConfiguration->second.find("Name");
263
264 if (findEthIndex != baseConfiguration->second.end() &&
265 findName != baseConfiguration->second.end())
266 {
267 const auto* pEthIndex =
268 std::get_if<uint64_t>(&findEthIndex->second);
269 const auto* pName =
270 std::get_if<std::string>(&findName->second);
271 if (pEthIndex != nullptr && pName != nullptr)
Qiang XU74ddf862019-09-12 17:12:13 +0800272 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400273 lanInfoMap[*pEthIndex] = *pName;
274 if (debugLanLeash)
275 {
276 std::cout << "find name of eth" << *pEthIndex
277 << " is " << *pName << "\n";
278 }
Qiang XU74ddf862019-09-12 17:12:13 +0800279 }
280 }
281 }
282
Patrick Williams2aaf7172024-08-16 15:20:40 -0400283 if (lanInfoMap.empty())
284 {
285 std::cerr << "can't find matched NIC name. \n";
286 }
287 });
Qiang XU74ddf862019-09-12 17:12:13 +0800288
289 getter->getConfiguration(
290 std::vector<std::string>{nicTypes.begin(), nicTypes.end()});
291}
292
Patrick Williams92f8f512022-07-22 19:26:55 -0500293static void processLanStatusChange(sdbusplus::message_t& message)
Qiang XU88b7f282019-08-14 22:51:43 +0800294{
295 const std::string& pathName = message.get_path();
296 std::string interfaceName;
Zev Weissafd15042022-07-18 12:28:40 -0700297 SensorBaseConfigMap properties;
Qiang XU88b7f282019-08-14 22:51:43 +0800298 message.read(interfaceName, properties);
299
300 auto findStateProperty = properties.find("OperationalState");
301 if (findStateProperty == properties.end())
302 {
303 return;
304 }
Qiang XU74ddf862019-09-12 17:12:13 +0800305 std::string* pState =
306 std::get_if<std::string>(&(findStateProperty->second));
Qiang XU88b7f282019-08-14 22:51:43 +0800307 if (pState == nullptr)
308 {
309 std::cerr << "invalid OperationalState \n";
310 return;
311 }
312
313 bool newLanConnected = (*pState == "routable" || *pState == "carrier" ||
314 *pState == "degraded");
315
316 // get ethNum from path. /org/freedesktop/network1/link/_32 for eth0
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500317 size_t pos = pathName.find("/_");
Qiang XU88b7f282019-08-14 22:51:43 +0800318 if (pos == std::string::npos || pathName.length() <= pos + 2)
319 {
320 std::cerr << "unexpected path name " << pathName << "\n";
321 return;
322 }
323 std::string suffixStr = pathName.substr(pos + 2);
324
325 auto findEthNum = pathSuffixMap.find(suffixStr);
326 if (findEthNum == pathSuffixMap.end())
327 {
328 std::cerr << "unexpected eth for suffixStr " << suffixStr << "\n";
329 return;
330 }
331 int ethNum = findEthNum->second;
Qiang XU74ddf862019-09-12 17:12:13 +0800332
333 // get lan status from map
Qiang XU88b7f282019-08-14 22:51:43 +0800334 auto findLanStatus = lanStatusMap.find(ethNum);
335 if (findLanStatus == lanStatusMap.end())
336 {
337 std::cerr << "unexpected eth " << ethNum << " in lanStatusMap \n";
338 return;
339 }
340 bool oldLanConnected = findLanStatus->second;
341
Qiang XU74ddf862019-09-12 17:12:13 +0800342 // get lan info from map
Ed Tanous2049bd22022-07-09 07:20:26 -0700343 std::string lanInfo;
344 if (!lanInfoMap.empty())
Qiang XU74ddf862019-09-12 17:12:13 +0800345 {
346 auto findLanInfo = lanInfoMap.find(ethNum);
347 if (findLanInfo == lanInfoMap.end())
348 {
349 std::cerr << "unexpected eth " << ethNum << " in lanInfoMap \n";
350 }
351 else
352 {
353 lanInfo = "(" + findLanInfo->second + ")";
354 }
355 }
356
Qiang XU88b7f282019-08-14 22:51:43 +0800357 if (debugLanLeash)
358 {
359 std::cout << "ethNum = " << ethNum << ", state = " << *pState
360 << ", oldLanConnected = "
361 << (oldLanConnected ? "true" : "false")
362 << ", newLanConnected = "
363 << (newLanConnected ? "true" : "false") << "\n";
364 }
365
366 if (oldLanConnected != newLanConnected)
367 {
Qiang XU74ddf862019-09-12 17:12:13 +0800368 std::string strEthNum = "eth" + std::to_string(ethNum) + lanInfo;
Ed Tanous2049bd22022-07-09 07:20:26 -0700369 const auto* strState = newLanConnected ? "connected" : "lost";
Patrick Williams2aaf7172024-08-16 15:20:40 -0400370 const auto* strMsgId =
371 newLanConnected ? "OpenBMC.0.1.LanRegained" : "OpenBMC.0.1.LanLost";
Patrick Williams0c42f402021-08-27 16:05:45 -0500372
373 lg2::info("{ETHDEV} LAN leash {STATE}", "ETHDEV", strEthNum, "STATE",
374 strState, "REDFISH_MESSAGE_ID", strMsgId,
375 "REDFISH_MESSAGE_ARGS", strEthNum);
376
Qiang XU88b7f282019-08-14 22:51:43 +0800377 lanStatusMap[ethNum] = newLanConnected;
Qiang XU88b7f282019-08-14 22:51:43 +0800378 }
379}
380
Lei YUdd68d4a2021-03-16 22:17:23 +0800381/** @brief Initialize the lan status.
382 *
383 * @return true on success and false on failure
384 */
Lei YUba637932021-03-17 10:35:00 +0800385static bool initializeLanStatus(
386 const std::shared_ptr<sdbusplus::asio::connection>& conn)
Qiang XU88b7f282019-08-14 22:51:43 +0800387{
Qiang XU74ddf862019-09-12 17:12:13 +0800388 // init lan port name from configuration
389 getNicNameInfo(conn);
390
391 // get eth info from sysfs
Qiang XU88b7f282019-08-14 22:51:43 +0800392 std::vector<fs::path> files;
393 if (!findFiles(fs::path("/sys/class/net/"), R"(eth\d+/ifindex)", files))
394 {
395 std::cerr << "No eth in system\n";
Lei YUdd68d4a2021-03-16 22:17:23 +0800396 return false;
Qiang XU88b7f282019-08-14 22:51:43 +0800397 }
398
399 // iterate through all found eth files, and save ifindex
Ed Tanous8a57ec02020-10-09 12:46:52 -0700400 for (const fs::path& fileName : files)
Qiang XU88b7f282019-08-14 22:51:43 +0800401 {
402 if (debugLanLeash)
403 {
404 std::cout << "Reading " << fileName << "\n";
405 }
406 std::ifstream sysFile(fileName);
407 if (!sysFile.good())
408 {
409 std::cerr << "Failure reading " << fileName << "\n";
410 continue;
411 }
412 std::string line;
413 getline(sysFile, line);
414 const uint8_t ifindex = std::stoi(line);
415 // pathSuffix is ASCII of ifindex
416 const std::string& pathSuffix = std::to_string(ifindex + 30);
417
418 // extract ethNum
419 const std::string& fileStr = fileName.string();
420 const int pos = fileStr.find("eth");
421 const std::string& ethNumStr = fileStr.substr(pos + 3);
422 int ethNum = 0;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700423 std::from_chars_result r = std::from_chars(
424 ethNumStr.data(), ethNumStr.data() + ethNumStr.size(), ethNum);
425 if (r.ec != std::errc())
Qiang XU88b7f282019-08-14 22:51:43 +0800426 {
427 std::cerr << "invalid ethNum string: " << ethNumStr << "\n";
428 continue;
429 }
430
431 // save pathSuffix
432 pathSuffixMap[pathSuffix] = ethNum;
433 if (debugLanLeash)
434 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400435 std::cout << "ethNum = " << std::to_string(ethNum) << ", ifindex = "
436 << line << ", pathSuffix = " << pathSuffix << "\n";
Qiang XU88b7f282019-08-14 22:51:43 +0800437 }
438
Qiang XU74ddf862019-09-12 17:12:13 +0800439 // init lan connected status from networkd
Qiang XU88b7f282019-08-14 22:51:43 +0800440 conn->async_method_call(
441 [ethNum](boost::system::error_code ec,
442 const std::variant<std::string>& property) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400443 lanStatusMap[ethNum] = false;
444 if (ec)
445 {
446 std::cerr
447 << "Error reading init status of eth" << ethNum << "\n";
448 return;
449 }
450 const std::string* pState = std::get_if<std::string>(&property);
451 if (pState == nullptr)
452 {
453 std::cerr << "Unable to read lan status value\n";
454 return;
455 }
456 bool isLanConnected =
457 (*pState == "routable" || *pState == "carrier" ||
458 *pState == "degraded");
459 if (debugLanLeash)
460 {
461 std::cout << "ethNum = " << std::to_string(ethNum)
462 << ", init LAN status = "
463 << (isLanConnected ? "true" : "false") << "\n";
464 }
465 lanStatusMap[ethNum] = isLanConnected;
466 },
Qiang XU88b7f282019-08-14 22:51:43 +0800467 "org.freedesktop.network1",
468 "/org/freedesktop/network1/link/_" + pathSuffix,
469 "org.freedesktop.DBus.Properties", "Get",
470 "org.freedesktop.network1.Link", "OperationalState");
471 }
Lei YUdd68d4a2021-03-16 22:17:23 +0800472 return true;
Qiang XU88b7f282019-08-14 22:51:43 +0800473}
474
Qiang XUe28d1fa2019-02-27 13:50:56 +0800475int main()
476{
Chau Lycebb28c2022-10-21 10:01:52 +0000477 std::shared_ptr<ChassisIntrusionSensor> intrusionSensor;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800478
479 // setup connection to dbus
Ed Tanous1f978632023-02-28 18:16:39 -0800480 boost::asio::io_context io;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800481 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800482
483 // setup object server, define interface
484 systemBus->request_name("xyz.openbmc_project.IntrusionSensor");
485
Ed Tanous14ed5e92022-07-12 15:50:23 -0700486 sdbusplus::asio::object_server objServer(systemBus, true);
487
Chau Ly889af822023-02-20 07:13:52 +0000488 objServer.add_manager("/xyz/openbmc_project/Chassis");
Ed Tanous14ed5e92022-07-12 15:50:23 -0700489
Chau Lycebb28c2022-10-21 10:01:52 +0000490 createSensorsFromConfig(io, objServer, systemBus, intrusionSensor);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800491
492 // callback to handle configuration change
Ian Woloschin0df80a12023-11-06 14:27:44 +0000493 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500494 std::function<void(sdbusplus::message_t&)> eventHandler =
495 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400496 if (message.is_method_error())
Ian Woloschin0df80a12023-11-06 14:27:44 +0000497 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400498 std::cerr << "callback method error\n";
Ian Woloschin0df80a12023-11-06 14:27:44 +0000499 return;
500 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400501 // this implicitly cancels the timer
502 filterTimer.expires_after(std::chrono::seconds(1));
503 filterTimer.async_wait([&](const boost::system::error_code& ec) {
504 if (ec == boost::asio::error::operation_aborted)
505 {
506 // timer was cancelled
507 return;
508 }
509 std::cout << "rescan due to configuration change \n";
510 createSensorsFromConfig(io, objServer, systemBus,
511 intrusionSensor);
512 });
513 };
Qiang XUe28d1fa2019-02-27 13:50:56 +0800514
Zev Weiss214d9712022-08-12 12:54:31 -0700515 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
516 setupPropertiesChangedMatches(
517 *systemBus, std::to_array<const char*>({sensorType}), eventHandler);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800518
Lei YUdd68d4a2021-03-16 22:17:23 +0800519 if (initializeLanStatus(systemBus))
520 {
521 // add match to monitor lan status change
Patrick Williams92f8f512022-07-22 19:26:55 -0500522 sdbusplus::bus::match_t lanStatusMatch(
523 static_cast<sdbusplus::bus_t&>(*systemBus),
Lei YUdd68d4a2021-03-16 22:17:23 +0800524 "type='signal', member='PropertiesChanged',"
525 "arg0namespace='org.freedesktop.network1.Link'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500526 [](sdbusplus::message_t& msg) { processLanStatusChange(msg); });
Lei YUdd68d4a2021-03-16 22:17:23 +0800527
528 // add match to monitor entity manager signal about nic name config
529 // change
Patrick Williams92f8f512022-07-22 19:26:55 -0500530 sdbusplus::bus::match_t lanConfigMatch(
531 static_cast<sdbusplus::bus_t&>(*systemBus),
Lei YUdd68d4a2021-03-16 22:17:23 +0800532 "type='signal', member='PropertiesChanged',path_namespace='" +
Zev Weiss054aad82022-08-18 01:37:34 -0700533 std::string(inventoryPath) + "',arg0namespace='" +
534 configInterfaceName(nicType) + "'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500535 [&systemBus](sdbusplus::message_t& msg) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400536 if (msg.is_method_error())
537 {
538 std::cerr << "callback method error\n";
539 return;
540 }
541 getNicNameInfo(systemBus);
542 });
Lei YUdd68d4a2021-03-16 22:17:23 +0800543 }
Qiang XU88b7f282019-08-14 22:51:43 +0800544
Qiang XUe28d1fa2019-02-27 13:50:56 +0800545 io.run();
546
547 return 0;
548}