blob: f888e174b595d41deaf1e44ab266eb6c0ef2324b [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
Chris Sides3d5260d2023-04-10 15:45:00 -050065static const std::map<std::string, FanTypes> compatibleFanTypes = {
66 {"aspeed,ast2400-pwm-tacho", FanTypes::aspeed},
67 {"aspeed,ast2500-pwm-tacho", FanTypes::aspeed},
68 {"nuvoton,npcm750-pwm-fan", FanTypes::nuvoton}
69 // add compatible string here for new fan type
70};
71
James Feist95b079b2018-11-21 09:28:00 -080072FanTypes getFanType(const fs::path& parentPath)
73{
Chris Sides9a472e82023-04-03 15:13:37 -050074 fs::path linkPath = parentPath / "of_node";
75 std::string canonical = fs::canonical(linkPath);
76
77 std::string compatiblePath = canonical + "/compatible";
78 std::ifstream compatibleStream(compatiblePath);
79
Chris Sides3d5260d2023-04-10 15:45:00 -050080 if (!compatibleStream)
James Feist95b079b2018-11-21 09:28:00 -080081 {
Chris Sides9a472e82023-04-03 15:13:37 -050082 std::cerr << "Error opening " << compatiblePath << "\n";
Chris Sides3d5260d2023-04-10 15:45:00 -050083 return FanTypes::i2c;
James Feist95b079b2018-11-21 09:28:00 -080084 }
Chris Sides9a472e82023-04-03 15:13:37 -050085
86 std::string compatibleString;
Chris Sides3d5260d2023-04-10 15:45:00 -050087 while (std::getline(compatibleStream, compatibleString))
Peter Lundgren8843b622019-09-12 10:33:41 -070088 {
Chris Sides9a472e82023-04-03 15:13:37 -050089 compatibleString.pop_back(); // trim EOL before comparisons
90
Chris Sides3d5260d2023-04-10 15:45:00 -050091 std::map<std::string, FanTypes>::const_iterator compatibleIterator =
92 compatibleFanTypes.find(compatibleString);
93
94 if (compatibleIterator != compatibleFanTypes.end())
Chris Sides9a472e82023-04-03 15:13:37 -050095 {
Chris Sides3d5260d2023-04-10 15:45:00 -050096 return compatibleIterator->second;
Chris Sides9a472e82023-04-03 15:13:37 -050097 }
Peter Lundgren8843b622019-09-12 10:33:41 -070098 }
Chris Sides9a472e82023-04-03 15:13:37 -050099
James Feist95b079b2018-11-21 09:28:00 -0800100 return FanTypes::i2c;
101}
Jeff Linabf91de2020-12-23 10:55:42 +0800102void enablePwm(const fs::path& filePath)
103{
104 std::fstream enableFile(filePath, std::ios::in | std::ios::out);
105 if (!enableFile.good())
106 {
107 std::cerr << "Error read/write " << filePath << "\n";
108 return;
109 }
James Feistdc6c55f2018-10-31 12:53:20 -0700110
Jeff Linabf91de2020-12-23 10:55:42 +0800111 std::string regulateMode;
112 std::getline(enableFile, regulateMode);
113 if (regulateMode == "0")
114 {
115 enableFile << 1;
116 }
117}
Howard Chiuddf25d12021-12-02 15:14:44 +0800118bool findPwmfanPath(unsigned int configPwmfanIndex, fs::path& pwmPath)
119{
120 /* Search PWM since pwm-fan had separated
121 * PWM from tach directory and 1 channel only*/
122 std::vector<fs::path> pwmfanPaths;
123 std::string pwnfanDevName("pwm-fan");
124
125 pwnfanDevName += std::to_string(configPwmfanIndex);
126
127 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwmfanPaths))
128 {
129 std::cerr << "No PWMs are found!\n";
130 return false;
131 }
132 for (const auto& path : pwmfanPaths)
133 {
134 std::error_code ec;
135 fs::path link = fs::read_symlink(path.parent_path() / "device", ec);
136
137 if (ec)
138 {
139 std::cerr << "read_symlink() failed: " << ec.message() << " ("
140 << ec.value() << ")\n";
141 continue;
142 }
143
144 if (link.filename().string() == pwnfanDevName)
145 {
146 pwmPath = path;
147 return true;
148 }
149 }
150 return false;
151}
152bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath)
153{
154 std::error_code ec;
155
156 /* Assuming PWM file is appeared in the same directory as fanX_input */
157 auto path = directory / ("pwm" + std::to_string(pwm + 1));
158 bool exists = fs::exists(path, ec);
159
160 if (ec || !exists)
161 {
162 /* PWM file not exist or error happened */
163 if (ec)
164 {
165 std::cerr << "exists() failed: " << ec.message() << " ("
166 << ec.value() << ")\n";
167 }
168 /* try search form pwm-fanX directory */
169 return findPwmfanPath(pwm, pwmPath);
170 }
171
172 pwmPath = path;
173 return true;
174}
Justin Ledford9c47bd72022-08-27 01:02:09 +0000175
176// The argument to this function should be the fanN_input file that we want to
177// enable. The function will locate the corresponding fanN_enable file if it
178// exists. Note that some drivers don't provide this file if the sensors are
179// always enabled.
180void enableFanInput(const fs::path& fanInputPath)
181{
182 std::error_code ec;
183 std::string path(fanInputPath.string());
184 boost::replace_last(path, "input", "enable");
185
186 bool exists = fs::exists(path, ec);
187 if (ec || !exists)
188 {
189 return;
190 }
191
192 std::fstream enableFile(path, std::ios::out);
193 if (!enableFile.good())
194 {
195 return;
196 }
197 enableFile << 1;
198}
199
Kuiying Wangd5407412020-09-09 16:06:56 +0800200void createRedundancySensor(
Josh Lehan5170fe62022-08-03 13:17:41 -0700201 const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
Kuiying Wangd5407412020-09-09 16:06:56 +0800202 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700203 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800204 sdbusplus::asio::object_server& objectServer)
205{
206
207 conn->async_method_call(
208 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700209 const ManagedObjectType& managedObj) {
Ed Tanousbb679322022-05-16 16:10:00 -0700210 if (ec)
211 {
212 std::cerr << "Error calling entity manager \n";
213 return;
214 }
Zev Weiss77636ec2022-08-12 18:21:01 -0700215 for (const auto& [path, interfaces] : managedObj)
Ed Tanousbb679322022-05-16 16:10:00 -0700216 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700217 for (const auto& [intf, cfg] : interfaces)
Kuiying Wangd5407412020-09-09 16:06:56 +0800218 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700219 if (intf == redundancyConfiguration)
Kuiying Wangd5407412020-09-09 16:06:56 +0800220 {
Ed Tanousbb679322022-05-16 16:10:00 -0700221 // currently only support one
Zev Weiss77636ec2022-08-12 18:21:01 -0700222 auto findCount = cfg.find("AllowedFailures");
223 if (findCount == cfg.end())
Kuiying Wangd5407412020-09-09 16:06:56 +0800224 {
Ed Tanousbb679322022-05-16 16:10:00 -0700225 std::cerr << "Malformed redundancy record \n";
Kuiying Wangd5407412020-09-09 16:06:56 +0800226 return;
227 }
Ed Tanousbb679322022-05-16 16:10:00 -0700228 std::vector<std::string> sensorList;
229
Zev Weiss77636ec2022-08-12 18:21:01 -0700230 for (const auto& [name, sensor] : sensors)
Ed Tanousbb679322022-05-16 16:10:00 -0700231 {
232 sensorList.push_back(
233 "/xyz/openbmc_project/sensors/fan_tach/" +
Zev Weiss77636ec2022-08-12 18:21:01 -0700234 sensor->name);
Ed Tanousbb679322022-05-16 16:10:00 -0700235 }
236 systemRedundancy.reset();
Zev Weiss77636ec2022-08-12 18:21:01 -0700237 systemRedundancy.emplace(
238 RedundancySensor(std::get<uint64_t>(findCount->second),
239 sensorList, objectServer, path));
Ed Tanousbb679322022-05-16 16:10:00 -0700240
241 return;
Kuiying Wangd5407412020-09-09 16:06:56 +0800242 }
243 }
Ed Tanousbb679322022-05-16 16:10:00 -0700244 }
Kuiying Wangd5407412020-09-09 16:06:56 +0800245 },
Nan Zhou3e620af2022-09-20 22:28:31 +0000246 "xyz.openbmc_project.EntityManager", "/xyz/openbmc_project/inventory",
Kuiying Wangd5407412020-09-09 16:06:56 +0800247 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
248}
249
James Feist6714a252018-09-10 15:26:18 -0700250void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800251 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Josh Lehan5170fe62022-08-03 13:17:41 -0700252 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700253 tachSensors,
254 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
255 pwmSensors,
256 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700257 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700258 sensorsChanged,
259 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700260{
James Feistde5e9702019-09-18 16:13:02 -0700261 auto getter = std::make_shared<GetSensorConfiguration>(
262 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700263 [&io, &objectServer, &tachSensors, &pwmSensors, &dbusConnection,
264 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700265 bool firstScan = sensorsChanged == nullptr;
266 std::vector<fs::path> paths;
267 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
268 {
269 std::cerr << "No fan sensors in system\n";
270 return;
271 }
272
273 // iterate through all found fan sensors, and try to match them with
274 // configuration
275 for (const auto& path : paths)
276 {
277 std::smatch match;
278 std::string pathStr = path.string();
279
280 std::regex_search(pathStr, match, inputRegex);
281 std::string indexStr = *(match.begin() + 1);
282
283 fs::path directory = path.parent_path();
284 FanTypes fanType = getFanType(directory);
Zev Weiss054aad82022-08-18 01:37:34 -0700285 std::string cfgIntf = configInterfaceName(sensorTypes[fanType]);
Ed Tanousbb679322022-05-16 16:10:00 -0700286
287 // convert to 0 based
288 size_t index = std::stoul(indexStr) - 1;
289
290 const char* baseType = nullptr;
291 const SensorData* sensorData = nullptr;
292 const std::string* interfacePath = nullptr;
293 const SensorBaseConfiguration* baseConfiguration = nullptr;
Zev Weiss77636ec2022-08-12 18:21:01 -0700294 for (const auto& [path, cfgData] : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800295 {
Ed Tanousbb679322022-05-16 16:10:00 -0700296 // find the base of the configuration to see if indexes
297 // match
Zev Weiss054aad82022-08-18 01:37:34 -0700298 auto sensorBaseFind = cfgData.find(cfgIntf);
Zev Weiss77636ec2022-08-12 18:21:01 -0700299 if (sensorBaseFind == cfgData.end())
James Feist95b079b2018-11-21 09:28:00 -0800300 {
Ed Tanousbb679322022-05-16 16:10:00 -0700301 continue;
302 }
303
304 baseConfiguration = &(*sensorBaseFind);
Zev Weiss77636ec2022-08-12 18:21:01 -0700305 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700306 baseType = sensorTypes[fanType];
307
308 auto findIndex = baseConfiguration->second.find("Index");
309 if (findIndex == baseConfiguration->second.end())
310 {
311 std::cerr << baseConfiguration->first << " missing index\n";
312 continue;
313 }
314 unsigned int configIndex = std::visit(
315 VariantToUnsignedIntVisitor(), findIndex->second);
316 if (configIndex != index)
317 {
318 continue;
319 }
320 if (fanType == FanTypes::aspeed || fanType == FanTypes::nuvoton)
321 {
Chris Sides3d5260d2023-04-10 15:45:00 -0500322 // there will be only 1 aspeed or nuvoton
323 // object in sysfs, we found the fan
Zev Weiss77636ec2022-08-12 18:21:01 -0700324 sensorData = &cfgData;
Ed Tanousbb679322022-05-16 16:10:00 -0700325 break;
326 }
327 if (fanType == FanTypes::i2c)
328 {
329 size_t bus = 0;
330 size_t address = 0;
331
332 std::string link =
333 fs::read_symlink(directory / "device").filename();
334
335 size_t findDash = link.find('-');
336 if (findDash == std::string::npos ||
337 link.size() <= findDash + 1)
James Feistde5e9702019-09-18 16:13:02 -0700338 {
Ed Tanousbb679322022-05-16 16:10:00 -0700339 std::cerr << "Error finding device from symlink";
James Feistde5e9702019-09-18 16:13:02 -0700340 }
Ed Tanousbb679322022-05-16 16:10:00 -0700341 bus = std::stoi(link.substr(0, findDash));
342 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800343
Ed Tanousbb679322022-05-16 16:10:00 -0700344 auto findBus = baseConfiguration->second.find("Bus");
345 auto findAddress =
346 baseConfiguration->second.find("Address");
347 if (findBus == baseConfiguration->second.end() ||
348 findAddress == baseConfiguration->second.end())
James Feistde5e9702019-09-18 16:13:02 -0700349 {
350 std::cerr << baseConfiguration->first
Ed Tanousbb679322022-05-16 16:10:00 -0700351 << " missing bus or address\n";
James Feistde5e9702019-09-18 16:13:02 -0700352 continue;
353 }
Ed Tanousbb679322022-05-16 16:10:00 -0700354 unsigned int configBus = std::visit(
355 VariantToUnsignedIntVisitor(), findBus->second);
356 unsigned int configAddress = std::visit(
357 VariantToUnsignedIntVisitor(), findAddress->second);
358
359 if (configBus == bus && configAddress == address)
James Feistde5e9702019-09-18 16:13:02 -0700360 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700361 sensorData = &cfgData;
James Feistde5e9702019-09-18 16:13:02 -0700362 break;
363 }
Ed Tanousbb679322022-05-16 16:10:00 -0700364 }
365 }
366 if (sensorData == nullptr)
367 {
368 std::cerr << "failed to find match for " << path.string()
369 << "\n";
370 continue;
371 }
372
373 auto findSensorName = baseConfiguration->second.find("Name");
374
375 if (findSensorName == baseConfiguration->second.end())
376 {
377 std::cerr << "could not determine configuration name for "
378 << path.string() << "\n";
379 continue;
380 }
381 std::string sensorName =
382 std::get<std::string>(findSensorName->second);
383
384 // on rescans, only update sensors we were signaled by
385 auto findSensor = tachSensors.find(sensorName);
386 if (!firstScan && findSensor != tachSensors.end())
387 {
388 bool found = false;
389 for (auto it = sensorsChanged->begin();
390 it != sensorsChanged->end(); it++)
391 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700392 if (it->ends_with(findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700393 {
Ed Tanousbb679322022-05-16 16:10:00 -0700394 sensorsChanged->erase(it);
395 findSensor->second = nullptr;
396 found = true;
397 break;
James Feistde5e9702019-09-18 16:13:02 -0700398 }
399 }
Ed Tanousbb679322022-05-16 16:10:00 -0700400 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700401 {
James Feist95b079b2018-11-21 09:28:00 -0800402 continue;
403 }
Ed Tanousbb679322022-05-16 16:10:00 -0700404 }
405 std::vector<thresholds::Threshold> sensorThresholds;
406 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
407 {
408 std::cerr << "error populating thresholds for " << sensorName
409 << "\n";
410 }
James Feist95b079b2018-11-21 09:28:00 -0800411
Ed Tanousbb679322022-05-16 16:10:00 -0700412 auto presenceConfig =
Zev Weiss054aad82022-08-18 01:37:34 -0700413 sensorData->find(cfgIntf + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800414
Ed Tanousbb679322022-05-16 16:10:00 -0700415 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
416
417 // presence sensors are optional
418 if (presenceConfig != sensorData->end())
419 {
420 auto findPolarity = presenceConfig->second.find("Polarity");
421 auto findPinName = presenceConfig->second.find("PinName");
422
423 if (findPinName == presenceConfig->second.end() ||
424 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800425 {
Ed Tanousbb679322022-05-16 16:10:00 -0700426 std::cerr << "Malformed Presence Configuration\n";
James Feistde5e9702019-09-18 16:13:02 -0700427 }
Ed Tanousbb679322022-05-16 16:10:00 -0700428 else
James Feistde5e9702019-09-18 16:13:02 -0700429 {
Ed Tanousbb679322022-05-16 16:10:00 -0700430 bool inverted =
431 std::get<std::string>(findPolarity->second) == "Low";
Ed Tanous2049bd22022-07-09 07:20:26 -0700432 if (const auto* pinName =
Ed Tanousbb679322022-05-16 16:10:00 -0700433 std::get_if<std::string>(&findPinName->second))
James Feistde5e9702019-09-18 16:13:02 -0700434 {
Ed Tanousbb679322022-05-16 16:10:00 -0700435 presenceSensor = std::make_unique<PresenceSensor>(
436 *pinName, inverted, io, sensorName);
James Feistde5e9702019-09-18 16:13:02 -0700437 }
438 else
439 {
Ed Tanousbb679322022-05-16 16:10:00 -0700440 std::cerr << "Malformed Presence pinName for sensor "
441 << sensorName << " \n";
James Feistde5e9702019-09-18 16:13:02 -0700442 }
443 }
Ed Tanousbb679322022-05-16 16:10:00 -0700444 }
445 std::optional<RedundancySensor>* redundancy = nullptr;
446 if (fanType == FanTypes::aspeed)
447 {
448 redundancy = &systemRedundancy;
449 }
450
Zev Weissa4d27682022-07-19 15:30:36 -0700451 PowerState powerState = getPowerState(baseConfiguration->second);
Yong Zhao77b3add2021-01-09 04:25:18 +0000452
Ed Tanousbb679322022-05-16 16:10:00 -0700453 constexpr double defaultMaxReading = 25000;
454 constexpr double defaultMinReading = 0;
455 std::pair<double, double> limits =
456 std::make_pair(defaultMinReading, defaultMaxReading);
457
458 auto connector =
Zev Weiss054aad82022-08-18 01:37:34 -0700459 sensorData->find(cfgIntf + std::string(".Connector"));
Ed Tanousbb679322022-05-16 16:10:00 -0700460
461 std::optional<std::string> led;
462 std::string pwmName;
463 fs::path pwmPath;
464
465 // The Mutable parameter is optional, defaulting to false
466 bool isValueMutable = false;
467 if (connector != sensorData->end())
468 {
469 auto findPwm = connector->second.find("Pwm");
470 if (findPwm != connector->second.end())
471 {
472 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
473 findPwm->second);
474 if (!findPwmPath(directory, pwm, pwmPath))
475 {
476 std::cerr << "Connector for " << sensorName
477 << " no pwm channel found!\n";
478 continue;
479 }
480
481 fs::path pwmEnableFile =
482 "pwm" + std::to_string(pwm + 1) + "_enable";
483 fs::path enablePath = pwmPath.parent_path() / pwmEnableFile;
484 enablePwm(enablePath);
485
486 /* use pwm name override if found in configuration else
487 * use default */
488 auto findOverride = connector->second.find("PwmName");
489 if (findOverride != connector->second.end())
490 {
491 pwmName = std::visit(VariantToStringVisitor(),
492 findOverride->second);
493 }
494 else
495 {
496 pwmName = "Pwm_" + std::to_string(pwm + 1);
497 }
498
499 // Check PWM sensor mutability
500 auto findMutable = connector->second.find("Mutable");
501 if (findMutable != connector->second.end())
502 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700503 const auto* ptrMutable =
Ed Tanousbb679322022-05-16 16:10:00 -0700504 std::get_if<bool>(&(findMutable->second));
Ed Tanous2049bd22022-07-09 07:20:26 -0700505 if (ptrMutable != nullptr)
Ed Tanousbb679322022-05-16 16:10:00 -0700506 {
507 isValueMutable = *ptrMutable;
508 }
509 }
510 }
511 else
512 {
513 std::cerr << "Connector for " << sensorName
514 << " missing pwm!\n";
515 }
516
517 auto findLED = connector->second.find("LED");
518 if (findLED != connector->second.end())
519 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700520 const auto* ledName =
521 std::get_if<std::string>(&(findLED->second));
Ed Tanousbb679322022-05-16 16:10:00 -0700522 if (ledName == nullptr)
523 {
524 std::cerr << "Wrong format for LED of " << sensorName
525 << "\n";
526 }
527 else
528 {
529 led = *ledName;
530 }
531 }
532 }
533
534 findLimits(limits, baseConfiguration);
Josh Lehan5170fe62022-08-03 13:17:41 -0700535
Justin Ledford9c47bd72022-08-27 01:02:09 +0000536 enableFanInput(path);
537
Josh Lehan5170fe62022-08-03 13:17:41 -0700538 auto& tachSensor = tachSensors[sensorName];
539 tachSensor = nullptr;
540 tachSensor = std::make_shared<TachSensor>(
Ed Tanousbb679322022-05-16 16:10:00 -0700541 path.string(), baseType, objectServer, dbusConnection,
542 std::move(presenceSensor), redundancy, io, sensorName,
543 std::move(sensorThresholds), *interfacePath, limits, powerState,
544 led);
Josh Lehan5170fe62022-08-03 13:17:41 -0700545 tachSensor->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700546
547 if (!pwmPath.empty() && fs::exists(pwmPath) &&
Ed Tanous2049bd22022-07-09 07:20:26 -0700548 (pwmSensors.count(pwmPath) == 0U))
Ed Tanousbb679322022-05-16 16:10:00 -0700549 {
550 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
551 pwmName, pwmPath, dbusConnection, objectServer,
552 *interfacePath, "Fan", isValueMutable);
553 }
554 }
555
556 createRedundancySensor(tachSensors, dbusConnection, objectServer);
Ed Tanous8a17c302021-09-02 15:07:11 -0700557 });
James Feistde5e9702019-09-18 16:13:02 -0700558 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700559 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
560 retries);
James Feist6714a252018-09-10 15:26:18 -0700561}
562
James Feistb6c0b912019-07-09 12:21:44 -0700563int main()
James Feist6714a252018-09-10 15:26:18 -0700564{
Ed Tanous1f978632023-02-28 18:16:39 -0800565 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700566 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700567 sdbusplus::asio::object_server objectServer(systemBus, true);
568
569 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanousd9067252022-10-22 15:35:52 -0700570 objectServer.add_manager("/xyz/openbmc_project/control");
Lei YUc2f83fe2022-12-02 14:49:47 +0800571 objectServer.add_manager("/xyz/openbmc_project/inventory");
James Feist6714a252018-09-10 15:26:18 -0700572 systemBus->request_name("xyz.openbmc_project.FanSensor");
Josh Lehan5170fe62022-08-03 13:17:41 -0700573 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700574 tachSensors;
575 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
576 pwmSensors;
James Feist5591cf082020-07-15 16:44:54 -0700577 auto sensorsChanged =
578 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700579
Ed Tanous83db50c2023-03-01 10:20:24 -0800580 boost::asio::post(io, [&]() {
James Feist6714a252018-09-10 15:26:18 -0700581 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
582 nullptr);
583 });
584
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700585 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500586 std::function<void(sdbusplus::message_t&)> eventHandler =
587 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700588 if (message.is_method_error())
589 {
590 std::cerr << "callback method error\n";
591 return;
592 }
593 sensorsChanged->insert(message.get_path());
594 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800595 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700596
597 filterTimer.async_wait([&](const boost::system::error_code& ec) {
598 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700599 {
Ed Tanousbb679322022-05-16 16:10:00 -0700600 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700601 return;
602 }
Ed Tanousbb679322022-05-16 16:10:00 -0700603 if (ec)
604 {
605 std::cerr << "timer error\n";
606 return;
607 }
608 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
609 sensorsChanged, 5);
610 });
611 };
James Feist6714a252018-09-10 15:26:18 -0700612
Zev Weiss214d9712022-08-12 12:54:31 -0700613 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
614 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700615
James Feistdc6c55f2018-10-31 12:53:20 -0700616 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500617 std::function<void(sdbusplus::message_t&)> redundancyHandler =
618 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700619 createRedundancySensor(tachSensors, systemBus, objectServer);
620 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500621 auto match = std::make_unique<sdbusplus::bus::match_t>(
622 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700623 "type='signal',member='PropertiesChanged',path_namespace='" +
624 std::string(inventoryPath) + "',arg0namespace='" +
625 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700626 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700627 matches.emplace_back(std::move(match));
628
Bruce Lee1263c3d2021-06-04 15:16:33 +0800629 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700630 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700631 return 0;
James Feist6714a252018-09-10 15:26:18 -0700632}