blob: a5a584427e2488c12b0f7cf41ebb0b6821446519 [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);
Jae Hyun Yoo241356e2020-02-20 12:48:04 -080072 if (boost::ends_with(canonical, "1e786000.pwm-tacho-controller") ||
Howard Chiuddf25d12021-12-02 15:14:44 +080073 boost::ends_with(canonical, "1e610000.pwm-tacho-controller") ||
74 boost::ends_with(canonical, "1e610000.pwm_tach:tach"))
James Feist95b079b2018-11-21 09:28:00 -080075 {
76 return FanTypes::aspeed;
77 }
Ed Tanous8a57ec02020-10-09 12:46:52 -070078 if (boost::ends_with(canonical, "f0103000.pwm-fan-controller"))
Peter Lundgren8843b622019-09-12 10:33:41 -070079 {
80 return FanTypes::nuvoton;
81 }
James Feist95b079b2018-11-21 09:28:00 -080082 // todo: will we need to support other types?
83 return FanTypes::i2c;
84}
Jeff Linabf91de2020-12-23 10:55:42 +080085void enablePwm(const fs::path& filePath)
86{
87 std::fstream enableFile(filePath, std::ios::in | std::ios::out);
88 if (!enableFile.good())
89 {
90 std::cerr << "Error read/write " << filePath << "\n";
91 return;
92 }
James Feistdc6c55f2018-10-31 12:53:20 -070093
Jeff Linabf91de2020-12-23 10:55:42 +080094 std::string regulateMode;
95 std::getline(enableFile, regulateMode);
96 if (regulateMode == "0")
97 {
98 enableFile << 1;
99 }
100}
Howard Chiuddf25d12021-12-02 15:14:44 +0800101bool findPwmfanPath(unsigned int configPwmfanIndex, fs::path& pwmPath)
102{
103 /* Search PWM since pwm-fan had separated
104 * PWM from tach directory and 1 channel only*/
105 std::vector<fs::path> pwmfanPaths;
106 std::string pwnfanDevName("pwm-fan");
107
108 pwnfanDevName += std::to_string(configPwmfanIndex);
109
110 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwmfanPaths))
111 {
112 std::cerr << "No PWMs are found!\n";
113 return false;
114 }
115 for (const auto& path : pwmfanPaths)
116 {
117 std::error_code ec;
118 fs::path link = fs::read_symlink(path.parent_path() / "device", ec);
119
120 if (ec)
121 {
122 std::cerr << "read_symlink() failed: " << ec.message() << " ("
123 << ec.value() << ")\n";
124 continue;
125 }
126
127 if (link.filename().string() == pwnfanDevName)
128 {
129 pwmPath = path;
130 return true;
131 }
132 }
133 return false;
134}
135bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath)
136{
137 std::error_code ec;
138
139 /* Assuming PWM file is appeared in the same directory as fanX_input */
140 auto path = directory / ("pwm" + std::to_string(pwm + 1));
141 bool exists = fs::exists(path, ec);
142
143 if (ec || !exists)
144 {
145 /* PWM file not exist or error happened */
146 if (ec)
147 {
148 std::cerr << "exists() failed: " << ec.message() << " ("
149 << ec.value() << ")\n";
150 }
151 /* try search form pwm-fanX directory */
152 return findPwmfanPath(pwm, pwmPath);
153 }
154
155 pwmPath = path;
156 return true;
157}
Kuiying Wangd5407412020-09-09 16:06:56 +0800158void createRedundancySensor(
159 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
160 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700161 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800162 sdbusplus::asio::object_server& objectServer)
163{
164
165 conn->async_method_call(
166 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700167 const ManagedObjectType& managedObj) {
Kuiying Wangd5407412020-09-09 16:06:56 +0800168 if (ec)
169 {
170 std::cerr << "Error calling entity manager \n";
171 return;
172 }
173 for (const auto& pathPair : managedObj)
174 {
175 for (const auto& interfacePair : pathPair.second)
176 {
177 if (interfacePair.first == redundancyConfiguration)
178 {
179 // currently only support one
180 auto findCount =
181 interfacePair.second.find("AllowedFailures");
182 if (findCount == interfacePair.second.end())
183 {
184 std::cerr << "Malformed redundancy record \n";
185 return;
186 }
187 std::vector<std::string> sensorList;
188
189 for (const auto& sensor : sensors)
190 {
191 sensorList.push_back(
192 "/xyz/openbmc_project/sensors/fan_tach/" +
193 sensor.second->name);
194 }
195 systemRedundancy.reset();
196 systemRedundancy.emplace(RedundancySensor(
197 std::get<uint64_t>(findCount->second), sensorList,
198 objectServer, pathPair.first));
199
200 return;
201 }
202 }
203 }
204 },
205 "xyz.openbmc_project.EntityManager", "/",
206 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
207}
208
James Feist6714a252018-09-10 15:26:18 -0700209void createSensors(
210 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
211 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
212 tachSensors,
213 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
214 pwmSensors,
215 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700216 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700217 sensorsChanged,
218 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700219{
James Feistde5e9702019-09-18 16:13:02 -0700220 auto getter = std::make_shared<GetSensorConfiguration>(
221 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700222 [&io, &objectServer, &tachSensors, &pwmSensors, &dbusConnection,
223 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
James Feistde5e9702019-09-18 16:13:02 -0700224 bool firstScan = sensorsChanged == nullptr;
225 std::vector<fs::path> paths;
226 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)",
227 paths))
James Feist95b079b2018-11-21 09:28:00 -0800228 {
Yong Zhao77b3add2021-01-09 04:25:18 +0000229 std::cerr << "No fan sensors in system\n";
James Feistde5e9702019-09-18 16:13:02 -0700230 return;
James Feist95b079b2018-11-21 09:28:00 -0800231 }
James Feist6714a252018-09-10 15:26:18 -0700232
James Feistde5e9702019-09-18 16:13:02 -0700233 // iterate through all found fan sensors, and try to match them with
234 // configuration
235 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -0700236 {
James Feistde5e9702019-09-18 16:13:02 -0700237 std::smatch match;
238 std::string pathStr = path.string();
239
240 std::regex_search(pathStr, match, inputRegex);
241 std::string indexStr = *(match.begin() + 1);
242
Yong Zhao77b3add2021-01-09 04:25:18 +0000243 fs::path directory = path.parent_path();
James Feistde5e9702019-09-18 16:13:02 -0700244 FanTypes fanType = getFanType(directory);
Yong Zhao77b3add2021-01-09 04:25:18 +0000245
James Feistde5e9702019-09-18 16:13:02 -0700246 // convert to 0 based
247 size_t index = std::stoul(indexStr) - 1;
248
Ed Tanousa771f6a2022-01-14 09:36:51 -0800249 const char* baseType = nullptr;
James Feistde5e9702019-09-18 16:13:02 -0700250 const SensorData* sensorData = nullptr;
251 const std::string* interfacePath = nullptr;
252 const SensorBaseConfiguration* baseConfiguration = nullptr;
253 for (const std::pair<sdbusplus::message::object_path,
254 SensorData>& sensor : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800255 {
James Feistde5e9702019-09-18 16:13:02 -0700256 // find the base of the configuration to see if indexes
257 // match
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +0000258 auto sensorBaseFind =
259 sensor.second.find(sensorTypes[fanType]);
260 if (sensorBaseFind == sensor.second.end())
James Feistde5e9702019-09-18 16:13:02 -0700261 {
262 continue;
263 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800264
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +0000265 baseConfiguration = &(*sensorBaseFind);
266 interfacePath = &(sensor.first.str);
267 baseType = sensorTypes[fanType];
268
James Feistde5e9702019-09-18 16:13:02 -0700269 auto findIndex = baseConfiguration->second.find("Index");
270 if (findIndex == baseConfiguration->second.end())
271 {
272 std::cerr << baseConfiguration->first
273 << " missing index\n";
274 continue;
275 }
276 unsigned int configIndex = std::visit(
277 VariantToUnsignedIntVisitor(), findIndex->second);
278 if (configIndex != index)
279 {
280 continue;
281 }
282 if (fanType == FanTypes::aspeed ||
283 fanType == FanTypes::nuvoton)
284 {
285 // there will be only 1 aspeed or nuvoton sensor object
286 // in sysfs, we found the fan
287 sensorData = &(sensor.second);
288 break;
289 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700290 if (fanType == FanTypes::i2c)
James Feistde5e9702019-09-18 16:13:02 -0700291 {
Yong Zhaoadd46822021-01-09 04:52:36 +0000292 size_t bus = 0;
293 size_t address = 0;
294
295 std::string link =
296 fs::read_symlink(directory / "device").filename();
297
298 size_t findDash = link.find('-');
299 if (findDash == std::string::npos ||
300 link.size() <= findDash + 1)
301 {
302 std::cerr << "Error finding device from symlink";
303 }
304 bus = std::stoi(link.substr(0, findDash));
305 address =
306 std::stoi(link.substr(findDash + 1), nullptr, 16);
307
James Feistde5e9702019-09-18 16:13:02 -0700308 auto findBus = baseConfiguration->second.find("Bus");
309 auto findAddress =
310 baseConfiguration->second.find("Address");
311 if (findBus == baseConfiguration->second.end() ||
312 findAddress == baseConfiguration->second.end())
313 {
314 std::cerr << baseConfiguration->first
315 << " missing bus or address\n";
316 continue;
317 }
318 unsigned int configBus = std::visit(
319 VariantToUnsignedIntVisitor(), findBus->second);
320 unsigned int configAddress = std::visit(
321 VariantToUnsignedIntVisitor(), findAddress->second);
322
323 if (configBus == bus && configAddress == address)
324 {
325 sensorData = &(sensor.second);
326 break;
327 }
328 }
329 }
330 if (sensorData == nullptr)
331 {
332 std::cerr << "failed to find match for " << path.string()
333 << "\n";
James Feist95b079b2018-11-21 09:28:00 -0800334 continue;
335 }
James Feist95b079b2018-11-21 09:28:00 -0800336
James Feistde5e9702019-09-18 16:13:02 -0700337 auto findSensorName = baseConfiguration->second.find("Name");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800338
James Feistde5e9702019-09-18 16:13:02 -0700339 if (findSensorName == baseConfiguration->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800340 {
James Feistde5e9702019-09-18 16:13:02 -0700341 std::cerr << "could not determine configuration name for "
342 << path.string() << "\n";
343 continue;
344 }
345 std::string sensorName =
346 std::get<std::string>(findSensorName->second);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800347
James Feistde5e9702019-09-18 16:13:02 -0700348 // on rescans, only update sensors we were signaled by
349 auto findSensor = tachSensors.find(sensorName);
350 if (!firstScan && findSensor != tachSensors.end())
351 {
352 bool found = false;
353 for (auto it = sensorsChanged->begin();
354 it != sensorsChanged->end(); it++)
355 {
356 if (boost::ends_with(*it, findSensor->second->name))
357 {
358 sensorsChanged->erase(it);
359 findSensor->second = nullptr;
360 found = true;
361 break;
362 }
363 }
364 if (!found)
365 {
366 continue;
367 }
368 }
369 std::vector<thresholds::Threshold> sensorThresholds;
370 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
371 {
372 std::cerr << "error populating thresholds for "
373 << sensorName << "\n";
374 }
375
376 auto presenceConfig =
377 sensorData->find(baseType + std::string(".Presence"));
378
379 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
380
381 // presence sensors are optional
382 if (presenceConfig != sensorData->end())
383 {
James Feistde5e9702019-09-18 16:13:02 -0700384 auto findPolarity = presenceConfig->second.find("Polarity");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800385 auto findPinName = presenceConfig->second.find("PinName");
James Feistde5e9702019-09-18 16:13:02 -0700386
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800387 if (findPinName == presenceConfig->second.end() ||
James Feistde5e9702019-09-18 16:13:02 -0700388 findPolarity == presenceConfig->second.end())
389 {
390 std::cerr << "Malformed Presence Configuration\n";
391 }
392 else
393 {
James Feistde5e9702019-09-18 16:13:02 -0700394 bool inverted = std::get<std::string>(
395 findPolarity->second) == "Low";
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800396 if (auto pinName =
397 std::get_if<std::string>(&findPinName->second))
398 {
399 presenceSensor = std::make_unique<PresenceSensor>(
400 *pinName, inverted, io, sensorName);
401 }
402 else
403 {
404 std::cerr
405 << "Malformed Presence pinName for sensor "
406 << sensorName << " \n";
407 }
James Feistde5e9702019-09-18 16:13:02 -0700408 }
409 }
410 std::optional<RedundancySensor>* redundancy = nullptr;
411 if (fanType == FanTypes::aspeed)
412 {
413 redundancy = &systemRedundancy;
414 }
415
Josh Lehanf920e092020-08-07 00:12:54 -0700416 PowerState powerState = PowerState::on;
417 auto findPower = baseConfiguration->second.find("PowerState");
418 if (findPower != baseConfiguration->second.end())
419 {
420 auto ptrPower =
421 std::get_if<std::string>(&(findPower->second));
422 if (ptrPower)
423 {
424 setReadState(*ptrPower, powerState);
425 }
426 }
427
James Feistde5e9702019-09-18 16:13:02 -0700428 constexpr double defaultMaxReading = 25000;
429 constexpr double defaultMinReading = 0;
Ed Tanousf69fbf92022-01-14 10:15:33 -0800430 std::pair<double, double> limits =
James Feistde5e9702019-09-18 16:13:02 -0700431 std::make_pair(defaultMinReading, defaultMaxReading);
432
James Feist49a8ccd2020-09-16 16:09:52 -0700433 auto connector =
434 sensorData->find(baseType + std::string(".Connector"));
435
436 std::optional<std::string> led;
Yong Zhao77b3add2021-01-09 04:25:18 +0000437 std::string pwmName;
Zhikui Rend05867c2021-02-26 16:10:34 -0800438 fs::path pwmPath;
James Feist49a8ccd2020-09-16 16:09:52 -0700439
Jie Yang3291b9c2021-07-29 14:46:51 -0700440 // The Mutable parameter is optional, defaulting to false
441 bool isValueMutable = false;
James Feist49a8ccd2020-09-16 16:09:52 -0700442 if (connector != sensorData->end())
443 {
444 auto findPwm = connector->second.find("Pwm");
445 if (findPwm != connector->second.end())
446 {
James Feist49a8ccd2020-09-16 16:09:52 -0700447 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
448 findPwm->second);
Howard Chiuddf25d12021-12-02 15:14:44 +0800449 if (!findPwmPath(directory, pwm, pwmPath))
450 {
451 std::cerr << "Connector for " << sensorName
452 << " no pwm channel found!\n";
453 continue;
454 }
455
456 fs::path pwmEnableFile =
457 "pwm" + std::to_string(pwm + 1) + "_enable";
458 fs::path enablePath =
459 pwmPath.parent_path() / pwmEnableFile;
460 enablePwm(enablePath);
461
James Feist49a8ccd2020-09-16 16:09:52 -0700462 /* use pwm name override if found in configuration else
463 * use default */
464 auto findOverride = connector->second.find("PwmName");
James Feist49a8ccd2020-09-16 16:09:52 -0700465 if (findOverride != connector->second.end())
466 {
467 pwmName = std::visit(VariantToStringVisitor(),
468 findOverride->second);
469 }
470 else
471 {
472 pwmName = "Pwm_" + std::to_string(pwm + 1);
473 }
Jie Yang3291b9c2021-07-29 14:46:51 -0700474
475 // Check PWM sensor mutability
476 auto findMutable = connector->second.find("Mutable");
477 if (findMutable != connector->second.end())
478 {
479 auto ptrMutable =
480 std::get_if<bool>(&(findMutable->second));
481 if (ptrMutable)
482 {
483 isValueMutable = *ptrMutable;
484 }
485 }
James Feist49a8ccd2020-09-16 16:09:52 -0700486 }
487 else
488 {
489 std::cerr << "Connector for " << sensorName
490 << " missing pwm!\n";
491 }
492
493 auto findLED = connector->second.find("LED");
494 if (findLED != connector->second.end())
495 {
496 auto ledName =
497 std::get_if<std::string>(&(findLED->second));
498 if (ledName == nullptr)
499 {
500 std::cerr << "Wrong format for LED of "
501 << sensorName << "\n";
502 }
503 else
504 {
505 led = *ledName;
506 }
507 }
508 }
509
James Feistde5e9702019-09-18 16:13:02 -0700510 findLimits(limits, baseConfiguration);
511 tachSensors[sensorName] = std::make_unique<TachSensor>(
512 path.string(), baseType, objectServer, dbusConnection,
513 std::move(presenceSensor), redundancy, io, sensorName,
Josh Lehanf920e092020-08-07 00:12:54 -0700514 std::move(sensorThresholds), *interfacePath, limits,
James Feist49a8ccd2020-09-16 16:09:52 -0700515 powerState, led);
Yong Zhao77b3add2021-01-09 04:25:18 +0000516
Zhikui Rend05867c2021-02-26 16:10:34 -0800517 if (!pwmPath.empty() && fs::exists(pwmPath) &&
518 !pwmSensors.count(pwmPath))
Yong Zhao77b3add2021-01-09 04:25:18 +0000519 {
520 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
521 pwmName, pwmPath, dbusConnection, objectServer,
Jie Yang3291b9c2021-07-29 14:46:51 -0700522 *interfacePath, "Fan", isValueMutable);
Yong Zhao77b3add2021-01-09 04:25:18 +0000523 }
James Feist95b079b2018-11-21 09:28:00 -0800524 }
Yong Zhao77b3add2021-01-09 04:25:18 +0000525
Kuiying Wangd5407412020-09-09 16:06:56 +0800526 createRedundancySensor(tachSensors, dbusConnection, objectServer);
Ed Tanous8a17c302021-09-02 15:07:11 -0700527 });
James Feistde5e9702019-09-18 16:13:02 -0700528 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700529 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
530 retries);
James Feist6714a252018-09-10 15:26:18 -0700531}
532
James Feistb6c0b912019-07-09 12:21:44 -0700533int main()
James Feist6714a252018-09-10 15:26:18 -0700534{
535 boost::asio::io_service io;
536 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
537 systemBus->request_name("xyz.openbmc_project.FanSensor");
538 sdbusplus::asio::object_server objectServer(systemBus);
539 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
540 tachSensors;
541 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
542 pwmSensors;
543 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700544 auto sensorsChanged =
545 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700546
547 io.post([&]() {
548 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
549 nullptr);
550 });
551
552 boost::asio::deadline_timer filterTimer(io);
553 std::function<void(sdbusplus::message::message&)> eventHandler =
554 [&](sdbusplus::message::message& message) {
555 if (message.is_method_error())
556 {
557 std::cerr << "callback method error\n";
558 return;
559 }
560 sensorsChanged->insert(message.get_path());
561 // this implicitly cancels the timer
562 filterTimer.expires_from_now(boost::posix_time::seconds(1));
563
564 filterTimer.async_wait([&](const boost::system::error_code& ec) {
565 if (ec == boost::asio::error::operation_aborted)
566 {
567 /* we were canceled*/
568 return;
569 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700570 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700571 {
572 std::cerr << "timer error\n";
573 return;
574 }
575 createSensors(io, objectServer, tachSensors, pwmSensors,
James Feistf27a55c2020-08-04 14:27:30 -0700576 systemBus, sensorsChanged, 5);
James Feist6714a252018-09-10 15:26:18 -0700577 });
578 };
579
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700580 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700581 {
582 auto match = std::make_unique<sdbusplus::bus::match::match>(
583 static_cast<sdbusplus::bus::bus&>(*systemBus),
584 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700585 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700586 eventHandler);
587 matches.emplace_back(std::move(match));
588 }
589
James Feistdc6c55f2018-10-31 12:53:20 -0700590 // redundancy sensor
591 std::function<void(sdbusplus::message::message&)> redundancyHandler =
592 [&tachSensors, &systemBus,
James Feistb6c0b912019-07-09 12:21:44 -0700593 &objectServer](sdbusplus::message::message&) {
James Feistdc6c55f2018-10-31 12:53:20 -0700594 createRedundancySensor(tachSensors, systemBus, objectServer);
595 };
596 auto match = std::make_unique<sdbusplus::bus::match::match>(
597 static_cast<sdbusplus::bus::bus&>(*systemBus),
598 "type='signal',member='PropertiesChanged',path_namespace='" +
599 std::string(inventoryPath) + "',arg0namespace='" +
600 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700601 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700602 matches.emplace_back(std::move(match));
603
Bruce Lee1263c3d2021-06-04 15:16:33 +0800604 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700605 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700606 return 0;
James Feist6714a252018-09-10 15:26:18 -0700607}