blob: ae7afc37f2076045dd77480bf84d36f94c6cf448 [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",
55 "xyz.openbmc_project.Configuration.ISL69243",
56 "xyz.openbmc_project.Configuration.ISL69260",
57 "xyz.openbmc_project.Configuration.LM25066",
58 "xyz.openbmc_project.Configuration.MAX16601",
59 "xyz.openbmc_project.Configuration.MAX20710",
60 "xyz.openbmc_project.Configuration.MAX20730",
61 "xyz.openbmc_project.Configuration.MAX20734",
62 "xyz.openbmc_project.Configuration.MAX20796",
63 "xyz.openbmc_project.Configuration.MAX34451",
Howard Chiub58ac3a2021-12-07 17:12:56 +080064 "xyz.openbmc_project.Configuration.MP5023",
Brandon Kim66558232021-11-09 16:53:08 -080065 "xyz.openbmc_project.Configuration.pmbus",
66 "xyz.openbmc_project.Configuration.PXE1610",
67 "xyz.openbmc_project.Configuration.RAA228000",
68 "xyz.openbmc_project.Configuration.RAA228228",
69 "xyz.openbmc_project.Configuration.RAA229004",
70 "xyz.openbmc_project.Configuration.TPS546D24",
71 "xyz.openbmc_project.Configuration.XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080072
Ed Tanousa2df7862021-12-07 16:30:27 -080073// clang-format off
74static constexpr auto pmbusNames{std::to_array<const char*>({
75 "adm1266",
76 "adm1272",
77 "adm1275",
78 "adm1278",
79 "dps800",
80 "ina219",
81 "ina230",
82 "ipsps1",
83 "isl68137",
84 "isl68220",
85 "isl68223",
86 "isl69243",
87 "isl69260",
88 "lm25066",
89 "max16601",
90 "max20710",
91 "max20730",
92 "max20734",
93 "max20796",
94 "max34451",
Howard Chiub58ac3a2021-12-07 17:12:56 +080095 "mp5023",
Ed Tanousa2df7862021-12-07 16:30:27 -080096 "pmbus",
97 "pxe1610",
98 "raa228000",
99 "raa228228",
100 "raa229004",
101 "tps546d24",
102 "xdpe12284"
103})};
104//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700105
Cheng C Yang209ec562019-03-12 16:37:44 +0800106namespace fs = std::filesystem;
107
Yong Libf8b1da2020-04-15 16:32:50 +0800108static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800109 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800110static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
111 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800112static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
113 pwmSensors;
114static boost::container::flat_map<std::string, std::string> sensorTable;
115static boost::container::flat_map<std::string, PSUProperty> labelMatch;
116static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800117static boost::container::flat_map<std::string, std::vector<std::string>>
118 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800119static boost::container::flat_map<
120 std::string,
121 boost::container::flat_map<std::string, std::vector<std::string>>>
122 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800123static boost::container::flat_map<std::string, std::vector<std::string>>
124 limitEventMatch;
125
Josh Lehan74d9bd92019-10-31 08:51:58 -0700126static std::vector<PSUProperty> psuProperties;
127
Cheng C Yang58b2b532019-05-31 00:19:45 +0800128// Function CheckEvent will check each attribute from eventMatch table in the
129// sysfs. If the attributes exists in sysfs, then store the complete path
130// of the attribute into eventPathList.
131void checkEvent(
132 const std::string& directory,
133 const boost::container::flat_map<std::string, std::vector<std::string>>&
134 eventMatch,
135 boost::container::flat_map<std::string, std::vector<std::string>>&
136 eventPathList)
137{
138 for (const auto& match : eventMatch)
139 {
140 const std::vector<std::string>& eventAttrs = match.second;
141 const std::string& eventName = match.first;
142 for (const auto& eventAttr : eventAttrs)
143 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700144 std::string eventPath = directory;
145 eventPath += "/";
146 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800147
148 std::ifstream eventFile(eventPath);
149 if (!eventFile.good())
150 {
151 continue;
152 }
153
154 eventPathList[eventName].push_back(eventPath);
155 }
156 }
157}
158
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800159// Check Group Events which contains more than one targets in each combine
160// events.
161void checkGroupEvent(
162 const std::string& directory,
163 const boost::container::flat_map<
164 std::string,
165 boost::container::flat_map<std::string, std::vector<std::string>>>&
166 groupEventMatch,
167 boost::container::flat_map<
168 std::string,
169 boost::container::flat_map<std::string, std::vector<std::string>>>&
170 groupEventPathList)
171{
172 for (const auto& match : groupEventMatch)
173 {
174 const std::string& groupEventName = match.first;
175 const boost::container::flat_map<std::string, std::vector<std::string>>
176 events = match.second;
177 boost::container::flat_map<std::string, std::vector<std::string>>
178 pathList;
179 for (const auto& match : events)
180 {
181 const std::string& eventName = match.first;
182 const std::vector<std::string>& eventAttrs = match.second;
183 for (const auto& eventAttr : eventAttrs)
184 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700185 std::string eventPath = directory;
186 eventPath += "/";
187 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800188 std::ifstream eventFile(eventPath);
189 if (!eventFile.good())
190 {
191 continue;
192 }
193
194 pathList[eventName].push_back(eventPath);
195 }
196 }
197 groupEventPathList[groupEventName] = pathList;
198 }
199}
200
Cheng C Yang58b2b532019-05-31 00:19:45 +0800201// Function checkEventLimits will check all the psu related xxx_input attributes
202// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
203// xxx_min_alarm exist, then store the existing paths of the alarm attributes
204// to eventPathList.
205void checkEventLimits(
206 const std::string& sensorPathStr,
207 const boost::container::flat_map<std::string, std::vector<std::string>>&
208 limitEventMatch,
209 boost::container::flat_map<std::string, std::vector<std::string>>&
210 eventPathList)
211{
Lei YUa2c7cea2020-12-23 14:07:28 +0800212 auto attributePartPos = sensorPathStr.find_last_of('_');
213 if (attributePartPos == std::string::npos)
214 {
215 // There is no '_' in the string, skip it
216 return;
217 }
218 auto attributePart =
219 std::string_view(sensorPathStr).substr(attributePartPos + 1);
220 if (attributePart != "input")
221 {
222 // If the sensor is not xxx_input, skip it
223 return;
224 }
225
226 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800227 for (const auto& limitMatch : limitEventMatch)
228 {
229 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
230 const std::string& eventName = limitMatch.first;
231 for (const auto& limitEventAttr : limitEventAttrs)
232 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800233 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800234 std::ifstream eventFile(limitEventPath);
235 if (!eventFile.good())
236 {
237 continue;
238 }
239 eventPathList[eventName].push_back(limitEventPath);
240 }
241 }
242}
Cheng C Yang916360b2019-05-07 18:47:16 +0800243
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530244static void
245 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
246 const std::string& interfacePath,
247 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
248 sdbusplus::asio::object_server& objectServer,
249 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800250{
251 for (const auto& pwmName : pwmTable)
252 {
253 if (pwmName.first != labelHead)
254 {
255 continue;
256 }
257
258 const std::string& sensorPathStr = sensorPath.string();
259 const std::string& pwmPathStr =
260 boost::replace_all_copy(sensorPathStr, "input", "target");
261 std::ifstream pwmFile(pwmPathStr);
262 if (!pwmFile.good())
263 {
264 continue;
265 }
266
267 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
268 if (findPWMSensor != pwmSensors.end())
269 {
270 continue;
271 }
272
273 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530274 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
275 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800276 }
277}
278
Zhikui Ren23c96e72020-11-05 22:32:28 -0800279static void createSensorsCallback(
280 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
281 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
282 const ManagedObjectType& sensorConfigs,
283 const std::shared_ptr<boost::container::flat_set<std::string>>&
284 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800285{
Josh Lehan49cfba92019-10-08 16:50:42 -0700286 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800287 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800288
289 std::vector<fs::path> pmbusPaths;
290 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
291 {
292 std::cerr << "No PSU sensors in system\n";
293 return;
294 }
295
296 boost::container::flat_set<std::string> directories;
297 for (const auto& pmbusPath : pmbusPaths)
298 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800299 boost::container::flat_map<std::string, std::vector<std::string>>
300 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800301 boost::container::flat_map<
302 std::string,
303 boost::container::flat_map<std::string, std::vector<std::string>>>
304 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800305
306 std::ifstream nameFile(pmbusPath);
307 if (!nameFile.good())
308 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700309 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800310 continue;
311 }
312
313 std::string pmbusName;
314 std::getline(nameFile, pmbusName);
315 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700316
317 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
318 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800319 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700320 // To avoid this error message, add your driver name to
321 // the pmbusNames vector at the top of this file.
322 std::cerr << "Driver name " << pmbusName
323 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800324 continue;
325 }
326
327 const std::string* psuName;
Cheng C Yang209ec562019-03-12 16:37:44 +0800328 auto directory = pmbusPath.parent_path();
329
330 auto ret = directories.insert(directory.string());
331 if (!ret.second)
332 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700333 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800334 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800335 }
336
James Feistb6c0b912019-07-09 12:21:44 -0700337 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800338 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700339 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800340 if (findHyphen == std::string::npos)
341 {
342 std::cerr << "found bad device" << deviceName << "\n";
343 continue;
344 }
345 std::string busStr = deviceName.substr(0, findHyphen);
346 std::string addrStr = deviceName.substr(findHyphen + 1);
347
348 size_t bus = 0;
349 size_t addr = 0;
350
351 try
352 {
353 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700354 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800355 }
Patrick Williams26601e82021-10-06 12:43:25 -0500356 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800357 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700358 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
359 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800360 continue;
361 }
362
Cheng C Yang209ec562019-03-12 16:37:44 +0800363 const std::pair<std::string, boost::container::flat_map<
364 std::string, BasicVariantType>>*
365 baseConfig = nullptr;
366 const SensorData* sensorData = nullptr;
367 const std::string* interfacePath = nullptr;
368 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800369 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800370
371 for (const std::pair<sdbusplus::message::object_path, SensorData>&
372 sensor : sensorConfigs)
373 {
374 sensorData = &(sensor.second);
375 for (const char* type : sensorTypes)
376 {
377 auto sensorBase = sensorData->find(type);
378 if (sensorBase != sensorData->end())
379 {
380 baseConfig = &(*sensorBase);
381 sensorType = type;
382 break;
383 }
384 }
385 if (baseConfig == nullptr)
386 {
387 std::cerr << "error finding base configuration for "
388 << deviceName << "\n";
389 continue;
390 }
391
392 auto configBus = baseConfig->second.find("Bus");
393 auto configAddress = baseConfig->second.find("Address");
394
395 if (configBus == baseConfig->second.end() ||
396 configAddress == baseConfig->second.end())
397 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800398 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800399 continue;
400 }
401
Cheng C Yang58b2b532019-05-31 00:19:45 +0800402 const uint64_t* confBus;
403 const uint64_t* confAddr;
404 if (!(confBus = std::get_if<uint64_t>(&(configBus->second))) ||
405 !(confAddr = std::get_if<uint64_t>(&(configAddress->second))))
406 {
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 }
445
Cheng C Yang58b2b532019-05-31 00:19:45 +0800446 if (!(psuName = std::get_if<std::string>(&(findPSUName->second))))
447 {
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}