blob: 13902c79721f512e9f87991b6035cfd088cbb0df [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
2// Copyright (c) 2017 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Ed Tanous8a57ec02020-10-09 12:46:52 -070017#include <PwmSensor.hpp>
18#include <TachSensor.hpp>
19#include <Utils.hpp>
20#include <VariantVisitors.hpp>
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070024#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070025#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27#include <sdbusplus/bus/match.hpp>
28
29#include <array>
James Feist24f02f22019-04-15 11:05:39 -070030#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070031#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <functional>
33#include <memory>
34#include <optional>
James Feist6714a252018-09-10 15:26:18 -070035#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <string>
37#include <utility>
38#include <variant>
39#include <vector>
James Feist6714a252018-09-10 15:26:18 -070040
James Feistcf3bce62019-01-08 10:07:19 -080041namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080042
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000043// The following two structures need to be consistent
Brandon Kim66558232021-11-09 16:53:08 -080044static auto sensorTypes{std::to_array<const char*>(
45 {"xyz.openbmc_project.Configuration.AspeedFan",
46 "xyz.openbmc_project.Configuration.I2CFan",
47 "xyz.openbmc_project.Configuration.NuvotonFan"})};
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000048
49enum FanTypes
50{
51 aspeed = 0,
52 i2c,
53 nuvoton,
54 max,
55};
56
57static_assert(std::tuple_size<decltype(sensorTypes)>::value == FanTypes::max,
58 "sensorTypes element number is not equal to FanTypes number");
59
James Feistdc6c55f2018-10-31 12:53:20 -070060constexpr const char* redundancyConfiguration =
61 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070062static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070063
James Feistdc6c55f2018-10-31 12:53:20 -070064// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070065std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080066
67FanTypes getFanType(const fs::path& parentPath)
68{
69 fs::path linkPath = parentPath / "device";
70 std::string canonical = fs::read_symlink(linkPath);
Potin Lai0b1ae9f2022-01-27 08:21:08 +080071 if (boost::ends_with(canonical, "pwm-tacho-controller") ||
72 boost::ends_with(canonical, "pwm_tach:tach"))
James Feist95b079b2018-11-21 09:28:00 -080073 {
74 return FanTypes::aspeed;
75 }
Potin Lai0b1ae9f2022-01-27 08:21:08 +080076 if (boost::ends_with(canonical, "pwm-fan-controller"))
Peter Lundgren8843b622019-09-12 10:33:41 -070077 {
78 return FanTypes::nuvoton;
79 }
James Feist95b079b2018-11-21 09:28:00 -080080 // todo: will we need to support other types?
81 return FanTypes::i2c;
82}
Jeff Linabf91de2020-12-23 10:55:42 +080083void enablePwm(const fs::path& filePath)
84{
85 std::fstream enableFile(filePath, std::ios::in | std::ios::out);
86 if (!enableFile.good())
87 {
88 std::cerr << "Error read/write " << filePath << "\n";
89 return;
90 }
James Feistdc6c55f2018-10-31 12:53:20 -070091
Jeff Linabf91de2020-12-23 10:55:42 +080092 std::string regulateMode;
93 std::getline(enableFile, regulateMode);
94 if (regulateMode == "0")
95 {
96 enableFile << 1;
97 }
98}
Howard Chiuddf25d12021-12-02 15:14:44 +080099bool findPwmfanPath(unsigned int configPwmfanIndex, fs::path& pwmPath)
100{
101 /* Search PWM since pwm-fan had separated
102 * PWM from tach directory and 1 channel only*/
103 std::vector<fs::path> pwmfanPaths;
104 std::string pwnfanDevName("pwm-fan");
105
106 pwnfanDevName += std::to_string(configPwmfanIndex);
107
108 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwmfanPaths))
109 {
110 std::cerr << "No PWMs are found!\n";
111 return false;
112 }
113 for (const auto& path : pwmfanPaths)
114 {
115 std::error_code ec;
116 fs::path link = fs::read_symlink(path.parent_path() / "device", ec);
117
118 if (ec)
119 {
120 std::cerr << "read_symlink() failed: " << ec.message() << " ("
121 << ec.value() << ")\n";
122 continue;
123 }
124
125 if (link.filename().string() == pwnfanDevName)
126 {
127 pwmPath = path;
128 return true;
129 }
130 }
131 return false;
132}
133bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath)
134{
135 std::error_code ec;
136
137 /* Assuming PWM file is appeared in the same directory as fanX_input */
138 auto path = directory / ("pwm" + std::to_string(pwm + 1));
139 bool exists = fs::exists(path, ec);
140
141 if (ec || !exists)
142 {
143 /* PWM file not exist or error happened */
144 if (ec)
145 {
146 std::cerr << "exists() failed: " << ec.message() << " ("
147 << ec.value() << ")\n";
148 }
149 /* try search form pwm-fanX directory */
150 return findPwmfanPath(pwm, pwmPath);
151 }
152
153 pwmPath = path;
154 return true;
155}
Kuiying Wangd5407412020-09-09 16:06:56 +0800156void createRedundancySensor(
Josh Lehan5170fe62022-08-03 13:17:41 -0700157 const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
Kuiying Wangd5407412020-09-09 16:06:56 +0800158 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700159 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800160 sdbusplus::asio::object_server& objectServer)
161{
162
163 conn->async_method_call(
164 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700165 const ManagedObjectType& managedObj) {
Ed Tanousbb679322022-05-16 16:10:00 -0700166 if (ec)
167 {
168 std::cerr << "Error calling entity manager \n";
169 return;
170 }
171 for (const auto& pathPair : managedObj)
172 {
173 for (const auto& interfacePair : pathPair.second)
Kuiying Wangd5407412020-09-09 16:06:56 +0800174 {
Ed Tanousbb679322022-05-16 16:10:00 -0700175 if (interfacePair.first == redundancyConfiguration)
Kuiying Wangd5407412020-09-09 16:06:56 +0800176 {
Ed Tanousbb679322022-05-16 16:10:00 -0700177 // currently only support one
178 auto findCount =
179 interfacePair.second.find("AllowedFailures");
180 if (findCount == interfacePair.second.end())
Kuiying Wangd5407412020-09-09 16:06:56 +0800181 {
Ed Tanousbb679322022-05-16 16:10:00 -0700182 std::cerr << "Malformed redundancy record \n";
Kuiying Wangd5407412020-09-09 16:06:56 +0800183 return;
184 }
Ed Tanousbb679322022-05-16 16:10:00 -0700185 std::vector<std::string> sensorList;
186
187 for (const auto& sensor : sensors)
188 {
189 sensorList.push_back(
190 "/xyz/openbmc_project/sensors/fan_tach/" +
191 sensor.second->name);
192 }
193 systemRedundancy.reset();
194 systemRedundancy.emplace(RedundancySensor(
195 std::get<uint64_t>(findCount->second), sensorList,
196 objectServer, pathPair.first));
197
198 return;
Kuiying Wangd5407412020-09-09 16:06:56 +0800199 }
200 }
Ed Tanousbb679322022-05-16 16:10:00 -0700201 }
Kuiying Wangd5407412020-09-09 16:06:56 +0800202 },
203 "xyz.openbmc_project.EntityManager", "/",
204 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
205}
206
James Feist6714a252018-09-10 15:26:18 -0700207void createSensors(
208 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Josh Lehan5170fe62022-08-03 13:17:41 -0700209 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700210 tachSensors,
211 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
212 pwmSensors,
213 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700214 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700215 sensorsChanged,
216 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700217{
James Feistde5e9702019-09-18 16:13:02 -0700218 auto getter = std::make_shared<GetSensorConfiguration>(
219 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700220 [&io, &objectServer, &tachSensors, &pwmSensors, &dbusConnection,
221 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700222 bool firstScan = sensorsChanged == nullptr;
223 std::vector<fs::path> paths;
224 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
225 {
226 std::cerr << "No fan sensors in system\n";
227 return;
228 }
229
230 // iterate through all found fan sensors, and try to match them with
231 // configuration
232 for (const auto& path : paths)
233 {
234 std::smatch match;
235 std::string pathStr = path.string();
236
237 std::regex_search(pathStr, match, inputRegex);
238 std::string indexStr = *(match.begin() + 1);
239
240 fs::path directory = path.parent_path();
241 FanTypes fanType = getFanType(directory);
242
243 // convert to 0 based
244 size_t index = std::stoul(indexStr) - 1;
245
246 const char* baseType = nullptr;
247 const SensorData* sensorData = nullptr;
248 const std::string* interfacePath = nullptr;
249 const SensorBaseConfiguration* baseConfiguration = nullptr;
250 for (const std::pair<sdbusplus::message::object_path, SensorData>&
251 sensor : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800252 {
Ed Tanousbb679322022-05-16 16:10:00 -0700253 // find the base of the configuration to see if indexes
254 // match
255 auto sensorBaseFind = sensor.second.find(sensorTypes[fanType]);
256 if (sensorBaseFind == sensor.second.end())
James Feist95b079b2018-11-21 09:28:00 -0800257 {
Ed Tanousbb679322022-05-16 16:10:00 -0700258 continue;
259 }
260
261 baseConfiguration = &(*sensorBaseFind);
262 interfacePath = &(sensor.first.str);
263 baseType = sensorTypes[fanType];
264
265 auto findIndex = baseConfiguration->second.find("Index");
266 if (findIndex == baseConfiguration->second.end())
267 {
268 std::cerr << baseConfiguration->first << " missing index\n";
269 continue;
270 }
271 unsigned int configIndex = std::visit(
272 VariantToUnsignedIntVisitor(), findIndex->second);
273 if (configIndex != index)
274 {
275 continue;
276 }
277 if (fanType == FanTypes::aspeed || fanType == FanTypes::nuvoton)
278 {
279 // there will be only 1 aspeed or nuvoton sensor object
280 // in sysfs, we found the fan
281 sensorData = &(sensor.second);
282 break;
283 }
284 if (fanType == FanTypes::i2c)
285 {
286 size_t bus = 0;
287 size_t address = 0;
288
289 std::string link =
290 fs::read_symlink(directory / "device").filename();
291
292 size_t findDash = link.find('-');
293 if (findDash == std::string::npos ||
294 link.size() <= findDash + 1)
James Feistde5e9702019-09-18 16:13:02 -0700295 {
Ed Tanousbb679322022-05-16 16:10:00 -0700296 std::cerr << "Error finding device from symlink";
James Feistde5e9702019-09-18 16:13:02 -0700297 }
Ed Tanousbb679322022-05-16 16:10:00 -0700298 bus = std::stoi(link.substr(0, findDash));
299 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800300
Ed Tanousbb679322022-05-16 16:10:00 -0700301 auto findBus = baseConfiguration->second.find("Bus");
302 auto findAddress =
303 baseConfiguration->second.find("Address");
304 if (findBus == baseConfiguration->second.end() ||
305 findAddress == baseConfiguration->second.end())
James Feistde5e9702019-09-18 16:13:02 -0700306 {
307 std::cerr << baseConfiguration->first
Ed Tanousbb679322022-05-16 16:10:00 -0700308 << " missing bus or address\n";
James Feistde5e9702019-09-18 16:13:02 -0700309 continue;
310 }
Ed Tanousbb679322022-05-16 16:10:00 -0700311 unsigned int configBus = std::visit(
312 VariantToUnsignedIntVisitor(), findBus->second);
313 unsigned int configAddress = std::visit(
314 VariantToUnsignedIntVisitor(), findAddress->second);
315
316 if (configBus == bus && configAddress == address)
James Feistde5e9702019-09-18 16:13:02 -0700317 {
James Feistde5e9702019-09-18 16:13:02 -0700318 sensorData = &(sensor.second);
319 break;
320 }
Ed Tanousbb679322022-05-16 16:10:00 -0700321 }
322 }
323 if (sensorData == nullptr)
324 {
325 std::cerr << "failed to find match for " << path.string()
326 << "\n";
327 continue;
328 }
329
330 auto findSensorName = baseConfiguration->second.find("Name");
331
332 if (findSensorName == baseConfiguration->second.end())
333 {
334 std::cerr << "could not determine configuration name for "
335 << path.string() << "\n";
336 continue;
337 }
338 std::string sensorName =
339 std::get<std::string>(findSensorName->second);
340
341 // on rescans, only update sensors we were signaled by
342 auto findSensor = tachSensors.find(sensorName);
343 if (!firstScan && findSensor != tachSensors.end())
344 {
345 bool found = false;
346 for (auto it = sensorsChanged->begin();
347 it != sensorsChanged->end(); it++)
348 {
349 if (boost::ends_with(*it, findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700350 {
Ed Tanousbb679322022-05-16 16:10:00 -0700351 sensorsChanged->erase(it);
352 findSensor->second = nullptr;
353 found = true;
354 break;
James Feistde5e9702019-09-18 16:13:02 -0700355 }
356 }
Ed Tanousbb679322022-05-16 16:10:00 -0700357 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700358 {
James Feist95b079b2018-11-21 09:28:00 -0800359 continue;
360 }
Ed Tanousbb679322022-05-16 16:10:00 -0700361 }
362 std::vector<thresholds::Threshold> sensorThresholds;
363 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
364 {
365 std::cerr << "error populating thresholds for " << sensorName
366 << "\n";
367 }
James Feist95b079b2018-11-21 09:28:00 -0800368
Ed Tanousbb679322022-05-16 16:10:00 -0700369 auto presenceConfig =
370 sensorData->find(baseType + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800371
Ed Tanousbb679322022-05-16 16:10:00 -0700372 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
373
374 // presence sensors are optional
375 if (presenceConfig != sensorData->end())
376 {
377 auto findPolarity = presenceConfig->second.find("Polarity");
378 auto findPinName = presenceConfig->second.find("PinName");
379
380 if (findPinName == presenceConfig->second.end() ||
381 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800382 {
Ed Tanousbb679322022-05-16 16:10:00 -0700383 std::cerr << "Malformed Presence Configuration\n";
James Feistde5e9702019-09-18 16:13:02 -0700384 }
Ed Tanousbb679322022-05-16 16:10:00 -0700385 else
James Feistde5e9702019-09-18 16:13:02 -0700386 {
Ed Tanousbb679322022-05-16 16:10:00 -0700387 bool inverted =
388 std::get<std::string>(findPolarity->second) == "Low";
Ed Tanous2049bd22022-07-09 07:20:26 -0700389 if (const auto* pinName =
Ed Tanousbb679322022-05-16 16:10:00 -0700390 std::get_if<std::string>(&findPinName->second))
James Feistde5e9702019-09-18 16:13:02 -0700391 {
Ed Tanousbb679322022-05-16 16:10:00 -0700392 presenceSensor = std::make_unique<PresenceSensor>(
393 *pinName, inverted, io, sensorName);
James Feistde5e9702019-09-18 16:13:02 -0700394 }
395 else
396 {
Ed Tanousbb679322022-05-16 16:10:00 -0700397 std::cerr << "Malformed Presence pinName for sensor "
398 << sensorName << " \n";
James Feistde5e9702019-09-18 16:13:02 -0700399 }
400 }
Ed Tanousbb679322022-05-16 16:10:00 -0700401 }
402 std::optional<RedundancySensor>* redundancy = nullptr;
403 if (fanType == FanTypes::aspeed)
404 {
405 redundancy = &systemRedundancy;
406 }
407
Zev Weissa4d27682022-07-19 15:30:36 -0700408 PowerState powerState = getPowerState(baseConfiguration->second);
Yong Zhao77b3add2021-01-09 04:25:18 +0000409
Ed Tanousbb679322022-05-16 16:10:00 -0700410 constexpr double defaultMaxReading = 25000;
411 constexpr double defaultMinReading = 0;
412 std::pair<double, double> limits =
413 std::make_pair(defaultMinReading, defaultMaxReading);
414
415 auto connector =
416 sensorData->find(baseType + std::string(".Connector"));
417
418 std::optional<std::string> led;
419 std::string pwmName;
420 fs::path pwmPath;
421
422 // The Mutable parameter is optional, defaulting to false
423 bool isValueMutable = false;
424 if (connector != sensorData->end())
425 {
426 auto findPwm = connector->second.find("Pwm");
427 if (findPwm != connector->second.end())
428 {
429 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
430 findPwm->second);
431 if (!findPwmPath(directory, pwm, pwmPath))
432 {
433 std::cerr << "Connector for " << sensorName
434 << " no pwm channel found!\n";
435 continue;
436 }
437
438 fs::path pwmEnableFile =
439 "pwm" + std::to_string(pwm + 1) + "_enable";
440 fs::path enablePath = pwmPath.parent_path() / pwmEnableFile;
441 enablePwm(enablePath);
442
443 /* use pwm name override if found in configuration else
444 * use default */
445 auto findOverride = connector->second.find("PwmName");
446 if (findOverride != connector->second.end())
447 {
448 pwmName = std::visit(VariantToStringVisitor(),
449 findOverride->second);
450 }
451 else
452 {
453 pwmName = "Pwm_" + std::to_string(pwm + 1);
454 }
455
456 // Check PWM sensor mutability
457 auto findMutable = connector->second.find("Mutable");
458 if (findMutable != connector->second.end())
459 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700460 const auto* ptrMutable =
Ed Tanousbb679322022-05-16 16:10:00 -0700461 std::get_if<bool>(&(findMutable->second));
Ed Tanous2049bd22022-07-09 07:20:26 -0700462 if (ptrMutable != nullptr)
Ed Tanousbb679322022-05-16 16:10:00 -0700463 {
464 isValueMutable = *ptrMutable;
465 }
466 }
467 }
468 else
469 {
470 std::cerr << "Connector for " << sensorName
471 << " missing pwm!\n";
472 }
473
474 auto findLED = connector->second.find("LED");
475 if (findLED != connector->second.end())
476 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700477 const auto* ledName =
478 std::get_if<std::string>(&(findLED->second));
Ed Tanousbb679322022-05-16 16:10:00 -0700479 if (ledName == nullptr)
480 {
481 std::cerr << "Wrong format for LED of " << sensorName
482 << "\n";
483 }
484 else
485 {
486 led = *ledName;
487 }
488 }
489 }
490
491 findLimits(limits, baseConfiguration);
Josh Lehan5170fe62022-08-03 13:17:41 -0700492
493 auto& tachSensor = tachSensors[sensorName];
494 tachSensor = nullptr;
495 tachSensor = std::make_shared<TachSensor>(
Ed Tanousbb679322022-05-16 16:10:00 -0700496 path.string(), baseType, objectServer, dbusConnection,
497 std::move(presenceSensor), redundancy, io, sensorName,
498 std::move(sensorThresholds), *interfacePath, limits, powerState,
499 led);
Josh Lehan5170fe62022-08-03 13:17:41 -0700500 tachSensor->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700501
502 if (!pwmPath.empty() && fs::exists(pwmPath) &&
Ed Tanous2049bd22022-07-09 07:20:26 -0700503 (pwmSensors.count(pwmPath) == 0U))
Ed Tanousbb679322022-05-16 16:10:00 -0700504 {
505 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
506 pwmName, pwmPath, dbusConnection, objectServer,
507 *interfacePath, "Fan", isValueMutable);
508 }
509 }
510
511 createRedundancySensor(tachSensors, dbusConnection, objectServer);
Ed Tanous8a17c302021-09-02 15:07:11 -0700512 });
James Feistde5e9702019-09-18 16:13:02 -0700513 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700514 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
515 retries);
James Feist6714a252018-09-10 15:26:18 -0700516}
517
James Feistb6c0b912019-07-09 12:21:44 -0700518int main()
James Feist6714a252018-09-10 15:26:18 -0700519{
520 boost::asio::io_service io;
521 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
522 systemBus->request_name("xyz.openbmc_project.FanSensor");
523 sdbusplus::asio::object_server objectServer(systemBus);
Josh Lehan5170fe62022-08-03 13:17:41 -0700524 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700525 tachSensors;
526 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
527 pwmSensors;
Patrick Williams92f8f512022-07-22 19:26:55 -0500528 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700529 auto sensorsChanged =
530 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700531
532 io.post([&]() {
533 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
534 nullptr);
535 });
536
537 boost::asio::deadline_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500538 std::function<void(sdbusplus::message_t&)> eventHandler =
539 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700540 if (message.is_method_error())
541 {
542 std::cerr << "callback method error\n";
543 return;
544 }
545 sensorsChanged->insert(message.get_path());
546 // this implicitly cancels the timer
547 filterTimer.expires_from_now(boost::posix_time::seconds(1));
548
549 filterTimer.async_wait([&](const boost::system::error_code& ec) {
550 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700551 {
Ed Tanousbb679322022-05-16 16:10:00 -0700552 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700553 return;
554 }
Ed Tanousbb679322022-05-16 16:10:00 -0700555 if (ec)
556 {
557 std::cerr << "timer error\n";
558 return;
559 }
560 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
561 sensorsChanged, 5);
562 });
563 };
James Feist6714a252018-09-10 15:26:18 -0700564
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700565 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700566 {
Patrick Williams92f8f512022-07-22 19:26:55 -0500567 auto match = std::make_unique<sdbusplus::bus::match_t>(
568 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feist6714a252018-09-10 15:26:18 -0700569 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700570 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700571 eventHandler);
572 matches.emplace_back(std::move(match));
573 }
574
James Feistdc6c55f2018-10-31 12:53:20 -0700575 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500576 std::function<void(sdbusplus::message_t&)> redundancyHandler =
577 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700578 createRedundancySensor(tachSensors, systemBus, objectServer);
579 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500580 auto match = std::make_unique<sdbusplus::bus::match_t>(
581 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700582 "type='signal',member='PropertiesChanged',path_namespace='" +
583 std::string(inventoryPath) + "',arg0namespace='" +
584 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700585 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700586 matches.emplace_back(std::move(match));
587
Bruce Lee1263c3d2021-06-04 15:16:33 +0800588 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700589 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700590 return 0;
James Feist6714a252018-09-10 15:26:18 -0700591}