blob: ab4f6f3da99d1455a0c067bfe586cc651db0258e [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
Ed Tanous8a57ec02020-10-09 12:46:52 -070017#include <PSUEvent.hpp>
18#include <PSUSensor.hpp>
19#include <Utils.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080020#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070021#include <boost/container/flat_map.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080022#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25#include <sdbusplus/bus/match.hpp>
26
27#include <array>
Josh Lehan74d9bd92019-10-31 08:51:58 -070028#include <cmath>
James Feist24f02f22019-04-15 11:05:39 -070029#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080030#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <functional>
Cheng C Yang58b2b532019-05-31 00:19:45 +080032#include <iostream>
Cheng C Yang209ec562019-03-12 16:37:44 +080033#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <string>
Lei YUa2c7cea2020-12-23 14:07:28 +080035#include <string_view>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <utility>
37#include <variant>
38#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080039
Ed Tanous8a57ec02020-10-09 12:46:52 -070040static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070041
Brandon Kim66558232021-11-09 16:53:08 -080042static constexpr auto sensorTypes{std::to_array<const char*>(
Zev Weiss054aad82022-08-18 01:37:34 -070043 {"ADM1266", "ADM1272", "ADM1275", "ADM1278", "ADM1293",
44 "ADS7830", "BMR490", "DPS800", "INA219", "INA230",
45 "IPSPS", "ISL68137", "ISL68220", "ISL68223", "ISL69225",
46 "ISL69243", "ISL69260", "LM25066", "MAX16601", "MAX20710",
47 "MAX20730", "MAX20734", "MAX20796", "MAX34451", "MP2971",
48 "MP2973", "MP5023", "pmbus", "PXE1610", "RAA228000",
49 "RAA228228", "RAA228620", "RAA229001", "RAA229004", "RAA229126",
50 "TPS546D24", "XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080051
Ed Tanousa2df7862021-12-07 16:30:27 -080052// clang-format off
53static constexpr auto pmbusNames{std::to_array<const char*>({
54 "adm1266",
55 "adm1272",
56 "adm1275",
57 "adm1278",
Zhikui Ren842e5432022-03-09 16:18:24 -080058 "adm1293",
Tim Chao6f379ce2022-02-24 11:08:09 +080059 "ads7830",
linchuyuan5cf66df2021-12-21 17:38:55 -080060 "bmr490",
Ed Tanousa2df7862021-12-07 16:30:27 -080061 "dps800",
62 "ina219",
63 "ina230",
64 "ipsps1",
65 "isl68137",
66 "isl68220",
67 "isl68223",
Khang Kieu51ad6672022-02-09 01:26:25 +000068 "isl69225",
Ed Tanousa2df7862021-12-07 16:30:27 -080069 "isl69243",
70 "isl69260",
71 "lm25066",
72 "max16601",
73 "max20710",
74 "max20730",
75 "max20734",
76 "max20796",
77 "max34451",
Shamim Alidccd1d42022-06-08 22:31:35 +053078 "mp2971",
79 "mp2973",
Howard Chiub58ac3a2021-12-07 17:12:56 +080080 "mp5023",
Ed Tanousa2df7862021-12-07 16:30:27 -080081 "pmbus",
82 "pxe1610",
83 "raa228000",
84 "raa228228",
Tom Tung75da5152021-11-24 09:38:07 +080085 "raa228620",
86 "raa229001",
Ed Tanousa2df7862021-12-07 16:30:27 -080087 "raa229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -080088 "raa229126",
Ed Tanousa2df7862021-12-07 16:30:27 -080089 "tps546d24",
90 "xdpe12284"
91})};
92//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -070093
Cheng C Yang209ec562019-03-12 16:37:44 +080094namespace fs = std::filesystem;
95
Yong Libf8b1da2020-04-15 16:32:50 +080096static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +080097 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +080098static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
99 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800100static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
101 pwmSensors;
102static boost::container::flat_map<std::string, std::string> sensorTable;
103static boost::container::flat_map<std::string, PSUProperty> labelMatch;
104static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800105static boost::container::flat_map<std::string, std::vector<std::string>>
106 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800107static boost::container::flat_map<
108 std::string,
109 boost::container::flat_map<std::string, std::vector<std::string>>>
110 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800111static boost::container::flat_map<std::string, std::vector<std::string>>
112 limitEventMatch;
113
Josh Lehan74d9bd92019-10-31 08:51:58 -0700114static std::vector<PSUProperty> psuProperties;
115
Cheng C Yang58b2b532019-05-31 00:19:45 +0800116// Function CheckEvent will check each attribute from eventMatch table in the
117// sysfs. If the attributes exists in sysfs, then store the complete path
118// of the attribute into eventPathList.
119void checkEvent(
120 const std::string& directory,
121 const boost::container::flat_map<std::string, std::vector<std::string>>&
122 eventMatch,
123 boost::container::flat_map<std::string, std::vector<std::string>>&
124 eventPathList)
125{
126 for (const auto& match : eventMatch)
127 {
128 const std::vector<std::string>& eventAttrs = match.second;
129 const std::string& eventName = match.first;
130 for (const auto& eventAttr : eventAttrs)
131 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700132 std::string eventPath = directory;
133 eventPath += "/";
134 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800135
136 std::ifstream eventFile(eventPath);
137 if (!eventFile.good())
138 {
139 continue;
140 }
141
142 eventPathList[eventName].push_back(eventPath);
143 }
144 }
145}
146
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800147// Check Group Events which contains more than one targets in each combine
148// events.
149void checkGroupEvent(
150 const std::string& directory,
151 const boost::container::flat_map<
152 std::string,
153 boost::container::flat_map<std::string, std::vector<std::string>>>&
154 groupEventMatch,
155 boost::container::flat_map<
156 std::string,
157 boost::container::flat_map<std::string, std::vector<std::string>>>&
158 groupEventPathList)
159{
160 for (const auto& match : groupEventMatch)
161 {
162 const std::string& groupEventName = match.first;
163 const boost::container::flat_map<std::string, std::vector<std::string>>
164 events = match.second;
165 boost::container::flat_map<std::string, std::vector<std::string>>
166 pathList;
167 for (const auto& match : events)
168 {
169 const std::string& eventName = match.first;
170 const std::vector<std::string>& eventAttrs = match.second;
171 for (const auto& eventAttr : eventAttrs)
172 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700173 std::string eventPath = directory;
174 eventPath += "/";
175 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800176 std::ifstream eventFile(eventPath);
177 if (!eventFile.good())
178 {
179 continue;
180 }
181
182 pathList[eventName].push_back(eventPath);
183 }
184 }
185 groupEventPathList[groupEventName] = pathList;
186 }
187}
188
Cheng C Yang58b2b532019-05-31 00:19:45 +0800189// Function checkEventLimits will check all the psu related xxx_input attributes
190// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
191// xxx_min_alarm exist, then store the existing paths of the alarm attributes
192// to eventPathList.
193void checkEventLimits(
194 const std::string& sensorPathStr,
195 const boost::container::flat_map<std::string, std::vector<std::string>>&
196 limitEventMatch,
197 boost::container::flat_map<std::string, std::vector<std::string>>&
198 eventPathList)
199{
Lei YUa2c7cea2020-12-23 14:07:28 +0800200 auto attributePartPos = sensorPathStr.find_last_of('_');
201 if (attributePartPos == std::string::npos)
202 {
203 // There is no '_' in the string, skip it
204 return;
205 }
206 auto attributePart =
207 std::string_view(sensorPathStr).substr(attributePartPos + 1);
208 if (attributePart != "input")
209 {
210 // If the sensor is not xxx_input, skip it
211 return;
212 }
213
214 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800215 for (const auto& limitMatch : limitEventMatch)
216 {
217 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
218 const std::string& eventName = limitMatch.first;
219 for (const auto& limitEventAttr : limitEventAttrs)
220 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800221 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800222 std::ifstream eventFile(limitEventPath);
223 if (!eventFile.good())
224 {
225 continue;
226 }
227 eventPathList[eventName].push_back(limitEventPath);
228 }
229 }
230}
Cheng C Yang916360b2019-05-07 18:47:16 +0800231
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530232static void
233 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
234 const std::string& interfacePath,
235 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
236 sdbusplus::asio::object_server& objectServer,
237 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800238{
Zev Weiss741f26e2022-08-12 18:21:02 -0700239 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800240 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700241 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800242 {
243 continue;
244 }
245
246 const std::string& sensorPathStr = sensorPath.string();
247 const std::string& pwmPathStr =
248 boost::replace_all_copy(sensorPathStr, "input", "target");
249 std::ifstream pwmFile(pwmPathStr);
250 if (!pwmFile.good())
251 {
252 continue;
253 }
254
255 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
256 if (findPWMSensor != pwmSensors.end())
257 {
258 continue;
259 }
260
Zev Weissd8c293a2022-08-15 18:58:41 -0700261 std::string name = "Pwm_";
262 name += psuName;
263 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700264 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700265
266 std::string objPath = interfacePath;
267 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700268 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700269
Cheng C Yang916360b2019-05-07 18:47:16 +0800270 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700271 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800272 }
273}
274
Zhikui Ren23c96e72020-11-05 22:32:28 -0800275static void createSensorsCallback(
276 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
277 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
278 const ManagedObjectType& sensorConfigs,
279 const std::shared_ptr<boost::container::flat_set<std::string>>&
280 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800281{
Josh Lehan49cfba92019-10-08 16:50:42 -0700282 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800283 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800284
285 std::vector<fs::path> pmbusPaths;
286 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
287 {
288 std::cerr << "No PSU sensors in system\n";
289 return;
290 }
291
292 boost::container::flat_set<std::string> directories;
293 for (const auto& pmbusPath : pmbusPaths)
294 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800295 boost::container::flat_map<std::string, std::vector<std::string>>
296 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800297 boost::container::flat_map<
298 std::string,
299 boost::container::flat_map<std::string, std::vector<std::string>>>
300 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800301
302 std::ifstream nameFile(pmbusPath);
303 if (!nameFile.good())
304 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700305 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800306 continue;
307 }
308
309 std::string pmbusName;
310 std::getline(nameFile, pmbusName);
311 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700312
313 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
314 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800315 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700316 // To avoid this error message, add your driver name to
317 // the pmbusNames vector at the top of this file.
318 std::cerr << "Driver name " << pmbusName
319 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800320 continue;
321 }
322
Cheng C Yang209ec562019-03-12 16:37:44 +0800323 auto directory = pmbusPath.parent_path();
324
325 auto ret = directories.insert(directory.string());
326 if (!ret.second)
327 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700328 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800329 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800330 }
331
James Feistb6c0b912019-07-09 12:21:44 -0700332 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800333 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700334 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800335 if (findHyphen == std::string::npos)
336 {
337 std::cerr << "found bad device" << deviceName << "\n";
338 continue;
339 }
340 std::string busStr = deviceName.substr(0, findHyphen);
341 std::string addrStr = deviceName.substr(findHyphen + 1);
342
343 size_t bus = 0;
344 size_t addr = 0;
345
346 try
347 {
348 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700349 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800350 }
Patrick Williams26601e82021-10-06 12:43:25 -0500351 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800352 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700353 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
354 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800355 continue;
356 }
357
Zev Weisse8b97ee2022-08-12 15:23:51 -0700358 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800359 const SensorData* sensorData = nullptr;
360 const std::string* interfacePath = nullptr;
361 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800362 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800363
Zev Weiss741f26e2022-08-12 18:21:02 -0700364 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800365 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700366 sensorData = &cfgData;
Cheng C Yang209ec562019-03-12 16:37:44 +0800367 for (const char* type : sensorTypes)
368 {
Zev Weiss054aad82022-08-18 01:37:34 -0700369 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800370 if (sensorBase != sensorData->end())
371 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700372 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800373 sensorType = type;
374 break;
375 }
376 }
377 if (baseConfig == nullptr)
378 {
379 std::cerr << "error finding base configuration for "
380 << deviceName << "\n";
381 continue;
382 }
383
Zev Weisse8b97ee2022-08-12 15:23:51 -0700384 auto configBus = baseConfig->find("Bus");
385 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800386
Zev Weisse8b97ee2022-08-12 15:23:51 -0700387 if (configBus == baseConfig->end() ||
388 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800389 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800390 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800391 continue;
392 }
393
Ed Tanousa771f6a2022-01-14 09:36:51 -0800394 const uint64_t* confBus = std::get_if<uint64_t>(&(configBus->second));
395 const uint64_t* confAddr = std::get_if<uint64_t>(&(configAddress->second));
396 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800397 {
398 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700399 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800400 continue;
401 }
402
403 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800404 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700405 std::cerr << "Configuration skipping " << *confBus << "-"
406 << *confAddr << " because not " << bus << "-" << addr
407 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800408 continue;
409 }
410
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800411 std::vector<thresholds::Threshold> confThresholds;
412 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
413 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800414 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800415 }
416 thresholdConfSize = confThresholds.size();
417
Zev Weiss741f26e2022-08-12 18:21:02 -0700418 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800419 break;
420 }
421 if (interfacePath == nullptr)
422 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700423 // To avoid this error message, add your export map entry,
424 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800425 std::cerr << "failed to find match for " << deviceName << "\n";
426 continue;
427 }
428
Zev Weisse8b97ee2022-08-12 15:23:51 -0700429 auto findPSUName = baseConfig->find("Name");
430 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800431 {
432 std::cerr << "could not determine configuration name for "
433 << deviceName << "\n";
434 continue;
435 }
Ed Tanousa771f6a2022-01-14 09:36:51 -0800436 const std::string* psuName = std::get_if<std::string>(&(findPSUName->second));
437 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800438 {
439 std::cerr << "Cannot find psu name, invalid configuration\n";
440 continue;
441 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800442
443 // on rescans, only update sensors we were signaled by
444 if (!firstScan)
445 {
Zhikui Renda98f092021-11-01 09:41:08 -0700446 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800447 auto it =
448 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
449 [psuNameStr](std::string& s) {
Zev Weiss6c106d62022-08-17 20:50:00 -0700450 return s.ends_with(psuNameStr);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800451 });
452
453 if (it == sensorsChanged->end())
454 {
455 continue;
456 }
457 sensorsChanged->erase(it);
458 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800459 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800460 checkGroupEvent(directory.string(), groupEventMatch,
461 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800462
Zev Weisse8b97ee2022-08-12 15:23:51 -0700463 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000464
Vijay Khemka996bad12019-05-28 15:15:16 -0700465 /* Check if there are more sensors in the same interface */
466 int i = 1;
467 std::vector<std::string> psuNames;
468 do
469 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700470 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700471 psuNames.push_back(
472 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700473 findPSUName = baseConfig->find("Name" + std::to_string(i++));
474 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700475
Cheng C Yange50345b2019-04-02 17:26:15 +0800476 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700477 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800478 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800479 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800480 continue;
481 }
482
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530483 /* read max value in sysfs for in, curr, power, temp, ... */
484 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
485 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700486 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530487 {
488 std::cerr << "No max name in PSU \n";
489 }
490 }
491
Zev Weiss8569bf22022-10-11 15:37:44 -0700492 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800493
Vijay Khemka996bad12019-05-28 15:15:16 -0700494 /* Find array of labels to be exposed if it is defined in config */
495 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700496 auto findLabelObj = baseConfig->find("Labels");
497 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700498 {
499 findLabels =
500 std::get<std::vector<std::string>>(findLabelObj->second);
501 }
502
Jason Ling5747fab2019-10-02 16:46:23 -0700503 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
504 std::smatch matches;
505
Cheng C Yange50345b2019-04-02 17:26:15 +0800506 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800507 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530508 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800509 std::string labelHead;
510 std::string sensorPathStr = sensorPath.string();
511 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700512 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700513 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
514 {
Josh Lehan06494452019-10-31 09:49:16 -0700515 // hwmon *_input filename without number:
516 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700517 sensorNameSubStr = matches[1];
518 }
519 else
520 {
Josh Lehan06494452019-10-31 09:49:16 -0700521 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700522 << sensorNameStr;
523 continue;
524 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800525
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530526 std::string labelPath;
527
528 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700529 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530530 if (pos != std::string::npos)
531 {
532
533 std::string sensorPathStrMax = sensorPathStr.substr(pos);
Ed Tanous2049bd22022-07-09 07:20:26 -0700534 if (sensorPathStrMax == "_max")
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530535 {
536 labelPath =
537 boost::replace_all_copy(sensorPathStr, "max", "label");
538 maxLabel = true;
539 }
540 else
541 {
542 labelPath = boost::replace_all_copy(sensorPathStr, "input",
543 "label");
544 maxLabel = false;
545 }
546 }
547 else
548 {
549 continue;
550 }
551
Cheng C Yangecba9de2019-09-12 23:41:50 +0800552 std::ifstream labelFile(labelPath);
553 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800554 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700555 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800556 {
557 std::cerr << "Input file " << sensorPath
558 << " has no corresponding label file\n";
559 }
Josh Lehan06494452019-10-31 09:49:16 -0700560 // hwmon *_input filename with number:
561 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700562 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800563 }
564 else
565 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800566 std::string label;
567 std::getline(labelFile, label);
568 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800569 auto findSensor = sensors.find(label);
570 if (findSensor != sensors.end())
571 {
572 continue;
573 }
574
Josh Lehan06494452019-10-31 09:49:16 -0700575 // hwmon corresponding *_label file contents:
576 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700577 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800578 }
579
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530580 /* append "max" for labelMatch */
581 if (maxLabel)
582 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700583 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530584 }
585
Ed Tanous8a57ec02020-10-09 12:46:52 -0700586 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700587 {
588 std::cerr << "Sensor type=\"" << sensorNameSubStr
589 << "\" label=\"" << labelHead << "\"\n";
590 }
591
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530592 checkPWMSensor(sensorPath, labelHead, *interfacePath,
593 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800594
Vijay Khemka996bad12019-05-28 15:15:16 -0700595 if (!findLabels.empty())
596 {
597 /* Check if this labelHead is enabled in config file */
598 if (std::find(findLabels.begin(), findLabels.end(),
599 labelHead) == findLabels.end())
600 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700601 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800602 {
603 std::cerr << "could not find " << labelHead
604 << " in the Labels list\n";
605 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700606 continue;
607 }
608 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800609
Cheng C Yange50345b2019-04-02 17:26:15 +0800610 auto findProperty = labelMatch.find(labelHead);
611 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800612 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700613 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700614 {
615 std::cerr << "Could not find matching default property for "
616 << labelHead << "\n";
617 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800618 continue;
619 }
620
Josh Lehan74d9bd92019-10-31 08:51:58 -0700621 // Protect the hardcoded labelMatch list from changes,
622 // by making a copy and modifying that instead.
623 // Avoid bleedthrough of one device's customizations to
624 // the next device, as each should be independently customizable.
625 psuProperties.push_back(findProperty->second);
626 auto psuProperty = psuProperties.rbegin();
627
628 // Use label head as prefix for reading from config file,
629 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
630 std::string keyName = labelHead + "_Name";
631 std::string keyScale = labelHead + "_Scale";
632 std::string keyMin = labelHead + "_Min";
633 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800634 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800635 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700636
637 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700638 auto findCustomName = baseConfig->find(keyName);
639 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700640 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700641 try
642 {
643 psuProperty->labelTypeName = std::visit(
644 VariantToStringVisitor(), findCustomName->second);
645 }
Patrick Williams26601e82021-10-06 12:43:25 -0500646 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700647 {
648 std::cerr << "Unable to parse " << keyName << "\n";
649 continue;
650 }
651
652 // All strings are valid, including empty string
653 customizedName = true;
654 }
655
656 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700657 auto findCustomScale = baseConfig->find(keyScale);
658 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700659 {
660 try
661 {
662 psuProperty->sensorScaleFactor = std::visit(
663 VariantToUnsignedIntVisitor(), findCustomScale->second);
664 }
Patrick Williams26601e82021-10-06 12:43:25 -0500665 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700666 {
667 std::cerr << "Unable to parse " << keyScale << "\n";
668 continue;
669 }
670
671 // Avoid later division by zero
672 if (psuProperty->sensorScaleFactor > 0)
673 {
674 customizedScale = true;
675 }
676 else
677 {
678 std::cerr << "Unable to accept " << keyScale << "\n";
679 continue;
680 }
681 }
682
Zev Weisse8b97ee2022-08-12 15:23:51 -0700683 auto findCustomMin = baseConfig->find(keyMin);
684 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700685 {
686 try
687 {
688 psuProperty->minReading = std::visit(
689 VariantToDoubleVisitor(), findCustomMin->second);
690 }
Patrick Williams26601e82021-10-06 12:43:25 -0500691 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700692 {
693 std::cerr << "Unable to parse " << keyMin << "\n";
694 continue;
695 }
696 }
697
Zev Weisse8b97ee2022-08-12 15:23:51 -0700698 auto findCustomMax = baseConfig->find(keyMax);
699 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700700 {
701 try
702 {
703 psuProperty->maxReading = std::visit(
704 VariantToDoubleVisitor(), findCustomMax->second);
705 }
Patrick Williams26601e82021-10-06 12:43:25 -0500706 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700707 {
708 std::cerr << "Unable to parse " << keyMax << "\n";
709 continue;
710 }
711 }
712
Zev Weisse8b97ee2022-08-12 15:23:51 -0700713 auto findCustomOffset = baseConfig->find(keyOffset);
714 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800715 {
716 try
717 {
718 psuProperty->sensorOffset = std::visit(
719 VariantToDoubleVisitor(), findCustomOffset->second);
720 }
Patrick Williams26601e82021-10-06 12:43:25 -0500721 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800722 {
723 std::cerr << "Unable to parse " << keyOffset << "\n";
724 continue;
725 }
726 }
727
Lotus Xucb5af732021-09-10 15:18:50 +0800728 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700729 auto findPowerState = baseConfig->find(keyPowerState);
730 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800731 {
732 std::string powerState = std::visit(VariantToStringVisitor(),
733 findPowerState->second);
734 setReadState(powerState, readState);
735 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700736 if (!(psuProperty->minReading < psuProperty->maxReading))
737 {
738 std::cerr << "Min must be less than Max\n";
739 continue;
740 }
741
742 // If the sensor name is being customized by config file,
743 // then prefix/suffix composition becomes not necessary,
744 // and in fact not wanted, because it gets in the way.
745 std::string psuNameFromIndex;
746 if (!customizedName)
747 {
748 /* Find out sensor name index for this label */
749 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500750 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700751 if (std::regex_search(labelHead, matches, rgx))
752 {
753 nameIndex = std::stoi(matches[1]);
754
755 // Decrement to preserve alignment, because hwmon
756 // human-readable filenames and labels use 1-based
757 // numbering, but the "Name", "Name1", "Name2", etc. naming
758 // convention (the psuNames vector) uses 0-based numbering.
759 if (nameIndex > 0)
760 {
761 --nameIndex;
762 }
763 }
764 else
765 {
766 nameIndex = 0;
767 }
768
769 if (psuNames.size() <= nameIndex)
770 {
771 std::cerr << "Could not pair " << labelHead
772 << " with a Name field\n";
773 continue;
774 }
775
776 psuNameFromIndex = psuNames[nameIndex];
777
Ed Tanous8a57ec02020-10-09 12:46:52 -0700778 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700779 {
780 std::cerr << "Sensor label head " << labelHead
781 << " paired with " << psuNameFromIndex
782 << " at index " << nameIndex << "\n";
783 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700784 }
785
Cheng C Yang58b2b532019-05-31 00:19:45 +0800786 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
787
Josh Lehan74d9bd92019-10-31 08:51:58 -0700788 // Similarly, if sensor scaling factor is being customized,
789 // then the below power-of-10 constraint becomes unnecessary,
790 // as config should be able to specify an arbitrary divisor.
791 unsigned int factor = psuProperty->sensorScaleFactor;
792 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700793 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700794 // Preserve existing usage of hardcoded labelMatch table below
795 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700796
Josh Lehan74d9bd92019-10-31 08:51:58 -0700797 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700798 char firstChar =
799 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700800 std::string strScaleFactor =
801 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
802
803 // Preserve existing configs by accepting earlier syntax,
804 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700805 auto findScaleFactor = baseConfig->find(strScaleFactor);
806 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700807 {
808 factor = std::visit(VariantToIntVisitor(),
809 findScaleFactor->second);
810 }
811
Ed Tanous8a57ec02020-10-09 12:46:52 -0700812 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700813 {
814 std::cerr << "Sensor scaling factor " << factor
815 << " string " << strScaleFactor << "\n";
816 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700817 }
818
Vijay Khemka996bad12019-05-28 15:15:16 -0700819 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530820 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
821 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800822 {
James Feist17ab6e02019-06-25 12:28:13 -0700823 std::cerr << "error populating thresholds for "
824 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800825 }
826
Zev Weiss6b6891c2021-04-22 02:46:21 -0500827 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
828 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800829 {
Jason Ling5747fab2019-10-02 16:46:23 -0700830 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700831 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800832 continue;
833 }
834
Ed Tanous8a57ec02020-10-09 12:46:52 -0700835 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700836 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700837 std::cerr << "Sensor properties: Name \""
838 << psuProperty->labelTypeName << "\" Scale "
839 << psuProperty->sensorScaleFactor << " Min "
840 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800841 << psuProperty->maxReading << " Offset "
842 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700843 }
844
845 std::string sensorName = psuProperty->labelTypeName;
846 if (customizedName)
847 {
848 if (sensorName.empty())
849 {
850 // Allow selective disabling of an individual sensor,
851 // by customizing its name to an empty string.
852 std::cerr << "Sensor disabled, empty string\n";
853 continue;
854 }
855 }
856 else
857 {
858 // Sensor name not customized, do prefix/suffix composition,
859 // preserving default behavior by using psuNameFromIndex.
860 sensorName =
861 psuNameFromIndex + " " + psuProperty->labelTypeName;
862 }
863
Ed Tanous8a57ec02020-10-09 12:46:52 -0700864 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700865 {
866 std::cerr << "Sensor name \"" << sensorName << "\" path \""
867 << sensorPathStr << "\" type \"" << sensorType
868 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700869 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800870 // destruct existing one first if already created
871 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800872 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800873 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800874 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000875 readState, findSensorUnit->second, factor,
876 psuProperty->maxReading, psuProperty->minReading,
877 psuProperty->sensorOffset, labelHead, thresholdConfSize,
878 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800879 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700880 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700881 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700882 {
883 std::cerr << "Created " << numCreated << " sensors so far\n";
884 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800885 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800886
887 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800888 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800889 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000890 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
891 *psuName, readState,
892 eventPathList, groupEventPathList,
893 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800894 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700895
Ed Tanous8a57ec02020-10-09 12:46:52 -0700896 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700897 {
898 std::cerr << "Created total of " << numCreated << " sensors\n";
899 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700900 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800901
Zhikui Ren23c96e72020-11-05 22:32:28 -0800902void createSensors(
903 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
904 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
905 const std::shared_ptr<boost::container::flat_set<std::string>>&
906 sensorsChanged)
907{
908 auto getter = std::make_shared<GetSensorConfiguration>(
909 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
910 const ManagedObjectType& sensorConfigs) {
911 createSensorsCallback(io, objectServer, dbusConnection,
912 sensorConfigs, sensorsChanged);
913 });
914 getter->getConfiguration(
915 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
916}
917
Cheng C Yang916360b2019-05-07 18:47:16 +0800918void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800919{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500920 sensorTable = {{"power", sensor_paths::unitWatts},
921 {"curr", sensor_paths::unitAmperes},
922 {"temp", sensor_paths::unitDegreesC},
923 {"in", sensor_paths::unitVolts},
924 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800925
Jeff Line41d52f2021-04-07 19:38:51 +0800926 labelMatch = {
927 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800928 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
929 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800930 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
931 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
932 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
933 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800934 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
935 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
936 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800937 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
938 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
939 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
940 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
941 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
942 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
943 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
944 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
945 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
946 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
947 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
948 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
949 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
950 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
951 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
952 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
953 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
954 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
955 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
956 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
957 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
958 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
959 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
960 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
961 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
962 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
963 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
964 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
965 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
966 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
967 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
968 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
969 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
970 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
971 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
972 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +0800973 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800974 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +0800975 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
976 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
977 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
978 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
979 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
980 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800981 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800982 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
983 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800984 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
985 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
986 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
987 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
988 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
989 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
990 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
991 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
992 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
993 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
994 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
995 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
996 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
997 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
998 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800999 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1000 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1001 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001002 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1003 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1004 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1005 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1006 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1007 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1008 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1009 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1010 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
1011 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001012
1013 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001014
1015 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1016 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1017
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001018 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1019 {"Failure", {"in2_alarm"}},
1020 {"ACLost", {"in1_beep"}},
1021 {"ConfigureError", {"in1_fault"}}};
1022
1023 groupEventMatch = {{"FanFault",
1024 {{"fan1", {"fan1_alarm", "fan1_fault"}},
1025 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001026}
1027
James Feistb6c0b912019-07-09 12:21:44 -07001028int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001029{
1030 boost::asio::io_service io;
1031 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1032
1033 systemBus->request_name("xyz.openbmc_project.PSUSensor");
1034 sdbusplus::asio::object_server objectServer(systemBus);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001035 auto sensorsChanged =
1036 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001037
Cheng C Yang916360b2019-05-07 18:47:16 +08001038 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001039
Zhikui Ren23c96e72020-11-05 22:32:28 -08001040 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001041 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001042 std::function<void(sdbusplus::message_t&)> eventHandler =
1043 [&](sdbusplus::message_t& message) {
Cheng C Yang209ec562019-03-12 16:37:44 +08001044 if (message.is_method_error())
1045 {
1046 std::cerr << "callback method error\n";
1047 return;
1048 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001049 sensorsChanged->insert(message.get_path());
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001050 filterTimer.expires_from_now(std::chrono::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +08001051 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1052 if (ec == boost::asio::error::operation_aborted)
1053 {
1054 return;
1055 }
Ed Tanous8a57ec02020-10-09 12:46:52 -07001056 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +08001057 {
1058 std::cerr << "timer error\n";
1059 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001060 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +08001061 });
1062 };
1063
Zev Weiss214d9712022-08-12 12:54:31 -07001064 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1065 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +08001066 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001067 io.run();
1068}