blob: cbd517346acb5915dac35b04eb06d4b3ae4051a6 [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
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},
Chris Sidesc361e222023-04-05 15:18:20 -050083 {"nuvoton,npcm750-pwm-fan", FanTypes::nuvoton},
Brian Mad37e1db2023-05-08 13:36:53 +080084 {"nuvoton,npcm845-pwm-fan", FanTypes::nuvoton},
Chris Sidesc361e222023-04-05 15:18:20 -050085 {"hpe,gxp-fan-ctrl", FanTypes::hpe}
Chris Sides3d5260d2023-04-10 15:45:00 -050086 // add compatible string here for new fan type
87};
88
Ed Tanous2e466962025-01-30 10:59:49 -080089FanTypes getFanType(const std::filesystem::path& parentPath)
James Feist95b079b2018-11-21 09:28:00 -080090{
Ed Tanous2e466962025-01-30 10:59:49 -080091 std::filesystem::path linkPath = parentPath / "of_node";
92 if (!std::filesystem::exists(linkPath))
Zhikui Ren39963222023-05-04 17:06:50 -070093 {
94 return FanTypes::i2c;
95 }
Chris Sides9a472e82023-04-03 15:13:37 -050096
Ed Tanous2e466962025-01-30 10:59:49 -080097 std::string canonical = std::filesystem::canonical(linkPath);
Chris Sides9a472e82023-04-03 15:13:37 -050098 std::string compatiblePath = canonical + "/compatible";
99 std::ifstream compatibleStream(compatiblePath);
100
Chris Sides3d5260d2023-04-10 15:45:00 -0500101 if (!compatibleStream)
James Feist95b079b2018-11-21 09:28:00 -0800102 {
Chris Sides9a472e82023-04-03 15:13:37 -0500103 std::cerr << "Error opening " << compatiblePath << "\n";
Chris Sides3d5260d2023-04-10 15:45:00 -0500104 return FanTypes::i2c;
James Feist95b079b2018-11-21 09:28:00 -0800105 }
Chris Sides9a472e82023-04-03 15:13:37 -0500106
107 std::string compatibleString;
Chris Sides3d5260d2023-04-10 15:45:00 -0500108 while (std::getline(compatibleStream, compatibleString))
Peter Lundgren8843b622019-09-12 10:33:41 -0700109 {
Chris Sides9a472e82023-04-03 15:13:37 -0500110 compatibleString.pop_back(); // trim EOL before comparisons
111
Chris Sides3d5260d2023-04-10 15:45:00 -0500112 std::map<std::string, FanTypes>::const_iterator compatibleIterator =
113 compatibleFanTypes.find(compatibleString);
114
115 if (compatibleIterator != compatibleFanTypes.end())
Chris Sides9a472e82023-04-03 15:13:37 -0500116 {
Chris Sides3d5260d2023-04-10 15:45:00 -0500117 return compatibleIterator->second;
Chris Sides9a472e82023-04-03 15:13:37 -0500118 }
Peter Lundgren8843b622019-09-12 10:33:41 -0700119 }
Chris Sides9a472e82023-04-03 15:13:37 -0500120
James Feist95b079b2018-11-21 09:28:00 -0800121 return FanTypes::i2c;
122}
Ed Tanous2e466962025-01-30 10:59:49 -0800123void enablePwm(const std::filesystem::path& filePath)
Jeff Linabf91de2020-12-23 10:55:42 +0800124{
125 std::fstream enableFile(filePath, std::ios::in | std::ios::out);
126 if (!enableFile.good())
127 {
128 std::cerr << "Error read/write " << filePath << "\n";
129 return;
130 }
James Feistdc6c55f2018-10-31 12:53:20 -0700131
Jeff Linabf91de2020-12-23 10:55:42 +0800132 std::string regulateMode;
133 std::getline(enableFile, regulateMode);
134 if (regulateMode == "0")
135 {
136 enableFile << 1;
137 }
138}
Ed Tanous2e466962025-01-30 10:59:49 -0800139bool findPwmfanPath(unsigned int configPwmfanIndex,
140 std::filesystem::path& pwmPath)
Howard Chiuddf25d12021-12-02 15:14:44 +0800141{
142 /* Search PWM since pwm-fan had separated
143 * PWM from tach directory and 1 channel only*/
Ed Tanous2e466962025-01-30 10:59:49 -0800144 std::vector<std::filesystem::path> pwmfanPaths;
Howard Chiuddf25d12021-12-02 15:14:44 +0800145 std::string pwnfanDevName("pwm-fan");
146
147 pwnfanDevName += std::to_string(configPwmfanIndex);
148
Ed Tanous2e466962025-01-30 10:59:49 -0800149 if (!findFiles(std::filesystem::path("/sys/class/hwmon"), R"(pwm\d+)",
150 pwmfanPaths))
Howard Chiuddf25d12021-12-02 15:14:44 +0800151 {
152 std::cerr << "No PWMs are found!\n";
153 return false;
154 }
155 for (const auto& path : pwmfanPaths)
156 {
157 std::error_code ec;
Ed Tanous2e466962025-01-30 10:59:49 -0800158 std::filesystem::path link =
159 std::filesystem::read_symlink(path.parent_path() / "device", ec);
Howard Chiuddf25d12021-12-02 15:14:44 +0800160
161 if (ec)
162 {
163 std::cerr << "read_symlink() failed: " << ec.message() << " ("
164 << ec.value() << ")\n";
165 continue;
166 }
167
168 if (link.filename().string() == pwnfanDevName)
169 {
170 pwmPath = path;
171 return true;
172 }
173 }
174 return false;
175}
Ed Tanous2e466962025-01-30 10:59:49 -0800176bool findPwmPath(const std::filesystem::path& directory, unsigned int pwm,
177 std::filesystem::path& pwmPath)
Howard Chiuddf25d12021-12-02 15:14:44 +0800178{
179 std::error_code ec;
180
181 /* Assuming PWM file is appeared in the same directory as fanX_input */
182 auto path = directory / ("pwm" + std::to_string(pwm + 1));
Ed Tanous2e466962025-01-30 10:59:49 -0800183 bool exists = std::filesystem::exists(path, ec);
Howard Chiuddf25d12021-12-02 15:14:44 +0800184
185 if (ec || !exists)
186 {
187 /* PWM file not exist or error happened */
188 if (ec)
189 {
190 std::cerr << "exists() failed: " << ec.message() << " ("
191 << ec.value() << ")\n";
192 }
193 /* try search form pwm-fanX directory */
194 return findPwmfanPath(pwm, pwmPath);
195 }
196
197 pwmPath = path;
198 return true;
199}
Justin Ledford9c47bd72022-08-27 01:02:09 +0000200
201// The argument to this function should be the fanN_input file that we want to
202// enable. The function will locate the corresponding fanN_enable file if it
203// exists. Note that some drivers don't provide this file if the sensors are
204// always enabled.
Ed Tanous2e466962025-01-30 10:59:49 -0800205void enableFanInput(const std::filesystem::path& fanInputPath)
Justin Ledford9c47bd72022-08-27 01:02:09 +0000206{
207 std::error_code ec;
208 std::string path(fanInputPath.string());
209 boost::replace_last(path, "input", "enable");
210
Ed Tanous2e466962025-01-30 10:59:49 -0800211 bool exists = std::filesystem::exists(path, ec);
Justin Ledford9c47bd72022-08-27 01:02:09 +0000212 if (ec || !exists)
213 {
214 return;
215 }
216
217 std::fstream enableFile(path, std::ios::out);
218 if (!enableFile.good())
219 {
220 return;
221 }
222 enableFile << 1;
223}
224
Kuiying Wangd5407412020-09-09 16:06:56 +0800225void createRedundancySensor(
Josh Lehan5170fe62022-08-03 13:17:41 -0700226 const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
Kuiying Wangd5407412020-09-09 16:06:56 +0800227 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700228 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800229 sdbusplus::asio::object_server& objectServer)
230{
Kuiying Wangd5407412020-09-09 16:06:56 +0800231 conn->async_method_call(
232 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700233 const ManagedObjectType& managedObj) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400234 if (ec)
Kuiying Wangd5407412020-09-09 16:06:56 +0800235 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400236 std::cerr << "Error calling entity manager \n";
237 return;
238 }
239 for (const auto& [path, interfaces] : managedObj)
240 {
241 for (const auto& [intf, cfg] : interfaces)
Kuiying Wangd5407412020-09-09 16:06:56 +0800242 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400243 if (intf == redundancyConfiguration)
Kuiying Wangd5407412020-09-09 16:06:56 +0800244 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400245 // currently only support one
246 auto findCount = cfg.find("AllowedFailures");
247 if (findCount == cfg.end())
248 {
249 std::cerr << "Malformed redundancy record \n";
250 return;
251 }
252 std::vector<std::string> sensorList;
253
254 for (const auto& [name, sensor] : sensors)
255 {
256 sensorList.push_back(
257 "/xyz/openbmc_project/sensors/fan_tach/" +
258 sensor->name);
259 }
260 systemRedundancy.reset();
261 systemRedundancy.emplace(RedundancySensor(
262 std::get<uint64_t>(findCount->second), sensorList,
263 objectServer, path));
264
Kuiying Wangd5407412020-09-09 16:06:56 +0800265 return;
266 }
267 }
268 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400269 },
Nan Zhou3e620af2022-09-20 22:28:31 +0000270 "xyz.openbmc_project.EntityManager", "/xyz/openbmc_project/inventory",
Kuiying Wangd5407412020-09-09 16:06:56 +0800271 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
272}
273
James Feist6714a252018-09-10 15:26:18 -0700274void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800275 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Josh Lehan5170fe62022-08-03 13:17:41 -0700276 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700277 tachSensors,
278 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
279 pwmSensors,
Chris Cain83929002024-03-06 14:20:09 -0600280 boost::container::flat_map<std::string, std::weak_ptr<PresenceGpio>>&
281 presenceGpios,
James Feist6714a252018-09-10 15:26:18 -0700282 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700283 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700284 sensorsChanged,
285 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700286{
Chris Cainc45e18f2024-07-24 15:58:00 -0500287 auto getter = std::make_shared<
288 GetSensorConfiguration>(dbusConnection, [&io, &objectServer,
289 &tachSensors, &pwmSensors,
290 &presenceGpios,
291 &dbusConnection,
292 sensorsChanged](
293 const ManagedObjectType&
294 sensorConfigurations) {
295 bool firstScan = sensorsChanged == nullptr;
Ed Tanous2e466962025-01-30 10:59:49 -0800296 std::vector<std::filesystem::path> paths;
297 if (!findFiles(std::filesystem::path("/sys/class/hwmon"),
298 R"(fan\d+_input)", paths))
Chris Cainc45e18f2024-07-24 15:58:00 -0500299 {
300 std::cerr << "No fan sensors in system\n";
301 return;
302 }
303
304 // iterate through all found fan sensors, and try to match them with
305 // configuration
306 for (const auto& path : paths)
307 {
308 std::smatch match;
309 std::string pathStr = path.string();
310
311 std::regex_search(pathStr, match, inputRegex);
312 std::string indexStr = *(match.begin() + 1);
313
Ed Tanous2e466962025-01-30 10:59:49 -0800314 std::filesystem::path directory = path.parent_path();
Chris Cainc45e18f2024-07-24 15:58:00 -0500315 FanTypes fanType = getFanType(directory);
316 std::string cfgIntf = configInterfaceName(sensorTypes[fanType]);
317
318 // convert to 0 based
319 size_t index = std::stoul(indexStr) - 1;
320
321 const char* baseType = nullptr;
322 const SensorData* sensorData = nullptr;
323 const std::string* interfacePath = nullptr;
324 const SensorBaseConfiguration* baseConfiguration = nullptr;
325 for (const auto& [path, cfgData] : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800326 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500327 // find the base of the configuration to see if indexes
328 // match
329 auto sensorBaseFind = cfgData.find(cfgIntf);
330 if (sensorBaseFind == cfgData.end())
Patrick Williams2aaf7172024-08-16 15:20:40 -0400331 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500332 continue;
333 }
334
335 baseConfiguration = &(*sensorBaseFind);
336 interfacePath = &path.str;
337 baseType = sensorTypes[fanType];
338
339 auto findIndex = baseConfiguration->second.find("Index");
340 if (findIndex == baseConfiguration->second.end())
341 {
342 std::cerr << baseConfiguration->first << " missing index\n";
343 continue;
344 }
345 unsigned int configIndex = std::visit(
346 VariantToUnsignedIntVisitor(), findIndex->second);
347 if (configIndex != index)
348 {
349 continue;
350 }
351 if (fanType == FanTypes::aspeed ||
352 fanType == FanTypes::nuvoton || fanType == FanTypes::hpe)
353 {
354 // there will be only 1 aspeed or nuvoton or hpe sensor
355 // object in sysfs, we found the fan
356 sensorData = &cfgData;
357 break;
358 }
359 if (fanType == FanTypes::i2c)
360 {
361 std::string deviceName =
Ed Tanous2e466962025-01-30 10:59:49 -0800362 std::filesystem::read_symlink(directory / "device")
363 .filename();
Chris Cainc45e18f2024-07-24 15:58:00 -0500364
365 size_t bus = 0;
366 size_t addr = 0;
367 if (!getDeviceBusAddr(deviceName, bus, addr))
James Feistde5e9702019-09-18 16:13:02 -0700368 {
Akshit Shah03d333e2023-08-23 22:14:28 +0000369 continue;
James Feistde5e9702019-09-18 16:13:02 -0700370 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800371
Chris Cainc45e18f2024-07-24 15:58:00 -0500372 auto findBus = baseConfiguration->second.find("Bus");
373 auto findAddress =
374 baseConfiguration->second.find("Address");
375 if (findBus == baseConfiguration->second.end() ||
376 findAddress == baseConfiguration->second.end())
377 {
378 std::cerr << baseConfiguration->first
379 << " missing bus or address\n";
380 continue;
381 }
382 unsigned int configBus = std::visit(
383 VariantToUnsignedIntVisitor(), findBus->second);
384 unsigned int configAddress = std::visit(
385 VariantToUnsignedIntVisitor(), findAddress->second);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400386
Chris Cainc45e18f2024-07-24 15:58:00 -0500387 if (configBus == bus && configAddress == addr)
James Feistde5e9702019-09-18 16:13:02 -0700388 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700389 sensorData = &cfgData;
James Feistde5e9702019-09-18 16:13:02 -0700390 break;
391 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500392 }
393 }
394 if (sensorData == nullptr)
395 {
396 std::cerr << "failed to find match for " << path.string()
397 << "\n";
398 continue;
399 }
400
401 auto findSensorName = baseConfiguration->second.find("Name");
402
403 if (findSensorName == baseConfiguration->second.end())
404 {
405 std::cerr << "could not determine configuration name for "
406 << path.string() << "\n";
407 continue;
408 }
409 std::string sensorName =
410 std::get<std::string>(findSensorName->second);
411
412 // on rescans, only update sensors we were signaled by
413 auto findSensor = tachSensors.find(sensorName);
414 if (!firstScan && findSensor != tachSensors.end())
415 {
416 bool found = false;
417 for (auto it = sensorsChanged->begin();
418 it != sensorsChanged->end(); it++)
419 {
420 if (it->ends_with(findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700421 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500422 sensorsChanged->erase(it);
423 findSensor->second = nullptr;
424 found = true;
425 break;
James Feistde5e9702019-09-18 16:13:02 -0700426 }
427 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500428 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700429 {
James Feist95b079b2018-11-21 09:28:00 -0800430 continue;
431 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500432 }
433 std::vector<thresholds::Threshold> sensorThresholds;
434 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
435 {
436 std::cerr << "error populating thresholds for " << sensorName
437 << "\n";
438 }
James Feist95b079b2018-11-21 09:28:00 -0800439
Chris Cainc45e18f2024-07-24 15:58:00 -0500440 auto presenceConfig =
441 sensorData->find(cfgIntf + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800442
Chris Cainc45e18f2024-07-24 15:58:00 -0500443 std::shared_ptr<PresenceGpio> presenceGpio(nullptr);
444
445 // presence sensors are optional
446 if (presenceConfig != sensorData->end())
447 {
448 auto findPolarity = presenceConfig->second.find("Polarity");
449 auto findPinName = presenceConfig->second.find("PinName");
450
451 if (findPinName == presenceConfig->second.end() ||
452 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800453 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500454 std::cerr << "Malformed Presence Configuration\n";
James Feistde5e9702019-09-18 16:13:02 -0700455 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500456 else
Patrick Williams2aaf7172024-08-16 15:20:40 -0400457 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500458 bool inverted =
459 std::get<std::string>(findPolarity->second) == "Low";
460 const auto* pinName =
461 std::get_if<std::string>(&findPinName->second);
462
463 if (pinName != nullptr)
James Feistde5e9702019-09-18 16:13:02 -0700464 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500465 auto findPresenceGpio = presenceGpios.find(*pinName);
466 if (findPresenceGpio != presenceGpios.end())
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +0200467 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500468 auto p = findPresenceGpio->second.lock();
469 if (p)
Patrick Williams2aaf7172024-08-16 15:20:40 -0400470 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500471 presenceGpio = p;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400472 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500473 }
474 if (!presenceGpio)
475 {
Xiaochao Ma4bbd02d2022-06-21 16:54:27 +0800476 auto findMonitorType =
477 presenceConfig->second.find("MonitorType");
478 bool polling = false;
479 if (findMonitorType != presenceConfig->second.end())
480 {
481 auto mType = std::get<std::string>(
482 findMonitorType->second);
483 if (mType == "Polling")
484 {
485 polling = true;
486 }
487 else if (mType != "Event")
488 {
489 std::cerr
490 << "Unsupported GPIO MonitorType of "
491 << mType << " for " << sensorName
492 << " (supported types: Polling, Event (default))\n";
493 }
494 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500495 try
Patrick Williams2aaf7172024-08-16 15:20:40 -0400496 {
Xiaochao Ma4bbd02d2022-06-21 16:54:27 +0800497 if (polling)
498 {
499 presenceGpio =
500 std::make_shared<PollingPresenceGpio>(
501 "Fan", sensorName, *pinName,
502 inverted, io);
503 }
504 else
505 {
506 presenceGpio =
507 std::make_shared<EventPresenceGpio>(
508 "Fan", sensorName, *pinName,
509 inverted, io);
510 }
Chris Cain83929002024-03-06 14:20:09 -0600511 presenceGpios[*pinName] = presenceGpio;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400512 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500513 catch (const std::system_error& e)
Patrick Williams2aaf7172024-08-16 15:20:40 -0400514 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500515 std::cerr
516 << "Failed to create GPIO monitor object for "
517 << *pinName << " / " << sensorName << ": "
518 << e.what() << "\n";
Patrick Williams2aaf7172024-08-16 15:20:40 -0400519 }
520 }
Ed Tanousbb679322022-05-16 16:10:00 -0700521 }
522 else
523 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500524 std::cerr << "Malformed Presence pinName for sensor "
525 << sensorName << " \n";
526 }
527 }
528 }
529 std::optional<RedundancySensor>* redundancy = nullptr;
530 if (fanType == FanTypes::aspeed)
531 {
532 redundancy = &systemRedundancy;
533 }
534
535 PowerState powerState = getPowerState(baseConfiguration->second);
536
537 constexpr double defaultMaxReading = 25000;
538 constexpr double defaultMinReading = 0;
539 std::pair<double, double> limits =
540 std::make_pair(defaultMinReading, defaultMaxReading);
541
542 auto connector =
543 sensorData->find(cfgIntf + std::string(".Connector"));
544
545 std::optional<std::string> led;
546 std::string pwmName;
Ed Tanous2e466962025-01-30 10:59:49 -0800547 std::filesystem::path pwmPath;
Chris Cainc45e18f2024-07-24 15:58:00 -0500548
549 // The Mutable parameter is optional, defaulting to false
550 bool isValueMutable = false;
551 if (connector != sensorData->end())
552 {
553 auto findPwm = connector->second.find("Pwm");
554 if (findPwm != connector->second.end())
555 {
556 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
557 findPwm->second);
558 if (!findPwmPath(directory, pwm, pwmPath))
559 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400560 std::cerr << "Connector for " << sensorName
Chris Cainc45e18f2024-07-24 15:58:00 -0500561 << " no pwm channel found!\n";
562 continue;
Ed Tanousbb679322022-05-16 16:10:00 -0700563 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400564
Ed Tanous2e466962025-01-30 10:59:49 -0800565 std::filesystem::path pwmEnableFile =
Chris Cainc45e18f2024-07-24 15:58:00 -0500566 "pwm" + std::to_string(pwm + 1) + "_enable";
Ed Tanous2e466962025-01-30 10:59:49 -0800567 std::filesystem::path enablePath =
568 pwmPath.parent_path() / pwmEnableFile;
Chris Cainc45e18f2024-07-24 15:58:00 -0500569 enablePwm(enablePath);
570
571 /* use pwm name override if found in configuration else
572 * use default */
573 auto findOverride = connector->second.find("PwmName");
574 if (findOverride != connector->second.end())
Patrick Williams2aaf7172024-08-16 15:20:40 -0400575 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500576 pwmName = std::visit(VariantToStringVisitor(),
577 findOverride->second);
578 }
579 else
580 {
581 pwmName = "Pwm_" + std::to_string(pwm + 1);
582 }
583
584 // Check PWM sensor mutability
585 auto findMutable = connector->second.find("Mutable");
586 if (findMutable != connector->second.end())
587 {
588 const auto* ptrMutable =
589 std::get_if<bool>(&(findMutable->second));
590 if (ptrMutable != nullptr)
Patrick Williams2aaf7172024-08-16 15:20:40 -0400591 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500592 isValueMutable = *ptrMutable;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400593 }
594 }
595 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500596 else
Patrick Williams2aaf7172024-08-16 15:20:40 -0400597 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500598 std::cerr
599 << "Connector for " << sensorName << " missing pwm!\n";
600 }
601
602 auto findLED = connector->second.find("LED");
603 if (findLED != connector->second.end())
604 {
605 const auto* ledName =
606 std::get_if<std::string>(&(findLED->second));
607 if (ledName == nullptr)
608 {
609 std::cerr
610 << "Wrong format for LED of " << sensorName << "\n";
611 }
612 else
613 {
614 led = *ledName;
615 }
Ed Tanousbb679322022-05-16 16:10:00 -0700616 }
617 }
618
Chris Cainc45e18f2024-07-24 15:58:00 -0500619 findLimits(limits, baseConfiguration);
620
621 enableFanInput(path);
622
623 auto& tachSensor = tachSensors[sensorName];
624 tachSensor = nullptr;
625 tachSensor = std::make_shared<TachSensor>(
626 path.string(), baseType, objectServer, dbusConnection,
627 presenceGpio, redundancy, io, sensorName,
628 std::move(sensorThresholds), *interfacePath, limits, powerState,
629 led);
630 tachSensor->setupRead();
631
Ed Tanous2e466962025-01-30 10:59:49 -0800632 if (!pwmPath.empty() && std::filesystem::exists(pwmPath) &&
Chris Cainc45e18f2024-07-24 15:58:00 -0500633 (pwmSensors.count(pwmPath) == 0U))
634 {
635 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
636 pwmName, pwmPath, dbusConnection, objectServer,
637 *interfacePath, "Fan", isValueMutable);
638 }
639 }
640
641 createRedundancySensor(tachSensors, dbusConnection, objectServer);
642 });
James Feistde5e9702019-09-18 16:13:02 -0700643 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700644 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
645 retries);
James Feist6714a252018-09-10 15:26:18 -0700646}
647
James Feistb6c0b912019-07-09 12:21:44 -0700648int main()
James Feist6714a252018-09-10 15:26:18 -0700649{
Ed Tanous1f978632023-02-28 18:16:39 -0800650 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700651 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700652 sdbusplus::asio::object_server objectServer(systemBus, true);
653
654 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanousd9067252022-10-22 15:35:52 -0700655 objectServer.add_manager("/xyz/openbmc_project/control");
Lei YUc2f83fe2022-12-02 14:49:47 +0800656 objectServer.add_manager("/xyz/openbmc_project/inventory");
James Feist6714a252018-09-10 15:26:18 -0700657 systemBus->request_name("xyz.openbmc_project.FanSensor");
Josh Lehan5170fe62022-08-03 13:17:41 -0700658 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700659 tachSensors;
660 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
661 pwmSensors;
Chris Cain83929002024-03-06 14:20:09 -0600662 boost::container::flat_map<std::string, std::weak_ptr<PresenceGpio>>
663 presenceGpios;
James Feist5591cf082020-07-15 16:44:54 -0700664 auto sensorsChanged =
665 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700666
Ed Tanous83db50c2023-03-01 10:20:24 -0800667 boost::asio::post(io, [&]() {
Chris Cain83929002024-03-06 14:20:09 -0600668 createSensors(io, objectServer, tachSensors, pwmSensors, presenceGpios,
669 systemBus, nullptr);
James Feist6714a252018-09-10 15:26:18 -0700670 });
671
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700672 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500673 std::function<void(sdbusplus::message_t&)> eventHandler =
674 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400675 if (message.is_method_error())
676 {
677 std::cerr << "callback method error\n";
678 return;
679 }
680 sensorsChanged->insert(message.get_path());
681 // this implicitly cancels the timer
682 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700683
Patrick Williams2aaf7172024-08-16 15:20:40 -0400684 filterTimer.async_wait([&](const boost::system::error_code& ec) {
685 if (ec == boost::asio::error::operation_aborted)
686 {
687 /* we were canceled*/
688 return;
689 }
690 if (ec)
691 {
692 std::cerr << "timer error\n";
693 return;
694 }
695 createSensors(io, objectServer, tachSensors, pwmSensors,
Chris Cain83929002024-03-06 14:20:09 -0600696 presenceGpios, systemBus, sensorsChanged, 5);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400697 });
698 };
James Feist6714a252018-09-10 15:26:18 -0700699
Zev Weiss214d9712022-08-12 12:54:31 -0700700 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
701 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700702
James Feistdc6c55f2018-10-31 12:53:20 -0700703 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500704 std::function<void(sdbusplus::message_t&)> redundancyHandler =
705 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400706 createRedundancySensor(tachSensors, systemBus, objectServer);
707 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500708 auto match = std::make_unique<sdbusplus::bus::match_t>(
709 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700710 "type='signal',member='PropertiesChanged',path_namespace='" +
711 std::string(inventoryPath) + "',arg0namespace='" +
712 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700713 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700714 matches.emplace_back(std::move(match));
715
Bruce Lee1263c3d2021-06-04 15:16:33 +0800716 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700717 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700718 return 0;
James Feist6714a252018-09-10 15:26:18 -0700719}