blob: 765a9d072ef71e3ec09388f0711a6b3c1e79e45e [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}
Justin Ledford9c47bd72022-08-27 01:02:09 +0000153
154// The argument to this function should be the fanN_input file that we want to
155// enable. The function will locate the corresponding fanN_enable file if it
156// exists. Note that some drivers don't provide this file if the sensors are
157// always enabled.
158void enableFanInput(const fs::path& fanInputPath)
159{
160 std::error_code ec;
161 std::string path(fanInputPath.string());
162 boost::replace_last(path, "input", "enable");
163
164 bool exists = fs::exists(path, ec);
165 if (ec || !exists)
166 {
167 return;
168 }
169
170 std::fstream enableFile(path, std::ios::out);
171 if (!enableFile.good())
172 {
173 return;
174 }
175 enableFile << 1;
176}
177
Kuiying Wangd5407412020-09-09 16:06:56 +0800178void createRedundancySensor(
Josh Lehan5170fe62022-08-03 13:17:41 -0700179 const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
Kuiying Wangd5407412020-09-09 16:06:56 +0800180 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700181 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800182 sdbusplus::asio::object_server& objectServer)
183{
184
185 conn->async_method_call(
186 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700187 const ManagedObjectType& managedObj) {
Ed Tanousbb679322022-05-16 16:10:00 -0700188 if (ec)
189 {
190 std::cerr << "Error calling entity manager \n";
191 return;
192 }
Zev Weiss77636ec2022-08-12 18:21:01 -0700193 for (const auto& [path, interfaces] : managedObj)
Ed Tanousbb679322022-05-16 16:10:00 -0700194 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700195 for (const auto& [intf, cfg] : interfaces)
Kuiying Wangd5407412020-09-09 16:06:56 +0800196 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700197 if (intf == redundancyConfiguration)
Kuiying Wangd5407412020-09-09 16:06:56 +0800198 {
Ed Tanousbb679322022-05-16 16:10:00 -0700199 // currently only support one
Zev Weiss77636ec2022-08-12 18:21:01 -0700200 auto findCount = cfg.find("AllowedFailures");
201 if (findCount == cfg.end())
Kuiying Wangd5407412020-09-09 16:06:56 +0800202 {
Ed Tanousbb679322022-05-16 16:10:00 -0700203 std::cerr << "Malformed redundancy record \n";
Kuiying Wangd5407412020-09-09 16:06:56 +0800204 return;
205 }
Ed Tanousbb679322022-05-16 16:10:00 -0700206 std::vector<std::string> sensorList;
207
Zev Weiss77636ec2022-08-12 18:21:01 -0700208 for (const auto& [name, sensor] : sensors)
Ed Tanousbb679322022-05-16 16:10:00 -0700209 {
210 sensorList.push_back(
211 "/xyz/openbmc_project/sensors/fan_tach/" +
Zev Weiss77636ec2022-08-12 18:21:01 -0700212 sensor->name);
Ed Tanousbb679322022-05-16 16:10:00 -0700213 }
214 systemRedundancy.reset();
Zev Weiss77636ec2022-08-12 18:21:01 -0700215 systemRedundancy.emplace(
216 RedundancySensor(std::get<uint64_t>(findCount->second),
217 sensorList, objectServer, path));
Ed Tanousbb679322022-05-16 16:10:00 -0700218
219 return;
Kuiying Wangd5407412020-09-09 16:06:56 +0800220 }
221 }
Ed Tanousbb679322022-05-16 16:10:00 -0700222 }
Kuiying Wangd5407412020-09-09 16:06:56 +0800223 },
Nan Zhou3e620af2022-09-20 22:28:31 +0000224 "xyz.openbmc_project.EntityManager", "/xyz/openbmc_project/inventory",
Kuiying Wangd5407412020-09-09 16:06:56 +0800225 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
226}
227
James Feist6714a252018-09-10 15:26:18 -0700228void createSensors(
229 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Josh Lehan5170fe62022-08-03 13:17:41 -0700230 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700231 tachSensors,
232 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
233 pwmSensors,
234 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700235 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700236 sensorsChanged,
237 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700238{
James Feistde5e9702019-09-18 16:13:02 -0700239 auto getter = std::make_shared<GetSensorConfiguration>(
240 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700241 [&io, &objectServer, &tachSensors, &pwmSensors, &dbusConnection,
242 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700243 bool firstScan = sensorsChanged == nullptr;
244 std::vector<fs::path> paths;
245 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
246 {
247 std::cerr << "No fan sensors in system\n";
248 return;
249 }
250
251 // iterate through all found fan sensors, and try to match them with
252 // configuration
253 for (const auto& path : paths)
254 {
255 std::smatch match;
256 std::string pathStr = path.string();
257
258 std::regex_search(pathStr, match, inputRegex);
259 std::string indexStr = *(match.begin() + 1);
260
261 fs::path directory = path.parent_path();
262 FanTypes fanType = getFanType(directory);
Zev Weiss054aad82022-08-18 01:37:34 -0700263 std::string cfgIntf = configInterfaceName(sensorTypes[fanType]);
Ed Tanousbb679322022-05-16 16:10:00 -0700264
265 // convert to 0 based
266 size_t index = std::stoul(indexStr) - 1;
267
268 const char* baseType = nullptr;
269 const SensorData* sensorData = nullptr;
270 const std::string* interfacePath = nullptr;
271 const SensorBaseConfiguration* baseConfiguration = nullptr;
Zev Weiss77636ec2022-08-12 18:21:01 -0700272 for (const auto& [path, cfgData] : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800273 {
Ed Tanousbb679322022-05-16 16:10:00 -0700274 // find the base of the configuration to see if indexes
275 // match
Zev Weiss054aad82022-08-18 01:37:34 -0700276 auto sensorBaseFind = cfgData.find(cfgIntf);
Zev Weiss77636ec2022-08-12 18:21:01 -0700277 if (sensorBaseFind == cfgData.end())
James Feist95b079b2018-11-21 09:28:00 -0800278 {
Ed Tanousbb679322022-05-16 16:10:00 -0700279 continue;
280 }
281
282 baseConfiguration = &(*sensorBaseFind);
Zev Weiss77636ec2022-08-12 18:21:01 -0700283 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700284 baseType = sensorTypes[fanType];
285
286 auto findIndex = baseConfiguration->second.find("Index");
287 if (findIndex == baseConfiguration->second.end())
288 {
289 std::cerr << baseConfiguration->first << " missing index\n";
290 continue;
291 }
292 unsigned int configIndex = std::visit(
293 VariantToUnsignedIntVisitor(), findIndex->second);
294 if (configIndex != index)
295 {
296 continue;
297 }
298 if (fanType == FanTypes::aspeed || fanType == FanTypes::nuvoton)
299 {
300 // there will be only 1 aspeed or nuvoton sensor object
301 // in sysfs, we found the fan
Zev Weiss77636ec2022-08-12 18:21:01 -0700302 sensorData = &cfgData;
Ed Tanousbb679322022-05-16 16:10:00 -0700303 break;
304 }
305 if (fanType == FanTypes::i2c)
306 {
307 size_t bus = 0;
308 size_t address = 0;
309
310 std::string link =
311 fs::read_symlink(directory / "device").filename();
312
313 size_t findDash = link.find('-');
314 if (findDash == std::string::npos ||
315 link.size() <= findDash + 1)
James Feistde5e9702019-09-18 16:13:02 -0700316 {
Ed Tanousbb679322022-05-16 16:10:00 -0700317 std::cerr << "Error finding device from symlink";
James Feistde5e9702019-09-18 16:13:02 -0700318 }
Ed Tanousbb679322022-05-16 16:10:00 -0700319 bus = std::stoi(link.substr(0, findDash));
320 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800321
Ed Tanousbb679322022-05-16 16:10:00 -0700322 auto findBus = baseConfiguration->second.find("Bus");
323 auto findAddress =
324 baseConfiguration->second.find("Address");
325 if (findBus == baseConfiguration->second.end() ||
326 findAddress == baseConfiguration->second.end())
James Feistde5e9702019-09-18 16:13:02 -0700327 {
328 std::cerr << baseConfiguration->first
Ed Tanousbb679322022-05-16 16:10:00 -0700329 << " missing bus or address\n";
James Feistde5e9702019-09-18 16:13:02 -0700330 continue;
331 }
Ed Tanousbb679322022-05-16 16:10:00 -0700332 unsigned int configBus = std::visit(
333 VariantToUnsignedIntVisitor(), findBus->second);
334 unsigned int configAddress = std::visit(
335 VariantToUnsignedIntVisitor(), findAddress->second);
336
337 if (configBus == bus && configAddress == address)
James Feistde5e9702019-09-18 16:13:02 -0700338 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700339 sensorData = &cfgData;
James Feistde5e9702019-09-18 16:13:02 -0700340 break;
341 }
Ed Tanousbb679322022-05-16 16:10:00 -0700342 }
343 }
344 if (sensorData == nullptr)
345 {
346 std::cerr << "failed to find match for " << path.string()
347 << "\n";
348 continue;
349 }
350
351 auto findSensorName = baseConfiguration->second.find("Name");
352
353 if (findSensorName == baseConfiguration->second.end())
354 {
355 std::cerr << "could not determine configuration name for "
356 << path.string() << "\n";
357 continue;
358 }
359 std::string sensorName =
360 std::get<std::string>(findSensorName->second);
361
362 // on rescans, only update sensors we were signaled by
363 auto findSensor = tachSensors.find(sensorName);
364 if (!firstScan && findSensor != tachSensors.end())
365 {
366 bool found = false;
367 for (auto it = sensorsChanged->begin();
368 it != sensorsChanged->end(); it++)
369 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700370 if (it->ends_with(findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700371 {
Ed Tanousbb679322022-05-16 16:10:00 -0700372 sensorsChanged->erase(it);
373 findSensor->second = nullptr;
374 found = true;
375 break;
James Feistde5e9702019-09-18 16:13:02 -0700376 }
377 }
Ed Tanousbb679322022-05-16 16:10:00 -0700378 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700379 {
James Feist95b079b2018-11-21 09:28:00 -0800380 continue;
381 }
Ed Tanousbb679322022-05-16 16:10:00 -0700382 }
383 std::vector<thresholds::Threshold> sensorThresholds;
384 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
385 {
386 std::cerr << "error populating thresholds for " << sensorName
387 << "\n";
388 }
James Feist95b079b2018-11-21 09:28:00 -0800389
Ed Tanousbb679322022-05-16 16:10:00 -0700390 auto presenceConfig =
Zev Weiss054aad82022-08-18 01:37:34 -0700391 sensorData->find(cfgIntf + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800392
Ed Tanousbb679322022-05-16 16:10:00 -0700393 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
394
395 // presence sensors are optional
396 if (presenceConfig != sensorData->end())
397 {
398 auto findPolarity = presenceConfig->second.find("Polarity");
399 auto findPinName = presenceConfig->second.find("PinName");
400
401 if (findPinName == presenceConfig->second.end() ||
402 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800403 {
Ed Tanousbb679322022-05-16 16:10:00 -0700404 std::cerr << "Malformed Presence Configuration\n";
James Feistde5e9702019-09-18 16:13:02 -0700405 }
Ed Tanousbb679322022-05-16 16:10:00 -0700406 else
James Feistde5e9702019-09-18 16:13:02 -0700407 {
Ed Tanousbb679322022-05-16 16:10:00 -0700408 bool inverted =
409 std::get<std::string>(findPolarity->second) == "Low";
Ed Tanous2049bd22022-07-09 07:20:26 -0700410 if (const auto* pinName =
Ed Tanousbb679322022-05-16 16:10:00 -0700411 std::get_if<std::string>(&findPinName->second))
James Feistde5e9702019-09-18 16:13:02 -0700412 {
Ed Tanousbb679322022-05-16 16:10:00 -0700413 presenceSensor = std::make_unique<PresenceSensor>(
414 *pinName, inverted, io, sensorName);
James Feistde5e9702019-09-18 16:13:02 -0700415 }
416 else
417 {
Ed Tanousbb679322022-05-16 16:10:00 -0700418 std::cerr << "Malformed Presence pinName for sensor "
419 << sensorName << " \n";
James Feistde5e9702019-09-18 16:13:02 -0700420 }
421 }
Ed Tanousbb679322022-05-16 16:10:00 -0700422 }
423 std::optional<RedundancySensor>* redundancy = nullptr;
424 if (fanType == FanTypes::aspeed)
425 {
426 redundancy = &systemRedundancy;
427 }
428
Zev Weissa4d27682022-07-19 15:30:36 -0700429 PowerState powerState = getPowerState(baseConfiguration->second);
Yong Zhao77b3add2021-01-09 04:25:18 +0000430
Ed Tanousbb679322022-05-16 16:10:00 -0700431 constexpr double defaultMaxReading = 25000;
432 constexpr double defaultMinReading = 0;
433 std::pair<double, double> limits =
434 std::make_pair(defaultMinReading, defaultMaxReading);
435
436 auto connector =
Zev Weiss054aad82022-08-18 01:37:34 -0700437 sensorData->find(cfgIntf + std::string(".Connector"));
Ed Tanousbb679322022-05-16 16:10:00 -0700438
439 std::optional<std::string> led;
440 std::string pwmName;
441 fs::path pwmPath;
442
443 // The Mutable parameter is optional, defaulting to false
444 bool isValueMutable = false;
445 if (connector != sensorData->end())
446 {
447 auto findPwm = connector->second.find("Pwm");
448 if (findPwm != connector->second.end())
449 {
450 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
451 findPwm->second);
452 if (!findPwmPath(directory, pwm, pwmPath))
453 {
454 std::cerr << "Connector for " << sensorName
455 << " no pwm channel found!\n";
456 continue;
457 }
458
459 fs::path pwmEnableFile =
460 "pwm" + std::to_string(pwm + 1) + "_enable";
461 fs::path enablePath = pwmPath.parent_path() / pwmEnableFile;
462 enablePwm(enablePath);
463
464 /* use pwm name override if found in configuration else
465 * use default */
466 auto findOverride = connector->second.find("PwmName");
467 if (findOverride != connector->second.end())
468 {
469 pwmName = std::visit(VariantToStringVisitor(),
470 findOverride->second);
471 }
472 else
473 {
474 pwmName = "Pwm_" + std::to_string(pwm + 1);
475 }
476
477 // Check PWM sensor mutability
478 auto findMutable = connector->second.find("Mutable");
479 if (findMutable != connector->second.end())
480 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700481 const auto* ptrMutable =
Ed Tanousbb679322022-05-16 16:10:00 -0700482 std::get_if<bool>(&(findMutable->second));
Ed Tanous2049bd22022-07-09 07:20:26 -0700483 if (ptrMutable != nullptr)
Ed Tanousbb679322022-05-16 16:10:00 -0700484 {
485 isValueMutable = *ptrMutable;
486 }
487 }
488 }
489 else
490 {
491 std::cerr << "Connector for " << sensorName
492 << " missing pwm!\n";
493 }
494
495 auto findLED = connector->second.find("LED");
496 if (findLED != connector->second.end())
497 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700498 const auto* ledName =
499 std::get_if<std::string>(&(findLED->second));
Ed Tanousbb679322022-05-16 16:10:00 -0700500 if (ledName == nullptr)
501 {
502 std::cerr << "Wrong format for LED of " << sensorName
503 << "\n";
504 }
505 else
506 {
507 led = *ledName;
508 }
509 }
510 }
511
512 findLimits(limits, baseConfiguration);
Josh Lehan5170fe62022-08-03 13:17:41 -0700513
Justin Ledford9c47bd72022-08-27 01:02:09 +0000514 enableFanInput(path);
515
Josh Lehan5170fe62022-08-03 13:17:41 -0700516 auto& tachSensor = tachSensors[sensorName];
517 tachSensor = nullptr;
518 tachSensor = std::make_shared<TachSensor>(
Ed Tanousbb679322022-05-16 16:10:00 -0700519 path.string(), baseType, objectServer, dbusConnection,
520 std::move(presenceSensor), redundancy, io, sensorName,
521 std::move(sensorThresholds), *interfacePath, limits, powerState,
522 led);
Josh Lehan5170fe62022-08-03 13:17:41 -0700523 tachSensor->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700524
525 if (!pwmPath.empty() && fs::exists(pwmPath) &&
Ed Tanous2049bd22022-07-09 07:20:26 -0700526 (pwmSensors.count(pwmPath) == 0U))
Ed Tanousbb679322022-05-16 16:10:00 -0700527 {
528 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
529 pwmName, pwmPath, dbusConnection, objectServer,
530 *interfacePath, "Fan", isValueMutable);
531 }
532 }
533
534 createRedundancySensor(tachSensors, dbusConnection, objectServer);
Ed Tanous8a17c302021-09-02 15:07:11 -0700535 });
James Feistde5e9702019-09-18 16:13:02 -0700536 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700537 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
538 retries);
James Feist6714a252018-09-10 15:26:18 -0700539}
540
James Feistb6c0b912019-07-09 12:21:44 -0700541int main()
James Feist6714a252018-09-10 15:26:18 -0700542{
543 boost::asio::io_service io;
544 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700545 sdbusplus::asio::object_server objectServer(systemBus, true);
546
547 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanousd9067252022-10-22 15:35:52 -0700548 objectServer.add_manager("/xyz/openbmc_project/control");
James Feist6714a252018-09-10 15:26:18 -0700549 systemBus->request_name("xyz.openbmc_project.FanSensor");
Josh Lehan5170fe62022-08-03 13:17:41 -0700550 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700551 tachSensors;
552 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
553 pwmSensors;
James Feist5591cf082020-07-15 16:44:54 -0700554 auto sensorsChanged =
555 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700556
557 io.post([&]() {
558 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
559 nullptr);
560 });
561
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700562 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500563 std::function<void(sdbusplus::message_t&)> eventHandler =
564 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700565 if (message.is_method_error())
566 {
567 std::cerr << "callback method error\n";
568 return;
569 }
570 sensorsChanged->insert(message.get_path());
571 // this implicitly cancels the timer
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700572 filterTimer.expires_from_now(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700573
574 filterTimer.async_wait([&](const boost::system::error_code& ec) {
575 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700576 {
Ed Tanousbb679322022-05-16 16:10:00 -0700577 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700578 return;
579 }
Ed Tanousbb679322022-05-16 16:10:00 -0700580 if (ec)
581 {
582 std::cerr << "timer error\n";
583 return;
584 }
585 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
586 sensorsChanged, 5);
587 });
588 };
James Feist6714a252018-09-10 15:26:18 -0700589
Zev Weiss214d9712022-08-12 12:54:31 -0700590 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
591 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700592
James Feistdc6c55f2018-10-31 12:53:20 -0700593 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500594 std::function<void(sdbusplus::message_t&)> redundancyHandler =
595 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700596 createRedundancySensor(tachSensors, systemBus, objectServer);
597 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500598 auto match = std::make_unique<sdbusplus::bus::match_t>(
599 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700600 "type='signal',member='PropertiesChanged',path_namespace='" +
601 std::string(inventoryPath) + "',arg0namespace='" +
602 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700603 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700604 matches.emplace_back(std::move(match));
605
Bruce Lee1263c3d2021-06-04 15:16:33 +0800606 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700607 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700608 return 0;
James Feist6714a252018-09-10 15:26:18 -0700609}