blob: 6dccee5e1cec5c5956b7c453c82ab359243504de [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
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "PwmSensor.hpp"
18#include "TachSensor.hpp"
19#include "Utils.hpp"
20#include "VariantVisitors.hpp"
21
James Feist6714a252018-09-10 15:26:18 -070022#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
Zev Weiss054aad82022-08-18 01:37:34 -070044static auto sensorTypes{
45 std::to_array<const char*>({"AspeedFan", "I2CFan", "NuvotonFan"})};
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000046
47enum FanTypes
48{
49 aspeed = 0,
50 i2c,
51 nuvoton,
52 max,
53};
54
55static_assert(std::tuple_size<decltype(sensorTypes)>::value == FanTypes::max,
56 "sensorTypes element number is not equal to FanTypes number");
57
James Feistdc6c55f2018-10-31 12:53:20 -070058constexpr const char* redundancyConfiguration =
59 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070060static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070061
James Feistdc6c55f2018-10-31 12:53:20 -070062// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070063std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080064
65FanTypes getFanType(const fs::path& parentPath)
66{
Chris Sides9a472e82023-04-03 15:13:37 -050067 fs::path linkPath = parentPath / "of_node";
68 std::string canonical = fs::canonical(linkPath);
69
70 std::string compatiblePath = canonical + "/compatible";
71 std::ifstream compatibleStream(compatiblePath);
72
73 if (!compatibleStream.is_open())
James Feist95b079b2018-11-21 09:28:00 -080074 {
Chris Sides9a472e82023-04-03 15:13:37 -050075 std::cerr << "Error opening " << compatiblePath << "\n";
76 return FanTypes::i2c; // Should this throw an exception instead?
James Feist95b079b2018-11-21 09:28:00 -080077 }
Chris Sides9a472e82023-04-03 15:13:37 -050078
79 std::string compatibleString;
80 while (compatibleStream.peek() != EOF)
Peter Lundgren8843b622019-09-12 10:33:41 -070081 {
Chris Sides9a472e82023-04-03 15:13:37 -050082 std::getline(compatibleStream, compatibleString);
83 compatibleString.pop_back(); // trim EOL before comparisons
84
85 if (compatibleString == "aspeed,ast2400-pwm-tacho" ||
86 compatibleString == "aspeed,ast2500-pwm-tacho")
87 {
88 return FanTypes::aspeed;
89 }
90 if (compatibleString == "nuvoton,npcm750-pwm-fan")
91 {
92 return FanTypes::nuvoton;
93 }
Peter Lundgren8843b622019-09-12 10:33:41 -070094 }
Chris Sides9a472e82023-04-03 15:13:37 -050095
James Feist95b079b2018-11-21 09:28:00 -080096 // todo: will we need to support other types?
97 return FanTypes::i2c;
98}
Jeff Linabf91de2020-12-23 10:55:42 +080099void enablePwm(const fs::path& filePath)
100{
101 std::fstream enableFile(filePath, std::ios::in | std::ios::out);
102 if (!enableFile.good())
103 {
104 std::cerr << "Error read/write " << filePath << "\n";
105 return;
106 }
James Feistdc6c55f2018-10-31 12:53:20 -0700107
Jeff Linabf91de2020-12-23 10:55:42 +0800108 std::string regulateMode;
109 std::getline(enableFile, regulateMode);
110 if (regulateMode == "0")
111 {
112 enableFile << 1;
113 }
114}
Howard Chiuddf25d12021-12-02 15:14:44 +0800115bool findPwmfanPath(unsigned int configPwmfanIndex, fs::path& pwmPath)
116{
117 /* Search PWM since pwm-fan had separated
118 * PWM from tach directory and 1 channel only*/
119 std::vector<fs::path> pwmfanPaths;
120 std::string pwnfanDevName("pwm-fan");
121
122 pwnfanDevName += std::to_string(configPwmfanIndex);
123
124 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwmfanPaths))
125 {
126 std::cerr << "No PWMs are found!\n";
127 return false;
128 }
129 for (const auto& path : pwmfanPaths)
130 {
131 std::error_code ec;
132 fs::path link = fs::read_symlink(path.parent_path() / "device", ec);
133
134 if (ec)
135 {
136 std::cerr << "read_symlink() failed: " << ec.message() << " ("
137 << ec.value() << ")\n";
138 continue;
139 }
140
141 if (link.filename().string() == pwnfanDevName)
142 {
143 pwmPath = path;
144 return true;
145 }
146 }
147 return false;
148}
149bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath)
150{
151 std::error_code ec;
152
153 /* Assuming PWM file is appeared in the same directory as fanX_input */
154 auto path = directory / ("pwm" + std::to_string(pwm + 1));
155 bool exists = fs::exists(path, ec);
156
157 if (ec || !exists)
158 {
159 /* PWM file not exist or error happened */
160 if (ec)
161 {
162 std::cerr << "exists() failed: " << ec.message() << " ("
163 << ec.value() << ")\n";
164 }
165 /* try search form pwm-fanX directory */
166 return findPwmfanPath(pwm, pwmPath);
167 }
168
169 pwmPath = path;
170 return true;
171}
Justin Ledford9c47bd72022-08-27 01:02:09 +0000172
173// The argument to this function should be the fanN_input file that we want to
174// enable. The function will locate the corresponding fanN_enable file if it
175// exists. Note that some drivers don't provide this file if the sensors are
176// always enabled.
177void enableFanInput(const fs::path& fanInputPath)
178{
179 std::error_code ec;
180 std::string path(fanInputPath.string());
181 boost::replace_last(path, "input", "enable");
182
183 bool exists = fs::exists(path, ec);
184 if (ec || !exists)
185 {
186 return;
187 }
188
189 std::fstream enableFile(path, std::ios::out);
190 if (!enableFile.good())
191 {
192 return;
193 }
194 enableFile << 1;
195}
196
Kuiying Wangd5407412020-09-09 16:06:56 +0800197void createRedundancySensor(
Josh Lehan5170fe62022-08-03 13:17:41 -0700198 const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
Kuiying Wangd5407412020-09-09 16:06:56 +0800199 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700200 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800201 sdbusplus::asio::object_server& objectServer)
202{
203
204 conn->async_method_call(
205 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700206 const ManagedObjectType& managedObj) {
Ed Tanousbb679322022-05-16 16:10:00 -0700207 if (ec)
208 {
209 std::cerr << "Error calling entity manager \n";
210 return;
211 }
Zev Weiss77636ec2022-08-12 18:21:01 -0700212 for (const auto& [path, interfaces] : managedObj)
Ed Tanousbb679322022-05-16 16:10:00 -0700213 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700214 for (const auto& [intf, cfg] : interfaces)
Kuiying Wangd5407412020-09-09 16:06:56 +0800215 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700216 if (intf == redundancyConfiguration)
Kuiying Wangd5407412020-09-09 16:06:56 +0800217 {
Ed Tanousbb679322022-05-16 16:10:00 -0700218 // currently only support one
Zev Weiss77636ec2022-08-12 18:21:01 -0700219 auto findCount = cfg.find("AllowedFailures");
220 if (findCount == cfg.end())
Kuiying Wangd5407412020-09-09 16:06:56 +0800221 {
Ed Tanousbb679322022-05-16 16:10:00 -0700222 std::cerr << "Malformed redundancy record \n";
Kuiying Wangd5407412020-09-09 16:06:56 +0800223 return;
224 }
Ed Tanousbb679322022-05-16 16:10:00 -0700225 std::vector<std::string> sensorList;
226
Zev Weiss77636ec2022-08-12 18:21:01 -0700227 for (const auto& [name, sensor] : sensors)
Ed Tanousbb679322022-05-16 16:10:00 -0700228 {
229 sensorList.push_back(
230 "/xyz/openbmc_project/sensors/fan_tach/" +
Zev Weiss77636ec2022-08-12 18:21:01 -0700231 sensor->name);
Ed Tanousbb679322022-05-16 16:10:00 -0700232 }
233 systemRedundancy.reset();
Zev Weiss77636ec2022-08-12 18:21:01 -0700234 systemRedundancy.emplace(
235 RedundancySensor(std::get<uint64_t>(findCount->second),
236 sensorList, objectServer, path));
Ed Tanousbb679322022-05-16 16:10:00 -0700237
238 return;
Kuiying Wangd5407412020-09-09 16:06:56 +0800239 }
240 }
Ed Tanousbb679322022-05-16 16:10:00 -0700241 }
Kuiying Wangd5407412020-09-09 16:06:56 +0800242 },
Nan Zhou3e620af2022-09-20 22:28:31 +0000243 "xyz.openbmc_project.EntityManager", "/xyz/openbmc_project/inventory",
Kuiying Wangd5407412020-09-09 16:06:56 +0800244 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
245}
246
James Feist6714a252018-09-10 15:26:18 -0700247void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800248 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Josh Lehan5170fe62022-08-03 13:17:41 -0700249 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700250 tachSensors,
251 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
252 pwmSensors,
253 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700254 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700255 sensorsChanged,
256 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700257{
James Feistde5e9702019-09-18 16:13:02 -0700258 auto getter = std::make_shared<GetSensorConfiguration>(
259 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700260 [&io, &objectServer, &tachSensors, &pwmSensors, &dbusConnection,
261 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700262 bool firstScan = sensorsChanged == nullptr;
263 std::vector<fs::path> paths;
264 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
265 {
266 std::cerr << "No fan sensors in system\n";
267 return;
268 }
269
270 // iterate through all found fan sensors, and try to match them with
271 // configuration
272 for (const auto& path : paths)
273 {
274 std::smatch match;
275 std::string pathStr = path.string();
276
277 std::regex_search(pathStr, match, inputRegex);
278 std::string indexStr = *(match.begin() + 1);
279
280 fs::path directory = path.parent_path();
281 FanTypes fanType = getFanType(directory);
Zev Weiss054aad82022-08-18 01:37:34 -0700282 std::string cfgIntf = configInterfaceName(sensorTypes[fanType]);
Ed Tanousbb679322022-05-16 16:10:00 -0700283
284 // convert to 0 based
285 size_t index = std::stoul(indexStr) - 1;
286
287 const char* baseType = nullptr;
288 const SensorData* sensorData = nullptr;
289 const std::string* interfacePath = nullptr;
290 const SensorBaseConfiguration* baseConfiguration = nullptr;
Zev Weiss77636ec2022-08-12 18:21:01 -0700291 for (const auto& [path, cfgData] : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800292 {
Ed Tanousbb679322022-05-16 16:10:00 -0700293 // find the base of the configuration to see if indexes
294 // match
Zev Weiss054aad82022-08-18 01:37:34 -0700295 auto sensorBaseFind = cfgData.find(cfgIntf);
Zev Weiss77636ec2022-08-12 18:21:01 -0700296 if (sensorBaseFind == cfgData.end())
James Feist95b079b2018-11-21 09:28:00 -0800297 {
Ed Tanousbb679322022-05-16 16:10:00 -0700298 continue;
299 }
300
301 baseConfiguration = &(*sensorBaseFind);
Zev Weiss77636ec2022-08-12 18:21:01 -0700302 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700303 baseType = sensorTypes[fanType];
304
305 auto findIndex = baseConfiguration->second.find("Index");
306 if (findIndex == baseConfiguration->second.end())
307 {
308 std::cerr << baseConfiguration->first << " missing index\n";
309 continue;
310 }
311 unsigned int configIndex = std::visit(
312 VariantToUnsignedIntVisitor(), findIndex->second);
313 if (configIndex != index)
314 {
315 continue;
316 }
317 if (fanType == FanTypes::aspeed || fanType == FanTypes::nuvoton)
318 {
319 // there will be only 1 aspeed or nuvoton sensor object
320 // in sysfs, we found the fan
Zev Weiss77636ec2022-08-12 18:21:01 -0700321 sensorData = &cfgData;
Ed Tanousbb679322022-05-16 16:10:00 -0700322 break;
323 }
324 if (fanType == FanTypes::i2c)
325 {
326 size_t bus = 0;
327 size_t address = 0;
328
329 std::string link =
330 fs::read_symlink(directory / "device").filename();
331
332 size_t findDash = link.find('-');
333 if (findDash == std::string::npos ||
334 link.size() <= findDash + 1)
James Feistde5e9702019-09-18 16:13:02 -0700335 {
Ed Tanousbb679322022-05-16 16:10:00 -0700336 std::cerr << "Error finding device from symlink";
James Feistde5e9702019-09-18 16:13:02 -0700337 }
Ed Tanousbb679322022-05-16 16:10:00 -0700338 bus = std::stoi(link.substr(0, findDash));
339 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800340
Ed Tanousbb679322022-05-16 16:10:00 -0700341 auto findBus = baseConfiguration->second.find("Bus");
342 auto findAddress =
343 baseConfiguration->second.find("Address");
344 if (findBus == baseConfiguration->second.end() ||
345 findAddress == baseConfiguration->second.end())
James Feistde5e9702019-09-18 16:13:02 -0700346 {
347 std::cerr << baseConfiguration->first
Ed Tanousbb679322022-05-16 16:10:00 -0700348 << " missing bus or address\n";
James Feistde5e9702019-09-18 16:13:02 -0700349 continue;
350 }
Ed Tanousbb679322022-05-16 16:10:00 -0700351 unsigned int configBus = std::visit(
352 VariantToUnsignedIntVisitor(), findBus->second);
353 unsigned int configAddress = std::visit(
354 VariantToUnsignedIntVisitor(), findAddress->second);
355
356 if (configBus == bus && configAddress == address)
James Feistde5e9702019-09-18 16:13:02 -0700357 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700358 sensorData = &cfgData;
James Feistde5e9702019-09-18 16:13:02 -0700359 break;
360 }
Ed Tanousbb679322022-05-16 16:10:00 -0700361 }
362 }
363 if (sensorData == nullptr)
364 {
365 std::cerr << "failed to find match for " << path.string()
366 << "\n";
367 continue;
368 }
369
370 auto findSensorName = baseConfiguration->second.find("Name");
371
372 if (findSensorName == baseConfiguration->second.end())
373 {
374 std::cerr << "could not determine configuration name for "
375 << path.string() << "\n";
376 continue;
377 }
378 std::string sensorName =
379 std::get<std::string>(findSensorName->second);
380
381 // on rescans, only update sensors we were signaled by
382 auto findSensor = tachSensors.find(sensorName);
383 if (!firstScan && findSensor != tachSensors.end())
384 {
385 bool found = false;
386 for (auto it = sensorsChanged->begin();
387 it != sensorsChanged->end(); it++)
388 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700389 if (it->ends_with(findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700390 {
Ed Tanousbb679322022-05-16 16:10:00 -0700391 sensorsChanged->erase(it);
392 findSensor->second = nullptr;
393 found = true;
394 break;
James Feistde5e9702019-09-18 16:13:02 -0700395 }
396 }
Ed Tanousbb679322022-05-16 16:10:00 -0700397 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700398 {
James Feist95b079b2018-11-21 09:28:00 -0800399 continue;
400 }
Ed Tanousbb679322022-05-16 16:10:00 -0700401 }
402 std::vector<thresholds::Threshold> sensorThresholds;
403 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
404 {
405 std::cerr << "error populating thresholds for " << sensorName
406 << "\n";
407 }
James Feist95b079b2018-11-21 09:28:00 -0800408
Ed Tanousbb679322022-05-16 16:10:00 -0700409 auto presenceConfig =
Zev Weiss054aad82022-08-18 01:37:34 -0700410 sensorData->find(cfgIntf + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800411
Ed Tanousbb679322022-05-16 16:10:00 -0700412 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
413
414 // presence sensors are optional
415 if (presenceConfig != sensorData->end())
416 {
417 auto findPolarity = presenceConfig->second.find("Polarity");
418 auto findPinName = presenceConfig->second.find("PinName");
419
420 if (findPinName == presenceConfig->second.end() ||
421 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800422 {
Ed Tanousbb679322022-05-16 16:10:00 -0700423 std::cerr << "Malformed Presence Configuration\n";
James Feistde5e9702019-09-18 16:13:02 -0700424 }
Ed Tanousbb679322022-05-16 16:10:00 -0700425 else
James Feistde5e9702019-09-18 16:13:02 -0700426 {
Ed Tanousbb679322022-05-16 16:10:00 -0700427 bool inverted =
428 std::get<std::string>(findPolarity->second) == "Low";
Ed Tanous2049bd22022-07-09 07:20:26 -0700429 if (const auto* pinName =
Ed Tanousbb679322022-05-16 16:10:00 -0700430 std::get_if<std::string>(&findPinName->second))
James Feistde5e9702019-09-18 16:13:02 -0700431 {
Ed Tanousbb679322022-05-16 16:10:00 -0700432 presenceSensor = std::make_unique<PresenceSensor>(
433 *pinName, inverted, io, sensorName);
James Feistde5e9702019-09-18 16:13:02 -0700434 }
435 else
436 {
Ed Tanousbb679322022-05-16 16:10:00 -0700437 std::cerr << "Malformed Presence pinName for sensor "
438 << sensorName << " \n";
James Feistde5e9702019-09-18 16:13:02 -0700439 }
440 }
Ed Tanousbb679322022-05-16 16:10:00 -0700441 }
442 std::optional<RedundancySensor>* redundancy = nullptr;
443 if (fanType == FanTypes::aspeed)
444 {
445 redundancy = &systemRedundancy;
446 }
447
Zev Weissa4d27682022-07-19 15:30:36 -0700448 PowerState powerState = getPowerState(baseConfiguration->second);
Yong Zhao77b3add2021-01-09 04:25:18 +0000449
Ed Tanousbb679322022-05-16 16:10:00 -0700450 constexpr double defaultMaxReading = 25000;
451 constexpr double defaultMinReading = 0;
452 std::pair<double, double> limits =
453 std::make_pair(defaultMinReading, defaultMaxReading);
454
455 auto connector =
Zev Weiss054aad82022-08-18 01:37:34 -0700456 sensorData->find(cfgIntf + std::string(".Connector"));
Ed Tanousbb679322022-05-16 16:10:00 -0700457
458 std::optional<std::string> led;
459 std::string pwmName;
460 fs::path pwmPath;
461
462 // The Mutable parameter is optional, defaulting to false
463 bool isValueMutable = false;
464 if (connector != sensorData->end())
465 {
466 auto findPwm = connector->second.find("Pwm");
467 if (findPwm != connector->second.end())
468 {
469 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
470 findPwm->second);
471 if (!findPwmPath(directory, pwm, pwmPath))
472 {
473 std::cerr << "Connector for " << sensorName
474 << " no pwm channel found!\n";
475 continue;
476 }
477
478 fs::path pwmEnableFile =
479 "pwm" + std::to_string(pwm + 1) + "_enable";
480 fs::path enablePath = pwmPath.parent_path() / pwmEnableFile;
481 enablePwm(enablePath);
482
483 /* use pwm name override if found in configuration else
484 * use default */
485 auto findOverride = connector->second.find("PwmName");
486 if (findOverride != connector->second.end())
487 {
488 pwmName = std::visit(VariantToStringVisitor(),
489 findOverride->second);
490 }
491 else
492 {
493 pwmName = "Pwm_" + std::to_string(pwm + 1);
494 }
495
496 // Check PWM sensor mutability
497 auto findMutable = connector->second.find("Mutable");
498 if (findMutable != connector->second.end())
499 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700500 const auto* ptrMutable =
Ed Tanousbb679322022-05-16 16:10:00 -0700501 std::get_if<bool>(&(findMutable->second));
Ed Tanous2049bd22022-07-09 07:20:26 -0700502 if (ptrMutable != nullptr)
Ed Tanousbb679322022-05-16 16:10:00 -0700503 {
504 isValueMutable = *ptrMutable;
505 }
506 }
507 }
508 else
509 {
510 std::cerr << "Connector for " << sensorName
511 << " missing pwm!\n";
512 }
513
514 auto findLED = connector->second.find("LED");
515 if (findLED != connector->second.end())
516 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700517 const auto* ledName =
518 std::get_if<std::string>(&(findLED->second));
Ed Tanousbb679322022-05-16 16:10:00 -0700519 if (ledName == nullptr)
520 {
521 std::cerr << "Wrong format for LED of " << sensorName
522 << "\n";
523 }
524 else
525 {
526 led = *ledName;
527 }
528 }
529 }
530
531 findLimits(limits, baseConfiguration);
Josh Lehan5170fe62022-08-03 13:17:41 -0700532
Justin Ledford9c47bd72022-08-27 01:02:09 +0000533 enableFanInput(path);
534
Josh Lehan5170fe62022-08-03 13:17:41 -0700535 auto& tachSensor = tachSensors[sensorName];
536 tachSensor = nullptr;
537 tachSensor = std::make_shared<TachSensor>(
Ed Tanousbb679322022-05-16 16:10:00 -0700538 path.string(), baseType, objectServer, dbusConnection,
539 std::move(presenceSensor), redundancy, io, sensorName,
540 std::move(sensorThresholds), *interfacePath, limits, powerState,
541 led);
Josh Lehan5170fe62022-08-03 13:17:41 -0700542 tachSensor->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700543
544 if (!pwmPath.empty() && fs::exists(pwmPath) &&
Ed Tanous2049bd22022-07-09 07:20:26 -0700545 (pwmSensors.count(pwmPath) == 0U))
Ed Tanousbb679322022-05-16 16:10:00 -0700546 {
547 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
548 pwmName, pwmPath, dbusConnection, objectServer,
549 *interfacePath, "Fan", isValueMutable);
550 }
551 }
552
553 createRedundancySensor(tachSensors, dbusConnection, objectServer);
Ed Tanous8a17c302021-09-02 15:07:11 -0700554 });
James Feistde5e9702019-09-18 16:13:02 -0700555 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700556 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
557 retries);
James Feist6714a252018-09-10 15:26:18 -0700558}
559
James Feistb6c0b912019-07-09 12:21:44 -0700560int main()
James Feist6714a252018-09-10 15:26:18 -0700561{
Ed Tanous1f978632023-02-28 18:16:39 -0800562 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700563 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700564 sdbusplus::asio::object_server objectServer(systemBus, true);
565
566 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanousd9067252022-10-22 15:35:52 -0700567 objectServer.add_manager("/xyz/openbmc_project/control");
Lei YUc2f83fe2022-12-02 14:49:47 +0800568 objectServer.add_manager("/xyz/openbmc_project/inventory");
James Feist6714a252018-09-10 15:26:18 -0700569 systemBus->request_name("xyz.openbmc_project.FanSensor");
Josh Lehan5170fe62022-08-03 13:17:41 -0700570 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700571 tachSensors;
572 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
573 pwmSensors;
James Feist5591cf082020-07-15 16:44:54 -0700574 auto sensorsChanged =
575 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700576
Ed Tanous83db50c2023-03-01 10:20:24 -0800577 boost::asio::post(io, [&]() {
James Feist6714a252018-09-10 15:26:18 -0700578 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
579 nullptr);
580 });
581
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700582 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500583 std::function<void(sdbusplus::message_t&)> eventHandler =
584 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700585 if (message.is_method_error())
586 {
587 std::cerr << "callback method error\n";
588 return;
589 }
590 sensorsChanged->insert(message.get_path());
591 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800592 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700593
594 filterTimer.async_wait([&](const boost::system::error_code& ec) {
595 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700596 {
Ed Tanousbb679322022-05-16 16:10:00 -0700597 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700598 return;
599 }
Ed Tanousbb679322022-05-16 16:10:00 -0700600 if (ec)
601 {
602 std::cerr << "timer error\n";
603 return;
604 }
605 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
606 sensorsChanged, 5);
607 });
608 };
James Feist6714a252018-09-10 15:26:18 -0700609
Zev Weiss214d9712022-08-12 12:54:31 -0700610 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
611 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700612
James Feistdc6c55f2018-10-31 12:53:20 -0700613 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500614 std::function<void(sdbusplus::message_t&)> redundancyHandler =
615 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700616 createRedundancySensor(tachSensors, systemBus, objectServer);
617 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500618 auto match = std::make_unique<sdbusplus::bus::match_t>(
619 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700620 "type='signal',member='PropertiesChanged',path_namespace='" +
621 std::string(inventoryPath) + "',arg0namespace='" +
622 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700623 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700624 matches.emplace_back(std::move(match));
625
Bruce Lee1263c3d2021-06-04 15:16:33 +0800626 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700627 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700628 return 0;
James Feist6714a252018-09-10 15:26:18 -0700629}