blob: a7b8b72dd49cdaf4923f66650f59800f0fc50ae4 [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},
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
James Feist95b079b2018-11-21 09:28:00 -080089FanTypes getFanType(const fs::path& parentPath)
90{
Chris Sides9a472e82023-04-03 15:13:37 -050091 fs::path linkPath = parentPath / "of_node";
Zhikui Ren39963222023-05-04 17:06:50 -070092 if (!fs::exists(linkPath))
93 {
94 return FanTypes::i2c;
95 }
Chris Sides9a472e82023-04-03 15:13:37 -050096
Zhikui Ren39963222023-05-04 17:06:50 -070097 std::string canonical = fs::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}
Jeff Linabf91de2020-12-23 10:55:42 +0800123void enablePwm(const fs::path& filePath)
124{
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}
Howard Chiuddf25d12021-12-02 15:14:44 +0800139bool findPwmfanPath(unsigned int configPwmfanIndex, fs::path& pwmPath)
140{
141 /* Search PWM since pwm-fan had separated
142 * PWM from tach directory and 1 channel only*/
143 std::vector<fs::path> pwmfanPaths;
144 std::string pwnfanDevName("pwm-fan");
145
146 pwnfanDevName += std::to_string(configPwmfanIndex);
147
148 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwmfanPaths))
149 {
150 std::cerr << "No PWMs are found!\n";
151 return false;
152 }
153 for (const auto& path : pwmfanPaths)
154 {
155 std::error_code ec;
156 fs::path link = fs::read_symlink(path.parent_path() / "device", ec);
157
158 if (ec)
159 {
160 std::cerr << "read_symlink() failed: " << ec.message() << " ("
161 << ec.value() << ")\n";
162 continue;
163 }
164
165 if (link.filename().string() == pwnfanDevName)
166 {
167 pwmPath = path;
168 return true;
169 }
170 }
171 return false;
172}
173bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath)
174{
175 std::error_code ec;
176
177 /* Assuming PWM file is appeared in the same directory as fanX_input */
178 auto path = directory / ("pwm" + std::to_string(pwm + 1));
179 bool exists = fs::exists(path, ec);
180
181 if (ec || !exists)
182 {
183 /* PWM file not exist or error happened */
184 if (ec)
185 {
186 std::cerr << "exists() failed: " << ec.message() << " ("
187 << ec.value() << ")\n";
188 }
189 /* try search form pwm-fanX directory */
190 return findPwmfanPath(pwm, pwmPath);
191 }
192
193 pwmPath = path;
194 return true;
195}
Justin Ledford9c47bd72022-08-27 01:02:09 +0000196
197// The argument to this function should be the fanN_input file that we want to
198// enable. The function will locate the corresponding fanN_enable file if it
199// exists. Note that some drivers don't provide this file if the sensors are
200// always enabled.
201void enableFanInput(const fs::path& fanInputPath)
202{
203 std::error_code ec;
204 std::string path(fanInputPath.string());
205 boost::replace_last(path, "input", "enable");
206
207 bool exists = fs::exists(path, ec);
208 if (ec || !exists)
209 {
210 return;
211 }
212
213 std::fstream enableFile(path, std::ios::out);
214 if (!enableFile.good())
215 {
216 return;
217 }
218 enableFile << 1;
219}
220
Kuiying Wangd5407412020-09-09 16:06:56 +0800221void createRedundancySensor(
Josh Lehan5170fe62022-08-03 13:17:41 -0700222 const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
Kuiying Wangd5407412020-09-09 16:06:56 +0800223 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700224 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800225 sdbusplus::asio::object_server& objectServer)
226{
Kuiying Wangd5407412020-09-09 16:06:56 +0800227 conn->async_method_call(
228 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700229 const ManagedObjectType& managedObj) {
Ed Tanousbb679322022-05-16 16:10:00 -0700230 if (ec)
231 {
232 std::cerr << "Error calling entity manager \n";
233 return;
234 }
Zev Weiss77636ec2022-08-12 18:21:01 -0700235 for (const auto& [path, interfaces] : managedObj)
Ed Tanousbb679322022-05-16 16:10:00 -0700236 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700237 for (const auto& [intf, cfg] : interfaces)
Kuiying Wangd5407412020-09-09 16:06:56 +0800238 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700239 if (intf == redundancyConfiguration)
Kuiying Wangd5407412020-09-09 16:06:56 +0800240 {
Ed Tanousbb679322022-05-16 16:10:00 -0700241 // currently only support one
Zev Weiss77636ec2022-08-12 18:21:01 -0700242 auto findCount = cfg.find("AllowedFailures");
243 if (findCount == cfg.end())
Kuiying Wangd5407412020-09-09 16:06:56 +0800244 {
Ed Tanousbb679322022-05-16 16:10:00 -0700245 std::cerr << "Malformed redundancy record \n";
Kuiying Wangd5407412020-09-09 16:06:56 +0800246 return;
247 }
Ed Tanousbb679322022-05-16 16:10:00 -0700248 std::vector<std::string> sensorList;
249
Zev Weiss77636ec2022-08-12 18:21:01 -0700250 for (const auto& [name, sensor] : sensors)
Ed Tanousbb679322022-05-16 16:10:00 -0700251 {
252 sensorList.push_back(
253 "/xyz/openbmc_project/sensors/fan_tach/" +
Zev Weiss77636ec2022-08-12 18:21:01 -0700254 sensor->name);
Ed Tanousbb679322022-05-16 16:10:00 -0700255 }
256 systemRedundancy.reset();
Zev Weiss77636ec2022-08-12 18:21:01 -0700257 systemRedundancy.emplace(
258 RedundancySensor(std::get<uint64_t>(findCount->second),
259 sensorList, objectServer, path));
Ed Tanousbb679322022-05-16 16:10:00 -0700260
261 return;
Kuiying Wangd5407412020-09-09 16:06:56 +0800262 }
263 }
Ed Tanousbb679322022-05-16 16:10:00 -0700264 }
Patrick Williams597e8422023-10-20 11:19:01 -0500265 },
Nan Zhou3e620af2022-09-20 22:28:31 +0000266 "xyz.openbmc_project.EntityManager", "/xyz/openbmc_project/inventory",
Kuiying Wangd5407412020-09-09 16:06:56 +0800267 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
268}
269
James Feist6714a252018-09-10 15:26:18 -0700270void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800271 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Josh Lehan5170fe62022-08-03 13:17:41 -0700272 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700273 tachSensors,
274 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
275 pwmSensors,
276 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700277 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700278 sensorsChanged,
279 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700280{
James Feistde5e9702019-09-18 16:13:02 -0700281 auto getter = std::make_shared<GetSensorConfiguration>(
282 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700283 [&io, &objectServer, &tachSensors, &pwmSensors, &dbusConnection,
284 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700285 bool firstScan = sensorsChanged == nullptr;
286 std::vector<fs::path> paths;
287 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
288 {
289 std::cerr << "No fan sensors in system\n";
290 return;
291 }
292
293 // iterate through all found fan sensors, and try to match them with
294 // configuration
295 for (const auto& path : paths)
296 {
297 std::smatch match;
298 std::string pathStr = path.string();
299
300 std::regex_search(pathStr, match, inputRegex);
301 std::string indexStr = *(match.begin() + 1);
302
303 fs::path directory = path.parent_path();
304 FanTypes fanType = getFanType(directory);
Zev Weiss054aad82022-08-18 01:37:34 -0700305 std::string cfgIntf = configInterfaceName(sensorTypes[fanType]);
Ed Tanousbb679322022-05-16 16:10:00 -0700306
307 // convert to 0 based
308 size_t index = std::stoul(indexStr) - 1;
309
310 const char* baseType = nullptr;
311 const SensorData* sensorData = nullptr;
312 const std::string* interfacePath = nullptr;
313 const SensorBaseConfiguration* baseConfiguration = nullptr;
Zev Weiss77636ec2022-08-12 18:21:01 -0700314 for (const auto& [path, cfgData] : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800315 {
Ed Tanousbb679322022-05-16 16:10:00 -0700316 // find the base of the configuration to see if indexes
317 // match
Zev Weiss054aad82022-08-18 01:37:34 -0700318 auto sensorBaseFind = cfgData.find(cfgIntf);
Zev Weiss77636ec2022-08-12 18:21:01 -0700319 if (sensorBaseFind == cfgData.end())
James Feist95b079b2018-11-21 09:28:00 -0800320 {
Ed Tanousbb679322022-05-16 16:10:00 -0700321 continue;
322 }
323
324 baseConfiguration = &(*sensorBaseFind);
Zev Weiss77636ec2022-08-12 18:21:01 -0700325 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700326 baseType = sensorTypes[fanType];
327
328 auto findIndex = baseConfiguration->second.find("Index");
329 if (findIndex == baseConfiguration->second.end())
330 {
331 std::cerr << baseConfiguration->first << " missing index\n";
332 continue;
333 }
334 unsigned int configIndex = std::visit(
335 VariantToUnsignedIntVisitor(), findIndex->second);
336 if (configIndex != index)
337 {
338 continue;
339 }
Chris Sidesc361e222023-04-05 15:18:20 -0500340 if (fanType == FanTypes::aspeed ||
341 fanType == FanTypes::nuvoton || fanType == FanTypes::hpe)
Ed Tanousbb679322022-05-16 16:10:00 -0700342 {
Chris Sidesc361e222023-04-05 15:18:20 -0500343 // there will be only 1 aspeed or nuvoton or hpe sensor
Chris Sides3d5260d2023-04-10 15:45:00 -0500344 // object in sysfs, we found the fan
Zev Weiss77636ec2022-08-12 18:21:01 -0700345 sensorData = &cfgData;
Ed Tanousbb679322022-05-16 16:10:00 -0700346 break;
347 }
348 if (fanType == FanTypes::i2c)
349 {
Akshit Shah03d333e2023-08-23 22:14:28 +0000350 std::string deviceName =
Ed Tanousbb679322022-05-16 16:10:00 -0700351 fs::read_symlink(directory / "device").filename();
352
Akshit Shah03d333e2023-08-23 22:14:28 +0000353 size_t bus = 0;
354 size_t addr = 0;
355 if (!getDeviceBusAddr(deviceName, bus, addr))
James Feistde5e9702019-09-18 16:13:02 -0700356 {
Akshit Shah03d333e2023-08-23 22:14:28 +0000357 continue;
James Feistde5e9702019-09-18 16:13:02 -0700358 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800359
Ed Tanousbb679322022-05-16 16:10:00 -0700360 auto findBus = baseConfiguration->second.find("Bus");
361 auto findAddress =
362 baseConfiguration->second.find("Address");
363 if (findBus == baseConfiguration->second.end() ||
364 findAddress == baseConfiguration->second.end())
James Feistde5e9702019-09-18 16:13:02 -0700365 {
366 std::cerr << baseConfiguration->first
Ed Tanousbb679322022-05-16 16:10:00 -0700367 << " missing bus or address\n";
James Feistde5e9702019-09-18 16:13:02 -0700368 continue;
369 }
Ed Tanousbb679322022-05-16 16:10:00 -0700370 unsigned int configBus = std::visit(
371 VariantToUnsignedIntVisitor(), findBus->second);
372 unsigned int configAddress = std::visit(
373 VariantToUnsignedIntVisitor(), findAddress->second);
374
Akshit Shah03d333e2023-08-23 22:14:28 +0000375 if (configBus == bus && configAddress == addr)
James Feistde5e9702019-09-18 16:13:02 -0700376 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700377 sensorData = &cfgData;
James Feistde5e9702019-09-18 16:13:02 -0700378 break;
379 }
Ed Tanousbb679322022-05-16 16:10:00 -0700380 }
381 }
382 if (sensorData == nullptr)
383 {
384 std::cerr << "failed to find match for " << path.string()
385 << "\n";
386 continue;
387 }
388
389 auto findSensorName = baseConfiguration->second.find("Name");
390
391 if (findSensorName == baseConfiguration->second.end())
392 {
393 std::cerr << "could not determine configuration name for "
394 << path.string() << "\n";
395 continue;
396 }
397 std::string sensorName =
398 std::get<std::string>(findSensorName->second);
399
400 // on rescans, only update sensors we were signaled by
401 auto findSensor = tachSensors.find(sensorName);
402 if (!firstScan && findSensor != tachSensors.end())
403 {
404 bool found = false;
405 for (auto it = sensorsChanged->begin();
406 it != sensorsChanged->end(); it++)
407 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700408 if (it->ends_with(findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700409 {
Ed Tanousbb679322022-05-16 16:10:00 -0700410 sensorsChanged->erase(it);
411 findSensor->second = nullptr;
412 found = true;
413 break;
James Feistde5e9702019-09-18 16:13:02 -0700414 }
415 }
Ed Tanousbb679322022-05-16 16:10:00 -0700416 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700417 {
James Feist95b079b2018-11-21 09:28:00 -0800418 continue;
419 }
Ed Tanousbb679322022-05-16 16:10:00 -0700420 }
421 std::vector<thresholds::Threshold> sensorThresholds;
422 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
423 {
424 std::cerr << "error populating thresholds for " << sensorName
425 << "\n";
426 }
James Feist95b079b2018-11-21 09:28:00 -0800427
Ed Tanousbb679322022-05-16 16:10:00 -0700428 auto presenceConfig =
Zev Weiss054aad82022-08-18 01:37:34 -0700429 sensorData->find(cfgIntf + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800430
Ed Tanousbb679322022-05-16 16:10:00 -0700431 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
432
433 // presence sensors are optional
434 if (presenceConfig != sensorData->end())
435 {
436 auto findPolarity = presenceConfig->second.find("Polarity");
437 auto findPinName = presenceConfig->second.find("PinName");
438
439 if (findPinName == presenceConfig->second.end() ||
440 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800441 {
Ed Tanousbb679322022-05-16 16:10:00 -0700442 std::cerr << "Malformed Presence Configuration\n";
James Feistde5e9702019-09-18 16:13:02 -0700443 }
Ed Tanousbb679322022-05-16 16:10:00 -0700444 else
James Feistde5e9702019-09-18 16:13:02 -0700445 {
Ed Tanousbb679322022-05-16 16:10:00 -0700446 bool inverted =
447 std::get<std::string>(findPolarity->second) == "Low";
Ed Tanous2049bd22022-07-09 07:20:26 -0700448 if (const auto* pinName =
Ed Tanousbb679322022-05-16 16:10:00 -0700449 std::get_if<std::string>(&findPinName->second))
James Feistde5e9702019-09-18 16:13:02 -0700450 {
Ed Tanousbb679322022-05-16 16:10:00 -0700451 presenceSensor = std::make_unique<PresenceSensor>(
452 *pinName, inverted, io, sensorName);
James Feistde5e9702019-09-18 16:13:02 -0700453 }
454 else
455 {
Ed Tanousbb679322022-05-16 16:10:00 -0700456 std::cerr << "Malformed Presence pinName for sensor "
457 << sensorName << " \n";
James Feistde5e9702019-09-18 16:13:02 -0700458 }
459 }
Ed Tanousbb679322022-05-16 16:10:00 -0700460 }
461 std::optional<RedundancySensor>* redundancy = nullptr;
462 if (fanType == FanTypes::aspeed)
463 {
464 redundancy = &systemRedundancy;
465 }
466
Zev Weissa4d27682022-07-19 15:30:36 -0700467 PowerState powerState = getPowerState(baseConfiguration->second);
Yong Zhao77b3add2021-01-09 04:25:18 +0000468
Ed Tanousbb679322022-05-16 16:10:00 -0700469 constexpr double defaultMaxReading = 25000;
470 constexpr double defaultMinReading = 0;
471 std::pair<double, double> limits =
472 std::make_pair(defaultMinReading, defaultMaxReading);
473
474 auto connector =
Zev Weiss054aad82022-08-18 01:37:34 -0700475 sensorData->find(cfgIntf + std::string(".Connector"));
Ed Tanousbb679322022-05-16 16:10:00 -0700476
477 std::optional<std::string> led;
478 std::string pwmName;
479 fs::path pwmPath;
480
481 // The Mutable parameter is optional, defaulting to false
482 bool isValueMutable = false;
483 if (connector != sensorData->end())
484 {
485 auto findPwm = connector->second.find("Pwm");
486 if (findPwm != connector->second.end())
487 {
488 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
489 findPwm->second);
490 if (!findPwmPath(directory, pwm, pwmPath))
491 {
492 std::cerr << "Connector for " << sensorName
493 << " no pwm channel found!\n";
494 continue;
495 }
496
Patrick Williams779c96a2023-05-10 07:50:42 -0500497 fs::path pwmEnableFile = "pwm" + std::to_string(pwm + 1) +
498 "_enable";
Ed Tanousbb679322022-05-16 16:10:00 -0700499 fs::path enablePath = pwmPath.parent_path() / pwmEnableFile;
500 enablePwm(enablePath);
501
502 /* use pwm name override if found in configuration else
503 * use default */
504 auto findOverride = connector->second.find("PwmName");
505 if (findOverride != connector->second.end())
506 {
507 pwmName = std::visit(VariantToStringVisitor(),
508 findOverride->second);
509 }
510 else
511 {
512 pwmName = "Pwm_" + std::to_string(pwm + 1);
513 }
514
515 // Check PWM sensor mutability
516 auto findMutable = connector->second.find("Mutable");
517 if (findMutable != connector->second.end())
518 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700519 const auto* ptrMutable =
Ed Tanousbb679322022-05-16 16:10:00 -0700520 std::get_if<bool>(&(findMutable->second));
Ed Tanous2049bd22022-07-09 07:20:26 -0700521 if (ptrMutable != nullptr)
Ed Tanousbb679322022-05-16 16:10:00 -0700522 {
523 isValueMutable = *ptrMutable;
524 }
525 }
526 }
527 else
528 {
529 std::cerr << "Connector for " << sensorName
530 << " missing pwm!\n";
531 }
532
533 auto findLED = connector->second.find("LED");
534 if (findLED != connector->second.end())
535 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700536 const auto* ledName =
537 std::get_if<std::string>(&(findLED->second));
Ed Tanousbb679322022-05-16 16:10:00 -0700538 if (ledName == nullptr)
539 {
540 std::cerr << "Wrong format for LED of " << sensorName
541 << "\n";
542 }
543 else
544 {
545 led = *ledName;
546 }
547 }
548 }
549
550 findLimits(limits, baseConfiguration);
Josh Lehan5170fe62022-08-03 13:17:41 -0700551
Justin Ledford9c47bd72022-08-27 01:02:09 +0000552 enableFanInput(path);
553
Josh Lehan5170fe62022-08-03 13:17:41 -0700554 auto& tachSensor = tachSensors[sensorName];
555 tachSensor = nullptr;
556 tachSensor = std::make_shared<TachSensor>(
Ed Tanousbb679322022-05-16 16:10:00 -0700557 path.string(), baseType, objectServer, dbusConnection,
558 std::move(presenceSensor), redundancy, io, sensorName,
559 std::move(sensorThresholds), *interfacePath, limits, powerState,
560 led);
Josh Lehan5170fe62022-08-03 13:17:41 -0700561 tachSensor->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700562
563 if (!pwmPath.empty() && fs::exists(pwmPath) &&
Ed Tanous2049bd22022-07-09 07:20:26 -0700564 (pwmSensors.count(pwmPath) == 0U))
Ed Tanousbb679322022-05-16 16:10:00 -0700565 {
566 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
567 pwmName, pwmPath, dbusConnection, objectServer,
568 *interfacePath, "Fan", isValueMutable);
569 }
570 }
571
572 createRedundancySensor(tachSensors, dbusConnection, objectServer);
Patrick Williams597e8422023-10-20 11:19:01 -0500573 });
James Feistde5e9702019-09-18 16:13:02 -0700574 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700575 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
576 retries);
James Feist6714a252018-09-10 15:26:18 -0700577}
578
James Feistb6c0b912019-07-09 12:21:44 -0700579int main()
James Feist6714a252018-09-10 15:26:18 -0700580{
Ed Tanous1f978632023-02-28 18:16:39 -0800581 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700582 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700583 sdbusplus::asio::object_server objectServer(systemBus, true);
584
585 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanousd9067252022-10-22 15:35:52 -0700586 objectServer.add_manager("/xyz/openbmc_project/control");
Lei YUc2f83fe2022-12-02 14:49:47 +0800587 objectServer.add_manager("/xyz/openbmc_project/inventory");
James Feist6714a252018-09-10 15:26:18 -0700588 systemBus->request_name("xyz.openbmc_project.FanSensor");
Josh Lehan5170fe62022-08-03 13:17:41 -0700589 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700590 tachSensors;
591 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
592 pwmSensors;
James Feist5591cf082020-07-15 16:44:54 -0700593 auto sensorsChanged =
594 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700595
Ed Tanous83db50c2023-03-01 10:20:24 -0800596 boost::asio::post(io, [&]() {
James Feist6714a252018-09-10 15:26:18 -0700597 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
598 nullptr);
599 });
600
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700601 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500602 std::function<void(sdbusplus::message_t&)> eventHandler =
603 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700604 if (message.is_method_error())
605 {
606 std::cerr << "callback method error\n";
607 return;
608 }
609 sensorsChanged->insert(message.get_path());
610 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800611 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700612
613 filterTimer.async_wait([&](const boost::system::error_code& ec) {
614 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700615 {
Ed Tanousbb679322022-05-16 16:10:00 -0700616 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700617 return;
618 }
Ed Tanousbb679322022-05-16 16:10:00 -0700619 if (ec)
620 {
621 std::cerr << "timer error\n";
622 return;
623 }
624 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
625 sensorsChanged, 5);
626 });
627 };
James Feist6714a252018-09-10 15:26:18 -0700628
Zev Weiss214d9712022-08-12 12:54:31 -0700629 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
630 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700631
James Feistdc6c55f2018-10-31 12:53:20 -0700632 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500633 std::function<void(sdbusplus::message_t&)> redundancyHandler =
634 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700635 createRedundancySensor(tachSensors, systemBus, objectServer);
636 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500637 auto match = std::make_unique<sdbusplus::bus::match_t>(
638 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700639 "type='signal',member='PropertiesChanged',path_namespace='" +
640 std::string(inventoryPath) + "',arg0namespace='" +
641 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700642 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700643 matches.emplace_back(std::move(match));
644
Bruce Lee1263c3d2021-06-04 15:16:33 +0800645 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700646 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700647 return 0;
James Feist6714a252018-09-10 15:26:18 -0700648}