blob: 50838a5fa1e8347f2d2fe05b64cde94e6244a856 [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>
25#include <boost/lexical_cast.hpp>
James Feist38fb5982020-05-28 10:09:54 -070026#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28#include <sdbusplus/bus/match.hpp>
29
30#include <array>
James Feist24f02f22019-04-15 11:05:39 -070031#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070032#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <functional>
34#include <memory>
35#include <optional>
James Feist6714a252018-09-10 15:26:18 -070036#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <string>
38#include <utility>
39#include <variant>
40#include <vector>
James Feist6714a252018-09-10 15:26:18 -070041
James Feistcf3bce62019-01-08 10:07:19 -080042namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080043
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000044// The following two structures need to be consistent
Brandon Kim66558232021-11-09 16:53:08 -080045static auto sensorTypes{std::to_array<const char*>(
46 {"xyz.openbmc_project.Configuration.AspeedFan",
47 "xyz.openbmc_project.Configuration.I2CFan",
48 "xyz.openbmc_project.Configuration.NuvotonFan"})};
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000049
50enum FanTypes
51{
52 aspeed = 0,
53 i2c,
54 nuvoton,
55 max,
56};
57
58static_assert(std::tuple_size<decltype(sensorTypes)>::value == FanTypes::max,
59 "sensorTypes element number is not equal to FanTypes number");
60
James Feistdc6c55f2018-10-31 12:53:20 -070061constexpr const char* redundancyConfiguration =
62 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070063static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070064
James Feistdc6c55f2018-10-31 12:53:20 -070065// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070066std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080067
68FanTypes getFanType(const fs::path& parentPath)
69{
70 fs::path linkPath = parentPath / "device";
71 std::string canonical = fs::read_symlink(linkPath);
Potin Lai0b1ae9f2022-01-27 08:21:08 +080072 if (boost::ends_with(canonical, "pwm-tacho-controller") ||
73 boost::ends_with(canonical, "pwm_tach:tach"))
James Feist95b079b2018-11-21 09:28:00 -080074 {
75 return FanTypes::aspeed;
76 }
Potin Lai0b1ae9f2022-01-27 08:21:08 +080077 if (boost::ends_with(canonical, "pwm-fan-controller"))
Peter Lundgren8843b622019-09-12 10:33:41 -070078 {
79 return FanTypes::nuvoton;
80 }
James Feist95b079b2018-11-21 09:28:00 -080081 // todo: will we need to support other types?
82 return FanTypes::i2c;
83}
Jeff Linabf91de2020-12-23 10:55:42 +080084void enablePwm(const fs::path& filePath)
85{
86 std::fstream enableFile(filePath, std::ios::in | std::ios::out);
87 if (!enableFile.good())
88 {
89 std::cerr << "Error read/write " << filePath << "\n";
90 return;
91 }
James Feistdc6c55f2018-10-31 12:53:20 -070092
Jeff Linabf91de2020-12-23 10:55:42 +080093 std::string regulateMode;
94 std::getline(enableFile, regulateMode);
95 if (regulateMode == "0")
96 {
97 enableFile << 1;
98 }
99}
Howard Chiuddf25d12021-12-02 15:14:44 +0800100bool findPwmfanPath(unsigned int configPwmfanIndex, fs::path& pwmPath)
101{
102 /* Search PWM since pwm-fan had separated
103 * PWM from tach directory and 1 channel only*/
104 std::vector<fs::path> pwmfanPaths;
105 std::string pwnfanDevName("pwm-fan");
106
107 pwnfanDevName += std::to_string(configPwmfanIndex);
108
109 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwmfanPaths))
110 {
111 std::cerr << "No PWMs are found!\n";
112 return false;
113 }
114 for (const auto& path : pwmfanPaths)
115 {
116 std::error_code ec;
117 fs::path link = fs::read_symlink(path.parent_path() / "device", ec);
118
119 if (ec)
120 {
121 std::cerr << "read_symlink() failed: " << ec.message() << " ("
122 << ec.value() << ")\n";
123 continue;
124 }
125
126 if (link.filename().string() == pwnfanDevName)
127 {
128 pwmPath = path;
129 return true;
130 }
131 }
132 return false;
133}
134bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath)
135{
136 std::error_code ec;
137
138 /* Assuming PWM file is appeared in the same directory as fanX_input */
139 auto path = directory / ("pwm" + std::to_string(pwm + 1));
140 bool exists = fs::exists(path, ec);
141
142 if (ec || !exists)
143 {
144 /* PWM file not exist or error happened */
145 if (ec)
146 {
147 std::cerr << "exists() failed: " << ec.message() << " ("
148 << ec.value() << ")\n";
149 }
150 /* try search form pwm-fanX directory */
151 return findPwmfanPath(pwm, pwmPath);
152 }
153
154 pwmPath = path;
155 return true;
156}
Kuiying Wangd5407412020-09-09 16:06:56 +0800157void createRedundancySensor(
158 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
159 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700160 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800161 sdbusplus::asio::object_server& objectServer)
162{
163
164 conn->async_method_call(
165 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700166 const ManagedObjectType& managedObj) {
Kuiying Wangd5407412020-09-09 16:06:56 +0800167 if (ec)
168 {
169 std::cerr << "Error calling entity manager \n";
170 return;
171 }
172 for (const auto& pathPair : managedObj)
173 {
174 for (const auto& interfacePair : pathPair.second)
175 {
176 if (interfacePair.first == redundancyConfiguration)
177 {
178 // currently only support one
179 auto findCount =
180 interfacePair.second.find("AllowedFailures");
181 if (findCount == interfacePair.second.end())
182 {
183 std::cerr << "Malformed redundancy record \n";
184 return;
185 }
186 std::vector<std::string> sensorList;
187
188 for (const auto& sensor : sensors)
189 {
190 sensorList.push_back(
191 "/xyz/openbmc_project/sensors/fan_tach/" +
192 sensor.second->name);
193 }
194 systemRedundancy.reset();
195 systemRedundancy.emplace(RedundancySensor(
196 std::get<uint64_t>(findCount->second), sensorList,
197 objectServer, pathPair.first));
198
199 return;
200 }
201 }
202 }
203 },
204 "xyz.openbmc_project.EntityManager", "/",
205 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
206}
207
James Feist6714a252018-09-10 15:26:18 -0700208void createSensors(
209 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
210 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
211 tachSensors,
212 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
213 pwmSensors,
214 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700215 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700216 sensorsChanged,
217 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700218{
James Feistde5e9702019-09-18 16:13:02 -0700219 auto getter = std::make_shared<GetSensorConfiguration>(
220 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700221 [&io, &objectServer, &tachSensors, &pwmSensors, &dbusConnection,
222 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
James Feistde5e9702019-09-18 16:13:02 -0700223 bool firstScan = sensorsChanged == nullptr;
224 std::vector<fs::path> paths;
225 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)",
226 paths))
James Feist95b079b2018-11-21 09:28:00 -0800227 {
Yong Zhao77b3add2021-01-09 04:25:18 +0000228 std::cerr << "No fan sensors in system\n";
James Feistde5e9702019-09-18 16:13:02 -0700229 return;
James Feist95b079b2018-11-21 09:28:00 -0800230 }
James Feist6714a252018-09-10 15:26:18 -0700231
James Feistde5e9702019-09-18 16:13:02 -0700232 // iterate through all found fan sensors, and try to match them with
233 // configuration
234 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -0700235 {
James Feistde5e9702019-09-18 16:13:02 -0700236 std::smatch match;
237 std::string pathStr = path.string();
238
239 std::regex_search(pathStr, match, inputRegex);
240 std::string indexStr = *(match.begin() + 1);
241
Yong Zhao77b3add2021-01-09 04:25:18 +0000242 fs::path directory = path.parent_path();
James Feistde5e9702019-09-18 16:13:02 -0700243 FanTypes fanType = getFanType(directory);
Yong Zhao77b3add2021-01-09 04:25:18 +0000244
James Feistde5e9702019-09-18 16:13:02 -0700245 // convert to 0 based
246 size_t index = std::stoul(indexStr) - 1;
247
Ed Tanousa771f6a2022-01-14 09:36:51 -0800248 const char* baseType = nullptr;
James Feistde5e9702019-09-18 16:13:02 -0700249 const SensorData* sensorData = nullptr;
250 const std::string* interfacePath = nullptr;
251 const SensorBaseConfiguration* baseConfiguration = nullptr;
252 for (const std::pair<sdbusplus::message::object_path,
253 SensorData>& sensor : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800254 {
James Feistde5e9702019-09-18 16:13:02 -0700255 // find the base of the configuration to see if indexes
256 // match
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +0000257 auto sensorBaseFind =
258 sensor.second.find(sensorTypes[fanType]);
259 if (sensorBaseFind == sensor.second.end())
James Feistde5e9702019-09-18 16:13:02 -0700260 {
261 continue;
262 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800263
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +0000264 baseConfiguration = &(*sensorBaseFind);
265 interfacePath = &(sensor.first.str);
266 baseType = sensorTypes[fanType];
267
James Feistde5e9702019-09-18 16:13:02 -0700268 auto findIndex = baseConfiguration->second.find("Index");
269 if (findIndex == baseConfiguration->second.end())
270 {
271 std::cerr << baseConfiguration->first
272 << " missing index\n";
273 continue;
274 }
275 unsigned int configIndex = std::visit(
276 VariantToUnsignedIntVisitor(), findIndex->second);
277 if (configIndex != index)
278 {
279 continue;
280 }
281 if (fanType == FanTypes::aspeed ||
282 fanType == FanTypes::nuvoton)
283 {
284 // there will be only 1 aspeed or nuvoton sensor object
285 // in sysfs, we found the fan
286 sensorData = &(sensor.second);
287 break;
288 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700289 if (fanType == FanTypes::i2c)
James Feistde5e9702019-09-18 16:13:02 -0700290 {
Yong Zhaoadd46822021-01-09 04:52:36 +0000291 size_t bus = 0;
292 size_t address = 0;
293
294 std::string link =
295 fs::read_symlink(directory / "device").filename();
296
297 size_t findDash = link.find('-');
298 if (findDash == std::string::npos ||
299 link.size() <= findDash + 1)
300 {
301 std::cerr << "Error finding device from symlink";
302 }
303 bus = std::stoi(link.substr(0, findDash));
304 address =
305 std::stoi(link.substr(findDash + 1), nullptr, 16);
306
James Feistde5e9702019-09-18 16:13:02 -0700307 auto findBus = baseConfiguration->second.find("Bus");
308 auto findAddress =
309 baseConfiguration->second.find("Address");
310 if (findBus == baseConfiguration->second.end() ||
311 findAddress == baseConfiguration->second.end())
312 {
313 std::cerr << baseConfiguration->first
314 << " missing bus or address\n";
315 continue;
316 }
317 unsigned int configBus = std::visit(
318 VariantToUnsignedIntVisitor(), findBus->second);
319 unsigned int configAddress = std::visit(
320 VariantToUnsignedIntVisitor(), findAddress->second);
321
322 if (configBus == bus && configAddress == address)
323 {
324 sensorData = &(sensor.second);
325 break;
326 }
327 }
328 }
329 if (sensorData == nullptr)
330 {
331 std::cerr << "failed to find match for " << path.string()
332 << "\n";
James Feist95b079b2018-11-21 09:28:00 -0800333 continue;
334 }
James Feist95b079b2018-11-21 09:28:00 -0800335
James Feistde5e9702019-09-18 16:13:02 -0700336 auto findSensorName = baseConfiguration->second.find("Name");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800337
James Feistde5e9702019-09-18 16:13:02 -0700338 if (findSensorName == baseConfiguration->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800339 {
James Feistde5e9702019-09-18 16:13:02 -0700340 std::cerr << "could not determine configuration name for "
341 << path.string() << "\n";
342 continue;
343 }
344 std::string sensorName =
345 std::get<std::string>(findSensorName->second);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800346
James Feistde5e9702019-09-18 16:13:02 -0700347 // on rescans, only update sensors we were signaled by
348 auto findSensor = tachSensors.find(sensorName);
349 if (!firstScan && findSensor != tachSensors.end())
350 {
351 bool found = false;
352 for (auto it = sensorsChanged->begin();
353 it != sensorsChanged->end(); it++)
354 {
355 if (boost::ends_with(*it, findSensor->second->name))
356 {
357 sensorsChanged->erase(it);
358 findSensor->second = nullptr;
359 found = true;
360 break;
361 }
362 }
363 if (!found)
364 {
365 continue;
366 }
367 }
368 std::vector<thresholds::Threshold> sensorThresholds;
369 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
370 {
371 std::cerr << "error populating thresholds for "
372 << sensorName << "\n";
373 }
374
375 auto presenceConfig =
376 sensorData->find(baseType + std::string(".Presence"));
377
378 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
379
380 // presence sensors are optional
381 if (presenceConfig != sensorData->end())
382 {
James Feistde5e9702019-09-18 16:13:02 -0700383 auto findPolarity = presenceConfig->second.find("Polarity");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800384 auto findPinName = presenceConfig->second.find("PinName");
James Feistde5e9702019-09-18 16:13:02 -0700385
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800386 if (findPinName == presenceConfig->second.end() ||
James Feistde5e9702019-09-18 16:13:02 -0700387 findPolarity == presenceConfig->second.end())
388 {
389 std::cerr << "Malformed Presence Configuration\n";
390 }
391 else
392 {
James Feistde5e9702019-09-18 16:13:02 -0700393 bool inverted = std::get<std::string>(
394 findPolarity->second) == "Low";
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800395 if (auto pinName =
396 std::get_if<std::string>(&findPinName->second))
397 {
398 presenceSensor = std::make_unique<PresenceSensor>(
399 *pinName, inverted, io, sensorName);
400 }
401 else
402 {
403 std::cerr
404 << "Malformed Presence pinName for sensor "
405 << sensorName << " \n";
406 }
James Feistde5e9702019-09-18 16:13:02 -0700407 }
408 }
409 std::optional<RedundancySensor>* redundancy = nullptr;
410 if (fanType == FanTypes::aspeed)
411 {
412 redundancy = &systemRedundancy;
413 }
414
Josh Lehanf920e092020-08-07 00:12:54 -0700415 PowerState powerState = PowerState::on;
416 auto findPower = baseConfiguration->second.find("PowerState");
417 if (findPower != baseConfiguration->second.end())
418 {
419 auto ptrPower =
420 std::get_if<std::string>(&(findPower->second));
421 if (ptrPower)
422 {
423 setReadState(*ptrPower, powerState);
424 }
425 }
426
James Feistde5e9702019-09-18 16:13:02 -0700427 constexpr double defaultMaxReading = 25000;
428 constexpr double defaultMinReading = 0;
Ed Tanousf69fbf92022-01-14 10:15:33 -0800429 std::pair<double, double> limits =
James Feistde5e9702019-09-18 16:13:02 -0700430 std::make_pair(defaultMinReading, defaultMaxReading);
431
James Feist49a8ccd2020-09-16 16:09:52 -0700432 auto connector =
433 sensorData->find(baseType + std::string(".Connector"));
434
435 std::optional<std::string> led;
Yong Zhao77b3add2021-01-09 04:25:18 +0000436 std::string pwmName;
Zhikui Rend05867c2021-02-26 16:10:34 -0800437 fs::path pwmPath;
James Feist49a8ccd2020-09-16 16:09:52 -0700438
Jie Yang3291b9c2021-07-29 14:46:51 -0700439 // The Mutable parameter is optional, defaulting to false
440 bool isValueMutable = false;
James Feist49a8ccd2020-09-16 16:09:52 -0700441 if (connector != sensorData->end())
442 {
443 auto findPwm = connector->second.find("Pwm");
444 if (findPwm != connector->second.end())
445 {
James Feist49a8ccd2020-09-16 16:09:52 -0700446 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
447 findPwm->second);
Howard Chiuddf25d12021-12-02 15:14:44 +0800448 if (!findPwmPath(directory, pwm, pwmPath))
449 {
450 std::cerr << "Connector for " << sensorName
451 << " no pwm channel found!\n";
452 continue;
453 }
454
455 fs::path pwmEnableFile =
456 "pwm" + std::to_string(pwm + 1) + "_enable";
457 fs::path enablePath =
458 pwmPath.parent_path() / pwmEnableFile;
459 enablePwm(enablePath);
460
James Feist49a8ccd2020-09-16 16:09:52 -0700461 /* use pwm name override if found in configuration else
462 * use default */
463 auto findOverride = connector->second.find("PwmName");
James Feist49a8ccd2020-09-16 16:09:52 -0700464 if (findOverride != connector->second.end())
465 {
466 pwmName = std::visit(VariantToStringVisitor(),
467 findOverride->second);
468 }
469 else
470 {
471 pwmName = "Pwm_" + std::to_string(pwm + 1);
472 }
Jie Yang3291b9c2021-07-29 14:46:51 -0700473
474 // Check PWM sensor mutability
475 auto findMutable = connector->second.find("Mutable");
476 if (findMutable != connector->second.end())
477 {
478 auto ptrMutable =
479 std::get_if<bool>(&(findMutable->second));
480 if (ptrMutable)
481 {
482 isValueMutable = *ptrMutable;
483 }
484 }
James Feist49a8ccd2020-09-16 16:09:52 -0700485 }
486 else
487 {
488 std::cerr << "Connector for " << sensorName
489 << " missing pwm!\n";
490 }
491
492 auto findLED = connector->second.find("LED");
493 if (findLED != connector->second.end())
494 {
495 auto ledName =
496 std::get_if<std::string>(&(findLED->second));
497 if (ledName == nullptr)
498 {
499 std::cerr << "Wrong format for LED of "
500 << sensorName << "\n";
501 }
502 else
503 {
504 led = *ledName;
505 }
506 }
507 }
508
James Feistde5e9702019-09-18 16:13:02 -0700509 findLimits(limits, baseConfiguration);
510 tachSensors[sensorName] = std::make_unique<TachSensor>(
511 path.string(), baseType, objectServer, dbusConnection,
512 std::move(presenceSensor), redundancy, io, sensorName,
Josh Lehanf920e092020-08-07 00:12:54 -0700513 std::move(sensorThresholds), *interfacePath, limits,
James Feist49a8ccd2020-09-16 16:09:52 -0700514 powerState, led);
Yong Zhao77b3add2021-01-09 04:25:18 +0000515
Zhikui Rend05867c2021-02-26 16:10:34 -0800516 if (!pwmPath.empty() && fs::exists(pwmPath) &&
517 !pwmSensors.count(pwmPath))
Yong Zhao77b3add2021-01-09 04:25:18 +0000518 {
519 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
520 pwmName, pwmPath, dbusConnection, objectServer,
Jie Yang3291b9c2021-07-29 14:46:51 -0700521 *interfacePath, "Fan", isValueMutable);
Yong Zhao77b3add2021-01-09 04:25:18 +0000522 }
James Feist95b079b2018-11-21 09:28:00 -0800523 }
Yong Zhao77b3add2021-01-09 04:25:18 +0000524
Kuiying Wangd5407412020-09-09 16:06:56 +0800525 createRedundancySensor(tachSensors, dbusConnection, objectServer);
Ed Tanous8a17c302021-09-02 15:07:11 -0700526 });
James Feistde5e9702019-09-18 16:13:02 -0700527 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700528 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
529 retries);
James Feist6714a252018-09-10 15:26:18 -0700530}
531
James Feistb6c0b912019-07-09 12:21:44 -0700532int main()
James Feist6714a252018-09-10 15:26:18 -0700533{
534 boost::asio::io_service io;
535 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
536 systemBus->request_name("xyz.openbmc_project.FanSensor");
537 sdbusplus::asio::object_server objectServer(systemBus);
538 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
539 tachSensors;
540 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
541 pwmSensors;
542 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700543 auto sensorsChanged =
544 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700545
546 io.post([&]() {
547 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
548 nullptr);
549 });
550
551 boost::asio::deadline_timer filterTimer(io);
552 std::function<void(sdbusplus::message::message&)> eventHandler =
553 [&](sdbusplus::message::message& message) {
554 if (message.is_method_error())
555 {
556 std::cerr << "callback method error\n";
557 return;
558 }
559 sensorsChanged->insert(message.get_path());
560 // this implicitly cancels the timer
561 filterTimer.expires_from_now(boost::posix_time::seconds(1));
562
563 filterTimer.async_wait([&](const boost::system::error_code& ec) {
564 if (ec == boost::asio::error::operation_aborted)
565 {
566 /* we were canceled*/
567 return;
568 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700569 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700570 {
571 std::cerr << "timer error\n";
572 return;
573 }
574 createSensors(io, objectServer, tachSensors, pwmSensors,
James Feistf27a55c2020-08-04 14:27:30 -0700575 systemBus, sensorsChanged, 5);
James Feist6714a252018-09-10 15:26:18 -0700576 });
577 };
578
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700579 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700580 {
581 auto match = std::make_unique<sdbusplus::bus::match::match>(
582 static_cast<sdbusplus::bus::bus&>(*systemBus),
583 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700584 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700585 eventHandler);
586 matches.emplace_back(std::move(match));
587 }
588
James Feistdc6c55f2018-10-31 12:53:20 -0700589 // redundancy sensor
590 std::function<void(sdbusplus::message::message&)> redundancyHandler =
591 [&tachSensors, &systemBus,
James Feistb6c0b912019-07-09 12:21:44 -0700592 &objectServer](sdbusplus::message::message&) {
James Feistdc6c55f2018-10-31 12:53:20 -0700593 createRedundancySensor(tachSensors, systemBus, objectServer);
594 };
595 auto match = std::make_unique<sdbusplus::bus::match::match>(
596 static_cast<sdbusplus::bus::bus&>(*systemBus),
597 "type='signal',member='PropertiesChanged',path_namespace='" +
598 std::string(inventoryPath) + "',arg0namespace='" +
599 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700600 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700601 matches.emplace_back(std::move(match));
602
Bruce Lee1263c3d2021-06-04 15:16:33 +0800603 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700604 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700605 return 0;
James Feist6714a252018-09-10 15:26:18 -0700606}