blob: b52abd9609aeda64c8d4306bd42d12dd449e43d6 [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 }
Zev Weiss77636ec2022-08-12 18:21:01 -0700171 for (const auto& [path, interfaces] : managedObj)
Ed Tanousbb679322022-05-16 16:10:00 -0700172 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700173 for (const auto& [intf, cfg] : interfaces)
Kuiying Wangd5407412020-09-09 16:06:56 +0800174 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700175 if (intf == redundancyConfiguration)
Kuiying Wangd5407412020-09-09 16:06:56 +0800176 {
Ed Tanousbb679322022-05-16 16:10:00 -0700177 // currently only support one
Zev Weiss77636ec2022-08-12 18:21:01 -0700178 auto findCount = cfg.find("AllowedFailures");
179 if (findCount == cfg.end())
Kuiying Wangd5407412020-09-09 16:06:56 +0800180 {
Ed Tanousbb679322022-05-16 16:10:00 -0700181 std::cerr << "Malformed redundancy record \n";
Kuiying Wangd5407412020-09-09 16:06:56 +0800182 return;
183 }
Ed Tanousbb679322022-05-16 16:10:00 -0700184 std::vector<std::string> sensorList;
185
Zev Weiss77636ec2022-08-12 18:21:01 -0700186 for (const auto& [name, sensor] : sensors)
Ed Tanousbb679322022-05-16 16:10:00 -0700187 {
188 sensorList.push_back(
189 "/xyz/openbmc_project/sensors/fan_tach/" +
Zev Weiss77636ec2022-08-12 18:21:01 -0700190 sensor->name);
Ed Tanousbb679322022-05-16 16:10:00 -0700191 }
192 systemRedundancy.reset();
Zev Weiss77636ec2022-08-12 18:21:01 -0700193 systemRedundancy.emplace(
194 RedundancySensor(std::get<uint64_t>(findCount->second),
195 sensorList, objectServer, path));
Ed Tanousbb679322022-05-16 16:10:00 -0700196
197 return;
Kuiying Wangd5407412020-09-09 16:06:56 +0800198 }
199 }
Ed Tanousbb679322022-05-16 16:10:00 -0700200 }
Kuiying Wangd5407412020-09-09 16:06:56 +0800201 },
202 "xyz.openbmc_project.EntityManager", "/",
203 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
204}
205
James Feist6714a252018-09-10 15:26:18 -0700206void createSensors(
207 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Josh Lehan5170fe62022-08-03 13:17:41 -0700208 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700209 tachSensors,
210 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
211 pwmSensors,
212 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700213 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700214 sensorsChanged,
215 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700216{
James Feistde5e9702019-09-18 16:13:02 -0700217 auto getter = std::make_shared<GetSensorConfiguration>(
218 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700219 [&io, &objectServer, &tachSensors, &pwmSensors, &dbusConnection,
220 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700221 bool firstScan = sensorsChanged == nullptr;
222 std::vector<fs::path> paths;
223 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
224 {
225 std::cerr << "No fan sensors in system\n";
226 return;
227 }
228
229 // iterate through all found fan sensors, and try to match them with
230 // configuration
231 for (const auto& path : paths)
232 {
233 std::smatch match;
234 std::string pathStr = path.string();
235
236 std::regex_search(pathStr, match, inputRegex);
237 std::string indexStr = *(match.begin() + 1);
238
239 fs::path directory = path.parent_path();
240 FanTypes fanType = getFanType(directory);
241
242 // convert to 0 based
243 size_t index = std::stoul(indexStr) - 1;
244
245 const char* baseType = nullptr;
246 const SensorData* sensorData = nullptr;
247 const std::string* interfacePath = nullptr;
248 const SensorBaseConfiguration* baseConfiguration = nullptr;
Zev Weiss77636ec2022-08-12 18:21:01 -0700249 for (const auto& [path, cfgData] : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800250 {
Ed Tanousbb679322022-05-16 16:10:00 -0700251 // find the base of the configuration to see if indexes
252 // match
Zev Weiss77636ec2022-08-12 18:21:01 -0700253 auto sensorBaseFind = cfgData.find(sensorTypes[fanType]);
254 if (sensorBaseFind == cfgData.end())
James Feist95b079b2018-11-21 09:28:00 -0800255 {
Ed Tanousbb679322022-05-16 16:10:00 -0700256 continue;
257 }
258
259 baseConfiguration = &(*sensorBaseFind);
Zev Weiss77636ec2022-08-12 18:21:01 -0700260 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700261 baseType = sensorTypes[fanType];
262
263 auto findIndex = baseConfiguration->second.find("Index");
264 if (findIndex == baseConfiguration->second.end())
265 {
266 std::cerr << baseConfiguration->first << " missing index\n";
267 continue;
268 }
269 unsigned int configIndex = std::visit(
270 VariantToUnsignedIntVisitor(), findIndex->second);
271 if (configIndex != index)
272 {
273 continue;
274 }
275 if (fanType == FanTypes::aspeed || fanType == FanTypes::nuvoton)
276 {
277 // there will be only 1 aspeed or nuvoton sensor object
278 // in sysfs, we found the fan
Zev Weiss77636ec2022-08-12 18:21:01 -0700279 sensorData = &cfgData;
Ed Tanousbb679322022-05-16 16:10:00 -0700280 break;
281 }
282 if (fanType == FanTypes::i2c)
283 {
284 size_t bus = 0;
285 size_t address = 0;
286
287 std::string link =
288 fs::read_symlink(directory / "device").filename();
289
290 size_t findDash = link.find('-');
291 if (findDash == std::string::npos ||
292 link.size() <= findDash + 1)
James Feistde5e9702019-09-18 16:13:02 -0700293 {
Ed Tanousbb679322022-05-16 16:10:00 -0700294 std::cerr << "Error finding device from symlink";
James Feistde5e9702019-09-18 16:13:02 -0700295 }
Ed Tanousbb679322022-05-16 16:10:00 -0700296 bus = std::stoi(link.substr(0, findDash));
297 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800298
Ed Tanousbb679322022-05-16 16:10:00 -0700299 auto findBus = baseConfiguration->second.find("Bus");
300 auto findAddress =
301 baseConfiguration->second.find("Address");
302 if (findBus == baseConfiguration->second.end() ||
303 findAddress == baseConfiguration->second.end())
James Feistde5e9702019-09-18 16:13:02 -0700304 {
305 std::cerr << baseConfiguration->first
Ed Tanousbb679322022-05-16 16:10:00 -0700306 << " missing bus or address\n";
James Feistde5e9702019-09-18 16:13:02 -0700307 continue;
308 }
Ed Tanousbb679322022-05-16 16:10:00 -0700309 unsigned int configBus = std::visit(
310 VariantToUnsignedIntVisitor(), findBus->second);
311 unsigned int configAddress = std::visit(
312 VariantToUnsignedIntVisitor(), findAddress->second);
313
314 if (configBus == bus && configAddress == address)
James Feistde5e9702019-09-18 16:13:02 -0700315 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700316 sensorData = &cfgData;
James Feistde5e9702019-09-18 16:13:02 -0700317 break;
318 }
Ed Tanousbb679322022-05-16 16:10:00 -0700319 }
320 }
321 if (sensorData == nullptr)
322 {
323 std::cerr << "failed to find match for " << path.string()
324 << "\n";
325 continue;
326 }
327
328 auto findSensorName = baseConfiguration->second.find("Name");
329
330 if (findSensorName == baseConfiguration->second.end())
331 {
332 std::cerr << "could not determine configuration name for "
333 << path.string() << "\n";
334 continue;
335 }
336 std::string sensorName =
337 std::get<std::string>(findSensorName->second);
338
339 // on rescans, only update sensors we were signaled by
340 auto findSensor = tachSensors.find(sensorName);
341 if (!firstScan && findSensor != tachSensors.end())
342 {
343 bool found = false;
344 for (auto it = sensorsChanged->begin();
345 it != sensorsChanged->end(); it++)
346 {
347 if (boost::ends_with(*it, findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700348 {
Ed Tanousbb679322022-05-16 16:10:00 -0700349 sensorsChanged->erase(it);
350 findSensor->second = nullptr;
351 found = true;
352 break;
James Feistde5e9702019-09-18 16:13:02 -0700353 }
354 }
Ed Tanousbb679322022-05-16 16:10:00 -0700355 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700356 {
James Feist95b079b2018-11-21 09:28:00 -0800357 continue;
358 }
Ed Tanousbb679322022-05-16 16:10:00 -0700359 }
360 std::vector<thresholds::Threshold> sensorThresholds;
361 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
362 {
363 std::cerr << "error populating thresholds for " << sensorName
364 << "\n";
365 }
James Feist95b079b2018-11-21 09:28:00 -0800366
Ed Tanousbb679322022-05-16 16:10:00 -0700367 auto presenceConfig =
368 sensorData->find(baseType + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800369
Ed Tanousbb679322022-05-16 16:10:00 -0700370 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
371
372 // presence sensors are optional
373 if (presenceConfig != sensorData->end())
374 {
375 auto findPolarity = presenceConfig->second.find("Polarity");
376 auto findPinName = presenceConfig->second.find("PinName");
377
378 if (findPinName == presenceConfig->second.end() ||
379 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800380 {
Ed Tanousbb679322022-05-16 16:10:00 -0700381 std::cerr << "Malformed Presence Configuration\n";
James Feistde5e9702019-09-18 16:13:02 -0700382 }
Ed Tanousbb679322022-05-16 16:10:00 -0700383 else
James Feistde5e9702019-09-18 16:13:02 -0700384 {
Ed Tanousbb679322022-05-16 16:10:00 -0700385 bool inverted =
386 std::get<std::string>(findPolarity->second) == "Low";
Ed Tanous2049bd22022-07-09 07:20:26 -0700387 if (const auto* pinName =
Ed Tanousbb679322022-05-16 16:10:00 -0700388 std::get_if<std::string>(&findPinName->second))
James Feistde5e9702019-09-18 16:13:02 -0700389 {
Ed Tanousbb679322022-05-16 16:10:00 -0700390 presenceSensor = std::make_unique<PresenceSensor>(
391 *pinName, inverted, io, sensorName);
James Feistde5e9702019-09-18 16:13:02 -0700392 }
393 else
394 {
Ed Tanousbb679322022-05-16 16:10:00 -0700395 std::cerr << "Malformed Presence pinName for sensor "
396 << sensorName << " \n";
James Feistde5e9702019-09-18 16:13:02 -0700397 }
398 }
Ed Tanousbb679322022-05-16 16:10:00 -0700399 }
400 std::optional<RedundancySensor>* redundancy = nullptr;
401 if (fanType == FanTypes::aspeed)
402 {
403 redundancy = &systemRedundancy;
404 }
405
Zev Weissa4d27682022-07-19 15:30:36 -0700406 PowerState powerState = getPowerState(baseConfiguration->second);
Yong Zhao77b3add2021-01-09 04:25:18 +0000407
Ed Tanousbb679322022-05-16 16:10:00 -0700408 constexpr double defaultMaxReading = 25000;
409 constexpr double defaultMinReading = 0;
410 std::pair<double, double> limits =
411 std::make_pair(defaultMinReading, defaultMaxReading);
412
413 auto connector =
414 sensorData->find(baseType + std::string(".Connector"));
415
416 std::optional<std::string> led;
417 std::string pwmName;
418 fs::path pwmPath;
419
420 // The Mutable parameter is optional, defaulting to false
421 bool isValueMutable = false;
422 if (connector != sensorData->end())
423 {
424 auto findPwm = connector->second.find("Pwm");
425 if (findPwm != connector->second.end())
426 {
427 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
428 findPwm->second);
429 if (!findPwmPath(directory, pwm, pwmPath))
430 {
431 std::cerr << "Connector for " << sensorName
432 << " no pwm channel found!\n";
433 continue;
434 }
435
436 fs::path pwmEnableFile =
437 "pwm" + std::to_string(pwm + 1) + "_enable";
438 fs::path enablePath = pwmPath.parent_path() / pwmEnableFile;
439 enablePwm(enablePath);
440
441 /* use pwm name override if found in configuration else
442 * use default */
443 auto findOverride = connector->second.find("PwmName");
444 if (findOverride != connector->second.end())
445 {
446 pwmName = std::visit(VariantToStringVisitor(),
447 findOverride->second);
448 }
449 else
450 {
451 pwmName = "Pwm_" + std::to_string(pwm + 1);
452 }
453
454 // Check PWM sensor mutability
455 auto findMutable = connector->second.find("Mutable");
456 if (findMutable != connector->second.end())
457 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700458 const auto* ptrMutable =
Ed Tanousbb679322022-05-16 16:10:00 -0700459 std::get_if<bool>(&(findMutable->second));
Ed Tanous2049bd22022-07-09 07:20:26 -0700460 if (ptrMutable != nullptr)
Ed Tanousbb679322022-05-16 16:10:00 -0700461 {
462 isValueMutable = *ptrMutable;
463 }
464 }
465 }
466 else
467 {
468 std::cerr << "Connector for " << sensorName
469 << " missing pwm!\n";
470 }
471
472 auto findLED = connector->second.find("LED");
473 if (findLED != connector->second.end())
474 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700475 const auto* ledName =
476 std::get_if<std::string>(&(findLED->second));
Ed Tanousbb679322022-05-16 16:10:00 -0700477 if (ledName == nullptr)
478 {
479 std::cerr << "Wrong format for LED of " << sensorName
480 << "\n";
481 }
482 else
483 {
484 led = *ledName;
485 }
486 }
487 }
488
489 findLimits(limits, baseConfiguration);
Josh Lehan5170fe62022-08-03 13:17:41 -0700490
491 auto& tachSensor = tachSensors[sensorName];
492 tachSensor = nullptr;
493 tachSensor = std::make_shared<TachSensor>(
Ed Tanousbb679322022-05-16 16:10:00 -0700494 path.string(), baseType, objectServer, dbusConnection,
495 std::move(presenceSensor), redundancy, io, sensorName,
496 std::move(sensorThresholds), *interfacePath, limits, powerState,
497 led);
Josh Lehan5170fe62022-08-03 13:17:41 -0700498 tachSensor->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700499
500 if (!pwmPath.empty() && fs::exists(pwmPath) &&
Ed Tanous2049bd22022-07-09 07:20:26 -0700501 (pwmSensors.count(pwmPath) == 0U))
Ed Tanousbb679322022-05-16 16:10:00 -0700502 {
503 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
504 pwmName, pwmPath, dbusConnection, objectServer,
505 *interfacePath, "Fan", isValueMutable);
506 }
507 }
508
509 createRedundancySensor(tachSensors, dbusConnection, objectServer);
Ed Tanous8a17c302021-09-02 15:07:11 -0700510 });
James Feistde5e9702019-09-18 16:13:02 -0700511 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700512 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
513 retries);
James Feist6714a252018-09-10 15:26:18 -0700514}
515
James Feistb6c0b912019-07-09 12:21:44 -0700516int main()
James Feist6714a252018-09-10 15:26:18 -0700517{
518 boost::asio::io_service io;
519 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
520 systemBus->request_name("xyz.openbmc_project.FanSensor");
521 sdbusplus::asio::object_server objectServer(systemBus);
Josh Lehan5170fe62022-08-03 13:17:41 -0700522 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700523 tachSensors;
524 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
525 pwmSensors;
James Feist5591cf082020-07-15 16:44:54 -0700526 auto sensorsChanged =
527 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700528
529 io.post([&]() {
530 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
531 nullptr);
532 });
533
534 boost::asio::deadline_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500535 std::function<void(sdbusplus::message_t&)> eventHandler =
536 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700537 if (message.is_method_error())
538 {
539 std::cerr << "callback method error\n";
540 return;
541 }
542 sensorsChanged->insert(message.get_path());
543 // this implicitly cancels the timer
544 filterTimer.expires_from_now(boost::posix_time::seconds(1));
545
546 filterTimer.async_wait([&](const boost::system::error_code& ec) {
547 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700548 {
Ed Tanousbb679322022-05-16 16:10:00 -0700549 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700550 return;
551 }
Ed Tanousbb679322022-05-16 16:10:00 -0700552 if (ec)
553 {
554 std::cerr << "timer error\n";
555 return;
556 }
557 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
558 sensorsChanged, 5);
559 });
560 };
James Feist6714a252018-09-10 15:26:18 -0700561
Zev Weiss214d9712022-08-12 12:54:31 -0700562 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
563 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700564
James Feistdc6c55f2018-10-31 12:53:20 -0700565 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500566 std::function<void(sdbusplus::message_t&)> redundancyHandler =
567 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700568 createRedundancySensor(tachSensors, systemBus, objectServer);
569 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500570 auto match = std::make_unique<sdbusplus::bus::match_t>(
571 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700572 "type='signal',member='PropertiesChanged',path_namespace='" +
573 std::string(inventoryPath) + "',arg0namespace='" +
574 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700575 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700576 matches.emplace_back(std::move(match));
577
Bruce Lee1263c3d2021-06-04 15:16:33 +0800578 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700579 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700580 return 0;
James Feist6714a252018-09-10 15:26:18 -0700581}