blob: 80589966fced85485ee2d04479980b1330804e98 [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",
Glukhov Mikhail83209bf2023-03-06 08:21:34 +030077 "NCP4200",
Glukhov Mikhail1f427c22023-02-27 14:25:29 +030078 "PLI1209BC",
79 "pmbus",
80 "PXE1610",
81 "RAA228000",
82 "RAA228228",
83 "RAA228620",
84 "RAA229001",
85 "RAA229004",
86 "RAA229126",
87 "TPS53679",
88 "TPS546D24",
89 "XDPE11280",
90 "XDPE12284"})};
91
Ed Tanousa2df7862021-12-07 16:30:27 -080092static constexpr auto pmbusNames{std::to_array<const char*>({
93 "adm1266",
94 "adm1272",
95 "adm1275",
96 "adm1278",
Zhikui Ren842e5432022-03-09 16:18:24 -080097 "adm1293",
Tim Chao6f379ce2022-02-24 11:08:09 +080098 "ads7830",
Zev Weissbc56cfb2022-11-21 16:40:09 -080099 "ahe50dc_fan",
linchuyuan5cf66df2021-12-21 17:38:55 -0800100 "bmr490",
Ed Tanousa2df7862021-12-07 16:30:27 -0800101 "dps800",
102 "ina219",
103 "ina230",
104 "ipsps1",
Naresh Solankiadbcd7c2022-11-02 10:07:26 +0100105 "ir38060",
106 "ir38164",
107 "ir38263",
Ed Tanousa2df7862021-12-07 16:30:27 -0800108 "isl68137",
109 "isl68220",
110 "isl68223",
Khang Kieu51ad6672022-02-09 01:26:25 +0000111 "isl69225",
Ed Tanousa2df7862021-12-07 16:30:27 -0800112 "isl69243",
113 "isl69260",
114 "lm25066",
115 "max16601",
116 "max20710",
117 "max20730",
118 "max20734",
119 "max20796",
120 "max34451",
Shamim Alidccd1d42022-06-08 22:31:35 +0530121 "mp2971",
122 "mp2973",
Konstantin Aladyshevc61a4a42023-02-20 18:31:16 +0300123 "mp2975",
Howard Chiub58ac3a2021-12-07 17:12:56 +0800124 "mp5023",
Glukhov Mikhail83209bf2023-03-06 08:21:34 +0300125 "ncp4200",
Naresh Solankiadbcd7c2022-11-02 10:07:26 +0100126 "pli1209bc",
Ed Tanousa2df7862021-12-07 16:30:27 -0800127 "pmbus",
128 "pxe1610",
129 "raa228000",
130 "raa228228",
Tom Tung75da5152021-11-24 09:38:07 +0800131 "raa228620",
132 "raa229001",
Ed Tanousa2df7862021-12-07 16:30:27 -0800133 "raa229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800134 "raa229126",
Konstantin Aladyshev21e12c32022-10-19 18:56:10 +0300135 "tps53679",
Ed Tanousa2df7862021-12-07 16:30:27 -0800136 "tps546d24",
Naresh Solankiadbcd7c2022-11-02 10:07:26 +0100137 "xdpe11280",
Ed Tanousa2df7862021-12-07 16:30:27 -0800138 "xdpe12284"
139})};
140//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700141
Cheng C Yang209ec562019-03-12 16:37:44 +0800142namespace fs = std::filesystem;
143
Yong Libf8b1da2020-04-15 16:32:50 +0800144static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800145 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800146static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
147 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800148static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
149 pwmSensors;
150static boost::container::flat_map<std::string, std::string> sensorTable;
151static boost::container::flat_map<std::string, PSUProperty> labelMatch;
152static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800153static boost::container::flat_map<std::string, std::vector<std::string>>
154 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800155static boost::container::flat_map<
156 std::string,
157 boost::container::flat_map<std::string, std::vector<std::string>>>
158 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800159static boost::container::flat_map<std::string, std::vector<std::string>>
160 limitEventMatch;
161
Josh Lehan74d9bd92019-10-31 08:51:58 -0700162static std::vector<PSUProperty> psuProperties;
163
Cheng C Yang58b2b532019-05-31 00:19:45 +0800164// Function CheckEvent will check each attribute from eventMatch table in the
165// sysfs. If the attributes exists in sysfs, then store the complete path
166// of the attribute into eventPathList.
167void checkEvent(
168 const std::string& directory,
169 const boost::container::flat_map<std::string, std::vector<std::string>>&
170 eventMatch,
171 boost::container::flat_map<std::string, std::vector<std::string>>&
172 eventPathList)
173{
174 for (const auto& match : eventMatch)
175 {
176 const std::vector<std::string>& eventAttrs = match.second;
177 const std::string& eventName = match.first;
178 for (const auto& eventAttr : eventAttrs)
179 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700180 std::string eventPath = directory;
181 eventPath += "/";
182 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800183
184 std::ifstream eventFile(eventPath);
185 if (!eventFile.good())
186 {
187 continue;
188 }
189
190 eventPathList[eventName].push_back(eventPath);
191 }
192 }
193}
194
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800195// Check Group Events which contains more than one targets in each combine
196// events.
197void checkGroupEvent(
198 const std::string& directory,
199 const boost::container::flat_map<
200 std::string,
201 boost::container::flat_map<std::string, std::vector<std::string>>>&
202 groupEventMatch,
203 boost::container::flat_map<
204 std::string,
205 boost::container::flat_map<std::string, std::vector<std::string>>>&
206 groupEventPathList)
207{
208 for (const auto& match : groupEventMatch)
209 {
210 const std::string& groupEventName = match.first;
211 const boost::container::flat_map<std::string, std::vector<std::string>>
212 events = match.second;
213 boost::container::flat_map<std::string, std::vector<std::string>>
214 pathList;
215 for (const auto& match : events)
216 {
217 const std::string& eventName = match.first;
218 const std::vector<std::string>& eventAttrs = match.second;
219 for (const auto& eventAttr : eventAttrs)
220 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700221 std::string eventPath = directory;
222 eventPath += "/";
223 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800224 std::ifstream eventFile(eventPath);
225 if (!eventFile.good())
226 {
227 continue;
228 }
229
230 pathList[eventName].push_back(eventPath);
231 }
232 }
233 groupEventPathList[groupEventName] = pathList;
234 }
235}
236
Cheng C Yang58b2b532019-05-31 00:19:45 +0800237// Function checkEventLimits will check all the psu related xxx_input attributes
238// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
239// xxx_min_alarm exist, then store the existing paths of the alarm attributes
240// to eventPathList.
241void checkEventLimits(
242 const std::string& sensorPathStr,
243 const boost::container::flat_map<std::string, std::vector<std::string>>&
244 limitEventMatch,
245 boost::container::flat_map<std::string, std::vector<std::string>>&
246 eventPathList)
247{
Lei YUa2c7cea2020-12-23 14:07:28 +0800248 auto attributePartPos = sensorPathStr.find_last_of('_');
249 if (attributePartPos == std::string::npos)
250 {
251 // There is no '_' in the string, skip it
252 return;
253 }
254 auto attributePart =
255 std::string_view(sensorPathStr).substr(attributePartPos + 1);
256 if (attributePart != "input")
257 {
258 // If the sensor is not xxx_input, skip it
259 return;
260 }
261
262 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800263 for (const auto& limitMatch : limitEventMatch)
264 {
265 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
266 const std::string& eventName = limitMatch.first;
267 for (const auto& limitEventAttr : limitEventAttrs)
268 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800269 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800270 std::ifstream eventFile(limitEventPath);
271 if (!eventFile.good())
272 {
273 continue;
274 }
275 eventPathList[eventName].push_back(limitEventPath);
276 }
277 }
278}
Cheng C Yang916360b2019-05-07 18:47:16 +0800279
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530280static void
281 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
282 const std::string& interfacePath,
283 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
284 sdbusplus::asio::object_server& objectServer,
285 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800286{
Zev Weiss741f26e2022-08-12 18:21:02 -0700287 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800288 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700289 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800290 {
291 continue;
292 }
293
294 const std::string& sensorPathStr = sensorPath.string();
295 const std::string& pwmPathStr =
296 boost::replace_all_copy(sensorPathStr, "input", "target");
297 std::ifstream pwmFile(pwmPathStr);
298 if (!pwmFile.good())
299 {
300 continue;
301 }
302
303 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
304 if (findPWMSensor != pwmSensors.end())
305 {
306 continue;
307 }
308
Zev Weissd8c293a2022-08-15 18:58:41 -0700309 std::string name = "Pwm_";
310 name += psuName;
311 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700312 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700313
314 std::string objPath = interfacePath;
315 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700316 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700317
Cheng C Yang916360b2019-05-07 18:47:16 +0800318 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700319 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800320 }
321}
322
Zhikui Ren23c96e72020-11-05 22:32:28 -0800323static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800324 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800325 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
326 const ManagedObjectType& sensorConfigs,
327 const std::shared_ptr<boost::container::flat_set<std::string>>&
328 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800329{
Josh Lehan49cfba92019-10-08 16:50:42 -0700330 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800331 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800332
333 std::vector<fs::path> pmbusPaths;
334 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
335 {
336 std::cerr << "No PSU sensors in system\n";
337 return;
338 }
339
340 boost::container::flat_set<std::string> directories;
341 for (const auto& pmbusPath : pmbusPaths)
342 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800343 boost::container::flat_map<std::string, std::vector<std::string>>
344 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800345 boost::container::flat_map<
346 std::string,
347 boost::container::flat_map<std::string, std::vector<std::string>>>
348 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800349
350 std::ifstream nameFile(pmbusPath);
351 if (!nameFile.good())
352 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700353 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800354 continue;
355 }
356
357 std::string pmbusName;
358 std::getline(nameFile, pmbusName);
359 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700360
361 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
362 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800363 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700364 // To avoid this error message, add your driver name to
365 // the pmbusNames vector at the top of this file.
366 std::cerr << "Driver name " << pmbusName
367 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800368 continue;
369 }
370
Cheng C Yang209ec562019-03-12 16:37:44 +0800371 auto directory = pmbusPath.parent_path();
372
373 auto ret = directories.insert(directory.string());
374 if (!ret.second)
375 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700376 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800377 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800378 }
379
James Feistb6c0b912019-07-09 12:21:44 -0700380 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800381 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700382 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800383 if (findHyphen == std::string::npos)
384 {
385 std::cerr << "found bad device" << deviceName << "\n";
386 continue;
387 }
388 std::string busStr = deviceName.substr(0, findHyphen);
389 std::string addrStr = deviceName.substr(findHyphen + 1);
390
391 size_t bus = 0;
392 size_t addr = 0;
393
394 try
395 {
396 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700397 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800398 }
Patrick Williams26601e82021-10-06 12:43:25 -0500399 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800400 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700401 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
402 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800403 continue;
404 }
405
Zev Weisse8b97ee2022-08-12 15:23:51 -0700406 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800407 const SensorData* sensorData = nullptr;
408 const std::string* interfacePath = nullptr;
409 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800410 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800411
Zev Weiss741f26e2022-08-12 18:21:02 -0700412 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800413 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700414 sensorData = &cfgData;
Cheng C Yang209ec562019-03-12 16:37:44 +0800415 for (const char* type : sensorTypes)
416 {
Zev Weiss054aad82022-08-18 01:37:34 -0700417 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800418 if (sensorBase != sensorData->end())
419 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700420 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800421 sensorType = type;
422 break;
423 }
424 }
425 if (baseConfig == nullptr)
426 {
427 std::cerr << "error finding base configuration for "
428 << deviceName << "\n";
429 continue;
430 }
431
Zev Weisse8b97ee2022-08-12 15:23:51 -0700432 auto configBus = baseConfig->find("Bus");
433 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800434
Zev Weisse8b97ee2022-08-12 15:23:51 -0700435 if (configBus == baseConfig->end() ||
436 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800437 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800438 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800439 continue;
440 }
441
Matt Simmering6747eba2023-01-18 16:03:55 -0800442 const uint64_t* confBus =
443 std::get_if<uint64_t>(&(configBus->second));
444 const uint64_t* confAddr =
445 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800446 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800447 {
448 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700449 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800450 continue;
451 }
452
453 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800454 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700455 std::cerr << "Configuration skipping " << *confBus << "-"
456 << *confAddr << " because not " << bus << "-" << addr
457 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800458 continue;
459 }
460
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800461 std::vector<thresholds::Threshold> confThresholds;
462 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
463 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800464 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800465 }
466 thresholdConfSize = confThresholds.size();
467
Zev Weiss741f26e2022-08-12 18:21:02 -0700468 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800469 break;
470 }
471 if (interfacePath == nullptr)
472 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700473 // To avoid this error message, add your export map entry,
474 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800475 std::cerr << "failed to find match for " << deviceName << "\n";
476 continue;
477 }
478
Zev Weisse8b97ee2022-08-12 15:23:51 -0700479 auto findPSUName = baseConfig->find("Name");
480 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800481 {
482 std::cerr << "could not determine configuration name for "
483 << deviceName << "\n";
484 continue;
485 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800486 const std::string* psuName =
487 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800488 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800489 {
490 std::cerr << "Cannot find psu name, invalid configuration\n";
491 continue;
492 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800493
494 // on rescans, only update sensors we were signaled by
495 if (!firstScan)
496 {
Zhikui Renda98f092021-11-01 09:41:08 -0700497 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800498 auto it =
499 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
500 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800501 return s.ends_with(psuNameStr);
502 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800503
504 if (it == sensorsChanged->end())
505 {
506 continue;
507 }
508 sensorsChanged->erase(it);
509 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800510 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800511 checkGroupEvent(directory.string(), groupEventMatch,
512 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800513
Zev Weisse8b97ee2022-08-12 15:23:51 -0700514 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000515
Vijay Khemka996bad12019-05-28 15:15:16 -0700516 /* Check if there are more sensors in the same interface */
517 int i = 1;
518 std::vector<std::string> psuNames;
519 do
520 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700521 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700522 psuNames.push_back(
523 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700524 findPSUName = baseConfig->find("Name" + std::to_string(i++));
525 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700526
Cheng C Yange50345b2019-04-02 17:26:15 +0800527 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700528 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800529 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800530 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800531 continue;
532 }
533
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530534 /* read max value in sysfs for in, curr, power, temp, ... */
535 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
536 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700537 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530538 {
539 std::cerr << "No max name in PSU \n";
540 }
541 }
542
Zev Weiss8569bf22022-10-11 15:37:44 -0700543 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800544
Vijay Khemka996bad12019-05-28 15:15:16 -0700545 /* Find array of labels to be exposed if it is defined in config */
546 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700547 auto findLabelObj = baseConfig->find("Labels");
548 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700549 {
550 findLabels =
551 std::get<std::vector<std::string>>(findLabelObj->second);
552 }
553
Jason Ling5747fab2019-10-02 16:46:23 -0700554 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
555 std::smatch matches;
556
Cheng C Yange50345b2019-04-02 17:26:15 +0800557 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800558 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530559 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800560 std::string labelHead;
561 std::string sensorPathStr = sensorPath.string();
562 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700563 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700564 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
565 {
Josh Lehan06494452019-10-31 09:49:16 -0700566 // hwmon *_input filename without number:
567 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700568 sensorNameSubStr = matches[1];
569 }
570 else
571 {
Josh Lehan06494452019-10-31 09:49:16 -0700572 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700573 << sensorNameStr;
574 continue;
575 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800576
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530577 std::string labelPath;
578
579 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700580 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530581 if (pos != std::string::npos)
582 {
583
584 std::string sensorPathStrMax = sensorPathStr.substr(pos);
Ed Tanous2049bd22022-07-09 07:20:26 -0700585 if (sensorPathStrMax == "_max")
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530586 {
587 labelPath =
588 boost::replace_all_copy(sensorPathStr, "max", "label");
589 maxLabel = true;
590 }
591 else
592 {
593 labelPath = boost::replace_all_copy(sensorPathStr, "input",
594 "label");
595 maxLabel = false;
596 }
597 }
598 else
599 {
600 continue;
601 }
602
Cheng C Yangecba9de2019-09-12 23:41:50 +0800603 std::ifstream labelFile(labelPath);
604 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800605 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700606 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800607 {
608 std::cerr << "Input file " << sensorPath
609 << " has no corresponding label file\n";
610 }
Josh Lehan06494452019-10-31 09:49:16 -0700611 // hwmon *_input filename with number:
612 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700613 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800614 }
615 else
616 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800617 std::string label;
618 std::getline(labelFile, label);
619 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800620 auto findSensor = sensors.find(label);
621 if (findSensor != sensors.end())
622 {
623 continue;
624 }
625
Josh Lehan06494452019-10-31 09:49:16 -0700626 // hwmon corresponding *_label file contents:
627 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700628 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800629 }
630
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530631 /* append "max" for labelMatch */
632 if (maxLabel)
633 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700634 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530635 }
636
Ed Tanous8a57ec02020-10-09 12:46:52 -0700637 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700638 {
639 std::cerr << "Sensor type=\"" << sensorNameSubStr
640 << "\" label=\"" << labelHead << "\"\n";
641 }
642
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530643 checkPWMSensor(sensorPath, labelHead, *interfacePath,
644 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800645
Vijay Khemka996bad12019-05-28 15:15:16 -0700646 if (!findLabels.empty())
647 {
648 /* Check if this labelHead is enabled in config file */
649 if (std::find(findLabels.begin(), findLabels.end(),
650 labelHead) == findLabels.end())
651 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700652 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800653 {
654 std::cerr << "could not find " << labelHead
655 << " in the Labels list\n";
656 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700657 continue;
658 }
659 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800660
Cheng C Yange50345b2019-04-02 17:26:15 +0800661 auto findProperty = labelMatch.find(labelHead);
662 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800663 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700664 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700665 {
666 std::cerr << "Could not find matching default property for "
667 << labelHead << "\n";
668 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800669 continue;
670 }
671
Josh Lehan74d9bd92019-10-31 08:51:58 -0700672 // Protect the hardcoded labelMatch list from changes,
673 // by making a copy and modifying that instead.
674 // Avoid bleedthrough of one device's customizations to
675 // the next device, as each should be independently customizable.
676 psuProperties.push_back(findProperty->second);
677 auto psuProperty = psuProperties.rbegin();
678
679 // Use label head as prefix for reading from config file,
680 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
681 std::string keyName = labelHead + "_Name";
682 std::string keyScale = labelHead + "_Scale";
683 std::string keyMin = labelHead + "_Min";
684 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800685 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800686 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700687
688 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700689 auto findCustomName = baseConfig->find(keyName);
690 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700691 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700692 try
693 {
694 psuProperty->labelTypeName = std::visit(
695 VariantToStringVisitor(), findCustomName->second);
696 }
Patrick Williams26601e82021-10-06 12:43:25 -0500697 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700698 {
699 std::cerr << "Unable to parse " << keyName << "\n";
700 continue;
701 }
702
703 // All strings are valid, including empty string
704 customizedName = true;
705 }
706
707 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700708 auto findCustomScale = baseConfig->find(keyScale);
709 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700710 {
711 try
712 {
713 psuProperty->sensorScaleFactor = std::visit(
714 VariantToUnsignedIntVisitor(), findCustomScale->second);
715 }
Patrick Williams26601e82021-10-06 12:43:25 -0500716 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700717 {
718 std::cerr << "Unable to parse " << keyScale << "\n";
719 continue;
720 }
721
722 // Avoid later division by zero
723 if (psuProperty->sensorScaleFactor > 0)
724 {
725 customizedScale = true;
726 }
727 else
728 {
729 std::cerr << "Unable to accept " << keyScale << "\n";
730 continue;
731 }
732 }
733
Zev Weisse8b97ee2022-08-12 15:23:51 -0700734 auto findCustomMin = baseConfig->find(keyMin);
735 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700736 {
737 try
738 {
739 psuProperty->minReading = std::visit(
740 VariantToDoubleVisitor(), findCustomMin->second);
741 }
Patrick Williams26601e82021-10-06 12:43:25 -0500742 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700743 {
744 std::cerr << "Unable to parse " << keyMin << "\n";
745 continue;
746 }
747 }
748
Zev Weisse8b97ee2022-08-12 15:23:51 -0700749 auto findCustomMax = baseConfig->find(keyMax);
750 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700751 {
752 try
753 {
754 psuProperty->maxReading = std::visit(
755 VariantToDoubleVisitor(), findCustomMax->second);
756 }
Patrick Williams26601e82021-10-06 12:43:25 -0500757 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700758 {
759 std::cerr << "Unable to parse " << keyMax << "\n";
760 continue;
761 }
762 }
763
Zev Weisse8b97ee2022-08-12 15:23:51 -0700764 auto findCustomOffset = baseConfig->find(keyOffset);
765 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800766 {
767 try
768 {
769 psuProperty->sensorOffset = std::visit(
770 VariantToDoubleVisitor(), findCustomOffset->second);
771 }
Patrick Williams26601e82021-10-06 12:43:25 -0500772 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800773 {
774 std::cerr << "Unable to parse " << keyOffset << "\n";
775 continue;
776 }
777 }
778
Lotus Xucb5af732021-09-10 15:18:50 +0800779 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700780 auto findPowerState = baseConfig->find(keyPowerState);
781 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800782 {
783 std::string powerState = std::visit(VariantToStringVisitor(),
784 findPowerState->second);
785 setReadState(powerState, readState);
786 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700787 if (!(psuProperty->minReading < psuProperty->maxReading))
788 {
789 std::cerr << "Min must be less than Max\n";
790 continue;
791 }
792
793 // If the sensor name is being customized by config file,
794 // then prefix/suffix composition becomes not necessary,
795 // and in fact not wanted, because it gets in the way.
796 std::string psuNameFromIndex;
797 if (!customizedName)
798 {
799 /* Find out sensor name index for this label */
800 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500801 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700802 if (std::regex_search(labelHead, matches, rgx))
803 {
804 nameIndex = std::stoi(matches[1]);
805
806 // Decrement to preserve alignment, because hwmon
807 // human-readable filenames and labels use 1-based
808 // numbering, but the "Name", "Name1", "Name2", etc. naming
809 // convention (the psuNames vector) uses 0-based numbering.
810 if (nameIndex > 0)
811 {
812 --nameIndex;
813 }
814 }
815 else
816 {
817 nameIndex = 0;
818 }
819
820 if (psuNames.size() <= nameIndex)
821 {
822 std::cerr << "Could not pair " << labelHead
823 << " with a Name field\n";
824 continue;
825 }
826
827 psuNameFromIndex = psuNames[nameIndex];
828
Ed Tanous8a57ec02020-10-09 12:46:52 -0700829 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700830 {
831 std::cerr << "Sensor label head " << labelHead
832 << " paired with " << psuNameFromIndex
833 << " at index " << nameIndex << "\n";
834 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700835 }
836
Cheng C Yang58b2b532019-05-31 00:19:45 +0800837 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
838
Josh Lehan74d9bd92019-10-31 08:51:58 -0700839 // Similarly, if sensor scaling factor is being customized,
840 // then the below power-of-10 constraint becomes unnecessary,
841 // as config should be able to specify an arbitrary divisor.
842 unsigned int factor = psuProperty->sensorScaleFactor;
843 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700844 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700845 // Preserve existing usage of hardcoded labelMatch table below
846 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700847
Josh Lehan74d9bd92019-10-31 08:51:58 -0700848 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700849 char firstChar =
850 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700851 std::string strScaleFactor =
852 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
853
854 // Preserve existing configs by accepting earlier syntax,
855 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700856 auto findScaleFactor = baseConfig->find(strScaleFactor);
857 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700858 {
859 factor = std::visit(VariantToIntVisitor(),
860 findScaleFactor->second);
861 }
862
Ed Tanous8a57ec02020-10-09 12:46:52 -0700863 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700864 {
865 std::cerr << "Sensor scaling factor " << factor
866 << " string " << strScaleFactor << "\n";
867 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700868 }
869
Vijay Khemka996bad12019-05-28 15:15:16 -0700870 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530871 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
872 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800873 {
James Feist17ab6e02019-06-25 12:28:13 -0700874 std::cerr << "error populating thresholds for "
875 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800876 }
877
Zev Weiss6b6891c2021-04-22 02:46:21 -0500878 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
879 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800880 {
Jason Ling5747fab2019-10-02 16:46:23 -0700881 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700882 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800883 continue;
884 }
885
Ed Tanous8a57ec02020-10-09 12:46:52 -0700886 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700887 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700888 std::cerr << "Sensor properties: Name \""
889 << psuProperty->labelTypeName << "\" Scale "
890 << psuProperty->sensorScaleFactor << " Min "
891 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800892 << psuProperty->maxReading << " Offset "
893 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700894 }
895
896 std::string sensorName = psuProperty->labelTypeName;
897 if (customizedName)
898 {
899 if (sensorName.empty())
900 {
901 // Allow selective disabling of an individual sensor,
902 // by customizing its name to an empty string.
903 std::cerr << "Sensor disabled, empty string\n";
904 continue;
905 }
906 }
907 else
908 {
909 // Sensor name not customized, do prefix/suffix composition,
910 // preserving default behavior by using psuNameFromIndex.
911 sensorName =
912 psuNameFromIndex + " " + psuProperty->labelTypeName;
913 }
914
Ed Tanous8a57ec02020-10-09 12:46:52 -0700915 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700916 {
917 std::cerr << "Sensor name \"" << sensorName << "\" path \""
918 << sensorPathStr << "\" type \"" << sensorType
919 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700920 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800921 // destruct existing one first if already created
922 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800923 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800924 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800925 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000926 readState, findSensorUnit->second, factor,
927 psuProperty->maxReading, psuProperty->minReading,
928 psuProperty->sensorOffset, labelHead, thresholdConfSize,
929 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800930 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700931 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700932 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700933 {
934 std::cerr << "Created " << numCreated << " sensors so far\n";
935 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800936 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800937
938 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800939 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800940 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000941 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
942 *psuName, readState,
943 eventPathList, groupEventPathList,
944 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800945 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700946
Ed Tanous8a57ec02020-10-09 12:46:52 -0700947 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700948 {
949 std::cerr << "Created total of " << numCreated << " sensors\n";
950 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800951}
Cheng C Yang209ec562019-03-12 16:37:44 +0800952
Zhikui Ren23c96e72020-11-05 22:32:28 -0800953void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800954 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800955 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
956 const std::shared_ptr<boost::container::flat_set<std::string>>&
957 sensorsChanged)
958{
959 auto getter = std::make_shared<GetSensorConfiguration>(
960 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
961 const ManagedObjectType& sensorConfigs) {
962 createSensorsCallback(io, objectServer, dbusConnection,
963 sensorConfigs, sensorsChanged);
964 });
965 getter->getConfiguration(
966 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
967}
968
Cheng C Yang916360b2019-05-07 18:47:16 +0800969void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800970{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500971 sensorTable = {{"power", sensor_paths::unitWatts},
972 {"curr", sensor_paths::unitAmperes},
973 {"temp", sensor_paths::unitDegreesC},
974 {"in", sensor_paths::unitVolts},
975 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800976
Jeff Line41d52f2021-04-07 19:38:51 +0800977 labelMatch = {
978 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800979 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
980 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800981 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
982 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
983 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
984 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800985 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
986 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
987 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800988 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
989 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
990 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
991 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
993 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
994 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
995 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
997 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
998 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
999 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1000 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1001 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1002 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1003 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1004 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1005 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1006 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1007 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1008 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1009 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1010 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1011 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1012 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1013 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1014 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1015 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1016 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1017 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1018 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1019 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1020 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1021 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1022 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1023 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001024 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001025 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001026 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1027 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1028 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1029 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1030 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1031 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001032 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001033 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1034 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001035 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1036 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1037 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1038 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1039 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1040 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1041 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1042 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1043 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1044 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1045 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1046 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1047 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1048 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1049 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001050 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1051 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1052 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001053 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1054 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1055 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1056 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1057 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1058 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1059 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1060 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1061 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001062 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1063 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1064 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001065
Matt Simmering6747eba2023-01-18 16:03:55 -08001066 pwmTable = {{"fan1", "Fan_1"},
1067 {"fan2", "Fan_2"},
1068 {"fan3", "Fan_3"},
1069 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001070
1071 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1072 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1073
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001074 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1075 {"Failure", {"in2_alarm"}},
1076 {"ACLost", {"in1_beep"}},
1077 {"ConfigureError", {"in1_fault"}}};
1078
1079 groupEventMatch = {{"FanFault",
1080 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001081 {"fan2", {"fan2_alarm", "fan2_fault"}},
1082 {"fan3", {"fan3_alarm", "fan3_fault"}},
1083 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001084}
1085
James Feistb6c0b912019-07-09 12:21:44 -07001086int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001087{
Ed Tanous1f978632023-02-28 18:16:39 -08001088 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001089 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1090
Ed Tanous14ed5e92022-07-12 15:50:23 -07001091 sdbusplus::asio::object_server objectServer(systemBus, true);
1092 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001093 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001094 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001095 auto sensorsChanged =
1096 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001097
Cheng C Yang916360b2019-05-07 18:47:16 +08001098 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001099
Ed Tanous83db50c2023-03-01 10:20:24 -08001100 boost::asio::post(io, [&]() { createSensors(io, objectServer, systemBus, nullptr); });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001101 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001102 std::function<void(sdbusplus::message_t&)> eventHandler =
1103 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001104 if (message.is_method_error())
1105 {
1106 std::cerr << "callback method error\n";
1107 return;
1108 }
1109 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001110 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001111 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1112 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001113 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001114 return;
1115 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001116 if (ec)
1117 {
1118 std::cerr << "timer error\n";
1119 }
1120 createSensors(io, objectServer, systemBus, sensorsChanged);
1121 });
1122 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001123
Zev Weiss214d9712022-08-12 12:54:31 -07001124 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1125 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +08001126 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001127 io.run();
1128}