blob: 13771280d894b280a31ad6743b95ac5a3ba7b08a [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",
Tim Chao6f379ce2022-02-24 11:08:09 +080048 "xyz.openbmc_project.Configuration.ADS7830",
linchuyuan5cf66df2021-12-21 17:38:55 -080049 "xyz.openbmc_project.Configuration.BMR490",
Brandon Kim66558232021-11-09 16:53:08 -080050 "xyz.openbmc_project.Configuration.DPS800",
51 "xyz.openbmc_project.Configuration.INA219",
52 "xyz.openbmc_project.Configuration.INA230",
53 "xyz.openbmc_project.Configuration.IPSPS",
54 "xyz.openbmc_project.Configuration.ISL68137",
55 "xyz.openbmc_project.Configuration.ISL68220",
56 "xyz.openbmc_project.Configuration.ISL68223",
Khang Kieu7d6b77d2022-02-10 10:43:36 +000057 "xyz.openbmc_project.Configuration.ISL69225",
Brandon Kim66558232021-11-09 16:53:08 -080058 "xyz.openbmc_project.Configuration.ISL69243",
59 "xyz.openbmc_project.Configuration.ISL69260",
60 "xyz.openbmc_project.Configuration.LM25066",
61 "xyz.openbmc_project.Configuration.MAX16601",
62 "xyz.openbmc_project.Configuration.MAX20710",
63 "xyz.openbmc_project.Configuration.MAX20730",
64 "xyz.openbmc_project.Configuration.MAX20734",
65 "xyz.openbmc_project.Configuration.MAX20796",
66 "xyz.openbmc_project.Configuration.MAX34451",
Howard Chiub58ac3a2021-12-07 17:12:56 +080067 "xyz.openbmc_project.Configuration.MP5023",
Brandon Kim66558232021-11-09 16:53:08 -080068 "xyz.openbmc_project.Configuration.pmbus",
69 "xyz.openbmc_project.Configuration.PXE1610",
70 "xyz.openbmc_project.Configuration.RAA228000",
71 "xyz.openbmc_project.Configuration.RAA228228",
72 "xyz.openbmc_project.Configuration.RAA229004",
73 "xyz.openbmc_project.Configuration.TPS546D24",
74 "xyz.openbmc_project.Configuration.XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080075
Ed Tanousa2df7862021-12-07 16:30:27 -080076// clang-format off
77static constexpr auto pmbusNames{std::to_array<const char*>({
78 "adm1266",
79 "adm1272",
80 "adm1275",
81 "adm1278",
Tim Chao6f379ce2022-02-24 11:08:09 +080082 "ads7830",
linchuyuan5cf66df2021-12-21 17:38:55 -080083 "bmr490",
Ed Tanousa2df7862021-12-07 16:30:27 -080084 "dps800",
85 "ina219",
86 "ina230",
87 "ipsps1",
88 "isl68137",
89 "isl68220",
90 "isl68223",
Khang Kieu51ad6672022-02-09 01:26:25 +000091 "isl69225",
Ed Tanousa2df7862021-12-07 16:30:27 -080092 "isl69243",
93 "isl69260",
94 "lm25066",
95 "max16601",
96 "max20710",
97 "max20730",
98 "max20734",
99 "max20796",
100 "max34451",
Howard Chiub58ac3a2021-12-07 17:12:56 +0800101 "mp5023",
Ed Tanousa2df7862021-12-07 16:30:27 -0800102 "pmbus",
103 "pxe1610",
104 "raa228000",
105 "raa228228",
106 "raa229004",
107 "tps546d24",
108 "xdpe12284"
109})};
110//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700111
Cheng C Yang209ec562019-03-12 16:37:44 +0800112namespace fs = std::filesystem;
113
Yong Libf8b1da2020-04-15 16:32:50 +0800114static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800115 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800116static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
117 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800118static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
119 pwmSensors;
120static boost::container::flat_map<std::string, std::string> sensorTable;
121static boost::container::flat_map<std::string, PSUProperty> labelMatch;
122static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800123static boost::container::flat_map<std::string, std::vector<std::string>>
124 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800125static boost::container::flat_map<
126 std::string,
127 boost::container::flat_map<std::string, std::vector<std::string>>>
128 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800129static boost::container::flat_map<std::string, std::vector<std::string>>
130 limitEventMatch;
131
Josh Lehan74d9bd92019-10-31 08:51:58 -0700132static std::vector<PSUProperty> psuProperties;
133
Cheng C Yang58b2b532019-05-31 00:19:45 +0800134// Function CheckEvent will check each attribute from eventMatch table in the
135// sysfs. If the attributes exists in sysfs, then store the complete path
136// of the attribute into eventPathList.
137void checkEvent(
138 const std::string& directory,
139 const boost::container::flat_map<std::string, std::vector<std::string>>&
140 eventMatch,
141 boost::container::flat_map<std::string, std::vector<std::string>>&
142 eventPathList)
143{
144 for (const auto& match : eventMatch)
145 {
146 const std::vector<std::string>& eventAttrs = match.second;
147 const std::string& eventName = match.first;
148 for (const auto& eventAttr : eventAttrs)
149 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700150 std::string eventPath = directory;
151 eventPath += "/";
152 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800153
154 std::ifstream eventFile(eventPath);
155 if (!eventFile.good())
156 {
157 continue;
158 }
159
160 eventPathList[eventName].push_back(eventPath);
161 }
162 }
163}
164
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800165// Check Group Events which contains more than one targets in each combine
166// events.
167void checkGroupEvent(
168 const std::string& directory,
169 const boost::container::flat_map<
170 std::string,
171 boost::container::flat_map<std::string, std::vector<std::string>>>&
172 groupEventMatch,
173 boost::container::flat_map<
174 std::string,
175 boost::container::flat_map<std::string, std::vector<std::string>>>&
176 groupEventPathList)
177{
178 for (const auto& match : groupEventMatch)
179 {
180 const std::string& groupEventName = match.first;
181 const boost::container::flat_map<std::string, std::vector<std::string>>
182 events = match.second;
183 boost::container::flat_map<std::string, std::vector<std::string>>
184 pathList;
185 for (const auto& match : events)
186 {
187 const std::string& eventName = match.first;
188 const std::vector<std::string>& eventAttrs = match.second;
189 for (const auto& eventAttr : eventAttrs)
190 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700191 std::string eventPath = directory;
192 eventPath += "/";
193 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800194 std::ifstream eventFile(eventPath);
195 if (!eventFile.good())
196 {
197 continue;
198 }
199
200 pathList[eventName].push_back(eventPath);
201 }
202 }
203 groupEventPathList[groupEventName] = pathList;
204 }
205}
206
Cheng C Yang58b2b532019-05-31 00:19:45 +0800207// Function checkEventLimits will check all the psu related xxx_input attributes
208// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
209// xxx_min_alarm exist, then store the existing paths of the alarm attributes
210// to eventPathList.
211void checkEventLimits(
212 const std::string& sensorPathStr,
213 const boost::container::flat_map<std::string, std::vector<std::string>>&
214 limitEventMatch,
215 boost::container::flat_map<std::string, std::vector<std::string>>&
216 eventPathList)
217{
Lei YUa2c7cea2020-12-23 14:07:28 +0800218 auto attributePartPos = sensorPathStr.find_last_of('_');
219 if (attributePartPos == std::string::npos)
220 {
221 // There is no '_' in the string, skip it
222 return;
223 }
224 auto attributePart =
225 std::string_view(sensorPathStr).substr(attributePartPos + 1);
226 if (attributePart != "input")
227 {
228 // If the sensor is not xxx_input, skip it
229 return;
230 }
231
232 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800233 for (const auto& limitMatch : limitEventMatch)
234 {
235 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
236 const std::string& eventName = limitMatch.first;
237 for (const auto& limitEventAttr : limitEventAttrs)
238 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800239 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800240 std::ifstream eventFile(limitEventPath);
241 if (!eventFile.good())
242 {
243 continue;
244 }
245 eventPathList[eventName].push_back(limitEventPath);
246 }
247 }
248}
Cheng C Yang916360b2019-05-07 18:47:16 +0800249
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530250static void
251 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
252 const std::string& interfacePath,
253 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
254 sdbusplus::asio::object_server& objectServer,
255 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800256{
257 for (const auto& pwmName : pwmTable)
258 {
259 if (pwmName.first != labelHead)
260 {
261 continue;
262 }
263
264 const std::string& sensorPathStr = sensorPath.string();
265 const std::string& pwmPathStr =
266 boost::replace_all_copy(sensorPathStr, "input", "target");
267 std::ifstream pwmFile(pwmPathStr);
268 if (!pwmFile.good())
269 {
270 continue;
271 }
272
273 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
274 if (findPWMSensor != pwmSensors.end())
275 {
276 continue;
277 }
278
279 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530280 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
281 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800282 }
283}
284
Zhikui Ren23c96e72020-11-05 22:32:28 -0800285static void createSensorsCallback(
286 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
287 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
288 const ManagedObjectType& sensorConfigs,
289 const std::shared_ptr<boost::container::flat_set<std::string>>&
290 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800291{
Josh Lehan49cfba92019-10-08 16:50:42 -0700292 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800293 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800294
295 std::vector<fs::path> pmbusPaths;
296 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
297 {
298 std::cerr << "No PSU sensors in system\n";
299 return;
300 }
301
302 boost::container::flat_set<std::string> directories;
303 for (const auto& pmbusPath : pmbusPaths)
304 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800305 boost::container::flat_map<std::string, std::vector<std::string>>
306 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800307 boost::container::flat_map<
308 std::string,
309 boost::container::flat_map<std::string, std::vector<std::string>>>
310 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800311
312 std::ifstream nameFile(pmbusPath);
313 if (!nameFile.good())
314 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700315 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800316 continue;
317 }
318
319 std::string pmbusName;
320 std::getline(nameFile, pmbusName);
321 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700322
323 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
324 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800325 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700326 // To avoid this error message, add your driver name to
327 // the pmbusNames vector at the top of this file.
328 std::cerr << "Driver name " << pmbusName
329 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800330 continue;
331 }
332
Cheng C Yang209ec562019-03-12 16:37:44 +0800333 auto directory = pmbusPath.parent_path();
334
335 auto ret = directories.insert(directory.string());
336 if (!ret.second)
337 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700338 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800339 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800340 }
341
James Feistb6c0b912019-07-09 12:21:44 -0700342 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800343 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700344 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800345 if (findHyphen == std::string::npos)
346 {
347 std::cerr << "found bad device" << deviceName << "\n";
348 continue;
349 }
350 std::string busStr = deviceName.substr(0, findHyphen);
351 std::string addrStr = deviceName.substr(findHyphen + 1);
352
353 size_t bus = 0;
354 size_t addr = 0;
355
356 try
357 {
358 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700359 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800360 }
Patrick Williams26601e82021-10-06 12:43:25 -0500361 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800362 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700363 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
364 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800365 continue;
366 }
367
Cheng C Yang209ec562019-03-12 16:37:44 +0800368 const std::pair<std::string, boost::container::flat_map<
369 std::string, BasicVariantType>>*
370 baseConfig = nullptr;
371 const SensorData* sensorData = nullptr;
372 const std::string* interfacePath = nullptr;
373 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800374 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800375
376 for (const std::pair<sdbusplus::message::object_path, SensorData>&
377 sensor : sensorConfigs)
378 {
379 sensorData = &(sensor.second);
380 for (const char* type : sensorTypes)
381 {
382 auto sensorBase = sensorData->find(type);
383 if (sensorBase != sensorData->end())
384 {
385 baseConfig = &(*sensorBase);
386 sensorType = type;
387 break;
388 }
389 }
390 if (baseConfig == nullptr)
391 {
392 std::cerr << "error finding base configuration for "
393 << deviceName << "\n";
394 continue;
395 }
396
397 auto configBus = baseConfig->second.find("Bus");
398 auto configAddress = baseConfig->second.find("Address");
399
400 if (configBus == baseConfig->second.end() ||
401 configAddress == baseConfig->second.end())
402 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800403 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800404 continue;
405 }
406
Ed Tanousa771f6a2022-01-14 09:36:51 -0800407 const uint64_t* confBus = std::get_if<uint64_t>(&(configBus->second));
408 const uint64_t* confAddr = std::get_if<uint64_t>(&(configAddress->second));
409 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800410 {
411 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700412 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800413 continue;
414 }
415
416 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800417 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700418 std::cerr << "Configuration skipping " << *confBus << "-"
419 << *confAddr << " because not " << bus << "-" << addr
420 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800421 continue;
422 }
423
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800424 std::vector<thresholds::Threshold> confThresholds;
425 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
426 {
427 std::cerr << "error populating totoal thresholds\n";
428 }
429 thresholdConfSize = confThresholds.size();
430
Cheng C Yang209ec562019-03-12 16:37:44 +0800431 interfacePath = &(sensor.first.str);
432 break;
433 }
434 if (interfacePath == nullptr)
435 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700436 // To avoid this error message, add your export map entry,
437 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800438 std::cerr << "failed to find match for " << deviceName << "\n";
439 continue;
440 }
441
Cheng C Yange50345b2019-04-02 17:26:15 +0800442 auto findPSUName = baseConfig->second.find("Name");
443 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800444 {
445 std::cerr << "could not determine configuration name for "
446 << deviceName << "\n";
447 continue;
448 }
Ed Tanousa771f6a2022-01-14 09:36:51 -0800449 const std::string* psuName = std::get_if<std::string>(&(findPSUName->second));
450 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800451 {
452 std::cerr << "Cannot find psu name, invalid configuration\n";
453 continue;
454 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800455
456 // on rescans, only update sensors we were signaled by
457 if (!firstScan)
458 {
Zhikui Renda98f092021-11-01 09:41:08 -0700459 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800460 auto it =
461 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
462 [psuNameStr](std::string& s) {
463 return boost::ends_with(s, psuNameStr);
464 });
465
466 if (it == sensorsChanged->end())
467 {
468 continue;
469 }
470 sensorsChanged->erase(it);
471 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800472 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800473 checkGroupEvent(directory.string(), groupEventMatch,
474 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800475
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000476 PowerState readState = PowerState::always;
477 auto findPowerOn = baseConfig->second.find("PowerState");
478 if (findPowerOn != baseConfig->second.end())
479 {
480 std::string powerState =
481 std::visit(VariantToStringVisitor(), findPowerOn->second);
482 setReadState(powerState, readState);
483 }
484
Vijay Khemka996bad12019-05-28 15:15:16 -0700485 /* Check if there are more sensors in the same interface */
486 int i = 1;
487 std::vector<std::string> psuNames;
488 do
489 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700490 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700491 psuNames.push_back(
492 escapeName(std::get<std::string>(findPSUName->second)));
Vijay Khemka996bad12019-05-28 15:15:16 -0700493 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
494 } while (findPSUName != baseConfig->second.end());
495
Cheng C Yange50345b2019-04-02 17:26:15 +0800496 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700497 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800498 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800499 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800500 continue;
501 }
502
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530503 /* read max value in sysfs for in, curr, power, temp, ... */
504 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
505 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700506 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530507 {
508 std::cerr << "No max name in PSU \n";
509 }
510 }
511
Lei YU7170a232021-02-04 16:19:27 +0800512 /* The poll rate for the sensors */
513 double pollRate = 0.0;
514 auto pollRateObj = baseConfig->second.find("PollRate");
515
516 if (pollRateObj != baseConfig->second.end())
517 {
518 pollRate =
519 std::visit(VariantToDoubleVisitor(), pollRateObj->second);
520 if (pollRate <= 0.0)
521 {
522 pollRate = PSUSensor::defaultSensorPoll;
523 }
524 }
525
Vijay Khemka996bad12019-05-28 15:15:16 -0700526 /* Find array of labels to be exposed if it is defined in config */
527 std::vector<std::string> findLabels;
528 auto findLabelObj = baseConfig->second.find("Labels");
529 if (findLabelObj != baseConfig->second.end())
530 {
531 findLabels =
532 std::get<std::vector<std::string>>(findLabelObj->second);
533 }
534
Jason Ling5747fab2019-10-02 16:46:23 -0700535 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
536 std::smatch matches;
537
Cheng C Yange50345b2019-04-02 17:26:15 +0800538 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800539 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530540 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800541 std::string labelHead;
542 std::string sensorPathStr = sensorPath.string();
543 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700544 std::string sensorNameSubStr{""};
545 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
546 {
Josh Lehan06494452019-10-31 09:49:16 -0700547 // hwmon *_input filename without number:
548 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700549 sensorNameSubStr = matches[1];
550 }
551 else
552 {
Josh Lehan06494452019-10-31 09:49:16 -0700553 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700554 << sensorNameStr;
555 continue;
556 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800557
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530558 std::string labelPath;
559
560 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700561 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530562 if (pos != std::string::npos)
563 {
564
565 std::string sensorPathStrMax = sensorPathStr.substr(pos);
566 if (sensorPathStrMax.compare("_max") == 0)
567 {
568 labelPath =
569 boost::replace_all_copy(sensorPathStr, "max", "label");
570 maxLabel = true;
571 }
572 else
573 {
574 labelPath = boost::replace_all_copy(sensorPathStr, "input",
575 "label");
576 maxLabel = false;
577 }
578 }
579 else
580 {
581 continue;
582 }
583
Cheng C Yangecba9de2019-09-12 23:41:50 +0800584 std::ifstream labelFile(labelPath);
585 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800586 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700587 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800588 {
589 std::cerr << "Input file " << sensorPath
590 << " has no corresponding label file\n";
591 }
Josh Lehan06494452019-10-31 09:49:16 -0700592 // hwmon *_input filename with number:
593 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700594 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800595 }
596 else
597 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800598 std::string label;
599 std::getline(labelFile, label);
600 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800601 auto findSensor = sensors.find(label);
602 if (findSensor != sensors.end())
603 {
604 continue;
605 }
606
Josh Lehan06494452019-10-31 09:49:16 -0700607 // hwmon corresponding *_label file contents:
608 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700609 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800610 }
611
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530612 /* append "max" for labelMatch */
613 if (maxLabel)
614 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700615 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530616 }
617
Ed Tanous8a57ec02020-10-09 12:46:52 -0700618 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700619 {
620 std::cerr << "Sensor type=\"" << sensorNameSubStr
621 << "\" label=\"" << labelHead << "\"\n";
622 }
623
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530624 checkPWMSensor(sensorPath, labelHead, *interfacePath,
625 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800626
Vijay Khemka996bad12019-05-28 15:15:16 -0700627 if (!findLabels.empty())
628 {
629 /* Check if this labelHead is enabled in config file */
630 if (std::find(findLabels.begin(), findLabels.end(),
631 labelHead) == findLabels.end())
632 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700633 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800634 {
635 std::cerr << "could not find " << labelHead
636 << " in the Labels list\n";
637 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700638 continue;
639 }
640 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800641
Cheng C Yange50345b2019-04-02 17:26:15 +0800642 auto findProperty = labelMatch.find(labelHead);
643 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800644 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700645 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700646 {
647 std::cerr << "Could not find matching default property for "
648 << labelHead << "\n";
649 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800650 continue;
651 }
652
Josh Lehan74d9bd92019-10-31 08:51:58 -0700653 // Protect the hardcoded labelMatch list from changes,
654 // by making a copy and modifying that instead.
655 // Avoid bleedthrough of one device's customizations to
656 // the next device, as each should be independently customizable.
657 psuProperties.push_back(findProperty->second);
658 auto psuProperty = psuProperties.rbegin();
659
660 // Use label head as prefix for reading from config file,
661 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
662 std::string keyName = labelHead + "_Name";
663 std::string keyScale = labelHead + "_Scale";
664 std::string keyMin = labelHead + "_Min";
665 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800666 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800667 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700668
669 bool customizedName = false;
670 auto findCustomName = baseConfig->second.find(keyName);
671 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700672 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700673 try
674 {
675 psuProperty->labelTypeName = std::visit(
676 VariantToStringVisitor(), findCustomName->second);
677 }
Patrick Williams26601e82021-10-06 12:43:25 -0500678 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700679 {
680 std::cerr << "Unable to parse " << keyName << "\n";
681 continue;
682 }
683
684 // All strings are valid, including empty string
685 customizedName = true;
686 }
687
688 bool customizedScale = false;
689 auto findCustomScale = baseConfig->second.find(keyScale);
690 if (findCustomScale != baseConfig->second.end())
691 {
692 try
693 {
694 psuProperty->sensorScaleFactor = std::visit(
695 VariantToUnsignedIntVisitor(), findCustomScale->second);
696 }
Patrick Williams26601e82021-10-06 12:43:25 -0500697 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700698 {
699 std::cerr << "Unable to parse " << keyScale << "\n";
700 continue;
701 }
702
703 // Avoid later division by zero
704 if (psuProperty->sensorScaleFactor > 0)
705 {
706 customizedScale = true;
707 }
708 else
709 {
710 std::cerr << "Unable to accept " << keyScale << "\n";
711 continue;
712 }
713 }
714
715 auto findCustomMin = baseConfig->second.find(keyMin);
716 if (findCustomMin != baseConfig->second.end())
717 {
718 try
719 {
720 psuProperty->minReading = std::visit(
721 VariantToDoubleVisitor(), findCustomMin->second);
722 }
Patrick Williams26601e82021-10-06 12:43:25 -0500723 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700724 {
725 std::cerr << "Unable to parse " << keyMin << "\n";
726 continue;
727 }
728 }
729
730 auto findCustomMax = baseConfig->second.find(keyMax);
731 if (findCustomMax != baseConfig->second.end())
732 {
733 try
734 {
735 psuProperty->maxReading = std::visit(
736 VariantToDoubleVisitor(), findCustomMax->second);
737 }
Patrick Williams26601e82021-10-06 12:43:25 -0500738 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700739 {
740 std::cerr << "Unable to parse " << keyMax << "\n";
741 continue;
742 }
743 }
744
Jeff Line41d52f2021-04-07 19:38:51 +0800745 auto findCustomOffset = baseConfig->second.find(keyOffset);
746 if (findCustomOffset != baseConfig->second.end())
747 {
748 try
749 {
750 psuProperty->sensorOffset = std::visit(
751 VariantToDoubleVisitor(), findCustomOffset->second);
752 }
Patrick Williams26601e82021-10-06 12:43:25 -0500753 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800754 {
755 std::cerr << "Unable to parse " << keyOffset << "\n";
756 continue;
757 }
758 }
759
Lotus Xucb5af732021-09-10 15:18:50 +0800760 // if we find label head power state set ,override the powerstate.
761 auto findPowerState = baseConfig->second.find(keyPowerState);
762 if (findPowerState != baseConfig->second.end())
763 {
764 std::string powerState = std::visit(VariantToStringVisitor(),
765 findPowerState->second);
766 setReadState(powerState, readState);
767 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700768 if (!(psuProperty->minReading < psuProperty->maxReading))
769 {
770 std::cerr << "Min must be less than Max\n";
771 continue;
772 }
773
774 // If the sensor name is being customized by config file,
775 // then prefix/suffix composition becomes not necessary,
776 // and in fact not wanted, because it gets in the way.
777 std::string psuNameFromIndex;
778 if (!customizedName)
779 {
780 /* Find out sensor name index for this label */
781 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500782 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700783 if (std::regex_search(labelHead, matches, rgx))
784 {
785 nameIndex = std::stoi(matches[1]);
786
787 // Decrement to preserve alignment, because hwmon
788 // human-readable filenames and labels use 1-based
789 // numbering, but the "Name", "Name1", "Name2", etc. naming
790 // convention (the psuNames vector) uses 0-based numbering.
791 if (nameIndex > 0)
792 {
793 --nameIndex;
794 }
795 }
796 else
797 {
798 nameIndex = 0;
799 }
800
801 if (psuNames.size() <= nameIndex)
802 {
803 std::cerr << "Could not pair " << labelHead
804 << " with a Name field\n";
805 continue;
806 }
807
808 psuNameFromIndex = psuNames[nameIndex];
809
Ed Tanous8a57ec02020-10-09 12:46:52 -0700810 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700811 {
812 std::cerr << "Sensor label head " << labelHead
813 << " paired with " << psuNameFromIndex
814 << " at index " << nameIndex << "\n";
815 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700816 }
817
Cheng C Yang58b2b532019-05-31 00:19:45 +0800818 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
819
Josh Lehan74d9bd92019-10-31 08:51:58 -0700820 // Similarly, if sensor scaling factor is being customized,
821 // then the below power-of-10 constraint becomes unnecessary,
822 // as config should be able to specify an arbitrary divisor.
823 unsigned int factor = psuProperty->sensorScaleFactor;
824 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700825 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700826 // Preserve existing usage of hardcoded labelMatch table below
827 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700828
Josh Lehan74d9bd92019-10-31 08:51:58 -0700829 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700830 char firstChar =
831 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700832 std::string strScaleFactor =
833 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
834
835 // Preserve existing configs by accepting earlier syntax,
836 // example CurrScaleFactor, PowerScaleFactor, ...
837 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
838 if (findScaleFactor != baseConfig->second.end())
839 {
840 factor = std::visit(VariantToIntVisitor(),
841 findScaleFactor->second);
842 }
843
Ed Tanous8a57ec02020-10-09 12:46:52 -0700844 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700845 {
846 std::cerr << "Sensor scaling factor " << factor
847 << " string " << strScaleFactor << "\n";
848 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700849 }
850
Vijay Khemka996bad12019-05-28 15:15:16 -0700851 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530852 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
853 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800854 {
James Feist17ab6e02019-06-25 12:28:13 -0700855 std::cerr << "error populating thresholds for "
856 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800857 }
858
Zev Weiss6b6891c2021-04-22 02:46:21 -0500859 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
860 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800861 {
Jason Ling5747fab2019-10-02 16:46:23 -0700862 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700863 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800864 continue;
865 }
866
Ed Tanous8a57ec02020-10-09 12:46:52 -0700867 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700868 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700869 std::cerr << "Sensor properties: Name \""
870 << psuProperty->labelTypeName << "\" Scale "
871 << psuProperty->sensorScaleFactor << " Min "
872 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800873 << psuProperty->maxReading << " Offset "
874 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700875 }
876
877 std::string sensorName = psuProperty->labelTypeName;
878 if (customizedName)
879 {
880 if (sensorName.empty())
881 {
882 // Allow selective disabling of an individual sensor,
883 // by customizing its name to an empty string.
884 std::cerr << "Sensor disabled, empty string\n";
885 continue;
886 }
887 }
888 else
889 {
890 // Sensor name not customized, do prefix/suffix composition,
891 // preserving default behavior by using psuNameFromIndex.
892 sensorName =
893 psuNameFromIndex + " " + psuProperty->labelTypeName;
894 }
895
Ed Tanous8a57ec02020-10-09 12:46:52 -0700896 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700897 {
898 std::cerr << "Sensor name \"" << sensorName << "\" path \""
899 << sensorPathStr << "\" type \"" << sensorType
900 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700901 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800902 // destruct existing one first if already created
903 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800904 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800905 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800906 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000907 readState, findSensorUnit->second, factor,
908 psuProperty->maxReading, psuProperty->minReading,
909 psuProperty->sensorOffset, labelHead, thresholdConfSize,
910 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800911 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700912 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700913 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700914 {
915 std::cerr << "Created " << numCreated << " sensors so far\n";
916 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800917 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800918
919 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800920 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800921 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000922 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
923 *psuName, readState,
924 eventPathList, groupEventPathList,
925 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800926 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700927
Ed Tanous8a57ec02020-10-09 12:46:52 -0700928 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700929 {
930 std::cerr << "Created total of " << numCreated << " sensors\n";
931 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800932 return;
933}
934
Zhikui Ren23c96e72020-11-05 22:32:28 -0800935void createSensors(
936 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
937 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
938 const std::shared_ptr<boost::container::flat_set<std::string>>&
939 sensorsChanged)
940{
941 auto getter = std::make_shared<GetSensorConfiguration>(
942 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
943 const ManagedObjectType& sensorConfigs) {
944 createSensorsCallback(io, objectServer, dbusConnection,
945 sensorConfigs, sensorsChanged);
946 });
947 getter->getConfiguration(
948 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
949}
950
Cheng C Yang916360b2019-05-07 18:47:16 +0800951void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800952{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500953 sensorTable = {{"power", sensor_paths::unitWatts},
954 {"curr", sensor_paths::unitAmperes},
955 {"temp", sensor_paths::unitDegreesC},
956 {"in", sensor_paths::unitVolts},
957 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800958
Jeff Line41d52f2021-04-07 19:38:51 +0800959 labelMatch = {
960 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
961 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
962 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
963 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
964 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
965 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
966 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
967 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
968 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
969 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
970 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
971 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
972 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
973 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
974 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
975 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
976 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
977 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
978 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
979 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
980 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
981 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
982 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
985 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
986 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
987 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
988 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
989 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
993 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
994 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
995 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
997 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
998 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
999 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1000 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001001 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001002 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001003 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1004 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1005 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1006 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1007 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1008 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001009 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
1010 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1011 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1012 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1013 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1014 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1015 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1016 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1017 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1018 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1019 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1020 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1021 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1022 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1023 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1024 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
1025 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1026 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1027 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1028 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1029 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1030 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1031 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1032 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1033 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
1034 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001035
1036 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001037
1038 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1039 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1040
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001041 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1042 {"Failure", {"in2_alarm"}},
1043 {"ACLost", {"in1_beep"}},
1044 {"ConfigureError", {"in1_fault"}}};
1045
1046 groupEventMatch = {{"FanFault",
1047 {{"fan1", {"fan1_alarm", "fan1_fault"}},
1048 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001049}
1050
James Feistb6c0b912019-07-09 12:21:44 -07001051int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001052{
1053 boost::asio::io_service io;
1054 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1055
1056 systemBus->request_name("xyz.openbmc_project.PSUSensor");
1057 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001058 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -08001059 auto sensorsChanged =
1060 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001061
Cheng C Yang916360b2019-05-07 18:47:16 +08001062 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001063
Zhikui Ren23c96e72020-11-05 22:32:28 -08001064 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +08001065 boost::asio::deadline_timer filterTimer(io);
1066 std::function<void(sdbusplus::message::message&)> eventHandler =
1067 [&](sdbusplus::message::message& message) {
1068 if (message.is_method_error())
1069 {
1070 std::cerr << "callback method error\n";
1071 return;
1072 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001073 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +08001074 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +08001075 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1076 if (ec == boost::asio::error::operation_aborted)
1077 {
1078 return;
1079 }
Ed Tanous8a57ec02020-10-09 12:46:52 -07001080 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +08001081 {
1082 std::cerr << "timer error\n";
1083 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001084 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +08001085 });
1086 };
1087
1088 for (const char* type : sensorTypes)
1089 {
1090 auto match = std::make_unique<sdbusplus::bus::match::match>(
1091 static_cast<sdbusplus::bus::bus&>(*systemBus),
1092 "type='signal',member='PropertiesChanged',path_namespace='" +
1093 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
1094 eventHandler);
1095 matches.emplace_back(std::move(match));
1096 }
Bruce Lee1263c3d2021-06-04 15:16:33 +08001097
1098 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001099 io.run();
1100}