blob: f37a3dc74c5597296918bf5b59c07f0eb5a2e4b6 [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
Paul Fertser678f53b2021-04-06 07:06:28 +000043static constexpr std::array<const char*, 23> sensorTypes = {
Josh Lehan62084c82020-06-22 15:27:12 -070044 "xyz.openbmc_project.Configuration.ADM1272",
Paul Fertser678f53b2021-04-06 07:06:28 +000045 "xyz.openbmc_project.Configuration.ADM1275",
Josh Lehan62084c82020-06-22 15:27:12 -070046 "xyz.openbmc_project.Configuration.ADM1278",
Jeff Lina0683a82021-02-22 13:56:55 +080047 "xyz.openbmc_project.Configuration.DPS800",
Josh Lehan62084c82020-06-22 15:27:12 -070048 "xyz.openbmc_project.Configuration.INA219",
Devjit Gopalpurde65ef72019-10-30 04:47:35 -070049 "xyz.openbmc_project.Configuration.INA230",
Alex Qiu6690d8f2020-01-22 17:56:33 -080050 "xyz.openbmc_project.Configuration.ISL68137",
Jason Lingdfad1ff2020-07-29 15:43:11 -070051 "xyz.openbmc_project.Configuration.ISL68220",
Gaurav Gandhi49585582020-12-22 01:01:24 +000052 "xyz.openbmc_project.Configuration.ISL68223",
53 "xyz.openbmc_project.Configuration.ISL69243",
Jeff Lina0683a82021-02-22 13:56:55 +080054 "xyz.openbmc_project.Configuration.ISL69260",
Alex Qiuf5709b42020-01-29 13:29:50 -080055 "xyz.openbmc_project.Configuration.MAX16601",
Gaurav Gandhi49585582020-12-22 01:01:24 +000056 "xyz.openbmc_project.Configuration.MAX20710",
Alex Qiu6690d8f2020-01-22 17:56:33 -080057 "xyz.openbmc_project.Configuration.MAX20730",
58 "xyz.openbmc_project.Configuration.MAX20734",
59 "xyz.openbmc_project.Configuration.MAX20796",
60 "xyz.openbmc_project.Configuration.MAX34451",
Josh Lehan62084c82020-06-22 15:27:12 -070061 "xyz.openbmc_project.Configuration.pmbus",
62 "xyz.openbmc_project.Configuration.PXE1610",
Jeff Lina0683a82021-02-22 13:56:55 +080063 "xyz.openbmc_project.Configuration.RAA228000",
64 "xyz.openbmc_project.Configuration.RAA228228",
65 "xyz.openbmc_project.Configuration.RAA229004",
66 "xyz.openbmc_project.Configuration.TPS546D24"};
Cheng C Yang209ec562019-03-12 16:37:44 +080067
Alex Qiu6690d8f2020-01-22 17:56:33 -080068static std::vector<std::string> pmbusNames = {
Paul Fertser678f53b2021-04-06 07:06:28 +000069 "adm1272", "adm1275", "adm1278", "dps800", "ina219", "ina230",
70 "isl68137", "isl68220", "isl68223", "isl69243", "isl69260", "max16601",
71 "max20710", "max20730", "max20734", "max20796", "max34451", "pmbus",
72 "pxe1610", "raa228000", "raa228228", "raa229004", "tps546d24"};
Josh Lehan0830c7b2019-10-08 16:35:09 -070073
Cheng C Yang209ec562019-03-12 16:37:44 +080074namespace fs = std::filesystem;
75
Yong Libf8b1da2020-04-15 16:32:50 +080076static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +080077 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +080078static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
79 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +080080static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
81 pwmSensors;
82static boost::container::flat_map<std::string, std::string> sensorTable;
83static boost::container::flat_map<std::string, PSUProperty> labelMatch;
84static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +080085static boost::container::flat_map<std::string, std::vector<std::string>>
86 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +080087static boost::container::flat_map<
88 std::string,
89 boost::container::flat_map<std::string, std::vector<std::string>>>
90 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +080091static boost::container::flat_map<std::string, std::vector<std::string>>
92 limitEventMatch;
93
Josh Lehan74d9bd92019-10-31 08:51:58 -070094static std::vector<PSUProperty> psuProperties;
95
Cheng C Yang58b2b532019-05-31 00:19:45 +080096// Function CheckEvent will check each attribute from eventMatch table in the
97// sysfs. If the attributes exists in sysfs, then store the complete path
98// of the attribute into eventPathList.
99void checkEvent(
100 const std::string& directory,
101 const boost::container::flat_map<std::string, std::vector<std::string>>&
102 eventMatch,
103 boost::container::flat_map<std::string, std::vector<std::string>>&
104 eventPathList)
105{
106 for (const auto& match : eventMatch)
107 {
108 const std::vector<std::string>& eventAttrs = match.second;
109 const std::string& eventName = match.first;
110 for (const auto& eventAttr : eventAttrs)
111 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700112 std::string eventPath = directory;
113 eventPath += "/";
114 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800115
116 std::ifstream eventFile(eventPath);
117 if (!eventFile.good())
118 {
119 continue;
120 }
121
122 eventPathList[eventName].push_back(eventPath);
123 }
124 }
125}
126
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800127// Check Group Events which contains more than one targets in each combine
128// events.
129void checkGroupEvent(
130 const std::string& directory,
131 const boost::container::flat_map<
132 std::string,
133 boost::container::flat_map<std::string, std::vector<std::string>>>&
134 groupEventMatch,
135 boost::container::flat_map<
136 std::string,
137 boost::container::flat_map<std::string, std::vector<std::string>>>&
138 groupEventPathList)
139{
140 for (const auto& match : groupEventMatch)
141 {
142 const std::string& groupEventName = match.first;
143 const boost::container::flat_map<std::string, std::vector<std::string>>
144 events = match.second;
145 boost::container::flat_map<std::string, std::vector<std::string>>
146 pathList;
147 for (const auto& match : events)
148 {
149 const std::string& eventName = match.first;
150 const std::vector<std::string>& eventAttrs = match.second;
151 for (const auto& eventAttr : eventAttrs)
152 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700153 std::string eventPath = directory;
154 eventPath += "/";
155 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800156 std::ifstream eventFile(eventPath);
157 if (!eventFile.good())
158 {
159 continue;
160 }
161
162 pathList[eventName].push_back(eventPath);
163 }
164 }
165 groupEventPathList[groupEventName] = pathList;
166 }
167}
168
Cheng C Yang58b2b532019-05-31 00:19:45 +0800169// Function checkEventLimits will check all the psu related xxx_input attributes
170// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
171// xxx_min_alarm exist, then store the existing paths of the alarm attributes
172// to eventPathList.
173void checkEventLimits(
174 const std::string& sensorPathStr,
175 const boost::container::flat_map<std::string, std::vector<std::string>>&
176 limitEventMatch,
177 boost::container::flat_map<std::string, std::vector<std::string>>&
178 eventPathList)
179{
Lei YUa2c7cea2020-12-23 14:07:28 +0800180 auto attributePartPos = sensorPathStr.find_last_of('_');
181 if (attributePartPos == std::string::npos)
182 {
183 // There is no '_' in the string, skip it
184 return;
185 }
186 auto attributePart =
187 std::string_view(sensorPathStr).substr(attributePartPos + 1);
188 if (attributePart != "input")
189 {
190 // If the sensor is not xxx_input, skip it
191 return;
192 }
193
194 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800195 for (const auto& limitMatch : limitEventMatch)
196 {
197 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
198 const std::string& eventName = limitMatch.first;
199 for (const auto& limitEventAttr : limitEventAttrs)
200 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800201 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800202 std::ifstream eventFile(limitEventPath);
203 if (!eventFile.good())
204 {
205 continue;
206 }
207 eventPathList[eventName].push_back(limitEventPath);
208 }
209 }
210}
Cheng C Yang916360b2019-05-07 18:47:16 +0800211
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530212static void
213 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
214 const std::string& interfacePath,
215 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
216 sdbusplus::asio::object_server& objectServer,
217 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800218{
219 for (const auto& pwmName : pwmTable)
220 {
221 if (pwmName.first != labelHead)
222 {
223 continue;
224 }
225
226 const std::string& sensorPathStr = sensorPath.string();
227 const std::string& pwmPathStr =
228 boost::replace_all_copy(sensorPathStr, "input", "target");
229 std::ifstream pwmFile(pwmPathStr);
230 if (!pwmFile.good())
231 {
232 continue;
233 }
234
235 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
236 if (findPWMSensor != pwmSensors.end())
237 {
238 continue;
239 }
240
241 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530242 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
243 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800244 }
245}
246
Zhikui Ren23c96e72020-11-05 22:32:28 -0800247static void createSensorsCallback(
248 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
249 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
250 const ManagedObjectType& sensorConfigs,
251 const std::shared_ptr<boost::container::flat_set<std::string>>&
252 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800253{
Josh Lehan49cfba92019-10-08 16:50:42 -0700254 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800255 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800256
257 std::vector<fs::path> pmbusPaths;
258 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
259 {
260 std::cerr << "No PSU sensors in system\n";
261 return;
262 }
263
264 boost::container::flat_set<std::string> directories;
265 for (const auto& pmbusPath : pmbusPaths)
266 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800267 boost::container::flat_map<std::string, std::vector<std::string>>
268 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800269 boost::container::flat_map<
270 std::string,
271 boost::container::flat_map<std::string, std::vector<std::string>>>
272 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800273
274 std::ifstream nameFile(pmbusPath);
275 if (!nameFile.good())
276 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700277 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800278 continue;
279 }
280
281 std::string pmbusName;
282 std::getline(nameFile, pmbusName);
283 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700284
285 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
286 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800287 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700288 // To avoid this error message, add your driver name to
289 // the pmbusNames vector at the top of this file.
290 std::cerr << "Driver name " << pmbusName
291 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800292 continue;
293 }
294
295 const std::string* psuName;
Cheng C Yang209ec562019-03-12 16:37:44 +0800296 auto directory = pmbusPath.parent_path();
297
298 auto ret = directories.insert(directory.string());
299 if (!ret.second)
300 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700301 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800302 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800303 }
304
James Feistb6c0b912019-07-09 12:21:44 -0700305 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800306 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700307 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800308 if (findHyphen == std::string::npos)
309 {
310 std::cerr << "found bad device" << deviceName << "\n";
311 continue;
312 }
313 std::string busStr = deviceName.substr(0, findHyphen);
314 std::string addrStr = deviceName.substr(findHyphen + 1);
315
316 size_t bus = 0;
317 size_t addr = 0;
318
319 try
320 {
321 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700322 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800323 }
James Feistb6c0b912019-07-09 12:21:44 -0700324 catch (std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800325 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700326 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
327 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800328 continue;
329 }
330
Cheng C Yang209ec562019-03-12 16:37:44 +0800331 const std::pair<std::string, boost::container::flat_map<
332 std::string, BasicVariantType>>*
333 baseConfig = nullptr;
334 const SensorData* sensorData = nullptr;
335 const std::string* interfacePath = nullptr;
336 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800337 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800338
339 for (const std::pair<sdbusplus::message::object_path, SensorData>&
340 sensor : sensorConfigs)
341 {
342 sensorData = &(sensor.second);
343 for (const char* type : sensorTypes)
344 {
345 auto sensorBase = sensorData->find(type);
346 if (sensorBase != sensorData->end())
347 {
348 baseConfig = &(*sensorBase);
349 sensorType = type;
350 break;
351 }
352 }
353 if (baseConfig == nullptr)
354 {
355 std::cerr << "error finding base configuration for "
356 << deviceName << "\n";
357 continue;
358 }
359
360 auto configBus = baseConfig->second.find("Bus");
361 auto configAddress = baseConfig->second.find("Address");
362
363 if (configBus == baseConfig->second.end() ||
364 configAddress == baseConfig->second.end())
365 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800366 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800367 continue;
368 }
369
Cheng C Yang58b2b532019-05-31 00:19:45 +0800370 const uint64_t* confBus;
371 const uint64_t* confAddr;
372 if (!(confBus = std::get_if<uint64_t>(&(configBus->second))) ||
373 !(confAddr = std::get_if<uint64_t>(&(configAddress->second))))
374 {
375 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700376 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800377 continue;
378 }
379
380 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800381 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700382 std::cerr << "Configuration skipping " << *confBus << "-"
383 << *confAddr << " because not " << bus << "-" << addr
384 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800385 continue;
386 }
387
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800388 std::vector<thresholds::Threshold> confThresholds;
389 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
390 {
391 std::cerr << "error populating totoal thresholds\n";
392 }
393 thresholdConfSize = confThresholds.size();
394
Cheng C Yang209ec562019-03-12 16:37:44 +0800395 interfacePath = &(sensor.first.str);
396 break;
397 }
398 if (interfacePath == nullptr)
399 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700400 // To avoid this error message, add your export map entry,
401 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800402 std::cerr << "failed to find match for " << deviceName << "\n";
403 continue;
404 }
405
Cheng C Yange50345b2019-04-02 17:26:15 +0800406 auto findPSUName = baseConfig->second.find("Name");
407 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800408 {
409 std::cerr << "could not determine configuration name for "
410 << deviceName << "\n";
411 continue;
412 }
413
Cheng C Yang58b2b532019-05-31 00:19:45 +0800414 if (!(psuName = std::get_if<std::string>(&(findPSUName->second))))
415 {
416 std::cerr << "Cannot find psu name, invalid configuration\n";
417 continue;
418 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800419
420 // on rescans, only update sensors we were signaled by
421 if (!firstScan)
422 {
423 std::string psuNameStr = "/" + *psuName;
424 auto it =
425 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
426 [psuNameStr](std::string& s) {
427 return boost::ends_with(s, psuNameStr);
428 });
429
430 if (it == sensorsChanged->end())
431 {
432 continue;
433 }
434 sensorsChanged->erase(it);
435 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800436 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800437 checkGroupEvent(directory.string(), groupEventMatch,
438 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800439
Vijay Khemka996bad12019-05-28 15:15:16 -0700440 /* Check if there are more sensors in the same interface */
441 int i = 1;
442 std::vector<std::string> psuNames;
443 do
444 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700445 // Individual string fields: Name, Name1, Name2, Name3, ...
Vijay Khemka996bad12019-05-28 15:15:16 -0700446 psuNames.push_back(std::get<std::string>(findPSUName->second));
447 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
448 } while (findPSUName != baseConfig->second.end());
449
Cheng C Yange50345b2019-04-02 17:26:15 +0800450 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700451 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800452 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800453 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800454 continue;
455 }
456
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530457 /* read max value in sysfs for in, curr, power, temp, ... */
458 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
459 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700460 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530461 {
462 std::cerr << "No max name in PSU \n";
463 }
464 }
465
Vijay Khemka996bad12019-05-28 15:15:16 -0700466 /* Find array of labels to be exposed if it is defined in config */
467 std::vector<std::string> findLabels;
468 auto findLabelObj = baseConfig->second.find("Labels");
469 if (findLabelObj != baseConfig->second.end())
470 {
471 findLabels =
472 std::get<std::vector<std::string>>(findLabelObj->second);
473 }
474
Jason Ling5747fab2019-10-02 16:46:23 -0700475 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
476 std::smatch matches;
477
Cheng C Yange50345b2019-04-02 17:26:15 +0800478 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800479 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530480 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800481 std::string labelHead;
482 std::string sensorPathStr = sensorPath.string();
483 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700484 std::string sensorNameSubStr{""};
485 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
486 {
Josh Lehan06494452019-10-31 09:49:16 -0700487 // hwmon *_input filename without number:
488 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700489 sensorNameSubStr = matches[1];
490 }
491 else
492 {
Josh Lehan06494452019-10-31 09:49:16 -0700493 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700494 << sensorNameStr;
495 continue;
496 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800497
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530498 std::string labelPath;
499
500 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700501 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530502 if (pos != std::string::npos)
503 {
504
505 std::string sensorPathStrMax = sensorPathStr.substr(pos);
506 if (sensorPathStrMax.compare("_max") == 0)
507 {
508 labelPath =
509 boost::replace_all_copy(sensorPathStr, "max", "label");
510 maxLabel = true;
511 }
512 else
513 {
514 labelPath = boost::replace_all_copy(sensorPathStr, "input",
515 "label");
516 maxLabel = false;
517 }
518 }
519 else
520 {
521 continue;
522 }
523
Cheng C Yangecba9de2019-09-12 23:41:50 +0800524 std::ifstream labelFile(labelPath);
525 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800526 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700527 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800528 {
529 std::cerr << "Input file " << sensorPath
530 << " has no corresponding label file\n";
531 }
Josh Lehan06494452019-10-31 09:49:16 -0700532 // hwmon *_input filename with number:
533 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700534 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800535 }
536 else
537 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800538 std::string label;
539 std::getline(labelFile, label);
540 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800541 auto findSensor = sensors.find(label);
542 if (findSensor != sensors.end())
543 {
544 continue;
545 }
546
Josh Lehan06494452019-10-31 09:49:16 -0700547 // hwmon corresponding *_label file contents:
548 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700549 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800550 }
551
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530552 /* append "max" for labelMatch */
553 if (maxLabel)
554 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700555 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530556 }
557
Ed Tanous8a57ec02020-10-09 12:46:52 -0700558 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700559 {
560 std::cerr << "Sensor type=\"" << sensorNameSubStr
561 << "\" label=\"" << labelHead << "\"\n";
562 }
563
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530564 checkPWMSensor(sensorPath, labelHead, *interfacePath,
565 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800566
Vijay Khemka996bad12019-05-28 15:15:16 -0700567 if (!findLabels.empty())
568 {
569 /* Check if this labelHead is enabled in config file */
570 if (std::find(findLabels.begin(), findLabels.end(),
571 labelHead) == findLabels.end())
572 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700573 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800574 {
575 std::cerr << "could not find " << labelHead
576 << " in the Labels list\n";
577 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700578 continue;
579 }
580 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800581
Cheng C Yange50345b2019-04-02 17:26:15 +0800582 auto findProperty = labelMatch.find(labelHead);
583 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800584 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700585 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700586 {
587 std::cerr << "Could not find matching default property for "
588 << labelHead << "\n";
589 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800590 continue;
591 }
592
Josh Lehan74d9bd92019-10-31 08:51:58 -0700593 // Protect the hardcoded labelMatch list from changes,
594 // by making a copy and modifying that instead.
595 // Avoid bleedthrough of one device's customizations to
596 // the next device, as each should be independently customizable.
597 psuProperties.push_back(findProperty->second);
598 auto psuProperty = psuProperties.rbegin();
599
600 // Use label head as prefix for reading from config file,
601 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
602 std::string keyName = labelHead + "_Name";
603 std::string keyScale = labelHead + "_Scale";
604 std::string keyMin = labelHead + "_Min";
605 std::string keyMax = labelHead + "_Max";
606
607 bool customizedName = false;
608 auto findCustomName = baseConfig->second.find(keyName);
609 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700610 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700611 try
612 {
613 psuProperty->labelTypeName = std::visit(
614 VariantToStringVisitor(), findCustomName->second);
615 }
616 catch (std::invalid_argument&)
617 {
618 std::cerr << "Unable to parse " << keyName << "\n";
619 continue;
620 }
621
622 // All strings are valid, including empty string
623 customizedName = true;
624 }
625
626 bool customizedScale = false;
627 auto findCustomScale = baseConfig->second.find(keyScale);
628 if (findCustomScale != baseConfig->second.end())
629 {
630 try
631 {
632 psuProperty->sensorScaleFactor = std::visit(
633 VariantToUnsignedIntVisitor(), findCustomScale->second);
634 }
635 catch (std::invalid_argument&)
636 {
637 std::cerr << "Unable to parse " << keyScale << "\n";
638 continue;
639 }
640
641 // Avoid later division by zero
642 if (psuProperty->sensorScaleFactor > 0)
643 {
644 customizedScale = true;
645 }
646 else
647 {
648 std::cerr << "Unable to accept " << keyScale << "\n";
649 continue;
650 }
651 }
652
653 auto findCustomMin = baseConfig->second.find(keyMin);
654 if (findCustomMin != baseConfig->second.end())
655 {
656 try
657 {
658 psuProperty->minReading = std::visit(
659 VariantToDoubleVisitor(), findCustomMin->second);
660 }
661 catch (std::invalid_argument&)
662 {
663 std::cerr << "Unable to parse " << keyMin << "\n";
664 continue;
665 }
666 }
667
668 auto findCustomMax = baseConfig->second.find(keyMax);
669 if (findCustomMax != baseConfig->second.end())
670 {
671 try
672 {
673 psuProperty->maxReading = std::visit(
674 VariantToDoubleVisitor(), findCustomMax->second);
675 }
676 catch (std::invalid_argument&)
677 {
678 std::cerr << "Unable to parse " << keyMax << "\n";
679 continue;
680 }
681 }
682
683 if (!(psuProperty->minReading < psuProperty->maxReading))
684 {
685 std::cerr << "Min must be less than Max\n";
686 continue;
687 }
688
689 // If the sensor name is being customized by config file,
690 // then prefix/suffix composition becomes not necessary,
691 // and in fact not wanted, because it gets in the way.
692 std::string psuNameFromIndex;
693 if (!customizedName)
694 {
695 /* Find out sensor name index for this label */
696 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500697 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700698 if (std::regex_search(labelHead, matches, rgx))
699 {
700 nameIndex = std::stoi(matches[1]);
701
702 // Decrement to preserve alignment, because hwmon
703 // human-readable filenames and labels use 1-based
704 // numbering, but the "Name", "Name1", "Name2", etc. naming
705 // convention (the psuNames vector) uses 0-based numbering.
706 if (nameIndex > 0)
707 {
708 --nameIndex;
709 }
710 }
711 else
712 {
713 nameIndex = 0;
714 }
715
716 if (psuNames.size() <= nameIndex)
717 {
718 std::cerr << "Could not pair " << labelHead
719 << " with a Name field\n";
720 continue;
721 }
722
723 psuNameFromIndex = psuNames[nameIndex];
724
Ed Tanous8a57ec02020-10-09 12:46:52 -0700725 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700726 {
727 std::cerr << "Sensor label head " << labelHead
728 << " paired with " << psuNameFromIndex
729 << " at index " << nameIndex << "\n";
730 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700731 }
732
Cheng C Yang58b2b532019-05-31 00:19:45 +0800733 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
734
Josh Lehan74d9bd92019-10-31 08:51:58 -0700735 // Similarly, if sensor scaling factor is being customized,
736 // then the below power-of-10 constraint becomes unnecessary,
737 // as config should be able to specify an arbitrary divisor.
738 unsigned int factor = psuProperty->sensorScaleFactor;
739 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700740 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700741 // Preserve existing usage of hardcoded labelMatch table below
742 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700743
Josh Lehan74d9bd92019-10-31 08:51:58 -0700744 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700745 char firstChar =
746 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700747 std::string strScaleFactor =
748 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
749
750 // Preserve existing configs by accepting earlier syntax,
751 // example CurrScaleFactor, PowerScaleFactor, ...
752 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
753 if (findScaleFactor != baseConfig->second.end())
754 {
755 factor = std::visit(VariantToIntVisitor(),
756 findScaleFactor->second);
757 }
758
Ed Tanous8a57ec02020-10-09 12:46:52 -0700759 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700760 {
761 std::cerr << "Sensor scaling factor " << factor
762 << " string " << strScaleFactor << "\n";
763 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700764 }
765
Vijay Khemka996bad12019-05-28 15:15:16 -0700766 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530767 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
768 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800769 {
James Feist17ab6e02019-06-25 12:28:13 -0700770 std::cerr << "error populating thresholds for "
771 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800772 }
773
774 auto findSensorType = sensorTable.find(sensorNameSubStr);
775 if (findSensorType == sensorTable.end())
776 {
Jason Ling5747fab2019-10-02 16:46:23 -0700777 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700778 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800779 continue;
780 }
781
Ed Tanous8a57ec02020-10-09 12:46:52 -0700782 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700783 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700784 std::cerr << "Sensor properties: Name \""
785 << psuProperty->labelTypeName << "\" Scale "
786 << psuProperty->sensorScaleFactor << " Min "
787 << psuProperty->minReading << " Max "
788 << psuProperty->maxReading << "\n";
789 }
790
791 std::string sensorName = psuProperty->labelTypeName;
792 if (customizedName)
793 {
794 if (sensorName.empty())
795 {
796 // Allow selective disabling of an individual sensor,
797 // by customizing its name to an empty string.
798 std::cerr << "Sensor disabled, empty string\n";
799 continue;
800 }
801 }
802 else
803 {
804 // Sensor name not customized, do prefix/suffix composition,
805 // preserving default behavior by using psuNameFromIndex.
806 sensorName =
807 psuNameFromIndex + " " + psuProperty->labelTypeName;
808 }
809
Ed Tanous8a57ec02020-10-09 12:46:52 -0700810 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700811 {
812 std::cerr << "Sensor name \"" << sensorName << "\" path \""
813 << sensorPathStr << "\" type \"" << sensorType
814 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700815 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800816 // destruct existing one first if already created
817 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800818 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800819 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800820 sensorName, std::move(sensorThresholds), *interfacePath,
Josh Lehan74d9bd92019-10-31 08:51:58 -0700821 findSensorType->second, factor, psuProperty->maxReading,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800822 psuProperty->minReading, labelHead, thresholdConfSize);
Yong Libf8b1da2020-04-15 16:32:50 +0800823 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700824 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700825 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700826 {
827 std::cerr << "Created " << numCreated << " sensors so far\n";
828 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800829 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800830
831 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800832 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800833 combineEvents[*psuName + "OperationalStatus"] =
Yong Li3046a022020-04-03 13:01:02 +0800834 std::make_unique<PSUCombineEvent>(
835 objectServer, dbusConnection, io, *psuName, eventPathList,
836 groupEventPathList, "OperationalStatus");
Cheng C Yang209ec562019-03-12 16:37:44 +0800837 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700838
Ed Tanous8a57ec02020-10-09 12:46:52 -0700839 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700840 {
841 std::cerr << "Created total of " << numCreated << " sensors\n";
842 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800843 return;
844}
845
Zhikui Ren23c96e72020-11-05 22:32:28 -0800846void createSensors(
847 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
848 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
849 const std::shared_ptr<boost::container::flat_set<std::string>>&
850 sensorsChanged)
851{
852 auto getter = std::make_shared<GetSensorConfiguration>(
853 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
854 const ManagedObjectType& sensorConfigs) {
855 createSensorsCallback(io, objectServer, dbusConnection,
856 sensorConfigs, sensorsChanged);
857 });
858 getter->getConfiguration(
859 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
860}
861
Cheng C Yang916360b2019-05-07 18:47:16 +0800862void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800863{
Cheng C Yange50345b2019-04-02 17:26:15 +0800864 sensorTable = {{"power", "power/"},
865 {"curr", "current/"},
866 {"temp", "temperature/"},
867 {"in", "voltage/"},
868 {"fan", "fan_tach/"}};
869
870 labelMatch = {{"pin", PSUProperty("Input Power", 3000, 0, 6)},
871 {"pout1", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700872 {"pout2", PSUProperty("Output Power", 3000, 0, 6)},
873 {"pout3", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700874 {"power1", PSUProperty("Output Power", 3000, 0, 6)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530875 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6)},
Cheng C Yangbdbde962019-04-26 22:50:34 +0800876 {"vin", PSUProperty("Input Voltage", 300, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530877 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700878 {"vout1", PSUProperty("Output Voltage", 255, 0, 3)},
879 {"vout2", PSUProperty("Output Voltage", 255, 0, 3)},
880 {"vout3", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700881 {"vout4", PSUProperty("Output Voltage", 255, 0, 3)},
882 {"vout5", PSUProperty("Output Voltage", 255, 0, 3)},
883 {"vout6", PSUProperty("Output Voltage", 255, 0, 3)},
884 {"vout7", PSUProperty("Output Voltage", 255, 0, 3)},
885 {"vout8", PSUProperty("Output Voltage", 255, 0, 3)},
886 {"vout9", PSUProperty("Output Voltage", 255, 0, 3)},
887 {"vout10", PSUProperty("Output Voltage", 255, 0, 3)},
888 {"vout11", PSUProperty("Output Voltage", 255, 0, 3)},
889 {"vout12", PSUProperty("Output Voltage", 255, 0, 3)},
890 {"vout13", PSUProperty("Output Voltage", 255, 0, 3)},
891 {"vout14", PSUProperty("Output Voltage", 255, 0, 3)},
892 {"vout15", PSUProperty("Output Voltage", 255, 0, 3)},
893 {"vout16", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling32047ef2020-09-30 15:43:32 -0700894 {"vout17", PSUProperty("Output Voltage", 255, 0, 3)},
895 {"vout18", PSUProperty("Output Voltage", 255, 0, 3)},
896 {"vout19", PSUProperty("Output Voltage", 255, 0, 3)},
897 {"vout20", PSUProperty("Output Voltage", 255, 0, 3)},
898 {"vout21", PSUProperty("Output Voltage", 255, 0, 3)},
899 {"vout22", PSUProperty("Output Voltage", 255, 0, 3)},
900 {"vout23", PSUProperty("Output Voltage", 255, 0, 3)},
901 {"vout24", PSUProperty("Output Voltage", 255, 0, 3)},
902 {"vout25", PSUProperty("Output Voltage", 255, 0, 3)},
903 {"vout26", PSUProperty("Output Voltage", 255, 0, 3)},
904 {"vout27", PSUProperty("Output Voltage", 255, 0, 3)},
905 {"vout28", PSUProperty("Output Voltage", 255, 0, 3)},
906 {"vout29", PSUProperty("Output Voltage", 255, 0, 3)},
907 {"vout30", PSUProperty("Output Voltage", 255, 0, 3)},
908 {"vout31", PSUProperty("Output Voltage", 255, 0, 3)},
909 {"vout32", PSUProperty("Output Voltage", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700910 {"in1", PSUProperty("Output Voltage", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800911 {"iin", PSUProperty("Input Current", 20, 0, 3)},
912 {"iout1", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700913 {"iout2", PSUProperty("Output Current", 255, 0, 3)},
914 {"iout3", PSUProperty("Output Current", 255, 0, 3)},
Peter Lundgren48b44232020-01-30 16:02:11 -0800915 {"iout4", PSUProperty("Output Current", 255, 0, 3)},
916 {"iout5", PSUProperty("Output Current", 255, 0, 3)},
917 {"iout6", PSUProperty("Output Current", 255, 0, 3)},
918 {"iout7", PSUProperty("Output Current", 255, 0, 3)},
919 {"iout8", PSUProperty("Output Current", 255, 0, 3)},
920 {"iout9", PSUProperty("Output Current", 255, 0, 3)},
921 {"iout10", PSUProperty("Output Current", 255, 0, 3)},
922 {"iout11", PSUProperty("Output Current", 255, 0, 3)},
923 {"iout12", PSUProperty("Output Current", 255, 0, 3)},
924 {"iout13", PSUProperty("Output Current", 255, 0, 3)},
925 {"iout14", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700926 {"curr1", PSUProperty("Output Current", 255, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530927 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800928 {"temp1", PSUProperty("Temperature", 127, -128, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700929 {"temp2", PSUProperty("Temperature", 127, -128, 3)},
930 {"temp3", PSUProperty("Temperature", 127, -128, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700931 {"temp4", PSUProperty("Temperature", 127, -128, 3)},
932 {"temp5", PSUProperty("Temperature", 127, -128, 3)},
Alex Qiuf5709b42020-01-29 13:29:50 -0800933 {"temp6", PSUProperty("Temperature", 127, -128, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530934 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3)},
Cheng C Yang8dbb3952019-05-23 00:03:12 +0800935 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0)},
936 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +0800937
938 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +0800939
940 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
941 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
942
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800943 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
944 {"Failure", {"in2_alarm"}},
945 {"ACLost", {"in1_beep"}},
946 {"ConfigureError", {"in1_fault"}}};
947
948 groupEventMatch = {{"FanFault",
949 {{"fan1", {"fan1_alarm", "fan1_fault"}},
950 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +0800951}
952
James Feistb6c0b912019-07-09 12:21:44 -0700953int main()
Cheng C Yang209ec562019-03-12 16:37:44 +0800954{
955 boost::asio::io_service io;
956 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
957
958 systemBus->request_name("xyz.openbmc_project.PSUSensor");
959 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800960 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800961 auto sensorsChanged =
962 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +0800963
Cheng C Yang916360b2019-05-07 18:47:16 +0800964 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +0800965
Zhikui Ren23c96e72020-11-05 22:32:28 -0800966 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +0800967 boost::asio::deadline_timer filterTimer(io);
968 std::function<void(sdbusplus::message::message&)> eventHandler =
969 [&](sdbusplus::message::message& message) {
970 if (message.is_method_error())
971 {
972 std::cerr << "callback method error\n";
973 return;
974 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800975 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +0800976 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +0800977 filterTimer.async_wait([&](const boost::system::error_code& ec) {
978 if (ec == boost::asio::error::operation_aborted)
979 {
980 return;
981 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700982 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +0800983 {
984 std::cerr << "timer error\n";
985 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800986 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +0800987 });
988 };
989
990 for (const char* type : sensorTypes)
991 {
992 auto match = std::make_unique<sdbusplus::bus::match::match>(
993 static_cast<sdbusplus::bus::bus&>(*systemBus),
994 "type='signal',member='PropertiesChanged',path_namespace='" +
995 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
996 eventHandler);
997 matches.emplace_back(std::move(match));
998 }
999 io.run();
1000}