blob: 6dab8897bd3333192d9faed6fced141a366e6524 [file] [log] [blame]
Cheng C Yang209ec562019-03-12 16:37:44 +08001/*
2// Copyright (c) 2019 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*/
Josh Lehan0830c7b2019-10-08 16:35:09 -070016
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "PSUEvent.hpp"
18#include "PSUSensor.hpp"
19#include "Utils.hpp"
20
Cheng C Yang209ec562019-03-12 16:37:44 +080021#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070022#include <boost/container/flat_map.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080023#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26#include <sdbusplus/bus/match.hpp>
27
28#include <array>
Josh Lehan74d9bd92019-10-31 08:51:58 -070029#include <cmath>
James Feist24f02f22019-04-15 11:05:39 -070030#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080031#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <functional>
Cheng C Yang58b2b532019-05-31 00:19:45 +080033#include <iostream>
Cheng C Yang209ec562019-03-12 16:37:44 +080034#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <string>
Lei YUa2c7cea2020-12-23 14:07:28 +080036#include <string_view>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <utility>
38#include <variant>
39#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080040
Ed Tanous8a57ec02020-10-09 12:46:52 -070041static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070042
Ed Tanousa2df7862021-12-07 16:30:27 -080043// clang-format off
Glukhov Mikhail1f427c22023-02-27 14:25:29 +030044static constexpr auto sensorTypes{std::to_array<const char*>({
45 "ADM1266",
46 "ADM1272",
47 "ADM1275",
48 "ADM1278",
49 "ADM1293",
50 "ADS7830",
51 "AHE50DC_FAN",
52 "BMR490",
53 "DPS800",
54 "INA219",
55 "INA230",
56 "IPSPS",
57 "IR38060",
58 "IR38164",
59 "IR38263",
60 "ISL68137",
61 "ISL68220",
62 "ISL68223",
63 "ISL69225",
64 "ISL69243",
65 "ISL69260",
66 "LM25066",
67 "MAX16601",
68 "MAX20710",
69 "MAX20730",
70 "MAX20734",
71 "MAX20796",
72 "MAX34451",
73 "MP2971",
74 "MP2973",
75 "MP2975",
76 "MP5023",
77 "PLI1209BC",
78 "pmbus",
79 "PXE1610",
80 "RAA228000",
81 "RAA228228",
82 "RAA228620",
83 "RAA229001",
84 "RAA229004",
85 "RAA229126",
86 "TPS53679",
87 "TPS546D24",
88 "XDPE11280",
89 "XDPE12284"})};
90
Ed Tanousa2df7862021-12-07 16:30:27 -080091static constexpr auto pmbusNames{std::to_array<const char*>({
92 "adm1266",
93 "adm1272",
94 "adm1275",
95 "adm1278",
Zhikui Ren842e5432022-03-09 16:18:24 -080096 "adm1293",
Tim Chao6f379ce2022-02-24 11:08:09 +080097 "ads7830",
Zev Weissbc56cfb2022-11-21 16:40:09 -080098 "ahe50dc_fan",
linchuyuan5cf66df2021-12-21 17:38:55 -080099 "bmr490",
Ed Tanousa2df7862021-12-07 16:30:27 -0800100 "dps800",
101 "ina219",
102 "ina230",
103 "ipsps1",
Naresh Solankiadbcd7c2022-11-02 10:07:26 +0100104 "ir38060",
105 "ir38164",
106 "ir38263",
Ed Tanousa2df7862021-12-07 16:30:27 -0800107 "isl68137",
108 "isl68220",
109 "isl68223",
Khang Kieu51ad6672022-02-09 01:26:25 +0000110 "isl69225",
Ed Tanousa2df7862021-12-07 16:30:27 -0800111 "isl69243",
112 "isl69260",
113 "lm25066",
114 "max16601",
115 "max20710",
116 "max20730",
117 "max20734",
118 "max20796",
119 "max34451",
Shamim Alidccd1d42022-06-08 22:31:35 +0530120 "mp2971",
121 "mp2973",
Konstantin Aladyshevc61a4a42023-02-20 18:31:16 +0300122 "mp2975",
Howard Chiub58ac3a2021-12-07 17:12:56 +0800123 "mp5023",
Naresh Solankiadbcd7c2022-11-02 10:07:26 +0100124 "pli1209bc",
Ed Tanousa2df7862021-12-07 16:30:27 -0800125 "pmbus",
126 "pxe1610",
127 "raa228000",
128 "raa228228",
Tom Tung75da5152021-11-24 09:38:07 +0800129 "raa228620",
130 "raa229001",
Ed Tanousa2df7862021-12-07 16:30:27 -0800131 "raa229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800132 "raa229126",
Konstantin Aladyshev21e12c32022-10-19 18:56:10 +0300133 "tps53679",
Ed Tanousa2df7862021-12-07 16:30:27 -0800134 "tps546d24",
Naresh Solankiadbcd7c2022-11-02 10:07:26 +0100135 "xdpe11280",
Ed Tanousa2df7862021-12-07 16:30:27 -0800136 "xdpe12284"
137})};
138//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700139
Cheng C Yang209ec562019-03-12 16:37:44 +0800140namespace fs = std::filesystem;
141
Yong Libf8b1da2020-04-15 16:32:50 +0800142static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800143 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800144static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
145 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800146static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
147 pwmSensors;
148static boost::container::flat_map<std::string, std::string> sensorTable;
149static boost::container::flat_map<std::string, PSUProperty> labelMatch;
150static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800151static boost::container::flat_map<std::string, std::vector<std::string>>
152 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800153static boost::container::flat_map<
154 std::string,
155 boost::container::flat_map<std::string, std::vector<std::string>>>
156 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800157static boost::container::flat_map<std::string, std::vector<std::string>>
158 limitEventMatch;
159
Josh Lehan74d9bd92019-10-31 08:51:58 -0700160static std::vector<PSUProperty> psuProperties;
161
Cheng C Yang58b2b532019-05-31 00:19:45 +0800162// Function CheckEvent will check each attribute from eventMatch table in the
163// sysfs. If the attributes exists in sysfs, then store the complete path
164// of the attribute into eventPathList.
165void checkEvent(
166 const std::string& directory,
167 const boost::container::flat_map<std::string, std::vector<std::string>>&
168 eventMatch,
169 boost::container::flat_map<std::string, std::vector<std::string>>&
170 eventPathList)
171{
172 for (const auto& match : eventMatch)
173 {
174 const std::vector<std::string>& eventAttrs = match.second;
175 const std::string& eventName = match.first;
176 for (const auto& eventAttr : eventAttrs)
177 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700178 std::string eventPath = directory;
179 eventPath += "/";
180 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800181
182 std::ifstream eventFile(eventPath);
183 if (!eventFile.good())
184 {
185 continue;
186 }
187
188 eventPathList[eventName].push_back(eventPath);
189 }
190 }
191}
192
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800193// Check Group Events which contains more than one targets in each combine
194// events.
195void checkGroupEvent(
196 const std::string& directory,
197 const boost::container::flat_map<
198 std::string,
199 boost::container::flat_map<std::string, std::vector<std::string>>>&
200 groupEventMatch,
201 boost::container::flat_map<
202 std::string,
203 boost::container::flat_map<std::string, std::vector<std::string>>>&
204 groupEventPathList)
205{
206 for (const auto& match : groupEventMatch)
207 {
208 const std::string& groupEventName = match.first;
209 const boost::container::flat_map<std::string, std::vector<std::string>>
210 events = match.second;
211 boost::container::flat_map<std::string, std::vector<std::string>>
212 pathList;
213 for (const auto& match : events)
214 {
215 const std::string& eventName = match.first;
216 const std::vector<std::string>& eventAttrs = match.second;
217 for (const auto& eventAttr : eventAttrs)
218 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700219 std::string eventPath = directory;
220 eventPath += "/";
221 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800222 std::ifstream eventFile(eventPath);
223 if (!eventFile.good())
224 {
225 continue;
226 }
227
228 pathList[eventName].push_back(eventPath);
229 }
230 }
231 groupEventPathList[groupEventName] = pathList;
232 }
233}
234
Cheng C Yang58b2b532019-05-31 00:19:45 +0800235// Function checkEventLimits will check all the psu related xxx_input attributes
236// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
237// xxx_min_alarm exist, then store the existing paths of the alarm attributes
238// to eventPathList.
239void checkEventLimits(
240 const std::string& sensorPathStr,
241 const boost::container::flat_map<std::string, std::vector<std::string>>&
242 limitEventMatch,
243 boost::container::flat_map<std::string, std::vector<std::string>>&
244 eventPathList)
245{
Lei YUa2c7cea2020-12-23 14:07:28 +0800246 auto attributePartPos = sensorPathStr.find_last_of('_');
247 if (attributePartPos == std::string::npos)
248 {
249 // There is no '_' in the string, skip it
250 return;
251 }
252 auto attributePart =
253 std::string_view(sensorPathStr).substr(attributePartPos + 1);
254 if (attributePart != "input")
255 {
256 // If the sensor is not xxx_input, skip it
257 return;
258 }
259
260 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800261 for (const auto& limitMatch : limitEventMatch)
262 {
263 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
264 const std::string& eventName = limitMatch.first;
265 for (const auto& limitEventAttr : limitEventAttrs)
266 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800267 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800268 std::ifstream eventFile(limitEventPath);
269 if (!eventFile.good())
270 {
271 continue;
272 }
273 eventPathList[eventName].push_back(limitEventPath);
274 }
275 }
276}
Cheng C Yang916360b2019-05-07 18:47:16 +0800277
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530278static void
279 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
280 const std::string& interfacePath,
281 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
282 sdbusplus::asio::object_server& objectServer,
283 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800284{
Zev Weiss741f26e2022-08-12 18:21:02 -0700285 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800286 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700287 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800288 {
289 continue;
290 }
291
292 const std::string& sensorPathStr = sensorPath.string();
293 const std::string& pwmPathStr =
294 boost::replace_all_copy(sensorPathStr, "input", "target");
295 std::ifstream pwmFile(pwmPathStr);
296 if (!pwmFile.good())
297 {
298 continue;
299 }
300
301 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
302 if (findPWMSensor != pwmSensors.end())
303 {
304 continue;
305 }
306
Zev Weissd8c293a2022-08-15 18:58:41 -0700307 std::string name = "Pwm_";
308 name += psuName;
309 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700310 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700311
312 std::string objPath = interfacePath;
313 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700314 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700315
Cheng C Yang916360b2019-05-07 18:47:16 +0800316 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700317 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800318 }
319}
320
Zhikui Ren23c96e72020-11-05 22:32:28 -0800321static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800322 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800323 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
324 const ManagedObjectType& sensorConfigs,
325 const std::shared_ptr<boost::container::flat_set<std::string>>&
326 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800327{
Josh Lehan49cfba92019-10-08 16:50:42 -0700328 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800329 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800330
331 std::vector<fs::path> pmbusPaths;
332 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
333 {
334 std::cerr << "No PSU sensors in system\n";
335 return;
336 }
337
338 boost::container::flat_set<std::string> directories;
339 for (const auto& pmbusPath : pmbusPaths)
340 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800341 boost::container::flat_map<std::string, std::vector<std::string>>
342 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800343 boost::container::flat_map<
344 std::string,
345 boost::container::flat_map<std::string, std::vector<std::string>>>
346 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800347
348 std::ifstream nameFile(pmbusPath);
349 if (!nameFile.good())
350 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700351 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800352 continue;
353 }
354
355 std::string pmbusName;
356 std::getline(nameFile, pmbusName);
357 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700358
359 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
360 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800361 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700362 // To avoid this error message, add your driver name to
363 // the pmbusNames vector at the top of this file.
364 std::cerr << "Driver name " << pmbusName
365 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800366 continue;
367 }
368
Cheng C Yang209ec562019-03-12 16:37:44 +0800369 auto directory = pmbusPath.parent_path();
370
371 auto ret = directories.insert(directory.string());
372 if (!ret.second)
373 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700374 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800375 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800376 }
377
James Feistb6c0b912019-07-09 12:21:44 -0700378 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800379 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700380 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800381 if (findHyphen == std::string::npos)
382 {
383 std::cerr << "found bad device" << deviceName << "\n";
384 continue;
385 }
386 std::string busStr = deviceName.substr(0, findHyphen);
387 std::string addrStr = deviceName.substr(findHyphen + 1);
388
389 size_t bus = 0;
390 size_t addr = 0;
391
392 try
393 {
394 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700395 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800396 }
Patrick Williams26601e82021-10-06 12:43:25 -0500397 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800398 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700399 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
400 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800401 continue;
402 }
403
Zev Weisse8b97ee2022-08-12 15:23:51 -0700404 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800405 const SensorData* sensorData = nullptr;
406 const std::string* interfacePath = nullptr;
407 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800408 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800409
Zev Weiss741f26e2022-08-12 18:21:02 -0700410 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800411 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700412 sensorData = &cfgData;
Cheng C Yang209ec562019-03-12 16:37:44 +0800413 for (const char* type : sensorTypes)
414 {
Zev Weiss054aad82022-08-18 01:37:34 -0700415 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800416 if (sensorBase != sensorData->end())
417 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700418 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800419 sensorType = type;
420 break;
421 }
422 }
423 if (baseConfig == nullptr)
424 {
425 std::cerr << "error finding base configuration for "
426 << deviceName << "\n";
427 continue;
428 }
429
Zev Weisse8b97ee2022-08-12 15:23:51 -0700430 auto configBus = baseConfig->find("Bus");
431 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800432
Zev Weisse8b97ee2022-08-12 15:23:51 -0700433 if (configBus == baseConfig->end() ||
434 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800435 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800436 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800437 continue;
438 }
439
Matt Simmering6747eba2023-01-18 16:03:55 -0800440 const uint64_t* confBus =
441 std::get_if<uint64_t>(&(configBus->second));
442 const uint64_t* confAddr =
443 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800444 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800445 {
446 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700447 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800448 continue;
449 }
450
451 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800452 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700453 std::cerr << "Configuration skipping " << *confBus << "-"
454 << *confAddr << " because not " << bus << "-" << addr
455 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800456 continue;
457 }
458
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800459 std::vector<thresholds::Threshold> confThresholds;
460 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
461 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800462 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800463 }
464 thresholdConfSize = confThresholds.size();
465
Zev Weiss741f26e2022-08-12 18:21:02 -0700466 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800467 break;
468 }
469 if (interfacePath == nullptr)
470 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700471 // To avoid this error message, add your export map entry,
472 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800473 std::cerr << "failed to find match for " << deviceName << "\n";
474 continue;
475 }
476
Zev Weisse8b97ee2022-08-12 15:23:51 -0700477 auto findPSUName = baseConfig->find("Name");
478 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800479 {
480 std::cerr << "could not determine configuration name for "
481 << deviceName << "\n";
482 continue;
483 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800484 const std::string* psuName =
485 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800486 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800487 {
488 std::cerr << "Cannot find psu name, invalid configuration\n";
489 continue;
490 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800491
492 // on rescans, only update sensors we were signaled by
493 if (!firstScan)
494 {
Zhikui Renda98f092021-11-01 09:41:08 -0700495 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800496 auto it =
497 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
498 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800499 return s.ends_with(psuNameStr);
500 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800501
502 if (it == sensorsChanged->end())
503 {
504 continue;
505 }
506 sensorsChanged->erase(it);
507 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800508 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800509 checkGroupEvent(directory.string(), groupEventMatch,
510 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800511
Zev Weisse8b97ee2022-08-12 15:23:51 -0700512 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000513
Vijay Khemka996bad12019-05-28 15:15:16 -0700514 /* Check if there are more sensors in the same interface */
515 int i = 1;
516 std::vector<std::string> psuNames;
517 do
518 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700519 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700520 psuNames.push_back(
521 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700522 findPSUName = baseConfig->find("Name" + std::to_string(i++));
523 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700524
Cheng C Yange50345b2019-04-02 17:26:15 +0800525 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700526 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800527 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800528 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800529 continue;
530 }
531
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530532 /* read max value in sysfs for in, curr, power, temp, ... */
533 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
534 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700535 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530536 {
537 std::cerr << "No max name in PSU \n";
538 }
539 }
540
Zev Weiss8569bf22022-10-11 15:37:44 -0700541 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800542
Vijay Khemka996bad12019-05-28 15:15:16 -0700543 /* Find array of labels to be exposed if it is defined in config */
544 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700545 auto findLabelObj = baseConfig->find("Labels");
546 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700547 {
548 findLabels =
549 std::get<std::vector<std::string>>(findLabelObj->second);
550 }
551
Jason Ling5747fab2019-10-02 16:46:23 -0700552 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
553 std::smatch matches;
554
Cheng C Yange50345b2019-04-02 17:26:15 +0800555 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800556 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530557 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800558 std::string labelHead;
559 std::string sensorPathStr = sensorPath.string();
560 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700561 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700562 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
563 {
Josh Lehan06494452019-10-31 09:49:16 -0700564 // hwmon *_input filename without number:
565 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700566 sensorNameSubStr = matches[1];
567 }
568 else
569 {
Josh Lehan06494452019-10-31 09:49:16 -0700570 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700571 << sensorNameStr;
572 continue;
573 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800574
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530575 std::string labelPath;
576
577 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700578 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530579 if (pos != std::string::npos)
580 {
581
582 std::string sensorPathStrMax = sensorPathStr.substr(pos);
Ed Tanous2049bd22022-07-09 07:20:26 -0700583 if (sensorPathStrMax == "_max")
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530584 {
585 labelPath =
586 boost::replace_all_copy(sensorPathStr, "max", "label");
587 maxLabel = true;
588 }
589 else
590 {
591 labelPath = boost::replace_all_copy(sensorPathStr, "input",
592 "label");
593 maxLabel = false;
594 }
595 }
596 else
597 {
598 continue;
599 }
600
Cheng C Yangecba9de2019-09-12 23:41:50 +0800601 std::ifstream labelFile(labelPath);
602 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800603 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700604 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800605 {
606 std::cerr << "Input file " << sensorPath
607 << " has no corresponding label file\n";
608 }
Josh Lehan06494452019-10-31 09:49:16 -0700609 // hwmon *_input filename with number:
610 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700611 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800612 }
613 else
614 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800615 std::string label;
616 std::getline(labelFile, label);
617 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800618 auto findSensor = sensors.find(label);
619 if (findSensor != sensors.end())
620 {
621 continue;
622 }
623
Josh Lehan06494452019-10-31 09:49:16 -0700624 // hwmon corresponding *_label file contents:
625 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700626 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800627 }
628
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530629 /* append "max" for labelMatch */
630 if (maxLabel)
631 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700632 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530633 }
634
Ed Tanous8a57ec02020-10-09 12:46:52 -0700635 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700636 {
637 std::cerr << "Sensor type=\"" << sensorNameSubStr
638 << "\" label=\"" << labelHead << "\"\n";
639 }
640
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530641 checkPWMSensor(sensorPath, labelHead, *interfacePath,
642 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800643
Vijay Khemka996bad12019-05-28 15:15:16 -0700644 if (!findLabels.empty())
645 {
646 /* Check if this labelHead is enabled in config file */
647 if (std::find(findLabels.begin(), findLabels.end(),
648 labelHead) == findLabels.end())
649 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700650 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800651 {
652 std::cerr << "could not find " << labelHead
653 << " in the Labels list\n";
654 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700655 continue;
656 }
657 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800658
Cheng C Yange50345b2019-04-02 17:26:15 +0800659 auto findProperty = labelMatch.find(labelHead);
660 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800661 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700662 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700663 {
664 std::cerr << "Could not find matching default property for "
665 << labelHead << "\n";
666 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800667 continue;
668 }
669
Josh Lehan74d9bd92019-10-31 08:51:58 -0700670 // Protect the hardcoded labelMatch list from changes,
671 // by making a copy and modifying that instead.
672 // Avoid bleedthrough of one device's customizations to
673 // the next device, as each should be independently customizable.
674 psuProperties.push_back(findProperty->second);
675 auto psuProperty = psuProperties.rbegin();
676
677 // Use label head as prefix for reading from config file,
678 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
679 std::string keyName = labelHead + "_Name";
680 std::string keyScale = labelHead + "_Scale";
681 std::string keyMin = labelHead + "_Min";
682 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800683 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800684 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700685
686 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700687 auto findCustomName = baseConfig->find(keyName);
688 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700689 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700690 try
691 {
692 psuProperty->labelTypeName = std::visit(
693 VariantToStringVisitor(), findCustomName->second);
694 }
Patrick Williams26601e82021-10-06 12:43:25 -0500695 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700696 {
697 std::cerr << "Unable to parse " << keyName << "\n";
698 continue;
699 }
700
701 // All strings are valid, including empty string
702 customizedName = true;
703 }
704
705 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700706 auto findCustomScale = baseConfig->find(keyScale);
707 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700708 {
709 try
710 {
711 psuProperty->sensorScaleFactor = std::visit(
712 VariantToUnsignedIntVisitor(), findCustomScale->second);
713 }
Patrick Williams26601e82021-10-06 12:43:25 -0500714 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700715 {
716 std::cerr << "Unable to parse " << keyScale << "\n";
717 continue;
718 }
719
720 // Avoid later division by zero
721 if (psuProperty->sensorScaleFactor > 0)
722 {
723 customizedScale = true;
724 }
725 else
726 {
727 std::cerr << "Unable to accept " << keyScale << "\n";
728 continue;
729 }
730 }
731
Zev Weisse8b97ee2022-08-12 15:23:51 -0700732 auto findCustomMin = baseConfig->find(keyMin);
733 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700734 {
735 try
736 {
737 psuProperty->minReading = std::visit(
738 VariantToDoubleVisitor(), findCustomMin->second);
739 }
Patrick Williams26601e82021-10-06 12:43:25 -0500740 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700741 {
742 std::cerr << "Unable to parse " << keyMin << "\n";
743 continue;
744 }
745 }
746
Zev Weisse8b97ee2022-08-12 15:23:51 -0700747 auto findCustomMax = baseConfig->find(keyMax);
748 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700749 {
750 try
751 {
752 psuProperty->maxReading = std::visit(
753 VariantToDoubleVisitor(), findCustomMax->second);
754 }
Patrick Williams26601e82021-10-06 12:43:25 -0500755 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700756 {
757 std::cerr << "Unable to parse " << keyMax << "\n";
758 continue;
759 }
760 }
761
Zev Weisse8b97ee2022-08-12 15:23:51 -0700762 auto findCustomOffset = baseConfig->find(keyOffset);
763 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800764 {
765 try
766 {
767 psuProperty->sensorOffset = std::visit(
768 VariantToDoubleVisitor(), findCustomOffset->second);
769 }
Patrick Williams26601e82021-10-06 12:43:25 -0500770 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800771 {
772 std::cerr << "Unable to parse " << keyOffset << "\n";
773 continue;
774 }
775 }
776
Lotus Xucb5af732021-09-10 15:18:50 +0800777 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700778 auto findPowerState = baseConfig->find(keyPowerState);
779 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800780 {
781 std::string powerState = std::visit(VariantToStringVisitor(),
782 findPowerState->second);
783 setReadState(powerState, readState);
784 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700785 if (!(psuProperty->minReading < psuProperty->maxReading))
786 {
787 std::cerr << "Min must be less than Max\n";
788 continue;
789 }
790
791 // If the sensor name is being customized by config file,
792 // then prefix/suffix composition becomes not necessary,
793 // and in fact not wanted, because it gets in the way.
794 std::string psuNameFromIndex;
795 if (!customizedName)
796 {
797 /* Find out sensor name index for this label */
798 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500799 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700800 if (std::regex_search(labelHead, matches, rgx))
801 {
802 nameIndex = std::stoi(matches[1]);
803
804 // Decrement to preserve alignment, because hwmon
805 // human-readable filenames and labels use 1-based
806 // numbering, but the "Name", "Name1", "Name2", etc. naming
807 // convention (the psuNames vector) uses 0-based numbering.
808 if (nameIndex > 0)
809 {
810 --nameIndex;
811 }
812 }
813 else
814 {
815 nameIndex = 0;
816 }
817
818 if (psuNames.size() <= nameIndex)
819 {
820 std::cerr << "Could not pair " << labelHead
821 << " with a Name field\n";
822 continue;
823 }
824
825 psuNameFromIndex = psuNames[nameIndex];
826
Ed Tanous8a57ec02020-10-09 12:46:52 -0700827 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700828 {
829 std::cerr << "Sensor label head " << labelHead
830 << " paired with " << psuNameFromIndex
831 << " at index " << nameIndex << "\n";
832 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700833 }
834
Cheng C Yang58b2b532019-05-31 00:19:45 +0800835 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
836
Josh Lehan74d9bd92019-10-31 08:51:58 -0700837 // Similarly, if sensor scaling factor is being customized,
838 // then the below power-of-10 constraint becomes unnecessary,
839 // as config should be able to specify an arbitrary divisor.
840 unsigned int factor = psuProperty->sensorScaleFactor;
841 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700842 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700843 // Preserve existing usage of hardcoded labelMatch table below
844 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700845
Josh Lehan74d9bd92019-10-31 08:51:58 -0700846 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700847 char firstChar =
848 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700849 std::string strScaleFactor =
850 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
851
852 // Preserve existing configs by accepting earlier syntax,
853 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700854 auto findScaleFactor = baseConfig->find(strScaleFactor);
855 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700856 {
857 factor = std::visit(VariantToIntVisitor(),
858 findScaleFactor->second);
859 }
860
Ed Tanous8a57ec02020-10-09 12:46:52 -0700861 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700862 {
863 std::cerr << "Sensor scaling factor " << factor
864 << " string " << strScaleFactor << "\n";
865 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700866 }
867
Vijay Khemka996bad12019-05-28 15:15:16 -0700868 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530869 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
870 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800871 {
James Feist17ab6e02019-06-25 12:28:13 -0700872 std::cerr << "error populating thresholds for "
873 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800874 }
875
Zev Weiss6b6891c2021-04-22 02:46:21 -0500876 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
877 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800878 {
Jason Ling5747fab2019-10-02 16:46:23 -0700879 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700880 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800881 continue;
882 }
883
Ed Tanous8a57ec02020-10-09 12:46:52 -0700884 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700885 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700886 std::cerr << "Sensor properties: Name \""
887 << psuProperty->labelTypeName << "\" Scale "
888 << psuProperty->sensorScaleFactor << " Min "
889 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800890 << psuProperty->maxReading << " Offset "
891 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700892 }
893
894 std::string sensorName = psuProperty->labelTypeName;
895 if (customizedName)
896 {
897 if (sensorName.empty())
898 {
899 // Allow selective disabling of an individual sensor,
900 // by customizing its name to an empty string.
901 std::cerr << "Sensor disabled, empty string\n";
902 continue;
903 }
904 }
905 else
906 {
907 // Sensor name not customized, do prefix/suffix composition,
908 // preserving default behavior by using psuNameFromIndex.
909 sensorName =
910 psuNameFromIndex + " " + psuProperty->labelTypeName;
911 }
912
Ed Tanous8a57ec02020-10-09 12:46:52 -0700913 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700914 {
915 std::cerr << "Sensor name \"" << sensorName << "\" path \""
916 << sensorPathStr << "\" type \"" << sensorType
917 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700918 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800919 // destruct existing one first if already created
920 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800921 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800922 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800923 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000924 readState, findSensorUnit->second, factor,
925 psuProperty->maxReading, psuProperty->minReading,
926 psuProperty->sensorOffset, labelHead, thresholdConfSize,
927 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800928 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700929 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700930 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700931 {
932 std::cerr << "Created " << numCreated << " sensors so far\n";
933 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800934 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800935
936 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800937 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800938 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000939 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
940 *psuName, readState,
941 eventPathList, groupEventPathList,
942 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800943 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700944
Ed Tanous8a57ec02020-10-09 12:46:52 -0700945 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700946 {
947 std::cerr << "Created total of " << numCreated << " sensors\n";
948 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800949}
Cheng C Yang209ec562019-03-12 16:37:44 +0800950
Zhikui Ren23c96e72020-11-05 22:32:28 -0800951void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800952 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800953 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
954 const std::shared_ptr<boost::container::flat_set<std::string>>&
955 sensorsChanged)
956{
957 auto getter = std::make_shared<GetSensorConfiguration>(
958 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
959 const ManagedObjectType& sensorConfigs) {
960 createSensorsCallback(io, objectServer, dbusConnection,
961 sensorConfigs, sensorsChanged);
962 });
963 getter->getConfiguration(
964 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
965}
966
Cheng C Yang916360b2019-05-07 18:47:16 +0800967void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800968{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500969 sensorTable = {{"power", sensor_paths::unitWatts},
970 {"curr", sensor_paths::unitAmperes},
971 {"temp", sensor_paths::unitDegreesC},
972 {"in", sensor_paths::unitVolts},
973 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800974
Jeff Line41d52f2021-04-07 19:38:51 +0800975 labelMatch = {
976 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800977 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
978 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800979 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
980 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
981 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
982 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800983 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
984 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
985 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800986 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
987 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
988 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
989 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
993 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
994 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
995 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
997 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
998 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
999 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1000 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1001 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1002 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1003 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1004 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1005 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1006 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1007 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1008 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1009 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1010 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1011 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1012 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1013 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1014 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1015 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1016 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1017 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1018 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1019 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1020 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1021 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001022 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001023 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001024 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1025 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1026 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1027 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1028 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1029 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001030 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001031 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1032 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001033 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1034 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1035 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1036 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1037 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1038 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1039 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1040 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1041 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1042 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1043 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1044 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1045 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1046 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1047 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001048 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1049 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1050 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001051 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1052 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1053 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1054 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1055 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1056 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1057 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1058 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1059 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001060 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1061 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1062 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001063
Matt Simmering6747eba2023-01-18 16:03:55 -08001064 pwmTable = {{"fan1", "Fan_1"},
1065 {"fan2", "Fan_2"},
1066 {"fan3", "Fan_3"},
1067 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001068
1069 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1070 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1071
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001072 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1073 {"Failure", {"in2_alarm"}},
1074 {"ACLost", {"in1_beep"}},
1075 {"ConfigureError", {"in1_fault"}}};
1076
1077 groupEventMatch = {{"FanFault",
1078 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001079 {"fan2", {"fan2_alarm", "fan2_fault"}},
1080 {"fan3", {"fan3_alarm", "fan3_fault"}},
1081 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001082}
1083
James Feistb6c0b912019-07-09 12:21:44 -07001084int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001085{
Ed Tanous1f978632023-02-28 18:16:39 -08001086 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001087 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1088
Ed Tanous14ed5e92022-07-12 15:50:23 -07001089 sdbusplus::asio::object_server objectServer(systemBus, true);
1090 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001091 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001092 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001093 auto sensorsChanged =
1094 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001095
Cheng C Yang916360b2019-05-07 18:47:16 +08001096 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001097
Ed Tanous83db50c2023-03-01 10:20:24 -08001098 boost::asio::post(io, [&]() { createSensors(io, objectServer, systemBus, nullptr); });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001099 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001100 std::function<void(sdbusplus::message_t&)> eventHandler =
1101 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001102 if (message.is_method_error())
1103 {
1104 std::cerr << "callback method error\n";
1105 return;
1106 }
1107 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001108 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001109 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1110 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001111 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001112 return;
1113 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001114 if (ec)
1115 {
1116 std::cerr << "timer error\n";
1117 }
1118 createSensors(io, objectServer, systemBus, sensorsChanged);
1119 });
1120 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001121
Zev Weiss214d9712022-08-12 12:54:31 -07001122 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1123 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +08001124 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001125 io.run();
1126}