blob: 33074b68bdd549ce1acdefcde7e2a9949eec8a14 [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
Chris Cain83929002024-03-06 14:20:09 -060017#include "PresenceGpio.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103018#include "PwmSensor.hpp"
19#include "TachSensor.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070020#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103021#include "Utils.hpp"
22#include "VariantVisitors.hpp"
23
James Feist6714a252018-09-10 15:26:18 -070024#include <boost/algorithm/string/replace.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070025#include <boost/asio/error.hpp>
26#include <boost/asio/io_context.hpp>
27#include <boost/asio/post.hpp>
28#include <boost/asio/steady_timer.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070030#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070031#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070033#include <sdbusplus/bus.hpp>
James Feist38fb5982020-05-28 10:09:54 -070034#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070035#include <sdbusplus/message.hpp>
James Feist38fb5982020-05-28 10:09:54 -070036
37#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070038#include <chrono>
39#include <cstddef>
40#include <cstdint>
James Feist24f02f22019-04-15 11:05:39 -070041#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070042#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070043#include <functional>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070044#include <ios>
45#include <iostream>
46#include <map>
Patrick Venture96e97db2019-10-31 13:44:38 -070047#include <memory>
48#include <optional>
James Feist6714a252018-09-10 15:26:18 -070049#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070050#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070051#include <system_error>
Patrick Venture96e97db2019-10-31 13:44:38 -070052#include <utility>
53#include <variant>
54#include <vector>
James Feist6714a252018-09-10 15:26:18 -070055
James Feistcf3bce62019-01-08 10:07:19 -080056namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080057
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000058// The following two structures need to be consistent
Chris Sidesc361e222023-04-05 15:18:20 -050059static auto sensorTypes{std::to_array<const char*>(
60 {"AspeedFan", "I2CFan", "NuvotonFan", "HPEFan"})};
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000061
62enum FanTypes
63{
64 aspeed = 0,
65 i2c,
66 nuvoton,
Chris Sidesc361e222023-04-05 15:18:20 -050067 hpe,
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000068 max,
69};
70
71static_assert(std::tuple_size<decltype(sensorTypes)>::value == FanTypes::max,
72 "sensorTypes element number is not equal to FanTypes number");
73
James Feistdc6c55f2018-10-31 12:53:20 -070074constexpr const char* redundancyConfiguration =
75 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070076static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070077
James Feistdc6c55f2018-10-31 12:53:20 -070078// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070079std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080080
Chris Sides3d5260d2023-04-10 15:45:00 -050081static const std::map<std::string, FanTypes> compatibleFanTypes = {
82 {"aspeed,ast2400-pwm-tacho", FanTypes::aspeed},
83 {"aspeed,ast2500-pwm-tacho", FanTypes::aspeed},
Potin Lai40c4d682023-05-19 17:19:03 +080084 {"aspeed,ast2600-pwm-tach", FanTypes::aspeed},
Chris Sidesc361e222023-04-05 15:18:20 -050085 {"nuvoton,npcm750-pwm-fan", FanTypes::nuvoton},
Brian Mad37e1db2023-05-08 13:36:53 +080086 {"nuvoton,npcm845-pwm-fan", FanTypes::nuvoton},
Chris Sidesc361e222023-04-05 15:18:20 -050087 {"hpe,gxp-fan-ctrl", FanTypes::hpe}
Chris Sides3d5260d2023-04-10 15:45:00 -050088 // add compatible string here for new fan type
89};
90
James Feist95b079b2018-11-21 09:28:00 -080091FanTypes getFanType(const fs::path& parentPath)
92{
Chris Sides9a472e82023-04-03 15:13:37 -050093 fs::path linkPath = parentPath / "of_node";
Zhikui Ren39963222023-05-04 17:06:50 -070094 if (!fs::exists(linkPath))
95 {
96 return FanTypes::i2c;
97 }
Chris Sides9a472e82023-04-03 15:13:37 -050098
Zhikui Ren39963222023-05-04 17:06:50 -070099 std::string canonical = fs::canonical(linkPath);
Chris Sides9a472e82023-04-03 15:13:37 -0500100 std::string compatiblePath = canonical + "/compatible";
101 std::ifstream compatibleStream(compatiblePath);
102
Chris Sides3d5260d2023-04-10 15:45:00 -0500103 if (!compatibleStream)
James Feist95b079b2018-11-21 09:28:00 -0800104 {
Chris Sides9a472e82023-04-03 15:13:37 -0500105 std::cerr << "Error opening " << compatiblePath << "\n";
Chris Sides3d5260d2023-04-10 15:45:00 -0500106 return FanTypes::i2c;
James Feist95b079b2018-11-21 09:28:00 -0800107 }
Chris Sides9a472e82023-04-03 15:13:37 -0500108
109 std::string compatibleString;
Chris Sides3d5260d2023-04-10 15:45:00 -0500110 while (std::getline(compatibleStream, compatibleString))
Peter Lundgren8843b622019-09-12 10:33:41 -0700111 {
Chris Sides9a472e82023-04-03 15:13:37 -0500112 compatibleString.pop_back(); // trim EOL before comparisons
113
Chris Sides3d5260d2023-04-10 15:45:00 -0500114 std::map<std::string, FanTypes>::const_iterator compatibleIterator =
115 compatibleFanTypes.find(compatibleString);
116
117 if (compatibleIterator != compatibleFanTypes.end())
Chris Sides9a472e82023-04-03 15:13:37 -0500118 {
Chris Sides3d5260d2023-04-10 15:45:00 -0500119 return compatibleIterator->second;
Chris Sides9a472e82023-04-03 15:13:37 -0500120 }
Peter Lundgren8843b622019-09-12 10:33:41 -0700121 }
Chris Sides9a472e82023-04-03 15:13:37 -0500122
James Feist95b079b2018-11-21 09:28:00 -0800123 return FanTypes::i2c;
124}
Jeff Linabf91de2020-12-23 10:55:42 +0800125void enablePwm(const fs::path& filePath)
126{
127 std::fstream enableFile(filePath, std::ios::in | std::ios::out);
128 if (!enableFile.good())
129 {
130 std::cerr << "Error read/write " << filePath << "\n";
131 return;
132 }
James Feistdc6c55f2018-10-31 12:53:20 -0700133
Jeff Linabf91de2020-12-23 10:55:42 +0800134 std::string regulateMode;
135 std::getline(enableFile, regulateMode);
136 if (regulateMode == "0")
137 {
138 enableFile << 1;
139 }
140}
Howard Chiuddf25d12021-12-02 15:14:44 +0800141bool findPwmfanPath(unsigned int configPwmfanIndex, fs::path& pwmPath)
142{
143 /* Search PWM since pwm-fan had separated
144 * PWM from tach directory and 1 channel only*/
145 std::vector<fs::path> pwmfanPaths;
146 std::string pwnfanDevName("pwm-fan");
147
148 pwnfanDevName += std::to_string(configPwmfanIndex);
149
150 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwmfanPaths))
151 {
152 std::cerr << "No PWMs are found!\n";
153 return false;
154 }
155 for (const auto& path : pwmfanPaths)
156 {
157 std::error_code ec;
158 fs::path link = fs::read_symlink(path.parent_path() / "device", ec);
159
160 if (ec)
161 {
162 std::cerr << "read_symlink() failed: " << ec.message() << " ("
163 << ec.value() << ")\n";
164 continue;
165 }
166
167 if (link.filename().string() == pwnfanDevName)
168 {
169 pwmPath = path;
170 return true;
171 }
172 }
173 return false;
174}
175bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath)
176{
177 std::error_code ec;
178
179 /* Assuming PWM file is appeared in the same directory as fanX_input */
180 auto path = directory / ("pwm" + std::to_string(pwm + 1));
181 bool exists = fs::exists(path, ec);
182
183 if (ec || !exists)
184 {
185 /* PWM file not exist or error happened */
186 if (ec)
187 {
188 std::cerr << "exists() failed: " << ec.message() << " ("
189 << ec.value() << ")\n";
190 }
191 /* try search form pwm-fanX directory */
192 return findPwmfanPath(pwm, pwmPath);
193 }
194
195 pwmPath = path;
196 return true;
197}
Justin Ledford9c47bd72022-08-27 01:02:09 +0000198
199// The argument to this function should be the fanN_input file that we want to
200// enable. The function will locate the corresponding fanN_enable file if it
201// exists. Note that some drivers don't provide this file if the sensors are
202// always enabled.
203void enableFanInput(const fs::path& fanInputPath)
204{
205 std::error_code ec;
206 std::string path(fanInputPath.string());
207 boost::replace_last(path, "input", "enable");
208
209 bool exists = fs::exists(path, ec);
210 if (ec || !exists)
211 {
212 return;
213 }
214
215 std::fstream enableFile(path, std::ios::out);
216 if (!enableFile.good())
217 {
218 return;
219 }
220 enableFile << 1;
221}
222
Kuiying Wangd5407412020-09-09 16:06:56 +0800223void createRedundancySensor(
Josh Lehan5170fe62022-08-03 13:17:41 -0700224 const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
Kuiying Wangd5407412020-09-09 16:06:56 +0800225 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700226 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800227 sdbusplus::asio::object_server& objectServer)
228{
Kuiying Wangd5407412020-09-09 16:06:56 +0800229 conn->async_method_call(
230 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700231 const ManagedObjectType& managedObj) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400232 if (ec)
Kuiying Wangd5407412020-09-09 16:06:56 +0800233 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400234 std::cerr << "Error calling entity manager \n";
235 return;
236 }
237 for (const auto& [path, interfaces] : managedObj)
238 {
239 for (const auto& [intf, cfg] : interfaces)
Kuiying Wangd5407412020-09-09 16:06:56 +0800240 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400241 if (intf == redundancyConfiguration)
Kuiying Wangd5407412020-09-09 16:06:56 +0800242 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400243 // currently only support one
244 auto findCount = cfg.find("AllowedFailures");
245 if (findCount == cfg.end())
246 {
247 std::cerr << "Malformed redundancy record \n";
248 return;
249 }
250 std::vector<std::string> sensorList;
251
252 for (const auto& [name, sensor] : sensors)
253 {
254 sensorList.push_back(
255 "/xyz/openbmc_project/sensors/fan_tach/" +
256 sensor->name);
257 }
258 systemRedundancy.reset();
259 systemRedundancy.emplace(RedundancySensor(
260 std::get<uint64_t>(findCount->second), sensorList,
261 objectServer, path));
262
Kuiying Wangd5407412020-09-09 16:06:56 +0800263 return;
264 }
265 }
266 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400267 },
Nan Zhou3e620af2022-09-20 22:28:31 +0000268 "xyz.openbmc_project.EntityManager", "/xyz/openbmc_project/inventory",
Kuiying Wangd5407412020-09-09 16:06:56 +0800269 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
270}
271
James Feist6714a252018-09-10 15:26:18 -0700272void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800273 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Josh Lehan5170fe62022-08-03 13:17:41 -0700274 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700275 tachSensors,
276 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
277 pwmSensors,
Chris Cain83929002024-03-06 14:20:09 -0600278 boost::container::flat_map<std::string, std::weak_ptr<PresenceGpio>>&
279 presenceGpios,
James Feist6714a252018-09-10 15:26:18 -0700280 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700281 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700282 sensorsChanged,
283 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700284{
Chris Cainc45e18f2024-07-24 15:58:00 -0500285 auto getter = std::make_shared<
286 GetSensorConfiguration>(dbusConnection, [&io, &objectServer,
287 &tachSensors, &pwmSensors,
288 &presenceGpios,
289 &dbusConnection,
290 sensorsChanged](
291 const ManagedObjectType&
292 sensorConfigurations) {
293 bool firstScan = sensorsChanged == nullptr;
294 std::vector<fs::path> paths;
295 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
296 {
297 std::cerr << "No fan sensors in system\n";
298 return;
299 }
300
301 // iterate through all found fan sensors, and try to match them with
302 // configuration
303 for (const auto& path : paths)
304 {
305 std::smatch match;
306 std::string pathStr = path.string();
307
308 std::regex_search(pathStr, match, inputRegex);
309 std::string indexStr = *(match.begin() + 1);
310
311 fs::path directory = path.parent_path();
312 FanTypes fanType = getFanType(directory);
313 std::string cfgIntf = configInterfaceName(sensorTypes[fanType]);
314
315 // convert to 0 based
316 size_t index = std::stoul(indexStr) - 1;
317
318 const char* baseType = nullptr;
319 const SensorData* sensorData = nullptr;
320 const std::string* interfacePath = nullptr;
321 const SensorBaseConfiguration* baseConfiguration = nullptr;
322 for (const auto& [path, cfgData] : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800323 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500324 // find the base of the configuration to see if indexes
325 // match
326 auto sensorBaseFind = cfgData.find(cfgIntf);
327 if (sensorBaseFind == cfgData.end())
Patrick Williams2aaf7172024-08-16 15:20:40 -0400328 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500329 continue;
330 }
331
332 baseConfiguration = &(*sensorBaseFind);
333 interfacePath = &path.str;
334 baseType = sensorTypes[fanType];
335
336 auto findIndex = baseConfiguration->second.find("Index");
337 if (findIndex == baseConfiguration->second.end())
338 {
339 std::cerr << baseConfiguration->first << " missing index\n";
340 continue;
341 }
342 unsigned int configIndex = std::visit(
343 VariantToUnsignedIntVisitor(), findIndex->second);
344 if (configIndex != index)
345 {
346 continue;
347 }
348 if (fanType == FanTypes::aspeed ||
349 fanType == FanTypes::nuvoton || fanType == FanTypes::hpe)
350 {
351 // there will be only 1 aspeed or nuvoton or hpe sensor
352 // object in sysfs, we found the fan
353 sensorData = &cfgData;
354 break;
355 }
356 if (fanType == FanTypes::i2c)
357 {
358 std::string deviceName =
359 fs::read_symlink(directory / "device").filename();
360
361 size_t bus = 0;
362 size_t addr = 0;
363 if (!getDeviceBusAddr(deviceName, bus, addr))
James Feistde5e9702019-09-18 16:13:02 -0700364 {
Akshit Shah03d333e2023-08-23 22:14:28 +0000365 continue;
James Feistde5e9702019-09-18 16:13:02 -0700366 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800367
Chris Cainc45e18f2024-07-24 15:58:00 -0500368 auto findBus = baseConfiguration->second.find("Bus");
369 auto findAddress =
370 baseConfiguration->second.find("Address");
371 if (findBus == baseConfiguration->second.end() ||
372 findAddress == baseConfiguration->second.end())
373 {
374 std::cerr << baseConfiguration->first
375 << " missing bus or address\n";
376 continue;
377 }
378 unsigned int configBus = std::visit(
379 VariantToUnsignedIntVisitor(), findBus->second);
380 unsigned int configAddress = std::visit(
381 VariantToUnsignedIntVisitor(), findAddress->second);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400382
Chris Cainc45e18f2024-07-24 15:58:00 -0500383 if (configBus == bus && configAddress == addr)
James Feistde5e9702019-09-18 16:13:02 -0700384 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700385 sensorData = &cfgData;
James Feistde5e9702019-09-18 16:13:02 -0700386 break;
387 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500388 }
389 }
390 if (sensorData == nullptr)
391 {
392 std::cerr << "failed to find match for " << path.string()
393 << "\n";
394 continue;
395 }
396
397 auto findSensorName = baseConfiguration->second.find("Name");
398
399 if (findSensorName == baseConfiguration->second.end())
400 {
401 std::cerr << "could not determine configuration name for "
402 << path.string() << "\n";
403 continue;
404 }
405 std::string sensorName =
406 std::get<std::string>(findSensorName->second);
407
408 // on rescans, only update sensors we were signaled by
409 auto findSensor = tachSensors.find(sensorName);
410 if (!firstScan && findSensor != tachSensors.end())
411 {
412 bool found = false;
413 for (auto it = sensorsChanged->begin();
414 it != sensorsChanged->end(); it++)
415 {
416 if (it->ends_with(findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700417 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500418 sensorsChanged->erase(it);
419 findSensor->second = nullptr;
420 found = true;
421 break;
James Feistde5e9702019-09-18 16:13:02 -0700422 }
423 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500424 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700425 {
James Feist95b079b2018-11-21 09:28:00 -0800426 continue;
427 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500428 }
429 std::vector<thresholds::Threshold> sensorThresholds;
430 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
431 {
432 std::cerr << "error populating thresholds for " << sensorName
433 << "\n";
434 }
James Feist95b079b2018-11-21 09:28:00 -0800435
Chris Cainc45e18f2024-07-24 15:58:00 -0500436 auto presenceConfig =
437 sensorData->find(cfgIntf + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800438
Chris Cainc45e18f2024-07-24 15:58:00 -0500439 std::shared_ptr<PresenceGpio> presenceGpio(nullptr);
440
441 // presence sensors are optional
442 if (presenceConfig != sensorData->end())
443 {
444 auto findPolarity = presenceConfig->second.find("Polarity");
445 auto findPinName = presenceConfig->second.find("PinName");
446
447 if (findPinName == presenceConfig->second.end() ||
448 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800449 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500450 std::cerr << "Malformed Presence Configuration\n";
James Feistde5e9702019-09-18 16:13:02 -0700451 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500452 else
Patrick Williams2aaf7172024-08-16 15:20:40 -0400453 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500454 bool inverted =
455 std::get<std::string>(findPolarity->second) == "Low";
456 const auto* pinName =
457 std::get_if<std::string>(&findPinName->second);
458
459 if (pinName != nullptr)
James Feistde5e9702019-09-18 16:13:02 -0700460 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500461 auto findPresenceGpio = presenceGpios.find(*pinName);
462 if (findPresenceGpio != presenceGpios.end())
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +0200463 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500464 auto p = findPresenceGpio->second.lock();
465 if (p)
Patrick Williams2aaf7172024-08-16 15:20:40 -0400466 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500467 presenceGpio = p;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400468 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500469 }
470 if (!presenceGpio)
471 {
472 try
Patrick Williams2aaf7172024-08-16 15:20:40 -0400473 {
Chris Cain83929002024-03-06 14:20:09 -0600474 presenceGpio =
475 std::make_shared<EventPresenceGpio>(
476 "Fan", sensorName, *pinName, inverted,
477 io);
478 presenceGpios[*pinName] = presenceGpio;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400479 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500480 catch (const std::system_error& e)
Patrick Williams2aaf7172024-08-16 15:20:40 -0400481 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500482 std::cerr
483 << "Failed to create GPIO monitor object for "
484 << *pinName << " / " << sensorName << ": "
485 << e.what() << "\n";
Patrick Williams2aaf7172024-08-16 15:20:40 -0400486 }
487 }
Ed Tanousbb679322022-05-16 16:10:00 -0700488 }
489 else
490 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500491 std::cerr << "Malformed Presence pinName for sensor "
492 << sensorName << " \n";
493 }
494 }
495 }
496 std::optional<RedundancySensor>* redundancy = nullptr;
497 if (fanType == FanTypes::aspeed)
498 {
499 redundancy = &systemRedundancy;
500 }
501
502 PowerState powerState = getPowerState(baseConfiguration->second);
503
504 constexpr double defaultMaxReading = 25000;
505 constexpr double defaultMinReading = 0;
506 std::pair<double, double> limits =
507 std::make_pair(defaultMinReading, defaultMaxReading);
508
509 auto connector =
510 sensorData->find(cfgIntf + std::string(".Connector"));
511
512 std::optional<std::string> led;
513 std::string pwmName;
514 fs::path pwmPath;
515
516 // The Mutable parameter is optional, defaulting to false
517 bool isValueMutable = false;
518 if (connector != sensorData->end())
519 {
520 auto findPwm = connector->second.find("Pwm");
521 if (findPwm != connector->second.end())
522 {
523 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
524 findPwm->second);
525 if (!findPwmPath(directory, pwm, pwmPath))
526 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400527 std::cerr << "Connector for " << sensorName
Chris Cainc45e18f2024-07-24 15:58:00 -0500528 << " no pwm channel found!\n";
529 continue;
Ed Tanousbb679322022-05-16 16:10:00 -0700530 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400531
Chris Cainc45e18f2024-07-24 15:58:00 -0500532 fs::path pwmEnableFile =
533 "pwm" + std::to_string(pwm + 1) + "_enable";
534 fs::path enablePath = pwmPath.parent_path() / pwmEnableFile;
535 enablePwm(enablePath);
536
537 /* use pwm name override if found in configuration else
538 * use default */
539 auto findOverride = connector->second.find("PwmName");
540 if (findOverride != connector->second.end())
Patrick Williams2aaf7172024-08-16 15:20:40 -0400541 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500542 pwmName = std::visit(VariantToStringVisitor(),
543 findOverride->second);
544 }
545 else
546 {
547 pwmName = "Pwm_" + std::to_string(pwm + 1);
548 }
549
550 // Check PWM sensor mutability
551 auto findMutable = connector->second.find("Mutable");
552 if (findMutable != connector->second.end())
553 {
554 const auto* ptrMutable =
555 std::get_if<bool>(&(findMutable->second));
556 if (ptrMutable != nullptr)
Patrick Williams2aaf7172024-08-16 15:20:40 -0400557 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500558 isValueMutable = *ptrMutable;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400559 }
560 }
561 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500562 else
Patrick Williams2aaf7172024-08-16 15:20:40 -0400563 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500564 std::cerr
565 << "Connector for " << sensorName << " missing pwm!\n";
566 }
567
568 auto findLED = connector->second.find("LED");
569 if (findLED != connector->second.end())
570 {
571 const auto* ledName =
572 std::get_if<std::string>(&(findLED->second));
573 if (ledName == nullptr)
574 {
575 std::cerr
576 << "Wrong format for LED of " << sensorName << "\n";
577 }
578 else
579 {
580 led = *ledName;
581 }
Ed Tanousbb679322022-05-16 16:10:00 -0700582 }
583 }
584
Chris Cainc45e18f2024-07-24 15:58:00 -0500585 findLimits(limits, baseConfiguration);
586
587 enableFanInput(path);
588
589 auto& tachSensor = tachSensors[sensorName];
590 tachSensor = nullptr;
591 tachSensor = std::make_shared<TachSensor>(
592 path.string(), baseType, objectServer, dbusConnection,
593 presenceGpio, redundancy, io, sensorName,
594 std::move(sensorThresholds), *interfacePath, limits, powerState,
595 led);
596 tachSensor->setupRead();
597
598 if (!pwmPath.empty() && fs::exists(pwmPath) &&
599 (pwmSensors.count(pwmPath) == 0U))
600 {
601 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
602 pwmName, pwmPath, dbusConnection, objectServer,
603 *interfacePath, "Fan", isValueMutable);
604 }
605 }
606
607 createRedundancySensor(tachSensors, dbusConnection, objectServer);
608 });
James Feistde5e9702019-09-18 16:13:02 -0700609 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700610 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
611 retries);
James Feist6714a252018-09-10 15:26:18 -0700612}
613
James Feistb6c0b912019-07-09 12:21:44 -0700614int main()
James Feist6714a252018-09-10 15:26:18 -0700615{
Ed Tanous1f978632023-02-28 18:16:39 -0800616 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700617 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700618 sdbusplus::asio::object_server objectServer(systemBus, true);
619
620 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanousd9067252022-10-22 15:35:52 -0700621 objectServer.add_manager("/xyz/openbmc_project/control");
Lei YUc2f83fe2022-12-02 14:49:47 +0800622 objectServer.add_manager("/xyz/openbmc_project/inventory");
James Feist6714a252018-09-10 15:26:18 -0700623 systemBus->request_name("xyz.openbmc_project.FanSensor");
Josh Lehan5170fe62022-08-03 13:17:41 -0700624 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700625 tachSensors;
626 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
627 pwmSensors;
Chris Cain83929002024-03-06 14:20:09 -0600628 boost::container::flat_map<std::string, std::weak_ptr<PresenceGpio>>
629 presenceGpios;
James Feist5591cf082020-07-15 16:44:54 -0700630 auto sensorsChanged =
631 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700632
Ed Tanous83db50c2023-03-01 10:20:24 -0800633 boost::asio::post(io, [&]() {
Chris Cain83929002024-03-06 14:20:09 -0600634 createSensors(io, objectServer, tachSensors, pwmSensors, presenceGpios,
635 systemBus, nullptr);
James Feist6714a252018-09-10 15:26:18 -0700636 });
637
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700638 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500639 std::function<void(sdbusplus::message_t&)> eventHandler =
640 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400641 if (message.is_method_error())
642 {
643 std::cerr << "callback method error\n";
644 return;
645 }
646 sensorsChanged->insert(message.get_path());
647 // this implicitly cancels the timer
648 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700649
Patrick Williams2aaf7172024-08-16 15:20:40 -0400650 filterTimer.async_wait([&](const boost::system::error_code& ec) {
651 if (ec == boost::asio::error::operation_aborted)
652 {
653 /* we were canceled*/
654 return;
655 }
656 if (ec)
657 {
658 std::cerr << "timer error\n";
659 return;
660 }
661 createSensors(io, objectServer, tachSensors, pwmSensors,
Chris Cain83929002024-03-06 14:20:09 -0600662 presenceGpios, systemBus, sensorsChanged, 5);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400663 });
664 };
James Feist6714a252018-09-10 15:26:18 -0700665
Zev Weiss214d9712022-08-12 12:54:31 -0700666 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
667 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700668
James Feistdc6c55f2018-10-31 12:53:20 -0700669 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500670 std::function<void(sdbusplus::message_t&)> redundancyHandler =
671 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400672 createRedundancySensor(tachSensors, systemBus, objectServer);
673 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500674 auto match = std::make_unique<sdbusplus::bus::match_t>(
675 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700676 "type='signal',member='PropertiesChanged',path_namespace='" +
677 std::string(inventoryPath) + "',arg0namespace='" +
678 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700679 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700680 matches.emplace_back(std::move(match));
681
Bruce Lee1263c3d2021-06-04 15:16:33 +0800682 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700683 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700684 return 0;
James Feist6714a252018-09-10 15:26:18 -0700685}