blob: 1e139cefdbfe76afcdc7ab34e3184abde7dd8e48 [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",
48 "xyz.openbmc_project.Configuration.DPS800",
49 "xyz.openbmc_project.Configuration.INA219",
50 "xyz.openbmc_project.Configuration.INA230",
51 "xyz.openbmc_project.Configuration.IPSPS",
52 "xyz.openbmc_project.Configuration.ISL68137",
53 "xyz.openbmc_project.Configuration.ISL68220",
54 "xyz.openbmc_project.Configuration.ISL68223",
Khang Kieu7d6b77d2022-02-10 10:43:36 +000055 "xyz.openbmc_project.Configuration.ISL69225",
Brandon Kim66558232021-11-09 16:53:08 -080056 "xyz.openbmc_project.Configuration.ISL69243",
57 "xyz.openbmc_project.Configuration.ISL69260",
58 "xyz.openbmc_project.Configuration.LM25066",
59 "xyz.openbmc_project.Configuration.MAX16601",
60 "xyz.openbmc_project.Configuration.MAX20710",
61 "xyz.openbmc_project.Configuration.MAX20730",
62 "xyz.openbmc_project.Configuration.MAX20734",
63 "xyz.openbmc_project.Configuration.MAX20796",
64 "xyz.openbmc_project.Configuration.MAX34451",
Howard Chiub58ac3a2021-12-07 17:12:56 +080065 "xyz.openbmc_project.Configuration.MP5023",
Brandon Kim66558232021-11-09 16:53:08 -080066 "xyz.openbmc_project.Configuration.pmbus",
67 "xyz.openbmc_project.Configuration.PXE1610",
68 "xyz.openbmc_project.Configuration.RAA228000",
69 "xyz.openbmc_project.Configuration.RAA228228",
70 "xyz.openbmc_project.Configuration.RAA229004",
71 "xyz.openbmc_project.Configuration.TPS546D24",
72 "xyz.openbmc_project.Configuration.XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080073
Ed Tanousa2df7862021-12-07 16:30:27 -080074// clang-format off
75static constexpr auto pmbusNames{std::to_array<const char*>({
76 "adm1266",
77 "adm1272",
78 "adm1275",
79 "adm1278",
80 "dps800",
81 "ina219",
82 "ina230",
83 "ipsps1",
84 "isl68137",
85 "isl68220",
86 "isl68223",
Khang Kieu51ad6672022-02-09 01:26:25 +000087 "isl69225",
Ed Tanousa2df7862021-12-07 16:30:27 -080088 "isl69243",
89 "isl69260",
90 "lm25066",
91 "max16601",
92 "max20710",
93 "max20730",
94 "max20734",
95 "max20796",
96 "max34451",
Howard Chiub58ac3a2021-12-07 17:12:56 +080097 "mp5023",
Ed Tanousa2df7862021-12-07 16:30:27 -080098 "pmbus",
99 "pxe1610",
100 "raa228000",
101 "raa228228",
102 "raa229004",
103 "tps546d24",
104 "xdpe12284"
105})};
106//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700107
Cheng C Yang209ec562019-03-12 16:37:44 +0800108namespace fs = std::filesystem;
109
Yong Libf8b1da2020-04-15 16:32:50 +0800110static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800111 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800112static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
113 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800114static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
115 pwmSensors;
116static boost::container::flat_map<std::string, std::string> sensorTable;
117static boost::container::flat_map<std::string, PSUProperty> labelMatch;
118static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800119static boost::container::flat_map<std::string, std::vector<std::string>>
120 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800121static boost::container::flat_map<
122 std::string,
123 boost::container::flat_map<std::string, std::vector<std::string>>>
124 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800125static boost::container::flat_map<std::string, std::vector<std::string>>
126 limitEventMatch;
127
Josh Lehan74d9bd92019-10-31 08:51:58 -0700128static std::vector<PSUProperty> psuProperties;
129
Cheng C Yang58b2b532019-05-31 00:19:45 +0800130// Function CheckEvent will check each attribute from eventMatch table in the
131// sysfs. If the attributes exists in sysfs, then store the complete path
132// of the attribute into eventPathList.
133void checkEvent(
134 const std::string& directory,
135 const boost::container::flat_map<std::string, std::vector<std::string>>&
136 eventMatch,
137 boost::container::flat_map<std::string, std::vector<std::string>>&
138 eventPathList)
139{
140 for (const auto& match : eventMatch)
141 {
142 const std::vector<std::string>& eventAttrs = match.second;
143 const std::string& eventName = match.first;
144 for (const auto& eventAttr : eventAttrs)
145 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700146 std::string eventPath = directory;
147 eventPath += "/";
148 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800149
150 std::ifstream eventFile(eventPath);
151 if (!eventFile.good())
152 {
153 continue;
154 }
155
156 eventPathList[eventName].push_back(eventPath);
157 }
158 }
159}
160
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800161// Check Group Events which contains more than one targets in each combine
162// events.
163void checkGroupEvent(
164 const std::string& directory,
165 const boost::container::flat_map<
166 std::string,
167 boost::container::flat_map<std::string, std::vector<std::string>>>&
168 groupEventMatch,
169 boost::container::flat_map<
170 std::string,
171 boost::container::flat_map<std::string, std::vector<std::string>>>&
172 groupEventPathList)
173{
174 for (const auto& match : groupEventMatch)
175 {
176 const std::string& groupEventName = match.first;
177 const boost::container::flat_map<std::string, std::vector<std::string>>
178 events = match.second;
179 boost::container::flat_map<std::string, std::vector<std::string>>
180 pathList;
181 for (const auto& match : events)
182 {
183 const std::string& eventName = match.first;
184 const std::vector<std::string>& eventAttrs = match.second;
185 for (const auto& eventAttr : eventAttrs)
186 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700187 std::string eventPath = directory;
188 eventPath += "/";
189 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800190 std::ifstream eventFile(eventPath);
191 if (!eventFile.good())
192 {
193 continue;
194 }
195
196 pathList[eventName].push_back(eventPath);
197 }
198 }
199 groupEventPathList[groupEventName] = pathList;
200 }
201}
202
Cheng C Yang58b2b532019-05-31 00:19:45 +0800203// Function checkEventLimits will check all the psu related xxx_input attributes
204// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
205// xxx_min_alarm exist, then store the existing paths of the alarm attributes
206// to eventPathList.
207void checkEventLimits(
208 const std::string& sensorPathStr,
209 const boost::container::flat_map<std::string, std::vector<std::string>>&
210 limitEventMatch,
211 boost::container::flat_map<std::string, std::vector<std::string>>&
212 eventPathList)
213{
Lei YUa2c7cea2020-12-23 14:07:28 +0800214 auto attributePartPos = sensorPathStr.find_last_of('_');
215 if (attributePartPos == std::string::npos)
216 {
217 // There is no '_' in the string, skip it
218 return;
219 }
220 auto attributePart =
221 std::string_view(sensorPathStr).substr(attributePartPos + 1);
222 if (attributePart != "input")
223 {
224 // If the sensor is not xxx_input, skip it
225 return;
226 }
227
228 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800229 for (const auto& limitMatch : limitEventMatch)
230 {
231 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
232 const std::string& eventName = limitMatch.first;
233 for (const auto& limitEventAttr : limitEventAttrs)
234 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800235 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800236 std::ifstream eventFile(limitEventPath);
237 if (!eventFile.good())
238 {
239 continue;
240 }
241 eventPathList[eventName].push_back(limitEventPath);
242 }
243 }
244}
Cheng C Yang916360b2019-05-07 18:47:16 +0800245
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530246static void
247 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
248 const std::string& interfacePath,
249 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
250 sdbusplus::asio::object_server& objectServer,
251 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800252{
253 for (const auto& pwmName : pwmTable)
254 {
255 if (pwmName.first != labelHead)
256 {
257 continue;
258 }
259
260 const std::string& sensorPathStr = sensorPath.string();
261 const std::string& pwmPathStr =
262 boost::replace_all_copy(sensorPathStr, "input", "target");
263 std::ifstream pwmFile(pwmPathStr);
264 if (!pwmFile.good())
265 {
266 continue;
267 }
268
269 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
270 if (findPWMSensor != pwmSensors.end())
271 {
272 continue;
273 }
274
275 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530276 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
277 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800278 }
279}
280
Zhikui Ren23c96e72020-11-05 22:32:28 -0800281static void createSensorsCallback(
282 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
283 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
284 const ManagedObjectType& sensorConfigs,
285 const std::shared_ptr<boost::container::flat_set<std::string>>&
286 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800287{
Josh Lehan49cfba92019-10-08 16:50:42 -0700288 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800289 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800290
291 std::vector<fs::path> pmbusPaths;
292 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
293 {
294 std::cerr << "No PSU sensors in system\n";
295 return;
296 }
297
298 boost::container::flat_set<std::string> directories;
299 for (const auto& pmbusPath : pmbusPaths)
300 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800301 boost::container::flat_map<std::string, std::vector<std::string>>
302 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800303 boost::container::flat_map<
304 std::string,
305 boost::container::flat_map<std::string, std::vector<std::string>>>
306 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800307
308 std::ifstream nameFile(pmbusPath);
309 if (!nameFile.good())
310 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700311 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800312 continue;
313 }
314
315 std::string pmbusName;
316 std::getline(nameFile, pmbusName);
317 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700318
319 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
320 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800321 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700322 // To avoid this error message, add your driver name to
323 // the pmbusNames vector at the top of this file.
324 std::cerr << "Driver name " << pmbusName
325 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800326 continue;
327 }
328
Cheng C Yang209ec562019-03-12 16:37:44 +0800329 auto directory = pmbusPath.parent_path();
330
331 auto ret = directories.insert(directory.string());
332 if (!ret.second)
333 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700334 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800335 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800336 }
337
James Feistb6c0b912019-07-09 12:21:44 -0700338 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800339 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700340 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800341 if (findHyphen == std::string::npos)
342 {
343 std::cerr << "found bad device" << deviceName << "\n";
344 continue;
345 }
346 std::string busStr = deviceName.substr(0, findHyphen);
347 std::string addrStr = deviceName.substr(findHyphen + 1);
348
349 size_t bus = 0;
350 size_t addr = 0;
351
352 try
353 {
354 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700355 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800356 }
Patrick Williams26601e82021-10-06 12:43:25 -0500357 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800358 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700359 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
360 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800361 continue;
362 }
363
Cheng C Yang209ec562019-03-12 16:37:44 +0800364 const std::pair<std::string, boost::container::flat_map<
365 std::string, BasicVariantType>>*
366 baseConfig = nullptr;
367 const SensorData* sensorData = nullptr;
368 const std::string* interfacePath = nullptr;
369 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800370 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800371
372 for (const std::pair<sdbusplus::message::object_path, SensorData>&
373 sensor : sensorConfigs)
374 {
375 sensorData = &(sensor.second);
376 for (const char* type : sensorTypes)
377 {
378 auto sensorBase = sensorData->find(type);
379 if (sensorBase != sensorData->end())
380 {
381 baseConfig = &(*sensorBase);
382 sensorType = type;
383 break;
384 }
385 }
386 if (baseConfig == nullptr)
387 {
388 std::cerr << "error finding base configuration for "
389 << deviceName << "\n";
390 continue;
391 }
392
393 auto configBus = baseConfig->second.find("Bus");
394 auto configAddress = baseConfig->second.find("Address");
395
396 if (configBus == baseConfig->second.end() ||
397 configAddress == baseConfig->second.end())
398 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800399 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800400 continue;
401 }
402
Ed Tanousa771f6a2022-01-14 09:36:51 -0800403 const uint64_t* confBus = std::get_if<uint64_t>(&(configBus->second));
404 const uint64_t* confAddr = std::get_if<uint64_t>(&(configAddress->second));
405 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800406 {
407 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700408 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800409 continue;
410 }
411
412 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800413 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700414 std::cerr << "Configuration skipping " << *confBus << "-"
415 << *confAddr << " because not " << bus << "-" << addr
416 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800417 continue;
418 }
419
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800420 std::vector<thresholds::Threshold> confThresholds;
421 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
422 {
423 std::cerr << "error populating totoal thresholds\n";
424 }
425 thresholdConfSize = confThresholds.size();
426
Cheng C Yang209ec562019-03-12 16:37:44 +0800427 interfacePath = &(sensor.first.str);
428 break;
429 }
430 if (interfacePath == nullptr)
431 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700432 // To avoid this error message, add your export map entry,
433 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800434 std::cerr << "failed to find match for " << deviceName << "\n";
435 continue;
436 }
437
Cheng C Yange50345b2019-04-02 17:26:15 +0800438 auto findPSUName = baseConfig->second.find("Name");
439 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800440 {
441 std::cerr << "could not determine configuration name for "
442 << deviceName << "\n";
443 continue;
444 }
Ed Tanousa771f6a2022-01-14 09:36:51 -0800445 const std::string* psuName = std::get_if<std::string>(&(findPSUName->second));
446 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800447 {
448 std::cerr << "Cannot find psu name, invalid configuration\n";
449 continue;
450 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800451
452 // on rescans, only update sensors we were signaled by
453 if (!firstScan)
454 {
Zhikui Renda98f092021-11-01 09:41:08 -0700455 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800456 auto it =
457 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
458 [psuNameStr](std::string& s) {
459 return boost::ends_with(s, psuNameStr);
460 });
461
462 if (it == sensorsChanged->end())
463 {
464 continue;
465 }
466 sensorsChanged->erase(it);
467 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800468 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800469 checkGroupEvent(directory.string(), groupEventMatch,
470 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800471
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000472 PowerState readState = PowerState::always;
473 auto findPowerOn = baseConfig->second.find("PowerState");
474 if (findPowerOn != baseConfig->second.end())
475 {
476 std::string powerState =
477 std::visit(VariantToStringVisitor(), findPowerOn->second);
478 setReadState(powerState, readState);
479 }
480
Vijay Khemka996bad12019-05-28 15:15:16 -0700481 /* Check if there are more sensors in the same interface */
482 int i = 1;
483 std::vector<std::string> psuNames;
484 do
485 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700486 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700487 psuNames.push_back(
488 escapeName(std::get<std::string>(findPSUName->second)));
Vijay Khemka996bad12019-05-28 15:15:16 -0700489 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
490 } while (findPSUName != baseConfig->second.end());
491
Cheng C Yange50345b2019-04-02 17:26:15 +0800492 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700493 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800494 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800495 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800496 continue;
497 }
498
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530499 /* read max value in sysfs for in, curr, power, temp, ... */
500 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
501 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700502 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530503 {
504 std::cerr << "No max name in PSU \n";
505 }
506 }
507
Lei YU7170a232021-02-04 16:19:27 +0800508 /* The poll rate for the sensors */
509 double pollRate = 0.0;
510 auto pollRateObj = baseConfig->second.find("PollRate");
511
512 if (pollRateObj != baseConfig->second.end())
513 {
514 pollRate =
515 std::visit(VariantToDoubleVisitor(), pollRateObj->second);
516 if (pollRate <= 0.0)
517 {
518 pollRate = PSUSensor::defaultSensorPoll;
519 }
520 }
521
Vijay Khemka996bad12019-05-28 15:15:16 -0700522 /* Find array of labels to be exposed if it is defined in config */
523 std::vector<std::string> findLabels;
524 auto findLabelObj = baseConfig->second.find("Labels");
525 if (findLabelObj != baseConfig->second.end())
526 {
527 findLabels =
528 std::get<std::vector<std::string>>(findLabelObj->second);
529 }
530
Jason Ling5747fab2019-10-02 16:46:23 -0700531 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
532 std::smatch matches;
533
Cheng C Yange50345b2019-04-02 17:26:15 +0800534 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800535 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530536 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800537 std::string labelHead;
538 std::string sensorPathStr = sensorPath.string();
539 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700540 std::string sensorNameSubStr{""};
541 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
542 {
Josh Lehan06494452019-10-31 09:49:16 -0700543 // hwmon *_input filename without number:
544 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700545 sensorNameSubStr = matches[1];
546 }
547 else
548 {
Josh Lehan06494452019-10-31 09:49:16 -0700549 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700550 << sensorNameStr;
551 continue;
552 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800553
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530554 std::string labelPath;
555
556 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700557 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530558 if (pos != std::string::npos)
559 {
560
561 std::string sensorPathStrMax = sensorPathStr.substr(pos);
562 if (sensorPathStrMax.compare("_max") == 0)
563 {
564 labelPath =
565 boost::replace_all_copy(sensorPathStr, "max", "label");
566 maxLabel = true;
567 }
568 else
569 {
570 labelPath = boost::replace_all_copy(sensorPathStr, "input",
571 "label");
572 maxLabel = false;
573 }
574 }
575 else
576 {
577 continue;
578 }
579
Cheng C Yangecba9de2019-09-12 23:41:50 +0800580 std::ifstream labelFile(labelPath);
581 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800582 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700583 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800584 {
585 std::cerr << "Input file " << sensorPath
586 << " has no corresponding label file\n";
587 }
Josh Lehan06494452019-10-31 09:49:16 -0700588 // hwmon *_input filename with number:
589 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700590 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800591 }
592 else
593 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800594 std::string label;
595 std::getline(labelFile, label);
596 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800597 auto findSensor = sensors.find(label);
598 if (findSensor != sensors.end())
599 {
600 continue;
601 }
602
Josh Lehan06494452019-10-31 09:49:16 -0700603 // hwmon corresponding *_label file contents:
604 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700605 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800606 }
607
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530608 /* append "max" for labelMatch */
609 if (maxLabel)
610 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700611 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530612 }
613
Ed Tanous8a57ec02020-10-09 12:46:52 -0700614 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700615 {
616 std::cerr << "Sensor type=\"" << sensorNameSubStr
617 << "\" label=\"" << labelHead << "\"\n";
618 }
619
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530620 checkPWMSensor(sensorPath, labelHead, *interfacePath,
621 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800622
Vijay Khemka996bad12019-05-28 15:15:16 -0700623 if (!findLabels.empty())
624 {
625 /* Check if this labelHead is enabled in config file */
626 if (std::find(findLabels.begin(), findLabels.end(),
627 labelHead) == findLabels.end())
628 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700629 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800630 {
631 std::cerr << "could not find " << labelHead
632 << " in the Labels list\n";
633 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700634 continue;
635 }
636 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800637
Cheng C Yange50345b2019-04-02 17:26:15 +0800638 auto findProperty = labelMatch.find(labelHead);
639 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800640 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700641 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700642 {
643 std::cerr << "Could not find matching default property for "
644 << labelHead << "\n";
645 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800646 continue;
647 }
648
Josh Lehan74d9bd92019-10-31 08:51:58 -0700649 // Protect the hardcoded labelMatch list from changes,
650 // by making a copy and modifying that instead.
651 // Avoid bleedthrough of one device's customizations to
652 // the next device, as each should be independently customizable.
653 psuProperties.push_back(findProperty->second);
654 auto psuProperty = psuProperties.rbegin();
655
656 // Use label head as prefix for reading from config file,
657 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
658 std::string keyName = labelHead + "_Name";
659 std::string keyScale = labelHead + "_Scale";
660 std::string keyMin = labelHead + "_Min";
661 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800662 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800663 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700664
665 bool customizedName = false;
666 auto findCustomName = baseConfig->second.find(keyName);
667 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700668 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700669 try
670 {
671 psuProperty->labelTypeName = std::visit(
672 VariantToStringVisitor(), findCustomName->second);
673 }
Patrick Williams26601e82021-10-06 12:43:25 -0500674 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700675 {
676 std::cerr << "Unable to parse " << keyName << "\n";
677 continue;
678 }
679
680 // All strings are valid, including empty string
681 customizedName = true;
682 }
683
684 bool customizedScale = false;
685 auto findCustomScale = baseConfig->second.find(keyScale);
686 if (findCustomScale != baseConfig->second.end())
687 {
688 try
689 {
690 psuProperty->sensorScaleFactor = std::visit(
691 VariantToUnsignedIntVisitor(), findCustomScale->second);
692 }
Patrick Williams26601e82021-10-06 12:43:25 -0500693 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700694 {
695 std::cerr << "Unable to parse " << keyScale << "\n";
696 continue;
697 }
698
699 // Avoid later division by zero
700 if (psuProperty->sensorScaleFactor > 0)
701 {
702 customizedScale = true;
703 }
704 else
705 {
706 std::cerr << "Unable to accept " << keyScale << "\n";
707 continue;
708 }
709 }
710
711 auto findCustomMin = baseConfig->second.find(keyMin);
712 if (findCustomMin != baseConfig->second.end())
713 {
714 try
715 {
716 psuProperty->minReading = std::visit(
717 VariantToDoubleVisitor(), findCustomMin->second);
718 }
Patrick Williams26601e82021-10-06 12:43:25 -0500719 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700720 {
721 std::cerr << "Unable to parse " << keyMin << "\n";
722 continue;
723 }
724 }
725
726 auto findCustomMax = baseConfig->second.find(keyMax);
727 if (findCustomMax != baseConfig->second.end())
728 {
729 try
730 {
731 psuProperty->maxReading = std::visit(
732 VariantToDoubleVisitor(), findCustomMax->second);
733 }
Patrick Williams26601e82021-10-06 12:43:25 -0500734 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700735 {
736 std::cerr << "Unable to parse " << keyMax << "\n";
737 continue;
738 }
739 }
740
Jeff Line41d52f2021-04-07 19:38:51 +0800741 auto findCustomOffset = baseConfig->second.find(keyOffset);
742 if (findCustomOffset != baseConfig->second.end())
743 {
744 try
745 {
746 psuProperty->sensorOffset = std::visit(
747 VariantToDoubleVisitor(), findCustomOffset->second);
748 }
Patrick Williams26601e82021-10-06 12:43:25 -0500749 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800750 {
751 std::cerr << "Unable to parse " << keyOffset << "\n";
752 continue;
753 }
754 }
755
Lotus Xucb5af732021-09-10 15:18:50 +0800756 // if we find label head power state set ,override the powerstate.
757 auto findPowerState = baseConfig->second.find(keyPowerState);
758 if (findPowerState != baseConfig->second.end())
759 {
760 std::string powerState = std::visit(VariantToStringVisitor(),
761 findPowerState->second);
762 setReadState(powerState, readState);
763 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700764 if (!(psuProperty->minReading < psuProperty->maxReading))
765 {
766 std::cerr << "Min must be less than Max\n";
767 continue;
768 }
769
770 // If the sensor name is being customized by config file,
771 // then prefix/suffix composition becomes not necessary,
772 // and in fact not wanted, because it gets in the way.
773 std::string psuNameFromIndex;
774 if (!customizedName)
775 {
776 /* Find out sensor name index for this label */
777 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500778 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700779 if (std::regex_search(labelHead, matches, rgx))
780 {
781 nameIndex = std::stoi(matches[1]);
782
783 // Decrement to preserve alignment, because hwmon
784 // human-readable filenames and labels use 1-based
785 // numbering, but the "Name", "Name1", "Name2", etc. naming
786 // convention (the psuNames vector) uses 0-based numbering.
787 if (nameIndex > 0)
788 {
789 --nameIndex;
790 }
791 }
792 else
793 {
794 nameIndex = 0;
795 }
796
797 if (psuNames.size() <= nameIndex)
798 {
799 std::cerr << "Could not pair " << labelHead
800 << " with a Name field\n";
801 continue;
802 }
803
804 psuNameFromIndex = psuNames[nameIndex];
805
Ed Tanous8a57ec02020-10-09 12:46:52 -0700806 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700807 {
808 std::cerr << "Sensor label head " << labelHead
809 << " paired with " << psuNameFromIndex
810 << " at index " << nameIndex << "\n";
811 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700812 }
813
Cheng C Yang58b2b532019-05-31 00:19:45 +0800814 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
815
Josh Lehan74d9bd92019-10-31 08:51:58 -0700816 // Similarly, if sensor scaling factor is being customized,
817 // then the below power-of-10 constraint becomes unnecessary,
818 // as config should be able to specify an arbitrary divisor.
819 unsigned int factor = psuProperty->sensorScaleFactor;
820 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700821 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700822 // Preserve existing usage of hardcoded labelMatch table below
823 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700824
Josh Lehan74d9bd92019-10-31 08:51:58 -0700825 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700826 char firstChar =
827 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700828 std::string strScaleFactor =
829 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
830
831 // Preserve existing configs by accepting earlier syntax,
832 // example CurrScaleFactor, PowerScaleFactor, ...
833 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
834 if (findScaleFactor != baseConfig->second.end())
835 {
836 factor = std::visit(VariantToIntVisitor(),
837 findScaleFactor->second);
838 }
839
Ed Tanous8a57ec02020-10-09 12:46:52 -0700840 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700841 {
842 std::cerr << "Sensor scaling factor " << factor
843 << " string " << strScaleFactor << "\n";
844 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700845 }
846
Vijay Khemka996bad12019-05-28 15:15:16 -0700847 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530848 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
849 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800850 {
James Feist17ab6e02019-06-25 12:28:13 -0700851 std::cerr << "error populating thresholds for "
852 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800853 }
854
Zev Weiss6b6891c2021-04-22 02:46:21 -0500855 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
856 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800857 {
Jason Ling5747fab2019-10-02 16:46:23 -0700858 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700859 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800860 continue;
861 }
862
Ed Tanous8a57ec02020-10-09 12:46:52 -0700863 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700864 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700865 std::cerr << "Sensor properties: Name \""
866 << psuProperty->labelTypeName << "\" Scale "
867 << psuProperty->sensorScaleFactor << " Min "
868 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800869 << psuProperty->maxReading << " Offset "
870 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700871 }
872
873 std::string sensorName = psuProperty->labelTypeName;
874 if (customizedName)
875 {
876 if (sensorName.empty())
877 {
878 // Allow selective disabling of an individual sensor,
879 // by customizing its name to an empty string.
880 std::cerr << "Sensor disabled, empty string\n";
881 continue;
882 }
883 }
884 else
885 {
886 // Sensor name not customized, do prefix/suffix composition,
887 // preserving default behavior by using psuNameFromIndex.
888 sensorName =
889 psuNameFromIndex + " " + psuProperty->labelTypeName;
890 }
891
Ed Tanous8a57ec02020-10-09 12:46:52 -0700892 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700893 {
894 std::cerr << "Sensor name \"" << sensorName << "\" path \""
895 << sensorPathStr << "\" type \"" << sensorType
896 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700897 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800898 // destruct existing one first if already created
899 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800900 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800901 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800902 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000903 readState, findSensorUnit->second, factor,
904 psuProperty->maxReading, psuProperty->minReading,
905 psuProperty->sensorOffset, labelHead, thresholdConfSize,
906 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800907 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700908 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700909 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700910 {
911 std::cerr << "Created " << numCreated << " sensors so far\n";
912 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800913 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800914
915 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800916 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800917 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000918 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
919 *psuName, readState,
920 eventPathList, groupEventPathList,
921 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800922 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700923
Ed Tanous8a57ec02020-10-09 12:46:52 -0700924 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700925 {
926 std::cerr << "Created total of " << numCreated << " sensors\n";
927 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800928 return;
929}
930
Zhikui Ren23c96e72020-11-05 22:32:28 -0800931void createSensors(
932 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
933 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
934 const std::shared_ptr<boost::container::flat_set<std::string>>&
935 sensorsChanged)
936{
937 auto getter = std::make_shared<GetSensorConfiguration>(
938 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
939 const ManagedObjectType& sensorConfigs) {
940 createSensorsCallback(io, objectServer, dbusConnection,
941 sensorConfigs, sensorsChanged);
942 });
943 getter->getConfiguration(
944 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
945}
946
Cheng C Yang916360b2019-05-07 18:47:16 +0800947void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800948{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500949 sensorTable = {{"power", sensor_paths::unitWatts},
950 {"curr", sensor_paths::unitAmperes},
951 {"temp", sensor_paths::unitDegreesC},
952 {"in", sensor_paths::unitVolts},
953 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800954
Jeff Line41d52f2021-04-07 19:38:51 +0800955 labelMatch = {
956 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
957 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
958 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
959 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
960 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
961 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
962 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
963 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
964 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
965 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
966 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
967 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
968 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
969 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
970 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
971 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
972 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
973 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
974 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
975 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
976 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
977 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
978 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
979 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
980 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
981 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
982 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
985 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
986 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
987 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
988 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
989 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
993 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
994 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
995 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
997 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
998 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
999 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1000 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1001 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1002 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1003 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1004 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1005 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1006 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1007 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1008 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1009 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1010 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1011 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1012 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1013 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
1014 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1015 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1016 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1017 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1018 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1019 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1020 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1021 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1022 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
1023 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001024
1025 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001026
1027 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1028 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1029
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001030 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1031 {"Failure", {"in2_alarm"}},
1032 {"ACLost", {"in1_beep"}},
1033 {"ConfigureError", {"in1_fault"}}};
1034
1035 groupEventMatch = {{"FanFault",
1036 {{"fan1", {"fan1_alarm", "fan1_fault"}},
1037 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001038}
1039
James Feistb6c0b912019-07-09 12:21:44 -07001040int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001041{
1042 boost::asio::io_service io;
1043 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1044
1045 systemBus->request_name("xyz.openbmc_project.PSUSensor");
1046 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001047 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -08001048 auto sensorsChanged =
1049 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001050
Cheng C Yang916360b2019-05-07 18:47:16 +08001051 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001052
Zhikui Ren23c96e72020-11-05 22:32:28 -08001053 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +08001054 boost::asio::deadline_timer filterTimer(io);
1055 std::function<void(sdbusplus::message::message&)> eventHandler =
1056 [&](sdbusplus::message::message& message) {
1057 if (message.is_method_error())
1058 {
1059 std::cerr << "callback method error\n";
1060 return;
1061 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001062 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +08001063 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +08001064 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1065 if (ec == boost::asio::error::operation_aborted)
1066 {
1067 return;
1068 }
Ed Tanous8a57ec02020-10-09 12:46:52 -07001069 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +08001070 {
1071 std::cerr << "timer error\n";
1072 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001073 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +08001074 });
1075 };
1076
1077 for (const char* type : sensorTypes)
1078 {
1079 auto match = std::make_unique<sdbusplus::bus::match::match>(
1080 static_cast<sdbusplus::bus::bus&>(*systemBus),
1081 "type='signal',member='PropertiesChanged',path_namespace='" +
1082 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
1083 eventHandler);
1084 matches.emplace_back(std::move(match));
1085 }
Bruce Lee1263c3d2021-06-04 15:16:33 +08001086
1087 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001088 io.run();
1089}