blob: e31eb1b8fd12d763fbb7388994da3232561b110e [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
Lei YU8e0eccd2021-04-23 11:01:59 +080043static constexpr std::array<const char*, 25> 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",
Lei YU8e0eccd2021-04-23 11:01:59 +080050 "xyz.openbmc_project.Configuration.IPSPS",
Alex Qiu6690d8f2020-01-22 17:56:33 -080051 "xyz.openbmc_project.Configuration.ISL68137",
Jason Lingdfad1ff2020-07-29 15:43:11 -070052 "xyz.openbmc_project.Configuration.ISL68220",
Gaurav Gandhi49585582020-12-22 01:01:24 +000053 "xyz.openbmc_project.Configuration.ISL68223",
54 "xyz.openbmc_project.Configuration.ISL69243",
Jeff Lina0683a82021-02-22 13:56:55 +080055 "xyz.openbmc_project.Configuration.ISL69260",
Zev Weisseb0b84d2021-04-22 15:03:38 -050056 "xyz.openbmc_project.Configuration.LM25066",
Alex Qiuf5709b42020-01-29 13:29:50 -080057 "xyz.openbmc_project.Configuration.MAX16601",
Gaurav Gandhi49585582020-12-22 01:01:24 +000058 "xyz.openbmc_project.Configuration.MAX20710",
Alex Qiu6690d8f2020-01-22 17:56:33 -080059 "xyz.openbmc_project.Configuration.MAX20730",
60 "xyz.openbmc_project.Configuration.MAX20734",
61 "xyz.openbmc_project.Configuration.MAX20796",
62 "xyz.openbmc_project.Configuration.MAX34451",
Josh Lehan62084c82020-06-22 15:27:12 -070063 "xyz.openbmc_project.Configuration.pmbus",
64 "xyz.openbmc_project.Configuration.PXE1610",
Jeff Lina0683a82021-02-22 13:56:55 +080065 "xyz.openbmc_project.Configuration.RAA228000",
66 "xyz.openbmc_project.Configuration.RAA228228",
67 "xyz.openbmc_project.Configuration.RAA229004",
68 "xyz.openbmc_project.Configuration.TPS546D24"};
Cheng C Yang209ec562019-03-12 16:37:44 +080069
Alex Qiu6690d8f2020-01-22 17:56:33 -080070static std::vector<std::string> pmbusNames = {
Lei YU8e0eccd2021-04-23 11:01:59 +080071 "adm1272", "adm1275", "adm1278", "dps800", "ina219",
72 "ina230", "ipsps1", "isl68137", "isl68220", "isl68223",
73 "isl69243", "isl69260", "lm25066", "max16601", "max20710",
74 "max20730", "max20734", "max20796", "max34451", "pmbus",
75 "pxe1610", "raa228000", "raa228228", "raa229004", "tps546d24"};
Josh Lehan0830c7b2019-10-08 16:35:09 -070076
Cheng C Yang209ec562019-03-12 16:37:44 +080077namespace fs = std::filesystem;
78
Yong Libf8b1da2020-04-15 16:32:50 +080079static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +080080 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +080081static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
82 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +080083static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
84 pwmSensors;
85static boost::container::flat_map<std::string, std::string> sensorTable;
86static boost::container::flat_map<std::string, PSUProperty> labelMatch;
87static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +080088static boost::container::flat_map<std::string, std::vector<std::string>>
89 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +080090static boost::container::flat_map<
91 std::string,
92 boost::container::flat_map<std::string, std::vector<std::string>>>
93 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +080094static boost::container::flat_map<std::string, std::vector<std::string>>
95 limitEventMatch;
96
Josh Lehan74d9bd92019-10-31 08:51:58 -070097static std::vector<PSUProperty> psuProperties;
98
Cheng C Yang58b2b532019-05-31 00:19:45 +080099// Function CheckEvent will check each attribute from eventMatch table in the
100// sysfs. If the attributes exists in sysfs, then store the complete path
101// of the attribute into eventPathList.
102void checkEvent(
103 const std::string& directory,
104 const boost::container::flat_map<std::string, std::vector<std::string>>&
105 eventMatch,
106 boost::container::flat_map<std::string, std::vector<std::string>>&
107 eventPathList)
108{
109 for (const auto& match : eventMatch)
110 {
111 const std::vector<std::string>& eventAttrs = match.second;
112 const std::string& eventName = match.first;
113 for (const auto& eventAttr : eventAttrs)
114 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700115 std::string eventPath = directory;
116 eventPath += "/";
117 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800118
119 std::ifstream eventFile(eventPath);
120 if (!eventFile.good())
121 {
122 continue;
123 }
124
125 eventPathList[eventName].push_back(eventPath);
126 }
127 }
128}
129
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800130// Check Group Events which contains more than one targets in each combine
131// events.
132void checkGroupEvent(
133 const std::string& directory,
134 const boost::container::flat_map<
135 std::string,
136 boost::container::flat_map<std::string, std::vector<std::string>>>&
137 groupEventMatch,
138 boost::container::flat_map<
139 std::string,
140 boost::container::flat_map<std::string, std::vector<std::string>>>&
141 groupEventPathList)
142{
143 for (const auto& match : groupEventMatch)
144 {
145 const std::string& groupEventName = match.first;
146 const boost::container::flat_map<std::string, std::vector<std::string>>
147 events = match.second;
148 boost::container::flat_map<std::string, std::vector<std::string>>
149 pathList;
150 for (const auto& match : events)
151 {
152 const std::string& eventName = match.first;
153 const std::vector<std::string>& eventAttrs = match.second;
154 for (const auto& eventAttr : eventAttrs)
155 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700156 std::string eventPath = directory;
157 eventPath += "/";
158 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800159 std::ifstream eventFile(eventPath);
160 if (!eventFile.good())
161 {
162 continue;
163 }
164
165 pathList[eventName].push_back(eventPath);
166 }
167 }
168 groupEventPathList[groupEventName] = pathList;
169 }
170}
171
Cheng C Yang58b2b532019-05-31 00:19:45 +0800172// Function checkEventLimits will check all the psu related xxx_input attributes
173// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
174// xxx_min_alarm exist, then store the existing paths of the alarm attributes
175// to eventPathList.
176void checkEventLimits(
177 const std::string& sensorPathStr,
178 const boost::container::flat_map<std::string, std::vector<std::string>>&
179 limitEventMatch,
180 boost::container::flat_map<std::string, std::vector<std::string>>&
181 eventPathList)
182{
Lei YUa2c7cea2020-12-23 14:07:28 +0800183 auto attributePartPos = sensorPathStr.find_last_of('_');
184 if (attributePartPos == std::string::npos)
185 {
186 // There is no '_' in the string, skip it
187 return;
188 }
189 auto attributePart =
190 std::string_view(sensorPathStr).substr(attributePartPos + 1);
191 if (attributePart != "input")
192 {
193 // If the sensor is not xxx_input, skip it
194 return;
195 }
196
197 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800198 for (const auto& limitMatch : limitEventMatch)
199 {
200 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
201 const std::string& eventName = limitMatch.first;
202 for (const auto& limitEventAttr : limitEventAttrs)
203 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800204 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800205 std::ifstream eventFile(limitEventPath);
206 if (!eventFile.good())
207 {
208 continue;
209 }
210 eventPathList[eventName].push_back(limitEventPath);
211 }
212 }
213}
Cheng C Yang916360b2019-05-07 18:47:16 +0800214
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530215static void
216 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
217 const std::string& interfacePath,
218 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
219 sdbusplus::asio::object_server& objectServer,
220 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800221{
222 for (const auto& pwmName : pwmTable)
223 {
224 if (pwmName.first != labelHead)
225 {
226 continue;
227 }
228
229 const std::string& sensorPathStr = sensorPath.string();
230 const std::string& pwmPathStr =
231 boost::replace_all_copy(sensorPathStr, "input", "target");
232 std::ifstream pwmFile(pwmPathStr);
233 if (!pwmFile.good())
234 {
235 continue;
236 }
237
238 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
239 if (findPWMSensor != pwmSensors.end())
240 {
241 continue;
242 }
243
244 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530245 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
246 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800247 }
248}
249
Zhikui Ren23c96e72020-11-05 22:32:28 -0800250static void createSensorsCallback(
251 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
252 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
253 const ManagedObjectType& sensorConfigs,
254 const std::shared_ptr<boost::container::flat_set<std::string>>&
255 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800256{
Josh Lehan49cfba92019-10-08 16:50:42 -0700257 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800258 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800259
260 std::vector<fs::path> pmbusPaths;
261 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
262 {
263 std::cerr << "No PSU sensors in system\n";
264 return;
265 }
266
267 boost::container::flat_set<std::string> directories;
268 for (const auto& pmbusPath : pmbusPaths)
269 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800270 boost::container::flat_map<std::string, std::vector<std::string>>
271 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800272 boost::container::flat_map<
273 std::string,
274 boost::container::flat_map<std::string, std::vector<std::string>>>
275 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800276
277 std::ifstream nameFile(pmbusPath);
278 if (!nameFile.good())
279 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700280 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800281 continue;
282 }
283
284 std::string pmbusName;
285 std::getline(nameFile, pmbusName);
286 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700287
288 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
289 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800290 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700291 // To avoid this error message, add your driver name to
292 // the pmbusNames vector at the top of this file.
293 std::cerr << "Driver name " << pmbusName
294 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800295 continue;
296 }
297
298 const std::string* psuName;
Cheng C Yang209ec562019-03-12 16:37:44 +0800299 auto directory = pmbusPath.parent_path();
300
301 auto ret = directories.insert(directory.string());
302 if (!ret.second)
303 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700304 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800305 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800306 }
307
James Feistb6c0b912019-07-09 12:21:44 -0700308 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800309 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700310 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800311 if (findHyphen == std::string::npos)
312 {
313 std::cerr << "found bad device" << deviceName << "\n";
314 continue;
315 }
316 std::string busStr = deviceName.substr(0, findHyphen);
317 std::string addrStr = deviceName.substr(findHyphen + 1);
318
319 size_t bus = 0;
320 size_t addr = 0;
321
322 try
323 {
324 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700325 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800326 }
James Feistb6c0b912019-07-09 12:21:44 -0700327 catch (std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800328 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700329 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
330 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800331 continue;
332 }
333
Cheng C Yang209ec562019-03-12 16:37:44 +0800334 const std::pair<std::string, boost::container::flat_map<
335 std::string, BasicVariantType>>*
336 baseConfig = nullptr;
337 const SensorData* sensorData = nullptr;
338 const std::string* interfacePath = nullptr;
339 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800340 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800341
342 for (const std::pair<sdbusplus::message::object_path, SensorData>&
343 sensor : sensorConfigs)
344 {
345 sensorData = &(sensor.second);
346 for (const char* type : sensorTypes)
347 {
348 auto sensorBase = sensorData->find(type);
349 if (sensorBase != sensorData->end())
350 {
351 baseConfig = &(*sensorBase);
352 sensorType = type;
353 break;
354 }
355 }
356 if (baseConfig == nullptr)
357 {
358 std::cerr << "error finding base configuration for "
359 << deviceName << "\n";
360 continue;
361 }
362
363 auto configBus = baseConfig->second.find("Bus");
364 auto configAddress = baseConfig->second.find("Address");
365
366 if (configBus == baseConfig->second.end() ||
367 configAddress == baseConfig->second.end())
368 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800369 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800370 continue;
371 }
372
Cheng C Yang58b2b532019-05-31 00:19:45 +0800373 const uint64_t* confBus;
374 const uint64_t* confAddr;
375 if (!(confBus = std::get_if<uint64_t>(&(configBus->second))) ||
376 !(confAddr = std::get_if<uint64_t>(&(configAddress->second))))
377 {
378 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700379 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800380 continue;
381 }
382
383 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800384 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700385 std::cerr << "Configuration skipping " << *confBus << "-"
386 << *confAddr << " because not " << bus << "-" << addr
387 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800388 continue;
389 }
390
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800391 std::vector<thresholds::Threshold> confThresholds;
392 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
393 {
394 std::cerr << "error populating totoal thresholds\n";
395 }
396 thresholdConfSize = confThresholds.size();
397
Cheng C Yang209ec562019-03-12 16:37:44 +0800398 interfacePath = &(sensor.first.str);
399 break;
400 }
401 if (interfacePath == nullptr)
402 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700403 // To avoid this error message, add your export map entry,
404 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800405 std::cerr << "failed to find match for " << deviceName << "\n";
406 continue;
407 }
408
Cheng C Yange50345b2019-04-02 17:26:15 +0800409 auto findPSUName = baseConfig->second.find("Name");
410 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800411 {
412 std::cerr << "could not determine configuration name for "
413 << deviceName << "\n";
414 continue;
415 }
416
Cheng C Yang58b2b532019-05-31 00:19:45 +0800417 if (!(psuName = std::get_if<std::string>(&(findPSUName->second))))
418 {
419 std::cerr << "Cannot find psu name, invalid configuration\n";
420 continue;
421 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800422
423 // on rescans, only update sensors we were signaled by
424 if (!firstScan)
425 {
426 std::string psuNameStr = "/" + *psuName;
427 auto it =
428 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
429 [psuNameStr](std::string& s) {
430 return boost::ends_with(s, psuNameStr);
431 });
432
433 if (it == sensorsChanged->end())
434 {
435 continue;
436 }
437 sensorsChanged->erase(it);
438 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800439 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800440 checkGroupEvent(directory.string(), groupEventMatch,
441 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800442
Vijay Khemka996bad12019-05-28 15:15:16 -0700443 /* Check if there are more sensors in the same interface */
444 int i = 1;
445 std::vector<std::string> psuNames;
446 do
447 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700448 // Individual string fields: Name, Name1, Name2, Name3, ...
Vijay Khemka996bad12019-05-28 15:15:16 -0700449 psuNames.push_back(std::get<std::string>(findPSUName->second));
450 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
451 } while (findPSUName != baseConfig->second.end());
452
Cheng C Yange50345b2019-04-02 17:26:15 +0800453 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700454 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800455 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800456 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800457 continue;
458 }
459
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530460 /* read max value in sysfs for in, curr, power, temp, ... */
461 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
462 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700463 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530464 {
465 std::cerr << "No max name in PSU \n";
466 }
467 }
468
Lei YU7170a232021-02-04 16:19:27 +0800469 /* The poll rate for the sensors */
470 double pollRate = 0.0;
471 auto pollRateObj = baseConfig->second.find("PollRate");
472
473 if (pollRateObj != baseConfig->second.end())
474 {
475 pollRate =
476 std::visit(VariantToDoubleVisitor(), pollRateObj->second);
477 if (pollRate <= 0.0)
478 {
479 pollRate = PSUSensor::defaultSensorPoll;
480 }
481 }
482
Vijay Khemka996bad12019-05-28 15:15:16 -0700483 /* Find array of labels to be exposed if it is defined in config */
484 std::vector<std::string> findLabels;
485 auto findLabelObj = baseConfig->second.find("Labels");
486 if (findLabelObj != baseConfig->second.end())
487 {
488 findLabels =
489 std::get<std::vector<std::string>>(findLabelObj->second);
490 }
491
Jason Ling5747fab2019-10-02 16:46:23 -0700492 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
493 std::smatch matches;
494
Cheng C Yange50345b2019-04-02 17:26:15 +0800495 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800496 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530497 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800498 std::string labelHead;
499 std::string sensorPathStr = sensorPath.string();
500 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700501 std::string sensorNameSubStr{""};
502 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
503 {
Josh Lehan06494452019-10-31 09:49:16 -0700504 // hwmon *_input filename without number:
505 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700506 sensorNameSubStr = matches[1];
507 }
508 else
509 {
Josh Lehan06494452019-10-31 09:49:16 -0700510 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700511 << sensorNameStr;
512 continue;
513 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800514
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530515 std::string labelPath;
516
517 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700518 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530519 if (pos != std::string::npos)
520 {
521
522 std::string sensorPathStrMax = sensorPathStr.substr(pos);
523 if (sensorPathStrMax.compare("_max") == 0)
524 {
525 labelPath =
526 boost::replace_all_copy(sensorPathStr, "max", "label");
527 maxLabel = true;
528 }
529 else
530 {
531 labelPath = boost::replace_all_copy(sensorPathStr, "input",
532 "label");
533 maxLabel = false;
534 }
535 }
536 else
537 {
538 continue;
539 }
540
Cheng C Yangecba9de2019-09-12 23:41:50 +0800541 std::ifstream labelFile(labelPath);
542 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800543 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700544 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800545 {
546 std::cerr << "Input file " << sensorPath
547 << " has no corresponding label file\n";
548 }
Josh Lehan06494452019-10-31 09:49:16 -0700549 // hwmon *_input filename with number:
550 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700551 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800552 }
553 else
554 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800555 std::string label;
556 std::getline(labelFile, label);
557 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800558 auto findSensor = sensors.find(label);
559 if (findSensor != sensors.end())
560 {
561 continue;
562 }
563
Josh Lehan06494452019-10-31 09:49:16 -0700564 // hwmon corresponding *_label file contents:
565 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700566 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800567 }
568
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530569 /* append "max" for labelMatch */
570 if (maxLabel)
571 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700572 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530573 }
574
Ed Tanous8a57ec02020-10-09 12:46:52 -0700575 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700576 {
577 std::cerr << "Sensor type=\"" << sensorNameSubStr
578 << "\" label=\"" << labelHead << "\"\n";
579 }
580
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530581 checkPWMSensor(sensorPath, labelHead, *interfacePath,
582 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800583
Vijay Khemka996bad12019-05-28 15:15:16 -0700584 if (!findLabels.empty())
585 {
586 /* Check if this labelHead is enabled in config file */
587 if (std::find(findLabels.begin(), findLabels.end(),
588 labelHead) == findLabels.end())
589 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700590 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800591 {
592 std::cerr << "could not find " << labelHead
593 << " in the Labels list\n";
594 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700595 continue;
596 }
597 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800598
Cheng C Yange50345b2019-04-02 17:26:15 +0800599 auto findProperty = labelMatch.find(labelHead);
600 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800601 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700602 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700603 {
604 std::cerr << "Could not find matching default property for "
605 << labelHead << "\n";
606 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800607 continue;
608 }
609
Josh Lehan74d9bd92019-10-31 08:51:58 -0700610 // Protect the hardcoded labelMatch list from changes,
611 // by making a copy and modifying that instead.
612 // Avoid bleedthrough of one device's customizations to
613 // the next device, as each should be independently customizable.
614 psuProperties.push_back(findProperty->second);
615 auto psuProperty = psuProperties.rbegin();
616
617 // Use label head as prefix for reading from config file,
618 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
619 std::string keyName = labelHead + "_Name";
620 std::string keyScale = labelHead + "_Scale";
621 std::string keyMin = labelHead + "_Min";
622 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800623 std::string keyOffset = labelHead + "_Offset";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700624
625 bool customizedName = false;
626 auto findCustomName = baseConfig->second.find(keyName);
627 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700628 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700629 try
630 {
631 psuProperty->labelTypeName = std::visit(
632 VariantToStringVisitor(), findCustomName->second);
633 }
634 catch (std::invalid_argument&)
635 {
636 std::cerr << "Unable to parse " << keyName << "\n";
637 continue;
638 }
639
640 // All strings are valid, including empty string
641 customizedName = true;
642 }
643
644 bool customizedScale = false;
645 auto findCustomScale = baseConfig->second.find(keyScale);
646 if (findCustomScale != baseConfig->second.end())
647 {
648 try
649 {
650 psuProperty->sensorScaleFactor = std::visit(
651 VariantToUnsignedIntVisitor(), findCustomScale->second);
652 }
653 catch (std::invalid_argument&)
654 {
655 std::cerr << "Unable to parse " << keyScale << "\n";
656 continue;
657 }
658
659 // Avoid later division by zero
660 if (psuProperty->sensorScaleFactor > 0)
661 {
662 customizedScale = true;
663 }
664 else
665 {
666 std::cerr << "Unable to accept " << keyScale << "\n";
667 continue;
668 }
669 }
670
671 auto findCustomMin = baseConfig->second.find(keyMin);
672 if (findCustomMin != baseConfig->second.end())
673 {
674 try
675 {
676 psuProperty->minReading = std::visit(
677 VariantToDoubleVisitor(), findCustomMin->second);
678 }
679 catch (std::invalid_argument&)
680 {
681 std::cerr << "Unable to parse " << keyMin << "\n";
682 continue;
683 }
684 }
685
686 auto findCustomMax = baseConfig->second.find(keyMax);
687 if (findCustomMax != baseConfig->second.end())
688 {
689 try
690 {
691 psuProperty->maxReading = std::visit(
692 VariantToDoubleVisitor(), findCustomMax->second);
693 }
694 catch (std::invalid_argument&)
695 {
696 std::cerr << "Unable to parse " << keyMax << "\n";
697 continue;
698 }
699 }
700
Jeff Line41d52f2021-04-07 19:38:51 +0800701 auto findCustomOffset = baseConfig->second.find(keyOffset);
702 if (findCustomOffset != baseConfig->second.end())
703 {
704 try
705 {
706 psuProperty->sensorOffset = std::visit(
707 VariantToDoubleVisitor(), findCustomOffset->second);
708 }
709 catch (std::invalid_argument&)
710 {
711 std::cerr << "Unable to parse " << keyOffset << "\n";
712 continue;
713 }
714 }
715
Josh Lehan74d9bd92019-10-31 08:51:58 -0700716 if (!(psuProperty->minReading < psuProperty->maxReading))
717 {
718 std::cerr << "Min must be less than Max\n";
719 continue;
720 }
721
722 // If the sensor name is being customized by config file,
723 // then prefix/suffix composition becomes not necessary,
724 // and in fact not wanted, because it gets in the way.
725 std::string psuNameFromIndex;
726 if (!customizedName)
727 {
728 /* Find out sensor name index for this label */
729 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500730 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700731 if (std::regex_search(labelHead, matches, rgx))
732 {
733 nameIndex = std::stoi(matches[1]);
734
735 // Decrement to preserve alignment, because hwmon
736 // human-readable filenames and labels use 1-based
737 // numbering, but the "Name", "Name1", "Name2", etc. naming
738 // convention (the psuNames vector) uses 0-based numbering.
739 if (nameIndex > 0)
740 {
741 --nameIndex;
742 }
743 }
744 else
745 {
746 nameIndex = 0;
747 }
748
749 if (psuNames.size() <= nameIndex)
750 {
751 std::cerr << "Could not pair " << labelHead
752 << " with a Name field\n";
753 continue;
754 }
755
756 psuNameFromIndex = psuNames[nameIndex];
757
Ed Tanous8a57ec02020-10-09 12:46:52 -0700758 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700759 {
760 std::cerr << "Sensor label head " << labelHead
761 << " paired with " << psuNameFromIndex
762 << " at index " << nameIndex << "\n";
763 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700764 }
765
Cheng C Yang58b2b532019-05-31 00:19:45 +0800766 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
767
Josh Lehan74d9bd92019-10-31 08:51:58 -0700768 // Similarly, if sensor scaling factor is being customized,
769 // then the below power-of-10 constraint becomes unnecessary,
770 // as config should be able to specify an arbitrary divisor.
771 unsigned int factor = psuProperty->sensorScaleFactor;
772 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700773 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700774 // Preserve existing usage of hardcoded labelMatch table below
775 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700776
Josh Lehan74d9bd92019-10-31 08:51:58 -0700777 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700778 char firstChar =
779 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700780 std::string strScaleFactor =
781 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
782
783 // Preserve existing configs by accepting earlier syntax,
784 // example CurrScaleFactor, PowerScaleFactor, ...
785 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
786 if (findScaleFactor != baseConfig->second.end())
787 {
788 factor = std::visit(VariantToIntVisitor(),
789 findScaleFactor->second);
790 }
791
Ed Tanous8a57ec02020-10-09 12:46:52 -0700792 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700793 {
794 std::cerr << "Sensor scaling factor " << factor
795 << " string " << strScaleFactor << "\n";
796 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700797 }
798
Vijay Khemka996bad12019-05-28 15:15:16 -0700799 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530800 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
801 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800802 {
James Feist17ab6e02019-06-25 12:28:13 -0700803 std::cerr << "error populating thresholds for "
804 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800805 }
806
Zev Weiss6b6891c2021-04-22 02:46:21 -0500807 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
808 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800809 {
Jason Ling5747fab2019-10-02 16:46:23 -0700810 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700811 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800812 continue;
813 }
814
Ed Tanous8a57ec02020-10-09 12:46:52 -0700815 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700816 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700817 std::cerr << "Sensor properties: Name \""
818 << psuProperty->labelTypeName << "\" Scale "
819 << psuProperty->sensorScaleFactor << " Min "
820 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800821 << psuProperty->maxReading << " Offset "
822 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700823 }
824
825 std::string sensorName = psuProperty->labelTypeName;
826 if (customizedName)
827 {
828 if (sensorName.empty())
829 {
830 // Allow selective disabling of an individual sensor,
831 // by customizing its name to an empty string.
832 std::cerr << "Sensor disabled, empty string\n";
833 continue;
834 }
835 }
836 else
837 {
838 // Sensor name not customized, do prefix/suffix composition,
839 // preserving default behavior by using psuNameFromIndex.
840 sensorName =
841 psuNameFromIndex + " " + psuProperty->labelTypeName;
842 }
843
Ed Tanous8a57ec02020-10-09 12:46:52 -0700844 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700845 {
846 std::cerr << "Sensor name \"" << sensorName << "\" path \""
847 << sensorPathStr << "\" type \"" << sensorType
848 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700849 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800850 // destruct existing one first if already created
851 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800852 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800853 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800854 sensorName, std::move(sensorThresholds), *interfacePath,
Zev Weiss6b6891c2021-04-22 02:46:21 -0500855 findSensorUnit->second, factor, psuProperty->maxReading,
Jeff Line41d52f2021-04-07 19:38:51 +0800856 psuProperty->minReading, psuProperty->sensorOffset, labelHead,
857 thresholdConfSize, pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800858 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700859 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700860 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700861 {
862 std::cerr << "Created " << numCreated << " sensors so far\n";
863 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800864 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800865
866 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800867 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800868 combineEvents[*psuName + "OperationalStatus"] =
Yong Li3046a022020-04-03 13:01:02 +0800869 std::make_unique<PSUCombineEvent>(
870 objectServer, dbusConnection, io, *psuName, eventPathList,
Lei YU7170a232021-02-04 16:19:27 +0800871 groupEventPathList, "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800872 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700873
Ed Tanous8a57ec02020-10-09 12:46:52 -0700874 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700875 {
876 std::cerr << "Created total of " << numCreated << " sensors\n";
877 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800878 return;
879}
880
Zhikui Ren23c96e72020-11-05 22:32:28 -0800881void createSensors(
882 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
883 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
884 const std::shared_ptr<boost::container::flat_set<std::string>>&
885 sensorsChanged)
886{
887 auto getter = std::make_shared<GetSensorConfiguration>(
888 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
889 const ManagedObjectType& sensorConfigs) {
890 createSensorsCallback(io, objectServer, dbusConnection,
891 sensorConfigs, sensorsChanged);
892 });
893 getter->getConfiguration(
894 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
895}
896
Cheng C Yang916360b2019-05-07 18:47:16 +0800897void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800898{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500899 sensorTable = {{"power", sensor_paths::unitWatts},
900 {"curr", sensor_paths::unitAmperes},
901 {"temp", sensor_paths::unitDegreesC},
902 {"in", sensor_paths::unitVolts},
903 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800904
Jeff Line41d52f2021-04-07 19:38:51 +0800905 labelMatch = {
906 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
907 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
908 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
909 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
910 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
911 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
912 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
913 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
914 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
915 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
916 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
917 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
918 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
919 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
920 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
921 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
922 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
923 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
924 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
925 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
926 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
927 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
928 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
929 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
930 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
931 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
932 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
933 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
934 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
935 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
936 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
937 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
938 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
939 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
940 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
941 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
942 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
943 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
944 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
945 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
946 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
947 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
948 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
949 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
950 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
951 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
952 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
953 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
954 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
955 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
956 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
957 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
958 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
959 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
960 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
961 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
962 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
963 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
964 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
965 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
966 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
967 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
968 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
969 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
970 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
971 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
972 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
973 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +0800974
975 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +0800976
977 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
978 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
979
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800980 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
981 {"Failure", {"in2_alarm"}},
982 {"ACLost", {"in1_beep"}},
983 {"ConfigureError", {"in1_fault"}}};
984
985 groupEventMatch = {{"FanFault",
986 {{"fan1", {"fan1_alarm", "fan1_fault"}},
987 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +0800988}
989
James Feistb6c0b912019-07-09 12:21:44 -0700990int main()
Cheng C Yang209ec562019-03-12 16:37:44 +0800991{
992 boost::asio::io_service io;
993 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
994
995 systemBus->request_name("xyz.openbmc_project.PSUSensor");
996 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800997 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800998 auto sensorsChanged =
999 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001000
Cheng C Yang916360b2019-05-07 18:47:16 +08001001 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001002
Zhikui Ren23c96e72020-11-05 22:32:28 -08001003 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +08001004 boost::asio::deadline_timer filterTimer(io);
1005 std::function<void(sdbusplus::message::message&)> eventHandler =
1006 [&](sdbusplus::message::message& message) {
1007 if (message.is_method_error())
1008 {
1009 std::cerr << "callback method error\n";
1010 return;
1011 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001012 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +08001013 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +08001014 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1015 if (ec == boost::asio::error::operation_aborted)
1016 {
1017 return;
1018 }
Ed Tanous8a57ec02020-10-09 12:46:52 -07001019 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +08001020 {
1021 std::cerr << "timer error\n";
1022 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001023 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +08001024 });
1025 };
1026
1027 for (const char* type : sensorTypes)
1028 {
1029 auto match = std::make_unique<sdbusplus::bus::match::match>(
1030 static_cast<sdbusplus::bus::bus&>(*systemBus),
1031 "type='signal',member='PropertiesChanged',path_namespace='" +
1032 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
1033 eventHandler);
1034 matches.emplace_back(std::move(match));
1035 }
Bruce Lee1263c3d2021-06-04 15:16:33 +08001036
1037 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001038 io.run();
1039}