blob: 98e41a0438890ae05285099d2b520df8afd8adc1 [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},
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 {
George Liue34e1232025-02-20 11:27:48 +0800103 lg2::error("Error opening '{PATH}'", "PATH", compatiblePath);
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 {
George Liue34e1232025-02-20 11:27:48 +0800128 lg2::error("Error read/write '{PATH}'", "PATH", filePath);
Jeff Linabf91de2020-12-23 10:55:42 +0800129 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 {
George Liue34e1232025-02-20 11:27:48 +0800152 lg2::error("No PWMs are found!");
Howard Chiuddf25d12021-12-02 15:14:44 +0800153 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 {
George Liue34e1232025-02-20 11:27:48 +0800163 lg2::error("read_symlink() failed: '{ERROR_MESSAGE}'",
164 "ERROR_MESSAGE", ec.message());
Howard Chiuddf25d12021-12-02 15:14:44 +0800165 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 {
George Liue34e1232025-02-20 11:27:48 +0800190 lg2::error("exists() failed: '{ERROR_MESSAGE}'", "ERROR_MESSAGE",
191 ec.message());
Howard Chiuddf25d12021-12-02 15:14:44 +0800192 }
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 {
George Liue34e1232025-02-20 11:27:48 +0800236 lg2::error("Error calling entity manager");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400237 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 {
George Liue34e1232025-02-20 11:27:48 +0800249 lg2::error("Malformed redundancy record");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400250 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 {
George Liue34e1232025-02-20 11:27:48 +0800300 lg2::error("No fan sensors in system");
Chris Cainc45e18f2024-07-24 15:58:00 -0500301 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 {
George Liue34e1232025-02-20 11:27:48 +0800342 lg2::error("'{INTERFACE}' missing index", "INTERFACE",
343 baseConfiguration->first);
Chris Cainc45e18f2024-07-24 15:58:00 -0500344 continue;
345 }
346 unsigned int configIndex = std::visit(
347 VariantToUnsignedIntVisitor(), findIndex->second);
348 if (configIndex != index)
349 {
350 continue;
351 }
352 if (fanType == FanTypes::aspeed ||
353 fanType == FanTypes::nuvoton || fanType == FanTypes::hpe)
354 {
355 // there will be only 1 aspeed or nuvoton or hpe sensor
356 // object in sysfs, we found the fan
357 sensorData = &cfgData;
358 break;
359 }
360 if (fanType == FanTypes::i2c)
361 {
362 std::string deviceName =
Ed Tanous2e466962025-01-30 10:59:49 -0800363 std::filesystem::read_symlink(directory / "device")
364 .filename();
Chris Cainc45e18f2024-07-24 15:58:00 -0500365
366 size_t bus = 0;
367 size_t addr = 0;
368 if (!getDeviceBusAddr(deviceName, bus, addr))
James Feistde5e9702019-09-18 16:13:02 -0700369 {
Akshit Shah03d333e2023-08-23 22:14:28 +0000370 continue;
James Feistde5e9702019-09-18 16:13:02 -0700371 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800372
Chris Cainc45e18f2024-07-24 15:58:00 -0500373 auto findBus = baseConfiguration->second.find("Bus");
374 auto findAddress =
375 baseConfiguration->second.find("Address");
376 if (findBus == baseConfiguration->second.end() ||
377 findAddress == baseConfiguration->second.end())
378 {
George Liue34e1232025-02-20 11:27:48 +0800379 lg2::error("'{INTERFACE}' missing bus or address",
380 "INTERFACE", baseConfiguration->first);
Chris Cainc45e18f2024-07-24 15:58:00 -0500381 continue;
382 }
383 unsigned int configBus = std::visit(
384 VariantToUnsignedIntVisitor(), findBus->second);
385 unsigned int configAddress = std::visit(
386 VariantToUnsignedIntVisitor(), findAddress->second);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400387
Chris Cainc45e18f2024-07-24 15:58:00 -0500388 if (configBus == bus && configAddress == addr)
James Feistde5e9702019-09-18 16:13:02 -0700389 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700390 sensorData = &cfgData;
James Feistde5e9702019-09-18 16:13:02 -0700391 break;
392 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500393 }
394 }
395 if (sensorData == nullptr)
396 {
George Liue34e1232025-02-20 11:27:48 +0800397 lg2::error("failed to find match for '{PATH}'", "PATH",
398 path.string());
Chris Cainc45e18f2024-07-24 15:58:00 -0500399 continue;
400 }
401
402 auto findSensorName = baseConfiguration->second.find("Name");
403
404 if (findSensorName == baseConfiguration->second.end())
405 {
George Liue34e1232025-02-20 11:27:48 +0800406 lg2::error(
407 "could not determine configuration name for '{PATH}'",
408 "PATH", path.string());
Chris Cainc45e18f2024-07-24 15:58:00 -0500409 continue;
410 }
411 std::string sensorName =
412 std::get<std::string>(findSensorName->second);
413
414 // on rescans, only update sensors we were signaled by
415 auto findSensor = tachSensors.find(sensorName);
416 if (!firstScan && findSensor != tachSensors.end())
417 {
418 bool found = false;
419 for (auto it = sensorsChanged->begin();
420 it != sensorsChanged->end(); it++)
421 {
422 if (it->ends_with(findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700423 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500424 sensorsChanged->erase(it);
425 findSensor->second = nullptr;
426 found = true;
427 break;
James Feistde5e9702019-09-18 16:13:02 -0700428 }
429 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500430 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700431 {
James Feist95b079b2018-11-21 09:28:00 -0800432 continue;
433 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500434 }
435 std::vector<thresholds::Threshold> sensorThresholds;
436 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
437 {
George Liue34e1232025-02-20 11:27:48 +0800438 lg2::error("error populating thresholds for '{NAME}'", "NAME",
439 sensorName);
Chris Cainc45e18f2024-07-24 15:58:00 -0500440 }
James Feist95b079b2018-11-21 09:28:00 -0800441
Chris Cainc45e18f2024-07-24 15:58:00 -0500442 auto presenceConfig =
443 sensorData->find(cfgIntf + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800444
Chris Cainc45e18f2024-07-24 15:58:00 -0500445 std::shared_ptr<PresenceGpio> presenceGpio(nullptr);
446
447 // presence sensors are optional
448 if (presenceConfig != sensorData->end())
449 {
450 auto findPolarity = presenceConfig->second.find("Polarity");
451 auto findPinName = presenceConfig->second.find("PinName");
452
453 if (findPinName == presenceConfig->second.end() ||
454 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800455 {
George Liue34e1232025-02-20 11:27:48 +0800456 lg2::error("Malformed Presence Configuration");
James Feistde5e9702019-09-18 16:13:02 -0700457 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500458 else
Patrick Williams2aaf7172024-08-16 15:20:40 -0400459 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500460 bool inverted =
461 std::get<std::string>(findPolarity->second) == "Low";
462 const auto* pinName =
463 std::get_if<std::string>(&findPinName->second);
464
465 if (pinName != nullptr)
James Feistde5e9702019-09-18 16:13:02 -0700466 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500467 auto findPresenceGpio = presenceGpios.find(*pinName);
468 if (findPresenceGpio != presenceGpios.end())
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +0200469 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500470 auto p = findPresenceGpio->second.lock();
471 if (p)
Patrick Williams2aaf7172024-08-16 15:20:40 -0400472 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500473 presenceGpio = p;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400474 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500475 }
476 if (!presenceGpio)
477 {
Xiaochao Ma4bbd02d2022-06-21 16:54:27 +0800478 auto findMonitorType =
479 presenceConfig->second.find("MonitorType");
480 bool polling = false;
481 if (findMonitorType != presenceConfig->second.end())
482 {
483 auto mType = std::get<std::string>(
484 findMonitorType->second);
485 if (mType == "Polling")
486 {
487 polling = true;
488 }
489 else if (mType != "Event")
490 {
George Liue34e1232025-02-20 11:27:48 +0800491 lg2::error(
492 "Unsupported GPIO MonitorType of '{TYPE}' for '{NAME}', "
493 "supported types: Polling, Event default",
494 "TYPE", mType, "NAME", sensorName);
Xiaochao Ma4bbd02d2022-06-21 16:54:27 +0800495 }
496 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500497 try
Patrick Williams2aaf7172024-08-16 15:20:40 -0400498 {
Xiaochao Ma4bbd02d2022-06-21 16:54:27 +0800499 if (polling)
500 {
501 presenceGpio =
502 std::make_shared<PollingPresenceGpio>(
503 "Fan", sensorName, *pinName,
504 inverted, io);
505 }
506 else
507 {
508 presenceGpio =
509 std::make_shared<EventPresenceGpio>(
510 "Fan", sensorName, *pinName,
511 inverted, io);
512 }
Chris Cain83929002024-03-06 14:20:09 -0600513 presenceGpios[*pinName] = presenceGpio;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400514 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500515 catch (const std::system_error& e)
Patrick Williams2aaf7172024-08-16 15:20:40 -0400516 {
George Liue34e1232025-02-20 11:27:48 +0800517 lg2::error(
518 "Failed to create GPIO monitor object for "
519 "'{PIN_NAME}' / '{SENSOR_NAME}': '{ERROR}'",
520 "PIN_NAME", *pinName, "SENSOR_NAME",
521 sensorName, "ERROR", e);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400522 }
523 }
Ed Tanousbb679322022-05-16 16:10:00 -0700524 }
525 else
526 {
George Liue34e1232025-02-20 11:27:48 +0800527 lg2::error(
528 "Malformed Presence pinName for sensor '{NAME}'",
529 "NAME", sensorName);
Chris Cainc45e18f2024-07-24 15:58:00 -0500530 }
531 }
532 }
533 std::optional<RedundancySensor>* redundancy = nullptr;
534 if (fanType == FanTypes::aspeed)
535 {
536 redundancy = &systemRedundancy;
537 }
538
539 PowerState powerState = getPowerState(baseConfiguration->second);
540
541 constexpr double defaultMaxReading = 25000;
542 constexpr double defaultMinReading = 0;
543 std::pair<double, double> limits =
544 std::make_pair(defaultMinReading, defaultMaxReading);
545
546 auto connector =
547 sensorData->find(cfgIntf + std::string(".Connector"));
548
549 std::optional<std::string> led;
550 std::string pwmName;
Ed Tanous2e466962025-01-30 10:59:49 -0800551 std::filesystem::path pwmPath;
Chris Cainc45e18f2024-07-24 15:58:00 -0500552
553 // The Mutable parameter is optional, defaulting to false
554 bool isValueMutable = false;
555 if (connector != sensorData->end())
556 {
557 auto findPwm = connector->second.find("Pwm");
558 if (findPwm != connector->second.end())
559 {
560 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
561 findPwm->second);
562 if (!findPwmPath(directory, pwm, pwmPath))
563 {
George Liue34e1232025-02-20 11:27:48 +0800564 lg2::error(
565 "Connector for '{NAME}' no pwm channel found!",
566 "NAME", sensorName);
Chris Cainc45e18f2024-07-24 15:58:00 -0500567 continue;
Ed Tanousbb679322022-05-16 16:10:00 -0700568 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400569
Ed Tanous2e466962025-01-30 10:59:49 -0800570 std::filesystem::path pwmEnableFile =
Chris Cainc45e18f2024-07-24 15:58:00 -0500571 "pwm" + std::to_string(pwm + 1) + "_enable";
Ed Tanous2e466962025-01-30 10:59:49 -0800572 std::filesystem::path enablePath =
573 pwmPath.parent_path() / pwmEnableFile;
Chris Cainc45e18f2024-07-24 15:58:00 -0500574 enablePwm(enablePath);
575
576 /* use pwm name override if found in configuration else
577 * use default */
578 auto findOverride = connector->second.find("PwmName");
579 if (findOverride != connector->second.end())
Patrick Williams2aaf7172024-08-16 15:20:40 -0400580 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500581 pwmName = std::visit(VariantToStringVisitor(),
582 findOverride->second);
583 }
584 else
585 {
586 pwmName = "Pwm_" + std::to_string(pwm + 1);
587 }
588
589 // Check PWM sensor mutability
590 auto findMutable = connector->second.find("Mutable");
591 if (findMutable != connector->second.end())
592 {
593 const auto* ptrMutable =
594 std::get_if<bool>(&(findMutable->second));
595 if (ptrMutable != nullptr)
Patrick Williams2aaf7172024-08-16 15:20:40 -0400596 {
Chris Cainc45e18f2024-07-24 15:58:00 -0500597 isValueMutable = *ptrMutable;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400598 }
599 }
600 }
Chris Cainc45e18f2024-07-24 15:58:00 -0500601 else
Patrick Williams2aaf7172024-08-16 15:20:40 -0400602 {
George Liue34e1232025-02-20 11:27:48 +0800603 lg2::error("Connector for '{NAME}' missing pwm!", "NAME",
604 sensorName);
Chris Cainc45e18f2024-07-24 15:58:00 -0500605 }
606
607 auto findLED = connector->second.find("LED");
608 if (findLED != connector->second.end())
609 {
610 const auto* ledName =
611 std::get_if<std::string>(&(findLED->second));
612 if (ledName == nullptr)
613 {
George Liue34e1232025-02-20 11:27:48 +0800614 lg2::error("Wrong format for LED of '{NAME}'", "NAME",
615 sensorName);
Chris Cainc45e18f2024-07-24 15:58:00 -0500616 }
617 else
618 {
619 led = *ledName;
620 }
Ed Tanousbb679322022-05-16 16:10:00 -0700621 }
622 }
623
Chris Cainc45e18f2024-07-24 15:58:00 -0500624 findLimits(limits, baseConfiguration);
625
626 enableFanInput(path);
627
628 auto& tachSensor = tachSensors[sensorName];
629 tachSensor = nullptr;
630 tachSensor = std::make_shared<TachSensor>(
631 path.string(), baseType, objectServer, dbusConnection,
632 presenceGpio, redundancy, io, sensorName,
633 std::move(sensorThresholds), *interfacePath, limits, powerState,
634 led);
635 tachSensor->setupRead();
636
Ed Tanous2e466962025-01-30 10:59:49 -0800637 if (!pwmPath.empty() && std::filesystem::exists(pwmPath) &&
Chris Cainc45e18f2024-07-24 15:58:00 -0500638 (pwmSensors.count(pwmPath) == 0U))
639 {
640 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
641 pwmName, pwmPath, dbusConnection, objectServer,
642 *interfacePath, "Fan", isValueMutable);
643 }
644 }
645
646 createRedundancySensor(tachSensors, dbusConnection, objectServer);
647 });
James Feistde5e9702019-09-18 16:13:02 -0700648 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700649 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
650 retries);
James Feist6714a252018-09-10 15:26:18 -0700651}
652
James Feistb6c0b912019-07-09 12:21:44 -0700653int main()
James Feist6714a252018-09-10 15:26:18 -0700654{
Ed Tanous1f978632023-02-28 18:16:39 -0800655 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700656 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700657 sdbusplus::asio::object_server objectServer(systemBus, true);
658
659 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanousd9067252022-10-22 15:35:52 -0700660 objectServer.add_manager("/xyz/openbmc_project/control");
Lei YUc2f83fe2022-12-02 14:49:47 +0800661 objectServer.add_manager("/xyz/openbmc_project/inventory");
James Feist6714a252018-09-10 15:26:18 -0700662 systemBus->request_name("xyz.openbmc_project.FanSensor");
Josh Lehan5170fe62022-08-03 13:17:41 -0700663 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700664 tachSensors;
665 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
666 pwmSensors;
Chris Cain83929002024-03-06 14:20:09 -0600667 boost::container::flat_map<std::string, std::weak_ptr<PresenceGpio>>
668 presenceGpios;
James Feist5591cf082020-07-15 16:44:54 -0700669 auto sensorsChanged =
670 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700671
Ed Tanous83db50c2023-03-01 10:20:24 -0800672 boost::asio::post(io, [&]() {
Chris Cain83929002024-03-06 14:20:09 -0600673 createSensors(io, objectServer, tachSensors, pwmSensors, presenceGpios,
674 systemBus, nullptr);
James Feist6714a252018-09-10 15:26:18 -0700675 });
676
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700677 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500678 std::function<void(sdbusplus::message_t&)> eventHandler =
679 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400680 if (message.is_method_error())
681 {
George Liue34e1232025-02-20 11:27:48 +0800682 lg2::error("callback method error");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400683 return;
684 }
685 sensorsChanged->insert(message.get_path());
686 // this implicitly cancels the timer
687 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700688
Patrick Williams2aaf7172024-08-16 15:20:40 -0400689 filterTimer.async_wait([&](const boost::system::error_code& ec) {
690 if (ec == boost::asio::error::operation_aborted)
691 {
692 /* we were canceled*/
693 return;
694 }
695 if (ec)
696 {
George Liue34e1232025-02-20 11:27:48 +0800697 lg2::error("timer error");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400698 return;
699 }
700 createSensors(io, objectServer, tachSensors, pwmSensors,
Chris Cain83929002024-03-06 14:20:09 -0600701 presenceGpios, systemBus, sensorsChanged, 5);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400702 });
703 };
James Feist6714a252018-09-10 15:26:18 -0700704
Zev Weiss214d9712022-08-12 12:54:31 -0700705 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
706 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700707
James Feistdc6c55f2018-10-31 12:53:20 -0700708 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500709 std::function<void(sdbusplus::message_t&)> redundancyHandler =
710 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400711 createRedundancySensor(tachSensors, systemBus, objectServer);
712 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500713 auto match = std::make_unique<sdbusplus::bus::match_t>(
714 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700715 "type='signal',member='PropertiesChanged',path_namespace='" +
716 std::string(inventoryPath) + "',arg0namespace='" +
717 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700718 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700719 matches.emplace_back(std::move(match));
720
Bruce Lee1263c3d2021-06-04 15:16:33 +0800721 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700722 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700723 return 0;
James Feist6714a252018-09-10 15:26:18 -0700724}