blob: dda9b6fad953779b3ac5c63544d39000b66d70c3 [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 Williams779c96a2023-05-10 07:50:42 -0500240 dbusConnection,
241 [](const ManagedObjectType& sensorConfigurations) {
242 // Get NIC name and save to map
243 lanInfoMap.clear();
244 for (const auto& [path, cfgData] : sensorConfigurations)
245 {
246 const std::pair<std::string, SensorBaseConfigMap>*
247 baseConfiguration = nullptr;
248
249 // find base configuration
250 auto sensorBase = cfgData.find(configInterfaceName(nicType));
251 if (sensorBase == cfgData.end())
Qiang XU74ddf862019-09-12 17:12:13 +0800252 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500253 continue;
254 }
255 baseConfiguration = &(*sensorBase);
Qiang XU74ddf862019-09-12 17:12:13 +0800256
Patrick Williams779c96a2023-05-10 07:50:42 -0500257 auto findEthIndex = baseConfiguration->second.find("EthIndex");
258 auto findName = baseConfiguration->second.find("Name");
259
260 if (findEthIndex != baseConfiguration->second.end() &&
261 findName != baseConfiguration->second.end())
262 {
263 const auto* pEthIndex =
264 std::get_if<uint64_t>(&findEthIndex->second);
265 const auto* pName = std::get_if<std::string>(&findName->second);
266 if (pEthIndex != nullptr && pName != nullptr)
Qiang XU74ddf862019-09-12 17:12:13 +0800267 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500268 lanInfoMap[*pEthIndex] = *pName;
269 if (debugLanLeash)
Qiang XU74ddf862019-09-12 17:12:13 +0800270 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500271 std::cout << "find name of eth" << *pEthIndex << " is "
272 << *pName << "\n";
Qiang XU74ddf862019-09-12 17:12:13 +0800273 }
274 }
275 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500276 }
Qiang XU74ddf862019-09-12 17:12:13 +0800277
Patrick Williams779c96a2023-05-10 07:50:42 -0500278 if (lanInfoMap.empty())
279 {
280 std::cerr << "can't find matched NIC name. \n";
281 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700282 });
Qiang XU74ddf862019-09-12 17:12:13 +0800283
284 getter->getConfiguration(
285 std::vector<std::string>{nicTypes.begin(), nicTypes.end()});
286}
287
Patrick Williams92f8f512022-07-22 19:26:55 -0500288static void processLanStatusChange(sdbusplus::message_t& message)
Qiang XU88b7f282019-08-14 22:51:43 +0800289{
290 const std::string& pathName = message.get_path();
291 std::string interfaceName;
Zev Weissafd15042022-07-18 12:28:40 -0700292 SensorBaseConfigMap properties;
Qiang XU88b7f282019-08-14 22:51:43 +0800293 message.read(interfaceName, properties);
294
295 auto findStateProperty = properties.find("OperationalState");
296 if (findStateProperty == properties.end())
297 {
298 return;
299 }
Qiang XU74ddf862019-09-12 17:12:13 +0800300 std::string* pState =
301 std::get_if<std::string>(&(findStateProperty->second));
Qiang XU88b7f282019-08-14 22:51:43 +0800302 if (pState == nullptr)
303 {
304 std::cerr << "invalid OperationalState \n";
305 return;
306 }
307
308 bool newLanConnected = (*pState == "routable" || *pState == "carrier" ||
309 *pState == "degraded");
310
311 // get ethNum from path. /org/freedesktop/network1/link/_32 for eth0
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500312 size_t pos = pathName.find("/_");
Qiang XU88b7f282019-08-14 22:51:43 +0800313 if (pos == std::string::npos || pathName.length() <= pos + 2)
314 {
315 std::cerr << "unexpected path name " << pathName << "\n";
316 return;
317 }
318 std::string suffixStr = pathName.substr(pos + 2);
319
320 auto findEthNum = pathSuffixMap.find(suffixStr);
321 if (findEthNum == pathSuffixMap.end())
322 {
323 std::cerr << "unexpected eth for suffixStr " << suffixStr << "\n";
324 return;
325 }
326 int ethNum = findEthNum->second;
Qiang XU74ddf862019-09-12 17:12:13 +0800327
328 // get lan status from map
Qiang XU88b7f282019-08-14 22:51:43 +0800329 auto findLanStatus = lanStatusMap.find(ethNum);
330 if (findLanStatus == lanStatusMap.end())
331 {
332 std::cerr << "unexpected eth " << ethNum << " in lanStatusMap \n";
333 return;
334 }
335 bool oldLanConnected = findLanStatus->second;
336
Qiang XU74ddf862019-09-12 17:12:13 +0800337 // get lan info from map
Ed Tanous2049bd22022-07-09 07:20:26 -0700338 std::string lanInfo;
339 if (!lanInfoMap.empty())
Qiang XU74ddf862019-09-12 17:12:13 +0800340 {
341 auto findLanInfo = lanInfoMap.find(ethNum);
342 if (findLanInfo == lanInfoMap.end())
343 {
344 std::cerr << "unexpected eth " << ethNum << " in lanInfoMap \n";
345 }
346 else
347 {
348 lanInfo = "(" + findLanInfo->second + ")";
349 }
350 }
351
Qiang XU88b7f282019-08-14 22:51:43 +0800352 if (debugLanLeash)
353 {
354 std::cout << "ethNum = " << ethNum << ", state = " << *pState
355 << ", oldLanConnected = "
356 << (oldLanConnected ? "true" : "false")
357 << ", newLanConnected = "
358 << (newLanConnected ? "true" : "false") << "\n";
359 }
360
361 if (oldLanConnected != newLanConnected)
362 {
Qiang XU74ddf862019-09-12 17:12:13 +0800363 std::string strEthNum = "eth" + std::to_string(ethNum) + lanInfo;
Ed Tanous2049bd22022-07-09 07:20:26 -0700364 const auto* strState = newLanConnected ? "connected" : "lost";
Patrick Williams779c96a2023-05-10 07:50:42 -0500365 const auto* strMsgId = newLanConnected ? "OpenBMC.0.1.LanRegained"
366 : "OpenBMC.0.1.LanLost";
Patrick Williams0c42f402021-08-27 16:05:45 -0500367
368 lg2::info("{ETHDEV} LAN leash {STATE}", "ETHDEV", strEthNum, "STATE",
369 strState, "REDFISH_MESSAGE_ID", strMsgId,
370 "REDFISH_MESSAGE_ARGS", strEthNum);
371
Qiang XU88b7f282019-08-14 22:51:43 +0800372 lanStatusMap[ethNum] = newLanConnected;
Qiang XU88b7f282019-08-14 22:51:43 +0800373 }
374}
375
Lei YUdd68d4a2021-03-16 22:17:23 +0800376/** @brief Initialize the lan status.
377 *
378 * @return true on success and false on failure
379 */
Lei YUba637932021-03-17 10:35:00 +0800380static bool initializeLanStatus(
381 const std::shared_ptr<sdbusplus::asio::connection>& conn)
Qiang XU88b7f282019-08-14 22:51:43 +0800382{
Qiang XU74ddf862019-09-12 17:12:13 +0800383 // init lan port name from configuration
384 getNicNameInfo(conn);
385
386 // get eth info from sysfs
Qiang XU88b7f282019-08-14 22:51:43 +0800387 std::vector<fs::path> files;
388 if (!findFiles(fs::path("/sys/class/net/"), R"(eth\d+/ifindex)", files))
389 {
390 std::cerr << "No eth in system\n";
Lei YUdd68d4a2021-03-16 22:17:23 +0800391 return false;
Qiang XU88b7f282019-08-14 22:51:43 +0800392 }
393
394 // iterate through all found eth files, and save ifindex
Ed Tanous8a57ec02020-10-09 12:46:52 -0700395 for (const fs::path& fileName : files)
Qiang XU88b7f282019-08-14 22:51:43 +0800396 {
397 if (debugLanLeash)
398 {
399 std::cout << "Reading " << fileName << "\n";
400 }
401 std::ifstream sysFile(fileName);
402 if (!sysFile.good())
403 {
404 std::cerr << "Failure reading " << fileName << "\n";
405 continue;
406 }
407 std::string line;
408 getline(sysFile, line);
409 const uint8_t ifindex = std::stoi(line);
410 // pathSuffix is ASCII of ifindex
411 const std::string& pathSuffix = std::to_string(ifindex + 30);
412
413 // extract ethNum
414 const std::string& fileStr = fileName.string();
415 const int pos = fileStr.find("eth");
416 const std::string& ethNumStr = fileStr.substr(pos + 3);
417 int ethNum = 0;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700418 std::from_chars_result r = std::from_chars(
419 ethNumStr.data(), ethNumStr.data() + ethNumStr.size(), ethNum);
420 if (r.ec != std::errc())
Qiang XU88b7f282019-08-14 22:51:43 +0800421 {
422 std::cerr << "invalid ethNum string: " << ethNumStr << "\n";
423 continue;
424 }
425
426 // save pathSuffix
427 pathSuffixMap[pathSuffix] = ethNum;
428 if (debugLanLeash)
429 {
430 std::cout << "ethNum = " << std::to_string(ethNum)
431 << ", ifindex = " << line
432 << ", pathSuffix = " << pathSuffix << "\n";
433 }
434
Qiang XU74ddf862019-09-12 17:12:13 +0800435 // init lan connected status from networkd
Qiang XU88b7f282019-08-14 22:51:43 +0800436 conn->async_method_call(
437 [ethNum](boost::system::error_code ec,
438 const std::variant<std::string>& property) {
Ed Tanousbb679322022-05-16 16:10:00 -0700439 lanStatusMap[ethNum] = false;
440 if (ec)
441 {
442 std::cerr << "Error reading init status of eth" << ethNum
443 << "\n";
444 return;
445 }
446 const std::string* pState = std::get_if<std::string>(&property);
447 if (pState == nullptr)
448 {
449 std::cerr << "Unable to read lan status value\n";
450 return;
451 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500452 bool isLanConnected = (*pState == "routable" ||
453 *pState == "carrier" ||
454 *pState == "degraded");
Ed Tanousbb679322022-05-16 16:10:00 -0700455 if (debugLanLeash)
456 {
457 std::cout << "ethNum = " << std::to_string(ethNum)
458 << ", init LAN status = "
459 << (isLanConnected ? "true" : "false") << "\n";
460 }
461 lanStatusMap[ethNum] = isLanConnected;
Qiang XU88b7f282019-08-14 22:51:43 +0800462 },
463 "org.freedesktop.network1",
464 "/org/freedesktop/network1/link/_" + pathSuffix,
465 "org.freedesktop.DBus.Properties", "Get",
466 "org.freedesktop.network1.Link", "OperationalState");
467 }
Lei YUdd68d4a2021-03-16 22:17:23 +0800468 return true;
Qiang XU88b7f282019-08-14 22:51:43 +0800469}
470
Qiang XUe28d1fa2019-02-27 13:50:56 +0800471int main()
472{
Chau Lycebb28c2022-10-21 10:01:52 +0000473 std::shared_ptr<ChassisIntrusionSensor> intrusionSensor;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800474
475 // setup connection to dbus
Ed Tanous1f978632023-02-28 18:16:39 -0800476 boost::asio::io_context io;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800477 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800478
479 // setup object server, define interface
480 systemBus->request_name("xyz.openbmc_project.IntrusionSensor");
481
Ed Tanous14ed5e92022-07-12 15:50:23 -0700482 sdbusplus::asio::object_server objServer(systemBus, true);
483
Chau Ly889af822023-02-20 07:13:52 +0000484 objServer.add_manager("/xyz/openbmc_project/Chassis");
Ed Tanous14ed5e92022-07-12 15:50:23 -0700485
Chau Lycebb28c2022-10-21 10:01:52 +0000486 createSensorsFromConfig(io, objServer, systemBus, intrusionSensor);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800487
488 // callback to handle configuration change
Patrick Williams92f8f512022-07-22 19:26:55 -0500489 std::function<void(sdbusplus::message_t&)> eventHandler =
490 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700491 if (message.is_method_error())
492 {
493 std::cerr << "callback method error\n";
494 return;
495 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800496
Ed Tanousbb679322022-05-16 16:10:00 -0700497 std::cout << "rescan due to configuration change \n";
Chau Lycebb28c2022-10-21 10:01:52 +0000498 createSensorsFromConfig(io, objServer, systemBus, intrusionSensor);
Ed Tanousbb679322022-05-16 16:10:00 -0700499 };
Qiang XUe28d1fa2019-02-27 13:50:56 +0800500
Zev Weiss214d9712022-08-12 12:54:31 -0700501 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
502 setupPropertiesChangedMatches(
503 *systemBus, std::to_array<const char*>({sensorType}), eventHandler);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800504
Lei YUdd68d4a2021-03-16 22:17:23 +0800505 if (initializeLanStatus(systemBus))
506 {
507 // add match to monitor lan status change
Patrick Williams92f8f512022-07-22 19:26:55 -0500508 sdbusplus::bus::match_t lanStatusMatch(
509 static_cast<sdbusplus::bus_t&>(*systemBus),
Lei YUdd68d4a2021-03-16 22:17:23 +0800510 "type='signal', member='PropertiesChanged',"
511 "arg0namespace='org.freedesktop.network1.Link'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500512 [](sdbusplus::message_t& msg) { processLanStatusChange(msg); });
Lei YUdd68d4a2021-03-16 22:17:23 +0800513
514 // add match to monitor entity manager signal about nic name config
515 // change
Patrick Williams92f8f512022-07-22 19:26:55 -0500516 sdbusplus::bus::match_t lanConfigMatch(
517 static_cast<sdbusplus::bus_t&>(*systemBus),
Lei YUdd68d4a2021-03-16 22:17:23 +0800518 "type='signal', member='PropertiesChanged',path_namespace='" +
Zev Weiss054aad82022-08-18 01:37:34 -0700519 std::string(inventoryPath) + "',arg0namespace='" +
520 configInterfaceName(nicType) + "'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500521 [&systemBus](sdbusplus::message_t& msg) {
Ed Tanousbb679322022-05-16 16:10:00 -0700522 if (msg.is_method_error())
523 {
524 std::cerr << "callback method error\n";
525 return;
526 }
527 getNicNameInfo(systemBus);
Lei YUdd68d4a2021-03-16 22:17:23 +0800528 });
529 }
Qiang XU88b7f282019-08-14 22:51:43 +0800530
Qiang XUe28d1fa2019-02-27 13:50:56 +0800531 io.run();
532
533 return 0;
534}