blob: 7ca5d3d3c6a1f66ce7ccbbc5d6e60ac6cc927c2b [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/predicate.hpp>
21#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
Brandon Kim66558232021-11-09 16:53:08 -080043static constexpr auto sensorTypes{std::to_array<const char*>(
44 {"xyz.openbmc_project.Configuration.ADM1266",
45 "xyz.openbmc_project.Configuration.ADM1272",
46 "xyz.openbmc_project.Configuration.ADM1275",
47 "xyz.openbmc_project.Configuration.ADM1278",
Zhikui Ren842e5432022-03-09 16:18:24 -080048 "xyz.openbmc_project.Configuration.ADM1293",
Tim Chao6f379ce2022-02-24 11:08:09 +080049 "xyz.openbmc_project.Configuration.ADS7830",
linchuyuan5cf66df2021-12-21 17:38:55 -080050 "xyz.openbmc_project.Configuration.BMR490",
Brandon Kim66558232021-11-09 16:53:08 -080051 "xyz.openbmc_project.Configuration.DPS800",
52 "xyz.openbmc_project.Configuration.INA219",
53 "xyz.openbmc_project.Configuration.INA230",
54 "xyz.openbmc_project.Configuration.IPSPS",
55 "xyz.openbmc_project.Configuration.ISL68137",
56 "xyz.openbmc_project.Configuration.ISL68220",
57 "xyz.openbmc_project.Configuration.ISL68223",
Khang Kieu7d6b77d2022-02-10 10:43:36 +000058 "xyz.openbmc_project.Configuration.ISL69225",
Brandon Kim66558232021-11-09 16:53:08 -080059 "xyz.openbmc_project.Configuration.ISL69243",
60 "xyz.openbmc_project.Configuration.ISL69260",
61 "xyz.openbmc_project.Configuration.LM25066",
62 "xyz.openbmc_project.Configuration.MAX16601",
63 "xyz.openbmc_project.Configuration.MAX20710",
64 "xyz.openbmc_project.Configuration.MAX20730",
65 "xyz.openbmc_project.Configuration.MAX20734",
66 "xyz.openbmc_project.Configuration.MAX20796",
67 "xyz.openbmc_project.Configuration.MAX34451",
Howard Chiub58ac3a2021-12-07 17:12:56 +080068 "xyz.openbmc_project.Configuration.MP5023",
Brandon Kim66558232021-11-09 16:53:08 -080069 "xyz.openbmc_project.Configuration.pmbus",
70 "xyz.openbmc_project.Configuration.PXE1610",
71 "xyz.openbmc_project.Configuration.RAA228000",
72 "xyz.openbmc_project.Configuration.RAA228228",
73 "xyz.openbmc_project.Configuration.RAA229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -080074 "xyz.openbmc_project.Configuration.RAA229126",
Brandon Kim66558232021-11-09 16:53:08 -080075 "xyz.openbmc_project.Configuration.TPS546D24",
76 "xyz.openbmc_project.Configuration.XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080077
Ed Tanousa2df7862021-12-07 16:30:27 -080078// clang-format off
79static constexpr auto pmbusNames{std::to_array<const char*>({
80 "adm1266",
81 "adm1272",
82 "adm1275",
83 "adm1278",
Zhikui Ren842e5432022-03-09 16:18:24 -080084 "adm1293",
Tim Chao6f379ce2022-02-24 11:08:09 +080085 "ads7830",
linchuyuan5cf66df2021-12-21 17:38:55 -080086 "bmr490",
Ed Tanousa2df7862021-12-07 16:30:27 -080087 "dps800",
88 "ina219",
89 "ina230",
90 "ipsps1",
91 "isl68137",
92 "isl68220",
93 "isl68223",
Khang Kieu51ad6672022-02-09 01:26:25 +000094 "isl69225",
Ed Tanousa2df7862021-12-07 16:30:27 -080095 "isl69243",
96 "isl69260",
97 "lm25066",
98 "max16601",
99 "max20710",
100 "max20730",
101 "max20734",
102 "max20796",
103 "max34451",
Howard Chiub58ac3a2021-12-07 17:12:56 +0800104 "mp5023",
Ed Tanousa2df7862021-12-07 16:30:27 -0800105 "pmbus",
106 "pxe1610",
107 "raa228000",
108 "raa228228",
109 "raa229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800110 "raa229126",
Ed Tanousa2df7862021-12-07 16:30:27 -0800111 "tps546d24",
112 "xdpe12284"
113})};
114//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700115
Cheng C Yang209ec562019-03-12 16:37:44 +0800116namespace fs = std::filesystem;
117
Yong Libf8b1da2020-04-15 16:32:50 +0800118static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800119 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800120static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
121 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800122static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
123 pwmSensors;
124static boost::container::flat_map<std::string, std::string> sensorTable;
125static boost::container::flat_map<std::string, PSUProperty> labelMatch;
126static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800127static boost::container::flat_map<std::string, std::vector<std::string>>
128 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800129static boost::container::flat_map<
130 std::string,
131 boost::container::flat_map<std::string, std::vector<std::string>>>
132 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800133static boost::container::flat_map<std::string, std::vector<std::string>>
134 limitEventMatch;
135
Josh Lehan74d9bd92019-10-31 08:51:58 -0700136static std::vector<PSUProperty> psuProperties;
137
Cheng C Yang58b2b532019-05-31 00:19:45 +0800138// Function CheckEvent will check each attribute from eventMatch table in the
139// sysfs. If the attributes exists in sysfs, then store the complete path
140// of the attribute into eventPathList.
141void checkEvent(
142 const std::string& directory,
143 const boost::container::flat_map<std::string, std::vector<std::string>>&
144 eventMatch,
145 boost::container::flat_map<std::string, std::vector<std::string>>&
146 eventPathList)
147{
148 for (const auto& match : eventMatch)
149 {
150 const std::vector<std::string>& eventAttrs = match.second;
151 const std::string& eventName = match.first;
152 for (const auto& eventAttr : eventAttrs)
153 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700154 std::string eventPath = directory;
155 eventPath += "/";
156 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800157
158 std::ifstream eventFile(eventPath);
159 if (!eventFile.good())
160 {
161 continue;
162 }
163
164 eventPathList[eventName].push_back(eventPath);
165 }
166 }
167}
168
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800169// Check Group Events which contains more than one targets in each combine
170// events.
171void checkGroupEvent(
172 const std::string& directory,
173 const boost::container::flat_map<
174 std::string,
175 boost::container::flat_map<std::string, std::vector<std::string>>>&
176 groupEventMatch,
177 boost::container::flat_map<
178 std::string,
179 boost::container::flat_map<std::string, std::vector<std::string>>>&
180 groupEventPathList)
181{
182 for (const auto& match : groupEventMatch)
183 {
184 const std::string& groupEventName = match.first;
185 const boost::container::flat_map<std::string, std::vector<std::string>>
186 events = match.second;
187 boost::container::flat_map<std::string, std::vector<std::string>>
188 pathList;
189 for (const auto& match : events)
190 {
191 const std::string& eventName = match.first;
192 const std::vector<std::string>& eventAttrs = match.second;
193 for (const auto& eventAttr : eventAttrs)
194 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700195 std::string eventPath = directory;
196 eventPath += "/";
197 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800198 std::ifstream eventFile(eventPath);
199 if (!eventFile.good())
200 {
201 continue;
202 }
203
204 pathList[eventName].push_back(eventPath);
205 }
206 }
207 groupEventPathList[groupEventName] = pathList;
208 }
209}
210
Cheng C Yang58b2b532019-05-31 00:19:45 +0800211// Function checkEventLimits will check all the psu related xxx_input attributes
212// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
213// xxx_min_alarm exist, then store the existing paths of the alarm attributes
214// to eventPathList.
215void checkEventLimits(
216 const std::string& sensorPathStr,
217 const boost::container::flat_map<std::string, std::vector<std::string>>&
218 limitEventMatch,
219 boost::container::flat_map<std::string, std::vector<std::string>>&
220 eventPathList)
221{
Lei YUa2c7cea2020-12-23 14:07:28 +0800222 auto attributePartPos = sensorPathStr.find_last_of('_');
223 if (attributePartPos == std::string::npos)
224 {
225 // There is no '_' in the string, skip it
226 return;
227 }
228 auto attributePart =
229 std::string_view(sensorPathStr).substr(attributePartPos + 1);
230 if (attributePart != "input")
231 {
232 // If the sensor is not xxx_input, skip it
233 return;
234 }
235
236 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800237 for (const auto& limitMatch : limitEventMatch)
238 {
239 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
240 const std::string& eventName = limitMatch.first;
241 for (const auto& limitEventAttr : limitEventAttrs)
242 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800243 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800244 std::ifstream eventFile(limitEventPath);
245 if (!eventFile.good())
246 {
247 continue;
248 }
249 eventPathList[eventName].push_back(limitEventPath);
250 }
251 }
252}
Cheng C Yang916360b2019-05-07 18:47:16 +0800253
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530254static void
255 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
256 const std::string& interfacePath,
257 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
258 sdbusplus::asio::object_server& objectServer,
259 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800260{
261 for (const auto& pwmName : pwmTable)
262 {
263 if (pwmName.first != labelHead)
264 {
265 continue;
266 }
267
268 const std::string& sensorPathStr = sensorPath.string();
269 const std::string& pwmPathStr =
270 boost::replace_all_copy(sensorPathStr, "input", "target");
271 std::ifstream pwmFile(pwmPathStr);
272 if (!pwmFile.good())
273 {
274 continue;
275 }
276
277 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
278 if (findPWMSensor != pwmSensors.end())
279 {
280 continue;
281 }
282
283 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530284 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
285 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800286 }
287}
288
Zhikui Ren23c96e72020-11-05 22:32:28 -0800289static void createSensorsCallback(
290 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
291 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
292 const ManagedObjectType& sensorConfigs,
293 const std::shared_ptr<boost::container::flat_set<std::string>>&
294 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800295{
Josh Lehan49cfba92019-10-08 16:50:42 -0700296 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800297 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800298
299 std::vector<fs::path> pmbusPaths;
300 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
301 {
302 std::cerr << "No PSU sensors in system\n";
303 return;
304 }
305
306 boost::container::flat_set<std::string> directories;
307 for (const auto& pmbusPath : pmbusPaths)
308 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800309 boost::container::flat_map<std::string, std::vector<std::string>>
310 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800311 boost::container::flat_map<
312 std::string,
313 boost::container::flat_map<std::string, std::vector<std::string>>>
314 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800315
316 std::ifstream nameFile(pmbusPath);
317 if (!nameFile.good())
318 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700319 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800320 continue;
321 }
322
323 std::string pmbusName;
324 std::getline(nameFile, pmbusName);
325 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700326
327 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
328 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800329 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700330 // To avoid this error message, add your driver name to
331 // the pmbusNames vector at the top of this file.
332 std::cerr << "Driver name " << pmbusName
333 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800334 continue;
335 }
336
Cheng C Yang209ec562019-03-12 16:37:44 +0800337 auto directory = pmbusPath.parent_path();
338
339 auto ret = directories.insert(directory.string());
340 if (!ret.second)
341 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700342 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800343 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800344 }
345
James Feistb6c0b912019-07-09 12:21:44 -0700346 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800347 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700348 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800349 if (findHyphen == std::string::npos)
350 {
351 std::cerr << "found bad device" << deviceName << "\n";
352 continue;
353 }
354 std::string busStr = deviceName.substr(0, findHyphen);
355 std::string addrStr = deviceName.substr(findHyphen + 1);
356
357 size_t bus = 0;
358 size_t addr = 0;
359
360 try
361 {
362 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700363 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800364 }
Patrick Williams26601e82021-10-06 12:43:25 -0500365 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800366 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700367 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
368 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800369 continue;
370 }
371
Cheng C Yang209ec562019-03-12 16:37:44 +0800372 const std::pair<std::string, boost::container::flat_map<
373 std::string, BasicVariantType>>*
374 baseConfig = nullptr;
375 const SensorData* sensorData = nullptr;
376 const std::string* interfacePath = nullptr;
377 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800378 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800379
380 for (const std::pair<sdbusplus::message::object_path, SensorData>&
381 sensor : sensorConfigs)
382 {
383 sensorData = &(sensor.second);
384 for (const char* type : sensorTypes)
385 {
386 auto sensorBase = sensorData->find(type);
387 if (sensorBase != sensorData->end())
388 {
389 baseConfig = &(*sensorBase);
390 sensorType = type;
391 break;
392 }
393 }
394 if (baseConfig == nullptr)
395 {
396 std::cerr << "error finding base configuration for "
397 << deviceName << "\n";
398 continue;
399 }
400
401 auto configBus = baseConfig->second.find("Bus");
402 auto configAddress = baseConfig->second.find("Address");
403
404 if (configBus == baseConfig->second.end() ||
405 configAddress == baseConfig->second.end())
406 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800407 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800408 continue;
409 }
410
Ed Tanousa771f6a2022-01-14 09:36:51 -0800411 const uint64_t* confBus = std::get_if<uint64_t>(&(configBus->second));
412 const uint64_t* confAddr = std::get_if<uint64_t>(&(configAddress->second));
413 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800414 {
415 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700416 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800417 continue;
418 }
419
420 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800421 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700422 std::cerr << "Configuration skipping " << *confBus << "-"
423 << *confAddr << " because not " << bus << "-" << addr
424 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800425 continue;
426 }
427
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800428 std::vector<thresholds::Threshold> confThresholds;
429 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
430 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800431 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800432 }
433 thresholdConfSize = confThresholds.size();
434
Cheng C Yang209ec562019-03-12 16:37:44 +0800435 interfacePath = &(sensor.first.str);
436 break;
437 }
438 if (interfacePath == nullptr)
439 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700440 // To avoid this error message, add your export map entry,
441 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800442 std::cerr << "failed to find match for " << deviceName << "\n";
443 continue;
444 }
445
Cheng C Yange50345b2019-04-02 17:26:15 +0800446 auto findPSUName = baseConfig->second.find("Name");
447 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800448 {
449 std::cerr << "could not determine configuration name for "
450 << deviceName << "\n";
451 continue;
452 }
Ed Tanousa771f6a2022-01-14 09:36:51 -0800453 const std::string* psuName = std::get_if<std::string>(&(findPSUName->second));
454 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800455 {
456 std::cerr << "Cannot find psu name, invalid configuration\n";
457 continue;
458 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800459
460 // on rescans, only update sensors we were signaled by
461 if (!firstScan)
462 {
Zhikui Renda98f092021-11-01 09:41:08 -0700463 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800464 auto it =
465 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
466 [psuNameStr](std::string& s) {
467 return boost::ends_with(s, psuNameStr);
468 });
469
470 if (it == sensorsChanged->end())
471 {
472 continue;
473 }
474 sensorsChanged->erase(it);
475 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800476 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800477 checkGroupEvent(directory.string(), groupEventMatch,
478 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800479
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000480 PowerState readState = PowerState::always;
481 auto findPowerOn = baseConfig->second.find("PowerState");
482 if (findPowerOn != baseConfig->second.end())
483 {
484 std::string powerState =
485 std::visit(VariantToStringVisitor(), findPowerOn->second);
486 setReadState(powerState, readState);
487 }
488
Vijay Khemka996bad12019-05-28 15:15:16 -0700489 /* Check if there are more sensors in the same interface */
490 int i = 1;
491 std::vector<std::string> psuNames;
492 do
493 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700494 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700495 psuNames.push_back(
496 escapeName(std::get<std::string>(findPSUName->second)));
Vijay Khemka996bad12019-05-28 15:15:16 -0700497 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
498 } while (findPSUName != baseConfig->second.end());
499
Cheng C Yange50345b2019-04-02 17:26:15 +0800500 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700501 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800502 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800503 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800504 continue;
505 }
506
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530507 /* read max value in sysfs for in, curr, power, temp, ... */
508 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
509 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700510 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530511 {
512 std::cerr << "No max name in PSU \n";
513 }
514 }
515
Lei YU7170a232021-02-04 16:19:27 +0800516 /* The poll rate for the sensors */
517 double pollRate = 0.0;
518 auto pollRateObj = baseConfig->second.find("PollRate");
519
520 if (pollRateObj != baseConfig->second.end())
521 {
522 pollRate =
523 std::visit(VariantToDoubleVisitor(), pollRateObj->second);
524 if (pollRate <= 0.0)
525 {
526 pollRate = PSUSensor::defaultSensorPoll;
527 }
528 }
529
Vijay Khemka996bad12019-05-28 15:15:16 -0700530 /* Find array of labels to be exposed if it is defined in config */
531 std::vector<std::string> findLabels;
532 auto findLabelObj = baseConfig->second.find("Labels");
533 if (findLabelObj != baseConfig->second.end())
534 {
535 findLabels =
536 std::get<std::vector<std::string>>(findLabelObj->second);
537 }
538
Jason Ling5747fab2019-10-02 16:46:23 -0700539 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
540 std::smatch matches;
541
Cheng C Yange50345b2019-04-02 17:26:15 +0800542 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800543 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530544 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800545 std::string labelHead;
546 std::string sensorPathStr = sensorPath.string();
547 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700548 std::string sensorNameSubStr{""};
549 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
550 {
Josh Lehan06494452019-10-31 09:49:16 -0700551 // hwmon *_input filename without number:
552 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700553 sensorNameSubStr = matches[1];
554 }
555 else
556 {
Josh Lehan06494452019-10-31 09:49:16 -0700557 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700558 << sensorNameStr;
559 continue;
560 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800561
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530562 std::string labelPath;
563
564 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700565 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530566 if (pos != std::string::npos)
567 {
568
569 std::string sensorPathStrMax = sensorPathStr.substr(pos);
570 if (sensorPathStrMax.compare("_max") == 0)
571 {
572 labelPath =
573 boost::replace_all_copy(sensorPathStr, "max", "label");
574 maxLabel = true;
575 }
576 else
577 {
578 labelPath = boost::replace_all_copy(sensorPathStr, "input",
579 "label");
580 maxLabel = false;
581 }
582 }
583 else
584 {
585 continue;
586 }
587
Cheng C Yangecba9de2019-09-12 23:41:50 +0800588 std::ifstream labelFile(labelPath);
589 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800590 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700591 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800592 {
593 std::cerr << "Input file " << sensorPath
594 << " has no corresponding label file\n";
595 }
Josh Lehan06494452019-10-31 09:49:16 -0700596 // hwmon *_input filename with number:
597 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700598 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800599 }
600 else
601 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800602 std::string label;
603 std::getline(labelFile, label);
604 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800605 auto findSensor = sensors.find(label);
606 if (findSensor != sensors.end())
607 {
608 continue;
609 }
610
Josh Lehan06494452019-10-31 09:49:16 -0700611 // hwmon corresponding *_label file contents:
612 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700613 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800614 }
615
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530616 /* append "max" for labelMatch */
617 if (maxLabel)
618 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700619 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530620 }
621
Ed Tanous8a57ec02020-10-09 12:46:52 -0700622 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700623 {
624 std::cerr << "Sensor type=\"" << sensorNameSubStr
625 << "\" label=\"" << labelHead << "\"\n";
626 }
627
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530628 checkPWMSensor(sensorPath, labelHead, *interfacePath,
629 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800630
Vijay Khemka996bad12019-05-28 15:15:16 -0700631 if (!findLabels.empty())
632 {
633 /* Check if this labelHead is enabled in config file */
634 if (std::find(findLabels.begin(), findLabels.end(),
635 labelHead) == findLabels.end())
636 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700637 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800638 {
639 std::cerr << "could not find " << labelHead
640 << " in the Labels list\n";
641 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700642 continue;
643 }
644 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800645
Cheng C Yange50345b2019-04-02 17:26:15 +0800646 auto findProperty = labelMatch.find(labelHead);
647 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800648 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700649 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700650 {
651 std::cerr << "Could not find matching default property for "
652 << labelHead << "\n";
653 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800654 continue;
655 }
656
Josh Lehan74d9bd92019-10-31 08:51:58 -0700657 // Protect the hardcoded labelMatch list from changes,
658 // by making a copy and modifying that instead.
659 // Avoid bleedthrough of one device's customizations to
660 // the next device, as each should be independently customizable.
661 psuProperties.push_back(findProperty->second);
662 auto psuProperty = psuProperties.rbegin();
663
664 // Use label head as prefix for reading from config file,
665 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
666 std::string keyName = labelHead + "_Name";
667 std::string keyScale = labelHead + "_Scale";
668 std::string keyMin = labelHead + "_Min";
669 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800670 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800671 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700672
673 bool customizedName = false;
674 auto findCustomName = baseConfig->second.find(keyName);
675 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700676 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700677 try
678 {
679 psuProperty->labelTypeName = std::visit(
680 VariantToStringVisitor(), findCustomName->second);
681 }
Patrick Williams26601e82021-10-06 12:43:25 -0500682 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700683 {
684 std::cerr << "Unable to parse " << keyName << "\n";
685 continue;
686 }
687
688 // All strings are valid, including empty string
689 customizedName = true;
690 }
691
692 bool customizedScale = false;
693 auto findCustomScale = baseConfig->second.find(keyScale);
694 if (findCustomScale != baseConfig->second.end())
695 {
696 try
697 {
698 psuProperty->sensorScaleFactor = std::visit(
699 VariantToUnsignedIntVisitor(), findCustomScale->second);
700 }
Patrick Williams26601e82021-10-06 12:43:25 -0500701 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700702 {
703 std::cerr << "Unable to parse " << keyScale << "\n";
704 continue;
705 }
706
707 // Avoid later division by zero
708 if (psuProperty->sensorScaleFactor > 0)
709 {
710 customizedScale = true;
711 }
712 else
713 {
714 std::cerr << "Unable to accept " << keyScale << "\n";
715 continue;
716 }
717 }
718
719 auto findCustomMin = baseConfig->second.find(keyMin);
720 if (findCustomMin != baseConfig->second.end())
721 {
722 try
723 {
724 psuProperty->minReading = std::visit(
725 VariantToDoubleVisitor(), findCustomMin->second);
726 }
Patrick Williams26601e82021-10-06 12:43:25 -0500727 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700728 {
729 std::cerr << "Unable to parse " << keyMin << "\n";
730 continue;
731 }
732 }
733
734 auto findCustomMax = baseConfig->second.find(keyMax);
735 if (findCustomMax != baseConfig->second.end())
736 {
737 try
738 {
739 psuProperty->maxReading = std::visit(
740 VariantToDoubleVisitor(), findCustomMax->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 " << keyMax << "\n";
745 continue;
746 }
747 }
748
Jeff Line41d52f2021-04-07 19:38:51 +0800749 auto findCustomOffset = baseConfig->second.find(keyOffset);
750 if (findCustomOffset != baseConfig->second.end())
751 {
752 try
753 {
754 psuProperty->sensorOffset = std::visit(
755 VariantToDoubleVisitor(), findCustomOffset->second);
756 }
Patrick Williams26601e82021-10-06 12:43:25 -0500757 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800758 {
759 std::cerr << "Unable to parse " << keyOffset << "\n";
760 continue;
761 }
762 }
763
Lotus Xucb5af732021-09-10 15:18:50 +0800764 // if we find label head power state set ,override the powerstate.
765 auto findPowerState = baseConfig->second.find(keyPowerState);
766 if (findPowerState != baseConfig->second.end())
767 {
768 std::string powerState = std::visit(VariantToStringVisitor(),
769 findPowerState->second);
770 setReadState(powerState, readState);
771 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700772 if (!(psuProperty->minReading < psuProperty->maxReading))
773 {
774 std::cerr << "Min must be less than Max\n";
775 continue;
776 }
777
778 // If the sensor name is being customized by config file,
779 // then prefix/suffix composition becomes not necessary,
780 // and in fact not wanted, because it gets in the way.
781 std::string psuNameFromIndex;
782 if (!customizedName)
783 {
784 /* Find out sensor name index for this label */
785 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500786 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700787 if (std::regex_search(labelHead, matches, rgx))
788 {
789 nameIndex = std::stoi(matches[1]);
790
791 // Decrement to preserve alignment, because hwmon
792 // human-readable filenames and labels use 1-based
793 // numbering, but the "Name", "Name1", "Name2", etc. naming
794 // convention (the psuNames vector) uses 0-based numbering.
795 if (nameIndex > 0)
796 {
797 --nameIndex;
798 }
799 }
800 else
801 {
802 nameIndex = 0;
803 }
804
805 if (psuNames.size() <= nameIndex)
806 {
807 std::cerr << "Could not pair " << labelHead
808 << " with a Name field\n";
809 continue;
810 }
811
812 psuNameFromIndex = psuNames[nameIndex];
813
Ed Tanous8a57ec02020-10-09 12:46:52 -0700814 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700815 {
816 std::cerr << "Sensor label head " << labelHead
817 << " paired with " << psuNameFromIndex
818 << " at index " << nameIndex << "\n";
819 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700820 }
821
Cheng C Yang58b2b532019-05-31 00:19:45 +0800822 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
823
Josh Lehan74d9bd92019-10-31 08:51:58 -0700824 // Similarly, if sensor scaling factor is being customized,
825 // then the below power-of-10 constraint becomes unnecessary,
826 // as config should be able to specify an arbitrary divisor.
827 unsigned int factor = psuProperty->sensorScaleFactor;
828 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700829 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700830 // Preserve existing usage of hardcoded labelMatch table below
831 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700832
Josh Lehan74d9bd92019-10-31 08:51:58 -0700833 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700834 char firstChar =
835 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700836 std::string strScaleFactor =
837 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
838
839 // Preserve existing configs by accepting earlier syntax,
840 // example CurrScaleFactor, PowerScaleFactor, ...
841 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
842 if (findScaleFactor != baseConfig->second.end())
843 {
844 factor = std::visit(VariantToIntVisitor(),
845 findScaleFactor->second);
846 }
847
Ed Tanous8a57ec02020-10-09 12:46:52 -0700848 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700849 {
850 std::cerr << "Sensor scaling factor " << factor
851 << " string " << strScaleFactor << "\n";
852 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700853 }
854
Vijay Khemka996bad12019-05-28 15:15:16 -0700855 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530856 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
857 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800858 {
James Feist17ab6e02019-06-25 12:28:13 -0700859 std::cerr << "error populating thresholds for "
860 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800861 }
862
Zev Weiss6b6891c2021-04-22 02:46:21 -0500863 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
864 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800865 {
Jason Ling5747fab2019-10-02 16:46:23 -0700866 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700867 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800868 continue;
869 }
870
Ed Tanous8a57ec02020-10-09 12:46:52 -0700871 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700872 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700873 std::cerr << "Sensor properties: Name \""
874 << psuProperty->labelTypeName << "\" Scale "
875 << psuProperty->sensorScaleFactor << " Min "
876 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800877 << psuProperty->maxReading << " Offset "
878 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700879 }
880
881 std::string sensorName = psuProperty->labelTypeName;
882 if (customizedName)
883 {
884 if (sensorName.empty())
885 {
886 // Allow selective disabling of an individual sensor,
887 // by customizing its name to an empty string.
888 std::cerr << "Sensor disabled, empty string\n";
889 continue;
890 }
891 }
892 else
893 {
894 // Sensor name not customized, do prefix/suffix composition,
895 // preserving default behavior by using psuNameFromIndex.
896 sensorName =
897 psuNameFromIndex + " " + psuProperty->labelTypeName;
898 }
899
Ed Tanous8a57ec02020-10-09 12:46:52 -0700900 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700901 {
902 std::cerr << "Sensor name \"" << sensorName << "\" path \""
903 << sensorPathStr << "\" type \"" << sensorType
904 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700905 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800906 // destruct existing one first if already created
907 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800908 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800909 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800910 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000911 readState, findSensorUnit->second, factor,
912 psuProperty->maxReading, psuProperty->minReading,
913 psuProperty->sensorOffset, labelHead, thresholdConfSize,
914 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800915 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700916 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700917 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700918 {
919 std::cerr << "Created " << numCreated << " sensors so far\n";
920 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800921 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800922
923 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800924 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800925 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000926 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
927 *psuName, readState,
928 eventPathList, groupEventPathList,
929 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800930 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700931
Ed Tanous8a57ec02020-10-09 12:46:52 -0700932 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700933 {
934 std::cerr << "Created total of " << numCreated << " sensors\n";
935 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800936 return;
937}
938
Zhikui Ren23c96e72020-11-05 22:32:28 -0800939void createSensors(
940 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
941 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
942 const std::shared_ptr<boost::container::flat_set<std::string>>&
943 sensorsChanged)
944{
945 auto getter = std::make_shared<GetSensorConfiguration>(
946 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
947 const ManagedObjectType& sensorConfigs) {
948 createSensorsCallback(io, objectServer, dbusConnection,
949 sensorConfigs, sensorsChanged);
950 });
951 getter->getConfiguration(
952 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
953}
954
Cheng C Yang916360b2019-05-07 18:47:16 +0800955void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800956{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500957 sensorTable = {{"power", sensor_paths::unitWatts},
958 {"curr", sensor_paths::unitAmperes},
959 {"temp", sensor_paths::unitDegreesC},
960 {"in", sensor_paths::unitVolts},
961 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800962
Jeff Line41d52f2021-04-07 19:38:51 +0800963 labelMatch = {
964 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800965 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
966 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800967 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
968 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
969 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
970 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800971 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
972 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
973 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800974 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
975 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
976 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
977 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
978 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
979 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
980 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
981 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
982 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
985 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
986 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
987 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
988 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
989 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
993 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
994 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
995 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
997 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
998 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
999 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1000 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1001 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1002 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1003 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1004 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1005 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1006 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1007 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1008 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1009 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001010 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001011 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001012 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1013 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1014 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1015 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1016 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1017 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001018 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001019 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1020 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001021 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1022 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1023 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1024 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1025 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1026 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1027 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1028 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1029 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1030 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1031 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1032 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1033 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1034 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1035 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001036 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1037 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1038 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001039 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1040 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1041 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1042 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1043 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1044 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1045 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1046 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1047 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
1048 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001049
1050 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001051
1052 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1053 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1054
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001055 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1056 {"Failure", {"in2_alarm"}},
1057 {"ACLost", {"in1_beep"}},
1058 {"ConfigureError", {"in1_fault"}}};
1059
1060 groupEventMatch = {{"FanFault",
1061 {{"fan1", {"fan1_alarm", "fan1_fault"}},
1062 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001063}
1064
James Feistb6c0b912019-07-09 12:21:44 -07001065int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001066{
1067 boost::asio::io_service io;
1068 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1069
1070 systemBus->request_name("xyz.openbmc_project.PSUSensor");
1071 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001072 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -08001073 auto sensorsChanged =
1074 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001075
Cheng C Yang916360b2019-05-07 18:47:16 +08001076 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001077
Zhikui Ren23c96e72020-11-05 22:32:28 -08001078 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +08001079 boost::asio::deadline_timer filterTimer(io);
1080 std::function<void(sdbusplus::message::message&)> eventHandler =
1081 [&](sdbusplus::message::message& message) {
1082 if (message.is_method_error())
1083 {
1084 std::cerr << "callback method error\n";
1085 return;
1086 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001087 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +08001088 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +08001089 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1090 if (ec == boost::asio::error::operation_aborted)
1091 {
1092 return;
1093 }
Ed Tanous8a57ec02020-10-09 12:46:52 -07001094 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +08001095 {
1096 std::cerr << "timer error\n";
1097 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001098 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +08001099 });
1100 };
1101
1102 for (const char* type : sensorTypes)
1103 {
1104 auto match = std::make_unique<sdbusplus::bus::match::match>(
1105 static_cast<sdbusplus::bus::bus&>(*systemBus),
1106 "type='signal',member='PropertiesChanged',path_namespace='" +
1107 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
1108 eventHandler);
1109 matches.emplace_back(std::move(match));
1110 }
Bruce Lee1263c3d2021-06-04 15:16:33 +08001111
1112 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001113 io.run();
1114}