blob: a523a5e1ea19f93500f59f180dd356d589ca502f [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",
Shamim Alidccd1d42022-06-08 22:31:35 +053068 "xyz.openbmc_project.Configuration.MP2971",
69 "xyz.openbmc_project.Configuration.MP2973",
Howard Chiub58ac3a2021-12-07 17:12:56 +080070 "xyz.openbmc_project.Configuration.MP5023",
Brandon Kim66558232021-11-09 16:53:08 -080071 "xyz.openbmc_project.Configuration.pmbus",
72 "xyz.openbmc_project.Configuration.PXE1610",
73 "xyz.openbmc_project.Configuration.RAA228000",
74 "xyz.openbmc_project.Configuration.RAA228228",
75 "xyz.openbmc_project.Configuration.RAA229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -080076 "xyz.openbmc_project.Configuration.RAA229126",
Brandon Kim66558232021-11-09 16:53:08 -080077 "xyz.openbmc_project.Configuration.TPS546D24",
78 "xyz.openbmc_project.Configuration.XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080079
Ed Tanousa2df7862021-12-07 16:30:27 -080080// clang-format off
81static constexpr auto pmbusNames{std::to_array<const char*>({
82 "adm1266",
83 "adm1272",
84 "adm1275",
85 "adm1278",
Zhikui Ren842e5432022-03-09 16:18:24 -080086 "adm1293",
Tim Chao6f379ce2022-02-24 11:08:09 +080087 "ads7830",
linchuyuan5cf66df2021-12-21 17:38:55 -080088 "bmr490",
Ed Tanousa2df7862021-12-07 16:30:27 -080089 "dps800",
90 "ina219",
91 "ina230",
92 "ipsps1",
93 "isl68137",
94 "isl68220",
95 "isl68223",
Khang Kieu51ad6672022-02-09 01:26:25 +000096 "isl69225",
Ed Tanousa2df7862021-12-07 16:30:27 -080097 "isl69243",
98 "isl69260",
99 "lm25066",
100 "max16601",
101 "max20710",
102 "max20730",
103 "max20734",
104 "max20796",
105 "max34451",
Shamim Alidccd1d42022-06-08 22:31:35 +0530106 "mp2971",
107 "mp2973",
Howard Chiub58ac3a2021-12-07 17:12:56 +0800108 "mp5023",
Ed Tanousa2df7862021-12-07 16:30:27 -0800109 "pmbus",
110 "pxe1610",
111 "raa228000",
112 "raa228228",
113 "raa229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800114 "raa229126",
Ed Tanousa2df7862021-12-07 16:30:27 -0800115 "tps546d24",
116 "xdpe12284"
117})};
118//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700119
Cheng C Yang209ec562019-03-12 16:37:44 +0800120namespace fs = std::filesystem;
121
Yong Libf8b1da2020-04-15 16:32:50 +0800122static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800123 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800124static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
125 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800126static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
127 pwmSensors;
128static boost::container::flat_map<std::string, std::string> sensorTable;
129static boost::container::flat_map<std::string, PSUProperty> labelMatch;
130static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800131static boost::container::flat_map<std::string, std::vector<std::string>>
132 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800133static boost::container::flat_map<
134 std::string,
135 boost::container::flat_map<std::string, std::vector<std::string>>>
136 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800137static boost::container::flat_map<std::string, std::vector<std::string>>
138 limitEventMatch;
139
Josh Lehan74d9bd92019-10-31 08:51:58 -0700140static std::vector<PSUProperty> psuProperties;
141
Cheng C Yang58b2b532019-05-31 00:19:45 +0800142// Function CheckEvent will check each attribute from eventMatch table in the
143// sysfs. If the attributes exists in sysfs, then store the complete path
144// of the attribute into eventPathList.
145void checkEvent(
146 const std::string& directory,
147 const boost::container::flat_map<std::string, std::vector<std::string>>&
148 eventMatch,
149 boost::container::flat_map<std::string, std::vector<std::string>>&
150 eventPathList)
151{
152 for (const auto& match : eventMatch)
153 {
154 const std::vector<std::string>& eventAttrs = match.second;
155 const std::string& eventName = match.first;
156 for (const auto& eventAttr : eventAttrs)
157 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700158 std::string eventPath = directory;
159 eventPath += "/";
160 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800161
162 std::ifstream eventFile(eventPath);
163 if (!eventFile.good())
164 {
165 continue;
166 }
167
168 eventPathList[eventName].push_back(eventPath);
169 }
170 }
171}
172
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800173// Check Group Events which contains more than one targets in each combine
174// events.
175void checkGroupEvent(
176 const std::string& directory,
177 const boost::container::flat_map<
178 std::string,
179 boost::container::flat_map<std::string, std::vector<std::string>>>&
180 groupEventMatch,
181 boost::container::flat_map<
182 std::string,
183 boost::container::flat_map<std::string, std::vector<std::string>>>&
184 groupEventPathList)
185{
186 for (const auto& match : groupEventMatch)
187 {
188 const std::string& groupEventName = match.first;
189 const boost::container::flat_map<std::string, std::vector<std::string>>
190 events = match.second;
191 boost::container::flat_map<std::string, std::vector<std::string>>
192 pathList;
193 for (const auto& match : events)
194 {
195 const std::string& eventName = match.first;
196 const std::vector<std::string>& eventAttrs = match.second;
197 for (const auto& eventAttr : eventAttrs)
198 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700199 std::string eventPath = directory;
200 eventPath += "/";
201 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800202 std::ifstream eventFile(eventPath);
203 if (!eventFile.good())
204 {
205 continue;
206 }
207
208 pathList[eventName].push_back(eventPath);
209 }
210 }
211 groupEventPathList[groupEventName] = pathList;
212 }
213}
214
Cheng C Yang58b2b532019-05-31 00:19:45 +0800215// Function checkEventLimits will check all the psu related xxx_input attributes
216// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
217// xxx_min_alarm exist, then store the existing paths of the alarm attributes
218// to eventPathList.
219void checkEventLimits(
220 const std::string& sensorPathStr,
221 const boost::container::flat_map<std::string, std::vector<std::string>>&
222 limitEventMatch,
223 boost::container::flat_map<std::string, std::vector<std::string>>&
224 eventPathList)
225{
Lei YUa2c7cea2020-12-23 14:07:28 +0800226 auto attributePartPos = sensorPathStr.find_last_of('_');
227 if (attributePartPos == std::string::npos)
228 {
229 // There is no '_' in the string, skip it
230 return;
231 }
232 auto attributePart =
233 std::string_view(sensorPathStr).substr(attributePartPos + 1);
234 if (attributePart != "input")
235 {
236 // If the sensor is not xxx_input, skip it
237 return;
238 }
239
240 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800241 for (const auto& limitMatch : limitEventMatch)
242 {
243 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
244 const std::string& eventName = limitMatch.first;
245 for (const auto& limitEventAttr : limitEventAttrs)
246 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800247 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800248 std::ifstream eventFile(limitEventPath);
249 if (!eventFile.good())
250 {
251 continue;
252 }
253 eventPathList[eventName].push_back(limitEventPath);
254 }
255 }
256}
Cheng C Yang916360b2019-05-07 18:47:16 +0800257
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530258static void
259 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
260 const std::string& interfacePath,
261 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
262 sdbusplus::asio::object_server& objectServer,
263 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800264{
265 for (const auto& pwmName : pwmTable)
266 {
267 if (pwmName.first != labelHead)
268 {
269 continue;
270 }
271
272 const std::string& sensorPathStr = sensorPath.string();
273 const std::string& pwmPathStr =
274 boost::replace_all_copy(sensorPathStr, "input", "target");
275 std::ifstream pwmFile(pwmPathStr);
276 if (!pwmFile.good())
277 {
278 continue;
279 }
280
281 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
282 if (findPWMSensor != pwmSensors.end())
283 {
284 continue;
285 }
286
287 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530288 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
289 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800290 }
291}
292
Zhikui Ren23c96e72020-11-05 22:32:28 -0800293static void createSensorsCallback(
294 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
295 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
296 const ManagedObjectType& sensorConfigs,
297 const std::shared_ptr<boost::container::flat_set<std::string>>&
298 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800299{
Josh Lehan49cfba92019-10-08 16:50:42 -0700300 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800301 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800302
303 std::vector<fs::path> pmbusPaths;
304 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
305 {
306 std::cerr << "No PSU sensors in system\n";
307 return;
308 }
309
310 boost::container::flat_set<std::string> directories;
311 for (const auto& pmbusPath : pmbusPaths)
312 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800313 boost::container::flat_map<std::string, std::vector<std::string>>
314 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800315 boost::container::flat_map<
316 std::string,
317 boost::container::flat_map<std::string, std::vector<std::string>>>
318 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800319
320 std::ifstream nameFile(pmbusPath);
321 if (!nameFile.good())
322 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700323 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800324 continue;
325 }
326
327 std::string pmbusName;
328 std::getline(nameFile, pmbusName);
329 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700330
331 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
332 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800333 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700334 // To avoid this error message, add your driver name to
335 // the pmbusNames vector at the top of this file.
336 std::cerr << "Driver name " << pmbusName
337 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800338 continue;
339 }
340
Cheng C Yang209ec562019-03-12 16:37:44 +0800341 auto directory = pmbusPath.parent_path();
342
343 auto ret = directories.insert(directory.string());
344 if (!ret.second)
345 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700346 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800347 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800348 }
349
James Feistb6c0b912019-07-09 12:21:44 -0700350 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800351 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700352 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800353 if (findHyphen == std::string::npos)
354 {
355 std::cerr << "found bad device" << deviceName << "\n";
356 continue;
357 }
358 std::string busStr = deviceName.substr(0, findHyphen);
359 std::string addrStr = deviceName.substr(findHyphen + 1);
360
361 size_t bus = 0;
362 size_t addr = 0;
363
364 try
365 {
366 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700367 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800368 }
Patrick Williams26601e82021-10-06 12:43:25 -0500369 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800370 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700371 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
372 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800373 continue;
374 }
375
Cheng C Yang209ec562019-03-12 16:37:44 +0800376 const std::pair<std::string, boost::container::flat_map<
377 std::string, BasicVariantType>>*
378 baseConfig = nullptr;
379 const SensorData* sensorData = nullptr;
380 const std::string* interfacePath = nullptr;
381 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800382 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800383
384 for (const std::pair<sdbusplus::message::object_path, SensorData>&
385 sensor : sensorConfigs)
386 {
387 sensorData = &(sensor.second);
388 for (const char* type : sensorTypes)
389 {
390 auto sensorBase = sensorData->find(type);
391 if (sensorBase != sensorData->end())
392 {
393 baseConfig = &(*sensorBase);
394 sensorType = type;
395 break;
396 }
397 }
398 if (baseConfig == nullptr)
399 {
400 std::cerr << "error finding base configuration for "
401 << deviceName << "\n";
402 continue;
403 }
404
405 auto configBus = baseConfig->second.find("Bus");
406 auto configAddress = baseConfig->second.find("Address");
407
408 if (configBus == baseConfig->second.end() ||
409 configAddress == baseConfig->second.end())
410 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800411 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800412 continue;
413 }
414
Ed Tanousa771f6a2022-01-14 09:36:51 -0800415 const uint64_t* confBus = std::get_if<uint64_t>(&(configBus->second));
416 const uint64_t* confAddr = std::get_if<uint64_t>(&(configAddress->second));
417 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800418 {
419 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700420 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800421 continue;
422 }
423
424 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800425 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700426 std::cerr << "Configuration skipping " << *confBus << "-"
427 << *confAddr << " because not " << bus << "-" << addr
428 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800429 continue;
430 }
431
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800432 std::vector<thresholds::Threshold> confThresholds;
433 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
434 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800435 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800436 }
437 thresholdConfSize = confThresholds.size();
438
Cheng C Yang209ec562019-03-12 16:37:44 +0800439 interfacePath = &(sensor.first.str);
440 break;
441 }
442 if (interfacePath == nullptr)
443 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700444 // To avoid this error message, add your export map entry,
445 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800446 std::cerr << "failed to find match for " << deviceName << "\n";
447 continue;
448 }
449
Cheng C Yange50345b2019-04-02 17:26:15 +0800450 auto findPSUName = baseConfig->second.find("Name");
451 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800452 {
453 std::cerr << "could not determine configuration name for "
454 << deviceName << "\n";
455 continue;
456 }
Ed Tanousa771f6a2022-01-14 09:36:51 -0800457 const std::string* psuName = std::get_if<std::string>(&(findPSUName->second));
458 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800459 {
460 std::cerr << "Cannot find psu name, invalid configuration\n";
461 continue;
462 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800463
464 // on rescans, only update sensors we were signaled by
465 if (!firstScan)
466 {
Zhikui Renda98f092021-11-01 09:41:08 -0700467 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800468 auto it =
469 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
470 [psuNameStr](std::string& s) {
471 return boost::ends_with(s, psuNameStr);
472 });
473
474 if (it == sensorsChanged->end())
475 {
476 continue;
477 }
478 sensorsChanged->erase(it);
479 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800480 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800481 checkGroupEvent(directory.string(), groupEventMatch,
482 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800483
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000484 PowerState readState = PowerState::always;
485 auto findPowerOn = baseConfig->second.find("PowerState");
486 if (findPowerOn != baseConfig->second.end())
487 {
488 std::string powerState =
489 std::visit(VariantToStringVisitor(), findPowerOn->second);
490 setReadState(powerState, readState);
491 }
492
Vijay Khemka996bad12019-05-28 15:15:16 -0700493 /* Check if there are more sensors in the same interface */
494 int i = 1;
495 std::vector<std::string> psuNames;
496 do
497 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700498 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700499 psuNames.push_back(
500 escapeName(std::get<std::string>(findPSUName->second)));
Vijay Khemka996bad12019-05-28 15:15:16 -0700501 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
502 } while (findPSUName != baseConfig->second.end());
503
Cheng C Yange50345b2019-04-02 17:26:15 +0800504 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700505 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800506 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800507 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800508 continue;
509 }
510
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530511 /* read max value in sysfs for in, curr, power, temp, ... */
512 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
513 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700514 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530515 {
516 std::cerr << "No max name in PSU \n";
517 }
518 }
519
Lei YU7170a232021-02-04 16:19:27 +0800520 /* The poll rate for the sensors */
521 double pollRate = 0.0;
522 auto pollRateObj = baseConfig->second.find("PollRate");
523
524 if (pollRateObj != baseConfig->second.end())
525 {
526 pollRate =
527 std::visit(VariantToDoubleVisitor(), pollRateObj->second);
528 if (pollRate <= 0.0)
529 {
530 pollRate = PSUSensor::defaultSensorPoll;
531 }
532 }
533
Vijay Khemka996bad12019-05-28 15:15:16 -0700534 /* Find array of labels to be exposed if it is defined in config */
535 std::vector<std::string> findLabels;
536 auto findLabelObj = baseConfig->second.find("Labels");
537 if (findLabelObj != baseConfig->second.end())
538 {
539 findLabels =
540 std::get<std::vector<std::string>>(findLabelObj->second);
541 }
542
Jason Ling5747fab2019-10-02 16:46:23 -0700543 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
544 std::smatch matches;
545
Cheng C Yange50345b2019-04-02 17:26:15 +0800546 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800547 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530548 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800549 std::string labelHead;
550 std::string sensorPathStr = sensorPath.string();
551 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700552 std::string sensorNameSubStr{""};
553 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
554 {
Josh Lehan06494452019-10-31 09:49:16 -0700555 // hwmon *_input filename without number:
556 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700557 sensorNameSubStr = matches[1];
558 }
559 else
560 {
Josh Lehan06494452019-10-31 09:49:16 -0700561 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700562 << sensorNameStr;
563 continue;
564 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800565
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530566 std::string labelPath;
567
568 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700569 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530570 if (pos != std::string::npos)
571 {
572
573 std::string sensorPathStrMax = sensorPathStr.substr(pos);
574 if (sensorPathStrMax.compare("_max") == 0)
575 {
576 labelPath =
577 boost::replace_all_copy(sensorPathStr, "max", "label");
578 maxLabel = true;
579 }
580 else
581 {
582 labelPath = boost::replace_all_copy(sensorPathStr, "input",
583 "label");
584 maxLabel = false;
585 }
586 }
587 else
588 {
589 continue;
590 }
591
Cheng C Yangecba9de2019-09-12 23:41:50 +0800592 std::ifstream labelFile(labelPath);
593 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800594 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700595 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800596 {
597 std::cerr << "Input file " << sensorPath
598 << " has no corresponding label file\n";
599 }
Josh Lehan06494452019-10-31 09:49:16 -0700600 // hwmon *_input filename with number:
601 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700602 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800603 }
604 else
605 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800606 std::string label;
607 std::getline(labelFile, label);
608 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800609 auto findSensor = sensors.find(label);
610 if (findSensor != sensors.end())
611 {
612 continue;
613 }
614
Josh Lehan06494452019-10-31 09:49:16 -0700615 // hwmon corresponding *_label file contents:
616 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700617 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800618 }
619
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530620 /* append "max" for labelMatch */
621 if (maxLabel)
622 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700623 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530624 }
625
Ed Tanous8a57ec02020-10-09 12:46:52 -0700626 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700627 {
628 std::cerr << "Sensor type=\"" << sensorNameSubStr
629 << "\" label=\"" << labelHead << "\"\n";
630 }
631
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530632 checkPWMSensor(sensorPath, labelHead, *interfacePath,
633 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800634
Vijay Khemka996bad12019-05-28 15:15:16 -0700635 if (!findLabels.empty())
636 {
637 /* Check if this labelHead is enabled in config file */
638 if (std::find(findLabels.begin(), findLabels.end(),
639 labelHead) == findLabels.end())
640 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700641 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800642 {
643 std::cerr << "could not find " << labelHead
644 << " in the Labels list\n";
645 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700646 continue;
647 }
648 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800649
Cheng C Yange50345b2019-04-02 17:26:15 +0800650 auto findProperty = labelMatch.find(labelHead);
651 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800652 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700653 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700654 {
655 std::cerr << "Could not find matching default property for "
656 << labelHead << "\n";
657 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800658 continue;
659 }
660
Josh Lehan74d9bd92019-10-31 08:51:58 -0700661 // Protect the hardcoded labelMatch list from changes,
662 // by making a copy and modifying that instead.
663 // Avoid bleedthrough of one device's customizations to
664 // the next device, as each should be independently customizable.
665 psuProperties.push_back(findProperty->second);
666 auto psuProperty = psuProperties.rbegin();
667
668 // Use label head as prefix for reading from config file,
669 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
670 std::string keyName = labelHead + "_Name";
671 std::string keyScale = labelHead + "_Scale";
672 std::string keyMin = labelHead + "_Min";
673 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800674 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800675 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700676
677 bool customizedName = false;
678 auto findCustomName = baseConfig->second.find(keyName);
679 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700680 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700681 try
682 {
683 psuProperty->labelTypeName = std::visit(
684 VariantToStringVisitor(), findCustomName->second);
685 }
Patrick Williams26601e82021-10-06 12:43:25 -0500686 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700687 {
688 std::cerr << "Unable to parse " << keyName << "\n";
689 continue;
690 }
691
692 // All strings are valid, including empty string
693 customizedName = true;
694 }
695
696 bool customizedScale = false;
697 auto findCustomScale = baseConfig->second.find(keyScale);
698 if (findCustomScale != baseConfig->second.end())
699 {
700 try
701 {
702 psuProperty->sensorScaleFactor = std::visit(
703 VariantToUnsignedIntVisitor(), findCustomScale->second);
704 }
Patrick Williams26601e82021-10-06 12:43:25 -0500705 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700706 {
707 std::cerr << "Unable to parse " << keyScale << "\n";
708 continue;
709 }
710
711 // Avoid later division by zero
712 if (psuProperty->sensorScaleFactor > 0)
713 {
714 customizedScale = true;
715 }
716 else
717 {
718 std::cerr << "Unable to accept " << keyScale << "\n";
719 continue;
720 }
721 }
722
723 auto findCustomMin = baseConfig->second.find(keyMin);
724 if (findCustomMin != baseConfig->second.end())
725 {
726 try
727 {
728 psuProperty->minReading = std::visit(
729 VariantToDoubleVisitor(), findCustomMin->second);
730 }
Patrick Williams26601e82021-10-06 12:43:25 -0500731 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700732 {
733 std::cerr << "Unable to parse " << keyMin << "\n";
734 continue;
735 }
736 }
737
738 auto findCustomMax = baseConfig->second.find(keyMax);
739 if (findCustomMax != baseConfig->second.end())
740 {
741 try
742 {
743 psuProperty->maxReading = std::visit(
744 VariantToDoubleVisitor(), findCustomMax->second);
745 }
Patrick Williams26601e82021-10-06 12:43:25 -0500746 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700747 {
748 std::cerr << "Unable to parse " << keyMax << "\n";
749 continue;
750 }
751 }
752
Jeff Line41d52f2021-04-07 19:38:51 +0800753 auto findCustomOffset = baseConfig->second.find(keyOffset);
754 if (findCustomOffset != baseConfig->second.end())
755 {
756 try
757 {
758 psuProperty->sensorOffset = std::visit(
759 VariantToDoubleVisitor(), findCustomOffset->second);
760 }
Patrick Williams26601e82021-10-06 12:43:25 -0500761 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800762 {
763 std::cerr << "Unable to parse " << keyOffset << "\n";
764 continue;
765 }
766 }
767
Lotus Xucb5af732021-09-10 15:18:50 +0800768 // if we find label head power state set ,override the powerstate.
769 auto findPowerState = baseConfig->second.find(keyPowerState);
770 if (findPowerState != baseConfig->second.end())
771 {
772 std::string powerState = std::visit(VariantToStringVisitor(),
773 findPowerState->second);
774 setReadState(powerState, readState);
775 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700776 if (!(psuProperty->minReading < psuProperty->maxReading))
777 {
778 std::cerr << "Min must be less than Max\n";
779 continue;
780 }
781
782 // If the sensor name is being customized by config file,
783 // then prefix/suffix composition becomes not necessary,
784 // and in fact not wanted, because it gets in the way.
785 std::string psuNameFromIndex;
786 if (!customizedName)
787 {
788 /* Find out sensor name index for this label */
789 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500790 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700791 if (std::regex_search(labelHead, matches, rgx))
792 {
793 nameIndex = std::stoi(matches[1]);
794
795 // Decrement to preserve alignment, because hwmon
796 // human-readable filenames and labels use 1-based
797 // numbering, but the "Name", "Name1", "Name2", etc. naming
798 // convention (the psuNames vector) uses 0-based numbering.
799 if (nameIndex > 0)
800 {
801 --nameIndex;
802 }
803 }
804 else
805 {
806 nameIndex = 0;
807 }
808
809 if (psuNames.size() <= nameIndex)
810 {
811 std::cerr << "Could not pair " << labelHead
812 << " with a Name field\n";
813 continue;
814 }
815
816 psuNameFromIndex = psuNames[nameIndex];
817
Ed Tanous8a57ec02020-10-09 12:46:52 -0700818 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700819 {
820 std::cerr << "Sensor label head " << labelHead
821 << " paired with " << psuNameFromIndex
822 << " at index " << nameIndex << "\n";
823 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700824 }
825
Cheng C Yang58b2b532019-05-31 00:19:45 +0800826 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
827
Josh Lehan74d9bd92019-10-31 08:51:58 -0700828 // Similarly, if sensor scaling factor is being customized,
829 // then the below power-of-10 constraint becomes unnecessary,
830 // as config should be able to specify an arbitrary divisor.
831 unsigned int factor = psuProperty->sensorScaleFactor;
832 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700833 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700834 // Preserve existing usage of hardcoded labelMatch table below
835 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700836
Josh Lehan74d9bd92019-10-31 08:51:58 -0700837 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700838 char firstChar =
839 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700840 std::string strScaleFactor =
841 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
842
843 // Preserve existing configs by accepting earlier syntax,
844 // example CurrScaleFactor, PowerScaleFactor, ...
845 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
846 if (findScaleFactor != baseConfig->second.end())
847 {
848 factor = std::visit(VariantToIntVisitor(),
849 findScaleFactor->second);
850 }
851
Ed Tanous8a57ec02020-10-09 12:46:52 -0700852 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700853 {
854 std::cerr << "Sensor scaling factor " << factor
855 << " string " << strScaleFactor << "\n";
856 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700857 }
858
Vijay Khemka996bad12019-05-28 15:15:16 -0700859 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530860 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
861 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800862 {
James Feist17ab6e02019-06-25 12:28:13 -0700863 std::cerr << "error populating thresholds for "
864 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800865 }
866
Zev Weiss6b6891c2021-04-22 02:46:21 -0500867 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
868 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800869 {
Jason Ling5747fab2019-10-02 16:46:23 -0700870 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700871 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800872 continue;
873 }
874
Ed Tanous8a57ec02020-10-09 12:46:52 -0700875 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700876 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700877 std::cerr << "Sensor properties: Name \""
878 << psuProperty->labelTypeName << "\" Scale "
879 << psuProperty->sensorScaleFactor << " Min "
880 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800881 << psuProperty->maxReading << " Offset "
882 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700883 }
884
885 std::string sensorName = psuProperty->labelTypeName;
886 if (customizedName)
887 {
888 if (sensorName.empty())
889 {
890 // Allow selective disabling of an individual sensor,
891 // by customizing its name to an empty string.
892 std::cerr << "Sensor disabled, empty string\n";
893 continue;
894 }
895 }
896 else
897 {
898 // Sensor name not customized, do prefix/suffix composition,
899 // preserving default behavior by using psuNameFromIndex.
900 sensorName =
901 psuNameFromIndex + " " + psuProperty->labelTypeName;
902 }
903
Ed Tanous8a57ec02020-10-09 12:46:52 -0700904 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700905 {
906 std::cerr << "Sensor name \"" << sensorName << "\" path \""
907 << sensorPathStr << "\" type \"" << sensorType
908 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700909 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800910 // destruct existing one first if already created
911 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800912 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800913 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800914 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000915 readState, findSensorUnit->second, factor,
916 psuProperty->maxReading, psuProperty->minReading,
917 psuProperty->sensorOffset, labelHead, thresholdConfSize,
918 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800919 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700920 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700921 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700922 {
923 std::cerr << "Created " << numCreated << " sensors so far\n";
924 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800925 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800926
927 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800928 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800929 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000930 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
931 *psuName, readState,
932 eventPathList, groupEventPathList,
933 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800934 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700935
Ed Tanous8a57ec02020-10-09 12:46:52 -0700936 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700937 {
938 std::cerr << "Created total of " << numCreated << " sensors\n";
939 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800940 return;
941}
942
Zhikui Ren23c96e72020-11-05 22:32:28 -0800943void createSensors(
944 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
945 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
946 const std::shared_ptr<boost::container::flat_set<std::string>>&
947 sensorsChanged)
948{
949 auto getter = std::make_shared<GetSensorConfiguration>(
950 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
951 const ManagedObjectType& sensorConfigs) {
952 createSensorsCallback(io, objectServer, dbusConnection,
953 sensorConfigs, sensorsChanged);
954 });
955 getter->getConfiguration(
956 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
957}
958
Cheng C Yang916360b2019-05-07 18:47:16 +0800959void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800960{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500961 sensorTable = {{"power", sensor_paths::unitWatts},
962 {"curr", sensor_paths::unitAmperes},
963 {"temp", sensor_paths::unitDegreesC},
964 {"in", sensor_paths::unitVolts},
965 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800966
Jeff Line41d52f2021-04-07 19:38:51 +0800967 labelMatch = {
968 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800969 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
970 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800971 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
972 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
973 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
974 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800975 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
976 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
977 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800978 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
979 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
980 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
981 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
982 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
985 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
986 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
987 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
988 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
989 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
993 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
994 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
995 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
997 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
998 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
999 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1000 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1001 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1002 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1003 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1004 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1005 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1006 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1007 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1008 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1009 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1010 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1011 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1012 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1013 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001014 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001015 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001016 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1017 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1018 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1019 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1020 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1021 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001022 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001023 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1024 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001025 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1026 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1027 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1028 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1029 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1030 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1031 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1032 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1033 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1034 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1035 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1036 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1037 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1038 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1039 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001040 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1041 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1042 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001043 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1044 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1045 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1046 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1047 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1048 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1049 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1050 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1051 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
1052 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001053
1054 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001055
1056 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1057 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1058
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001059 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1060 {"Failure", {"in2_alarm"}},
1061 {"ACLost", {"in1_beep"}},
1062 {"ConfigureError", {"in1_fault"}}};
1063
1064 groupEventMatch = {{"FanFault",
1065 {{"fan1", {"fan1_alarm", "fan1_fault"}},
1066 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001067}
1068
James Feistb6c0b912019-07-09 12:21:44 -07001069int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001070{
1071 boost::asio::io_service io;
1072 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1073
1074 systemBus->request_name("xyz.openbmc_project.PSUSensor");
1075 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001076 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -08001077 auto sensorsChanged =
1078 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001079
Cheng C Yang916360b2019-05-07 18:47:16 +08001080 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001081
Zhikui Ren23c96e72020-11-05 22:32:28 -08001082 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +08001083 boost::asio::deadline_timer filterTimer(io);
1084 std::function<void(sdbusplus::message::message&)> eventHandler =
1085 [&](sdbusplus::message::message& message) {
1086 if (message.is_method_error())
1087 {
1088 std::cerr << "callback method error\n";
1089 return;
1090 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001091 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +08001092 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +08001093 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1094 if (ec == boost::asio::error::operation_aborted)
1095 {
1096 return;
1097 }
Ed Tanous8a57ec02020-10-09 12:46:52 -07001098 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +08001099 {
1100 std::cerr << "timer error\n";
1101 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001102 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +08001103 });
1104 };
1105
1106 for (const char* type : sensorTypes)
1107 {
1108 auto match = std::make_unique<sdbusplus::bus::match::match>(
1109 static_cast<sdbusplus::bus::bus&>(*systemBus),
1110 "type='signal',member='PropertiesChanged',path_namespace='" +
1111 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
1112 eventHandler);
1113 matches.emplace_back(std::move(match));
1114 }
Bruce Lee1263c3d2021-06-04 15:16:33 +08001115
1116 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001117 io.run();
1118}