blob: 01ae6d84060fef772df869525b2b345612b0c98b [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";
623
624 bool customizedName = false;
625 auto findCustomName = baseConfig->second.find(keyName);
626 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700627 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700628 try
629 {
630 psuProperty->labelTypeName = std::visit(
631 VariantToStringVisitor(), findCustomName->second);
632 }
633 catch (std::invalid_argument&)
634 {
635 std::cerr << "Unable to parse " << keyName << "\n";
636 continue;
637 }
638
639 // All strings are valid, including empty string
640 customizedName = true;
641 }
642
643 bool customizedScale = false;
644 auto findCustomScale = baseConfig->second.find(keyScale);
645 if (findCustomScale != baseConfig->second.end())
646 {
647 try
648 {
649 psuProperty->sensorScaleFactor = std::visit(
650 VariantToUnsignedIntVisitor(), findCustomScale->second);
651 }
652 catch (std::invalid_argument&)
653 {
654 std::cerr << "Unable to parse " << keyScale << "\n";
655 continue;
656 }
657
658 // Avoid later division by zero
659 if (psuProperty->sensorScaleFactor > 0)
660 {
661 customizedScale = true;
662 }
663 else
664 {
665 std::cerr << "Unable to accept " << keyScale << "\n";
666 continue;
667 }
668 }
669
670 auto findCustomMin = baseConfig->second.find(keyMin);
671 if (findCustomMin != baseConfig->second.end())
672 {
673 try
674 {
675 psuProperty->minReading = std::visit(
676 VariantToDoubleVisitor(), findCustomMin->second);
677 }
678 catch (std::invalid_argument&)
679 {
680 std::cerr << "Unable to parse " << keyMin << "\n";
681 continue;
682 }
683 }
684
685 auto findCustomMax = baseConfig->second.find(keyMax);
686 if (findCustomMax != baseConfig->second.end())
687 {
688 try
689 {
690 psuProperty->maxReading = std::visit(
691 VariantToDoubleVisitor(), findCustomMax->second);
692 }
693 catch (std::invalid_argument&)
694 {
695 std::cerr << "Unable to parse " << keyMax << "\n";
696 continue;
697 }
698 }
699
700 if (!(psuProperty->minReading < psuProperty->maxReading))
701 {
702 std::cerr << "Min must be less than Max\n";
703 continue;
704 }
705
706 // If the sensor name is being customized by config file,
707 // then prefix/suffix composition becomes not necessary,
708 // and in fact not wanted, because it gets in the way.
709 std::string psuNameFromIndex;
710 if (!customizedName)
711 {
712 /* Find out sensor name index for this label */
713 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500714 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700715 if (std::regex_search(labelHead, matches, rgx))
716 {
717 nameIndex = std::stoi(matches[1]);
718
719 // Decrement to preserve alignment, because hwmon
720 // human-readable filenames and labels use 1-based
721 // numbering, but the "Name", "Name1", "Name2", etc. naming
722 // convention (the psuNames vector) uses 0-based numbering.
723 if (nameIndex > 0)
724 {
725 --nameIndex;
726 }
727 }
728 else
729 {
730 nameIndex = 0;
731 }
732
733 if (psuNames.size() <= nameIndex)
734 {
735 std::cerr << "Could not pair " << labelHead
736 << " with a Name field\n";
737 continue;
738 }
739
740 psuNameFromIndex = psuNames[nameIndex];
741
Ed Tanous8a57ec02020-10-09 12:46:52 -0700742 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700743 {
744 std::cerr << "Sensor label head " << labelHead
745 << " paired with " << psuNameFromIndex
746 << " at index " << nameIndex << "\n";
747 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700748 }
749
Cheng C Yang58b2b532019-05-31 00:19:45 +0800750 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
751
Josh Lehan74d9bd92019-10-31 08:51:58 -0700752 // Similarly, if sensor scaling factor is being customized,
753 // then the below power-of-10 constraint becomes unnecessary,
754 // as config should be able to specify an arbitrary divisor.
755 unsigned int factor = psuProperty->sensorScaleFactor;
756 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700757 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700758 // Preserve existing usage of hardcoded labelMatch table below
759 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700760
Josh Lehan74d9bd92019-10-31 08:51:58 -0700761 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700762 char firstChar =
763 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700764 std::string strScaleFactor =
765 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
766
767 // Preserve existing configs by accepting earlier syntax,
768 // example CurrScaleFactor, PowerScaleFactor, ...
769 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
770 if (findScaleFactor != baseConfig->second.end())
771 {
772 factor = std::visit(VariantToIntVisitor(),
773 findScaleFactor->second);
774 }
775
Ed Tanous8a57ec02020-10-09 12:46:52 -0700776 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700777 {
778 std::cerr << "Sensor scaling factor " << factor
779 << " string " << strScaleFactor << "\n";
780 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700781 }
782
Vijay Khemka996bad12019-05-28 15:15:16 -0700783 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530784 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
785 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800786 {
James Feist17ab6e02019-06-25 12:28:13 -0700787 std::cerr << "error populating thresholds for "
788 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800789 }
790
Zev Weiss6b6891c2021-04-22 02:46:21 -0500791 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
792 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800793 {
Jason Ling5747fab2019-10-02 16:46:23 -0700794 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700795 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800796 continue;
797 }
798
Ed Tanous8a57ec02020-10-09 12:46:52 -0700799 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700800 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700801 std::cerr << "Sensor properties: Name \""
802 << psuProperty->labelTypeName << "\" Scale "
803 << psuProperty->sensorScaleFactor << " Min "
804 << psuProperty->minReading << " Max "
805 << psuProperty->maxReading << "\n";
806 }
807
808 std::string sensorName = psuProperty->labelTypeName;
809 if (customizedName)
810 {
811 if (sensorName.empty())
812 {
813 // Allow selective disabling of an individual sensor,
814 // by customizing its name to an empty string.
815 std::cerr << "Sensor disabled, empty string\n";
816 continue;
817 }
818 }
819 else
820 {
821 // Sensor name not customized, do prefix/suffix composition,
822 // preserving default behavior by using psuNameFromIndex.
823 sensorName =
824 psuNameFromIndex + " " + psuProperty->labelTypeName;
825 }
826
Ed Tanous8a57ec02020-10-09 12:46:52 -0700827 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700828 {
829 std::cerr << "Sensor name \"" << sensorName << "\" path \""
830 << sensorPathStr << "\" type \"" << sensorType
831 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700832 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800833 // destruct existing one first if already created
834 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800835 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800836 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800837 sensorName, std::move(sensorThresholds), *interfacePath,
Zev Weiss6b6891c2021-04-22 02:46:21 -0500838 findSensorUnit->second, factor, psuProperty->maxReading,
Lei YU7170a232021-02-04 16:19:27 +0800839 psuProperty->minReading, labelHead, thresholdConfSize,
840 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800841 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700842 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700843 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700844 {
845 std::cerr << "Created " << numCreated << " sensors so far\n";
846 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800847 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800848
849 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800850 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800851 combineEvents[*psuName + "OperationalStatus"] =
Yong Li3046a022020-04-03 13:01:02 +0800852 std::make_unique<PSUCombineEvent>(
853 objectServer, dbusConnection, io, *psuName, eventPathList,
Lei YU7170a232021-02-04 16:19:27 +0800854 groupEventPathList, "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800855 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700856
Ed Tanous8a57ec02020-10-09 12:46:52 -0700857 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700858 {
859 std::cerr << "Created total of " << numCreated << " sensors\n";
860 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800861 return;
862}
863
Zhikui Ren23c96e72020-11-05 22:32:28 -0800864void createSensors(
865 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
866 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
867 const std::shared_ptr<boost::container::flat_set<std::string>>&
868 sensorsChanged)
869{
870 auto getter = std::make_shared<GetSensorConfiguration>(
871 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
872 const ManagedObjectType& sensorConfigs) {
873 createSensorsCallback(io, objectServer, dbusConnection,
874 sensorConfigs, sensorsChanged);
875 });
876 getter->getConfiguration(
877 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
878}
879
Cheng C Yang916360b2019-05-07 18:47:16 +0800880void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800881{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500882 sensorTable = {{"power", sensor_paths::unitWatts},
883 {"curr", sensor_paths::unitAmperes},
884 {"temp", sensor_paths::unitDegreesC},
885 {"in", sensor_paths::unitVolts},
886 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800887
888 labelMatch = {{"pin", PSUProperty("Input Power", 3000, 0, 6)},
889 {"pout1", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700890 {"pout2", PSUProperty("Output Power", 3000, 0, 6)},
891 {"pout3", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700892 {"power1", PSUProperty("Output Power", 3000, 0, 6)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530893 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6)},
Cheng C Yangbdbde962019-04-26 22:50:34 +0800894 {"vin", PSUProperty("Input Voltage", 300, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530895 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700896 {"vout1", PSUProperty("Output Voltage", 255, 0, 3)},
897 {"vout2", PSUProperty("Output Voltage", 255, 0, 3)},
898 {"vout3", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700899 {"vout4", PSUProperty("Output Voltage", 255, 0, 3)},
900 {"vout5", PSUProperty("Output Voltage", 255, 0, 3)},
901 {"vout6", PSUProperty("Output Voltage", 255, 0, 3)},
902 {"vout7", PSUProperty("Output Voltage", 255, 0, 3)},
903 {"vout8", PSUProperty("Output Voltage", 255, 0, 3)},
904 {"vout9", PSUProperty("Output Voltage", 255, 0, 3)},
905 {"vout10", PSUProperty("Output Voltage", 255, 0, 3)},
906 {"vout11", PSUProperty("Output Voltage", 255, 0, 3)},
907 {"vout12", PSUProperty("Output Voltage", 255, 0, 3)},
908 {"vout13", PSUProperty("Output Voltage", 255, 0, 3)},
909 {"vout14", PSUProperty("Output Voltage", 255, 0, 3)},
910 {"vout15", PSUProperty("Output Voltage", 255, 0, 3)},
911 {"vout16", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling32047ef2020-09-30 15:43:32 -0700912 {"vout17", PSUProperty("Output Voltage", 255, 0, 3)},
913 {"vout18", PSUProperty("Output Voltage", 255, 0, 3)},
914 {"vout19", PSUProperty("Output Voltage", 255, 0, 3)},
915 {"vout20", PSUProperty("Output Voltage", 255, 0, 3)},
916 {"vout21", PSUProperty("Output Voltage", 255, 0, 3)},
917 {"vout22", PSUProperty("Output Voltage", 255, 0, 3)},
918 {"vout23", PSUProperty("Output Voltage", 255, 0, 3)},
919 {"vout24", PSUProperty("Output Voltage", 255, 0, 3)},
920 {"vout25", PSUProperty("Output Voltage", 255, 0, 3)},
921 {"vout26", PSUProperty("Output Voltage", 255, 0, 3)},
922 {"vout27", PSUProperty("Output Voltage", 255, 0, 3)},
923 {"vout28", PSUProperty("Output Voltage", 255, 0, 3)},
924 {"vout29", PSUProperty("Output Voltage", 255, 0, 3)},
925 {"vout30", PSUProperty("Output Voltage", 255, 0, 3)},
926 {"vout31", PSUProperty("Output Voltage", 255, 0, 3)},
927 {"vout32", PSUProperty("Output Voltage", 255, 0, 3)},
Zev Weisseb0b84d2021-04-22 15:03:38 -0500928 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700929 {"in1", PSUProperty("Output Voltage", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800930 {"iin", PSUProperty("Input Current", 20, 0, 3)},
931 {"iout1", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700932 {"iout2", PSUProperty("Output Current", 255, 0, 3)},
933 {"iout3", PSUProperty("Output Current", 255, 0, 3)},
Peter Lundgren48b44232020-01-30 16:02:11 -0800934 {"iout4", PSUProperty("Output Current", 255, 0, 3)},
935 {"iout5", PSUProperty("Output Current", 255, 0, 3)},
936 {"iout6", PSUProperty("Output Current", 255, 0, 3)},
937 {"iout7", PSUProperty("Output Current", 255, 0, 3)},
938 {"iout8", PSUProperty("Output Current", 255, 0, 3)},
939 {"iout9", PSUProperty("Output Current", 255, 0, 3)},
940 {"iout10", PSUProperty("Output Current", 255, 0, 3)},
941 {"iout11", PSUProperty("Output Current", 255, 0, 3)},
942 {"iout12", PSUProperty("Output Current", 255, 0, 3)},
943 {"iout13", PSUProperty("Output Current", 255, 0, 3)},
944 {"iout14", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700945 {"curr1", PSUProperty("Output Current", 255, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530946 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800947 {"temp1", PSUProperty("Temperature", 127, -128, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700948 {"temp2", PSUProperty("Temperature", 127, -128, 3)},
949 {"temp3", PSUProperty("Temperature", 127, -128, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700950 {"temp4", PSUProperty("Temperature", 127, -128, 3)},
951 {"temp5", PSUProperty("Temperature", 127, -128, 3)},
Alex Qiuf5709b42020-01-29 13:29:50 -0800952 {"temp6", PSUProperty("Temperature", 127, -128, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530953 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3)},
Cheng C Yang8dbb3952019-05-23 00:03:12 +0800954 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0)},
955 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +0800956
957 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +0800958
959 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
960 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
961
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800962 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
963 {"Failure", {"in2_alarm"}},
964 {"ACLost", {"in1_beep"}},
965 {"ConfigureError", {"in1_fault"}}};
966
967 groupEventMatch = {{"FanFault",
968 {{"fan1", {"fan1_alarm", "fan1_fault"}},
969 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +0800970}
971
James Feistb6c0b912019-07-09 12:21:44 -0700972int main()
Cheng C Yang209ec562019-03-12 16:37:44 +0800973{
974 boost::asio::io_service io;
975 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
976
977 systemBus->request_name("xyz.openbmc_project.PSUSensor");
978 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800979 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800980 auto sensorsChanged =
981 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +0800982
Cheng C Yang916360b2019-05-07 18:47:16 +0800983 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +0800984
Zhikui Ren23c96e72020-11-05 22:32:28 -0800985 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +0800986 boost::asio::deadline_timer filterTimer(io);
987 std::function<void(sdbusplus::message::message&)> eventHandler =
988 [&](sdbusplus::message::message& message) {
989 if (message.is_method_error())
990 {
991 std::cerr << "callback method error\n";
992 return;
993 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800994 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +0800995 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +0800996 filterTimer.async_wait([&](const boost::system::error_code& ec) {
997 if (ec == boost::asio::error::operation_aborted)
998 {
999 return;
1000 }
Ed Tanous8a57ec02020-10-09 12:46:52 -07001001 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +08001002 {
1003 std::cerr << "timer error\n";
1004 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001005 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +08001006 });
1007 };
1008
1009 for (const char* type : sensorTypes)
1010 {
1011 auto match = std::make_unique<sdbusplus::bus::match::match>(
1012 static_cast<sdbusplus::bus::bus&>(*systemBus),
1013 "type='signal',member='PropertiesChanged',path_namespace='" +
1014 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
1015 eventHandler);
1016 matches.emplace_back(std::move(match));
1017 }
Bruce Lee1263c3d2021-06-04 15:16:33 +08001018
1019 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001020 io.run();
1021}