blob: 7ea8806751ded608ebbaafd7d899a5c6cc9d54eb [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},
Brian Mad37e1db2023-05-08 13:36:53 +080070 {"nuvoton,npcm845-pwm-fan", FanTypes::nuvoton},
Chris Sidesc361e222023-04-05 15:18:20 -050071 {"hpe,gxp-fan-ctrl", FanTypes::hpe}
Chris Sides3d5260d2023-04-10 15:45:00 -050072 // add compatible string here for new fan type
73};
74
James Feist95b079b2018-11-21 09:28:00 -080075FanTypes getFanType(const fs::path& parentPath)
76{
Chris Sides9a472e82023-04-03 15:13:37 -050077 fs::path linkPath = parentPath / "of_node";
Zhikui Ren39963222023-05-04 17:06:50 -070078 if (!fs::exists(linkPath))
79 {
80 return FanTypes::i2c;
81 }
Chris Sides9a472e82023-04-03 15:13:37 -050082
Zhikui Ren39963222023-05-04 17:06:50 -070083 std::string canonical = fs::canonical(linkPath);
Chris Sides9a472e82023-04-03 15:13:37 -050084 std::string compatiblePath = canonical + "/compatible";
85 std::ifstream compatibleStream(compatiblePath);
86
Chris Sides3d5260d2023-04-10 15:45:00 -050087 if (!compatibleStream)
James Feist95b079b2018-11-21 09:28:00 -080088 {
Chris Sides9a472e82023-04-03 15:13:37 -050089 std::cerr << "Error opening " << compatiblePath << "\n";
Chris Sides3d5260d2023-04-10 15:45:00 -050090 return FanTypes::i2c;
James Feist95b079b2018-11-21 09:28:00 -080091 }
Chris Sides9a472e82023-04-03 15:13:37 -050092
93 std::string compatibleString;
Chris Sides3d5260d2023-04-10 15:45:00 -050094 while (std::getline(compatibleStream, compatibleString))
Peter Lundgren8843b622019-09-12 10:33:41 -070095 {
Chris Sides9a472e82023-04-03 15:13:37 -050096 compatibleString.pop_back(); // trim EOL before comparisons
97
Chris Sides3d5260d2023-04-10 15:45:00 -050098 std::map<std::string, FanTypes>::const_iterator compatibleIterator =
99 compatibleFanTypes.find(compatibleString);
100
101 if (compatibleIterator != compatibleFanTypes.end())
Chris Sides9a472e82023-04-03 15:13:37 -0500102 {
Chris Sides3d5260d2023-04-10 15:45:00 -0500103 return compatibleIterator->second;
Chris Sides9a472e82023-04-03 15:13:37 -0500104 }
Peter Lundgren8843b622019-09-12 10:33:41 -0700105 }
Chris Sides9a472e82023-04-03 15:13:37 -0500106
James Feist95b079b2018-11-21 09:28:00 -0800107 return FanTypes::i2c;
108}
Jeff Linabf91de2020-12-23 10:55:42 +0800109void enablePwm(const fs::path& filePath)
110{
111 std::fstream enableFile(filePath, std::ios::in | std::ios::out);
112 if (!enableFile.good())
113 {
114 std::cerr << "Error read/write " << filePath << "\n";
115 return;
116 }
James Feistdc6c55f2018-10-31 12:53:20 -0700117
Jeff Linabf91de2020-12-23 10:55:42 +0800118 std::string regulateMode;
119 std::getline(enableFile, regulateMode);
120 if (regulateMode == "0")
121 {
122 enableFile << 1;
123 }
124}
Howard Chiuddf25d12021-12-02 15:14:44 +0800125bool findPwmfanPath(unsigned int configPwmfanIndex, fs::path& pwmPath)
126{
127 /* Search PWM since pwm-fan had separated
128 * PWM from tach directory and 1 channel only*/
129 std::vector<fs::path> pwmfanPaths;
130 std::string pwnfanDevName("pwm-fan");
131
132 pwnfanDevName += std::to_string(configPwmfanIndex);
133
134 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwmfanPaths))
135 {
136 std::cerr << "No PWMs are found!\n";
137 return false;
138 }
139 for (const auto& path : pwmfanPaths)
140 {
141 std::error_code ec;
142 fs::path link = fs::read_symlink(path.parent_path() / "device", ec);
143
144 if (ec)
145 {
146 std::cerr << "read_symlink() failed: " << ec.message() << " ("
147 << ec.value() << ")\n";
148 continue;
149 }
150
151 if (link.filename().string() == pwnfanDevName)
152 {
153 pwmPath = path;
154 return true;
155 }
156 }
157 return false;
158}
159bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath)
160{
161 std::error_code ec;
162
163 /* Assuming PWM file is appeared in the same directory as fanX_input */
164 auto path = directory / ("pwm" + std::to_string(pwm + 1));
165 bool exists = fs::exists(path, ec);
166
167 if (ec || !exists)
168 {
169 /* PWM file not exist or error happened */
170 if (ec)
171 {
172 std::cerr << "exists() failed: " << ec.message() << " ("
173 << ec.value() << ")\n";
174 }
175 /* try search form pwm-fanX directory */
176 return findPwmfanPath(pwm, pwmPath);
177 }
178
179 pwmPath = path;
180 return true;
181}
Justin Ledford9c47bd72022-08-27 01:02:09 +0000182
183// The argument to this function should be the fanN_input file that we want to
184// enable. The function will locate the corresponding fanN_enable file if it
185// exists. Note that some drivers don't provide this file if the sensors are
186// always enabled.
187void enableFanInput(const fs::path& fanInputPath)
188{
189 std::error_code ec;
190 std::string path(fanInputPath.string());
191 boost::replace_last(path, "input", "enable");
192
193 bool exists = fs::exists(path, ec);
194 if (ec || !exists)
195 {
196 return;
197 }
198
199 std::fstream enableFile(path, std::ios::out);
200 if (!enableFile.good())
201 {
202 return;
203 }
204 enableFile << 1;
205}
206
Kuiying Wangd5407412020-09-09 16:06:56 +0800207void createRedundancySensor(
Josh Lehan5170fe62022-08-03 13:17:41 -0700208 const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
Kuiying Wangd5407412020-09-09 16:06:56 +0800209 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700210 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +0800211 sdbusplus::asio::object_server& objectServer)
212{
Kuiying Wangd5407412020-09-09 16:06:56 +0800213 conn->async_method_call(
214 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700215 const ManagedObjectType& managedObj) {
Ed Tanousbb679322022-05-16 16:10:00 -0700216 if (ec)
217 {
218 std::cerr << "Error calling entity manager \n";
219 return;
220 }
Zev Weiss77636ec2022-08-12 18:21:01 -0700221 for (const auto& [path, interfaces] : managedObj)
Ed Tanousbb679322022-05-16 16:10:00 -0700222 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700223 for (const auto& [intf, cfg] : interfaces)
Kuiying Wangd5407412020-09-09 16:06:56 +0800224 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700225 if (intf == redundancyConfiguration)
Kuiying Wangd5407412020-09-09 16:06:56 +0800226 {
Ed Tanousbb679322022-05-16 16:10:00 -0700227 // currently only support one
Zev Weiss77636ec2022-08-12 18:21:01 -0700228 auto findCount = cfg.find("AllowedFailures");
229 if (findCount == cfg.end())
Kuiying Wangd5407412020-09-09 16:06:56 +0800230 {
Ed Tanousbb679322022-05-16 16:10:00 -0700231 std::cerr << "Malformed redundancy record \n";
Kuiying Wangd5407412020-09-09 16:06:56 +0800232 return;
233 }
Ed Tanousbb679322022-05-16 16:10:00 -0700234 std::vector<std::string> sensorList;
235
Zev Weiss77636ec2022-08-12 18:21:01 -0700236 for (const auto& [name, sensor] : sensors)
Ed Tanousbb679322022-05-16 16:10:00 -0700237 {
238 sensorList.push_back(
239 "/xyz/openbmc_project/sensors/fan_tach/" +
Zev Weiss77636ec2022-08-12 18:21:01 -0700240 sensor->name);
Ed Tanousbb679322022-05-16 16:10:00 -0700241 }
242 systemRedundancy.reset();
Zev Weiss77636ec2022-08-12 18:21:01 -0700243 systemRedundancy.emplace(
244 RedundancySensor(std::get<uint64_t>(findCount->second),
245 sensorList, objectServer, path));
Ed Tanousbb679322022-05-16 16:10:00 -0700246
247 return;
Kuiying Wangd5407412020-09-09 16:06:56 +0800248 }
249 }
Ed Tanousbb679322022-05-16 16:10:00 -0700250 }
Patrick Williams597e8422023-10-20 11:19:01 -0500251 },
Nan Zhou3e620af2022-09-20 22:28:31 +0000252 "xyz.openbmc_project.EntityManager", "/xyz/openbmc_project/inventory",
Kuiying Wangd5407412020-09-09 16:06:56 +0800253 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
254}
255
James Feist6714a252018-09-10 15:26:18 -0700256void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800257 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Josh Lehan5170fe62022-08-03 13:17:41 -0700258 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
James Feist6714a252018-09-10 15:26:18 -0700259 tachSensors,
260 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
261 pwmSensors,
262 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700263 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700264 sensorsChanged,
265 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700266{
James Feistde5e9702019-09-18 16:13:02 -0700267 auto getter = std::make_shared<GetSensorConfiguration>(
268 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -0700269 [&io, &objectServer, &tachSensors, &pwmSensors, &dbusConnection,
270 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700271 bool firstScan = sensorsChanged == nullptr;
272 std::vector<fs::path> paths;
273 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
274 {
275 std::cerr << "No fan sensors in system\n";
276 return;
277 }
278
279 // iterate through all found fan sensors, and try to match them with
280 // configuration
281 for (const auto& path : paths)
282 {
283 std::smatch match;
284 std::string pathStr = path.string();
285
286 std::regex_search(pathStr, match, inputRegex);
287 std::string indexStr = *(match.begin() + 1);
288
289 fs::path directory = path.parent_path();
290 FanTypes fanType = getFanType(directory);
Zev Weiss054aad82022-08-18 01:37:34 -0700291 std::string cfgIntf = configInterfaceName(sensorTypes[fanType]);
Ed Tanousbb679322022-05-16 16:10:00 -0700292
293 // convert to 0 based
294 size_t index = std::stoul(indexStr) - 1;
295
296 const char* baseType = nullptr;
297 const SensorData* sensorData = nullptr;
298 const std::string* interfacePath = nullptr;
299 const SensorBaseConfiguration* baseConfiguration = nullptr;
Zev Weiss77636ec2022-08-12 18:21:01 -0700300 for (const auto& [path, cfgData] : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800301 {
Ed Tanousbb679322022-05-16 16:10:00 -0700302 // find the base of the configuration to see if indexes
303 // match
Zev Weiss054aad82022-08-18 01:37:34 -0700304 auto sensorBaseFind = cfgData.find(cfgIntf);
Zev Weiss77636ec2022-08-12 18:21:01 -0700305 if (sensorBaseFind == cfgData.end())
James Feist95b079b2018-11-21 09:28:00 -0800306 {
Ed Tanousbb679322022-05-16 16:10:00 -0700307 continue;
308 }
309
310 baseConfiguration = &(*sensorBaseFind);
Zev Weiss77636ec2022-08-12 18:21:01 -0700311 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700312 baseType = sensorTypes[fanType];
313
314 auto findIndex = baseConfiguration->second.find("Index");
315 if (findIndex == baseConfiguration->second.end())
316 {
317 std::cerr << baseConfiguration->first << " missing index\n";
318 continue;
319 }
320 unsigned int configIndex = std::visit(
321 VariantToUnsignedIntVisitor(), findIndex->second);
322 if (configIndex != index)
323 {
324 continue;
325 }
Chris Sidesc361e222023-04-05 15:18:20 -0500326 if (fanType == FanTypes::aspeed ||
327 fanType == FanTypes::nuvoton || fanType == FanTypes::hpe)
Ed Tanousbb679322022-05-16 16:10:00 -0700328 {
Chris Sidesc361e222023-04-05 15:18:20 -0500329 // there will be only 1 aspeed or nuvoton or hpe sensor
Chris Sides3d5260d2023-04-10 15:45:00 -0500330 // object in sysfs, we found the fan
Zev Weiss77636ec2022-08-12 18:21:01 -0700331 sensorData = &cfgData;
Ed Tanousbb679322022-05-16 16:10:00 -0700332 break;
333 }
334 if (fanType == FanTypes::i2c)
335 {
Akshit Shah03d333e2023-08-23 22:14:28 +0000336 std::string deviceName =
Ed Tanousbb679322022-05-16 16:10:00 -0700337 fs::read_symlink(directory / "device").filename();
338
Akshit Shah03d333e2023-08-23 22:14:28 +0000339 size_t bus = 0;
340 size_t addr = 0;
341 if (!getDeviceBusAddr(deviceName, bus, addr))
James Feistde5e9702019-09-18 16:13:02 -0700342 {
Akshit Shah03d333e2023-08-23 22:14:28 +0000343 continue;
James Feistde5e9702019-09-18 16:13:02 -0700344 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800345
Ed Tanousbb679322022-05-16 16:10:00 -0700346 auto findBus = baseConfiguration->second.find("Bus");
347 auto findAddress =
348 baseConfiguration->second.find("Address");
349 if (findBus == baseConfiguration->second.end() ||
350 findAddress == baseConfiguration->second.end())
James Feistde5e9702019-09-18 16:13:02 -0700351 {
352 std::cerr << baseConfiguration->first
Ed Tanousbb679322022-05-16 16:10:00 -0700353 << " missing bus or address\n";
James Feistde5e9702019-09-18 16:13:02 -0700354 continue;
355 }
Ed Tanousbb679322022-05-16 16:10:00 -0700356 unsigned int configBus = std::visit(
357 VariantToUnsignedIntVisitor(), findBus->second);
358 unsigned int configAddress = std::visit(
359 VariantToUnsignedIntVisitor(), findAddress->second);
360
Akshit Shah03d333e2023-08-23 22:14:28 +0000361 if (configBus == bus && configAddress == addr)
James Feistde5e9702019-09-18 16:13:02 -0700362 {
Zev Weiss77636ec2022-08-12 18:21:01 -0700363 sensorData = &cfgData;
James Feistde5e9702019-09-18 16:13:02 -0700364 break;
365 }
Ed Tanousbb679322022-05-16 16:10:00 -0700366 }
367 }
368 if (sensorData == nullptr)
369 {
370 std::cerr << "failed to find match for " << path.string()
371 << "\n";
372 continue;
373 }
374
375 auto findSensorName = baseConfiguration->second.find("Name");
376
377 if (findSensorName == baseConfiguration->second.end())
378 {
379 std::cerr << "could not determine configuration name for "
380 << path.string() << "\n";
381 continue;
382 }
383 std::string sensorName =
384 std::get<std::string>(findSensorName->second);
385
386 // on rescans, only update sensors we were signaled by
387 auto findSensor = tachSensors.find(sensorName);
388 if (!firstScan && findSensor != tachSensors.end())
389 {
390 bool found = false;
391 for (auto it = sensorsChanged->begin();
392 it != sensorsChanged->end(); it++)
393 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700394 if (it->ends_with(findSensor->second->name))
James Feistde5e9702019-09-18 16:13:02 -0700395 {
Ed Tanousbb679322022-05-16 16:10:00 -0700396 sensorsChanged->erase(it);
397 findSensor->second = nullptr;
398 found = true;
399 break;
James Feistde5e9702019-09-18 16:13:02 -0700400 }
401 }
Ed Tanousbb679322022-05-16 16:10:00 -0700402 if (!found)
James Feistde5e9702019-09-18 16:13:02 -0700403 {
James Feist95b079b2018-11-21 09:28:00 -0800404 continue;
405 }
Ed Tanousbb679322022-05-16 16:10:00 -0700406 }
407 std::vector<thresholds::Threshold> sensorThresholds;
408 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
409 {
410 std::cerr << "error populating thresholds for " << sensorName
411 << "\n";
412 }
James Feist95b079b2018-11-21 09:28:00 -0800413
Ed Tanousbb679322022-05-16 16:10:00 -0700414 auto presenceConfig =
Zev Weiss054aad82022-08-18 01:37:34 -0700415 sensorData->find(cfgIntf + std::string(".Presence"));
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800416
Ed Tanousbb679322022-05-16 16:10:00 -0700417 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
418
419 // presence sensors are optional
420 if (presenceConfig != sensorData->end())
421 {
422 auto findPolarity = presenceConfig->second.find("Polarity");
423 auto findPinName = presenceConfig->second.find("PinName");
424
425 if (findPinName == presenceConfig->second.end() ||
426 findPolarity == presenceConfig->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800427 {
Ed Tanousbb679322022-05-16 16:10:00 -0700428 std::cerr << "Malformed Presence Configuration\n";
James Feistde5e9702019-09-18 16:13:02 -0700429 }
Ed Tanousbb679322022-05-16 16:10:00 -0700430 else
James Feistde5e9702019-09-18 16:13:02 -0700431 {
Ed Tanousbb679322022-05-16 16:10:00 -0700432 bool inverted =
433 std::get<std::string>(findPolarity->second) == "Low";
Ed Tanous2049bd22022-07-09 07:20:26 -0700434 if (const auto* pinName =
Ed Tanousbb679322022-05-16 16:10:00 -0700435 std::get_if<std::string>(&findPinName->second))
James Feistde5e9702019-09-18 16:13:02 -0700436 {
Ed Tanousbb679322022-05-16 16:10:00 -0700437 presenceSensor = std::make_unique<PresenceSensor>(
438 *pinName, inverted, io, sensorName);
James Feistde5e9702019-09-18 16:13:02 -0700439 }
440 else
441 {
Ed Tanousbb679322022-05-16 16:10:00 -0700442 std::cerr << "Malformed Presence pinName for sensor "
443 << sensorName << " \n";
James Feistde5e9702019-09-18 16:13:02 -0700444 }
445 }
Ed Tanousbb679322022-05-16 16:10:00 -0700446 }
447 std::optional<RedundancySensor>* redundancy = nullptr;
448 if (fanType == FanTypes::aspeed)
449 {
450 redundancy = &systemRedundancy;
451 }
452
Zev Weissa4d27682022-07-19 15:30:36 -0700453 PowerState powerState = getPowerState(baseConfiguration->second);
Yong Zhao77b3add2021-01-09 04:25:18 +0000454
Ed Tanousbb679322022-05-16 16:10:00 -0700455 constexpr double defaultMaxReading = 25000;
456 constexpr double defaultMinReading = 0;
457 std::pair<double, double> limits =
458 std::make_pair(defaultMinReading, defaultMaxReading);
459
460 auto connector =
Zev Weiss054aad82022-08-18 01:37:34 -0700461 sensorData->find(cfgIntf + std::string(".Connector"));
Ed Tanousbb679322022-05-16 16:10:00 -0700462
463 std::optional<std::string> led;
464 std::string pwmName;
465 fs::path pwmPath;
466
467 // The Mutable parameter is optional, defaulting to false
468 bool isValueMutable = false;
469 if (connector != sensorData->end())
470 {
471 auto findPwm = connector->second.find("Pwm");
472 if (findPwm != connector->second.end())
473 {
474 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
475 findPwm->second);
476 if (!findPwmPath(directory, pwm, pwmPath))
477 {
478 std::cerr << "Connector for " << sensorName
479 << " no pwm channel found!\n";
480 continue;
481 }
482
Patrick Williams779c96a2023-05-10 07:50:42 -0500483 fs::path pwmEnableFile = "pwm" + std::to_string(pwm + 1) +
484 "_enable";
Ed Tanousbb679322022-05-16 16:10:00 -0700485 fs::path enablePath = pwmPath.parent_path() / pwmEnableFile;
486 enablePwm(enablePath);
487
488 /* use pwm name override if found in configuration else
489 * use default */
490 auto findOverride = connector->second.find("PwmName");
491 if (findOverride != connector->second.end())
492 {
493 pwmName = std::visit(VariantToStringVisitor(),
494 findOverride->second);
495 }
496 else
497 {
498 pwmName = "Pwm_" + std::to_string(pwm + 1);
499 }
500
501 // Check PWM sensor mutability
502 auto findMutable = connector->second.find("Mutable");
503 if (findMutable != connector->second.end())
504 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700505 const auto* ptrMutable =
Ed Tanousbb679322022-05-16 16:10:00 -0700506 std::get_if<bool>(&(findMutable->second));
Ed Tanous2049bd22022-07-09 07:20:26 -0700507 if (ptrMutable != nullptr)
Ed Tanousbb679322022-05-16 16:10:00 -0700508 {
509 isValueMutable = *ptrMutable;
510 }
511 }
512 }
513 else
514 {
515 std::cerr << "Connector for " << sensorName
516 << " missing pwm!\n";
517 }
518
519 auto findLED = connector->second.find("LED");
520 if (findLED != connector->second.end())
521 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700522 const auto* ledName =
523 std::get_if<std::string>(&(findLED->second));
Ed Tanousbb679322022-05-16 16:10:00 -0700524 if (ledName == nullptr)
525 {
526 std::cerr << "Wrong format for LED of " << sensorName
527 << "\n";
528 }
529 else
530 {
531 led = *ledName;
532 }
533 }
534 }
535
536 findLimits(limits, baseConfiguration);
Josh Lehan5170fe62022-08-03 13:17:41 -0700537
Justin Ledford9c47bd72022-08-27 01:02:09 +0000538 enableFanInput(path);
539
Josh Lehan5170fe62022-08-03 13:17:41 -0700540 auto& tachSensor = tachSensors[sensorName];
541 tachSensor = nullptr;
542 tachSensor = std::make_shared<TachSensor>(
Ed Tanousbb679322022-05-16 16:10:00 -0700543 path.string(), baseType, objectServer, dbusConnection,
544 std::move(presenceSensor), redundancy, io, sensorName,
545 std::move(sensorThresholds), *interfacePath, limits, powerState,
546 led);
Josh Lehan5170fe62022-08-03 13:17:41 -0700547 tachSensor->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700548
549 if (!pwmPath.empty() && fs::exists(pwmPath) &&
Ed Tanous2049bd22022-07-09 07:20:26 -0700550 (pwmSensors.count(pwmPath) == 0U))
Ed Tanousbb679322022-05-16 16:10:00 -0700551 {
552 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
553 pwmName, pwmPath, dbusConnection, objectServer,
554 *interfacePath, "Fan", isValueMutable);
555 }
556 }
557
558 createRedundancySensor(tachSensors, dbusConnection, objectServer);
Patrick Williams597e8422023-10-20 11:19:01 -0500559 });
James Feistde5e9702019-09-18 16:13:02 -0700560 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700561 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
562 retries);
James Feist6714a252018-09-10 15:26:18 -0700563}
564
James Feistb6c0b912019-07-09 12:21:44 -0700565int main()
James Feist6714a252018-09-10 15:26:18 -0700566{
Ed Tanous1f978632023-02-28 18:16:39 -0800567 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700568 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700569 sdbusplus::asio::object_server objectServer(systemBus, true);
570
571 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanousd9067252022-10-22 15:35:52 -0700572 objectServer.add_manager("/xyz/openbmc_project/control");
Lei YUc2f83fe2022-12-02 14:49:47 +0800573 objectServer.add_manager("/xyz/openbmc_project/inventory");
James Feist6714a252018-09-10 15:26:18 -0700574 systemBus->request_name("xyz.openbmc_project.FanSensor");
Josh Lehan5170fe62022-08-03 13:17:41 -0700575 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
James Feist6714a252018-09-10 15:26:18 -0700576 tachSensors;
577 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
578 pwmSensors;
James Feist5591cf082020-07-15 16:44:54 -0700579 auto sensorsChanged =
580 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700581
Ed Tanous83db50c2023-03-01 10:20:24 -0800582 boost::asio::post(io, [&]() {
James Feist6714a252018-09-10 15:26:18 -0700583 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
584 nullptr);
585 });
586
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700587 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500588 std::function<void(sdbusplus::message_t&)> eventHandler =
589 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700590 if (message.is_method_error())
591 {
592 std::cerr << "callback method error\n";
593 return;
594 }
595 sensorsChanged->insert(message.get_path());
596 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800597 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700598
599 filterTimer.async_wait([&](const boost::system::error_code& ec) {
600 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700601 {
Ed Tanousbb679322022-05-16 16:10:00 -0700602 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700603 return;
604 }
Ed Tanousbb679322022-05-16 16:10:00 -0700605 if (ec)
606 {
607 std::cerr << "timer error\n";
608 return;
609 }
610 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
611 sensorsChanged, 5);
612 });
613 };
James Feist6714a252018-09-10 15:26:18 -0700614
Zev Weiss214d9712022-08-12 12:54:31 -0700615 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
616 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700617
James Feistdc6c55f2018-10-31 12:53:20 -0700618 // redundancy sensor
Patrick Williams92f8f512022-07-22 19:26:55 -0500619 std::function<void(sdbusplus::message_t&)> redundancyHandler =
620 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700621 createRedundancySensor(tachSensors, systemBus, objectServer);
622 };
Patrick Williams92f8f512022-07-22 19:26:55 -0500623 auto match = std::make_unique<sdbusplus::bus::match_t>(
624 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistdc6c55f2018-10-31 12:53:20 -0700625 "type='signal',member='PropertiesChanged',path_namespace='" +
626 std::string(inventoryPath) + "',arg0namespace='" +
627 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700628 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700629 matches.emplace_back(std::move(match));
630
Bruce Lee1263c3d2021-06-04 15:16:33 +0800631 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700632 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700633 return 0;
James Feist6714a252018-09-10 15:26:18 -0700634}