blob: 4566b08ad7e7dfa3b45115e8d64ebd63f1da6dcc [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 Tanous1f978632023-02-28 18:16:39 -080020#include <boost/asio/io_context.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070021#include <boost/container/flat_map.hpp>
Patrick Williams0c42f402021-08-27 16:05:45 -050022#include <phosphor-logging/lg2.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080025#include <sdbusplus/bus.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070026#include <sdbusplus/bus/match.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080027#include <sdbusplus/exception.hpp>
28#include <sdbusplus/server.hpp>
29#include <sdbusplus/timer.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>
34#include <ctime>
35#include <fstream>
36#include <functional>
37#include <iostream>
38#include <memory>
Patrick Venture96e97db2019-10-31 13:44:38 -070039#include <stdexcept>
40#include <string>
41#include <utility>
42#include <vector>
Qiang XUe28d1fa2019-02-27 13:50:56 +080043
Ed Tanous8a57ec02020-10-09 12:46:52 -070044static constexpr bool debug = false;
Qiang XUe28d1fa2019-02-27 13:50:56 +080045
Zev Weiss054aad82022-08-18 01:37:34 -070046static constexpr const char* sensorType = "ChassisIntrusionSensor";
47static constexpr const char* nicType = "NIC";
Brandon Kim66558232021-11-09 16:53:08 -080048static constexpr auto nicTypes{std::to_array<const char*>({nicType})};
Qiang XUe28d1fa2019-02-27 13:50:56 +080049
Chau Ly95f49932023-04-19 09:44:55 +000050static const std::map<std::string, std::string> compatibleHwmonNames = {
51 {"Aspeed2600_Hwmon", "intrusion0_alarm"}
52 // Add compatible strings here for new hwmon intrusion detection
53 // drivers that have different hwmon names but would also like to
54 // use the available Hwmon class.
55};
56
Chau Lycebb28c2022-10-21 10:01:52 +000057static void createSensorsFromConfig(
58 boost::asio::io_context& io, sdbusplus::asio::object_server& objServer,
Lei YUba637932021-03-17 10:35:00 +080059 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Chau Lycebb28c2022-10-21 10:01:52 +000060 std::shared_ptr<ChassisIntrusionSensor>& pSensor)
Qiang XUe28d1fa2019-02-27 13:50:56 +080061{
62 // find matched configuration according to sensor type
63 ManagedObjectType sensorConfigurations;
64 bool useCache = false;
65
66 if (!getSensorConfiguration(sensorType, dbusConnection,
67 sensorConfigurations, useCache))
68 {
69 std::cerr << "error communicating to entity manager\n";
Chau Lycebb28c2022-10-21 10:01:52 +000070 return;
Qiang XUe28d1fa2019-02-27 13:50:56 +080071 }
72
73 const SensorData* sensorData = nullptr;
Zev Weissafd15042022-07-18 12:28:40 -070074 const std::pair<std::string, SensorBaseConfigMap>* baseConfiguration =
75 nullptr;
Qiang XUe28d1fa2019-02-27 13:50:56 +080076
Zev Weissf343b8a2022-08-12 18:21:01 -070077 for (const auto& [path, cfgData] : sensorConfigurations)
Qiang XUe28d1fa2019-02-27 13:50:56 +080078 {
79 baseConfiguration = nullptr;
Zev Weissf343b8a2022-08-12 18:21:01 -070080 sensorData = &cfgData;
Qiang XUe28d1fa2019-02-27 13:50:56 +080081
82 // match sensor type
Zev Weiss054aad82022-08-18 01:37:34 -070083 auto sensorBase = sensorData->find(configInterfaceName(sensorType));
Qiang XUe28d1fa2019-02-27 13:50:56 +080084 if (sensorBase == sensorData->end())
85 {
86 std::cerr << "error finding base configuration \n";
87 continue;
88 }
89
90 baseConfiguration = &(*sensorBase);
91
Chau Lyb318dca2022-10-26 04:12:52 +000092 // Rearm defaults to "Automatic" mode
93 bool autoRearm = true;
94 auto findRearm = baseConfiguration->second.find("Rearm");
95 if (findRearm != baseConfiguration->second.end())
96 {
97 std::string rearmStr = std::get<std::string>(findRearm->second);
98 if (rearmStr != "Automatic" && rearmStr != "Manual")
99 {
100 std::cerr << "Wrong input for Rearm parameter\n";
101 continue;
102 }
103 autoRearm = (rearmStr == "Automatic");
104 }
105
Chau Ly95f49932023-04-19 09:44:55 +0000106 // judge class, "Gpio", "Hwmon" or "I2C"
Qiang XUe28d1fa2019-02-27 13:50:56 +0800107 auto findClass = baseConfiguration->second.find("Class");
Chau Ly95f49932023-04-19 09:44:55 +0000108 if (findClass != baseConfiguration->second.end())
Qiang XUe28d1fa2019-02-27 13:50:56 +0800109 {
Chau Ly95f49932023-04-19 09:44:55 +0000110 auto classString = std::get<std::string>(findClass->second);
111 if (classString == "Gpio")
112 {
113 auto findGpioPolarity =
114 baseConfiguration->second.find("GpioPolarity");
Qiang XUe28d1fa2019-02-27 13:50:56 +0800115
Chau Ly95f49932023-04-19 09:44:55 +0000116 if (findGpioPolarity == baseConfiguration->second.end())
117 {
118 std::cerr
119 << "error finding gpio polarity in configuration \n";
120 continue;
121 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800122
Chau Ly95f49932023-04-19 09:44:55 +0000123 try
Chau Lycebb28c2022-10-21 10:01:52 +0000124 {
Chau Ly95f49932023-04-19 09:44:55 +0000125 bool gpioInverted =
126 (std::get<std::string>(findGpioPolarity->second) ==
127 "Low");
128 pSensor = std::make_shared<ChassisIntrusionGpioSensor>(
Chau Lyb318dca2022-10-26 04:12:52 +0000129 autoRearm, io, objServer, gpioInverted);
Chau Ly95f49932023-04-19 09:44:55 +0000130 pSensor->start();
131 if (debug)
132 {
133 std::cout
134 << "find chassis intrusion sensor polarity inverted "
135 "flag is "
136 << gpioInverted << "\n";
137 }
138 return;
Chau Lycebb28c2022-10-21 10:01:52 +0000139 }
Chau Ly95f49932023-04-19 09:44:55 +0000140 catch (const std::bad_variant_access& e)
Chau Lycebb28c2022-10-21 10:01:52 +0000141 {
Chau Ly95f49932023-04-19 09:44:55 +0000142 std::cerr << "invalid value for gpio info in config. \n";
143 continue;
Chau Lycebb28c2022-10-21 10:01:52 +0000144 }
Chau Ly95f49932023-04-19 09:44:55 +0000145 catch (const std::exception& e)
146 {
147 std::cerr << e.what() << std::endl;
148 continue;
149 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800150 }
Chau Ly95f49932023-04-19 09:44:55 +0000151 // If class string contains Hwmon string
152 else if (classString.find("Hwmon") != std::string::npos)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800153 {
Chau Ly95f49932023-04-19 09:44:55 +0000154 std::string hwmonName;
155 std::map<std::string, std::string>::const_iterator
156 compatIterator = compatibleHwmonNames.find(classString);
157
158 if (compatIterator == compatibleHwmonNames.end())
159 {
160 std::cerr << "Hwmon Class string is not supported\n";
161 continue;
162 }
163
164 hwmonName = compatIterator->second;
165
166 try
167 {
168 pSensor = std::make_shared<ChassisIntrusionHwmonSensor>(
Chau Lyb318dca2022-10-26 04:12:52 +0000169 autoRearm, io, objServer, hwmonName);
Chau Ly95f49932023-04-19 09:44:55 +0000170 pSensor->start();
171 return;
172 }
173 catch (const std::exception& e)
174 {
175 std::cerr << e.what() << std::endl;
176 continue;
177 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800178 }
Chau Ly95f49932023-04-19 09:44:55 +0000179 else
Qiang XUe28d1fa2019-02-27 13:50:56 +0800180 {
Chau Ly95f49932023-04-19 09:44:55 +0000181 auto findBus = baseConfiguration->second.find("Bus");
182 auto findAddress = baseConfiguration->second.find("Address");
183 if (findBus == baseConfiguration->second.end() ||
184 findAddress == baseConfiguration->second.end())
185 {
186 std::cerr
187 << "error finding bus or address in configuration \n";
188 continue;
189 }
190 try
191 {
192 int busId = std::get<uint64_t>(findBus->second);
193 int slaveAddr = std::get<uint64_t>(findAddress->second);
194 pSensor = std::make_shared<ChassisIntrusionPchSensor>(
Chau Lyb318dca2022-10-26 04:12:52 +0000195 autoRearm, io, objServer, busId, slaveAddr);
Chau Ly95f49932023-04-19 09:44:55 +0000196 pSensor->start();
197 if (debug)
198 {
199 std::cout << "find matched bus " << busId
200 << ", matched slave addr " << slaveAddr
201 << "\n";
202 }
203 return;
204 }
205 catch (const std::bad_variant_access& e)
206 {
207 std::cerr
208 << "invalid value for bus or address in config. \n";
209 continue;
210 }
211 catch (const std::exception& e)
212 {
213 std::cerr << e.what() << std::endl;
214 continue;
215 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800216 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800217 }
218 }
219
Chau Ly95f49932023-04-19 09:44:55 +0000220 std::cerr << " Can't find matched I2C, GPIO or Hwmon configuration\n";
Chau Lycebb28c2022-10-21 10:01:52 +0000221
222 // Make sure nothing runs when there's failure in configuration for the
223 // sensor after rescan
224 if (pSensor)
225 {
226 std::cerr << " Reset the occupied sensor pointer\n";
227 pSensor = nullptr;
228 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800229}
230
Qiang XU88b7f282019-08-14 22:51:43 +0800231static constexpr bool debugLanLeash = false;
232boost::container::flat_map<int, bool> lanStatusMap;
Qiang XU74ddf862019-09-12 17:12:13 +0800233boost::container::flat_map<int, std::string> lanInfoMap;
Qiang XU88b7f282019-08-14 22:51:43 +0800234boost::container::flat_map<std::string, int> pathSuffixMap;
235
Lei YUba637932021-03-17 10:35:00 +0800236static void getNicNameInfo(
237 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
Qiang XU74ddf862019-09-12 17:12:13 +0800238{
239 auto getter = std::make_shared<GetSensorConfiguration>(
Patrick Williams597e8422023-10-20 11:19:01 -0500240 dbusConnection, [](const ManagedObjectType& sensorConfigurations) {
Patrick Williams779c96a2023-05-10 07:50:42 -0500241 // Get NIC name and save to map
242 lanInfoMap.clear();
243 for (const auto& [path, cfgData] : sensorConfigurations)
244 {
245 const std::pair<std::string, SensorBaseConfigMap>*
246 baseConfiguration = nullptr;
247
248 // find base configuration
249 auto sensorBase = cfgData.find(configInterfaceName(nicType));
250 if (sensorBase == cfgData.end())
Qiang XU74ddf862019-09-12 17:12:13 +0800251 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500252 continue;
253 }
254 baseConfiguration = &(*sensorBase);
Qiang XU74ddf862019-09-12 17:12:13 +0800255
Patrick Williams779c96a2023-05-10 07:50:42 -0500256 auto findEthIndex = baseConfiguration->second.find("EthIndex");
257 auto findName = baseConfiguration->second.find("Name");
258
259 if (findEthIndex != baseConfiguration->second.end() &&
260 findName != baseConfiguration->second.end())
261 {
262 const auto* pEthIndex =
263 std::get_if<uint64_t>(&findEthIndex->second);
264 const auto* pName = std::get_if<std::string>(&findName->second);
265 if (pEthIndex != nullptr && pName != nullptr)
Qiang XU74ddf862019-09-12 17:12:13 +0800266 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500267 lanInfoMap[*pEthIndex] = *pName;
268 if (debugLanLeash)
Qiang XU74ddf862019-09-12 17:12:13 +0800269 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500270 std::cout << "find name of eth" << *pEthIndex << " is "
271 << *pName << "\n";
Qiang XU74ddf862019-09-12 17:12:13 +0800272 }
273 }
274 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500275 }
Qiang XU74ddf862019-09-12 17:12:13 +0800276
Patrick Williams779c96a2023-05-10 07:50:42 -0500277 if (lanInfoMap.empty())
278 {
279 std::cerr << "can't find matched NIC name. \n";
280 }
Patrick Williams597e8422023-10-20 11:19:01 -0500281 });
Qiang XU74ddf862019-09-12 17:12:13 +0800282
283 getter->getConfiguration(
284 std::vector<std::string>{nicTypes.begin(), nicTypes.end()});
285}
286
Patrick Williams92f8f512022-07-22 19:26:55 -0500287static void processLanStatusChange(sdbusplus::message_t& message)
Qiang XU88b7f282019-08-14 22:51:43 +0800288{
289 const std::string& pathName = message.get_path();
290 std::string interfaceName;
Zev Weissafd15042022-07-18 12:28:40 -0700291 SensorBaseConfigMap properties;
Qiang XU88b7f282019-08-14 22:51:43 +0800292 message.read(interfaceName, properties);
293
294 auto findStateProperty = properties.find("OperationalState");
295 if (findStateProperty == properties.end())
296 {
297 return;
298 }
Qiang XU74ddf862019-09-12 17:12:13 +0800299 std::string* pState =
300 std::get_if<std::string>(&(findStateProperty->second));
Qiang XU88b7f282019-08-14 22:51:43 +0800301 if (pState == nullptr)
302 {
303 std::cerr << "invalid OperationalState \n";
304 return;
305 }
306
307 bool newLanConnected = (*pState == "routable" || *pState == "carrier" ||
308 *pState == "degraded");
309
310 // get ethNum from path. /org/freedesktop/network1/link/_32 for eth0
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500311 size_t pos = pathName.find("/_");
Qiang XU88b7f282019-08-14 22:51:43 +0800312 if (pos == std::string::npos || pathName.length() <= pos + 2)
313 {
314 std::cerr << "unexpected path name " << pathName << "\n";
315 return;
316 }
317 std::string suffixStr = pathName.substr(pos + 2);
318
319 auto findEthNum = pathSuffixMap.find(suffixStr);
320 if (findEthNum == pathSuffixMap.end())
321 {
322 std::cerr << "unexpected eth for suffixStr " << suffixStr << "\n";
323 return;
324 }
325 int ethNum = findEthNum->second;
Qiang XU74ddf862019-09-12 17:12:13 +0800326
327 // get lan status from map
Qiang XU88b7f282019-08-14 22:51:43 +0800328 auto findLanStatus = lanStatusMap.find(ethNum);
329 if (findLanStatus == lanStatusMap.end())
330 {
331 std::cerr << "unexpected eth " << ethNum << " in lanStatusMap \n";
332 return;
333 }
334 bool oldLanConnected = findLanStatus->second;
335
Qiang XU74ddf862019-09-12 17:12:13 +0800336 // get lan info from map
Ed Tanous2049bd22022-07-09 07:20:26 -0700337 std::string lanInfo;
338 if (!lanInfoMap.empty())
Qiang XU74ddf862019-09-12 17:12:13 +0800339 {
340 auto findLanInfo = lanInfoMap.find(ethNum);
341 if (findLanInfo == lanInfoMap.end())
342 {
343 std::cerr << "unexpected eth " << ethNum << " in lanInfoMap \n";
344 }
345 else
346 {
347 lanInfo = "(" + findLanInfo->second + ")";
348 }
349 }
350
Qiang XU88b7f282019-08-14 22:51:43 +0800351 if (debugLanLeash)
352 {
353 std::cout << "ethNum = " << ethNum << ", state = " << *pState
354 << ", oldLanConnected = "
355 << (oldLanConnected ? "true" : "false")
356 << ", newLanConnected = "
357 << (newLanConnected ? "true" : "false") << "\n";
358 }
359
360 if (oldLanConnected != newLanConnected)
361 {
Qiang XU74ddf862019-09-12 17:12:13 +0800362 std::string strEthNum = "eth" + std::to_string(ethNum) + lanInfo;
Ed Tanous2049bd22022-07-09 07:20:26 -0700363 const auto* strState = newLanConnected ? "connected" : "lost";
Patrick Williams779c96a2023-05-10 07:50:42 -0500364 const auto* strMsgId = newLanConnected ? "OpenBMC.0.1.LanRegained"
365 : "OpenBMC.0.1.LanLost";
Patrick Williams0c42f402021-08-27 16:05:45 -0500366
367 lg2::info("{ETHDEV} LAN leash {STATE}", "ETHDEV", strEthNum, "STATE",
368 strState, "REDFISH_MESSAGE_ID", strMsgId,
369 "REDFISH_MESSAGE_ARGS", strEthNum);
370
Qiang XU88b7f282019-08-14 22:51:43 +0800371 lanStatusMap[ethNum] = newLanConnected;
Qiang XU88b7f282019-08-14 22:51:43 +0800372 }
373}
374
Lei YUdd68d4a2021-03-16 22:17:23 +0800375/** @brief Initialize the lan status.
376 *
377 * @return true on success and false on failure
378 */
Lei YUba637932021-03-17 10:35:00 +0800379static bool initializeLanStatus(
380 const std::shared_ptr<sdbusplus::asio::connection>& conn)
Qiang XU88b7f282019-08-14 22:51:43 +0800381{
Qiang XU74ddf862019-09-12 17:12:13 +0800382 // init lan port name from configuration
383 getNicNameInfo(conn);
384
385 // get eth info from sysfs
Qiang XU88b7f282019-08-14 22:51:43 +0800386 std::vector<fs::path> files;
387 if (!findFiles(fs::path("/sys/class/net/"), R"(eth\d+/ifindex)", files))
388 {
389 std::cerr << "No eth in system\n";
Lei YUdd68d4a2021-03-16 22:17:23 +0800390 return false;
Qiang XU88b7f282019-08-14 22:51:43 +0800391 }
392
393 // iterate through all found eth files, and save ifindex
Ed Tanous8a57ec02020-10-09 12:46:52 -0700394 for (const fs::path& fileName : files)
Qiang XU88b7f282019-08-14 22:51:43 +0800395 {
396 if (debugLanLeash)
397 {
398 std::cout << "Reading " << fileName << "\n";
399 }
400 std::ifstream sysFile(fileName);
401 if (!sysFile.good())
402 {
403 std::cerr << "Failure reading " << fileName << "\n";
404 continue;
405 }
406 std::string line;
407 getline(sysFile, line);
408 const uint8_t ifindex = std::stoi(line);
409 // pathSuffix is ASCII of ifindex
410 const std::string& pathSuffix = std::to_string(ifindex + 30);
411
412 // extract ethNum
413 const std::string& fileStr = fileName.string();
414 const int pos = fileStr.find("eth");
415 const std::string& ethNumStr = fileStr.substr(pos + 3);
416 int ethNum = 0;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700417 std::from_chars_result r = std::from_chars(
418 ethNumStr.data(), ethNumStr.data() + ethNumStr.size(), ethNum);
419 if (r.ec != std::errc())
Qiang XU88b7f282019-08-14 22:51:43 +0800420 {
421 std::cerr << "invalid ethNum string: " << ethNumStr << "\n";
422 continue;
423 }
424
425 // save pathSuffix
426 pathSuffixMap[pathSuffix] = ethNum;
427 if (debugLanLeash)
428 {
429 std::cout << "ethNum = " << std::to_string(ethNum)
430 << ", ifindex = " << line
431 << ", pathSuffix = " << pathSuffix << "\n";
432 }
433
Qiang XU74ddf862019-09-12 17:12:13 +0800434 // init lan connected status from networkd
Qiang XU88b7f282019-08-14 22:51:43 +0800435 conn->async_method_call(
436 [ethNum](boost::system::error_code ec,
437 const std::variant<std::string>& property) {
Ed Tanousbb679322022-05-16 16:10:00 -0700438 lanStatusMap[ethNum] = false;
439 if (ec)
440 {
441 std::cerr << "Error reading init status of eth" << ethNum
442 << "\n";
443 return;
444 }
445 const std::string* pState = std::get_if<std::string>(&property);
446 if (pState == nullptr)
447 {
448 std::cerr << "Unable to read lan status value\n";
449 return;
450 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500451 bool isLanConnected = (*pState == "routable" ||
452 *pState == "carrier" ||
453 *pState == "degraded");
Ed Tanousbb679322022-05-16 16:10:00 -0700454 if (debugLanLeash)
455 {
456 std::cout << "ethNum = " << std::to_string(ethNum)
457 << ", init LAN status = "
458 << (isLanConnected ? "true" : "false") << "\n";
459 }
460 lanStatusMap[ethNum] = isLanConnected;
Patrick Williams597e8422023-10-20 11:19:01 -0500461 },
Qiang XU88b7f282019-08-14 22:51:43 +0800462 "org.freedesktop.network1",
463 "/org/freedesktop/network1/link/_" + pathSuffix,
464 "org.freedesktop.DBus.Properties", "Get",
465 "org.freedesktop.network1.Link", "OperationalState");
466 }
Lei YUdd68d4a2021-03-16 22:17:23 +0800467 return true;
Qiang XU88b7f282019-08-14 22:51:43 +0800468}
469
Qiang XUe28d1fa2019-02-27 13:50:56 +0800470int main()
471{
Chau Lycebb28c2022-10-21 10:01:52 +0000472 std::shared_ptr<ChassisIntrusionSensor> intrusionSensor;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800473
474 // setup connection to dbus
Ed Tanous1f978632023-02-28 18:16:39 -0800475 boost::asio::io_context io;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800476 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800477
478 // setup object server, define interface
479 systemBus->request_name("xyz.openbmc_project.IntrusionSensor");
480
Ed Tanous14ed5e92022-07-12 15:50:23 -0700481 sdbusplus::asio::object_server objServer(systemBus, true);
482
Chau Ly889af822023-02-20 07:13:52 +0000483 objServer.add_manager("/xyz/openbmc_project/Chassis");
Ed Tanous14ed5e92022-07-12 15:50:23 -0700484
Chau Lycebb28c2022-10-21 10:01:52 +0000485 createSensorsFromConfig(io, objServer, systemBus, intrusionSensor);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800486
487 // callback to handle configuration change
Patrick Williams92f8f512022-07-22 19:26:55 -0500488 std::function<void(sdbusplus::message_t&)> eventHandler =
489 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700490 if (message.is_method_error())
491 {
492 std::cerr << "callback method error\n";
493 return;
494 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800495
Ed Tanousbb679322022-05-16 16:10:00 -0700496 std::cout << "rescan due to configuration change \n";
Chau Lycebb28c2022-10-21 10:01:52 +0000497 createSensorsFromConfig(io, objServer, systemBus, intrusionSensor);
Ed Tanousbb679322022-05-16 16:10:00 -0700498 };
Qiang XUe28d1fa2019-02-27 13:50:56 +0800499
Zev Weiss214d9712022-08-12 12:54:31 -0700500 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
501 setupPropertiesChangedMatches(
502 *systemBus, std::to_array<const char*>({sensorType}), eventHandler);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800503
Lei YUdd68d4a2021-03-16 22:17:23 +0800504 if (initializeLanStatus(systemBus))
505 {
506 // add match to monitor lan status change
Patrick Williams92f8f512022-07-22 19:26:55 -0500507 sdbusplus::bus::match_t lanStatusMatch(
508 static_cast<sdbusplus::bus_t&>(*systemBus),
Lei YUdd68d4a2021-03-16 22:17:23 +0800509 "type='signal', member='PropertiesChanged',"
510 "arg0namespace='org.freedesktop.network1.Link'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500511 [](sdbusplus::message_t& msg) { processLanStatusChange(msg); });
Lei YUdd68d4a2021-03-16 22:17:23 +0800512
513 // add match to monitor entity manager signal about nic name config
514 // change
Patrick Williams92f8f512022-07-22 19:26:55 -0500515 sdbusplus::bus::match_t lanConfigMatch(
516 static_cast<sdbusplus::bus_t&>(*systemBus),
Lei YUdd68d4a2021-03-16 22:17:23 +0800517 "type='signal', member='PropertiesChanged',path_namespace='" +
Zev Weiss054aad82022-08-18 01:37:34 -0700518 std::string(inventoryPath) + "',arg0namespace='" +
519 configInterfaceName(nicType) + "'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500520 [&systemBus](sdbusplus::message_t& msg) {
Ed Tanousbb679322022-05-16 16:10:00 -0700521 if (msg.is_method_error())
522 {
523 std::cerr << "callback method error\n";
524 return;
525 }
526 getNicNameInfo(systemBus);
Patrick Williams597e8422023-10-20 11:19:01 -0500527 });
Lei YUdd68d4a2021-03-16 22:17:23 +0800528 }
Qiang XU88b7f282019-08-14 22:51:43 +0800529
Qiang XUe28d1fa2019-02-27 13:50:56 +0800530 io.run();
531
532 return 0;
533}