blob: d1d385cdb6f4a6dcf24c14b07d167b1665c92c0a [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",
64 "xyz.openbmc_project.Configuration.pmbus",
65 "xyz.openbmc_project.Configuration.PXE1610",
66 "xyz.openbmc_project.Configuration.RAA228000",
67 "xyz.openbmc_project.Configuration.RAA228228",
68 "xyz.openbmc_project.Configuration.RAA229004",
69 "xyz.openbmc_project.Configuration.TPS546D24",
70 "xyz.openbmc_project.Configuration.XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080071
Ed Tanousa2df7862021-12-07 16:30:27 -080072// clang-format off
73static constexpr auto pmbusNames{std::to_array<const char*>({
74 "adm1266",
75 "adm1272",
76 "adm1275",
77 "adm1278",
78 "dps800",
79 "ina219",
80 "ina230",
81 "ipsps1",
82 "isl68137",
83 "isl68220",
84 "isl68223",
85 "isl69243",
86 "isl69260",
87 "lm25066",
88 "max16601",
89 "max20710",
90 "max20730",
91 "max20734",
92 "max20796",
93 "max34451",
94 "pmbus",
95 "pxe1610",
96 "raa228000",
97 "raa228228",
98 "raa229004",
99 "tps546d24",
100 "xdpe12284"
101})};
102//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700103
Cheng C Yang209ec562019-03-12 16:37:44 +0800104namespace fs = std::filesystem;
105
Yong Libf8b1da2020-04-15 16:32:50 +0800106static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800107 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800108static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
109 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800110static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
111 pwmSensors;
112static boost::container::flat_map<std::string, std::string> sensorTable;
113static boost::container::flat_map<std::string, PSUProperty> labelMatch;
114static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800115static boost::container::flat_map<std::string, std::vector<std::string>>
116 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800117static boost::container::flat_map<
118 std::string,
119 boost::container::flat_map<std::string, std::vector<std::string>>>
120 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800121static boost::container::flat_map<std::string, std::vector<std::string>>
122 limitEventMatch;
123
Josh Lehan74d9bd92019-10-31 08:51:58 -0700124static std::vector<PSUProperty> psuProperties;
125
Cheng C Yang58b2b532019-05-31 00:19:45 +0800126// Function CheckEvent will check each attribute from eventMatch table in the
127// sysfs. If the attributes exists in sysfs, then store the complete path
128// of the attribute into eventPathList.
129void checkEvent(
130 const std::string& directory,
131 const boost::container::flat_map<std::string, std::vector<std::string>>&
132 eventMatch,
133 boost::container::flat_map<std::string, std::vector<std::string>>&
134 eventPathList)
135{
136 for (const auto& match : eventMatch)
137 {
138 const std::vector<std::string>& eventAttrs = match.second;
139 const std::string& eventName = match.first;
140 for (const auto& eventAttr : eventAttrs)
141 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700142 std::string eventPath = directory;
143 eventPath += "/";
144 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800145
146 std::ifstream eventFile(eventPath);
147 if (!eventFile.good())
148 {
149 continue;
150 }
151
152 eventPathList[eventName].push_back(eventPath);
153 }
154 }
155}
156
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800157// Check Group Events which contains more than one targets in each combine
158// events.
159void checkGroupEvent(
160 const std::string& directory,
161 const boost::container::flat_map<
162 std::string,
163 boost::container::flat_map<std::string, std::vector<std::string>>>&
164 groupEventMatch,
165 boost::container::flat_map<
166 std::string,
167 boost::container::flat_map<std::string, std::vector<std::string>>>&
168 groupEventPathList)
169{
170 for (const auto& match : groupEventMatch)
171 {
172 const std::string& groupEventName = match.first;
173 const boost::container::flat_map<std::string, std::vector<std::string>>
174 events = match.second;
175 boost::container::flat_map<std::string, std::vector<std::string>>
176 pathList;
177 for (const auto& match : events)
178 {
179 const std::string& eventName = match.first;
180 const std::vector<std::string>& eventAttrs = match.second;
181 for (const auto& eventAttr : eventAttrs)
182 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700183 std::string eventPath = directory;
184 eventPath += "/";
185 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800186 std::ifstream eventFile(eventPath);
187 if (!eventFile.good())
188 {
189 continue;
190 }
191
192 pathList[eventName].push_back(eventPath);
193 }
194 }
195 groupEventPathList[groupEventName] = pathList;
196 }
197}
198
Cheng C Yang58b2b532019-05-31 00:19:45 +0800199// Function checkEventLimits will check all the psu related xxx_input attributes
200// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
201// xxx_min_alarm exist, then store the existing paths of the alarm attributes
202// to eventPathList.
203void checkEventLimits(
204 const std::string& sensorPathStr,
205 const boost::container::flat_map<std::string, std::vector<std::string>>&
206 limitEventMatch,
207 boost::container::flat_map<std::string, std::vector<std::string>>&
208 eventPathList)
209{
Lei YUa2c7cea2020-12-23 14:07:28 +0800210 auto attributePartPos = sensorPathStr.find_last_of('_');
211 if (attributePartPos == std::string::npos)
212 {
213 // There is no '_' in the string, skip it
214 return;
215 }
216 auto attributePart =
217 std::string_view(sensorPathStr).substr(attributePartPos + 1);
218 if (attributePart != "input")
219 {
220 // If the sensor is not xxx_input, skip it
221 return;
222 }
223
224 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800225 for (const auto& limitMatch : limitEventMatch)
226 {
227 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
228 const std::string& eventName = limitMatch.first;
229 for (const auto& limitEventAttr : limitEventAttrs)
230 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800231 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800232 std::ifstream eventFile(limitEventPath);
233 if (!eventFile.good())
234 {
235 continue;
236 }
237 eventPathList[eventName].push_back(limitEventPath);
238 }
239 }
240}
Cheng C Yang916360b2019-05-07 18:47:16 +0800241
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530242static void
243 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
244 const std::string& interfacePath,
245 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
246 sdbusplus::asio::object_server& objectServer,
247 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800248{
249 for (const auto& pwmName : pwmTable)
250 {
251 if (pwmName.first != labelHead)
252 {
253 continue;
254 }
255
256 const std::string& sensorPathStr = sensorPath.string();
257 const std::string& pwmPathStr =
258 boost::replace_all_copy(sensorPathStr, "input", "target");
259 std::ifstream pwmFile(pwmPathStr);
260 if (!pwmFile.good())
261 {
262 continue;
263 }
264
265 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
266 if (findPWMSensor != pwmSensors.end())
267 {
268 continue;
269 }
270
271 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530272 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
273 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800274 }
275}
276
Zhikui Ren23c96e72020-11-05 22:32:28 -0800277static void createSensorsCallback(
278 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
279 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
280 const ManagedObjectType& sensorConfigs,
281 const std::shared_ptr<boost::container::flat_set<std::string>>&
282 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800283{
Josh Lehan49cfba92019-10-08 16:50:42 -0700284 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800285 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800286
287 std::vector<fs::path> pmbusPaths;
288 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
289 {
290 std::cerr << "No PSU sensors in system\n";
291 return;
292 }
293
294 boost::container::flat_set<std::string> directories;
295 for (const auto& pmbusPath : pmbusPaths)
296 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800297 boost::container::flat_map<std::string, std::vector<std::string>>
298 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800299 boost::container::flat_map<
300 std::string,
301 boost::container::flat_map<std::string, std::vector<std::string>>>
302 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800303
304 std::ifstream nameFile(pmbusPath);
305 if (!nameFile.good())
306 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700307 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800308 continue;
309 }
310
311 std::string pmbusName;
312 std::getline(nameFile, pmbusName);
313 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700314
315 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
316 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800317 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700318 // To avoid this error message, add your driver name to
319 // the pmbusNames vector at the top of this file.
320 std::cerr << "Driver name " << pmbusName
321 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800322 continue;
323 }
324
325 const std::string* psuName;
Cheng C Yang209ec562019-03-12 16:37:44 +0800326 auto directory = pmbusPath.parent_path();
327
328 auto ret = directories.insert(directory.string());
329 if (!ret.second)
330 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700331 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800332 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800333 }
334
James Feistb6c0b912019-07-09 12:21:44 -0700335 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800336 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700337 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800338 if (findHyphen == std::string::npos)
339 {
340 std::cerr << "found bad device" << deviceName << "\n";
341 continue;
342 }
343 std::string busStr = deviceName.substr(0, findHyphen);
344 std::string addrStr = deviceName.substr(findHyphen + 1);
345
346 size_t bus = 0;
347 size_t addr = 0;
348
349 try
350 {
351 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700352 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800353 }
Patrick Williams26601e82021-10-06 12:43:25 -0500354 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800355 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700356 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
357 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800358 continue;
359 }
360
Cheng C Yang209ec562019-03-12 16:37:44 +0800361 const std::pair<std::string, boost::container::flat_map<
362 std::string, BasicVariantType>>*
363 baseConfig = nullptr;
364 const SensorData* sensorData = nullptr;
365 const std::string* interfacePath = nullptr;
366 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800367 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800368
369 for (const std::pair<sdbusplus::message::object_path, SensorData>&
370 sensor : sensorConfigs)
371 {
372 sensorData = &(sensor.second);
373 for (const char* type : sensorTypes)
374 {
375 auto sensorBase = sensorData->find(type);
376 if (sensorBase != sensorData->end())
377 {
378 baseConfig = &(*sensorBase);
379 sensorType = type;
380 break;
381 }
382 }
383 if (baseConfig == nullptr)
384 {
385 std::cerr << "error finding base configuration for "
386 << deviceName << "\n";
387 continue;
388 }
389
390 auto configBus = baseConfig->second.find("Bus");
391 auto configAddress = baseConfig->second.find("Address");
392
393 if (configBus == baseConfig->second.end() ||
394 configAddress == baseConfig->second.end())
395 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800396 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800397 continue;
398 }
399
Cheng C Yang58b2b532019-05-31 00:19:45 +0800400 const uint64_t* confBus;
401 const uint64_t* confAddr;
402 if (!(confBus = std::get_if<uint64_t>(&(configBus->second))) ||
403 !(confAddr = std::get_if<uint64_t>(&(configAddress->second))))
404 {
405 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700406 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800407 continue;
408 }
409
410 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800411 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700412 std::cerr << "Configuration skipping " << *confBus << "-"
413 << *confAddr << " because not " << bus << "-" << addr
414 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800415 continue;
416 }
417
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800418 std::vector<thresholds::Threshold> confThresholds;
419 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
420 {
421 std::cerr << "error populating totoal thresholds\n";
422 }
423 thresholdConfSize = confThresholds.size();
424
Cheng C Yang209ec562019-03-12 16:37:44 +0800425 interfacePath = &(sensor.first.str);
426 break;
427 }
428 if (interfacePath == nullptr)
429 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700430 // To avoid this error message, add your export map entry,
431 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800432 std::cerr << "failed to find match for " << deviceName << "\n";
433 continue;
434 }
435
Cheng C Yange50345b2019-04-02 17:26:15 +0800436 auto findPSUName = baseConfig->second.find("Name");
437 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800438 {
439 std::cerr << "could not determine configuration name for "
440 << deviceName << "\n";
441 continue;
442 }
443
Cheng C Yang58b2b532019-05-31 00:19:45 +0800444 if (!(psuName = std::get_if<std::string>(&(findPSUName->second))))
445 {
446 std::cerr << "Cannot find psu name, invalid configuration\n";
447 continue;
448 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800449
450 // on rescans, only update sensors we were signaled by
451 if (!firstScan)
452 {
Zhikui Renda98f092021-11-01 09:41:08 -0700453 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800454 auto it =
455 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
456 [psuNameStr](std::string& s) {
457 return boost::ends_with(s, psuNameStr);
458 });
459
460 if (it == sensorsChanged->end())
461 {
462 continue;
463 }
464 sensorsChanged->erase(it);
465 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800466 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800467 checkGroupEvent(directory.string(), groupEventMatch,
468 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800469
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000470 PowerState readState = PowerState::always;
471 auto findPowerOn = baseConfig->second.find("PowerState");
472 if (findPowerOn != baseConfig->second.end())
473 {
474 std::string powerState =
475 std::visit(VariantToStringVisitor(), findPowerOn->second);
476 setReadState(powerState, readState);
477 }
478
Vijay Khemka996bad12019-05-28 15:15:16 -0700479 /* Check if there are more sensors in the same interface */
480 int i = 1;
481 std::vector<std::string> psuNames;
482 do
483 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700484 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700485 psuNames.push_back(
486 escapeName(std::get<std::string>(findPSUName->second)));
Vijay Khemka996bad12019-05-28 15:15:16 -0700487 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
488 } while (findPSUName != baseConfig->second.end());
489
Cheng C Yange50345b2019-04-02 17:26:15 +0800490 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700491 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800492 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800493 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800494 continue;
495 }
496
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530497 /* read max value in sysfs for in, curr, power, temp, ... */
498 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
499 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700500 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530501 {
502 std::cerr << "No max name in PSU \n";
503 }
504 }
505
Lei YU7170a232021-02-04 16:19:27 +0800506 /* The poll rate for the sensors */
507 double pollRate = 0.0;
508 auto pollRateObj = baseConfig->second.find("PollRate");
509
510 if (pollRateObj != baseConfig->second.end())
511 {
512 pollRate =
513 std::visit(VariantToDoubleVisitor(), pollRateObj->second);
514 if (pollRate <= 0.0)
515 {
516 pollRate = PSUSensor::defaultSensorPoll;
517 }
518 }
519
Vijay Khemka996bad12019-05-28 15:15:16 -0700520 /* Find array of labels to be exposed if it is defined in config */
521 std::vector<std::string> findLabels;
522 auto findLabelObj = baseConfig->second.find("Labels");
523 if (findLabelObj != baseConfig->second.end())
524 {
525 findLabels =
526 std::get<std::vector<std::string>>(findLabelObj->second);
527 }
528
Jason Ling5747fab2019-10-02 16:46:23 -0700529 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
530 std::smatch matches;
531
Cheng C Yange50345b2019-04-02 17:26:15 +0800532 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800533 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530534 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800535 std::string labelHead;
536 std::string sensorPathStr = sensorPath.string();
537 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700538 std::string sensorNameSubStr{""};
539 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
540 {
Josh Lehan06494452019-10-31 09:49:16 -0700541 // hwmon *_input filename without number:
542 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700543 sensorNameSubStr = matches[1];
544 }
545 else
546 {
Josh Lehan06494452019-10-31 09:49:16 -0700547 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700548 << sensorNameStr;
549 continue;
550 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800551
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530552 std::string labelPath;
553
554 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700555 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530556 if (pos != std::string::npos)
557 {
558
559 std::string sensorPathStrMax = sensorPathStr.substr(pos);
560 if (sensorPathStrMax.compare("_max") == 0)
561 {
562 labelPath =
563 boost::replace_all_copy(sensorPathStr, "max", "label");
564 maxLabel = true;
565 }
566 else
567 {
568 labelPath = boost::replace_all_copy(sensorPathStr, "input",
569 "label");
570 maxLabel = false;
571 }
572 }
573 else
574 {
575 continue;
576 }
577
Cheng C Yangecba9de2019-09-12 23:41:50 +0800578 std::ifstream labelFile(labelPath);
579 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800580 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700581 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800582 {
583 std::cerr << "Input file " << sensorPath
584 << " has no corresponding label file\n";
585 }
Josh Lehan06494452019-10-31 09:49:16 -0700586 // hwmon *_input filename with number:
587 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700588 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800589 }
590 else
591 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800592 std::string label;
593 std::getline(labelFile, label);
594 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800595 auto findSensor = sensors.find(label);
596 if (findSensor != sensors.end())
597 {
598 continue;
599 }
600
Josh Lehan06494452019-10-31 09:49:16 -0700601 // hwmon corresponding *_label file contents:
602 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700603 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800604 }
605
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530606 /* append "max" for labelMatch */
607 if (maxLabel)
608 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700609 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530610 }
611
Ed Tanous8a57ec02020-10-09 12:46:52 -0700612 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700613 {
614 std::cerr << "Sensor type=\"" << sensorNameSubStr
615 << "\" label=\"" << labelHead << "\"\n";
616 }
617
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530618 checkPWMSensor(sensorPath, labelHead, *interfacePath,
619 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800620
Vijay Khemka996bad12019-05-28 15:15:16 -0700621 if (!findLabels.empty())
622 {
623 /* Check if this labelHead is enabled in config file */
624 if (std::find(findLabels.begin(), findLabels.end(),
625 labelHead) == findLabels.end())
626 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700627 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800628 {
629 std::cerr << "could not find " << labelHead
630 << " in the Labels list\n";
631 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700632 continue;
633 }
634 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800635
Cheng C Yange50345b2019-04-02 17:26:15 +0800636 auto findProperty = labelMatch.find(labelHead);
637 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800638 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700639 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700640 {
641 std::cerr << "Could not find matching default property for "
642 << labelHead << "\n";
643 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800644 continue;
645 }
646
Josh Lehan74d9bd92019-10-31 08:51:58 -0700647 // Protect the hardcoded labelMatch list from changes,
648 // by making a copy and modifying that instead.
649 // Avoid bleedthrough of one device's customizations to
650 // the next device, as each should be independently customizable.
651 psuProperties.push_back(findProperty->second);
652 auto psuProperty = psuProperties.rbegin();
653
654 // Use label head as prefix for reading from config file,
655 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
656 std::string keyName = labelHead + "_Name";
657 std::string keyScale = labelHead + "_Scale";
658 std::string keyMin = labelHead + "_Min";
659 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800660 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800661 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700662
663 bool customizedName = false;
664 auto findCustomName = baseConfig->second.find(keyName);
665 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700666 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700667 try
668 {
669 psuProperty->labelTypeName = std::visit(
670 VariantToStringVisitor(), findCustomName->second);
671 }
Patrick Williams26601e82021-10-06 12:43:25 -0500672 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700673 {
674 std::cerr << "Unable to parse " << keyName << "\n";
675 continue;
676 }
677
678 // All strings are valid, including empty string
679 customizedName = true;
680 }
681
682 bool customizedScale = false;
683 auto findCustomScale = baseConfig->second.find(keyScale);
684 if (findCustomScale != baseConfig->second.end())
685 {
686 try
687 {
688 psuProperty->sensorScaleFactor = std::visit(
689 VariantToUnsignedIntVisitor(), findCustomScale->second);
690 }
Patrick Williams26601e82021-10-06 12:43:25 -0500691 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700692 {
693 std::cerr << "Unable to parse " << keyScale << "\n";
694 continue;
695 }
696
697 // Avoid later division by zero
698 if (psuProperty->sensorScaleFactor > 0)
699 {
700 customizedScale = true;
701 }
702 else
703 {
704 std::cerr << "Unable to accept " << keyScale << "\n";
705 continue;
706 }
707 }
708
709 auto findCustomMin = baseConfig->second.find(keyMin);
710 if (findCustomMin != baseConfig->second.end())
711 {
712 try
713 {
714 psuProperty->minReading = std::visit(
715 VariantToDoubleVisitor(), findCustomMin->second);
716 }
Patrick Williams26601e82021-10-06 12:43:25 -0500717 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700718 {
719 std::cerr << "Unable to parse " << keyMin << "\n";
720 continue;
721 }
722 }
723
724 auto findCustomMax = baseConfig->second.find(keyMax);
725 if (findCustomMax != baseConfig->second.end())
726 {
727 try
728 {
729 psuProperty->maxReading = std::visit(
730 VariantToDoubleVisitor(), findCustomMax->second);
731 }
Patrick Williams26601e82021-10-06 12:43:25 -0500732 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700733 {
734 std::cerr << "Unable to parse " << keyMax << "\n";
735 continue;
736 }
737 }
738
Jeff Line41d52f2021-04-07 19:38:51 +0800739 auto findCustomOffset = baseConfig->second.find(keyOffset);
740 if (findCustomOffset != baseConfig->second.end())
741 {
742 try
743 {
744 psuProperty->sensorOffset = std::visit(
745 VariantToDoubleVisitor(), findCustomOffset->second);
746 }
Patrick Williams26601e82021-10-06 12:43:25 -0500747 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800748 {
749 std::cerr << "Unable to parse " << keyOffset << "\n";
750 continue;
751 }
752 }
753
Lotus Xucb5af732021-09-10 15:18:50 +0800754 // if we find label head power state set ,override the powerstate.
755 auto findPowerState = baseConfig->second.find(keyPowerState);
756 if (findPowerState != baseConfig->second.end())
757 {
758 std::string powerState = std::visit(VariantToStringVisitor(),
759 findPowerState->second);
760 setReadState(powerState, readState);
761 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700762 if (!(psuProperty->minReading < psuProperty->maxReading))
763 {
764 std::cerr << "Min must be less than Max\n";
765 continue;
766 }
767
768 // If the sensor name is being customized by config file,
769 // then prefix/suffix composition becomes not necessary,
770 // and in fact not wanted, because it gets in the way.
771 std::string psuNameFromIndex;
772 if (!customizedName)
773 {
774 /* Find out sensor name index for this label */
775 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500776 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700777 if (std::regex_search(labelHead, matches, rgx))
778 {
779 nameIndex = std::stoi(matches[1]);
780
781 // Decrement to preserve alignment, because hwmon
782 // human-readable filenames and labels use 1-based
783 // numbering, but the "Name", "Name1", "Name2", etc. naming
784 // convention (the psuNames vector) uses 0-based numbering.
785 if (nameIndex > 0)
786 {
787 --nameIndex;
788 }
789 }
790 else
791 {
792 nameIndex = 0;
793 }
794
795 if (psuNames.size() <= nameIndex)
796 {
797 std::cerr << "Could not pair " << labelHead
798 << " with a Name field\n";
799 continue;
800 }
801
802 psuNameFromIndex = psuNames[nameIndex];
803
Ed Tanous8a57ec02020-10-09 12:46:52 -0700804 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700805 {
806 std::cerr << "Sensor label head " << labelHead
807 << " paired with " << psuNameFromIndex
808 << " at index " << nameIndex << "\n";
809 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700810 }
811
Cheng C Yang58b2b532019-05-31 00:19:45 +0800812 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
813
Josh Lehan74d9bd92019-10-31 08:51:58 -0700814 // Similarly, if sensor scaling factor is being customized,
815 // then the below power-of-10 constraint becomes unnecessary,
816 // as config should be able to specify an arbitrary divisor.
817 unsigned int factor = psuProperty->sensorScaleFactor;
818 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700819 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700820 // Preserve existing usage of hardcoded labelMatch table below
821 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700822
Josh Lehan74d9bd92019-10-31 08:51:58 -0700823 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700824 char firstChar =
825 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700826 std::string strScaleFactor =
827 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
828
829 // Preserve existing configs by accepting earlier syntax,
830 // example CurrScaleFactor, PowerScaleFactor, ...
831 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
832 if (findScaleFactor != baseConfig->second.end())
833 {
834 factor = std::visit(VariantToIntVisitor(),
835 findScaleFactor->second);
836 }
837
Ed Tanous8a57ec02020-10-09 12:46:52 -0700838 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700839 {
840 std::cerr << "Sensor scaling factor " << factor
841 << " string " << strScaleFactor << "\n";
842 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700843 }
844
Vijay Khemka996bad12019-05-28 15:15:16 -0700845 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530846 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
847 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800848 {
James Feist17ab6e02019-06-25 12:28:13 -0700849 std::cerr << "error populating thresholds for "
850 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800851 }
852
Zev Weiss6b6891c2021-04-22 02:46:21 -0500853 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
854 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800855 {
Jason Ling5747fab2019-10-02 16:46:23 -0700856 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700857 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800858 continue;
859 }
860
Ed Tanous8a57ec02020-10-09 12:46:52 -0700861 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700862 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700863 std::cerr << "Sensor properties: Name \""
864 << psuProperty->labelTypeName << "\" Scale "
865 << psuProperty->sensorScaleFactor << " Min "
866 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800867 << psuProperty->maxReading << " Offset "
868 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700869 }
870
871 std::string sensorName = psuProperty->labelTypeName;
872 if (customizedName)
873 {
874 if (sensorName.empty())
875 {
876 // Allow selective disabling of an individual sensor,
877 // by customizing its name to an empty string.
878 std::cerr << "Sensor disabled, empty string\n";
879 continue;
880 }
881 }
882 else
883 {
884 // Sensor name not customized, do prefix/suffix composition,
885 // preserving default behavior by using psuNameFromIndex.
886 sensorName =
887 psuNameFromIndex + " " + psuProperty->labelTypeName;
888 }
889
Ed Tanous8a57ec02020-10-09 12:46:52 -0700890 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700891 {
892 std::cerr << "Sensor name \"" << sensorName << "\" path \""
893 << sensorPathStr << "\" type \"" << sensorType
894 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700895 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800896 // destruct existing one first if already created
897 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800898 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800899 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800900 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000901 readState, findSensorUnit->second, factor,
902 psuProperty->maxReading, psuProperty->minReading,
903 psuProperty->sensorOffset, labelHead, thresholdConfSize,
904 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800905 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700906 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700907 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700908 {
909 std::cerr << "Created " << numCreated << " sensors so far\n";
910 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800911 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800912
913 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800914 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800915 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000916 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
917 *psuName, readState,
918 eventPathList, groupEventPathList,
919 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800920 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700921
Ed Tanous8a57ec02020-10-09 12:46:52 -0700922 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700923 {
924 std::cerr << "Created total of " << numCreated << " sensors\n";
925 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800926 return;
927}
928
Zhikui Ren23c96e72020-11-05 22:32:28 -0800929void createSensors(
930 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
931 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
932 const std::shared_ptr<boost::container::flat_set<std::string>>&
933 sensorsChanged)
934{
935 auto getter = std::make_shared<GetSensorConfiguration>(
936 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
937 const ManagedObjectType& sensorConfigs) {
938 createSensorsCallback(io, objectServer, dbusConnection,
939 sensorConfigs, sensorsChanged);
940 });
941 getter->getConfiguration(
942 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
943}
944
Cheng C Yang916360b2019-05-07 18:47:16 +0800945void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800946{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500947 sensorTable = {{"power", sensor_paths::unitWatts},
948 {"curr", sensor_paths::unitAmperes},
949 {"temp", sensor_paths::unitDegreesC},
950 {"in", sensor_paths::unitVolts},
951 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800952
Jeff Line41d52f2021-04-07 19:38:51 +0800953 labelMatch = {
954 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
955 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
956 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
957 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
958 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
959 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
960 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
961 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
962 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
963 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
964 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
965 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
966 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
967 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
968 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
969 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
970 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
971 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
972 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
973 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
974 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
975 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
976 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
977 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
978 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
979 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
980 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
981 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
982 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
985 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
986 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
987 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
988 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
989 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
993 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
994 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
995 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
997 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
998 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
999 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1000 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1001 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1002 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1003 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1004 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1005 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1006 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1007 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1008 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1009 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1010 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1011 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
1012 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1013 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1014 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1015 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1016 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1017 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1018 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1019 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1020 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
1021 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001022
1023 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001024
1025 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1026 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1027
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001028 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1029 {"Failure", {"in2_alarm"}},
1030 {"ACLost", {"in1_beep"}},
1031 {"ConfigureError", {"in1_fault"}}};
1032
1033 groupEventMatch = {{"FanFault",
1034 {{"fan1", {"fan1_alarm", "fan1_fault"}},
1035 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001036}
1037
James Feistb6c0b912019-07-09 12:21:44 -07001038int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001039{
1040 boost::asio::io_service io;
1041 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1042
1043 systemBus->request_name("xyz.openbmc_project.PSUSensor");
1044 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001045 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -08001046 auto sensorsChanged =
1047 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001048
Cheng C Yang916360b2019-05-07 18:47:16 +08001049 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001050
Zhikui Ren23c96e72020-11-05 22:32:28 -08001051 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +08001052 boost::asio::deadline_timer filterTimer(io);
1053 std::function<void(sdbusplus::message::message&)> eventHandler =
1054 [&](sdbusplus::message::message& message) {
1055 if (message.is_method_error())
1056 {
1057 std::cerr << "callback method error\n";
1058 return;
1059 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001060 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +08001061 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +08001062 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1063 if (ec == boost::asio::error::operation_aborted)
1064 {
1065 return;
1066 }
Ed Tanous8a57ec02020-10-09 12:46:52 -07001067 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +08001068 {
1069 std::cerr << "timer error\n";
1070 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001071 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +08001072 });
1073 };
1074
1075 for (const char* type : sensorTypes)
1076 {
1077 auto match = std::make_unique<sdbusplus::bus::match::match>(
1078 static_cast<sdbusplus::bus::bus&>(*systemBus),
1079 "type='signal',member='PropertiesChanged',path_namespace='" +
1080 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
1081 eventHandler);
1082 matches.emplace_back(std::move(match));
1083 }
Bruce Lee1263c3d2021-06-04 15:16:33 +08001084
1085 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001086 io.run();
1087}