blob: 83c0c58c948a586923a9f48604159774e8173d06 [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
Jeff Lina0683a82021-02-22 13:56:55 +080043static constexpr std::array<const char*, 22> sensorTypes = {
Josh Lehan62084c82020-06-22 15:27:12 -070044 "xyz.openbmc_project.Configuration.ADM1272",
45 "xyz.openbmc_project.Configuration.ADM1278",
Jeff Lina0683a82021-02-22 13:56:55 +080046 "xyz.openbmc_project.Configuration.DPS800",
Josh Lehan62084c82020-06-22 15:27:12 -070047 "xyz.openbmc_project.Configuration.INA219",
Devjit Gopalpurde65ef72019-10-30 04:47:35 -070048 "xyz.openbmc_project.Configuration.INA230",
Alex Qiu6690d8f2020-01-22 17:56:33 -080049 "xyz.openbmc_project.Configuration.ISL68137",
Jason Lingdfad1ff2020-07-29 15:43:11 -070050 "xyz.openbmc_project.Configuration.ISL68220",
Gaurav Gandhi49585582020-12-22 01:01:24 +000051 "xyz.openbmc_project.Configuration.ISL68223",
52 "xyz.openbmc_project.Configuration.ISL69243",
Jeff Lina0683a82021-02-22 13:56:55 +080053 "xyz.openbmc_project.Configuration.ISL69260",
Alex Qiuf5709b42020-01-29 13:29:50 -080054 "xyz.openbmc_project.Configuration.MAX16601",
Gaurav Gandhi49585582020-12-22 01:01:24 +000055 "xyz.openbmc_project.Configuration.MAX20710",
Alex Qiu6690d8f2020-01-22 17:56:33 -080056 "xyz.openbmc_project.Configuration.MAX20730",
57 "xyz.openbmc_project.Configuration.MAX20734",
58 "xyz.openbmc_project.Configuration.MAX20796",
59 "xyz.openbmc_project.Configuration.MAX34451",
Josh Lehan62084c82020-06-22 15:27:12 -070060 "xyz.openbmc_project.Configuration.pmbus",
61 "xyz.openbmc_project.Configuration.PXE1610",
Jeff Lina0683a82021-02-22 13:56:55 +080062 "xyz.openbmc_project.Configuration.RAA228000",
63 "xyz.openbmc_project.Configuration.RAA228228",
64 "xyz.openbmc_project.Configuration.RAA229004",
65 "xyz.openbmc_project.Configuration.TPS546D24"};
Cheng C Yang209ec562019-03-12 16:37:44 +080066
Alex Qiu6690d8f2020-01-22 17:56:33 -080067static std::vector<std::string> pmbusNames = {
Jeff Lina0683a82021-02-22 13:56:55 +080068 "adm1272", "adm1278", "dps800", "ina219", "ina230", "isl68137",
69 "isl68220", "isl68223", "isl69243", "isl69260", "max16601", "max20710",
70 "max20730", "max20734", "max20796", "max34451", "pmbus", "pxe1610",
71 "raa228000", "raa228228", "raa229004", "tps546d24"};
Josh Lehan0830c7b2019-10-08 16:35:09 -070072
Cheng C Yang209ec562019-03-12 16:37:44 +080073namespace fs = std::filesystem;
74
Yong Libf8b1da2020-04-15 16:32:50 +080075static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +080076 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +080077static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
78 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +080079static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
80 pwmSensors;
81static boost::container::flat_map<std::string, std::string> sensorTable;
82static boost::container::flat_map<std::string, PSUProperty> labelMatch;
83static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +080084static boost::container::flat_map<std::string, std::vector<std::string>>
85 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +080086static boost::container::flat_map<
87 std::string,
88 boost::container::flat_map<std::string, std::vector<std::string>>>
89 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +080090static boost::container::flat_map<std::string, std::vector<std::string>>
91 limitEventMatch;
92
Josh Lehan74d9bd92019-10-31 08:51:58 -070093static std::vector<PSUProperty> psuProperties;
94
Cheng C Yang58b2b532019-05-31 00:19:45 +080095// Function CheckEvent will check each attribute from eventMatch table in the
96// sysfs. If the attributes exists in sysfs, then store the complete path
97// of the attribute into eventPathList.
98void checkEvent(
99 const std::string& directory,
100 const boost::container::flat_map<std::string, std::vector<std::string>>&
101 eventMatch,
102 boost::container::flat_map<std::string, std::vector<std::string>>&
103 eventPathList)
104{
105 for (const auto& match : eventMatch)
106 {
107 const std::vector<std::string>& eventAttrs = match.second;
108 const std::string& eventName = match.first;
109 for (const auto& eventAttr : eventAttrs)
110 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700111 std::string eventPath = directory;
112 eventPath += "/";
113 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800114
115 std::ifstream eventFile(eventPath);
116 if (!eventFile.good())
117 {
118 continue;
119 }
120
121 eventPathList[eventName].push_back(eventPath);
122 }
123 }
124}
125
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800126// Check Group Events which contains more than one targets in each combine
127// events.
128void checkGroupEvent(
129 const std::string& directory,
130 const boost::container::flat_map<
131 std::string,
132 boost::container::flat_map<std::string, std::vector<std::string>>>&
133 groupEventMatch,
134 boost::container::flat_map<
135 std::string,
136 boost::container::flat_map<std::string, std::vector<std::string>>>&
137 groupEventPathList)
138{
139 for (const auto& match : groupEventMatch)
140 {
141 const std::string& groupEventName = match.first;
142 const boost::container::flat_map<std::string, std::vector<std::string>>
143 events = match.second;
144 boost::container::flat_map<std::string, std::vector<std::string>>
145 pathList;
146 for (const auto& match : events)
147 {
148 const std::string& eventName = match.first;
149 const std::vector<std::string>& eventAttrs = match.second;
150 for (const auto& eventAttr : eventAttrs)
151 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700152 std::string eventPath = directory;
153 eventPath += "/";
154 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800155 std::ifstream eventFile(eventPath);
156 if (!eventFile.good())
157 {
158 continue;
159 }
160
161 pathList[eventName].push_back(eventPath);
162 }
163 }
164 groupEventPathList[groupEventName] = pathList;
165 }
166}
167
Cheng C Yang58b2b532019-05-31 00:19:45 +0800168// Function checkEventLimits will check all the psu related xxx_input attributes
169// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
170// xxx_min_alarm exist, then store the existing paths of the alarm attributes
171// to eventPathList.
172void checkEventLimits(
173 const std::string& sensorPathStr,
174 const boost::container::flat_map<std::string, std::vector<std::string>>&
175 limitEventMatch,
176 boost::container::flat_map<std::string, std::vector<std::string>>&
177 eventPathList)
178{
Lei YUa2c7cea2020-12-23 14:07:28 +0800179 auto attributePartPos = sensorPathStr.find_last_of('_');
180 if (attributePartPos == std::string::npos)
181 {
182 // There is no '_' in the string, skip it
183 return;
184 }
185 auto attributePart =
186 std::string_view(sensorPathStr).substr(attributePartPos + 1);
187 if (attributePart != "input")
188 {
189 // If the sensor is not xxx_input, skip it
190 return;
191 }
192
193 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800194 for (const auto& limitMatch : limitEventMatch)
195 {
196 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
197 const std::string& eventName = limitMatch.first;
198 for (const auto& limitEventAttr : limitEventAttrs)
199 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800200 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800201 std::ifstream eventFile(limitEventPath);
202 if (!eventFile.good())
203 {
204 continue;
205 }
206 eventPathList[eventName].push_back(limitEventPath);
207 }
208 }
209}
Cheng C Yang916360b2019-05-07 18:47:16 +0800210
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530211static void
212 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
213 const std::string& interfacePath,
214 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
215 sdbusplus::asio::object_server& objectServer,
216 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800217{
218 for (const auto& pwmName : pwmTable)
219 {
220 if (pwmName.first != labelHead)
221 {
222 continue;
223 }
224
225 const std::string& sensorPathStr = sensorPath.string();
226 const std::string& pwmPathStr =
227 boost::replace_all_copy(sensorPathStr, "input", "target");
228 std::ifstream pwmFile(pwmPathStr);
229 if (!pwmFile.good())
230 {
231 continue;
232 }
233
234 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
235 if (findPWMSensor != pwmSensors.end())
236 {
237 continue;
238 }
239
240 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530241 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
242 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800243 }
244}
245
Zhikui Ren23c96e72020-11-05 22:32:28 -0800246static void createSensorsCallback(
247 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
248 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
249 const ManagedObjectType& sensorConfigs,
250 const std::shared_ptr<boost::container::flat_set<std::string>>&
251 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800252{
Josh Lehan49cfba92019-10-08 16:50:42 -0700253 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800254 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800255
256 std::vector<fs::path> pmbusPaths;
257 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
258 {
259 std::cerr << "No PSU sensors in system\n";
260 return;
261 }
262
263 boost::container::flat_set<std::string> directories;
264 for (const auto& pmbusPath : pmbusPaths)
265 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800266 boost::container::flat_map<std::string, std::vector<std::string>>
267 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800268 boost::container::flat_map<
269 std::string,
270 boost::container::flat_map<std::string, std::vector<std::string>>>
271 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800272
273 std::ifstream nameFile(pmbusPath);
274 if (!nameFile.good())
275 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700276 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800277 continue;
278 }
279
280 std::string pmbusName;
281 std::getline(nameFile, pmbusName);
282 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700283
284 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
285 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800286 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700287 // To avoid this error message, add your driver name to
288 // the pmbusNames vector at the top of this file.
289 std::cerr << "Driver name " << pmbusName
290 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800291 continue;
292 }
293
294 const std::string* psuName;
Cheng C Yang209ec562019-03-12 16:37:44 +0800295 auto directory = pmbusPath.parent_path();
296
297 auto ret = directories.insert(directory.string());
298 if (!ret.second)
299 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700300 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800301 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800302 }
303
James Feistb6c0b912019-07-09 12:21:44 -0700304 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800305 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700306 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800307 if (findHyphen == std::string::npos)
308 {
309 std::cerr << "found bad device" << deviceName << "\n";
310 continue;
311 }
312 std::string busStr = deviceName.substr(0, findHyphen);
313 std::string addrStr = deviceName.substr(findHyphen + 1);
314
315 size_t bus = 0;
316 size_t addr = 0;
317
318 try
319 {
320 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700321 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800322 }
James Feistb6c0b912019-07-09 12:21:44 -0700323 catch (std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800324 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700325 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
326 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800327 continue;
328 }
329
Cheng C Yang209ec562019-03-12 16:37:44 +0800330 const std::pair<std::string, boost::container::flat_map<
331 std::string, BasicVariantType>>*
332 baseConfig = nullptr;
333 const SensorData* sensorData = nullptr;
334 const std::string* interfacePath = nullptr;
335 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800336 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800337
338 for (const std::pair<sdbusplus::message::object_path, SensorData>&
339 sensor : sensorConfigs)
340 {
341 sensorData = &(sensor.second);
342 for (const char* type : sensorTypes)
343 {
344 auto sensorBase = sensorData->find(type);
345 if (sensorBase != sensorData->end())
346 {
347 baseConfig = &(*sensorBase);
348 sensorType = type;
349 break;
350 }
351 }
352 if (baseConfig == nullptr)
353 {
354 std::cerr << "error finding base configuration for "
355 << deviceName << "\n";
356 continue;
357 }
358
359 auto configBus = baseConfig->second.find("Bus");
360 auto configAddress = baseConfig->second.find("Address");
361
362 if (configBus == baseConfig->second.end() ||
363 configAddress == baseConfig->second.end())
364 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800365 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800366 continue;
367 }
368
Cheng C Yang58b2b532019-05-31 00:19:45 +0800369 const uint64_t* confBus;
370 const uint64_t* confAddr;
371 if (!(confBus = std::get_if<uint64_t>(&(configBus->second))) ||
372 !(confAddr = std::get_if<uint64_t>(&(configAddress->second))))
373 {
374 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700375 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800376 continue;
377 }
378
379 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800380 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700381 std::cerr << "Configuration skipping " << *confBus << "-"
382 << *confAddr << " because not " << bus << "-" << addr
383 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800384 continue;
385 }
386
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800387 std::vector<thresholds::Threshold> confThresholds;
388 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
389 {
390 std::cerr << "error populating totoal thresholds\n";
391 }
392 thresholdConfSize = confThresholds.size();
393
Cheng C Yang209ec562019-03-12 16:37:44 +0800394 interfacePath = &(sensor.first.str);
395 break;
396 }
397 if (interfacePath == nullptr)
398 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700399 // To avoid this error message, add your export map entry,
400 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800401 std::cerr << "failed to find match for " << deviceName << "\n";
402 continue;
403 }
404
Cheng C Yange50345b2019-04-02 17:26:15 +0800405 auto findPSUName = baseConfig->second.find("Name");
406 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800407 {
408 std::cerr << "could not determine configuration name for "
409 << deviceName << "\n";
410 continue;
411 }
412
Cheng C Yang58b2b532019-05-31 00:19:45 +0800413 if (!(psuName = std::get_if<std::string>(&(findPSUName->second))))
414 {
415 std::cerr << "Cannot find psu name, invalid configuration\n";
416 continue;
417 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800418
419 // on rescans, only update sensors we were signaled by
420 if (!firstScan)
421 {
422 std::string psuNameStr = "/" + *psuName;
423 auto it =
424 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
425 [psuNameStr](std::string& s) {
426 return boost::ends_with(s, psuNameStr);
427 });
428
429 if (it == sensorsChanged->end())
430 {
431 continue;
432 }
433 sensorsChanged->erase(it);
434 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800435 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800436 checkGroupEvent(directory.string(), groupEventMatch,
437 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800438
Vijay Khemka996bad12019-05-28 15:15:16 -0700439 /* Check if there are more sensors in the same interface */
440 int i = 1;
441 std::vector<std::string> psuNames;
442 do
443 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700444 // Individual string fields: Name, Name1, Name2, Name3, ...
Vijay Khemka996bad12019-05-28 15:15:16 -0700445 psuNames.push_back(std::get<std::string>(findPSUName->second));
446 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
447 } while (findPSUName != baseConfig->second.end());
448
Cheng C Yange50345b2019-04-02 17:26:15 +0800449 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700450 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800451 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800452 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800453 continue;
454 }
455
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530456 /* read max value in sysfs for in, curr, power, temp, ... */
457 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
458 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700459 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530460 {
461 std::cerr << "No max name in PSU \n";
462 }
463 }
464
Vijay Khemka996bad12019-05-28 15:15:16 -0700465 /* Find array of labels to be exposed if it is defined in config */
466 std::vector<std::string> findLabels;
467 auto findLabelObj = baseConfig->second.find("Labels");
468 if (findLabelObj != baseConfig->second.end())
469 {
470 findLabels =
471 std::get<std::vector<std::string>>(findLabelObj->second);
472 }
473
Jason Ling5747fab2019-10-02 16:46:23 -0700474 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
475 std::smatch matches;
476
Cheng C Yange50345b2019-04-02 17:26:15 +0800477 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800478 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530479 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800480 std::string labelHead;
481 std::string sensorPathStr = sensorPath.string();
482 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700483 std::string sensorNameSubStr{""};
484 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
485 {
Josh Lehan06494452019-10-31 09:49:16 -0700486 // hwmon *_input filename without number:
487 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700488 sensorNameSubStr = matches[1];
489 }
490 else
491 {
Josh Lehan06494452019-10-31 09:49:16 -0700492 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700493 << sensorNameStr;
494 continue;
495 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800496
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530497 std::string labelPath;
498
499 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700500 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530501 if (pos != std::string::npos)
502 {
503
504 std::string sensorPathStrMax = sensorPathStr.substr(pos);
505 if (sensorPathStrMax.compare("_max") == 0)
506 {
507 labelPath =
508 boost::replace_all_copy(sensorPathStr, "max", "label");
509 maxLabel = true;
510 }
511 else
512 {
513 labelPath = boost::replace_all_copy(sensorPathStr, "input",
514 "label");
515 maxLabel = false;
516 }
517 }
518 else
519 {
520 continue;
521 }
522
Cheng C Yangecba9de2019-09-12 23:41:50 +0800523 std::ifstream labelFile(labelPath);
524 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800525 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700526 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800527 {
528 std::cerr << "Input file " << sensorPath
529 << " has no corresponding label file\n";
530 }
Josh Lehan06494452019-10-31 09:49:16 -0700531 // hwmon *_input filename with number:
532 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700533 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800534 }
535 else
536 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800537 std::string label;
538 std::getline(labelFile, label);
539 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800540 auto findSensor = sensors.find(label);
541 if (findSensor != sensors.end())
542 {
543 continue;
544 }
545
Josh Lehan06494452019-10-31 09:49:16 -0700546 // hwmon corresponding *_label file contents:
547 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700548 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800549 }
550
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530551 /* append "max" for labelMatch */
552 if (maxLabel)
553 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700554 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530555 }
556
Ed Tanous8a57ec02020-10-09 12:46:52 -0700557 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700558 {
559 std::cerr << "Sensor type=\"" << sensorNameSubStr
560 << "\" label=\"" << labelHead << "\"\n";
561 }
562
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530563 checkPWMSensor(sensorPath, labelHead, *interfacePath,
564 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800565
Vijay Khemka996bad12019-05-28 15:15:16 -0700566 if (!findLabels.empty())
567 {
568 /* Check if this labelHead is enabled in config file */
569 if (std::find(findLabels.begin(), findLabels.end(),
570 labelHead) == findLabels.end())
571 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700572 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800573 {
574 std::cerr << "could not find " << labelHead
575 << " in the Labels list\n";
576 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700577 continue;
578 }
579 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800580
Cheng C Yange50345b2019-04-02 17:26:15 +0800581 auto findProperty = labelMatch.find(labelHead);
582 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800583 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700584 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700585 {
586 std::cerr << "Could not find matching default property for "
587 << labelHead << "\n";
588 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800589 continue;
590 }
591
Josh Lehan74d9bd92019-10-31 08:51:58 -0700592 // Protect the hardcoded labelMatch list from changes,
593 // by making a copy and modifying that instead.
594 // Avoid bleedthrough of one device's customizations to
595 // the next device, as each should be independently customizable.
596 psuProperties.push_back(findProperty->second);
597 auto psuProperty = psuProperties.rbegin();
598
599 // Use label head as prefix for reading from config file,
600 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
601 std::string keyName = labelHead + "_Name";
602 std::string keyScale = labelHead + "_Scale";
603 std::string keyMin = labelHead + "_Min";
604 std::string keyMax = labelHead + "_Max";
605
606 bool customizedName = false;
607 auto findCustomName = baseConfig->second.find(keyName);
608 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700609 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700610 try
611 {
612 psuProperty->labelTypeName = std::visit(
613 VariantToStringVisitor(), findCustomName->second);
614 }
615 catch (std::invalid_argument&)
616 {
617 std::cerr << "Unable to parse " << keyName << "\n";
618 continue;
619 }
620
621 // All strings are valid, including empty string
622 customizedName = true;
623 }
624
625 bool customizedScale = false;
626 auto findCustomScale = baseConfig->second.find(keyScale);
627 if (findCustomScale != baseConfig->second.end())
628 {
629 try
630 {
631 psuProperty->sensorScaleFactor = std::visit(
632 VariantToUnsignedIntVisitor(), findCustomScale->second);
633 }
634 catch (std::invalid_argument&)
635 {
636 std::cerr << "Unable to parse " << keyScale << "\n";
637 continue;
638 }
639
640 // Avoid later division by zero
641 if (psuProperty->sensorScaleFactor > 0)
642 {
643 customizedScale = true;
644 }
645 else
646 {
647 std::cerr << "Unable to accept " << keyScale << "\n";
648 continue;
649 }
650 }
651
652 auto findCustomMin = baseConfig->second.find(keyMin);
653 if (findCustomMin != baseConfig->second.end())
654 {
655 try
656 {
657 psuProperty->minReading = std::visit(
658 VariantToDoubleVisitor(), findCustomMin->second);
659 }
660 catch (std::invalid_argument&)
661 {
662 std::cerr << "Unable to parse " << keyMin << "\n";
663 continue;
664 }
665 }
666
667 auto findCustomMax = baseConfig->second.find(keyMax);
668 if (findCustomMax != baseConfig->second.end())
669 {
670 try
671 {
672 psuProperty->maxReading = std::visit(
673 VariantToDoubleVisitor(), findCustomMax->second);
674 }
675 catch (std::invalid_argument&)
676 {
677 std::cerr << "Unable to parse " << keyMax << "\n";
678 continue;
679 }
680 }
681
682 if (!(psuProperty->minReading < psuProperty->maxReading))
683 {
684 std::cerr << "Min must be less than Max\n";
685 continue;
686 }
687
688 // If the sensor name is being customized by config file,
689 // then prefix/suffix composition becomes not necessary,
690 // and in fact not wanted, because it gets in the way.
691 std::string psuNameFromIndex;
692 if (!customizedName)
693 {
694 /* Find out sensor name index for this label */
695 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500696 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700697 if (std::regex_search(labelHead, matches, rgx))
698 {
699 nameIndex = std::stoi(matches[1]);
700
701 // Decrement to preserve alignment, because hwmon
702 // human-readable filenames and labels use 1-based
703 // numbering, but the "Name", "Name1", "Name2", etc. naming
704 // convention (the psuNames vector) uses 0-based numbering.
705 if (nameIndex > 0)
706 {
707 --nameIndex;
708 }
709 }
710 else
711 {
712 nameIndex = 0;
713 }
714
715 if (psuNames.size() <= nameIndex)
716 {
717 std::cerr << "Could not pair " << labelHead
718 << " with a Name field\n";
719 continue;
720 }
721
722 psuNameFromIndex = psuNames[nameIndex];
723
Ed Tanous8a57ec02020-10-09 12:46:52 -0700724 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700725 {
726 std::cerr << "Sensor label head " << labelHead
727 << " paired with " << psuNameFromIndex
728 << " at index " << nameIndex << "\n";
729 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700730 }
731
Cheng C Yang58b2b532019-05-31 00:19:45 +0800732 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
733
Josh Lehan74d9bd92019-10-31 08:51:58 -0700734 // Similarly, if sensor scaling factor is being customized,
735 // then the below power-of-10 constraint becomes unnecessary,
736 // as config should be able to specify an arbitrary divisor.
737 unsigned int factor = psuProperty->sensorScaleFactor;
738 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700739 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700740 // Preserve existing usage of hardcoded labelMatch table below
741 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700742
Josh Lehan74d9bd92019-10-31 08:51:58 -0700743 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700744 char firstChar =
745 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700746 std::string strScaleFactor =
747 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
748
749 // Preserve existing configs by accepting earlier syntax,
750 // example CurrScaleFactor, PowerScaleFactor, ...
751 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
752 if (findScaleFactor != baseConfig->second.end())
753 {
754 factor = std::visit(VariantToIntVisitor(),
755 findScaleFactor->second);
756 }
757
Ed Tanous8a57ec02020-10-09 12:46:52 -0700758 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700759 {
760 std::cerr << "Sensor scaling factor " << factor
761 << " string " << strScaleFactor << "\n";
762 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700763 }
764
Vijay Khemka996bad12019-05-28 15:15:16 -0700765 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530766 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
767 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800768 {
James Feist17ab6e02019-06-25 12:28:13 -0700769 std::cerr << "error populating thresholds for "
770 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800771 }
772
773 auto findSensorType = sensorTable.find(sensorNameSubStr);
774 if (findSensorType == sensorTable.end())
775 {
Jason Ling5747fab2019-10-02 16:46:23 -0700776 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700777 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800778 continue;
779 }
780
Ed Tanous8a57ec02020-10-09 12:46:52 -0700781 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700782 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700783 std::cerr << "Sensor properties: Name \""
784 << psuProperty->labelTypeName << "\" Scale "
785 << psuProperty->sensorScaleFactor << " Min "
786 << psuProperty->minReading << " Max "
787 << psuProperty->maxReading << "\n";
788 }
789
790 std::string sensorName = psuProperty->labelTypeName;
791 if (customizedName)
792 {
793 if (sensorName.empty())
794 {
795 // Allow selective disabling of an individual sensor,
796 // by customizing its name to an empty string.
797 std::cerr << "Sensor disabled, empty string\n";
798 continue;
799 }
800 }
801 else
802 {
803 // Sensor name not customized, do prefix/suffix composition,
804 // preserving default behavior by using psuNameFromIndex.
805 sensorName =
806 psuNameFromIndex + " " + psuProperty->labelTypeName;
807 }
808
Ed Tanous8a57ec02020-10-09 12:46:52 -0700809 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700810 {
811 std::cerr << "Sensor name \"" << sensorName << "\" path \""
812 << sensorPathStr << "\" type \"" << sensorType
813 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700814 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800815 // destruct existing one first if already created
816 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800817 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800818 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800819 sensorName, std::move(sensorThresholds), *interfacePath,
Josh Lehan74d9bd92019-10-31 08:51:58 -0700820 findSensorType->second, factor, psuProperty->maxReading,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800821 psuProperty->minReading, labelHead, thresholdConfSize);
Yong Libf8b1da2020-04-15 16:32:50 +0800822 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700823 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700824 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700825 {
826 std::cerr << "Created " << numCreated << " sensors so far\n";
827 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800828 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800829
830 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800831 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800832 combineEvents[*psuName + "OperationalStatus"] =
Yong Li3046a022020-04-03 13:01:02 +0800833 std::make_unique<PSUCombineEvent>(
834 objectServer, dbusConnection, io, *psuName, eventPathList,
835 groupEventPathList, "OperationalStatus");
Cheng C Yang209ec562019-03-12 16:37:44 +0800836 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700837
Ed Tanous8a57ec02020-10-09 12:46:52 -0700838 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700839 {
840 std::cerr << "Created total of " << numCreated << " sensors\n";
841 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800842 return;
843}
844
Zhikui Ren23c96e72020-11-05 22:32:28 -0800845void createSensors(
846 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
847 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
848 const std::shared_ptr<boost::container::flat_set<std::string>>&
849 sensorsChanged)
850{
851 auto getter = std::make_shared<GetSensorConfiguration>(
852 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
853 const ManagedObjectType& sensorConfigs) {
854 createSensorsCallback(io, objectServer, dbusConnection,
855 sensorConfigs, sensorsChanged);
856 });
857 getter->getConfiguration(
858 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
859}
860
Cheng C Yang916360b2019-05-07 18:47:16 +0800861void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800862{
Cheng C Yange50345b2019-04-02 17:26:15 +0800863 sensorTable = {{"power", "power/"},
864 {"curr", "current/"},
865 {"temp", "temperature/"},
866 {"in", "voltage/"},
867 {"fan", "fan_tach/"}};
868
869 labelMatch = {{"pin", PSUProperty("Input Power", 3000, 0, 6)},
870 {"pout1", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700871 {"pout2", PSUProperty("Output Power", 3000, 0, 6)},
872 {"pout3", PSUProperty("Output Power", 3000, 0, 6)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700873 {"power1", PSUProperty("Output Power", 3000, 0, 6)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530874 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6)},
Cheng C Yangbdbde962019-04-26 22:50:34 +0800875 {"vin", PSUProperty("Input Voltage", 300, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530876 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700877 {"vout1", PSUProperty("Output Voltage", 255, 0, 3)},
878 {"vout2", PSUProperty("Output Voltage", 255, 0, 3)},
879 {"vout3", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700880 {"vout4", PSUProperty("Output Voltage", 255, 0, 3)},
881 {"vout5", PSUProperty("Output Voltage", 255, 0, 3)},
882 {"vout6", PSUProperty("Output Voltage", 255, 0, 3)},
883 {"vout7", PSUProperty("Output Voltage", 255, 0, 3)},
884 {"vout8", PSUProperty("Output Voltage", 255, 0, 3)},
885 {"vout9", PSUProperty("Output Voltage", 255, 0, 3)},
886 {"vout10", PSUProperty("Output Voltage", 255, 0, 3)},
887 {"vout11", PSUProperty("Output Voltage", 255, 0, 3)},
888 {"vout12", PSUProperty("Output Voltage", 255, 0, 3)},
889 {"vout13", PSUProperty("Output Voltage", 255, 0, 3)},
890 {"vout14", PSUProperty("Output Voltage", 255, 0, 3)},
891 {"vout15", PSUProperty("Output Voltage", 255, 0, 3)},
892 {"vout16", PSUProperty("Output Voltage", 255, 0, 3)},
Jason Ling32047ef2020-09-30 15:43:32 -0700893 {"vout17", PSUProperty("Output Voltage", 255, 0, 3)},
894 {"vout18", PSUProperty("Output Voltage", 255, 0, 3)},
895 {"vout19", PSUProperty("Output Voltage", 255, 0, 3)},
896 {"vout20", PSUProperty("Output Voltage", 255, 0, 3)},
897 {"vout21", PSUProperty("Output Voltage", 255, 0, 3)},
898 {"vout22", PSUProperty("Output Voltage", 255, 0, 3)},
899 {"vout23", PSUProperty("Output Voltage", 255, 0, 3)},
900 {"vout24", PSUProperty("Output Voltage", 255, 0, 3)},
901 {"vout25", PSUProperty("Output Voltage", 255, 0, 3)},
902 {"vout26", PSUProperty("Output Voltage", 255, 0, 3)},
903 {"vout27", PSUProperty("Output Voltage", 255, 0, 3)},
904 {"vout28", PSUProperty("Output Voltage", 255, 0, 3)},
905 {"vout29", PSUProperty("Output Voltage", 255, 0, 3)},
906 {"vout30", PSUProperty("Output Voltage", 255, 0, 3)},
907 {"vout31", PSUProperty("Output Voltage", 255, 0, 3)},
908 {"vout32", PSUProperty("Output Voltage", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700909 {"in1", PSUProperty("Output Voltage", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800910 {"iin", PSUProperty("Input Current", 20, 0, 3)},
911 {"iout1", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700912 {"iout2", PSUProperty("Output Current", 255, 0, 3)},
913 {"iout3", PSUProperty("Output Current", 255, 0, 3)},
Peter Lundgren48b44232020-01-30 16:02:11 -0800914 {"iout4", PSUProperty("Output Current", 255, 0, 3)},
915 {"iout5", PSUProperty("Output Current", 255, 0, 3)},
916 {"iout6", PSUProperty("Output Current", 255, 0, 3)},
917 {"iout7", PSUProperty("Output Current", 255, 0, 3)},
918 {"iout8", PSUProperty("Output Current", 255, 0, 3)},
919 {"iout9", PSUProperty("Output Current", 255, 0, 3)},
920 {"iout10", PSUProperty("Output Current", 255, 0, 3)},
921 {"iout11", PSUProperty("Output Current", 255, 0, 3)},
922 {"iout12", PSUProperty("Output Current", 255, 0, 3)},
923 {"iout13", PSUProperty("Output Current", 255, 0, 3)},
924 {"iout14", PSUProperty("Output Current", 255, 0, 3)},
Vijay Khemkabe664412019-06-28 12:10:04 -0700925 {"curr1", PSUProperty("Output Current", 255, 0, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530926 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3)},
Cheng C Yange50345b2019-04-02 17:26:15 +0800927 {"temp1", PSUProperty("Temperature", 127, -128, 3)},
Vijay Khemka996bad12019-05-28 15:15:16 -0700928 {"temp2", PSUProperty("Temperature", 127, -128, 3)},
929 {"temp3", PSUProperty("Temperature", 127, -128, 3)},
Jason Ling5747fab2019-10-02 16:46:23 -0700930 {"temp4", PSUProperty("Temperature", 127, -128, 3)},
931 {"temp5", PSUProperty("Temperature", 127, -128, 3)},
Alex Qiuf5709b42020-01-29 13:29:50 -0800932 {"temp6", PSUProperty("Temperature", 127, -128, 3)},
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530933 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3)},
Cheng C Yang8dbb3952019-05-23 00:03:12 +0800934 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0)},
935 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +0800936
937 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +0800938
939 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
940 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
941
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800942 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
943 {"Failure", {"in2_alarm"}},
944 {"ACLost", {"in1_beep"}},
945 {"ConfigureError", {"in1_fault"}}};
946
947 groupEventMatch = {{"FanFault",
948 {{"fan1", {"fan1_alarm", "fan1_fault"}},
949 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +0800950}
951
James Feistb6c0b912019-07-09 12:21:44 -0700952int main()
Cheng C Yang209ec562019-03-12 16:37:44 +0800953{
954 boost::asio::io_service io;
955 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
956
957 systemBus->request_name("xyz.openbmc_project.PSUSensor");
958 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +0800959 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800960 auto sensorsChanged =
961 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +0800962
Cheng C Yang916360b2019-05-07 18:47:16 +0800963 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +0800964
Zhikui Ren23c96e72020-11-05 22:32:28 -0800965 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +0800966 boost::asio::deadline_timer filterTimer(io);
967 std::function<void(sdbusplus::message::message&)> eventHandler =
968 [&](sdbusplus::message::message& message) {
969 if (message.is_method_error())
970 {
971 std::cerr << "callback method error\n";
972 return;
973 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800974 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +0800975 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +0800976 filterTimer.async_wait([&](const boost::system::error_code& ec) {
977 if (ec == boost::asio::error::operation_aborted)
978 {
979 return;
980 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700981 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +0800982 {
983 std::cerr << "timer error\n";
984 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800985 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +0800986 });
987 };
988
989 for (const char* type : sensorTypes)
990 {
991 auto match = std::make_unique<sdbusplus::bus::match::match>(
992 static_cast<sdbusplus::bus::bus&>(*systemBus),
993 "type='signal',member='PropertiesChanged',path_namespace='" +
994 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
995 eventHandler);
996 matches.emplace_back(std::move(match));
997 }
998 io.run();
999}