blob: 32022c1714e77b8277d4f7f86141a4ce64f61b60 [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"
19#include "Utils.hpp"
20#include "VariantVisitors.hpp"
21
James Feist6714a252018-09-10 15:26:18 -070022#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070024#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070025#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27#include <sdbusplus/bus/match.hpp>
28
29#include <array>
James Feist24f02f22019-04-15 11:05:39 -070030#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070031#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <functional>
33#include <memory>
34#include <optional>
James Feist6714a252018-09-10 15:26:18 -070035#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <string>
37#include <utility>
38#include <variant>
39#include <vector>
James Feist6714a252018-09-10 15:26:18 -070040
James Feistcf3bce62019-01-08 10:07:19 -080041namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080042
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000043// The following two structures need to be consistent
Chris Sidesc361e222023-04-05 15:18:20 -050044static auto sensorTypes{std::to_array<const char*>(
45 {"AspeedFan", "I2CFan", "NuvotonFan", "HPEFan"})};
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000046
47enum FanTypes
48{
49 aspeed = 0,
50 i2c,
51 nuvoton,
Chris Sidesc361e222023-04-05 15:18:20 -050052 hpe,
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000053 max,
54};
55
56static_assert(std::tuple_size<decltype(sensorTypes)>::value == FanTypes::max,
57 "sensorTypes element number is not equal to FanTypes number");
58
James Feistdc6c55f2018-10-31 12:53:20 -070059constexpr const char* redundancyConfiguration =
60 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070061static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070062
James Feistdc6c55f2018-10-31 12:53:20 -070063// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070064std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080065
Chris Sides3d5260d2023-04-10 15:45:00 -050066static const std::map<std::string, FanTypes> compatibleFanTypes = {
67 {"aspeed,ast2400-pwm-tacho", FanTypes::aspeed},
68 {"aspeed,ast2500-pwm-tacho", FanTypes::aspeed},
Chris Sidesc361e222023-04-05 15:18:20 -050069 {"nuvoton,npcm750-pwm-fan", FanTypes::nuvoton},
70 {"hpe,gxp-fan-ctrl", FanTypes::hpe}
Chris Sides3d5260d2023-04-10 15:45:00 -050071 // add compatible string here for new fan type
72};
73
James Feist95b079b2018-11-21 09:28:00 -080074FanTypes getFanType(const fs::path& parentPath)
75{
Chris Sides9a472e82023-04-03 15:13:37 -050076 fs::path linkPath = parentPath / "of_node";
77 std::string canonical = fs::canonical(linkPath);
78
79 std::string compatiblePath = canonical + "/compatible";
80 std::ifstream compatibleStream(compatiblePath);
81
Chris Sides3d5260d2023-04-10 15:45:00 -050082 if (!compatibleStream)
James Feist95b079b2018-11-21 09:28:00 -080083 {
Chris Sides9a472e82023-04-03 15:13:37 -050084 std::cerr << "Error opening " << compatiblePath << "\n";
Chris Sides3d5260d2023-04-10 15:45:00 -050085 return FanTypes::i2c;
James Feist95b079b2018-11-21 09:28:00 -080086 }
Chris Sides9a472e82023-04-03 15:13:37 -050087
88 std::string compatibleString;
Chris Sides3d5260d2023-04-10 15:45:00 -050089 while (std::getline(compatibleStream, compatibleString))
Peter Lundgren8843b622019-09-12 10:33:41 -070090 {
Chris Sides9a472e82023-04-03 15:13:37 -050091 compatibleString.pop_back(); // trim EOL before comparisons
92
Chris Sides3d5260d2023-04-10 15:45:00 -050093 std::map<std::string, FanTypes>::const_iterator compatibleIterator =
94 compatibleFanTypes.find(compatibleString);
95
96 if (compatibleIterator != compatibleFanTypes.end())
Chris Sides9a472e82023-04-03 15:13:37 -050097 {
Chris Sides3d5260d2023-04-10 15:45:00 -050098 return compatibleIterator->second;
Chris Sides9a472e82023-04-03 15:13:37 -050099 }
Peter Lundgren8843b622019-09-12 10:33:41 -0700100 }
Chris Sides9a472e82023-04-03 15:13:37 -0500101
James Feist95b079b2018-11-21 09:28:00 -0800102 return FanTypes::i2c;
103}
Jeff Linabf91de2020-12-23 10:55:42 +0800104void enablePwm(const fs::path& filePath)
105{
106 std::fstream enableFile(filePath, std::ios::in | std::ios::out);
107 if (!enableFile.good())
108 {
109 std::cerr << "Error read/write " << filePath << "\n";
110 return;
111 }
James Feistdc6c55f2018-10-31 12:53:20 -0700112
Jeff Linabf91de2020-12-23 10:55:42 +0800113 std::string regulateMode;
114 std::getline(enableFile, regulateMode);
115 if (regulateMode == "0")
116 {
117 enableFile << 1;
118 }
119}
Howard Chiuddf25d12021-12-02 15:14:44 +0800120bool findPwmfanPath(unsigned int configPwmfanIndex, fs::path& pwmPath)
121{
122 /* Search PWM since pwm-fan had separated
123 * PWM from tach directory and 1 channel only*/
124 std::vector<fs::path> pwmfanPaths;
125 std::string pwnfanDevName("pwm-fan");
126
127 pwnfanDevName += std::to_string(configPwmfanIndex);
128
129 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwmfanPaths))
130 {
131 std::cerr << "No PWMs are found!\n";
132 return false;
133 }
134 for (const auto& path : pwmfanPaths)
135 {
136 std::error_code ec;
137 fs::path link = fs::read_symlink(path.parent_path() / "device", ec);
138
139 if (ec)
140 {
141 std::cerr << "read_symlink() failed: " << ec.message() << " ("
142 << ec.value() << ")\n";
143 continue;
144 }
145
146 if (link.filename().string() == pwnfanDevName)
147 {
148 pwmPath = path;
149 return true;
150 }
151 }
152 return false;
153}
154bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath)
155{
156 std::error_code ec;
157
158 /* Assuming PWM file is appeared in the same directory as fanX_input */
159 auto path = directory / ("pwm" + std::to_string(pwm + 1));
160 bool exists = fs::exists(path, ec);
161
162 if (ec || !exists)
163 {
164 /* PWM file not exist or error happened */
165 if (ec)
166 {
167 std::cerr << "exists() failed: " << ec.message() << " ("
168 << ec.value() << ")\n";
169 }
170 /* try search form pwm-fanX directory */
171 return findPwmfanPath(pwm, pwmPath);
172 }
173
174 pwmPath = path;
175 return true;
176}
Justin Ledford9c47bd72022-08-27 01:02:09 +0000177
178// The argument to this function should be the fanN_input file that we want to
179// enable. The function will locate the corresponding fanN_enable file if it
180// exists. Note that some drivers don't provide this file if the sensors are
181// always enabled.
182void enableFanInput(const fs::path& fanInputPath)
183{
184 std::error_code ec;
185 std::string path(fanInputPath.string());
186 boost::replace_last(path, "input", "enable");
187
188 bool exists = fs::exists(path, ec);
189 if (ec || !exists)
190 {
191 return;
192 }
193
194 std::fstream enableFile(path, std::ios::out);
195 if (!enableFile.good())
196 {
197 return;
198 }
199 enableFile << 1;
200}
201
Kuiying Wangd5407412020-09-09 16:06:56 +0800202void createRedundancySensor(
Josh Lehan5170fe62022-08-03 13:17:41 -0700203 const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
Kuiying Wangd5407412020-09-09 16:06:56 +0800204 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700205 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800206 sdbusplus::asio::object_server& objectServer)
207{
208
209 conn->async_method_call(
210 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700211 const ManagedObjectType& managedObj) {
Ed Tanousbb679322022-05-16 16:10:00 -0700212 if (ec)
213 {
214 std::cerr << "Error calling entity manager \n";
215 return;
216 }
Zev Weiss77636ec2022-08-12 18:21:01 -0700217 for (const auto& [path, interfaces] : managedObj)
Ed Tanousbb679322022-05-16 16:10:00 -0700218 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700219 for (const auto& [intf, cfg] : interfaces)
Kuiying Wangd5407412020-09-09 16:06:56 +0800220 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700221 if (intf == redundancyConfiguration)
Kuiying Wangd5407412020-09-09 16:06:56 +0800222 {
Ed Tanousbb679322022-05-16 16:10:00 -0700223 // currently only support one
Zev Weiss77636ec2022-08-12 18:21:01 -0700224 auto findCount = cfg.find("AllowedFailures");
225 if (findCount == cfg.end())
Kuiying Wangd5407412020-09-09 16:06:56 +0800226 {
Ed Tanousbb679322022-05-16 16:10:00 -0700227 std::cerr << "Malformed redundancy record \n";
Kuiying Wangd5407412020-09-09 16:06:56 +0800228 return;
229 }
Ed Tanousbb679322022-05-16 16:10:00 -0700230 std::vector<std::string> sensorList;
231
Zev Weiss77636ec2022-08-12 18:21:01 -0700232 for (const auto& [name, sensor] : sensors)
Ed Tanousbb679322022-05-16 16:10:00 -0700233 {
234 sensorList.push_back(
235 "/xyz/openbmc_project/sensors/fan_tach/" +
Zev Weiss77636ec2022-08-12 18:21:01 -0700236 sensor->name);
Ed Tanousbb679322022-05-16 16:10:00 -0700237 }
238 systemRedundancy.reset();
Zev Weiss77636ec2022-08-12 18:21:01 -0700239 systemRedundancy.emplace(
240 RedundancySensor(std::get<uint64_t>(findCount->second),
241 sensorList, objectServer, path));
Ed Tanousbb679322022-05-16 16:10:00 -0700242
243 return;
Kuiying Wangd5407412020-09-09 16:06:56 +0800244 }
245 }
Ed Tanousbb679322022-05-16 16:10:00 -0700246 }
Kuiying Wangd5407412020-09-09 16:06:56 +0800247 },
Nan Zhou3e620af2022-09-20 22:28:31 +0000248 "xyz.openbmc_project.EntityManager", "/xyz/openbmc_project/inventory",
Kuiying Wangd5407412020-09-09 16:06:56 +0800249 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
250}
251
James Feist6714a252018-09-10 15:26:18 -0700252void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800253 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Josh Lehan5170fe62022-08-03 13:17:41 -0700254 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700255 tachSensors,
256 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
257 pwmSensors,
258 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700259 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700260 sensorsChanged,
261 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700262{
James Feistde5e9702019-09-18 16:13:02 -0700263 auto getter = std::make_shared<GetSensorConfiguration>(
264 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700265 [&io, &objectServer, &tachSensors, &pwmSensors, &dbusConnection,
266 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700267 bool firstScan = sensorsChanged == nullptr;
268 std::vector<fs::path> paths;
269 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
270 {
271 std::cerr << "No fan sensors in system\n";
272 return;
273 }
274
275 // iterate through all found fan sensors, and try to match them with
276 // configuration
277 for (const auto& path : paths)
278 {
279 std::smatch match;
280 std::string pathStr = path.string();
281
282 std::regex_search(pathStr, match, inputRegex);
283 std::string indexStr = *(match.begin() + 1);
284
285 fs::path directory = path.parent_path();
286 FanTypes fanType = getFanType(directory);
Zev Weiss054aad82022-08-18 01:37:34 -0700287 std::string cfgIntf = configInterfaceName(sensorTypes[fanType]);
Ed Tanousbb679322022-05-16 16:10:00 -0700288
289 // convert to 0 based
290 size_t index = std::stoul(indexStr) - 1;
291
292 const char* baseType = nullptr;
293 const SensorData* sensorData = nullptr;
294 const std::string* interfacePath = nullptr;
295 const SensorBaseConfiguration* baseConfiguration = nullptr;
Zev Weiss77636ec2022-08-12 18:21:01 -0700296 for (const auto& [path, cfgData] : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800297 {
Ed Tanousbb679322022-05-16 16:10:00 -0700298 // find the base of the configuration to see if indexes
299 // match
Zev Weiss054aad82022-08-18 01:37:34 -0700300 auto sensorBaseFind = cfgData.find(cfgIntf);
Zev Weiss77636ec2022-08-12 18:21:01 -0700301 if (sensorBaseFind == cfgData.end())
James Feist95b079b2018-11-21 09:28:00 -0800302 {
Ed Tanousbb679322022-05-16 16:10:00 -0700303 continue;
304 }
305
306 baseConfiguration = &(*sensorBaseFind);
Zev Weiss77636ec2022-08-12 18:21:01 -0700307 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700308 baseType = sensorTypes[fanType];
309
310 auto findIndex = baseConfiguration->second.find("Index");
311 if (findIndex == baseConfiguration->second.end())
312 {
313 std::cerr << baseConfiguration->first << " missing index\n";
314 continue;
315 }
316 unsigned int configIndex = std::visit(
317 VariantToUnsignedIntVisitor(), findIndex->second);
318 if (configIndex != index)
319 {
320 continue;
321 }
Chris Sidesc361e222023-04-05 15:18:20 -0500322 if (fanType == FanTypes::aspeed ||
323 fanType == FanTypes::nuvoton || fanType == FanTypes::hpe)
Ed Tanousbb679322022-05-16 16:10:00 -0700324 {
Chris Sidesc361e222023-04-05 15:18:20 -0500325 // there will be only 1 aspeed or nuvoton or hpe sensor
Chris Sides3d5260d2023-04-10 15:45:00 -0500326 // object in sysfs, we found the fan
Zev Weiss77636ec2022-08-12 18:21:01 -0700327 sensorData = &cfgData;
Ed Tanousbb679322022-05-16 16:10:00 -0700328 break;
329 }
330 if (fanType == FanTypes::i2c)
331 {
332 size_t bus = 0;
333 size_t address = 0;
334
335 std::string link =
336 fs::read_symlink(directory / "device").filename();
337
338 size_t findDash = link.find('-');
339 if (findDash == std::string::npos ||
340 link.size() <= findDash + 1)
James Feistde5e9702019-09-18 16:13:02 -0700341 {
Ed Tanousbb679322022-05-16 16:10:00 -0700342 std::cerr << "Error finding device from symlink";
James Feistde5e9702019-09-18 16:13:02 -0700343 }
Ed Tanousbb679322022-05-16 16:10:00 -0700344 bus = std::stoi(link.substr(0, findDash));
345 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800346
Ed Tanousbb679322022-05-16 16:10:00 -0700347 auto findBus = baseConfiguration->second.find("Bus");
348 auto findAddress =
349 baseConfiguration->second.find("Address");
350 if (findBus == baseConfiguration->second.end() ||
351 findAddress == baseConfiguration->second.end())
James Feistde5e9702019-09-18 16:13:02 -0700352 {
353 std::cerr << baseConfiguration->first
Ed Tanousbb679322022-05-16 16:10:00 -0700354 << " missing bus or address\n";
James Feistde5e9702019-09-18 16:13:02 -0700355 continue;
356 }
Ed Tanousbb679322022-05-16 16:10:00 -0700357 unsigned int configBus = std::visit(
358 VariantToUnsignedIntVisitor(), findBus->second);
359 unsigned int configAddress = std::visit(
360 VariantToUnsignedIntVisitor(), findAddress->second);
361
362 if (configBus == bus && configAddress == address)
James Feistde5e9702019-09-18 16:13:02 -0700363 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700364 sensorData = &cfgData;
James Feistde5e9702019-09-18 16:13:02 -0700365 break;
366 }
Ed Tanousbb679322022-05-16 16:10:00 -0700367 }
368 }
369 if (sensorData == nullptr)
370 {
371 std::cerr << "failed to find match for " << path.string()
372 << "\n";
373 continue;
374 }
375
376 auto findSensorName = baseConfiguration->second.find("Name");
377
378 if (findSensorName == baseConfiguration->second.end())
379 {
380 std::cerr << "could not determine configuration name for "
381 << path.string() << "\n";
382 continue;
383 }
384 std::string sensorName =
385 std::get<std::string>(findSensorName->second);
386
387 // on rescans, only update sensors we were signaled by
388 auto findSensor = tachSensors.find(sensorName);
389 if (!firstScan && findSensor != tachSensors.end())
390 {
391 bool found = false;
392 for (auto it = sensorsChanged->begin();
393 it != sensorsChanged->end(); it++)
394 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700395 if (it->ends_with(findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700396 {
Ed Tanousbb679322022-05-16 16:10:00 -0700397 sensorsChanged->erase(it);
398 findSensor->second = nullptr;
399 found = true;
400 break;
James Feistde5e9702019-09-18 16:13:02 -0700401 }
402 }
Ed Tanousbb679322022-05-16 16:10:00 -0700403 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700404 {
James Feist95b079b2018-11-21 09:28:00 -0800405 continue;
406 }
Ed Tanousbb679322022-05-16 16:10:00 -0700407 }
408 std::vector<thresholds::Threshold> sensorThresholds;
409 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
410 {
411 std::cerr << "error populating thresholds for " << sensorName
412 << "\n";
413 }
James Feist95b079b2018-11-21 09:28:00 -0800414
Ed Tanousbb679322022-05-16 16:10:00 -0700415 auto presenceConfig =
Zev Weiss054aad82022-08-18 01:37:34 -0700416 sensorData->find(cfgIntf + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800417
Ed Tanousbb679322022-05-16 16:10:00 -0700418 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
419
420 // presence sensors are optional
421 if (presenceConfig != sensorData->end())
422 {
423 auto findPolarity = presenceConfig->second.find("Polarity");
424 auto findPinName = presenceConfig->second.find("PinName");
425
426 if (findPinName == presenceConfig->second.end() ||
427 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800428 {
Ed Tanousbb679322022-05-16 16:10:00 -0700429 std::cerr << "Malformed Presence Configuration\n";
James Feistde5e9702019-09-18 16:13:02 -0700430 }
Ed Tanousbb679322022-05-16 16:10:00 -0700431 else
James Feistde5e9702019-09-18 16:13:02 -0700432 {
Ed Tanousbb679322022-05-16 16:10:00 -0700433 bool inverted =
434 std::get<std::string>(findPolarity->second) == "Low";
Ed Tanous2049bd22022-07-09 07:20:26 -0700435 if (const auto* pinName =
Ed Tanousbb679322022-05-16 16:10:00 -0700436 std::get_if<std::string>(&findPinName->second))
James Feistde5e9702019-09-18 16:13:02 -0700437 {
Ed Tanousbb679322022-05-16 16:10:00 -0700438 presenceSensor = std::make_unique<PresenceSensor>(
439 *pinName, inverted, io, sensorName);
James Feistde5e9702019-09-18 16:13:02 -0700440 }
441 else
442 {
Ed Tanousbb679322022-05-16 16:10:00 -0700443 std::cerr << "Malformed Presence pinName for sensor "
444 << sensorName << " \n";
James Feistde5e9702019-09-18 16:13:02 -0700445 }
446 }
Ed Tanousbb679322022-05-16 16:10:00 -0700447 }
448 std::optional<RedundancySensor>* redundancy = nullptr;
449 if (fanType == FanTypes::aspeed)
450 {
451 redundancy = &systemRedundancy;
452 }
453
Zev Weissa4d27682022-07-19 15:30:36 -0700454 PowerState powerState = getPowerState(baseConfiguration->second);
Yong Zhao77b3add2021-01-09 04:25:18 +0000455
Ed Tanousbb679322022-05-16 16:10:00 -0700456 constexpr double defaultMaxReading = 25000;
457 constexpr double defaultMinReading = 0;
458 std::pair<double, double> limits =
459 std::make_pair(defaultMinReading, defaultMaxReading);
460
461 auto connector =
Zev Weiss054aad82022-08-18 01:37:34 -0700462 sensorData->find(cfgIntf + std::string(".Connector"));
Ed Tanousbb679322022-05-16 16:10:00 -0700463
464 std::optional<std::string> led;
465 std::string pwmName;
466 fs::path pwmPath;
467
468 // The Mutable parameter is optional, defaulting to false
469 bool isValueMutable = false;
470 if (connector != sensorData->end())
471 {
472 auto findPwm = connector->second.find("Pwm");
473 if (findPwm != connector->second.end())
474 {
475 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
476 findPwm->second);
477 if (!findPwmPath(directory, pwm, pwmPath))
478 {
479 std::cerr << "Connector for " << sensorName
480 << " no pwm channel found!\n";
481 continue;
482 }
483
484 fs::path pwmEnableFile =
485 "pwm" + std::to_string(pwm + 1) + "_enable";
486 fs::path enablePath = pwmPath.parent_path() / pwmEnableFile;
487 enablePwm(enablePath);
488
489 /* use pwm name override if found in configuration else
490 * use default */
491 auto findOverride = connector->second.find("PwmName");
492 if (findOverride != connector->second.end())
493 {
494 pwmName = std::visit(VariantToStringVisitor(),
495 findOverride->second);
496 }
497 else
498 {
499 pwmName = "Pwm_" + std::to_string(pwm + 1);
500 }
501
502 // Check PWM sensor mutability
503 auto findMutable = connector->second.find("Mutable");
504 if (findMutable != connector->second.end())
505 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700506 const auto* ptrMutable =
Ed Tanousbb679322022-05-16 16:10:00 -0700507 std::get_if<bool>(&(findMutable->second));
Ed Tanous2049bd22022-07-09 07:20:26 -0700508 if (ptrMutable != nullptr)
Ed Tanousbb679322022-05-16 16:10:00 -0700509 {
510 isValueMutable = *ptrMutable;
511 }
512 }
513 }
514 else
515 {
516 std::cerr << "Connector for " << sensorName
517 << " missing pwm!\n";
518 }
519
520 auto findLED = connector->second.find("LED");
521 if (findLED != connector->second.end())
522 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700523 const auto* ledName =
524 std::get_if<std::string>(&(findLED->second));
Ed Tanousbb679322022-05-16 16:10:00 -0700525 if (ledName == nullptr)
526 {
527 std::cerr << "Wrong format for LED of " << sensorName
528 << "\n";
529 }
530 else
531 {
532 led = *ledName;
533 }
534 }
535 }
536
537 findLimits(limits, baseConfiguration);
Josh Lehan5170fe62022-08-03 13:17:41 -0700538
Justin Ledford9c47bd72022-08-27 01:02:09 +0000539 enableFanInput(path);
540
Josh Lehan5170fe62022-08-03 13:17:41 -0700541 auto& tachSensor = tachSensors[sensorName];
542 tachSensor = nullptr;
543 tachSensor = std::make_shared<TachSensor>(
Ed Tanousbb679322022-05-16 16:10:00 -0700544 path.string(), baseType, objectServer, dbusConnection,
545 std::move(presenceSensor), redundancy, io, sensorName,
546 std::move(sensorThresholds), *interfacePath, limits, powerState,
547 led);
Josh Lehan5170fe62022-08-03 13:17:41 -0700548 tachSensor->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700549
550 if (!pwmPath.empty() && fs::exists(pwmPath) &&
Ed Tanous2049bd22022-07-09 07:20:26 -0700551 (pwmSensors.count(pwmPath) == 0U))
Ed Tanousbb679322022-05-16 16:10:00 -0700552 {
553 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
554 pwmName, pwmPath, dbusConnection, objectServer,
555 *interfacePath, "Fan", isValueMutable);
556 }
557 }
558
559 createRedundancySensor(tachSensors, dbusConnection, objectServer);
Ed Tanous8a17c302021-09-02 15:07:11 -0700560 });
James Feistde5e9702019-09-18 16:13:02 -0700561 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700562 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
563 retries);
James Feist6714a252018-09-10 15:26:18 -0700564}
565
James Feistb6c0b912019-07-09 12:21:44 -0700566int main()
James Feist6714a252018-09-10 15:26:18 -0700567{
Ed Tanous1f978632023-02-28 18:16:39 -0800568 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700569 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700570 sdbusplus::asio::object_server objectServer(systemBus, true);
571
572 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanousd9067252022-10-22 15:35:52 -0700573 objectServer.add_manager("/xyz/openbmc_project/control");
Lei YUc2f83fe2022-12-02 14:49:47 +0800574 objectServer.add_manager("/xyz/openbmc_project/inventory");
James Feist6714a252018-09-10 15:26:18 -0700575 systemBus->request_name("xyz.openbmc_project.FanSensor");
Josh Lehan5170fe62022-08-03 13:17:41 -0700576 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700577 tachSensors;
578 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
579 pwmSensors;
James Feist5591cf082020-07-15 16:44:54 -0700580 auto sensorsChanged =
581 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700582
Ed Tanous83db50c2023-03-01 10:20:24 -0800583 boost::asio::post(io, [&]() {
James Feist6714a252018-09-10 15:26:18 -0700584 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
585 nullptr);
586 });
587
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700588 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500589 std::function<void(sdbusplus::message_t&)> eventHandler =
590 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700591 if (message.is_method_error())
592 {
593 std::cerr << "callback method error\n";
594 return;
595 }
596 sensorsChanged->insert(message.get_path());
597 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800598 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700599
600 filterTimer.async_wait([&](const boost::system::error_code& ec) {
601 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700602 {
Ed Tanousbb679322022-05-16 16:10:00 -0700603 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700604 return;
605 }
Ed Tanousbb679322022-05-16 16:10:00 -0700606 if (ec)
607 {
608 std::cerr << "timer error\n";
609 return;
610 }
611 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
612 sensorsChanged, 5);
613 });
614 };
James Feist6714a252018-09-10 15:26:18 -0700615
Zev Weiss214d9712022-08-12 12:54:31 -0700616 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
617 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700618
James Feistdc6c55f2018-10-31 12:53:20 -0700619 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500620 std::function<void(sdbusplus::message_t&)> redundancyHandler =
621 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700622 createRedundancySensor(tachSensors, systemBus, objectServer);
623 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500624 auto match = std::make_unique<sdbusplus::bus::match_t>(
625 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700626 "type='signal',member='PropertiesChanged',path_namespace='" +
627 std::string(inventoryPath) + "',arg0namespace='" +
628 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700629 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700630 matches.emplace_back(std::move(match));
631
Bruce Lee1263c3d2021-06-04 15:16:33 +0800632 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700633 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700634 return 0;
James Feist6714a252018-09-10 15:26:18 -0700635}