blob: 3f0dfbfad9d2a3263878afad65309b0803b3ca2f [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
Vijay Khemka996bad12019-05-28 15:15:16 -0700469 /* Find array of labels to be exposed if it is defined in config */
470 std::vector<std::string> findLabels;
471 auto findLabelObj = baseConfig->second.find("Labels");
472 if (findLabelObj != baseConfig->second.end())
473 {
474 findLabels =
475 std::get<std::vector<std::string>>(findLabelObj->second);
476 }
477
Jason Ling5747fab2019-10-02 16:46:23 -0700478 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
479 std::smatch matches;
480
Cheng C Yange50345b2019-04-02 17:26:15 +0800481 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800482 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530483 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800484 std::string labelHead;
485 std::string sensorPathStr = sensorPath.string();
486 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700487 std::string sensorNameSubStr{""};
488 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
489 {
Josh Lehan06494452019-10-31 09:49:16 -0700490 // hwmon *_input filename without number:
491 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700492 sensorNameSubStr = matches[1];
493 }
494 else
495 {
Josh Lehan06494452019-10-31 09:49:16 -0700496 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700497 << sensorNameStr;
498 continue;
499 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800500
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530501 std::string labelPath;
502
503 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700504 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530505 if (pos != std::string::npos)
506 {
507
508 std::string sensorPathStrMax = sensorPathStr.substr(pos);
509 if (sensorPathStrMax.compare("_max") == 0)
510 {
511 labelPath =
512 boost::replace_all_copy(sensorPathStr, "max", "label");
513 maxLabel = true;
514 }
515 else
516 {
517 labelPath = boost::replace_all_copy(sensorPathStr, "input",
518 "label");
519 maxLabel = false;
520 }
521 }
522 else
523 {
524 continue;
525 }
526
Cheng C Yangecba9de2019-09-12 23:41:50 +0800527 std::ifstream labelFile(labelPath);
528 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800529 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700530 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800531 {
532 std::cerr << "Input file " << sensorPath
533 << " has no corresponding label file\n";
534 }
Josh Lehan06494452019-10-31 09:49:16 -0700535 // hwmon *_input filename with number:
536 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700537 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800538 }
539 else
540 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800541 std::string label;
542 std::getline(labelFile, label);
543 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800544 auto findSensor = sensors.find(label);
545 if (findSensor != sensors.end())
546 {
547 continue;
548 }
549
Josh Lehan06494452019-10-31 09:49:16 -0700550 // hwmon corresponding *_label file contents:
551 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700552 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800553 }
554
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530555 /* append "max" for labelMatch */
556 if (maxLabel)
557 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700558 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530559 }
560
Ed Tanous8a57ec02020-10-09 12:46:52 -0700561 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700562 {
563 std::cerr << "Sensor type=\"" << sensorNameSubStr
564 << "\" label=\"" << labelHead << "\"\n";
565 }
566
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530567 checkPWMSensor(sensorPath, labelHead, *interfacePath,
568 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800569
Vijay Khemka996bad12019-05-28 15:15:16 -0700570 if (!findLabels.empty())
571 {
572 /* Check if this labelHead is enabled in config file */
573 if (std::find(findLabels.begin(), findLabels.end(),
574 labelHead) == findLabels.end())
575 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700576 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800577 {
578 std::cerr << "could not find " << labelHead
579 << " in the Labels list\n";
580 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700581 continue;
582 }
583 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800584
Cheng C Yange50345b2019-04-02 17:26:15 +0800585 auto findProperty = labelMatch.find(labelHead);
586 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800587 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700588 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700589 {
590 std::cerr << "Could not find matching default property for "
591 << labelHead << "\n";
592 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800593 continue;
594 }
595
Josh Lehan74d9bd92019-10-31 08:51:58 -0700596 // Protect the hardcoded labelMatch list from changes,
597 // by making a copy and modifying that instead.
598 // Avoid bleedthrough of one device's customizations to
599 // the next device, as each should be independently customizable.
600 psuProperties.push_back(findProperty->second);
601 auto psuProperty = psuProperties.rbegin();
602
603 // Use label head as prefix for reading from config file,
604 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
605 std::string keyName = labelHead + "_Name";
606 std::string keyScale = labelHead + "_Scale";
607 std::string keyMin = labelHead + "_Min";
608 std::string keyMax = labelHead + "_Max";
609
610 bool customizedName = false;
611 auto findCustomName = baseConfig->second.find(keyName);
612 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700613 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700614 try
615 {
616 psuProperty->labelTypeName = std::visit(
617 VariantToStringVisitor(), findCustomName->second);
618 }
619 catch (std::invalid_argument&)
620 {
621 std::cerr << "Unable to parse " << keyName << "\n";
622 continue;
623 }
624
625 // All strings are valid, including empty string
626 customizedName = true;
627 }
628
629 bool customizedScale = false;
630 auto findCustomScale = baseConfig->second.find(keyScale);
631 if (findCustomScale != baseConfig->second.end())
632 {
633 try
634 {
635 psuProperty->sensorScaleFactor = std::visit(
636 VariantToUnsignedIntVisitor(), findCustomScale->second);
637 }
638 catch (std::invalid_argument&)
639 {
640 std::cerr << "Unable to parse " << keyScale << "\n";
641 continue;
642 }
643
644 // Avoid later division by zero
645 if (psuProperty->sensorScaleFactor > 0)
646 {
647 customizedScale = true;
648 }
649 else
650 {
651 std::cerr << "Unable to accept " << keyScale << "\n";
652 continue;
653 }
654 }
655
656 auto findCustomMin = baseConfig->second.find(keyMin);
657 if (findCustomMin != baseConfig->second.end())
658 {
659 try
660 {
661 psuProperty->minReading = std::visit(
662 VariantToDoubleVisitor(), findCustomMin->second);
663 }
664 catch (std::invalid_argument&)
665 {
666 std::cerr << "Unable to parse " << keyMin << "\n";
667 continue;
668 }
669 }
670
671 auto findCustomMax = baseConfig->second.find(keyMax);
672 if (findCustomMax != baseConfig->second.end())
673 {
674 try
675 {
676 psuProperty->maxReading = std::visit(
677 VariantToDoubleVisitor(), findCustomMax->second);
678 }
679 catch (std::invalid_argument&)
680 {
681 std::cerr << "Unable to parse " << keyMax << "\n";
682 continue;
683 }
684 }
685
686 if (!(psuProperty->minReading < psuProperty->maxReading))
687 {
688 std::cerr << "Min must be less than Max\n";
689 continue;
690 }
691
692 // If the sensor name is being customized by config file,
693 // then prefix/suffix composition becomes not necessary,
694 // and in fact not wanted, because it gets in the way.
695 std::string psuNameFromIndex;
696 if (!customizedName)
697 {
698 /* Find out sensor name index for this label */
699 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500700 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700701 if (std::regex_search(labelHead, matches, rgx))
702 {
703 nameIndex = std::stoi(matches[1]);
704
705 // Decrement to preserve alignment, because hwmon
706 // human-readable filenames and labels use 1-based
707 // numbering, but the "Name", "Name1", "Name2", etc. naming
708 // convention (the psuNames vector) uses 0-based numbering.
709 if (nameIndex > 0)
710 {
711 --nameIndex;
712 }
713 }
714 else
715 {
716 nameIndex = 0;
717 }
718
719 if (psuNames.size() <= nameIndex)
720 {
721 std::cerr << "Could not pair " << labelHead
722 << " with a Name field\n";
723 continue;
724 }
725
726 psuNameFromIndex = psuNames[nameIndex];
727
Ed Tanous8a57ec02020-10-09 12:46:52 -0700728 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700729 {
730 std::cerr << "Sensor label head " << labelHead
731 << " paired with " << psuNameFromIndex
732 << " at index " << nameIndex << "\n";
733 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700734 }
735
Cheng C Yang58b2b532019-05-31 00:19:45 +0800736 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
737
Josh Lehan74d9bd92019-10-31 08:51:58 -0700738 // Similarly, if sensor scaling factor is being customized,
739 // then the below power-of-10 constraint becomes unnecessary,
740 // as config should be able to specify an arbitrary divisor.
741 unsigned int factor = psuProperty->sensorScaleFactor;
742 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700743 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700744 // Preserve existing usage of hardcoded labelMatch table below
745 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700746
Josh Lehan74d9bd92019-10-31 08:51:58 -0700747 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700748 char firstChar =
749 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700750 std::string strScaleFactor =
751 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
752
753 // Preserve existing configs by accepting earlier syntax,
754 // example CurrScaleFactor, PowerScaleFactor, ...
755 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
756 if (findScaleFactor != baseConfig->second.end())
757 {
758 factor = std::visit(VariantToIntVisitor(),
759 findScaleFactor->second);
760 }
761
Ed Tanous8a57ec02020-10-09 12:46:52 -0700762 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700763 {
764 std::cerr << "Sensor scaling factor " << factor
765 << " string " << strScaleFactor << "\n";
766 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700767 }
768
Vijay Khemka996bad12019-05-28 15:15:16 -0700769 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530770 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
771 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800772 {
James Feist17ab6e02019-06-25 12:28:13 -0700773 std::cerr << "error populating thresholds for "
774 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800775 }
776
Zev Weiss6b6891c2021-04-22 02:46:21 -0500777 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
778 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800779 {
Jason Ling5747fab2019-10-02 16:46:23 -0700780 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700781 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800782 continue;
783 }
784
Ed Tanous8a57ec02020-10-09 12:46:52 -0700785 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700786 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700787 std::cerr << "Sensor properties: Name \""
788 << psuProperty->labelTypeName << "\" Scale "
789 << psuProperty->sensorScaleFactor << " Min "
790 << psuProperty->minReading << " Max "
791 << psuProperty->maxReading << "\n";
792 }
793
794 std::string sensorName = psuProperty->labelTypeName;
795 if (customizedName)
796 {
797 if (sensorName.empty())
798 {
799 // Allow selective disabling of an individual sensor,
800 // by customizing its name to an empty string.
801 std::cerr << "Sensor disabled, empty string\n";
802 continue;
803 }
804 }
805 else
806 {
807 // Sensor name not customized, do prefix/suffix composition,
808 // preserving default behavior by using psuNameFromIndex.
809 sensorName =
810 psuNameFromIndex + " " + psuProperty->labelTypeName;
811 }
812
Ed Tanous8a57ec02020-10-09 12:46:52 -0700813 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700814 {
815 std::cerr << "Sensor name \"" << sensorName << "\" path \""
816 << sensorPathStr << "\" type \"" << sensorType
817 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700818 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800819 // destruct existing one first if already created
820 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800821 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800822 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800823 sensorName, std::move(sensorThresholds), *interfacePath,
Zev Weiss6b6891c2021-04-22 02:46:21 -0500824 findSensorUnit->second, factor, psuProperty->maxReading,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800825 psuProperty->minReading, labelHead, thresholdConfSize);
Yong Libf8b1da2020-04-15 16:32:50 +0800826 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700827 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700828 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700829 {
830 std::cerr << "Created " << numCreated << " sensors so far\n";
831 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800832 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800833
834 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800835 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800836 combineEvents[*psuName + "OperationalStatus"] =
Yong Li3046a022020-04-03 13:01:02 +0800837 std::make_unique<PSUCombineEvent>(
838 objectServer, dbusConnection, io, *psuName, eventPathList,
839 groupEventPathList, "OperationalStatus");
Cheng C Yang209ec562019-03-12 16:37:44 +0800840 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700841
Ed Tanous8a57ec02020-10-09 12:46:52 -0700842 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700843 {
844 std::cerr << "Created total of " << numCreated << " sensors\n";
845 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800846 return;
847}
848
Zhikui Ren23c96e72020-11-05 22:32:28 -0800849void createSensors(
850 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
851 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
852 const std::shared_ptr<boost::container::flat_set<std::string>>&
853 sensorsChanged)
854{
855 auto getter = std::make_shared<GetSensorConfiguration>(
856 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
857 const ManagedObjectType& sensorConfigs) {
858 createSensorsCallback(io, objectServer, dbusConnection,
859 sensorConfigs, sensorsChanged);
860 });
861 getter->getConfiguration(
862 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
863}
864
Cheng C Yang916360b2019-05-07 18:47:16 +0800865void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800866{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500867 sensorTable = {{"power", sensor_paths::unitWatts},
868 {"curr", sensor_paths::unitAmperes},
869 {"temp", sensor_paths::unitDegreesC},
870 {"in", sensor_paths::unitVolts},
871 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800872
873 labelMatch = {{"pin", PSUProperty("Input Power", 3000, 0, 6)},
874 {"pout1", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700875 {"pout2", PSUProperty("Output Power", 3000, 0, 6)},
876 {"pout3", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700877 {"power1", PSUProperty("Output Power", 3000, 0, 6)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530878 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6)},
Cheng C Yangbdbde962019-04-26 22:50:34 +0800879 {"vin", PSUProperty("Input Voltage", 300, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530880 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700881 {"vout1", PSUProperty("Output Voltage", 255, 0, 3)},
882 {"vout2", PSUProperty("Output Voltage", 255, 0, 3)},
883 {"vout3", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700884 {"vout4", PSUProperty("Output Voltage", 255, 0, 3)},
885 {"vout5", PSUProperty("Output Voltage", 255, 0, 3)},
886 {"vout6", PSUProperty("Output Voltage", 255, 0, 3)},
887 {"vout7", PSUProperty("Output Voltage", 255, 0, 3)},
888 {"vout8", PSUProperty("Output Voltage", 255, 0, 3)},
889 {"vout9", PSUProperty("Output Voltage", 255, 0, 3)},
890 {"vout10", PSUProperty("Output Voltage", 255, 0, 3)},
891 {"vout11", PSUProperty("Output Voltage", 255, 0, 3)},
892 {"vout12", PSUProperty("Output Voltage", 255, 0, 3)},
893 {"vout13", PSUProperty("Output Voltage", 255, 0, 3)},
894 {"vout14", PSUProperty("Output Voltage", 255, 0, 3)},
895 {"vout15", PSUProperty("Output Voltage", 255, 0, 3)},
896 {"vout16", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling32047ef2020-09-30 15:43:32 -0700897 {"vout17", PSUProperty("Output Voltage", 255, 0, 3)},
898 {"vout18", PSUProperty("Output Voltage", 255, 0, 3)},
899 {"vout19", PSUProperty("Output Voltage", 255, 0, 3)},
900 {"vout20", PSUProperty("Output Voltage", 255, 0, 3)},
901 {"vout21", PSUProperty("Output Voltage", 255, 0, 3)},
902 {"vout22", PSUProperty("Output Voltage", 255, 0, 3)},
903 {"vout23", PSUProperty("Output Voltage", 255, 0, 3)},
904 {"vout24", PSUProperty("Output Voltage", 255, 0, 3)},
905 {"vout25", PSUProperty("Output Voltage", 255, 0, 3)},
906 {"vout26", PSUProperty("Output Voltage", 255, 0, 3)},
907 {"vout27", PSUProperty("Output Voltage", 255, 0, 3)},
908 {"vout28", PSUProperty("Output Voltage", 255, 0, 3)},
909 {"vout29", PSUProperty("Output Voltage", 255, 0, 3)},
910 {"vout30", PSUProperty("Output Voltage", 255, 0, 3)},
911 {"vout31", PSUProperty("Output Voltage", 255, 0, 3)},
912 {"vout32", PSUProperty("Output Voltage", 255, 0, 3)},
Zev Weisseb0b84d2021-04-22 15:03:38 -0500913 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700914 {"in1", PSUProperty("Output Voltage", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800915 {"iin", PSUProperty("Input Current", 20, 0, 3)},
916 {"iout1", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700917 {"iout2", PSUProperty("Output Current", 255, 0, 3)},
918 {"iout3", PSUProperty("Output Current", 255, 0, 3)},
Peter Lundgren48b44232020-01-30 16:02:11 -0800919 {"iout4", PSUProperty("Output Current", 255, 0, 3)},
920 {"iout5", PSUProperty("Output Current", 255, 0, 3)},
921 {"iout6", PSUProperty("Output Current", 255, 0, 3)},
922 {"iout7", PSUProperty("Output Current", 255, 0, 3)},
923 {"iout8", PSUProperty("Output Current", 255, 0, 3)},
924 {"iout9", PSUProperty("Output Current", 255, 0, 3)},
925 {"iout10", PSUProperty("Output Current", 255, 0, 3)},
926 {"iout11", PSUProperty("Output Current", 255, 0, 3)},
927 {"iout12", PSUProperty("Output Current", 255, 0, 3)},
928 {"iout13", PSUProperty("Output Current", 255, 0, 3)},
929 {"iout14", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700930 {"curr1", PSUProperty("Output Current", 255, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530931 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800932 {"temp1", PSUProperty("Temperature", 127, -128, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700933 {"temp2", PSUProperty("Temperature", 127, -128, 3)},
934 {"temp3", PSUProperty("Temperature", 127, -128, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700935 {"temp4", PSUProperty("Temperature", 127, -128, 3)},
936 {"temp5", PSUProperty("Temperature", 127, -128, 3)},
Alex Qiuf5709b42020-01-29 13:29:50 -0800937 {"temp6", PSUProperty("Temperature", 127, -128, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530938 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3)},
Cheng C Yang8dbb3952019-05-23 00:03:12 +0800939 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0)},
940 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +0800941
942 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +0800943
944 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
945 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
946
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800947 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
948 {"Failure", {"in2_alarm"}},
949 {"ACLost", {"in1_beep"}},
950 {"ConfigureError", {"in1_fault"}}};
951
952 groupEventMatch = {{"FanFault",
953 {{"fan1", {"fan1_alarm", "fan1_fault"}},
954 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +0800955}
956
James Feistb6c0b912019-07-09 12:21:44 -0700957int main()
Cheng C Yang209ec562019-03-12 16:37:44 +0800958{
959 boost::asio::io_service io;
960 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
961
962 systemBus->request_name("xyz.openbmc_project.PSUSensor");
963 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800964 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800965 auto sensorsChanged =
966 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +0800967
Cheng C Yang916360b2019-05-07 18:47:16 +0800968 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +0800969
Zhikui Ren23c96e72020-11-05 22:32:28 -0800970 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +0800971 boost::asio::deadline_timer filterTimer(io);
972 std::function<void(sdbusplus::message::message&)> eventHandler =
973 [&](sdbusplus::message::message& message) {
974 if (message.is_method_error())
975 {
976 std::cerr << "callback method error\n";
977 return;
978 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800979 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +0800980 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +0800981 filterTimer.async_wait([&](const boost::system::error_code& ec) {
982 if (ec == boost::asio::error::operation_aborted)
983 {
984 return;
985 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700986 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +0800987 {
988 std::cerr << "timer error\n";
989 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800990 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +0800991 });
992 };
993
994 for (const char* type : sensorTypes)
995 {
996 auto match = std::make_unique<sdbusplus::bus::match::match>(
997 static_cast<sdbusplus::bus::bus&>(*systemBus),
998 "type='signal',member='PropertiesChanged',path_namespace='" +
999 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
1000 eventHandler);
1001 matches.emplace_back(std::move(match));
1002 }
Bruce Lee1263c3d2021-06-04 15:16:33 +08001003
1004 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001005 io.run();
1006}