blob: 0f7d17efa51b9f1d2e37874084681999a14a6396 [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>
36#include <utility>
37#include <variant>
38#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080039
Ed Tanous8a57ec02020-10-09 12:46:52 -070040static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070041
Jeff Lina0683a82021-02-22 13:56:55 +080042static constexpr std::array<const char*, 22> sensorTypes = {
Josh Lehan62084c82020-06-22 15:27:12 -070043 "xyz.openbmc_project.Configuration.ADM1272",
44 "xyz.openbmc_project.Configuration.ADM1278",
Jeff Lina0683a82021-02-22 13:56:55 +080045 "xyz.openbmc_project.Configuration.DPS800",
Josh Lehan62084c82020-06-22 15:27:12 -070046 "xyz.openbmc_project.Configuration.INA219",
Devjit Gopalpurde65ef72019-10-30 04:47:35 -070047 "xyz.openbmc_project.Configuration.INA230",
Alex Qiu6690d8f2020-01-22 17:56:33 -080048 "xyz.openbmc_project.Configuration.ISL68137",
Jason Lingdfad1ff2020-07-29 15:43:11 -070049 "xyz.openbmc_project.Configuration.ISL68220",
Gaurav Gandhi49585582020-12-22 01:01:24 +000050 "xyz.openbmc_project.Configuration.ISL68223",
51 "xyz.openbmc_project.Configuration.ISL69243",
Jeff Lina0683a82021-02-22 13:56:55 +080052 "xyz.openbmc_project.Configuration.ISL69260",
Alex Qiuf5709b42020-01-29 13:29:50 -080053 "xyz.openbmc_project.Configuration.MAX16601",
Gaurav Gandhi49585582020-12-22 01:01:24 +000054 "xyz.openbmc_project.Configuration.MAX20710",
Alex Qiu6690d8f2020-01-22 17:56:33 -080055 "xyz.openbmc_project.Configuration.MAX20730",
56 "xyz.openbmc_project.Configuration.MAX20734",
57 "xyz.openbmc_project.Configuration.MAX20796",
58 "xyz.openbmc_project.Configuration.MAX34451",
Josh Lehan62084c82020-06-22 15:27:12 -070059 "xyz.openbmc_project.Configuration.pmbus",
60 "xyz.openbmc_project.Configuration.PXE1610",
Jeff Lina0683a82021-02-22 13:56:55 +080061 "xyz.openbmc_project.Configuration.RAA228000",
62 "xyz.openbmc_project.Configuration.RAA228228",
63 "xyz.openbmc_project.Configuration.RAA229004",
64 "xyz.openbmc_project.Configuration.TPS546D24"};
Cheng C Yang209ec562019-03-12 16:37:44 +080065
Alex Qiu6690d8f2020-01-22 17:56:33 -080066static std::vector<std::string> pmbusNames = {
Jeff Lina0683a82021-02-22 13:56:55 +080067 "adm1272", "adm1278", "dps800", "ina219", "ina230", "isl68137",
68 "isl68220", "isl68223", "isl69243", "isl69260", "max16601", "max20710",
69 "max20730", "max20734", "max20796", "max34451", "pmbus", "pxe1610",
70 "raa228000", "raa228228", "raa229004", "tps546d24"};
Josh Lehan0830c7b2019-10-08 16:35:09 -070071
Cheng C Yang209ec562019-03-12 16:37:44 +080072namespace fs = std::filesystem;
73
Yong Libf8b1da2020-04-15 16:32:50 +080074static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +080075 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +080076static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
77 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +080078static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
79 pwmSensors;
80static boost::container::flat_map<std::string, std::string> sensorTable;
81static boost::container::flat_map<std::string, PSUProperty> labelMatch;
82static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +080083static boost::container::flat_map<std::string, std::vector<std::string>>
84 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +080085static boost::container::flat_map<
86 std::string,
87 boost::container::flat_map<std::string, std::vector<std::string>>>
88 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +080089static boost::container::flat_map<std::string, std::vector<std::string>>
90 limitEventMatch;
91
Josh Lehan74d9bd92019-10-31 08:51:58 -070092static std::vector<PSUProperty> psuProperties;
93
Cheng C Yang58b2b532019-05-31 00:19:45 +080094// Function CheckEvent will check each attribute from eventMatch table in the
95// sysfs. If the attributes exists in sysfs, then store the complete path
96// of the attribute into eventPathList.
97void checkEvent(
98 const std::string& directory,
99 const boost::container::flat_map<std::string, std::vector<std::string>>&
100 eventMatch,
101 boost::container::flat_map<std::string, std::vector<std::string>>&
102 eventPathList)
103{
104 for (const auto& match : eventMatch)
105 {
106 const std::vector<std::string>& eventAttrs = match.second;
107 const std::string& eventName = match.first;
108 for (const auto& eventAttr : eventAttrs)
109 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700110 std::string eventPath = directory;
111 eventPath += "/";
112 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800113
114 std::ifstream eventFile(eventPath);
115 if (!eventFile.good())
116 {
117 continue;
118 }
119
120 eventPathList[eventName].push_back(eventPath);
121 }
122 }
123}
124
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800125// Check Group Events which contains more than one targets in each combine
126// events.
127void checkGroupEvent(
128 const std::string& directory,
129 const boost::container::flat_map<
130 std::string,
131 boost::container::flat_map<std::string, std::vector<std::string>>>&
132 groupEventMatch,
133 boost::container::flat_map<
134 std::string,
135 boost::container::flat_map<std::string, std::vector<std::string>>>&
136 groupEventPathList)
137{
138 for (const auto& match : groupEventMatch)
139 {
140 const std::string& groupEventName = match.first;
141 const boost::container::flat_map<std::string, std::vector<std::string>>
142 events = match.second;
143 boost::container::flat_map<std::string, std::vector<std::string>>
144 pathList;
145 for (const auto& match : events)
146 {
147 const std::string& eventName = match.first;
148 const std::vector<std::string>& eventAttrs = match.second;
149 for (const auto& eventAttr : eventAttrs)
150 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700151 std::string eventPath = directory;
152 eventPath += "/";
153 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800154 std::ifstream eventFile(eventPath);
155 if (!eventFile.good())
156 {
157 continue;
158 }
159
160 pathList[eventName].push_back(eventPath);
161 }
162 }
163 groupEventPathList[groupEventName] = pathList;
164 }
165}
166
Cheng C Yang58b2b532019-05-31 00:19:45 +0800167// Function checkEventLimits will check all the psu related xxx_input attributes
168// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
169// xxx_min_alarm exist, then store the existing paths of the alarm attributes
170// to eventPathList.
171void checkEventLimits(
172 const std::string& sensorPathStr,
173 const boost::container::flat_map<std::string, std::vector<std::string>>&
174 limitEventMatch,
175 boost::container::flat_map<std::string, std::vector<std::string>>&
176 eventPathList)
177{
178 for (const auto& limitMatch : limitEventMatch)
179 {
180 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
181 const std::string& eventName = limitMatch.first;
182 for (const auto& limitEventAttr : limitEventAttrs)
183 {
184 auto limitEventPath =
185 boost::replace_all_copy(sensorPathStr, "input", limitEventAttr);
186 std::ifstream eventFile(limitEventPath);
187 if (!eventFile.good())
188 {
189 continue;
190 }
191 eventPathList[eventName].push_back(limitEventPath);
192 }
193 }
194}
Cheng C Yang916360b2019-05-07 18:47:16 +0800195
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530196static void
197 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
198 const std::string& interfacePath,
199 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
200 sdbusplus::asio::object_server& objectServer,
201 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800202{
203 for (const auto& pwmName : pwmTable)
204 {
205 if (pwmName.first != labelHead)
206 {
207 continue;
208 }
209
210 const std::string& sensorPathStr = sensorPath.string();
211 const std::string& pwmPathStr =
212 boost::replace_all_copy(sensorPathStr, "input", "target");
213 std::ifstream pwmFile(pwmPathStr);
214 if (!pwmFile.good())
215 {
216 continue;
217 }
218
219 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
220 if (findPWMSensor != pwmSensors.end())
221 {
222 continue;
223 }
224
225 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530226 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
227 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800228 }
229}
230
Zhikui Ren23c96e72020-11-05 22:32:28 -0800231static void createSensorsCallback(
232 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
233 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
234 const ManagedObjectType& sensorConfigs,
235 const std::shared_ptr<boost::container::flat_set<std::string>>&
236 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800237{
Josh Lehan49cfba92019-10-08 16:50:42 -0700238 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800239 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800240
241 std::vector<fs::path> pmbusPaths;
242 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
243 {
244 std::cerr << "No PSU sensors in system\n";
245 return;
246 }
247
248 boost::container::flat_set<std::string> directories;
249 for (const auto& pmbusPath : pmbusPaths)
250 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800251 boost::container::flat_map<std::string, std::vector<std::string>>
252 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800253 boost::container::flat_map<
254 std::string,
255 boost::container::flat_map<std::string, std::vector<std::string>>>
256 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800257
258 std::ifstream nameFile(pmbusPath);
259 if (!nameFile.good())
260 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700261 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800262 continue;
263 }
264
265 std::string pmbusName;
266 std::getline(nameFile, pmbusName);
267 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700268
269 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
270 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800271 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700272 // To avoid this error message, add your driver name to
273 // the pmbusNames vector at the top of this file.
274 std::cerr << "Driver name " << pmbusName
275 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800276 continue;
277 }
278
279 const std::string* psuName;
Cheng C Yang209ec562019-03-12 16:37:44 +0800280 auto directory = pmbusPath.parent_path();
281
282 auto ret = directories.insert(directory.string());
283 if (!ret.second)
284 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700285 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800286 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800287 }
288
James Feistb6c0b912019-07-09 12:21:44 -0700289 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800290 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700291 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800292 if (findHyphen == std::string::npos)
293 {
294 std::cerr << "found bad device" << deviceName << "\n";
295 continue;
296 }
297 std::string busStr = deviceName.substr(0, findHyphen);
298 std::string addrStr = deviceName.substr(findHyphen + 1);
299
300 size_t bus = 0;
301 size_t addr = 0;
302
303 try
304 {
305 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700306 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800307 }
James Feistb6c0b912019-07-09 12:21:44 -0700308 catch (std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800309 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700310 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
311 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800312 continue;
313 }
314
Cheng C Yang209ec562019-03-12 16:37:44 +0800315 const std::pair<std::string, boost::container::flat_map<
316 std::string, BasicVariantType>>*
317 baseConfig = nullptr;
318 const SensorData* sensorData = nullptr;
319 const std::string* interfacePath = nullptr;
320 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800321 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800322
323 for (const std::pair<sdbusplus::message::object_path, SensorData>&
324 sensor : sensorConfigs)
325 {
326 sensorData = &(sensor.second);
327 for (const char* type : sensorTypes)
328 {
329 auto sensorBase = sensorData->find(type);
330 if (sensorBase != sensorData->end())
331 {
332 baseConfig = &(*sensorBase);
333 sensorType = type;
334 break;
335 }
336 }
337 if (baseConfig == nullptr)
338 {
339 std::cerr << "error finding base configuration for "
340 << deviceName << "\n";
341 continue;
342 }
343
344 auto configBus = baseConfig->second.find("Bus");
345 auto configAddress = baseConfig->second.find("Address");
346
347 if (configBus == baseConfig->second.end() ||
348 configAddress == baseConfig->second.end())
349 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800350 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800351 continue;
352 }
353
Cheng C Yang58b2b532019-05-31 00:19:45 +0800354 const uint64_t* confBus;
355 const uint64_t* confAddr;
356 if (!(confBus = std::get_if<uint64_t>(&(configBus->second))) ||
357 !(confAddr = std::get_if<uint64_t>(&(configAddress->second))))
358 {
359 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700360 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800361 continue;
362 }
363
364 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800365 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700366 std::cerr << "Configuration skipping " << *confBus << "-"
367 << *confAddr << " because not " << bus << "-" << addr
368 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800369 continue;
370 }
371
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800372 std::vector<thresholds::Threshold> confThresholds;
373 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
374 {
375 std::cerr << "error populating totoal thresholds\n";
376 }
377 thresholdConfSize = confThresholds.size();
378
Cheng C Yang209ec562019-03-12 16:37:44 +0800379 interfacePath = &(sensor.first.str);
380 break;
381 }
382 if (interfacePath == nullptr)
383 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700384 // To avoid this error message, add your export map entry,
385 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800386 std::cerr << "failed to find match for " << deviceName << "\n";
387 continue;
388 }
389
Cheng C Yange50345b2019-04-02 17:26:15 +0800390 auto findPSUName = baseConfig->second.find("Name");
391 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800392 {
393 std::cerr << "could not determine configuration name for "
394 << deviceName << "\n";
395 continue;
396 }
397
Cheng C Yang58b2b532019-05-31 00:19:45 +0800398 if (!(psuName = std::get_if<std::string>(&(findPSUName->second))))
399 {
400 std::cerr << "Cannot find psu name, invalid configuration\n";
401 continue;
402 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800403
404 // on rescans, only update sensors we were signaled by
405 if (!firstScan)
406 {
407 std::string psuNameStr = "/" + *psuName;
408 auto it =
409 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
410 [psuNameStr](std::string& s) {
411 return boost::ends_with(s, psuNameStr);
412 });
413
414 if (it == sensorsChanged->end())
415 {
416 continue;
417 }
418 sensorsChanged->erase(it);
419 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800420 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800421 checkGroupEvent(directory.string(), groupEventMatch,
422 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800423
Vijay Khemka996bad12019-05-28 15:15:16 -0700424 /* Check if there are more sensors in the same interface */
425 int i = 1;
426 std::vector<std::string> psuNames;
427 do
428 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700429 // Individual string fields: Name, Name1, Name2, Name3, ...
Vijay Khemka996bad12019-05-28 15:15:16 -0700430 psuNames.push_back(std::get<std::string>(findPSUName->second));
431 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
432 } while (findPSUName != baseConfig->second.end());
433
Cheng C Yange50345b2019-04-02 17:26:15 +0800434 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700435 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800436 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800437 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800438 continue;
439 }
440
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530441 /* read max value in sysfs for in, curr, power, temp, ... */
442 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
443 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700444 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530445 {
446 std::cerr << "No max name in PSU \n";
447 }
448 }
449
Vijay Khemka996bad12019-05-28 15:15:16 -0700450 /* Find array of labels to be exposed if it is defined in config */
451 std::vector<std::string> findLabels;
452 auto findLabelObj = baseConfig->second.find("Labels");
453 if (findLabelObj != baseConfig->second.end())
454 {
455 findLabels =
456 std::get<std::vector<std::string>>(findLabelObj->second);
457 }
458
Jason Ling5747fab2019-10-02 16:46:23 -0700459 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
460 std::smatch matches;
461
Cheng C Yange50345b2019-04-02 17:26:15 +0800462 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800463 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530464 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800465 std::string labelHead;
466 std::string sensorPathStr = sensorPath.string();
467 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700468 std::string sensorNameSubStr{""};
469 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
470 {
Josh Lehan06494452019-10-31 09:49:16 -0700471 // hwmon *_input filename without number:
472 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700473 sensorNameSubStr = matches[1];
474 }
475 else
476 {
Josh Lehan06494452019-10-31 09:49:16 -0700477 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700478 << sensorNameStr;
479 continue;
480 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800481
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530482 std::string labelPath;
483
484 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700485 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530486 if (pos != std::string::npos)
487 {
488
489 std::string sensorPathStrMax = sensorPathStr.substr(pos);
490 if (sensorPathStrMax.compare("_max") == 0)
491 {
492 labelPath =
493 boost::replace_all_copy(sensorPathStr, "max", "label");
494 maxLabel = true;
495 }
496 else
497 {
498 labelPath = boost::replace_all_copy(sensorPathStr, "input",
499 "label");
500 maxLabel = false;
501 }
502 }
503 else
504 {
505 continue;
506 }
507
Cheng C Yangecba9de2019-09-12 23:41:50 +0800508 std::ifstream labelFile(labelPath);
509 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800510 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700511 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800512 {
513 std::cerr << "Input file " << sensorPath
514 << " has no corresponding label file\n";
515 }
Josh Lehan06494452019-10-31 09:49:16 -0700516 // hwmon *_input filename with number:
517 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700518 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800519 }
520 else
521 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800522 std::string label;
523 std::getline(labelFile, label);
524 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800525 auto findSensor = sensors.find(label);
526 if (findSensor != sensors.end())
527 {
528 continue;
529 }
530
Josh Lehan06494452019-10-31 09:49:16 -0700531 // hwmon corresponding *_label file contents:
532 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700533 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800534 }
535
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530536 /* append "max" for labelMatch */
537 if (maxLabel)
538 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700539 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530540 }
541
Ed Tanous8a57ec02020-10-09 12:46:52 -0700542 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700543 {
544 std::cerr << "Sensor type=\"" << sensorNameSubStr
545 << "\" label=\"" << labelHead << "\"\n";
546 }
547
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530548 checkPWMSensor(sensorPath, labelHead, *interfacePath,
549 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800550
Vijay Khemka996bad12019-05-28 15:15:16 -0700551 if (!findLabels.empty())
552 {
553 /* Check if this labelHead is enabled in config file */
554 if (std::find(findLabels.begin(), findLabels.end(),
555 labelHead) == findLabels.end())
556 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700557 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800558 {
559 std::cerr << "could not find " << labelHead
560 << " in the Labels list\n";
561 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700562 continue;
563 }
564 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800565
Cheng C Yange50345b2019-04-02 17:26:15 +0800566 auto findProperty = labelMatch.find(labelHead);
567 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800568 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700569 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700570 {
571 std::cerr << "Could not find matching default property for "
572 << labelHead << "\n";
573 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800574 continue;
575 }
576
Josh Lehan74d9bd92019-10-31 08:51:58 -0700577 // Protect the hardcoded labelMatch list from changes,
578 // by making a copy and modifying that instead.
579 // Avoid bleedthrough of one device's customizations to
580 // the next device, as each should be independently customizable.
581 psuProperties.push_back(findProperty->second);
582 auto psuProperty = psuProperties.rbegin();
583
584 // Use label head as prefix for reading from config file,
585 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
586 std::string keyName = labelHead + "_Name";
587 std::string keyScale = labelHead + "_Scale";
588 std::string keyMin = labelHead + "_Min";
589 std::string keyMax = labelHead + "_Max";
590
591 bool customizedName = false;
592 auto findCustomName = baseConfig->second.find(keyName);
593 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700594 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700595 try
596 {
597 psuProperty->labelTypeName = std::visit(
598 VariantToStringVisitor(), findCustomName->second);
599 }
600 catch (std::invalid_argument&)
601 {
602 std::cerr << "Unable to parse " << keyName << "\n";
603 continue;
604 }
605
606 // All strings are valid, including empty string
607 customizedName = true;
608 }
609
610 bool customizedScale = false;
611 auto findCustomScale = baseConfig->second.find(keyScale);
612 if (findCustomScale != baseConfig->second.end())
613 {
614 try
615 {
616 psuProperty->sensorScaleFactor = std::visit(
617 VariantToUnsignedIntVisitor(), findCustomScale->second);
618 }
619 catch (std::invalid_argument&)
620 {
621 std::cerr << "Unable to parse " << keyScale << "\n";
622 continue;
623 }
624
625 // Avoid later division by zero
626 if (psuProperty->sensorScaleFactor > 0)
627 {
628 customizedScale = true;
629 }
630 else
631 {
632 std::cerr << "Unable to accept " << keyScale << "\n";
633 continue;
634 }
635 }
636
637 auto findCustomMin = baseConfig->second.find(keyMin);
638 if (findCustomMin != baseConfig->second.end())
639 {
640 try
641 {
642 psuProperty->minReading = std::visit(
643 VariantToDoubleVisitor(), findCustomMin->second);
644 }
645 catch (std::invalid_argument&)
646 {
647 std::cerr << "Unable to parse " << keyMin << "\n";
648 continue;
649 }
650 }
651
652 auto findCustomMax = baseConfig->second.find(keyMax);
653 if (findCustomMax != baseConfig->second.end())
654 {
655 try
656 {
657 psuProperty->maxReading = std::visit(
658 VariantToDoubleVisitor(), findCustomMax->second);
659 }
660 catch (std::invalid_argument&)
661 {
662 std::cerr << "Unable to parse " << keyMax << "\n";
663 continue;
664 }
665 }
666
667 if (!(psuProperty->minReading < psuProperty->maxReading))
668 {
669 std::cerr << "Min must be less than Max\n";
670 continue;
671 }
672
673 // If the sensor name is being customized by config file,
674 // then prefix/suffix composition becomes not necessary,
675 // and in fact not wanted, because it gets in the way.
676 std::string psuNameFromIndex;
677 if (!customizedName)
678 {
679 /* Find out sensor name index for this label */
680 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500681 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700682 if (std::regex_search(labelHead, matches, rgx))
683 {
684 nameIndex = std::stoi(matches[1]);
685
686 // Decrement to preserve alignment, because hwmon
687 // human-readable filenames and labels use 1-based
688 // numbering, but the "Name", "Name1", "Name2", etc. naming
689 // convention (the psuNames vector) uses 0-based numbering.
690 if (nameIndex > 0)
691 {
692 --nameIndex;
693 }
694 }
695 else
696 {
697 nameIndex = 0;
698 }
699
700 if (psuNames.size() <= nameIndex)
701 {
702 std::cerr << "Could not pair " << labelHead
703 << " with a Name field\n";
704 continue;
705 }
706
707 psuNameFromIndex = psuNames[nameIndex];
708
Ed Tanous8a57ec02020-10-09 12:46:52 -0700709 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700710 {
711 std::cerr << "Sensor label head " << labelHead
712 << " paired with " << psuNameFromIndex
713 << " at index " << nameIndex << "\n";
714 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700715 }
716
Cheng C Yang58b2b532019-05-31 00:19:45 +0800717 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
718
Josh Lehan74d9bd92019-10-31 08:51:58 -0700719 // Similarly, if sensor scaling factor is being customized,
720 // then the below power-of-10 constraint becomes unnecessary,
721 // as config should be able to specify an arbitrary divisor.
722 unsigned int factor = psuProperty->sensorScaleFactor;
723 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700724 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700725 // Preserve existing usage of hardcoded labelMatch table below
726 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700727
Josh Lehan74d9bd92019-10-31 08:51:58 -0700728 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700729 char firstChar =
730 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700731 std::string strScaleFactor =
732 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
733
734 // Preserve existing configs by accepting earlier syntax,
735 // example CurrScaleFactor, PowerScaleFactor, ...
736 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
737 if (findScaleFactor != baseConfig->second.end())
738 {
739 factor = std::visit(VariantToIntVisitor(),
740 findScaleFactor->second);
741 }
742
Ed Tanous8a57ec02020-10-09 12:46:52 -0700743 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700744 {
745 std::cerr << "Sensor scaling factor " << factor
746 << " string " << strScaleFactor << "\n";
747 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700748 }
749
Vijay Khemka996bad12019-05-28 15:15:16 -0700750 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530751 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
752 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800753 {
James Feist17ab6e02019-06-25 12:28:13 -0700754 std::cerr << "error populating thresholds for "
755 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800756 }
757
758 auto findSensorType = sensorTable.find(sensorNameSubStr);
759 if (findSensorType == sensorTable.end())
760 {
Jason Ling5747fab2019-10-02 16:46:23 -0700761 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700762 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800763 continue;
764 }
765
Ed Tanous8a57ec02020-10-09 12:46:52 -0700766 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700767 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700768 std::cerr << "Sensor properties: Name \""
769 << psuProperty->labelTypeName << "\" Scale "
770 << psuProperty->sensorScaleFactor << " Min "
771 << psuProperty->minReading << " Max "
772 << psuProperty->maxReading << "\n";
773 }
774
775 std::string sensorName = psuProperty->labelTypeName;
776 if (customizedName)
777 {
778 if (sensorName.empty())
779 {
780 // Allow selective disabling of an individual sensor,
781 // by customizing its name to an empty string.
782 std::cerr << "Sensor disabled, empty string\n";
783 continue;
784 }
785 }
786 else
787 {
788 // Sensor name not customized, do prefix/suffix composition,
789 // preserving default behavior by using psuNameFromIndex.
790 sensorName =
791 psuNameFromIndex + " " + psuProperty->labelTypeName;
792 }
793
Ed Tanous8a57ec02020-10-09 12:46:52 -0700794 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700795 {
796 std::cerr << "Sensor name \"" << sensorName << "\" path \""
797 << sensorPathStr << "\" type \"" << sensorType
798 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700799 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800800 // destruct existing one first if already created
801 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800802 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800803 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800804 sensorName, std::move(sensorThresholds), *interfacePath,
Josh Lehan74d9bd92019-10-31 08:51:58 -0700805 findSensorType->second, factor, psuProperty->maxReading,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800806 psuProperty->minReading, labelHead, thresholdConfSize);
Yong Libf8b1da2020-04-15 16:32:50 +0800807 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700808 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700809 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700810 {
811 std::cerr << "Created " << numCreated << " sensors so far\n";
812 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800813 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800814
815 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800816 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800817 combineEvents[*psuName + "OperationalStatus"] =
Yong Li3046a022020-04-03 13:01:02 +0800818 std::make_unique<PSUCombineEvent>(
819 objectServer, dbusConnection, io, *psuName, eventPathList,
820 groupEventPathList, "OperationalStatus");
Cheng C Yang209ec562019-03-12 16:37:44 +0800821 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700822
Ed Tanous8a57ec02020-10-09 12:46:52 -0700823 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700824 {
825 std::cerr << "Created total of " << numCreated << " sensors\n";
826 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800827 return;
828}
829
Zhikui Ren23c96e72020-11-05 22:32:28 -0800830void createSensors(
831 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
832 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
833 const std::shared_ptr<boost::container::flat_set<std::string>>&
834 sensorsChanged)
835{
836 auto getter = std::make_shared<GetSensorConfiguration>(
837 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
838 const ManagedObjectType& sensorConfigs) {
839 createSensorsCallback(io, objectServer, dbusConnection,
840 sensorConfigs, sensorsChanged);
841 });
842 getter->getConfiguration(
843 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
844}
845
Cheng C Yang916360b2019-05-07 18:47:16 +0800846void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800847{
Cheng C Yange50345b2019-04-02 17:26:15 +0800848 sensorTable = {{"power", "power/"},
849 {"curr", "current/"},
850 {"temp", "temperature/"},
851 {"in", "voltage/"},
852 {"fan", "fan_tach/"}};
853
854 labelMatch = {{"pin", PSUProperty("Input Power", 3000, 0, 6)},
855 {"pout1", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700856 {"pout2", PSUProperty("Output Power", 3000, 0, 6)},
857 {"pout3", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700858 {"power1", PSUProperty("Output Power", 3000, 0, 6)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530859 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6)},
Cheng C Yangbdbde962019-04-26 22:50:34 +0800860 {"vin", PSUProperty("Input Voltage", 300, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530861 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700862 {"vout1", PSUProperty("Output Voltage", 255, 0, 3)},
863 {"vout2", PSUProperty("Output Voltage", 255, 0, 3)},
864 {"vout3", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700865 {"vout4", PSUProperty("Output Voltage", 255, 0, 3)},
866 {"vout5", PSUProperty("Output Voltage", 255, 0, 3)},
867 {"vout6", PSUProperty("Output Voltage", 255, 0, 3)},
868 {"vout7", PSUProperty("Output Voltage", 255, 0, 3)},
869 {"vout8", PSUProperty("Output Voltage", 255, 0, 3)},
870 {"vout9", PSUProperty("Output Voltage", 255, 0, 3)},
871 {"vout10", PSUProperty("Output Voltage", 255, 0, 3)},
872 {"vout11", PSUProperty("Output Voltage", 255, 0, 3)},
873 {"vout12", PSUProperty("Output Voltage", 255, 0, 3)},
874 {"vout13", PSUProperty("Output Voltage", 255, 0, 3)},
875 {"vout14", PSUProperty("Output Voltage", 255, 0, 3)},
876 {"vout15", PSUProperty("Output Voltage", 255, 0, 3)},
877 {"vout16", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling32047ef2020-09-30 15:43:32 -0700878 {"vout17", PSUProperty("Output Voltage", 255, 0, 3)},
879 {"vout18", PSUProperty("Output Voltage", 255, 0, 3)},
880 {"vout19", PSUProperty("Output Voltage", 255, 0, 3)},
881 {"vout20", PSUProperty("Output Voltage", 255, 0, 3)},
882 {"vout21", PSUProperty("Output Voltage", 255, 0, 3)},
883 {"vout22", PSUProperty("Output Voltage", 255, 0, 3)},
884 {"vout23", PSUProperty("Output Voltage", 255, 0, 3)},
885 {"vout24", PSUProperty("Output Voltage", 255, 0, 3)},
886 {"vout25", PSUProperty("Output Voltage", 255, 0, 3)},
887 {"vout26", PSUProperty("Output Voltage", 255, 0, 3)},
888 {"vout27", PSUProperty("Output Voltage", 255, 0, 3)},
889 {"vout28", PSUProperty("Output Voltage", 255, 0, 3)},
890 {"vout29", PSUProperty("Output Voltage", 255, 0, 3)},
891 {"vout30", PSUProperty("Output Voltage", 255, 0, 3)},
892 {"vout31", PSUProperty("Output Voltage", 255, 0, 3)},
893 {"vout32", PSUProperty("Output Voltage", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700894 {"in1", PSUProperty("Output Voltage", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800895 {"iin", PSUProperty("Input Current", 20, 0, 3)},
896 {"iout1", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700897 {"iout2", PSUProperty("Output Current", 255, 0, 3)},
898 {"iout3", PSUProperty("Output Current", 255, 0, 3)},
Peter Lundgren48b44232020-01-30 16:02:11 -0800899 {"iout4", PSUProperty("Output Current", 255, 0, 3)},
900 {"iout5", PSUProperty("Output Current", 255, 0, 3)},
901 {"iout6", PSUProperty("Output Current", 255, 0, 3)},
902 {"iout7", PSUProperty("Output Current", 255, 0, 3)},
903 {"iout8", PSUProperty("Output Current", 255, 0, 3)},
904 {"iout9", PSUProperty("Output Current", 255, 0, 3)},
905 {"iout10", PSUProperty("Output Current", 255, 0, 3)},
906 {"iout11", PSUProperty("Output Current", 255, 0, 3)},
907 {"iout12", PSUProperty("Output Current", 255, 0, 3)},
908 {"iout13", PSUProperty("Output Current", 255, 0, 3)},
909 {"iout14", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700910 {"curr1", PSUProperty("Output Current", 255, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530911 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800912 {"temp1", PSUProperty("Temperature", 127, -128, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700913 {"temp2", PSUProperty("Temperature", 127, -128, 3)},
914 {"temp3", PSUProperty("Temperature", 127, -128, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700915 {"temp4", PSUProperty("Temperature", 127, -128, 3)},
916 {"temp5", PSUProperty("Temperature", 127, -128, 3)},
Alex Qiuf5709b42020-01-29 13:29:50 -0800917 {"temp6", PSUProperty("Temperature", 127, -128, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530918 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3)},
Cheng C Yang8dbb3952019-05-23 00:03:12 +0800919 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0)},
920 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +0800921
922 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +0800923
924 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
925 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
926
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800927 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
928 {"Failure", {"in2_alarm"}},
929 {"ACLost", {"in1_beep"}},
930 {"ConfigureError", {"in1_fault"}}};
931
932 groupEventMatch = {{"FanFault",
933 {{"fan1", {"fan1_alarm", "fan1_fault"}},
934 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +0800935}
936
James Feistb6c0b912019-07-09 12:21:44 -0700937int main()
Cheng C Yang209ec562019-03-12 16:37:44 +0800938{
939 boost::asio::io_service io;
940 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
941
942 systemBus->request_name("xyz.openbmc_project.PSUSensor");
943 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800944 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800945 auto sensorsChanged =
946 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +0800947
Cheng C Yang916360b2019-05-07 18:47:16 +0800948 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +0800949
Zhikui Ren23c96e72020-11-05 22:32:28 -0800950 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +0800951 boost::asio::deadline_timer filterTimer(io);
952 std::function<void(sdbusplus::message::message&)> eventHandler =
953 [&](sdbusplus::message::message& message) {
954 if (message.is_method_error())
955 {
956 std::cerr << "callback method error\n";
957 return;
958 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800959 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +0800960 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +0800961 filterTimer.async_wait([&](const boost::system::error_code& ec) {
962 if (ec == boost::asio::error::operation_aborted)
963 {
964 return;
965 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700966 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +0800967 {
968 std::cerr << "timer error\n";
969 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800970 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +0800971 });
972 };
973
974 for (const char* type : sensorTypes)
975 {
976 auto match = std::make_unique<sdbusplus::bus::match::match>(
977 static_cast<sdbusplus::bus::bus&>(*systemBus),
978 "type='signal',member='PropertiesChanged',path_namespace='" +
979 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
980 eventHandler);
981 matches.emplace_back(std::move(match));
982 }
983 io.run();
984}