blob: 3397ea83c8493e35a16c09557d5064d1ae997f65 [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
Gaurav Gandhi49585582020-12-22 01:01:24 +000042static constexpr std::array<const char*, 17> sensorTypes = {
Josh Lehan62084c82020-06-22 15:27:12 -070043 "xyz.openbmc_project.Configuration.ADM1272",
44 "xyz.openbmc_project.Configuration.ADM1278",
45 "xyz.openbmc_project.Configuration.INA219",
Devjit Gopalpurde65ef72019-10-30 04:47:35 -070046 "xyz.openbmc_project.Configuration.INA230",
Alex Qiu6690d8f2020-01-22 17:56:33 -080047 "xyz.openbmc_project.Configuration.ISL68137",
Jason Lingdfad1ff2020-07-29 15:43:11 -070048 "xyz.openbmc_project.Configuration.ISL68220",
Gaurav Gandhi49585582020-12-22 01:01:24 +000049 "xyz.openbmc_project.Configuration.ISL68223",
50 "xyz.openbmc_project.Configuration.ISL69243",
Alex Qiuf5709b42020-01-29 13:29:50 -080051 "xyz.openbmc_project.Configuration.MAX16601",
Gaurav Gandhi49585582020-12-22 01:01:24 +000052 "xyz.openbmc_project.Configuration.MAX20710",
Alex Qiu6690d8f2020-01-22 17:56:33 -080053 "xyz.openbmc_project.Configuration.MAX20730",
54 "xyz.openbmc_project.Configuration.MAX20734",
55 "xyz.openbmc_project.Configuration.MAX20796",
56 "xyz.openbmc_project.Configuration.MAX34451",
Josh Lehan62084c82020-06-22 15:27:12 -070057 "xyz.openbmc_project.Configuration.pmbus",
58 "xyz.openbmc_project.Configuration.PXE1610",
59 "xyz.openbmc_project.Configuration.RAA228228"};
Cheng C Yang209ec562019-03-12 16:37:44 +080060
Alex Qiu6690d8f2020-01-22 17:56:33 -080061static std::vector<std::string> pmbusNames = {
Gaurav Gandhi49585582020-12-22 01:01:24 +000062 "adm1272", "adm1278", "ina219", "ina230", "isl68137", "isl68220",
63 "isl68223", "isl69243", "max16601", "max20710", "max20730", "max20734",
64 "max20796", "max34451", "pmbus", "pxe1610", "raa228228"};
Josh Lehan0830c7b2019-10-08 16:35:09 -070065
Cheng C Yang209ec562019-03-12 16:37:44 +080066namespace fs = std::filesystem;
67
Yong Libf8b1da2020-04-15 16:32:50 +080068static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +080069 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +080070static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
71 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +080072static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
73 pwmSensors;
74static boost::container::flat_map<std::string, std::string> sensorTable;
75static boost::container::flat_map<std::string, PSUProperty> labelMatch;
76static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +080077static boost::container::flat_map<std::string, std::vector<std::string>>
78 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +080079static boost::container::flat_map<
80 std::string,
81 boost::container::flat_map<std::string, std::vector<std::string>>>
82 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +080083static boost::container::flat_map<std::string, std::vector<std::string>>
84 limitEventMatch;
85
Josh Lehan74d9bd92019-10-31 08:51:58 -070086static std::vector<PSUProperty> psuProperties;
87
Cheng C Yang58b2b532019-05-31 00:19:45 +080088// Function CheckEvent will check each attribute from eventMatch table in the
89// sysfs. If the attributes exists in sysfs, then store the complete path
90// of the attribute into eventPathList.
91void checkEvent(
92 const std::string& directory,
93 const boost::container::flat_map<std::string, std::vector<std::string>>&
94 eventMatch,
95 boost::container::flat_map<std::string, std::vector<std::string>>&
96 eventPathList)
97{
98 for (const auto& match : eventMatch)
99 {
100 const std::vector<std::string>& eventAttrs = match.second;
101 const std::string& eventName = match.first;
102 for (const auto& eventAttr : eventAttrs)
103 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700104 std::string eventPath = directory;
105 eventPath += "/";
106 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800107
108 std::ifstream eventFile(eventPath);
109 if (!eventFile.good())
110 {
111 continue;
112 }
113
114 eventPathList[eventName].push_back(eventPath);
115 }
116 }
117}
118
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800119// Check Group Events which contains more than one targets in each combine
120// events.
121void checkGroupEvent(
122 const std::string& directory,
123 const boost::container::flat_map<
124 std::string,
125 boost::container::flat_map<std::string, std::vector<std::string>>>&
126 groupEventMatch,
127 boost::container::flat_map<
128 std::string,
129 boost::container::flat_map<std::string, std::vector<std::string>>>&
130 groupEventPathList)
131{
132 for (const auto& match : groupEventMatch)
133 {
134 const std::string& groupEventName = match.first;
135 const boost::container::flat_map<std::string, std::vector<std::string>>
136 events = match.second;
137 boost::container::flat_map<std::string, std::vector<std::string>>
138 pathList;
139 for (const auto& match : events)
140 {
141 const std::string& eventName = match.first;
142 const std::vector<std::string>& eventAttrs = match.second;
143 for (const auto& eventAttr : eventAttrs)
144 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700145 std::string eventPath = directory;
146 eventPath += "/";
147 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800148 std::ifstream eventFile(eventPath);
149 if (!eventFile.good())
150 {
151 continue;
152 }
153
154 pathList[eventName].push_back(eventPath);
155 }
156 }
157 groupEventPathList[groupEventName] = pathList;
158 }
159}
160
Cheng C Yang58b2b532019-05-31 00:19:45 +0800161// Function checkEventLimits will check all the psu related xxx_input attributes
162// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
163// xxx_min_alarm exist, then store the existing paths of the alarm attributes
164// to eventPathList.
165void checkEventLimits(
166 const std::string& sensorPathStr,
167 const boost::container::flat_map<std::string, std::vector<std::string>>&
168 limitEventMatch,
169 boost::container::flat_map<std::string, std::vector<std::string>>&
170 eventPathList)
171{
172 for (const auto& limitMatch : limitEventMatch)
173 {
174 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
175 const std::string& eventName = limitMatch.first;
176 for (const auto& limitEventAttr : limitEventAttrs)
177 {
178 auto limitEventPath =
179 boost::replace_all_copy(sensorPathStr, "input", limitEventAttr);
180 std::ifstream eventFile(limitEventPath);
181 if (!eventFile.good())
182 {
183 continue;
184 }
185 eventPathList[eventName].push_back(limitEventPath);
186 }
187 }
188}
Cheng C Yang916360b2019-05-07 18:47:16 +0800189
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530190static void
191 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
192 const std::string& interfacePath,
193 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
194 sdbusplus::asio::object_server& objectServer,
195 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800196{
197 for (const auto& pwmName : pwmTable)
198 {
199 if (pwmName.first != labelHead)
200 {
201 continue;
202 }
203
204 const std::string& sensorPathStr = sensorPath.string();
205 const std::string& pwmPathStr =
206 boost::replace_all_copy(sensorPathStr, "input", "target");
207 std::ifstream pwmFile(pwmPathStr);
208 if (!pwmFile.good())
209 {
210 continue;
211 }
212
213 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
214 if (findPWMSensor != pwmSensors.end())
215 {
216 continue;
217 }
218
219 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530220 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
221 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800222 }
223}
224
Zhikui Ren23c96e72020-11-05 22:32:28 -0800225static void createSensorsCallback(
226 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
227 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
228 const ManagedObjectType& sensorConfigs,
229 const std::shared_ptr<boost::container::flat_set<std::string>>&
230 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800231{
Josh Lehan49cfba92019-10-08 16:50:42 -0700232 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800233 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800234
235 std::vector<fs::path> pmbusPaths;
236 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
237 {
238 std::cerr << "No PSU sensors in system\n";
239 return;
240 }
241
242 boost::container::flat_set<std::string> directories;
243 for (const auto& pmbusPath : pmbusPaths)
244 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800245 boost::container::flat_map<std::string, std::vector<std::string>>
246 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800247 boost::container::flat_map<
248 std::string,
249 boost::container::flat_map<std::string, std::vector<std::string>>>
250 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800251
252 std::ifstream nameFile(pmbusPath);
253 if (!nameFile.good())
254 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700255 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800256 continue;
257 }
258
259 std::string pmbusName;
260 std::getline(nameFile, pmbusName);
261 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700262
263 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
264 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800265 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700266 // To avoid this error message, add your driver name to
267 // the pmbusNames vector at the top of this file.
268 std::cerr << "Driver name " << pmbusName
269 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800270 continue;
271 }
272
273 const std::string* psuName;
Cheng C Yang209ec562019-03-12 16:37:44 +0800274 auto directory = pmbusPath.parent_path();
275
276 auto ret = directories.insert(directory.string());
277 if (!ret.second)
278 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700279 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800280 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800281 }
282
James Feistb6c0b912019-07-09 12:21:44 -0700283 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800284 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700285 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800286 if (findHyphen == std::string::npos)
287 {
288 std::cerr << "found bad device" << deviceName << "\n";
289 continue;
290 }
291 std::string busStr = deviceName.substr(0, findHyphen);
292 std::string addrStr = deviceName.substr(findHyphen + 1);
293
294 size_t bus = 0;
295 size_t addr = 0;
296
297 try
298 {
299 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700300 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800301 }
James Feistb6c0b912019-07-09 12:21:44 -0700302 catch (std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800303 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700304 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
305 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800306 continue;
307 }
308
Cheng C Yang209ec562019-03-12 16:37:44 +0800309 const std::pair<std::string, boost::container::flat_map<
310 std::string, BasicVariantType>>*
311 baseConfig = nullptr;
312 const SensorData* sensorData = nullptr;
313 const std::string* interfacePath = nullptr;
314 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800315 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800316
317 for (const std::pair<sdbusplus::message::object_path, SensorData>&
318 sensor : sensorConfigs)
319 {
320 sensorData = &(sensor.second);
321 for (const char* type : sensorTypes)
322 {
323 auto sensorBase = sensorData->find(type);
324 if (sensorBase != sensorData->end())
325 {
326 baseConfig = &(*sensorBase);
327 sensorType = type;
328 break;
329 }
330 }
331 if (baseConfig == nullptr)
332 {
333 std::cerr << "error finding base configuration for "
334 << deviceName << "\n";
335 continue;
336 }
337
338 auto configBus = baseConfig->second.find("Bus");
339 auto configAddress = baseConfig->second.find("Address");
340
341 if (configBus == baseConfig->second.end() ||
342 configAddress == baseConfig->second.end())
343 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800344 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800345 continue;
346 }
347
Cheng C Yang58b2b532019-05-31 00:19:45 +0800348 const uint64_t* confBus;
349 const uint64_t* confAddr;
350 if (!(confBus = std::get_if<uint64_t>(&(configBus->second))) ||
351 !(confAddr = std::get_if<uint64_t>(&(configAddress->second))))
352 {
353 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700354 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800355 continue;
356 }
357
358 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800359 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700360 std::cerr << "Configuration skipping " << *confBus << "-"
361 << *confAddr << " because not " << bus << "-" << addr
362 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800363 continue;
364 }
365
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800366 std::vector<thresholds::Threshold> confThresholds;
367 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
368 {
369 std::cerr << "error populating totoal thresholds\n";
370 }
371 thresholdConfSize = confThresholds.size();
372
Cheng C Yang209ec562019-03-12 16:37:44 +0800373 interfacePath = &(sensor.first.str);
374 break;
375 }
376 if (interfacePath == nullptr)
377 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700378 // To avoid this error message, add your export map entry,
379 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800380 std::cerr << "failed to find match for " << deviceName << "\n";
381 continue;
382 }
383
Cheng C Yange50345b2019-04-02 17:26:15 +0800384 auto findPSUName = baseConfig->second.find("Name");
385 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800386 {
387 std::cerr << "could not determine configuration name for "
388 << deviceName << "\n";
389 continue;
390 }
391
Cheng C Yang58b2b532019-05-31 00:19:45 +0800392 if (!(psuName = std::get_if<std::string>(&(findPSUName->second))))
393 {
394 std::cerr << "Cannot find psu name, invalid configuration\n";
395 continue;
396 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800397
398 // on rescans, only update sensors we were signaled by
399 if (!firstScan)
400 {
401 std::string psuNameStr = "/" + *psuName;
402 auto it =
403 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
404 [psuNameStr](std::string& s) {
405 return boost::ends_with(s, psuNameStr);
406 });
407
408 if (it == sensorsChanged->end())
409 {
410 continue;
411 }
412 sensorsChanged->erase(it);
413 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800414 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800415 checkGroupEvent(directory.string(), groupEventMatch,
416 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800417
Vijay Khemka996bad12019-05-28 15:15:16 -0700418 /* Check if there are more sensors in the same interface */
419 int i = 1;
420 std::vector<std::string> psuNames;
421 do
422 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700423 // Individual string fields: Name, Name1, Name2, Name3, ...
Vijay Khemka996bad12019-05-28 15:15:16 -0700424 psuNames.push_back(std::get<std::string>(findPSUName->second));
425 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
426 } while (findPSUName != baseConfig->second.end());
427
Cheng C Yange50345b2019-04-02 17:26:15 +0800428 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700429 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800430 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800431 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800432 continue;
433 }
434
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530435 /* read max value in sysfs for in, curr, power, temp, ... */
436 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
437 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700438 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530439 {
440 std::cerr << "No max name in PSU \n";
441 }
442 }
443
Vijay Khemka996bad12019-05-28 15:15:16 -0700444 /* Find array of labels to be exposed if it is defined in config */
445 std::vector<std::string> findLabels;
446 auto findLabelObj = baseConfig->second.find("Labels");
447 if (findLabelObj != baseConfig->second.end())
448 {
449 findLabels =
450 std::get<std::vector<std::string>>(findLabelObj->second);
451 }
452
Jason Ling5747fab2019-10-02 16:46:23 -0700453 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
454 std::smatch matches;
455
Cheng C Yange50345b2019-04-02 17:26:15 +0800456 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800457 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530458 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800459 std::string labelHead;
460 std::string sensorPathStr = sensorPath.string();
461 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700462 std::string sensorNameSubStr{""};
463 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
464 {
Josh Lehan06494452019-10-31 09:49:16 -0700465 // hwmon *_input filename without number:
466 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700467 sensorNameSubStr = matches[1];
468 }
469 else
470 {
Josh Lehan06494452019-10-31 09:49:16 -0700471 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700472 << sensorNameStr;
473 continue;
474 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800475
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530476 std::string labelPath;
477
478 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700479 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530480 if (pos != std::string::npos)
481 {
482
483 std::string sensorPathStrMax = sensorPathStr.substr(pos);
484 if (sensorPathStrMax.compare("_max") == 0)
485 {
486 labelPath =
487 boost::replace_all_copy(sensorPathStr, "max", "label");
488 maxLabel = true;
489 }
490 else
491 {
492 labelPath = boost::replace_all_copy(sensorPathStr, "input",
493 "label");
494 maxLabel = false;
495 }
496 }
497 else
498 {
499 continue;
500 }
501
Cheng C Yangecba9de2019-09-12 23:41:50 +0800502 std::ifstream labelFile(labelPath);
503 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800504 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700505 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800506 {
507 std::cerr << "Input file " << sensorPath
508 << " has no corresponding label file\n";
509 }
Josh Lehan06494452019-10-31 09:49:16 -0700510 // hwmon *_input filename with number:
511 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700512 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800513 }
514 else
515 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800516 std::string label;
517 std::getline(labelFile, label);
518 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800519 auto findSensor = sensors.find(label);
520 if (findSensor != sensors.end())
521 {
522 continue;
523 }
524
Josh Lehan06494452019-10-31 09:49:16 -0700525 // hwmon corresponding *_label file contents:
526 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700527 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800528 }
529
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530530 /* append "max" for labelMatch */
531 if (maxLabel)
532 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700533 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530534 }
535
Ed Tanous8a57ec02020-10-09 12:46:52 -0700536 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700537 {
538 std::cerr << "Sensor type=\"" << sensorNameSubStr
539 << "\" label=\"" << labelHead << "\"\n";
540 }
541
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530542 checkPWMSensor(sensorPath, labelHead, *interfacePath,
543 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800544
Vijay Khemka996bad12019-05-28 15:15:16 -0700545 if (!findLabels.empty())
546 {
547 /* Check if this labelHead is enabled in config file */
548 if (std::find(findLabels.begin(), findLabels.end(),
549 labelHead) == findLabels.end())
550 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700551 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800552 {
553 std::cerr << "could not find " << labelHead
554 << " in the Labels list\n";
555 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700556 continue;
557 }
558 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800559
Cheng C Yange50345b2019-04-02 17:26:15 +0800560 auto findProperty = labelMatch.find(labelHead);
561 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800562 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700563 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700564 {
565 std::cerr << "Could not find matching default property for "
566 << labelHead << "\n";
567 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800568 continue;
569 }
570
Josh Lehan74d9bd92019-10-31 08:51:58 -0700571 // Protect the hardcoded labelMatch list from changes,
572 // by making a copy and modifying that instead.
573 // Avoid bleedthrough of one device's customizations to
574 // the next device, as each should be independently customizable.
575 psuProperties.push_back(findProperty->second);
576 auto psuProperty = psuProperties.rbegin();
577
578 // Use label head as prefix for reading from config file,
579 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
580 std::string keyName = labelHead + "_Name";
581 std::string keyScale = labelHead + "_Scale";
582 std::string keyMin = labelHead + "_Min";
583 std::string keyMax = labelHead + "_Max";
584
585 bool customizedName = false;
586 auto findCustomName = baseConfig->second.find(keyName);
587 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700588 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700589 try
590 {
591 psuProperty->labelTypeName = std::visit(
592 VariantToStringVisitor(), findCustomName->second);
593 }
594 catch (std::invalid_argument&)
595 {
596 std::cerr << "Unable to parse " << keyName << "\n";
597 continue;
598 }
599
600 // All strings are valid, including empty string
601 customizedName = true;
602 }
603
604 bool customizedScale = false;
605 auto findCustomScale = baseConfig->second.find(keyScale);
606 if (findCustomScale != baseConfig->second.end())
607 {
608 try
609 {
610 psuProperty->sensorScaleFactor = std::visit(
611 VariantToUnsignedIntVisitor(), findCustomScale->second);
612 }
613 catch (std::invalid_argument&)
614 {
615 std::cerr << "Unable to parse " << keyScale << "\n";
616 continue;
617 }
618
619 // Avoid later division by zero
620 if (psuProperty->sensorScaleFactor > 0)
621 {
622 customizedScale = true;
623 }
624 else
625 {
626 std::cerr << "Unable to accept " << keyScale << "\n";
627 continue;
628 }
629 }
630
631 auto findCustomMin = baseConfig->second.find(keyMin);
632 if (findCustomMin != baseConfig->second.end())
633 {
634 try
635 {
636 psuProperty->minReading = std::visit(
637 VariantToDoubleVisitor(), findCustomMin->second);
638 }
639 catch (std::invalid_argument&)
640 {
641 std::cerr << "Unable to parse " << keyMin << "\n";
642 continue;
643 }
644 }
645
646 auto findCustomMax = baseConfig->second.find(keyMax);
647 if (findCustomMax != baseConfig->second.end())
648 {
649 try
650 {
651 psuProperty->maxReading = std::visit(
652 VariantToDoubleVisitor(), findCustomMax->second);
653 }
654 catch (std::invalid_argument&)
655 {
656 std::cerr << "Unable to parse " << keyMax << "\n";
657 continue;
658 }
659 }
660
661 if (!(psuProperty->minReading < psuProperty->maxReading))
662 {
663 std::cerr << "Min must be less than Max\n";
664 continue;
665 }
666
667 // If the sensor name is being customized by config file,
668 // then prefix/suffix composition becomes not necessary,
669 // and in fact not wanted, because it gets in the way.
670 std::string psuNameFromIndex;
671 if (!customizedName)
672 {
673 /* Find out sensor name index for this label */
674 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500675 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700676 if (std::regex_search(labelHead, matches, rgx))
677 {
678 nameIndex = std::stoi(matches[1]);
679
680 // Decrement to preserve alignment, because hwmon
681 // human-readable filenames and labels use 1-based
682 // numbering, but the "Name", "Name1", "Name2", etc. naming
683 // convention (the psuNames vector) uses 0-based numbering.
684 if (nameIndex > 0)
685 {
686 --nameIndex;
687 }
688 }
689 else
690 {
691 nameIndex = 0;
692 }
693
694 if (psuNames.size() <= nameIndex)
695 {
696 std::cerr << "Could not pair " << labelHead
697 << " with a Name field\n";
698 continue;
699 }
700
701 psuNameFromIndex = psuNames[nameIndex];
702
Ed Tanous8a57ec02020-10-09 12:46:52 -0700703 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700704 {
705 std::cerr << "Sensor label head " << labelHead
706 << " paired with " << psuNameFromIndex
707 << " at index " << nameIndex << "\n";
708 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700709 }
710
Cheng C Yang58b2b532019-05-31 00:19:45 +0800711 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
712
Josh Lehan74d9bd92019-10-31 08:51:58 -0700713 // Similarly, if sensor scaling factor is being customized,
714 // then the below power-of-10 constraint becomes unnecessary,
715 // as config should be able to specify an arbitrary divisor.
716 unsigned int factor = psuProperty->sensorScaleFactor;
717 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700718 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700719 // Preserve existing usage of hardcoded labelMatch table below
720 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700721
Josh Lehan74d9bd92019-10-31 08:51:58 -0700722 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700723 char firstChar =
724 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700725 std::string strScaleFactor =
726 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
727
728 // Preserve existing configs by accepting earlier syntax,
729 // example CurrScaleFactor, PowerScaleFactor, ...
730 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
731 if (findScaleFactor != baseConfig->second.end())
732 {
733 factor = std::visit(VariantToIntVisitor(),
734 findScaleFactor->second);
735 }
736
Ed Tanous8a57ec02020-10-09 12:46:52 -0700737 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700738 {
739 std::cerr << "Sensor scaling factor " << factor
740 << " string " << strScaleFactor << "\n";
741 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700742 }
743
Vijay Khemka996bad12019-05-28 15:15:16 -0700744 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530745 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
746 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800747 {
James Feist17ab6e02019-06-25 12:28:13 -0700748 std::cerr << "error populating thresholds for "
749 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800750 }
751
752 auto findSensorType = sensorTable.find(sensorNameSubStr);
753 if (findSensorType == sensorTable.end())
754 {
Jason Ling5747fab2019-10-02 16:46:23 -0700755 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700756 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800757 continue;
758 }
759
Ed Tanous8a57ec02020-10-09 12:46:52 -0700760 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700761 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700762 std::cerr << "Sensor properties: Name \""
763 << psuProperty->labelTypeName << "\" Scale "
764 << psuProperty->sensorScaleFactor << " Min "
765 << psuProperty->minReading << " Max "
766 << psuProperty->maxReading << "\n";
767 }
768
769 std::string sensorName = psuProperty->labelTypeName;
770 if (customizedName)
771 {
772 if (sensorName.empty())
773 {
774 // Allow selective disabling of an individual sensor,
775 // by customizing its name to an empty string.
776 std::cerr << "Sensor disabled, empty string\n";
777 continue;
778 }
779 }
780 else
781 {
782 // Sensor name not customized, do prefix/suffix composition,
783 // preserving default behavior by using psuNameFromIndex.
784 sensorName =
785 psuNameFromIndex + " " + psuProperty->labelTypeName;
786 }
787
Ed Tanous8a57ec02020-10-09 12:46:52 -0700788 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700789 {
790 std::cerr << "Sensor name \"" << sensorName << "\" path \""
791 << sensorPathStr << "\" type \"" << sensorType
792 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700793 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800794 // destruct existing one first if already created
795 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800796 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800797 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800798 sensorName, std::move(sensorThresholds), *interfacePath,
Josh Lehan74d9bd92019-10-31 08:51:58 -0700799 findSensorType->second, factor, psuProperty->maxReading,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800800 psuProperty->minReading, labelHead, thresholdConfSize);
Yong Libf8b1da2020-04-15 16:32:50 +0800801 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700802 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700803 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700804 {
805 std::cerr << "Created " << numCreated << " sensors so far\n";
806 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800807 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800808
809 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800810 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800811 combineEvents[*psuName + "OperationalStatus"] =
Yong Li3046a022020-04-03 13:01:02 +0800812 std::make_unique<PSUCombineEvent>(
813 objectServer, dbusConnection, io, *psuName, eventPathList,
814 groupEventPathList, "OperationalStatus");
Cheng C Yang209ec562019-03-12 16:37:44 +0800815 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700816
Ed Tanous8a57ec02020-10-09 12:46:52 -0700817 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700818 {
819 std::cerr << "Created total of " << numCreated << " sensors\n";
820 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800821 return;
822}
823
Zhikui Ren23c96e72020-11-05 22:32:28 -0800824void createSensors(
825 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
826 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
827 const std::shared_ptr<boost::container::flat_set<std::string>>&
828 sensorsChanged)
829{
830 auto getter = std::make_shared<GetSensorConfiguration>(
831 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
832 const ManagedObjectType& sensorConfigs) {
833 createSensorsCallback(io, objectServer, dbusConnection,
834 sensorConfigs, sensorsChanged);
835 });
836 getter->getConfiguration(
837 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
838}
839
Cheng C Yang916360b2019-05-07 18:47:16 +0800840void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800841{
Cheng C Yange50345b2019-04-02 17:26:15 +0800842 sensorTable = {{"power", "power/"},
843 {"curr", "current/"},
844 {"temp", "temperature/"},
845 {"in", "voltage/"},
846 {"fan", "fan_tach/"}};
847
848 labelMatch = {{"pin", PSUProperty("Input Power", 3000, 0, 6)},
849 {"pout1", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700850 {"pout2", PSUProperty("Output Power", 3000, 0, 6)},
851 {"pout3", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700852 {"power1", PSUProperty("Output Power", 3000, 0, 6)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530853 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6)},
Cheng C Yangbdbde962019-04-26 22:50:34 +0800854 {"vin", PSUProperty("Input Voltage", 300, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530855 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700856 {"vout1", PSUProperty("Output Voltage", 255, 0, 3)},
857 {"vout2", PSUProperty("Output Voltage", 255, 0, 3)},
858 {"vout3", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700859 {"vout4", PSUProperty("Output Voltage", 255, 0, 3)},
860 {"vout5", PSUProperty("Output Voltage", 255, 0, 3)},
861 {"vout6", PSUProperty("Output Voltage", 255, 0, 3)},
862 {"vout7", PSUProperty("Output Voltage", 255, 0, 3)},
863 {"vout8", PSUProperty("Output Voltage", 255, 0, 3)},
864 {"vout9", PSUProperty("Output Voltage", 255, 0, 3)},
865 {"vout10", PSUProperty("Output Voltage", 255, 0, 3)},
866 {"vout11", PSUProperty("Output Voltage", 255, 0, 3)},
867 {"vout12", PSUProperty("Output Voltage", 255, 0, 3)},
868 {"vout13", PSUProperty("Output Voltage", 255, 0, 3)},
869 {"vout14", PSUProperty("Output Voltage", 255, 0, 3)},
870 {"vout15", PSUProperty("Output Voltage", 255, 0, 3)},
871 {"vout16", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling32047ef2020-09-30 15:43:32 -0700872 {"vout17", PSUProperty("Output Voltage", 255, 0, 3)},
873 {"vout18", PSUProperty("Output Voltage", 255, 0, 3)},
874 {"vout19", PSUProperty("Output Voltage", 255, 0, 3)},
875 {"vout20", PSUProperty("Output Voltage", 255, 0, 3)},
876 {"vout21", PSUProperty("Output Voltage", 255, 0, 3)},
877 {"vout22", PSUProperty("Output Voltage", 255, 0, 3)},
878 {"vout23", PSUProperty("Output Voltage", 255, 0, 3)},
879 {"vout24", PSUProperty("Output Voltage", 255, 0, 3)},
880 {"vout25", PSUProperty("Output Voltage", 255, 0, 3)},
881 {"vout26", PSUProperty("Output Voltage", 255, 0, 3)},
882 {"vout27", PSUProperty("Output Voltage", 255, 0, 3)},
883 {"vout28", PSUProperty("Output Voltage", 255, 0, 3)},
884 {"vout29", PSUProperty("Output Voltage", 255, 0, 3)},
885 {"vout30", PSUProperty("Output Voltage", 255, 0, 3)},
886 {"vout31", PSUProperty("Output Voltage", 255, 0, 3)},
887 {"vout32", PSUProperty("Output Voltage", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700888 {"in1", PSUProperty("Output Voltage", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800889 {"iin", PSUProperty("Input Current", 20, 0, 3)},
890 {"iout1", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700891 {"iout2", PSUProperty("Output Current", 255, 0, 3)},
892 {"iout3", PSUProperty("Output Current", 255, 0, 3)},
Peter Lundgren48b44232020-01-30 16:02:11 -0800893 {"iout4", PSUProperty("Output Current", 255, 0, 3)},
894 {"iout5", PSUProperty("Output Current", 255, 0, 3)},
895 {"iout6", PSUProperty("Output Current", 255, 0, 3)},
896 {"iout7", PSUProperty("Output Current", 255, 0, 3)},
897 {"iout8", PSUProperty("Output Current", 255, 0, 3)},
898 {"iout9", PSUProperty("Output Current", 255, 0, 3)},
899 {"iout10", PSUProperty("Output Current", 255, 0, 3)},
900 {"iout11", PSUProperty("Output Current", 255, 0, 3)},
901 {"iout12", PSUProperty("Output Current", 255, 0, 3)},
902 {"iout13", PSUProperty("Output Current", 255, 0, 3)},
903 {"iout14", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700904 {"curr1", PSUProperty("Output Current", 255, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530905 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800906 {"temp1", PSUProperty("Temperature", 127, -128, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700907 {"temp2", PSUProperty("Temperature", 127, -128, 3)},
908 {"temp3", PSUProperty("Temperature", 127, -128, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700909 {"temp4", PSUProperty("Temperature", 127, -128, 3)},
910 {"temp5", PSUProperty("Temperature", 127, -128, 3)},
Alex Qiuf5709b42020-01-29 13:29:50 -0800911 {"temp6", PSUProperty("Temperature", 127, -128, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530912 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3)},
Cheng C Yang8dbb3952019-05-23 00:03:12 +0800913 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0)},
914 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +0800915
916 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +0800917
918 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
919 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
920
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800921 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
922 {"Failure", {"in2_alarm"}},
923 {"ACLost", {"in1_beep"}},
924 {"ConfigureError", {"in1_fault"}}};
925
926 groupEventMatch = {{"FanFault",
927 {{"fan1", {"fan1_alarm", "fan1_fault"}},
928 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +0800929}
930
James Feistb6c0b912019-07-09 12:21:44 -0700931int main()
Cheng C Yang209ec562019-03-12 16:37:44 +0800932{
933 boost::asio::io_service io;
934 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
935
936 systemBus->request_name("xyz.openbmc_project.PSUSensor");
937 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800938 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800939 auto sensorsChanged =
940 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +0800941
Cheng C Yang916360b2019-05-07 18:47:16 +0800942 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +0800943
Zhikui Ren23c96e72020-11-05 22:32:28 -0800944 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +0800945 boost::asio::deadline_timer filterTimer(io);
946 std::function<void(sdbusplus::message::message&)> eventHandler =
947 [&](sdbusplus::message::message& message) {
948 if (message.is_method_error())
949 {
950 std::cerr << "callback method error\n";
951 return;
952 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800953 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +0800954 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +0800955 filterTimer.async_wait([&](const boost::system::error_code& ec) {
956 if (ec == boost::asio::error::operation_aborted)
957 {
958 return;
959 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700960 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +0800961 {
962 std::cerr << "timer error\n";
963 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800964 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +0800965 });
966 };
967
968 for (const char* type : sensorTypes)
969 {
970 auto match = std::make_unique<sdbusplus::bus::match::match>(
971 static_cast<sdbusplus::bus::bus&>(*systemBus),
972 "type='signal',member='PropertiesChanged',path_namespace='" +
973 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
974 eventHandler);
975 matches.emplace_back(std::move(match));
976 }
977 io.run();
978}