blob: 4e7542bb86bb4c8c4c113325b53533358f07d838 [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/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070022#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26#include <sdbusplus/bus/match.hpp>
27
28#include <array>
James Feist24f02f22019-04-15 11:05:39 -070029#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070030#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <functional>
32#include <memory>
33#include <optional>
James Feist6714a252018-09-10 15:26:18 -070034#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <string>
36#include <utility>
37#include <variant>
38#include <vector>
James Feist6714a252018-09-10 15:26:18 -070039
James Feistcf3bce62019-01-08 10:07:19 -080040namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080041
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000042// The following two structures need to be consistent
Zev Weiss054aad82022-08-18 01:37:34 -070043static auto sensorTypes{
44 std::to_array<const char*>({"AspeedFan", "I2CFan", "NuvotonFan"})};
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000045
46enum FanTypes
47{
48 aspeed = 0,
49 i2c,
50 nuvoton,
51 max,
52};
53
54static_assert(std::tuple_size<decltype(sensorTypes)>::value == FanTypes::max,
55 "sensorTypes element number is not equal to FanTypes number");
56
James Feistdc6c55f2018-10-31 12:53:20 -070057constexpr const char* redundancyConfiguration =
58 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070059static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070060
James Feistdc6c55f2018-10-31 12:53:20 -070061// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070062std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080063
64FanTypes getFanType(const fs::path& parentPath)
65{
66 fs::path linkPath = parentPath / "device";
67 std::string canonical = fs::read_symlink(linkPath);
Zev Weiss6c106d62022-08-17 20:50:00 -070068 if (canonical.ends_with("pwm-tacho-controller") ||
69 canonical.ends_with("pwm_tach:tach"))
James Feist95b079b2018-11-21 09:28:00 -080070 {
71 return FanTypes::aspeed;
72 }
Zev Weiss6c106d62022-08-17 20:50:00 -070073 if (canonical.ends_with("pwm-fan-controller"))
Peter Lundgren8843b622019-09-12 10:33:41 -070074 {
75 return FanTypes::nuvoton;
76 }
James Feist95b079b2018-11-21 09:28:00 -080077 // todo: will we need to support other types?
78 return FanTypes::i2c;
79}
Jeff Linabf91de2020-12-23 10:55:42 +080080void enablePwm(const fs::path& filePath)
81{
82 std::fstream enableFile(filePath, std::ios::in | std::ios::out);
83 if (!enableFile.good())
84 {
85 std::cerr << "Error read/write " << filePath << "\n";
86 return;
87 }
James Feistdc6c55f2018-10-31 12:53:20 -070088
Jeff Linabf91de2020-12-23 10:55:42 +080089 std::string regulateMode;
90 std::getline(enableFile, regulateMode);
91 if (regulateMode == "0")
92 {
93 enableFile << 1;
94 }
95}
Howard Chiuddf25d12021-12-02 15:14:44 +080096bool findPwmfanPath(unsigned int configPwmfanIndex, fs::path& pwmPath)
97{
98 /* Search PWM since pwm-fan had separated
99 * PWM from tach directory and 1 channel only*/
100 std::vector<fs::path> pwmfanPaths;
101 std::string pwnfanDevName("pwm-fan");
102
103 pwnfanDevName += std::to_string(configPwmfanIndex);
104
105 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwmfanPaths))
106 {
107 std::cerr << "No PWMs are found!\n";
108 return false;
109 }
110 for (const auto& path : pwmfanPaths)
111 {
112 std::error_code ec;
113 fs::path link = fs::read_symlink(path.parent_path() / "device", ec);
114
115 if (ec)
116 {
117 std::cerr << "read_symlink() failed: " << ec.message() << " ("
118 << ec.value() << ")\n";
119 continue;
120 }
121
122 if (link.filename().string() == pwnfanDevName)
123 {
124 pwmPath = path;
125 return true;
126 }
127 }
128 return false;
129}
130bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath)
131{
132 std::error_code ec;
133
134 /* Assuming PWM file is appeared in the same directory as fanX_input */
135 auto path = directory / ("pwm" + std::to_string(pwm + 1));
136 bool exists = fs::exists(path, ec);
137
138 if (ec || !exists)
139 {
140 /* PWM file not exist or error happened */
141 if (ec)
142 {
143 std::cerr << "exists() failed: " << ec.message() << " ("
144 << ec.value() << ")\n";
145 }
146 /* try search form pwm-fanX directory */
147 return findPwmfanPath(pwm, pwmPath);
148 }
149
150 pwmPath = path;
151 return true;
152}
Kuiying Wangd5407412020-09-09 16:06:56 +0800153void createRedundancySensor(
Josh Lehan5170fe62022-08-03 13:17:41 -0700154 const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
Kuiying Wangd5407412020-09-09 16:06:56 +0800155 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700156 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800157 sdbusplus::asio::object_server& objectServer)
158{
159
160 conn->async_method_call(
161 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700162 const ManagedObjectType& managedObj) {
Ed Tanousbb679322022-05-16 16:10:00 -0700163 if (ec)
164 {
165 std::cerr << "Error calling entity manager \n";
166 return;
167 }
Zev Weiss77636ec2022-08-12 18:21:01 -0700168 for (const auto& [path, interfaces] : managedObj)
Ed Tanousbb679322022-05-16 16:10:00 -0700169 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700170 for (const auto& [intf, cfg] : interfaces)
Kuiying Wangd5407412020-09-09 16:06:56 +0800171 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700172 if (intf == redundancyConfiguration)
Kuiying Wangd5407412020-09-09 16:06:56 +0800173 {
Ed Tanousbb679322022-05-16 16:10:00 -0700174 // currently only support one
Zev Weiss77636ec2022-08-12 18:21:01 -0700175 auto findCount = cfg.find("AllowedFailures");
176 if (findCount == cfg.end())
Kuiying Wangd5407412020-09-09 16:06:56 +0800177 {
Ed Tanousbb679322022-05-16 16:10:00 -0700178 std::cerr << "Malformed redundancy record \n";
Kuiying Wangd5407412020-09-09 16:06:56 +0800179 return;
180 }
Ed Tanousbb679322022-05-16 16:10:00 -0700181 std::vector<std::string> sensorList;
182
Zev Weiss77636ec2022-08-12 18:21:01 -0700183 for (const auto& [name, sensor] : sensors)
Ed Tanousbb679322022-05-16 16:10:00 -0700184 {
185 sensorList.push_back(
186 "/xyz/openbmc_project/sensors/fan_tach/" +
Zev Weiss77636ec2022-08-12 18:21:01 -0700187 sensor->name);
Ed Tanousbb679322022-05-16 16:10:00 -0700188 }
189 systemRedundancy.reset();
Zev Weiss77636ec2022-08-12 18:21:01 -0700190 systemRedundancy.emplace(
191 RedundancySensor(std::get<uint64_t>(findCount->second),
192 sensorList, objectServer, path));
Ed Tanousbb679322022-05-16 16:10:00 -0700193
194 return;
Kuiying Wangd5407412020-09-09 16:06:56 +0800195 }
196 }
Ed Tanousbb679322022-05-16 16:10:00 -0700197 }
Kuiying Wangd5407412020-09-09 16:06:56 +0800198 },
199 "xyz.openbmc_project.EntityManager", "/",
200 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
201}
202
James Feist6714a252018-09-10 15:26:18 -0700203void createSensors(
204 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Josh Lehan5170fe62022-08-03 13:17:41 -0700205 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700206 tachSensors,
207 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
208 pwmSensors,
209 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700210 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700211 sensorsChanged,
212 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700213{
James Feistde5e9702019-09-18 16:13:02 -0700214 auto getter = std::make_shared<GetSensorConfiguration>(
215 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700216 [&io, &objectServer, &tachSensors, &pwmSensors, &dbusConnection,
217 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700218 bool firstScan = sensorsChanged == nullptr;
219 std::vector<fs::path> paths;
220 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
221 {
222 std::cerr << "No fan sensors in system\n";
223 return;
224 }
225
226 // iterate through all found fan sensors, and try to match them with
227 // configuration
228 for (const auto& path : paths)
229 {
230 std::smatch match;
231 std::string pathStr = path.string();
232
233 std::regex_search(pathStr, match, inputRegex);
234 std::string indexStr = *(match.begin() + 1);
235
236 fs::path directory = path.parent_path();
237 FanTypes fanType = getFanType(directory);
Zev Weiss054aad82022-08-18 01:37:34 -0700238 std::string cfgIntf = configInterfaceName(sensorTypes[fanType]);
Ed Tanousbb679322022-05-16 16:10:00 -0700239
240 // convert to 0 based
241 size_t index = std::stoul(indexStr) - 1;
242
243 const char* baseType = nullptr;
244 const SensorData* sensorData = nullptr;
245 const std::string* interfacePath = nullptr;
246 const SensorBaseConfiguration* baseConfiguration = nullptr;
Zev Weiss77636ec2022-08-12 18:21:01 -0700247 for (const auto& [path, cfgData] : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800248 {
Ed Tanousbb679322022-05-16 16:10:00 -0700249 // find the base of the configuration to see if indexes
250 // match
Zev Weiss054aad82022-08-18 01:37:34 -0700251 auto sensorBaseFind = cfgData.find(cfgIntf);
Zev Weiss77636ec2022-08-12 18:21:01 -0700252 if (sensorBaseFind == cfgData.end())
James Feist95b079b2018-11-21 09:28:00 -0800253 {
Ed Tanousbb679322022-05-16 16:10:00 -0700254 continue;
255 }
256
257 baseConfiguration = &(*sensorBaseFind);
Zev Weiss77636ec2022-08-12 18:21:01 -0700258 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700259 baseType = sensorTypes[fanType];
260
261 auto findIndex = baseConfiguration->second.find("Index");
262 if (findIndex == baseConfiguration->second.end())
263 {
264 std::cerr << baseConfiguration->first << " missing index\n";
265 continue;
266 }
267 unsigned int configIndex = std::visit(
268 VariantToUnsignedIntVisitor(), findIndex->second);
269 if (configIndex != index)
270 {
271 continue;
272 }
273 if (fanType == FanTypes::aspeed || fanType == FanTypes::nuvoton)
274 {
275 // there will be only 1 aspeed or nuvoton sensor object
276 // in sysfs, we found the fan
Zev Weiss77636ec2022-08-12 18:21:01 -0700277 sensorData = &cfgData;
Ed Tanousbb679322022-05-16 16:10:00 -0700278 break;
279 }
280 if (fanType == FanTypes::i2c)
281 {
282 size_t bus = 0;
283 size_t address = 0;
284
285 std::string link =
286 fs::read_symlink(directory / "device").filename();
287
288 size_t findDash = link.find('-');
289 if (findDash == std::string::npos ||
290 link.size() <= findDash + 1)
James Feistde5e9702019-09-18 16:13:02 -0700291 {
Ed Tanousbb679322022-05-16 16:10:00 -0700292 std::cerr << "Error finding device from symlink";
James Feistde5e9702019-09-18 16:13:02 -0700293 }
Ed Tanousbb679322022-05-16 16:10:00 -0700294 bus = std::stoi(link.substr(0, findDash));
295 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800296
Ed Tanousbb679322022-05-16 16:10:00 -0700297 auto findBus = baseConfiguration->second.find("Bus");
298 auto findAddress =
299 baseConfiguration->second.find("Address");
300 if (findBus == baseConfiguration->second.end() ||
301 findAddress == baseConfiguration->second.end())
James Feistde5e9702019-09-18 16:13:02 -0700302 {
303 std::cerr << baseConfiguration->first
Ed Tanousbb679322022-05-16 16:10:00 -0700304 << " missing bus or address\n";
James Feistde5e9702019-09-18 16:13:02 -0700305 continue;
306 }
Ed Tanousbb679322022-05-16 16:10:00 -0700307 unsigned int configBus = std::visit(
308 VariantToUnsignedIntVisitor(), findBus->second);
309 unsigned int configAddress = std::visit(
310 VariantToUnsignedIntVisitor(), findAddress->second);
311
312 if (configBus == bus && configAddress == address)
James Feistde5e9702019-09-18 16:13:02 -0700313 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700314 sensorData = &cfgData;
James Feistde5e9702019-09-18 16:13:02 -0700315 break;
316 }
Ed Tanousbb679322022-05-16 16:10:00 -0700317 }
318 }
319 if (sensorData == nullptr)
320 {
321 std::cerr << "failed to find match for " << path.string()
322 << "\n";
323 continue;
324 }
325
326 auto findSensorName = baseConfiguration->second.find("Name");
327
328 if (findSensorName == baseConfiguration->second.end())
329 {
330 std::cerr << "could not determine configuration name for "
331 << path.string() << "\n";
332 continue;
333 }
334 std::string sensorName =
335 std::get<std::string>(findSensorName->second);
336
337 // on rescans, only update sensors we were signaled by
338 auto findSensor = tachSensors.find(sensorName);
339 if (!firstScan && findSensor != tachSensors.end())
340 {
341 bool found = false;
342 for (auto it = sensorsChanged->begin();
343 it != sensorsChanged->end(); it++)
344 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700345 if (it->ends_with(findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700346 {
Ed Tanousbb679322022-05-16 16:10:00 -0700347 sensorsChanged->erase(it);
348 findSensor->second = nullptr;
349 found = true;
350 break;
James Feistde5e9702019-09-18 16:13:02 -0700351 }
352 }
Ed Tanousbb679322022-05-16 16:10:00 -0700353 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700354 {
James Feist95b079b2018-11-21 09:28:00 -0800355 continue;
356 }
Ed Tanousbb679322022-05-16 16:10:00 -0700357 }
358 std::vector<thresholds::Threshold> sensorThresholds;
359 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
360 {
361 std::cerr << "error populating thresholds for " << sensorName
362 << "\n";
363 }
James Feist95b079b2018-11-21 09:28:00 -0800364
Ed Tanousbb679322022-05-16 16:10:00 -0700365 auto presenceConfig =
Zev Weiss054aad82022-08-18 01:37:34 -0700366 sensorData->find(cfgIntf + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800367
Ed Tanousbb679322022-05-16 16:10:00 -0700368 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
369
370 // presence sensors are optional
371 if (presenceConfig != sensorData->end())
372 {
373 auto findPolarity = presenceConfig->second.find("Polarity");
374 auto findPinName = presenceConfig->second.find("PinName");
375
376 if (findPinName == presenceConfig->second.end() ||
377 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800378 {
Ed Tanousbb679322022-05-16 16:10:00 -0700379 std::cerr << "Malformed Presence Configuration\n";
James Feistde5e9702019-09-18 16:13:02 -0700380 }
Ed Tanousbb679322022-05-16 16:10:00 -0700381 else
James Feistde5e9702019-09-18 16:13:02 -0700382 {
Ed Tanousbb679322022-05-16 16:10:00 -0700383 bool inverted =
384 std::get<std::string>(findPolarity->second) == "Low";
Ed Tanous2049bd22022-07-09 07:20:26 -0700385 if (const auto* pinName =
Ed Tanousbb679322022-05-16 16:10:00 -0700386 std::get_if<std::string>(&findPinName->second))
James Feistde5e9702019-09-18 16:13:02 -0700387 {
Ed Tanousbb679322022-05-16 16:10:00 -0700388 presenceSensor = std::make_unique<PresenceSensor>(
389 *pinName, inverted, io, sensorName);
James Feistde5e9702019-09-18 16:13:02 -0700390 }
391 else
392 {
Ed Tanousbb679322022-05-16 16:10:00 -0700393 std::cerr << "Malformed Presence pinName for sensor "
394 << sensorName << " \n";
James Feistde5e9702019-09-18 16:13:02 -0700395 }
396 }
Ed Tanousbb679322022-05-16 16:10:00 -0700397 }
398 std::optional<RedundancySensor>* redundancy = nullptr;
399 if (fanType == FanTypes::aspeed)
400 {
401 redundancy = &systemRedundancy;
402 }
403
Zev Weissa4d27682022-07-19 15:30:36 -0700404 PowerState powerState = getPowerState(baseConfiguration->second);
Yong Zhao77b3add2021-01-09 04:25:18 +0000405
Ed Tanousbb679322022-05-16 16:10:00 -0700406 constexpr double defaultMaxReading = 25000;
407 constexpr double defaultMinReading = 0;
408 std::pair<double, double> limits =
409 std::make_pair(defaultMinReading, defaultMaxReading);
410
411 auto connector =
Zev Weiss054aad82022-08-18 01:37:34 -0700412 sensorData->find(cfgIntf + std::string(".Connector"));
Ed Tanousbb679322022-05-16 16:10:00 -0700413
414 std::optional<std::string> led;
415 std::string pwmName;
416 fs::path pwmPath;
417
418 // The Mutable parameter is optional, defaulting to false
419 bool isValueMutable = false;
420 if (connector != sensorData->end())
421 {
422 auto findPwm = connector->second.find("Pwm");
423 if (findPwm != connector->second.end())
424 {
425 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
426 findPwm->second);
427 if (!findPwmPath(directory, pwm, pwmPath))
428 {
429 std::cerr << "Connector for " << sensorName
430 << " no pwm channel found!\n";
431 continue;
432 }
433
434 fs::path pwmEnableFile =
435 "pwm" + std::to_string(pwm + 1) + "_enable";
436 fs::path enablePath = pwmPath.parent_path() / pwmEnableFile;
437 enablePwm(enablePath);
438
439 /* use pwm name override if found in configuration else
440 * use default */
441 auto findOverride = connector->second.find("PwmName");
442 if (findOverride != connector->second.end())
443 {
444 pwmName = std::visit(VariantToStringVisitor(),
445 findOverride->second);
446 }
447 else
448 {
449 pwmName = "Pwm_" + std::to_string(pwm + 1);
450 }
451
452 // Check PWM sensor mutability
453 auto findMutable = connector->second.find("Mutable");
454 if (findMutable != connector->second.end())
455 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700456 const auto* ptrMutable =
Ed Tanousbb679322022-05-16 16:10:00 -0700457 std::get_if<bool>(&(findMutable->second));
Ed Tanous2049bd22022-07-09 07:20:26 -0700458 if (ptrMutable != nullptr)
Ed Tanousbb679322022-05-16 16:10:00 -0700459 {
460 isValueMutable = *ptrMutable;
461 }
462 }
463 }
464 else
465 {
466 std::cerr << "Connector for " << sensorName
467 << " missing pwm!\n";
468 }
469
470 auto findLED = connector->second.find("LED");
471 if (findLED != connector->second.end())
472 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700473 const auto* ledName =
474 std::get_if<std::string>(&(findLED->second));
Ed Tanousbb679322022-05-16 16:10:00 -0700475 if (ledName == nullptr)
476 {
477 std::cerr << "Wrong format for LED of " << sensorName
478 << "\n";
479 }
480 else
481 {
482 led = *ledName;
483 }
484 }
485 }
486
487 findLimits(limits, baseConfiguration);
Josh Lehan5170fe62022-08-03 13:17:41 -0700488
489 auto& tachSensor = tachSensors[sensorName];
490 tachSensor = nullptr;
491 tachSensor = std::make_shared<TachSensor>(
Ed Tanousbb679322022-05-16 16:10:00 -0700492 path.string(), baseType, objectServer, dbusConnection,
493 std::move(presenceSensor), redundancy, io, sensorName,
494 std::move(sensorThresholds), *interfacePath, limits, powerState,
495 led);
Josh Lehan5170fe62022-08-03 13:17:41 -0700496 tachSensor->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700497
498 if (!pwmPath.empty() && fs::exists(pwmPath) &&
Ed Tanous2049bd22022-07-09 07:20:26 -0700499 (pwmSensors.count(pwmPath) == 0U))
Ed Tanousbb679322022-05-16 16:10:00 -0700500 {
501 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
502 pwmName, pwmPath, dbusConnection, objectServer,
503 *interfacePath, "Fan", isValueMutable);
504 }
505 }
506
507 createRedundancySensor(tachSensors, dbusConnection, objectServer);
Ed Tanous8a17c302021-09-02 15:07:11 -0700508 });
James Feistde5e9702019-09-18 16:13:02 -0700509 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700510 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
511 retries);
James Feist6714a252018-09-10 15:26:18 -0700512}
513
James Feistb6c0b912019-07-09 12:21:44 -0700514int main()
James Feist6714a252018-09-10 15:26:18 -0700515{
516 boost::asio::io_service io;
517 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
518 systemBus->request_name("xyz.openbmc_project.FanSensor");
519 sdbusplus::asio::object_server objectServer(systemBus);
Josh Lehan5170fe62022-08-03 13:17:41 -0700520 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700521 tachSensors;
522 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
523 pwmSensors;
James Feist5591cf082020-07-15 16:44:54 -0700524 auto sensorsChanged =
525 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700526
527 io.post([&]() {
528 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
529 nullptr);
530 });
531
532 boost::asio::deadline_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500533 std::function<void(sdbusplus::message_t&)> eventHandler =
534 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700535 if (message.is_method_error())
536 {
537 std::cerr << "callback method error\n";
538 return;
539 }
540 sensorsChanged->insert(message.get_path());
541 // this implicitly cancels the timer
542 filterTimer.expires_from_now(boost::posix_time::seconds(1));
543
544 filterTimer.async_wait([&](const boost::system::error_code& ec) {
545 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700546 {
Ed Tanousbb679322022-05-16 16:10:00 -0700547 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700548 return;
549 }
Ed Tanousbb679322022-05-16 16:10:00 -0700550 if (ec)
551 {
552 std::cerr << "timer error\n";
553 return;
554 }
555 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
556 sensorsChanged, 5);
557 });
558 };
James Feist6714a252018-09-10 15:26:18 -0700559
Zev Weiss214d9712022-08-12 12:54:31 -0700560 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
561 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700562
James Feistdc6c55f2018-10-31 12:53:20 -0700563 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500564 std::function<void(sdbusplus::message_t&)> redundancyHandler =
565 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700566 createRedundancySensor(tachSensors, systemBus, objectServer);
567 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500568 auto match = std::make_unique<sdbusplus::bus::match_t>(
569 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700570 "type='signal',member='PropertiesChanged',path_namespace='" +
571 std::string(inventoryPath) + "',arg0namespace='" +
572 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700573 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700574 matches.emplace_back(std::move(match));
575
Bruce Lee1263c3d2021-06-04 15:16:33 +0800576 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700577 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700578 return 0;
James Feist6714a252018-09-10 15:26:18 -0700579}