blob: 8061d4c1ffe5e730e90a648055a4670b2d5259eb [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
2// Copyright (c) 2017 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "PwmSensor.hpp"
18#include "TachSensor.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070019#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103020#include "Utils.hpp"
21#include "VariantVisitors.hpp"
22
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/algorithm/string/replace.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070024#include <boost/asio/error.hpp>
25#include <boost/asio/io_context.hpp>
26#include <boost/asio/post.hpp>
27#include <boost/asio/steady_timer.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070029#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070030#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070032#include <sdbusplus/bus.hpp>
James Feist38fb5982020-05-28 10:09:54 -070033#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070034#include <sdbusplus/message.hpp>
James Feist38fb5982020-05-28 10:09:54 -070035
36#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070037#include <chrono>
38#include <cstddef>
39#include <cstdint>
James Feist24f02f22019-04-15 11:05:39 -070040#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070041#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070042#include <functional>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070043#include <ios>
44#include <iostream>
45#include <map>
Patrick Venture96e97db2019-10-31 13:44:38 -070046#include <memory>
47#include <optional>
James Feist6714a252018-09-10 15:26:18 -070048#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070049#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070050#include <system_error>
Patrick Venture96e97db2019-10-31 13:44:38 -070051#include <utility>
52#include <variant>
53#include <vector>
James Feist6714a252018-09-10 15:26:18 -070054
James Feistcf3bce62019-01-08 10:07:19 -080055namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080056
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000057// The following two structures need to be consistent
Chris Sidesc361e222023-04-05 15:18:20 -050058static auto sensorTypes{std::to_array<const char*>(
59 {"AspeedFan", "I2CFan", "NuvotonFan", "HPEFan"})};
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000060
61enum FanTypes
62{
63 aspeed = 0,
64 i2c,
65 nuvoton,
Chris Sidesc361e222023-04-05 15:18:20 -050066 hpe,
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000067 max,
68};
69
70static_assert(std::tuple_size<decltype(sensorTypes)>::value == FanTypes::max,
71 "sensorTypes element number is not equal to FanTypes number");
72
James Feistdc6c55f2018-10-31 12:53:20 -070073constexpr const char* redundancyConfiguration =
74 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070075static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070076
James Feistdc6c55f2018-10-31 12:53:20 -070077// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070078std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080079
Chris Sides3d5260d2023-04-10 15:45:00 -050080static const std::map<std::string, FanTypes> compatibleFanTypes = {
81 {"aspeed,ast2400-pwm-tacho", FanTypes::aspeed},
82 {"aspeed,ast2500-pwm-tacho", FanTypes::aspeed},
Potin Lai40c4d682023-05-19 17:19:03 +080083 {"aspeed,ast2600-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
James Feist95b079b2018-11-21 09:28:00 -080090FanTypes getFanType(const fs::path& parentPath)
91{
Chris Sides9a472e82023-04-03 15:13:37 -050092 fs::path linkPath = parentPath / "of_node";
Zhikui Ren39963222023-05-04 17:06:50 -070093 if (!fs::exists(linkPath))
94 {
95 return FanTypes::i2c;
96 }
Chris Sides9a472e82023-04-03 15:13:37 -050097
Zhikui Ren39963222023-05-04 17:06:50 -070098 std::string canonical = fs::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 {
Chris Sides9a472e82023-04-03 15:13:37 -0500104 std::cerr << "Error opening " << compatiblePath << "\n";
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}
Jeff Linabf91de2020-12-23 10:55:42 +0800124void enablePwm(const fs::path& filePath)
125{
126 std::fstream enableFile(filePath, std::ios::in | std::ios::out);
127 if (!enableFile.good())
128 {
129 std::cerr << "Error read/write " << filePath << "\n";
130 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}
Howard Chiuddf25d12021-12-02 15:14:44 +0800140bool findPwmfanPath(unsigned int configPwmfanIndex, fs::path& pwmPath)
141{
142 /* Search PWM since pwm-fan had separated
143 * PWM from tach directory and 1 channel only*/
144 std::vector<fs::path> pwmfanPaths;
145 std::string pwnfanDevName("pwm-fan");
146
147 pwnfanDevName += std::to_string(configPwmfanIndex);
148
149 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwmfanPaths))
150 {
151 std::cerr << "No PWMs are found!\n";
152 return false;
153 }
154 for (const auto& path : pwmfanPaths)
155 {
156 std::error_code ec;
157 fs::path link = fs::read_symlink(path.parent_path() / "device", ec);
158
159 if (ec)
160 {
161 std::cerr << "read_symlink() failed: " << ec.message() << " ("
162 << ec.value() << ")\n";
163 continue;
164 }
165
166 if (link.filename().string() == pwnfanDevName)
167 {
168 pwmPath = path;
169 return true;
170 }
171 }
172 return false;
173}
174bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath)
175{
176 std::error_code ec;
177
178 /* Assuming PWM file is appeared in the same directory as fanX_input */
179 auto path = directory / ("pwm" + std::to_string(pwm + 1));
180 bool exists = fs::exists(path, ec);
181
182 if (ec || !exists)
183 {
184 /* PWM file not exist or error happened */
185 if (ec)
186 {
187 std::cerr << "exists() failed: " << ec.message() << " ("
188 << ec.value() << ")\n";
189 }
190 /* try search form pwm-fanX directory */
191 return findPwmfanPath(pwm, pwmPath);
192 }
193
194 pwmPath = path;
195 return true;
196}
Justin Ledford9c47bd72022-08-27 01:02:09 +0000197
198// The argument to this function should be the fanN_input file that we want to
199// enable. The function will locate the corresponding fanN_enable file if it
200// exists. Note that some drivers don't provide this file if the sensors are
201// always enabled.
202void enableFanInput(const fs::path& fanInputPath)
203{
204 std::error_code ec;
205 std::string path(fanInputPath.string());
206 boost::replace_last(path, "input", "enable");
207
208 bool exists = fs::exists(path, ec);
209 if (ec || !exists)
210 {
211 return;
212 }
213
214 std::fstream enableFile(path, std::ios::out);
215 if (!enableFile.good())
216 {
217 return;
218 }
219 enableFile << 1;
220}
221
Kuiying Wangd5407412020-09-09 16:06:56 +0800222void createRedundancySensor(
Josh Lehan5170fe62022-08-03 13:17:41 -0700223 const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
Kuiying Wangd5407412020-09-09 16:06:56 +0800224 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700225 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800226 sdbusplus::asio::object_server& objectServer)
227{
Kuiying Wangd5407412020-09-09 16:06:56 +0800228 conn->async_method_call(
229 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700230 const ManagedObjectType& managedObj) {
Ed Tanousbb679322022-05-16 16:10:00 -0700231 if (ec)
232 {
233 std::cerr << "Error calling entity manager \n";
234 return;
235 }
Zev Weiss77636ec2022-08-12 18:21:01 -0700236 for (const auto& [path, interfaces] : managedObj)
Ed Tanousbb679322022-05-16 16:10:00 -0700237 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700238 for (const auto& [intf, cfg] : interfaces)
Kuiying Wangd5407412020-09-09 16:06:56 +0800239 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700240 if (intf == redundancyConfiguration)
Kuiying Wangd5407412020-09-09 16:06:56 +0800241 {
Ed Tanousbb679322022-05-16 16:10:00 -0700242 // currently only support one
Zev Weiss77636ec2022-08-12 18:21:01 -0700243 auto findCount = cfg.find("AllowedFailures");
244 if (findCount == cfg.end())
Kuiying Wangd5407412020-09-09 16:06:56 +0800245 {
Ed Tanousbb679322022-05-16 16:10:00 -0700246 std::cerr << "Malformed redundancy record \n";
Kuiying Wangd5407412020-09-09 16:06:56 +0800247 return;
248 }
Ed Tanousbb679322022-05-16 16:10:00 -0700249 std::vector<std::string> sensorList;
250
Zev Weiss77636ec2022-08-12 18:21:01 -0700251 for (const auto& [name, sensor] : sensors)
Ed Tanousbb679322022-05-16 16:10:00 -0700252 {
253 sensorList.push_back(
254 "/xyz/openbmc_project/sensors/fan_tach/" +
Zev Weiss77636ec2022-08-12 18:21:01 -0700255 sensor->name);
Ed Tanousbb679322022-05-16 16:10:00 -0700256 }
257 systemRedundancy.reset();
Zev Weiss77636ec2022-08-12 18:21:01 -0700258 systemRedundancy.emplace(
259 RedundancySensor(std::get<uint64_t>(findCount->second),
260 sensorList, objectServer, path));
Ed Tanousbb679322022-05-16 16:10:00 -0700261
262 return;
Kuiying Wangd5407412020-09-09 16:06:56 +0800263 }
264 }
Ed Tanousbb679322022-05-16 16:10:00 -0700265 }
Patrick Williams597e8422023-10-20 11:19:01 -0500266 },
Nan Zhou3e620af2022-09-20 22:28:31 +0000267 "xyz.openbmc_project.EntityManager", "/xyz/openbmc_project/inventory",
Kuiying Wangd5407412020-09-09 16:06:56 +0800268 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
269}
270
James Feist6714a252018-09-10 15:26:18 -0700271void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800272 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Josh Lehan5170fe62022-08-03 13:17:41 -0700273 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700274 tachSensors,
275 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
276 pwmSensors,
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +0200277 boost::container::flat_map<std::string, std::weak_ptr<PresenceSensor>>&
278 presenceSensors,
James Feist6714a252018-09-10 15:26:18 -0700279 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700280 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700281 sensorsChanged,
282 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700283{
James Feistde5e9702019-09-18 16:13:02 -0700284 auto getter = std::make_shared<GetSensorConfiguration>(
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +0200285 dbusConnection, [&io, &objectServer, &tachSensors, &pwmSensors,
286 &presenceSensors, &dbusConnection, sensorsChanged](
287 const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700288 bool firstScan = sensorsChanged == nullptr;
289 std::vector<fs::path> paths;
290 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
291 {
292 std::cerr << "No fan sensors in system\n";
293 return;
294 }
295
296 // iterate through all found fan sensors, and try to match them with
297 // configuration
298 for (const auto& path : paths)
299 {
300 std::smatch match;
301 std::string pathStr = path.string();
302
303 std::regex_search(pathStr, match, inputRegex);
304 std::string indexStr = *(match.begin() + 1);
305
306 fs::path directory = path.parent_path();
307 FanTypes fanType = getFanType(directory);
Zev Weiss054aad82022-08-18 01:37:34 -0700308 std::string cfgIntf = configInterfaceName(sensorTypes[fanType]);
Ed Tanousbb679322022-05-16 16:10:00 -0700309
310 // convert to 0 based
311 size_t index = std::stoul(indexStr) - 1;
312
313 const char* baseType = nullptr;
314 const SensorData* sensorData = nullptr;
315 const std::string* interfacePath = nullptr;
316 const SensorBaseConfiguration* baseConfiguration = nullptr;
Zev Weiss77636ec2022-08-12 18:21:01 -0700317 for (const auto& [path, cfgData] : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800318 {
Ed Tanousbb679322022-05-16 16:10:00 -0700319 // find the base of the configuration to see if indexes
320 // match
Zev Weiss054aad82022-08-18 01:37:34 -0700321 auto sensorBaseFind = cfgData.find(cfgIntf);
Zev Weiss77636ec2022-08-12 18:21:01 -0700322 if (sensorBaseFind == cfgData.end())
James Feist95b079b2018-11-21 09:28:00 -0800323 {
Ed Tanousbb679322022-05-16 16:10:00 -0700324 continue;
325 }
326
327 baseConfiguration = &(*sensorBaseFind);
Zev Weiss77636ec2022-08-12 18:21:01 -0700328 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700329 baseType = sensorTypes[fanType];
330
331 auto findIndex = baseConfiguration->second.find("Index");
332 if (findIndex == baseConfiguration->second.end())
333 {
334 std::cerr << baseConfiguration->first << " missing index\n";
335 continue;
336 }
337 unsigned int configIndex = std::visit(
338 VariantToUnsignedIntVisitor(), findIndex->second);
339 if (configIndex != index)
340 {
341 continue;
342 }
Chris Sidesc361e222023-04-05 15:18:20 -0500343 if (fanType == FanTypes::aspeed ||
344 fanType == FanTypes::nuvoton || fanType == FanTypes::hpe)
Ed Tanousbb679322022-05-16 16:10:00 -0700345 {
Chris Sidesc361e222023-04-05 15:18:20 -0500346 // there will be only 1 aspeed or nuvoton or hpe sensor
Chris Sides3d5260d2023-04-10 15:45:00 -0500347 // object in sysfs, we found the fan
Zev Weiss77636ec2022-08-12 18:21:01 -0700348 sensorData = &cfgData;
Ed Tanousbb679322022-05-16 16:10:00 -0700349 break;
350 }
351 if (fanType == FanTypes::i2c)
352 {
Akshit Shah03d333e2023-08-23 22:14:28 +0000353 std::string deviceName =
Ed Tanousbb679322022-05-16 16:10:00 -0700354 fs::read_symlink(directory / "device").filename();
355
Akshit Shah03d333e2023-08-23 22:14:28 +0000356 size_t bus = 0;
357 size_t addr = 0;
358 if (!getDeviceBusAddr(deviceName, bus, addr))
James Feistde5e9702019-09-18 16:13:02 -0700359 {
Akshit Shah03d333e2023-08-23 22:14:28 +0000360 continue;
James Feistde5e9702019-09-18 16:13:02 -0700361 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800362
Ed Tanousbb679322022-05-16 16:10:00 -0700363 auto findBus = baseConfiguration->second.find("Bus");
364 auto findAddress =
365 baseConfiguration->second.find("Address");
366 if (findBus == baseConfiguration->second.end() ||
367 findAddress == baseConfiguration->second.end())
James Feistde5e9702019-09-18 16:13:02 -0700368 {
369 std::cerr << baseConfiguration->first
Ed Tanousbb679322022-05-16 16:10:00 -0700370 << " missing bus or address\n";
James Feistde5e9702019-09-18 16:13:02 -0700371 continue;
372 }
Ed Tanousbb679322022-05-16 16:10:00 -0700373 unsigned int configBus = std::visit(
374 VariantToUnsignedIntVisitor(), findBus->second);
375 unsigned int configAddress = std::visit(
376 VariantToUnsignedIntVisitor(), findAddress->second);
377
Akshit Shah03d333e2023-08-23 22:14:28 +0000378 if (configBus == bus && configAddress == addr)
James Feistde5e9702019-09-18 16:13:02 -0700379 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700380 sensorData = &cfgData;
James Feistde5e9702019-09-18 16:13:02 -0700381 break;
382 }
Ed Tanousbb679322022-05-16 16:10:00 -0700383 }
384 }
385 if (sensorData == nullptr)
386 {
387 std::cerr << "failed to find match for " << path.string()
388 << "\n";
389 continue;
390 }
391
392 auto findSensorName = baseConfiguration->second.find("Name");
393
394 if (findSensorName == baseConfiguration->second.end())
395 {
396 std::cerr << "could not determine configuration name for "
397 << path.string() << "\n";
398 continue;
399 }
400 std::string sensorName =
401 std::get<std::string>(findSensorName->second);
402
403 // on rescans, only update sensors we were signaled by
404 auto findSensor = tachSensors.find(sensorName);
405 if (!firstScan && findSensor != tachSensors.end())
406 {
407 bool found = false;
408 for (auto it = sensorsChanged->begin();
409 it != sensorsChanged->end(); it++)
410 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700411 if (it->ends_with(findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700412 {
Ed Tanousbb679322022-05-16 16:10:00 -0700413 sensorsChanged->erase(it);
414 findSensor->second = nullptr;
415 found = true;
416 break;
James Feistde5e9702019-09-18 16:13:02 -0700417 }
418 }
Ed Tanousbb679322022-05-16 16:10:00 -0700419 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700420 {
James Feist95b079b2018-11-21 09:28:00 -0800421 continue;
422 }
Ed Tanousbb679322022-05-16 16:10:00 -0700423 }
424 std::vector<thresholds::Threshold> sensorThresholds;
425 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
426 {
427 std::cerr << "error populating thresholds for " << sensorName
428 << "\n";
429 }
James Feist95b079b2018-11-21 09:28:00 -0800430
Ed Tanousbb679322022-05-16 16:10:00 -0700431 auto presenceConfig =
Zev Weiss054aad82022-08-18 01:37:34 -0700432 sensorData->find(cfgIntf + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800433
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +0200434 std::shared_ptr<PresenceSensor> presenceSensor(nullptr);
Ed Tanousbb679322022-05-16 16:10:00 -0700435
436 // presence sensors are optional
437 if (presenceConfig != sensorData->end())
438 {
439 auto findPolarity = presenceConfig->second.find("Polarity");
440 auto findPinName = presenceConfig->second.find("PinName");
441
442 if (findPinName == presenceConfig->second.end() ||
443 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800444 {
Ed Tanousbb679322022-05-16 16:10:00 -0700445 std::cerr << "Malformed Presence Configuration\n";
James Feistde5e9702019-09-18 16:13:02 -0700446 }
Ed Tanousbb679322022-05-16 16:10:00 -0700447 else
James Feistde5e9702019-09-18 16:13:02 -0700448 {
Ed Tanousbb679322022-05-16 16:10:00 -0700449 bool inverted =
450 std::get<std::string>(findPolarity->second) == "Low";
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +0200451 const auto* pinName =
452 std::get_if<std::string>(&findPinName->second);
453
454 if (pinName != nullptr)
James Feistde5e9702019-09-18 16:13:02 -0700455 {
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +0200456 auto findPresenceSensor =
457 presenceSensors.find(*pinName);
458 if (findPresenceSensor != presenceSensors.end())
459 {
460 auto p = findPresenceSensor->second.lock();
461 if (p)
462 {
463 presenceSensor = p;
464 }
465 }
466 if (!presenceSensor)
467 {
468 presenceSensor = std::make_shared<PresenceSensor>(
469 *pinName, inverted, io, sensorName);
470 presenceSensors[*pinName] = presenceSensor;
471 }
James Feistde5e9702019-09-18 16:13:02 -0700472 }
473 else
474 {
Ed Tanousbb679322022-05-16 16:10:00 -0700475 std::cerr << "Malformed Presence pinName for sensor "
476 << sensorName << " \n";
James Feistde5e9702019-09-18 16:13:02 -0700477 }
478 }
Ed Tanousbb679322022-05-16 16:10:00 -0700479 }
480 std::optional<RedundancySensor>* redundancy = nullptr;
481 if (fanType == FanTypes::aspeed)
482 {
483 redundancy = &systemRedundancy;
484 }
485
Zev Weissa4d27682022-07-19 15:30:36 -0700486 PowerState powerState = getPowerState(baseConfiguration->second);
Yong Zhao77b3add2021-01-09 04:25:18 +0000487
Ed Tanousbb679322022-05-16 16:10:00 -0700488 constexpr double defaultMaxReading = 25000;
489 constexpr double defaultMinReading = 0;
490 std::pair<double, double> limits =
491 std::make_pair(defaultMinReading, defaultMaxReading);
492
493 auto connector =
Zev Weiss054aad82022-08-18 01:37:34 -0700494 sensorData->find(cfgIntf + std::string(".Connector"));
Ed Tanousbb679322022-05-16 16:10:00 -0700495
496 std::optional<std::string> led;
497 std::string pwmName;
498 fs::path pwmPath;
499
500 // The Mutable parameter is optional, defaulting to false
501 bool isValueMutable = false;
502 if (connector != sensorData->end())
503 {
504 auto findPwm = connector->second.find("Pwm");
505 if (findPwm != connector->second.end())
506 {
507 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
508 findPwm->second);
509 if (!findPwmPath(directory, pwm, pwmPath))
510 {
511 std::cerr << "Connector for " << sensorName
512 << " no pwm channel found!\n";
513 continue;
514 }
515
Patrick Williams779c96a2023-05-10 07:50:42 -0500516 fs::path pwmEnableFile = "pwm" + std::to_string(pwm + 1) +
517 "_enable";
Ed Tanousbb679322022-05-16 16:10:00 -0700518 fs::path enablePath = pwmPath.parent_path() / pwmEnableFile;
519 enablePwm(enablePath);
520
521 /* use pwm name override if found in configuration else
522 * use default */
523 auto findOverride = connector->second.find("PwmName");
524 if (findOverride != connector->second.end())
525 {
526 pwmName = std::visit(VariantToStringVisitor(),
527 findOverride->second);
528 }
529 else
530 {
531 pwmName = "Pwm_" + std::to_string(pwm + 1);
532 }
533
534 // Check PWM sensor mutability
535 auto findMutable = connector->second.find("Mutable");
536 if (findMutable != connector->second.end())
537 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700538 const auto* ptrMutable =
Ed Tanousbb679322022-05-16 16:10:00 -0700539 std::get_if<bool>(&(findMutable->second));
Ed Tanous2049bd22022-07-09 07:20:26 -0700540 if (ptrMutable != nullptr)
Ed Tanousbb679322022-05-16 16:10:00 -0700541 {
542 isValueMutable = *ptrMutable;
543 }
544 }
545 }
546 else
547 {
548 std::cerr << "Connector for " << sensorName
549 << " missing pwm!\n";
550 }
551
552 auto findLED = connector->second.find("LED");
553 if (findLED != connector->second.end())
554 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700555 const auto* ledName =
556 std::get_if<std::string>(&(findLED->second));
Ed Tanousbb679322022-05-16 16:10:00 -0700557 if (ledName == nullptr)
558 {
559 std::cerr << "Wrong format for LED of " << sensorName
560 << "\n";
561 }
562 else
563 {
564 led = *ledName;
565 }
566 }
567 }
568
569 findLimits(limits, baseConfiguration);
Josh Lehan5170fe62022-08-03 13:17:41 -0700570
Justin Ledford9c47bd72022-08-27 01:02:09 +0000571 enableFanInput(path);
572
Josh Lehan5170fe62022-08-03 13:17:41 -0700573 auto& tachSensor = tachSensors[sensorName];
574 tachSensor = nullptr;
575 tachSensor = std::make_shared<TachSensor>(
Ed Tanousbb679322022-05-16 16:10:00 -0700576 path.string(), baseType, objectServer, dbusConnection,
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +0200577 presenceSensor, redundancy, io, sensorName,
Ed Tanousbb679322022-05-16 16:10:00 -0700578 std::move(sensorThresholds), *interfacePath, limits, powerState,
579 led);
Josh Lehan5170fe62022-08-03 13:17:41 -0700580 tachSensor->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700581
582 if (!pwmPath.empty() && fs::exists(pwmPath) &&
Ed Tanous2049bd22022-07-09 07:20:26 -0700583 (pwmSensors.count(pwmPath) == 0U))
Ed Tanousbb679322022-05-16 16:10:00 -0700584 {
585 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
586 pwmName, pwmPath, dbusConnection, objectServer,
587 *interfacePath, "Fan", isValueMutable);
588 }
589 }
590
591 createRedundancySensor(tachSensors, dbusConnection, objectServer);
Patrick Williams597e8422023-10-20 11:19:01 -0500592 });
James Feistde5e9702019-09-18 16:13:02 -0700593 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700594 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
595 retries);
James Feist6714a252018-09-10 15:26:18 -0700596}
597
James Feistb6c0b912019-07-09 12:21:44 -0700598int main()
James Feist6714a252018-09-10 15:26:18 -0700599{
Ed Tanous1f978632023-02-28 18:16:39 -0800600 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700601 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700602 sdbusplus::asio::object_server objectServer(systemBus, true);
603
604 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanousd9067252022-10-22 15:35:52 -0700605 objectServer.add_manager("/xyz/openbmc_project/control");
Lei YUc2f83fe2022-12-02 14:49:47 +0800606 objectServer.add_manager("/xyz/openbmc_project/inventory");
James Feist6714a252018-09-10 15:26:18 -0700607 systemBus->request_name("xyz.openbmc_project.FanSensor");
Josh Lehan5170fe62022-08-03 13:17:41 -0700608 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700609 tachSensors;
610 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
611 pwmSensors;
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +0200612 boost::container::flat_map<std::string, std::weak_ptr<PresenceSensor>>
613 presenceSensors;
James Feist5591cf082020-07-15 16:44:54 -0700614 auto sensorsChanged =
615 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700616
Ed Tanous83db50c2023-03-01 10:20:24 -0800617 boost::asio::post(io, [&]() {
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +0200618 createSensors(io, objectServer, tachSensors, pwmSensors,
619 presenceSensors, systemBus, nullptr);
James Feist6714a252018-09-10 15:26:18 -0700620 });
621
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700622 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500623 std::function<void(sdbusplus::message_t&)> eventHandler =
624 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700625 if (message.is_method_error())
626 {
627 std::cerr << "callback method error\n";
628 return;
629 }
630 sensorsChanged->insert(message.get_path());
631 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800632 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700633
634 filterTimer.async_wait([&](const boost::system::error_code& ec) {
635 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700636 {
Ed Tanousbb679322022-05-16 16:10:00 -0700637 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700638 return;
639 }
Ed Tanousbb679322022-05-16 16:10:00 -0700640 if (ec)
641 {
642 std::cerr << "timer error\n";
643 return;
644 }
Patrick Rudolph8b34f2c2024-07-16 11:38:45 +0200645 createSensors(io, objectServer, tachSensors, pwmSensors,
646 presenceSensors, systemBus, sensorsChanged, 5);
Ed Tanousbb679322022-05-16 16:10:00 -0700647 });
648 };
James Feist6714a252018-09-10 15:26:18 -0700649
Zev Weiss214d9712022-08-12 12:54:31 -0700650 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
651 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700652
James Feistdc6c55f2018-10-31 12:53:20 -0700653 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500654 std::function<void(sdbusplus::message_t&)> redundancyHandler =
655 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700656 createRedundancySensor(tachSensors, systemBus, objectServer);
657 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500658 auto match = std::make_unique<sdbusplus::bus::match_t>(
659 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700660 "type='signal',member='PropertiesChanged',path_namespace='" +
661 std::string(inventoryPath) + "',arg0namespace='" +
662 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700663 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700664 matches.emplace_back(std::move(match));
665
Bruce Lee1263c3d2021-06-04 15:16:33 +0800666 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700667 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700668 return 0;
James Feist6714a252018-09-10 15:26:18 -0700669}