blob: 9c8a73836dcd0fe673a9945ce401ee9b534bca3c [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";
Zhikui Ren39963222023-05-04 17:06:50 -070077 if (!fs::exists(linkPath))
78 {
79 return FanTypes::i2c;
80 }
Chris Sides9a472e82023-04-03 15:13:37 -050081
Zhikui Ren39963222023-05-04 17:06:50 -070082 std::string canonical = fs::canonical(linkPath);
Chris Sides9a472e82023-04-03 15:13:37 -050083 std::string compatiblePath = canonical + "/compatible";
84 std::ifstream compatibleStream(compatiblePath);
85
Chris Sides3d5260d2023-04-10 15:45:00 -050086 if (!compatibleStream)
James Feist95b079b2018-11-21 09:28:00 -080087 {
Chris Sides9a472e82023-04-03 15:13:37 -050088 std::cerr << "Error opening " << compatiblePath << "\n";
Chris Sides3d5260d2023-04-10 15:45:00 -050089 return FanTypes::i2c;
James Feist95b079b2018-11-21 09:28:00 -080090 }
Chris Sides9a472e82023-04-03 15:13:37 -050091
92 std::string compatibleString;
Chris Sides3d5260d2023-04-10 15:45:00 -050093 while (std::getline(compatibleStream, compatibleString))
Peter Lundgren8843b622019-09-12 10:33:41 -070094 {
Chris Sides9a472e82023-04-03 15:13:37 -050095 compatibleString.pop_back(); // trim EOL before comparisons
96
Chris Sides3d5260d2023-04-10 15:45:00 -050097 std::map<std::string, FanTypes>::const_iterator compatibleIterator =
98 compatibleFanTypes.find(compatibleString);
99
100 if (compatibleIterator != compatibleFanTypes.end())
Chris Sides9a472e82023-04-03 15:13:37 -0500101 {
Chris Sides3d5260d2023-04-10 15:45:00 -0500102 return compatibleIterator->second;
Chris Sides9a472e82023-04-03 15:13:37 -0500103 }
Peter Lundgren8843b622019-09-12 10:33:41 -0700104 }
Chris Sides9a472e82023-04-03 15:13:37 -0500105
James Feist95b079b2018-11-21 09:28:00 -0800106 return FanTypes::i2c;
107}
Jeff Linabf91de2020-12-23 10:55:42 +0800108void enablePwm(const fs::path& filePath)
109{
110 std::fstream enableFile(filePath, std::ios::in | std::ios::out);
111 if (!enableFile.good())
112 {
113 std::cerr << "Error read/write " << filePath << "\n";
114 return;
115 }
James Feistdc6c55f2018-10-31 12:53:20 -0700116
Jeff Linabf91de2020-12-23 10:55:42 +0800117 std::string regulateMode;
118 std::getline(enableFile, regulateMode);
119 if (regulateMode == "0")
120 {
121 enableFile << 1;
122 }
123}
Howard Chiuddf25d12021-12-02 15:14:44 +0800124bool findPwmfanPath(unsigned int configPwmfanIndex, fs::path& pwmPath)
125{
126 /* Search PWM since pwm-fan had separated
127 * PWM from tach directory and 1 channel only*/
128 std::vector<fs::path> pwmfanPaths;
129 std::string pwnfanDevName("pwm-fan");
130
131 pwnfanDevName += std::to_string(configPwmfanIndex);
132
133 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwmfanPaths))
134 {
135 std::cerr << "No PWMs are found!\n";
136 return false;
137 }
138 for (const auto& path : pwmfanPaths)
139 {
140 std::error_code ec;
141 fs::path link = fs::read_symlink(path.parent_path() / "device", ec);
142
143 if (ec)
144 {
145 std::cerr << "read_symlink() failed: " << ec.message() << " ("
146 << ec.value() << ")\n";
147 continue;
148 }
149
150 if (link.filename().string() == pwnfanDevName)
151 {
152 pwmPath = path;
153 return true;
154 }
155 }
156 return false;
157}
158bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath)
159{
160 std::error_code ec;
161
162 /* Assuming PWM file is appeared in the same directory as fanX_input */
163 auto path = directory / ("pwm" + std::to_string(pwm + 1));
164 bool exists = fs::exists(path, ec);
165
166 if (ec || !exists)
167 {
168 /* PWM file not exist or error happened */
169 if (ec)
170 {
171 std::cerr << "exists() failed: " << ec.message() << " ("
172 << ec.value() << ")\n";
173 }
174 /* try search form pwm-fanX directory */
175 return findPwmfanPath(pwm, pwmPath);
176 }
177
178 pwmPath = path;
179 return true;
180}
Justin Ledford9c47bd72022-08-27 01:02:09 +0000181
182// The argument to this function should be the fanN_input file that we want to
183// enable. The function will locate the corresponding fanN_enable file if it
184// exists. Note that some drivers don't provide this file if the sensors are
185// always enabled.
186void enableFanInput(const fs::path& fanInputPath)
187{
188 std::error_code ec;
189 std::string path(fanInputPath.string());
190 boost::replace_last(path, "input", "enable");
191
192 bool exists = fs::exists(path, ec);
193 if (ec || !exists)
194 {
195 return;
196 }
197
198 std::fstream enableFile(path, std::ios::out);
199 if (!enableFile.good())
200 {
201 return;
202 }
203 enableFile << 1;
204}
205
Kuiying Wangd5407412020-09-09 16:06:56 +0800206void createRedundancySensor(
Josh Lehan5170fe62022-08-03 13:17:41 -0700207 const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
Kuiying Wangd5407412020-09-09 16:06:56 +0800208 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700209 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800210 sdbusplus::asio::object_server& objectServer)
211{
Kuiying Wangd5407412020-09-09 16:06:56 +0800212 conn->async_method_call(
213 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700214 const ManagedObjectType& managedObj) {
Ed Tanousbb679322022-05-16 16:10:00 -0700215 if (ec)
216 {
217 std::cerr << "Error calling entity manager \n";
218 return;
219 }
Zev Weiss77636ec2022-08-12 18:21:01 -0700220 for (const auto& [path, interfaces] : managedObj)
Ed Tanousbb679322022-05-16 16:10:00 -0700221 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700222 for (const auto& [intf, cfg] : interfaces)
Kuiying Wangd5407412020-09-09 16:06:56 +0800223 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700224 if (intf == redundancyConfiguration)
Kuiying Wangd5407412020-09-09 16:06:56 +0800225 {
Ed Tanousbb679322022-05-16 16:10:00 -0700226 // currently only support one
Zev Weiss77636ec2022-08-12 18:21:01 -0700227 auto findCount = cfg.find("AllowedFailures");
228 if (findCount == cfg.end())
Kuiying Wangd5407412020-09-09 16:06:56 +0800229 {
Ed Tanousbb679322022-05-16 16:10:00 -0700230 std::cerr << "Malformed redundancy record \n";
Kuiying Wangd5407412020-09-09 16:06:56 +0800231 return;
232 }
Ed Tanousbb679322022-05-16 16:10:00 -0700233 std::vector<std::string> sensorList;
234
Zev Weiss77636ec2022-08-12 18:21:01 -0700235 for (const auto& [name, sensor] : sensors)
Ed Tanousbb679322022-05-16 16:10:00 -0700236 {
237 sensorList.push_back(
238 "/xyz/openbmc_project/sensors/fan_tach/" +
Zev Weiss77636ec2022-08-12 18:21:01 -0700239 sensor->name);
Ed Tanousbb679322022-05-16 16:10:00 -0700240 }
241 systemRedundancy.reset();
Zev Weiss77636ec2022-08-12 18:21:01 -0700242 systemRedundancy.emplace(
243 RedundancySensor(std::get<uint64_t>(findCount->second),
244 sensorList, objectServer, path));
Ed Tanousbb679322022-05-16 16:10:00 -0700245
246 return;
Kuiying Wangd5407412020-09-09 16:06:56 +0800247 }
248 }
Ed Tanousbb679322022-05-16 16:10:00 -0700249 }
Kuiying Wangd5407412020-09-09 16:06:56 +0800250 },
Nan Zhou3e620af2022-09-20 22:28:31 +0000251 "xyz.openbmc_project.EntityManager", "/xyz/openbmc_project/inventory",
Kuiying Wangd5407412020-09-09 16:06:56 +0800252 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
253}
254
James Feist6714a252018-09-10 15:26:18 -0700255void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800256 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Josh Lehan5170fe62022-08-03 13:17:41 -0700257 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700258 tachSensors,
259 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
260 pwmSensors,
261 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700262 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700263 sensorsChanged,
264 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700265{
James Feistde5e9702019-09-18 16:13:02 -0700266 auto getter = std::make_shared<GetSensorConfiguration>(
267 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700268 [&io, &objectServer, &tachSensors, &pwmSensors, &dbusConnection,
269 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700270 bool firstScan = sensorsChanged == nullptr;
271 std::vector<fs::path> paths;
272 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
273 {
274 std::cerr << "No fan sensors in system\n";
275 return;
276 }
277
278 // iterate through all found fan sensors, and try to match them with
279 // configuration
280 for (const auto& path : paths)
281 {
282 std::smatch match;
283 std::string pathStr = path.string();
284
285 std::regex_search(pathStr, match, inputRegex);
286 std::string indexStr = *(match.begin() + 1);
287
288 fs::path directory = path.parent_path();
289 FanTypes fanType = getFanType(directory);
Zev Weiss054aad82022-08-18 01:37:34 -0700290 std::string cfgIntf = configInterfaceName(sensorTypes[fanType]);
Ed Tanousbb679322022-05-16 16:10:00 -0700291
292 // convert to 0 based
293 size_t index = std::stoul(indexStr) - 1;
294
295 const char* baseType = nullptr;
296 const SensorData* sensorData = nullptr;
297 const std::string* interfacePath = nullptr;
298 const SensorBaseConfiguration* baseConfiguration = nullptr;
Zev Weiss77636ec2022-08-12 18:21:01 -0700299 for (const auto& [path, cfgData] : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800300 {
Ed Tanousbb679322022-05-16 16:10:00 -0700301 // find the base of the configuration to see if indexes
302 // match
Zev Weiss054aad82022-08-18 01:37:34 -0700303 auto sensorBaseFind = cfgData.find(cfgIntf);
Zev Weiss77636ec2022-08-12 18:21:01 -0700304 if (sensorBaseFind == cfgData.end())
James Feist95b079b2018-11-21 09:28:00 -0800305 {
Ed Tanousbb679322022-05-16 16:10:00 -0700306 continue;
307 }
308
309 baseConfiguration = &(*sensorBaseFind);
Zev Weiss77636ec2022-08-12 18:21:01 -0700310 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700311 baseType = sensorTypes[fanType];
312
313 auto findIndex = baseConfiguration->second.find("Index");
314 if (findIndex == baseConfiguration->second.end())
315 {
316 std::cerr << baseConfiguration->first << " missing index\n";
317 continue;
318 }
319 unsigned int configIndex = std::visit(
320 VariantToUnsignedIntVisitor(), findIndex->second);
321 if (configIndex != index)
322 {
323 continue;
324 }
Chris Sidesc361e222023-04-05 15:18:20 -0500325 if (fanType == FanTypes::aspeed ||
326 fanType == FanTypes::nuvoton || fanType == FanTypes::hpe)
Ed Tanousbb679322022-05-16 16:10:00 -0700327 {
Chris Sidesc361e222023-04-05 15:18:20 -0500328 // there will be only 1 aspeed or nuvoton or hpe sensor
Chris Sides3d5260d2023-04-10 15:45:00 -0500329 // object in sysfs, we found the fan
Zev Weiss77636ec2022-08-12 18:21:01 -0700330 sensorData = &cfgData;
Ed Tanousbb679322022-05-16 16:10:00 -0700331 break;
332 }
333 if (fanType == FanTypes::i2c)
334 {
335 size_t bus = 0;
336 size_t address = 0;
337
338 std::string link =
339 fs::read_symlink(directory / "device").filename();
340
341 size_t findDash = link.find('-');
342 if (findDash == std::string::npos ||
343 link.size() <= findDash + 1)
James Feistde5e9702019-09-18 16:13:02 -0700344 {
Ed Tanousbb679322022-05-16 16:10:00 -0700345 std::cerr << "Error finding device from symlink";
James Feistde5e9702019-09-18 16:13:02 -0700346 }
Ed Tanousbb679322022-05-16 16:10:00 -0700347 bus = std::stoi(link.substr(0, findDash));
348 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800349
Ed Tanousbb679322022-05-16 16:10:00 -0700350 auto findBus = baseConfiguration->second.find("Bus");
351 auto findAddress =
352 baseConfiguration->second.find("Address");
353 if (findBus == baseConfiguration->second.end() ||
354 findAddress == baseConfiguration->second.end())
James Feistde5e9702019-09-18 16:13:02 -0700355 {
356 std::cerr << baseConfiguration->first
Ed Tanousbb679322022-05-16 16:10:00 -0700357 << " missing bus or address\n";
James Feistde5e9702019-09-18 16:13:02 -0700358 continue;
359 }
Ed Tanousbb679322022-05-16 16:10:00 -0700360 unsigned int configBus = std::visit(
361 VariantToUnsignedIntVisitor(), findBus->second);
362 unsigned int configAddress = std::visit(
363 VariantToUnsignedIntVisitor(), findAddress->second);
364
365 if (configBus == bus && configAddress == address)
James Feistde5e9702019-09-18 16:13:02 -0700366 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700367 sensorData = &cfgData;
James Feistde5e9702019-09-18 16:13:02 -0700368 break;
369 }
Ed Tanousbb679322022-05-16 16:10:00 -0700370 }
371 }
372 if (sensorData == nullptr)
373 {
374 std::cerr << "failed to find match for " << path.string()
375 << "\n";
376 continue;
377 }
378
379 auto findSensorName = baseConfiguration->second.find("Name");
380
381 if (findSensorName == baseConfiguration->second.end())
382 {
383 std::cerr << "could not determine configuration name for "
384 << path.string() << "\n";
385 continue;
386 }
387 std::string sensorName =
388 std::get<std::string>(findSensorName->second);
389
390 // on rescans, only update sensors we were signaled by
391 auto findSensor = tachSensors.find(sensorName);
392 if (!firstScan && findSensor != tachSensors.end())
393 {
394 bool found = false;
395 for (auto it = sensorsChanged->begin();
396 it != sensorsChanged->end(); it++)
397 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700398 if (it->ends_with(findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700399 {
Ed Tanousbb679322022-05-16 16:10:00 -0700400 sensorsChanged->erase(it);
401 findSensor->second = nullptr;
402 found = true;
403 break;
James Feistde5e9702019-09-18 16:13:02 -0700404 }
405 }
Ed Tanousbb679322022-05-16 16:10:00 -0700406 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700407 {
James Feist95b079b2018-11-21 09:28:00 -0800408 continue;
409 }
Ed Tanousbb679322022-05-16 16:10:00 -0700410 }
411 std::vector<thresholds::Threshold> sensorThresholds;
412 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
413 {
414 std::cerr << "error populating thresholds for " << sensorName
415 << "\n";
416 }
James Feist95b079b2018-11-21 09:28:00 -0800417
Ed Tanousbb679322022-05-16 16:10:00 -0700418 auto presenceConfig =
Zev Weiss054aad82022-08-18 01:37:34 -0700419 sensorData->find(cfgIntf + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800420
Ed Tanousbb679322022-05-16 16:10:00 -0700421 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
422
423 // presence sensors are optional
424 if (presenceConfig != sensorData->end())
425 {
426 auto findPolarity = presenceConfig->second.find("Polarity");
427 auto findPinName = presenceConfig->second.find("PinName");
428
429 if (findPinName == presenceConfig->second.end() ||
430 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800431 {
Ed Tanousbb679322022-05-16 16:10:00 -0700432 std::cerr << "Malformed Presence Configuration\n";
James Feistde5e9702019-09-18 16:13:02 -0700433 }
Ed Tanousbb679322022-05-16 16:10:00 -0700434 else
James Feistde5e9702019-09-18 16:13:02 -0700435 {
Ed Tanousbb679322022-05-16 16:10:00 -0700436 bool inverted =
437 std::get<std::string>(findPolarity->second) == "Low";
Ed Tanous2049bd22022-07-09 07:20:26 -0700438 if (const auto* pinName =
Ed Tanousbb679322022-05-16 16:10:00 -0700439 std::get_if<std::string>(&findPinName->second))
James Feistde5e9702019-09-18 16:13:02 -0700440 {
Ed Tanousbb679322022-05-16 16:10:00 -0700441 presenceSensor = std::make_unique<PresenceSensor>(
442 *pinName, inverted, io, sensorName);
James Feistde5e9702019-09-18 16:13:02 -0700443 }
444 else
445 {
Ed Tanousbb679322022-05-16 16:10:00 -0700446 std::cerr << "Malformed Presence pinName for sensor "
447 << sensorName << " \n";
James Feistde5e9702019-09-18 16:13:02 -0700448 }
449 }
Ed Tanousbb679322022-05-16 16:10:00 -0700450 }
451 std::optional<RedundancySensor>* redundancy = nullptr;
452 if (fanType == FanTypes::aspeed)
453 {
454 redundancy = &systemRedundancy;
455 }
456
Zev Weissa4d27682022-07-19 15:30:36 -0700457 PowerState powerState = getPowerState(baseConfiguration->second);
Yong Zhao77b3add2021-01-09 04:25:18 +0000458
Ed Tanousbb679322022-05-16 16:10:00 -0700459 constexpr double defaultMaxReading = 25000;
460 constexpr double defaultMinReading = 0;
461 std::pair<double, double> limits =
462 std::make_pair(defaultMinReading, defaultMaxReading);
463
464 auto connector =
Zev Weiss054aad82022-08-18 01:37:34 -0700465 sensorData->find(cfgIntf + std::string(".Connector"));
Ed Tanousbb679322022-05-16 16:10:00 -0700466
467 std::optional<std::string> led;
468 std::string pwmName;
469 fs::path pwmPath;
470
471 // The Mutable parameter is optional, defaulting to false
472 bool isValueMutable = false;
473 if (connector != sensorData->end())
474 {
475 auto findPwm = connector->second.find("Pwm");
476 if (findPwm != connector->second.end())
477 {
478 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
479 findPwm->second);
480 if (!findPwmPath(directory, pwm, pwmPath))
481 {
482 std::cerr << "Connector for " << sensorName
483 << " no pwm channel found!\n";
484 continue;
485 }
486
Patrick Williams779c96a2023-05-10 07:50:42 -0500487 fs::path pwmEnableFile = "pwm" + std::to_string(pwm + 1) +
488 "_enable";
Ed Tanousbb679322022-05-16 16:10:00 -0700489 fs::path enablePath = pwmPath.parent_path() / pwmEnableFile;
490 enablePwm(enablePath);
491
492 /* use pwm name override if found in configuration else
493 * use default */
494 auto findOverride = connector->second.find("PwmName");
495 if (findOverride != connector->second.end())
496 {
497 pwmName = std::visit(VariantToStringVisitor(),
498 findOverride->second);
499 }
500 else
501 {
502 pwmName = "Pwm_" + std::to_string(pwm + 1);
503 }
504
505 // Check PWM sensor mutability
506 auto findMutable = connector->second.find("Mutable");
507 if (findMutable != connector->second.end())
508 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700509 const auto* ptrMutable =
Ed Tanousbb679322022-05-16 16:10:00 -0700510 std::get_if<bool>(&(findMutable->second));
Ed Tanous2049bd22022-07-09 07:20:26 -0700511 if (ptrMutable != nullptr)
Ed Tanousbb679322022-05-16 16:10:00 -0700512 {
513 isValueMutable = *ptrMutable;
514 }
515 }
516 }
517 else
518 {
519 std::cerr << "Connector for " << sensorName
520 << " missing pwm!\n";
521 }
522
523 auto findLED = connector->second.find("LED");
524 if (findLED != connector->second.end())
525 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700526 const auto* ledName =
527 std::get_if<std::string>(&(findLED->second));
Ed Tanousbb679322022-05-16 16:10:00 -0700528 if (ledName == nullptr)
529 {
530 std::cerr << "Wrong format for LED of " << sensorName
531 << "\n";
532 }
533 else
534 {
535 led = *ledName;
536 }
537 }
538 }
539
540 findLimits(limits, baseConfiguration);
Josh Lehan5170fe62022-08-03 13:17:41 -0700541
Justin Ledford9c47bd72022-08-27 01:02:09 +0000542 enableFanInput(path);
543
Josh Lehan5170fe62022-08-03 13:17:41 -0700544 auto& tachSensor = tachSensors[sensorName];
545 tachSensor = nullptr;
546 tachSensor = std::make_shared<TachSensor>(
Ed Tanousbb679322022-05-16 16:10:00 -0700547 path.string(), baseType, objectServer, dbusConnection,
548 std::move(presenceSensor), redundancy, io, sensorName,
549 std::move(sensorThresholds), *interfacePath, limits, powerState,
550 led);
Josh Lehan5170fe62022-08-03 13:17:41 -0700551 tachSensor->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700552
553 if (!pwmPath.empty() && fs::exists(pwmPath) &&
Ed Tanous2049bd22022-07-09 07:20:26 -0700554 (pwmSensors.count(pwmPath) == 0U))
Ed Tanousbb679322022-05-16 16:10:00 -0700555 {
556 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
557 pwmName, pwmPath, dbusConnection, objectServer,
558 *interfacePath, "Fan", isValueMutable);
559 }
560 }
561
562 createRedundancySensor(tachSensors, dbusConnection, objectServer);
Ed Tanous8a17c302021-09-02 15:07:11 -0700563 });
James Feistde5e9702019-09-18 16:13:02 -0700564 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700565 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
566 retries);
James Feist6714a252018-09-10 15:26:18 -0700567}
568
James Feistb6c0b912019-07-09 12:21:44 -0700569int main()
James Feist6714a252018-09-10 15:26:18 -0700570{
Ed Tanous1f978632023-02-28 18:16:39 -0800571 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700572 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700573 sdbusplus::asio::object_server objectServer(systemBus, true);
574
575 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanousd9067252022-10-22 15:35:52 -0700576 objectServer.add_manager("/xyz/openbmc_project/control");
Lei YUc2f83fe2022-12-02 14:49:47 +0800577 objectServer.add_manager("/xyz/openbmc_project/inventory");
James Feist6714a252018-09-10 15:26:18 -0700578 systemBus->request_name("xyz.openbmc_project.FanSensor");
Josh Lehan5170fe62022-08-03 13:17:41 -0700579 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700580 tachSensors;
581 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
582 pwmSensors;
James Feist5591cf082020-07-15 16:44:54 -0700583 auto sensorsChanged =
584 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700585
Ed Tanous83db50c2023-03-01 10:20:24 -0800586 boost::asio::post(io, [&]() {
James Feist6714a252018-09-10 15:26:18 -0700587 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
588 nullptr);
589 });
590
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700591 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500592 std::function<void(sdbusplus::message_t&)> eventHandler =
593 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700594 if (message.is_method_error())
595 {
596 std::cerr << "callback method error\n";
597 return;
598 }
599 sensorsChanged->insert(message.get_path());
600 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800601 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700602
603 filterTimer.async_wait([&](const boost::system::error_code& ec) {
604 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700605 {
Ed Tanousbb679322022-05-16 16:10:00 -0700606 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700607 return;
608 }
Ed Tanousbb679322022-05-16 16:10:00 -0700609 if (ec)
610 {
611 std::cerr << "timer error\n";
612 return;
613 }
614 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
615 sensorsChanged, 5);
616 });
617 };
James Feist6714a252018-09-10 15:26:18 -0700618
Zev Weiss214d9712022-08-12 12:54:31 -0700619 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
620 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700621
James Feistdc6c55f2018-10-31 12:53:20 -0700622 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500623 std::function<void(sdbusplus::message_t&)> redundancyHandler =
624 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700625 createRedundancySensor(tachSensors, systemBus, objectServer);
626 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500627 auto match = std::make_unique<sdbusplus::bus::match_t>(
628 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700629 "type='signal',member='PropertiesChanged',path_namespace='" +
630 std::string(inventoryPath) + "',arg0namespace='" +
631 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700632 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700633 matches.emplace_back(std::move(match));
634
Bruce Lee1263c3d2021-06-04 15:16:33 +0800635 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700636 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700637 return 0;
James Feist6714a252018-09-10 15:26:18 -0700638}