blob: 8db8a52075398a201b7097d5fc24f17ba73f491e [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>
George Liue34e1232025-02-20 11:27:48 +080031#include <phosphor-logging/lg2.hpp>
James Feist38fb5982020-05-28 10:09:54 -070032#include <sdbusplus/asio/connection.hpp>
33#include <sdbusplus/asio/object_server.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070034#include <sdbusplus/bus.hpp>
James Feist38fb5982020-05-28 10:09:54 -070035#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070036#include <sdbusplus/message.hpp>
James Feist38fb5982020-05-28 10:09:54 -070037
38#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070039#include <chrono>
40#include <cstddef>
41#include <cstdint>
James Feist24f02f22019-04-15 11:05:39 -070042#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070043#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070044#include <functional>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070045#include <ios>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070046#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
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000056// The following two structures need to be consistent
Chris Sidesc361e222023-04-05 15:18:20 -050057static auto sensorTypes{std::to_array<const char*>(
58 {"AspeedFan", "I2CFan", "NuvotonFan", "HPEFan"})};
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000059
60enum FanTypes
61{
62 aspeed = 0,
63 i2c,
64 nuvoton,
Chris Sidesc361e222023-04-05 15:18:20 -050065 hpe,
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000066 max,
67};
68
69static_assert(std::tuple_size<decltype(sensorTypes)>::value == FanTypes::max,
70 "sensorTypes element number is not equal to FanTypes number");
71
James Feistdc6c55f2018-10-31 12:53:20 -070072constexpr const char* redundancyConfiguration =
73 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070074static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070075
James Feistdc6c55f2018-10-31 12:53:20 -070076// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070077std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080078
Chris Sides3d5260d2023-04-10 15:45:00 -050079static const std::map<std::string, FanTypes> compatibleFanTypes = {
80 {"aspeed,ast2400-pwm-tacho", FanTypes::aspeed},
81 {"aspeed,ast2500-pwm-tacho", FanTypes::aspeed},
Potin Lai40c4d682023-05-19 17:19:03 +080082 {"aspeed,ast2600-pwm-tach", FanTypes::aspeed},
krishna raj2085a232025-11-13 16:12:46 +053083 {"aspeed,ast2700-pwm-tach", FanTypes::aspeed},
Chris Sidesc361e222023-04-05 15:18:20 -050084 {"nuvoton,npcm750-pwm-fan", FanTypes::nuvoton},
Brian Mad37e1db2023-05-08 13:36:53 +080085 {"nuvoton,npcm845-pwm-fan", FanTypes::nuvoton},
Chris Sidesc361e222023-04-05 15:18:20 -050086 {"hpe,gxp-fan-ctrl", FanTypes::hpe}
Chris Sides3d5260d2023-04-10 15:45:00 -050087 // add compatible string here for new fan type
88};
89
Ed Tanous2e466962025-01-30 10:59:49 -080090FanTypes getFanType(const std::filesystem::path& parentPath)
James Feist95b079b2018-11-21 09:28:00 -080091{
Ed Tanous2e466962025-01-30 10:59:49 -080092 std::filesystem::path linkPath = parentPath / "of_node";
93 if (!std::filesystem::exists(linkPath))
Zhikui Ren39963222023-05-04 17:06:50 -070094 {
95 return FanTypes::i2c;
96 }
Chris Sides9a472e82023-04-03 15:13:37 -050097
Ed Tanous2e466962025-01-30 10:59:49 -080098 std::string canonical = std::filesystem::canonical(linkPath);
Chris Sides9a472e82023-04-03 15:13:37 -050099 std::string compatiblePath = canonical + "/compatible";
100 std::ifstream compatibleStream(compatiblePath);
101
Chris Sides3d5260d2023-04-10 15:45:00 -0500102 if (!compatibleStream)
James Feist95b079b2018-11-21 09:28:00 -0800103 {
George Liue34e1232025-02-20 11:27:48 +0800104 lg2::error("Error opening '{PATH}'", "PATH", compatiblePath);
Chris Sides3d5260d2023-04-10 15:45:00 -0500105 return FanTypes::i2c;
James Feist95b079b2018-11-21 09:28:00 -0800106 }
Chris Sides9a472e82023-04-03 15:13:37 -0500107
108 std::string compatibleString;
Chris Sides3d5260d2023-04-10 15:45:00 -0500109 while (std::getline(compatibleStream, compatibleString))
Peter Lundgren8843b622019-09-12 10:33:41 -0700110 {
Chris Sides9a472e82023-04-03 15:13:37 -0500111 compatibleString.pop_back(); // trim EOL before comparisons
112
Chris Sides3d5260d2023-04-10 15:45:00 -0500113 std::map<std::string, FanTypes>::const_iterator compatibleIterator =
114 compatibleFanTypes.find(compatibleString);
115
116 if (compatibleIterator != compatibleFanTypes.end())
Chris Sides9a472e82023-04-03 15:13:37 -0500117 {
Chris Sides3d5260d2023-04-10 15:45:00 -0500118 return compatibleIterator->second;
Chris Sides9a472e82023-04-03 15:13:37 -0500119 }
Peter Lundgren8843b622019-09-12 10:33:41 -0700120 }
Chris Sides9a472e82023-04-03 15:13:37 -0500121
James Feist95b079b2018-11-21 09:28:00 -0800122 return FanTypes::i2c;
123}
Ed Tanous2e466962025-01-30 10:59:49 -0800124void enablePwm(const std::filesystem::path& filePath)
Jeff Linabf91de2020-12-23 10:55:42 +0800125{
126 std::fstream enableFile(filePath, std::ios::in | std::ios::out);
127 if (!enableFile.good())
128 {
George Liue34e1232025-02-20 11:27:48 +0800129 lg2::error("Error read/write '{PATH}'", "PATH", filePath);
Jeff Linabf91de2020-12-23 10:55:42 +0800130 return;
131 }
James Feistdc6c55f2018-10-31 12:53:20 -0700132
Jeff Linabf91de2020-12-23 10:55:42 +0800133 std::string regulateMode;
134 std::getline(enableFile, regulateMode);
135 if (regulateMode == "0")
136 {
137 enableFile << 1;
138 }
139}
Ed Tanous2e466962025-01-30 10:59:49 -0800140bool findPwmfanPath(unsigned int configPwmfanIndex,
141 std::filesystem::path& pwmPath)
Howard Chiuddf25d12021-12-02 15:14:44 +0800142{
143 /* Search PWM since pwm-fan had separated
144 * PWM from tach directory and 1 channel only*/
Ed Tanous2e466962025-01-30 10:59:49 -0800145 std::vector<std::filesystem::path> pwmfanPaths;
Howard Chiuddf25d12021-12-02 15:14:44 +0800146 std::string pwnfanDevName("pwm-fan");
147
148 pwnfanDevName += std::to_string(configPwmfanIndex);
149
Ed Tanous2e466962025-01-30 10:59:49 -0800150 if (!findFiles(std::filesystem::path("/sys/class/hwmon"), R"(pwm\d+)",
151 pwmfanPaths))
Howard Chiuddf25d12021-12-02 15:14:44 +0800152 {
George Liue34e1232025-02-20 11:27:48 +0800153 lg2::error("No PWMs are found!");
Howard Chiuddf25d12021-12-02 15:14:44 +0800154 return false;
155 }
156 for (const auto& path : pwmfanPaths)
157 {
158 std::error_code ec;
Ed Tanous2e466962025-01-30 10:59:49 -0800159 std::filesystem::path link =
160 std::filesystem::read_symlink(path.parent_path() / "device", ec);
Howard Chiuddf25d12021-12-02 15:14:44 +0800161
162 if (ec)
163 {
George Liue34e1232025-02-20 11:27:48 +0800164 lg2::error("read_symlink() failed: '{ERROR_MESSAGE}'",
165 "ERROR_MESSAGE", ec.message());
Howard Chiuddf25d12021-12-02 15:14:44 +0800166 continue;
167 }
168
169 if (link.filename().string() == pwnfanDevName)
170 {
171 pwmPath = path;
172 return true;
173 }
174 }
175 return false;
176}
Ed Tanous2e466962025-01-30 10:59:49 -0800177bool findPwmPath(const std::filesystem::path& directory, unsigned int pwm,
178 std::filesystem::path& pwmPath)
Howard Chiuddf25d12021-12-02 15:14:44 +0800179{
180 std::error_code ec;
181
182 /* Assuming PWM file is appeared in the same directory as fanX_input */
183 auto path = directory / ("pwm" + std::to_string(pwm + 1));
Ed Tanous2e466962025-01-30 10:59:49 -0800184 bool exists = std::filesystem::exists(path, ec);
Howard Chiuddf25d12021-12-02 15:14:44 +0800185
186 if (ec || !exists)
187 {
188 /* PWM file not exist or error happened */
189 if (ec)
190 {
George Liue34e1232025-02-20 11:27:48 +0800191 lg2::error("exists() failed: '{ERROR_MESSAGE}'", "ERROR_MESSAGE",
192 ec.message());
Howard Chiuddf25d12021-12-02 15:14:44 +0800193 }
194 /* try search form pwm-fanX directory */
195 return findPwmfanPath(pwm, pwmPath);
196 }
197
198 pwmPath = path;
199 return true;
200}
Justin Ledford9c47bd72022-08-27 01:02:09 +0000201
202// The argument to this function should be the fanN_input file that we want to
203// enable. The function will locate the corresponding fanN_enable file if it
204// exists. Note that some drivers don't provide this file if the sensors are
205// always enabled.
Ed Tanous2e466962025-01-30 10:59:49 -0800206void enableFanInput(const std::filesystem::path& fanInputPath)
Justin Ledford9c47bd72022-08-27 01:02:09 +0000207{
208 std::error_code ec;
209 std::string path(fanInputPath.string());
210 boost::replace_last(path, "input", "enable");
211
Ed Tanous2e466962025-01-30 10:59:49 -0800212 bool exists = std::filesystem::exists(path, ec);
Justin Ledford9c47bd72022-08-27 01:02:09 +0000213 if (ec || !exists)
214 {
215 return;
216 }
217
218 std::fstream enableFile(path, std::ios::out);
219 if (!enableFile.good())
220 {
221 return;
222 }
223 enableFile << 1;
224}
225
Kuiying Wangd5407412020-09-09 16:06:56 +0800226void createRedundancySensor(
Josh Lehan5170fe62022-08-03 13:17:41 -0700227 const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
Kuiying Wangd5407412020-09-09 16:06:56 +0800228 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700229 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800230 sdbusplus::asio::object_server& objectServer)
231{
Kuiying Wangd5407412020-09-09 16:06:56 +0800232 conn->async_method_call(
233 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700234 const ManagedObjectType& managedObj) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400235 if (ec)
Kuiying Wangd5407412020-09-09 16:06:56 +0800236 {
George Liue34e1232025-02-20 11:27:48 +0800237 lg2::error("Error calling entity manager");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400238 return;
239 }
240 for (const auto& [path, interfaces] : managedObj)
241 {
242 for (const auto& [intf, cfg] : interfaces)
Kuiying Wangd5407412020-09-09 16:06:56 +0800243 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400244 if (intf == redundancyConfiguration)
Kuiying Wangd5407412020-09-09 16:06:56 +0800245 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400246 // currently only support one
247 auto findCount = cfg.find("AllowedFailures");
248 if (findCount == cfg.end())
249 {
George Liue34e1232025-02-20 11:27:48 +0800250 lg2::error("Malformed redundancy record");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400251 return;
252 }
253 std::vector<std::string> sensorList;
254
255 for (const auto& [name, sensor] : sensors)
256 {
257 sensorList.push_back(
258 "/xyz/openbmc_project/sensors/fan_tach/" +
259 sensor->name);
260 }
261 systemRedundancy.reset();
262 systemRedundancy.emplace(RedundancySensor(
263 std::get<uint64_t>(findCount->second), sensorList,
264 objectServer, path));
265
Kuiying Wangd5407412020-09-09 16:06:56 +0800266 return;
267 }
268 }
269 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400270 },
Nan Zhou3e620af2022-09-20 22:28:31 +0000271 "xyz.openbmc_project.EntityManager", "/xyz/openbmc_project/inventory",
Kuiying Wangd5407412020-09-09 16:06:56 +0800272 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
273}
274
James Feist6714a252018-09-10 15:26:18 -0700275void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800276 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Josh Lehan5170fe62022-08-03 13:17:41 -0700277 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700278 tachSensors,
279 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
280 pwmSensors,
Chris Cain83929002024-03-06 14:20:09 -0600281 boost::container::flat_map<std::string, std::weak_ptr<PresenceGpio>>&
282 presenceGpios,
James Feist6714a252018-09-10 15:26:18 -0700283 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700284 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700285 sensorsChanged,
286 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700287{
Chris Cainc45e18f2024-07-24 15:58:00 -0500288 auto getter = std::make_shared<
289 GetSensorConfiguration>(dbusConnection, [&io, &objectServer,
290 &tachSensors, &pwmSensors,
291 &presenceGpios,
292 &dbusConnection,
293 sensorsChanged](
294 const ManagedObjectType&
295 sensorConfigurations) {
296 bool firstScan = sensorsChanged == nullptr;
Ed Tanous2e466962025-01-30 10:59:49 -0800297 std::vector<std::filesystem::path> paths;
298 if (!findFiles(std::filesystem::path("/sys/class/hwmon"),
299 R"(fan\d+_input)", paths))
Chris Cainc45e18f2024-07-24 15:58:00 -0500300 {
George Liue34e1232025-02-20 11:27:48 +0800301 lg2::error("No fan sensors in system");
Chris Cainc45e18f2024-07-24 15:58:00 -0500302 return;
303 }
304
305 // iterate through all found fan sensors, and try to match them with
306 // configuration
307 for (const auto& path : paths)
308 {
309 std::smatch match;
310 std::string pathStr = path.string();
311
312 std::regex_search(pathStr, match, inputRegex);
313 std::string indexStr = *(match.begin() + 1);
314
Ed Tanous2e466962025-01-30 10:59:49 -0800315 std::filesystem::path directory = path.parent_path();
Chris Cainc45e18f2024-07-24 15:58:00 -0500316 FanTypes fanType = getFanType(directory);
317 std::string cfgIntf = configInterfaceName(sensorTypes[fanType]);
318
319 // convert to 0 based
320 size_t index = std::stoul(indexStr) - 1;
321
322 const char* baseType = nullptr;
323 const SensorData* sensorData = nullptr;
324 const std::string* interfacePath = nullptr;
325 const SensorBaseConfiguration* baseConfiguration = nullptr;
326 for (const auto& [path, cfgData] : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800327 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500328 // find the base of the configuration to see if indexes
329 // match
330 auto sensorBaseFind = cfgData.find(cfgIntf);
331 if (sensorBaseFind == cfgData.end())
Patrick Williams2aaf7172024-08-16 15:20:40 -0400332 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500333 continue;
334 }
335
336 baseConfiguration = &(*sensorBaseFind);
337 interfacePath = &path.str;
338 baseType = sensorTypes[fanType];
339
340 auto findIndex = baseConfiguration->second.find("Index");
341 if (findIndex == baseConfiguration->second.end())
342 {
George Liue34e1232025-02-20 11:27:48 +0800343 lg2::error("'{INTERFACE}' missing index", "INTERFACE",
344 baseConfiguration->first);
Chris Cainc45e18f2024-07-24 15:58:00 -0500345 continue;
346 }
347 unsigned int configIndex = std::visit(
348 VariantToUnsignedIntVisitor(), findIndex->second);
349 if (configIndex != index)
350 {
351 continue;
352 }
353 if (fanType == FanTypes::aspeed ||
354 fanType == FanTypes::nuvoton || fanType == FanTypes::hpe)
355 {
356 // there will be only 1 aspeed or nuvoton or hpe sensor
357 // object in sysfs, we found the fan
358 sensorData = &cfgData;
359 break;
360 }
361 if (fanType == FanTypes::i2c)
362 {
363 std::string deviceName =
Ed Tanous2e466962025-01-30 10:59:49 -0800364 std::filesystem::read_symlink(directory / "device")
365 .filename();
Chris Cainc45e18f2024-07-24 15:58:00 -0500366
367 size_t bus = 0;
368 size_t addr = 0;
369 if (!getDeviceBusAddr(deviceName, bus, addr))
James Feistde5e9702019-09-18 16:13:02 -0700370 {
Akshit Shah03d333e2023-08-23 22:14:28 +0000371 continue;
James Feistde5e9702019-09-18 16:13:02 -0700372 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800373
Chris Cainc45e18f2024-07-24 15:58:00 -0500374 auto findBus = baseConfiguration->second.find("Bus");
375 auto findAddress =
376 baseConfiguration->second.find("Address");
377 if (findBus == baseConfiguration->second.end() ||
378 findAddress == baseConfiguration->second.end())
379 {
George Liue34e1232025-02-20 11:27:48 +0800380 lg2::error("'{INTERFACE}' missing bus or address",
381 "INTERFACE", baseConfiguration->first);
Chris Cainc45e18f2024-07-24 15:58:00 -0500382 continue;
383 }
384 unsigned int configBus = std::visit(
385 VariantToUnsignedIntVisitor(), findBus->second);
386 unsigned int configAddress = std::visit(
387 VariantToUnsignedIntVisitor(), findAddress->second);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400388
Chris Cainc45e18f2024-07-24 15:58:00 -0500389 if (configBus == bus && configAddress == addr)
James Feistde5e9702019-09-18 16:13:02 -0700390 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700391 sensorData = &cfgData;
James Feistde5e9702019-09-18 16:13:02 -0700392 break;
393 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500394 }
395 }
396 if (sensorData == nullptr)
397 {
George Liue34e1232025-02-20 11:27:48 +0800398 lg2::error("failed to find match for '{PATH}'", "PATH",
399 path.string());
Chris Cainc45e18f2024-07-24 15:58:00 -0500400 continue;
401 }
402
403 auto findSensorName = baseConfiguration->second.find("Name");
404
405 if (findSensorName == baseConfiguration->second.end())
406 {
George Liue34e1232025-02-20 11:27:48 +0800407 lg2::error(
408 "could not determine configuration name for '{PATH}'",
409 "PATH", path.string());
Chris Cainc45e18f2024-07-24 15:58:00 -0500410 continue;
411 }
412 std::string sensorName =
413 std::get<std::string>(findSensorName->second);
414
415 // on rescans, only update sensors we were signaled by
416 auto findSensor = tachSensors.find(sensorName);
417 if (!firstScan && findSensor != tachSensors.end())
418 {
419 bool found = false;
420 for (auto it = sensorsChanged->begin();
421 it != sensorsChanged->end(); it++)
422 {
423 if (it->ends_with(findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700424 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500425 sensorsChanged->erase(it);
426 findSensor->second = nullptr;
427 found = true;
428 break;
James Feistde5e9702019-09-18 16:13:02 -0700429 }
430 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500431 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700432 {
James Feist95b079b2018-11-21 09:28:00 -0800433 continue;
434 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500435 }
436 std::vector<thresholds::Threshold> sensorThresholds;
437 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
438 {
George Liue34e1232025-02-20 11:27:48 +0800439 lg2::error("error populating thresholds for '{NAME}'", "NAME",
440 sensorName);
Chris Cainc45e18f2024-07-24 15:58:00 -0500441 }
James Feist95b079b2018-11-21 09:28:00 -0800442
Chris Cainc45e18f2024-07-24 15:58:00 -0500443 auto presenceConfig =
444 sensorData->find(cfgIntf + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800445
Chris Cainc45e18f2024-07-24 15:58:00 -0500446 std::shared_ptr<PresenceGpio> presenceGpio(nullptr);
447
448 // presence sensors are optional
449 if (presenceConfig != sensorData->end())
450 {
451 auto findPolarity = presenceConfig->second.find("Polarity");
452 auto findPinName = presenceConfig->second.find("PinName");
453
454 if (findPinName == presenceConfig->second.end() ||
455 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800456 {
George Liue34e1232025-02-20 11:27:48 +0800457 lg2::error("Malformed Presence Configuration");
James Feistde5e9702019-09-18 16:13:02 -0700458 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500459 else
Patrick Williams2aaf7172024-08-16 15:20:40 -0400460 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500461 bool inverted =
462 std::get<std::string>(findPolarity->second) == "Low";
463 const auto* pinName =
464 std::get_if<std::string>(&findPinName->second);
465
466 if (pinName != nullptr)
James Feistde5e9702019-09-18 16:13:02 -0700467 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500468 auto findPresenceGpio = presenceGpios.find(*pinName);
469 if (findPresenceGpio != presenceGpios.end())
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +0200470 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500471 auto p = findPresenceGpio->second.lock();
472 if (p)
Patrick Williams2aaf7172024-08-16 15:20:40 -0400473 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500474 presenceGpio = p;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400475 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500476 }
477 if (!presenceGpio)
478 {
Xiaochao Ma4bbd02d2022-06-21 16:54:27 +0800479 auto findMonitorType =
480 presenceConfig->second.find("MonitorType");
481 bool polling = false;
482 if (findMonitorType != presenceConfig->second.end())
483 {
484 auto mType = std::get<std::string>(
485 findMonitorType->second);
486 if (mType == "Polling")
487 {
488 polling = true;
489 }
490 else if (mType != "Event")
491 {
George Liue34e1232025-02-20 11:27:48 +0800492 lg2::error(
493 "Unsupported GPIO MonitorType of '{TYPE}' for '{NAME}', "
494 "supported types: Polling, Event default",
495 "TYPE", mType, "NAME", sensorName);
Xiaochao Ma4bbd02d2022-06-21 16:54:27 +0800496 }
497 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500498 try
Patrick Williams2aaf7172024-08-16 15:20:40 -0400499 {
Xiaochao Ma4bbd02d2022-06-21 16:54:27 +0800500 if (polling)
501 {
502 presenceGpio =
503 std::make_shared<PollingPresenceGpio>(
504 "Fan", sensorName, *pinName,
505 inverted, io);
506 }
507 else
508 {
509 presenceGpio =
510 std::make_shared<EventPresenceGpio>(
511 "Fan", sensorName, *pinName,
512 inverted, io);
513 }
Chris Cain83929002024-03-06 14:20:09 -0600514 presenceGpios[*pinName] = presenceGpio;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400515 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500516 catch (const std::system_error& e)
Patrick Williams2aaf7172024-08-16 15:20:40 -0400517 {
George Liue34e1232025-02-20 11:27:48 +0800518 lg2::error(
519 "Failed to create GPIO monitor object for "
520 "'{PIN_NAME}' / '{SENSOR_NAME}': '{ERROR}'",
521 "PIN_NAME", *pinName, "SENSOR_NAME",
522 sensorName, "ERROR", e);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400523 }
524 }
Ed Tanousbb679322022-05-16 16:10:00 -0700525 }
526 else
527 {
George Liue34e1232025-02-20 11:27:48 +0800528 lg2::error(
529 "Malformed Presence pinName for sensor '{NAME}'",
530 "NAME", sensorName);
Chris Cainc45e18f2024-07-24 15:58:00 -0500531 }
532 }
533 }
534 std::optional<RedundancySensor>* redundancy = nullptr;
535 if (fanType == FanTypes::aspeed)
536 {
537 redundancy = &systemRedundancy;
538 }
539
540 PowerState powerState = getPowerState(baseConfiguration->second);
541
542 constexpr double defaultMaxReading = 25000;
543 constexpr double defaultMinReading = 0;
544 std::pair<double, double> limits =
545 std::make_pair(defaultMinReading, defaultMaxReading);
546
547 auto connector =
548 sensorData->find(cfgIntf + std::string(".Connector"));
549
550 std::optional<std::string> led;
551 std::string pwmName;
Ed Tanous2e466962025-01-30 10:59:49 -0800552 std::filesystem::path pwmPath;
Chris Cainc45e18f2024-07-24 15:58:00 -0500553
554 // The Mutable parameter is optional, defaulting to false
555 bool isValueMutable = false;
556 if (connector != sensorData->end())
557 {
558 auto findPwm = connector->second.find("Pwm");
559 if (findPwm != connector->second.end())
560 {
561 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
562 findPwm->second);
563 if (!findPwmPath(directory, pwm, pwmPath))
564 {
George Liue34e1232025-02-20 11:27:48 +0800565 lg2::error(
566 "Connector for '{NAME}' no pwm channel found!",
567 "NAME", sensorName);
Chris Cainc45e18f2024-07-24 15:58:00 -0500568 continue;
Ed Tanousbb679322022-05-16 16:10:00 -0700569 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400570
Ed Tanous2e466962025-01-30 10:59:49 -0800571 std::filesystem::path pwmEnableFile =
Chris Cainc45e18f2024-07-24 15:58:00 -0500572 "pwm" + std::to_string(pwm + 1) + "_enable";
Ed Tanous2e466962025-01-30 10:59:49 -0800573 std::filesystem::path enablePath =
574 pwmPath.parent_path() / pwmEnableFile;
Chris Cainc45e18f2024-07-24 15:58:00 -0500575 enablePwm(enablePath);
576
577 /* use pwm name override if found in configuration else
578 * use default */
579 auto findOverride = connector->second.find("PwmName");
580 if (findOverride != connector->second.end())
Patrick Williams2aaf7172024-08-16 15:20:40 -0400581 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500582 pwmName = std::visit(VariantToStringVisitor(),
583 findOverride->second);
584 }
585 else
586 {
587 pwmName = "Pwm_" + std::to_string(pwm + 1);
588 }
589
590 // Check PWM sensor mutability
591 auto findMutable = connector->second.find("Mutable");
592 if (findMutable != connector->second.end())
593 {
594 const auto* ptrMutable =
595 std::get_if<bool>(&(findMutable->second));
596 if (ptrMutable != nullptr)
Patrick Williams2aaf7172024-08-16 15:20:40 -0400597 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500598 isValueMutable = *ptrMutable;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400599 }
600 }
601 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500602 else
Patrick Williams2aaf7172024-08-16 15:20:40 -0400603 {
George Liue34e1232025-02-20 11:27:48 +0800604 lg2::error("Connector for '{NAME}' missing pwm!", "NAME",
605 sensorName);
Chris Cainc45e18f2024-07-24 15:58:00 -0500606 }
607
608 auto findLED = connector->second.find("LED");
609 if (findLED != connector->second.end())
610 {
611 const auto* ledName =
612 std::get_if<std::string>(&(findLED->second));
613 if (ledName == nullptr)
614 {
George Liue34e1232025-02-20 11:27:48 +0800615 lg2::error("Wrong format for LED of '{NAME}'", "NAME",
616 sensorName);
Chris Cainc45e18f2024-07-24 15:58:00 -0500617 }
618 else
619 {
620 led = *ledName;
621 }
Ed Tanousbb679322022-05-16 16:10:00 -0700622 }
623 }
624
Chris Cainc45e18f2024-07-24 15:58:00 -0500625 findLimits(limits, baseConfiguration);
626
627 enableFanInput(path);
628
629 auto& tachSensor = tachSensors[sensorName];
630 tachSensor = nullptr;
631 tachSensor = std::make_shared<TachSensor>(
632 path.string(), baseType, objectServer, dbusConnection,
633 presenceGpio, redundancy, io, sensorName,
634 std::move(sensorThresholds), *interfacePath, limits, powerState,
635 led);
636 tachSensor->setupRead();
637
Ed Tanous2e466962025-01-30 10:59:49 -0800638 if (!pwmPath.empty() && std::filesystem::exists(pwmPath) &&
Chris Cainc45e18f2024-07-24 15:58:00 -0500639 (pwmSensors.count(pwmPath) == 0U))
640 {
641 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
642 pwmName, pwmPath, dbusConnection, objectServer,
643 *interfacePath, "Fan", isValueMutable);
644 }
645 }
646
647 createRedundancySensor(tachSensors, dbusConnection, objectServer);
648 });
James Feistde5e9702019-09-18 16:13:02 -0700649 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700650 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
651 retries);
James Feist6714a252018-09-10 15:26:18 -0700652}
653
James Feistb6c0b912019-07-09 12:21:44 -0700654int main()
James Feist6714a252018-09-10 15:26:18 -0700655{
Ed Tanous1f978632023-02-28 18:16:39 -0800656 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700657 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700658 sdbusplus::asio::object_server objectServer(systemBus, true);
659
660 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanousd9067252022-10-22 15:35:52 -0700661 objectServer.add_manager("/xyz/openbmc_project/control");
Lei YUc2f83fe2022-12-02 14:49:47 +0800662 objectServer.add_manager("/xyz/openbmc_project/inventory");
James Feist6714a252018-09-10 15:26:18 -0700663 systemBus->request_name("xyz.openbmc_project.FanSensor");
Josh Lehan5170fe62022-08-03 13:17:41 -0700664 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700665 tachSensors;
666 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
667 pwmSensors;
Chris Cain83929002024-03-06 14:20:09 -0600668 boost::container::flat_map<std::string, std::weak_ptr<PresenceGpio>>
669 presenceGpios;
James Feist5591cf082020-07-15 16:44:54 -0700670 auto sensorsChanged =
671 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700672
Ed Tanous83db50c2023-03-01 10:20:24 -0800673 boost::asio::post(io, [&]() {
Chris Cain83929002024-03-06 14:20:09 -0600674 createSensors(io, objectServer, tachSensors, pwmSensors, presenceGpios,
675 systemBus, nullptr);
James Feist6714a252018-09-10 15:26:18 -0700676 });
677
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700678 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500679 std::function<void(sdbusplus::message_t&)> eventHandler =
680 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400681 sensorsChanged->insert(message.get_path());
682 // this implicitly cancels the timer
683 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700684
Patrick Williams2aaf7172024-08-16 15:20:40 -0400685 filterTimer.async_wait([&](const boost::system::error_code& ec) {
686 if (ec == boost::asio::error::operation_aborted)
687 {
688 /* we were canceled*/
689 return;
690 }
691 if (ec)
692 {
George Liue34e1232025-02-20 11:27:48 +0800693 lg2::error("timer error");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400694 return;
695 }
696 createSensors(io, objectServer, tachSensors, pwmSensors,
Chris Cain83929002024-03-06 14:20:09 -0600697 presenceGpios, systemBus, sensorsChanged, 5);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400698 });
699 };
James Feist6714a252018-09-10 15:26:18 -0700700
Zev Weiss214d9712022-08-12 12:54:31 -0700701 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
702 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700703
James Feistdc6c55f2018-10-31 12:53:20 -0700704 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500705 std::function<void(sdbusplus::message_t&)> redundancyHandler =
706 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400707 createRedundancySensor(tachSensors, systemBus, objectServer);
708 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500709 auto match = std::make_unique<sdbusplus::bus::match_t>(
710 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700711 "type='signal',member='PropertiesChanged',path_namespace='" +
712 std::string(inventoryPath) + "',arg0namespace='" +
713 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700714 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700715 matches.emplace_back(std::move(match));
716
Bruce Lee1263c3d2021-06-04 15:16:33 +0800717 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700718 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700719 return 0;
James Feist6714a252018-09-10 15:26:18 -0700720}