blob: 28d37ff175e343af3a14a2b06e54874b2cd2f3fc [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
Brandon Kim66558232021-11-09 16:53:08 -080043static constexpr auto sensorTypes{std::to_array<const char*>(
44 {"xyz.openbmc_project.Configuration.ADM1266",
45 "xyz.openbmc_project.Configuration.ADM1272",
46 "xyz.openbmc_project.Configuration.ADM1275",
47 "xyz.openbmc_project.Configuration.ADM1278",
48 "xyz.openbmc_project.Configuration.DPS800",
49 "xyz.openbmc_project.Configuration.INA219",
50 "xyz.openbmc_project.Configuration.INA230",
51 "xyz.openbmc_project.Configuration.IPSPS",
52 "xyz.openbmc_project.Configuration.ISL68137",
53 "xyz.openbmc_project.Configuration.ISL68220",
54 "xyz.openbmc_project.Configuration.ISL68223",
55 "xyz.openbmc_project.Configuration.ISL69243",
56 "xyz.openbmc_project.Configuration.ISL69260",
57 "xyz.openbmc_project.Configuration.LM25066",
58 "xyz.openbmc_project.Configuration.MAX16601",
59 "xyz.openbmc_project.Configuration.MAX20710",
60 "xyz.openbmc_project.Configuration.MAX20730",
61 "xyz.openbmc_project.Configuration.MAX20734",
62 "xyz.openbmc_project.Configuration.MAX20796",
63 "xyz.openbmc_project.Configuration.MAX34451",
Howard Chiub58ac3a2021-12-07 17:12:56 +080064 "xyz.openbmc_project.Configuration.MP5023",
Brandon Kim66558232021-11-09 16:53:08 -080065 "xyz.openbmc_project.Configuration.pmbus",
66 "xyz.openbmc_project.Configuration.PXE1610",
67 "xyz.openbmc_project.Configuration.RAA228000",
68 "xyz.openbmc_project.Configuration.RAA228228",
69 "xyz.openbmc_project.Configuration.RAA229004",
70 "xyz.openbmc_project.Configuration.TPS546D24",
71 "xyz.openbmc_project.Configuration.XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080072
Ed Tanousa2df7862021-12-07 16:30:27 -080073// clang-format off
74static constexpr auto pmbusNames{std::to_array<const char*>({
75 "adm1266",
76 "adm1272",
77 "adm1275",
78 "adm1278",
79 "dps800",
80 "ina219",
81 "ina230",
82 "ipsps1",
83 "isl68137",
84 "isl68220",
85 "isl68223",
86 "isl69243",
87 "isl69260",
88 "lm25066",
89 "max16601",
90 "max20710",
91 "max20730",
92 "max20734",
93 "max20796",
94 "max34451",
Howard Chiub58ac3a2021-12-07 17:12:56 +080095 "mp5023",
Ed Tanousa2df7862021-12-07 16:30:27 -080096 "pmbus",
97 "pxe1610",
98 "raa228000",
99 "raa228228",
100 "raa229004",
101 "tps546d24",
102 "xdpe12284"
103})};
104//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700105
Cheng C Yang209ec562019-03-12 16:37:44 +0800106namespace fs = std::filesystem;
107
Yong Libf8b1da2020-04-15 16:32:50 +0800108static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800109 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800110static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
111 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800112static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
113 pwmSensors;
114static boost::container::flat_map<std::string, std::string> sensorTable;
115static boost::container::flat_map<std::string, PSUProperty> labelMatch;
116static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800117static boost::container::flat_map<std::string, std::vector<std::string>>
118 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800119static boost::container::flat_map<
120 std::string,
121 boost::container::flat_map<std::string, std::vector<std::string>>>
122 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800123static boost::container::flat_map<std::string, std::vector<std::string>>
124 limitEventMatch;
125
Josh Lehan74d9bd92019-10-31 08:51:58 -0700126static std::vector<PSUProperty> psuProperties;
127
Cheng C Yang58b2b532019-05-31 00:19:45 +0800128// Function CheckEvent will check each attribute from eventMatch table in the
129// sysfs. If the attributes exists in sysfs, then store the complete path
130// of the attribute into eventPathList.
131void checkEvent(
132 const std::string& directory,
133 const boost::container::flat_map<std::string, std::vector<std::string>>&
134 eventMatch,
135 boost::container::flat_map<std::string, std::vector<std::string>>&
136 eventPathList)
137{
138 for (const auto& match : eventMatch)
139 {
140 const std::vector<std::string>& eventAttrs = match.second;
141 const std::string& eventName = match.first;
142 for (const auto& eventAttr : eventAttrs)
143 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700144 std::string eventPath = directory;
145 eventPath += "/";
146 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800147
148 std::ifstream eventFile(eventPath);
149 if (!eventFile.good())
150 {
151 continue;
152 }
153
154 eventPathList[eventName].push_back(eventPath);
155 }
156 }
157}
158
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800159// Check Group Events which contains more than one targets in each combine
160// events.
161void checkGroupEvent(
162 const std::string& directory,
163 const boost::container::flat_map<
164 std::string,
165 boost::container::flat_map<std::string, std::vector<std::string>>>&
166 groupEventMatch,
167 boost::container::flat_map<
168 std::string,
169 boost::container::flat_map<std::string, std::vector<std::string>>>&
170 groupEventPathList)
171{
172 for (const auto& match : groupEventMatch)
173 {
174 const std::string& groupEventName = match.first;
175 const boost::container::flat_map<std::string, std::vector<std::string>>
176 events = match.second;
177 boost::container::flat_map<std::string, std::vector<std::string>>
178 pathList;
179 for (const auto& match : events)
180 {
181 const std::string& eventName = match.first;
182 const std::vector<std::string>& eventAttrs = match.second;
183 for (const auto& eventAttr : eventAttrs)
184 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700185 std::string eventPath = directory;
186 eventPath += "/";
187 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800188 std::ifstream eventFile(eventPath);
189 if (!eventFile.good())
190 {
191 continue;
192 }
193
194 pathList[eventName].push_back(eventPath);
195 }
196 }
197 groupEventPathList[groupEventName] = pathList;
198 }
199}
200
Cheng C Yang58b2b532019-05-31 00:19:45 +0800201// Function checkEventLimits will check all the psu related xxx_input attributes
202// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
203// xxx_min_alarm exist, then store the existing paths of the alarm attributes
204// to eventPathList.
205void checkEventLimits(
206 const std::string& sensorPathStr,
207 const boost::container::flat_map<std::string, std::vector<std::string>>&
208 limitEventMatch,
209 boost::container::flat_map<std::string, std::vector<std::string>>&
210 eventPathList)
211{
Lei YUa2c7cea2020-12-23 14:07:28 +0800212 auto attributePartPos = sensorPathStr.find_last_of('_');
213 if (attributePartPos == std::string::npos)
214 {
215 // There is no '_' in the string, skip it
216 return;
217 }
218 auto attributePart =
219 std::string_view(sensorPathStr).substr(attributePartPos + 1);
220 if (attributePart != "input")
221 {
222 // If the sensor is not xxx_input, skip it
223 return;
224 }
225
226 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800227 for (const auto& limitMatch : limitEventMatch)
228 {
229 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
230 const std::string& eventName = limitMatch.first;
231 for (const auto& limitEventAttr : limitEventAttrs)
232 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800233 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800234 std::ifstream eventFile(limitEventPath);
235 if (!eventFile.good())
236 {
237 continue;
238 }
239 eventPathList[eventName].push_back(limitEventPath);
240 }
241 }
242}
Cheng C Yang916360b2019-05-07 18:47:16 +0800243
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530244static void
245 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
246 const std::string& interfacePath,
247 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
248 sdbusplus::asio::object_server& objectServer,
249 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800250{
251 for (const auto& pwmName : pwmTable)
252 {
253 if (pwmName.first != labelHead)
254 {
255 continue;
256 }
257
258 const std::string& sensorPathStr = sensorPath.string();
259 const std::string& pwmPathStr =
260 boost::replace_all_copy(sensorPathStr, "input", "target");
261 std::ifstream pwmFile(pwmPathStr);
262 if (!pwmFile.good())
263 {
264 continue;
265 }
266
267 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
268 if (findPWMSensor != pwmSensors.end())
269 {
270 continue;
271 }
272
273 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530274 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
275 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800276 }
277}
278
Zhikui Ren23c96e72020-11-05 22:32:28 -0800279static void createSensorsCallback(
280 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
281 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
282 const ManagedObjectType& sensorConfigs,
283 const std::shared_ptr<boost::container::flat_set<std::string>>&
284 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800285{
Josh Lehan49cfba92019-10-08 16:50:42 -0700286 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800287 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800288
289 std::vector<fs::path> pmbusPaths;
290 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
291 {
292 std::cerr << "No PSU sensors in system\n";
293 return;
294 }
295
296 boost::container::flat_set<std::string> directories;
297 for (const auto& pmbusPath : pmbusPaths)
298 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800299 boost::container::flat_map<std::string, std::vector<std::string>>
300 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800301 boost::container::flat_map<
302 std::string,
303 boost::container::flat_map<std::string, std::vector<std::string>>>
304 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800305
306 std::ifstream nameFile(pmbusPath);
307 if (!nameFile.good())
308 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700309 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800310 continue;
311 }
312
313 std::string pmbusName;
314 std::getline(nameFile, pmbusName);
315 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700316
317 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
318 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800319 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700320 // To avoid this error message, add your driver name to
321 // the pmbusNames vector at the top of this file.
322 std::cerr << "Driver name " << pmbusName
323 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800324 continue;
325 }
326
Cheng C Yang209ec562019-03-12 16:37:44 +0800327 auto directory = pmbusPath.parent_path();
328
329 auto ret = directories.insert(directory.string());
330 if (!ret.second)
331 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700332 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800333 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800334 }
335
James Feistb6c0b912019-07-09 12:21:44 -0700336 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800337 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700338 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800339 if (findHyphen == std::string::npos)
340 {
341 std::cerr << "found bad device" << deviceName << "\n";
342 continue;
343 }
344 std::string busStr = deviceName.substr(0, findHyphen);
345 std::string addrStr = deviceName.substr(findHyphen + 1);
346
347 size_t bus = 0;
348 size_t addr = 0;
349
350 try
351 {
352 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700353 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800354 }
Patrick Williams26601e82021-10-06 12:43:25 -0500355 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800356 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700357 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
358 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800359 continue;
360 }
361
Cheng C Yang209ec562019-03-12 16:37:44 +0800362 const std::pair<std::string, boost::container::flat_map<
363 std::string, BasicVariantType>>*
364 baseConfig = nullptr;
365 const SensorData* sensorData = nullptr;
366 const std::string* interfacePath = nullptr;
367 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800368 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800369
370 for (const std::pair<sdbusplus::message::object_path, SensorData>&
371 sensor : sensorConfigs)
372 {
373 sensorData = &(sensor.second);
374 for (const char* type : sensorTypes)
375 {
376 auto sensorBase = sensorData->find(type);
377 if (sensorBase != sensorData->end())
378 {
379 baseConfig = &(*sensorBase);
380 sensorType = type;
381 break;
382 }
383 }
384 if (baseConfig == nullptr)
385 {
386 std::cerr << "error finding base configuration for "
387 << deviceName << "\n";
388 continue;
389 }
390
391 auto configBus = baseConfig->second.find("Bus");
392 auto configAddress = baseConfig->second.find("Address");
393
394 if (configBus == baseConfig->second.end() ||
395 configAddress == baseConfig->second.end())
396 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800397 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800398 continue;
399 }
400
Ed Tanousa771f6a2022-01-14 09:36:51 -0800401 const uint64_t* confBus = std::get_if<uint64_t>(&(configBus->second));
402 const uint64_t* confAddr = std::get_if<uint64_t>(&(configAddress->second));
403 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800404 {
405 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700406 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800407 continue;
408 }
409
410 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800411 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700412 std::cerr << "Configuration skipping " << *confBus << "-"
413 << *confAddr << " because not " << bus << "-" << addr
414 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800415 continue;
416 }
417
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800418 std::vector<thresholds::Threshold> confThresholds;
419 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
420 {
421 std::cerr << "error populating totoal thresholds\n";
422 }
423 thresholdConfSize = confThresholds.size();
424
Cheng C Yang209ec562019-03-12 16:37:44 +0800425 interfacePath = &(sensor.first.str);
426 break;
427 }
428 if (interfacePath == nullptr)
429 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700430 // To avoid this error message, add your export map entry,
431 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800432 std::cerr << "failed to find match for " << deviceName << "\n";
433 continue;
434 }
435
Cheng C Yange50345b2019-04-02 17:26:15 +0800436 auto findPSUName = baseConfig->second.find("Name");
437 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800438 {
439 std::cerr << "could not determine configuration name for "
440 << deviceName << "\n";
441 continue;
442 }
Ed Tanousa771f6a2022-01-14 09:36:51 -0800443 const std::string* psuName = std::get_if<std::string>(&(findPSUName->second));
444 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800445 {
446 std::cerr << "Cannot find psu name, invalid configuration\n";
447 continue;
448 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800449
450 // on rescans, only update sensors we were signaled by
451 if (!firstScan)
452 {
Zhikui Renda98f092021-11-01 09:41:08 -0700453 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800454 auto it =
455 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
456 [psuNameStr](std::string& s) {
457 return boost::ends_with(s, psuNameStr);
458 });
459
460 if (it == sensorsChanged->end())
461 {
462 continue;
463 }
464 sensorsChanged->erase(it);
465 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800466 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800467 checkGroupEvent(directory.string(), groupEventMatch,
468 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800469
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000470 PowerState readState = PowerState::always;
471 auto findPowerOn = baseConfig->second.find("PowerState");
472 if (findPowerOn != baseConfig->second.end())
473 {
474 std::string powerState =
475 std::visit(VariantToStringVisitor(), findPowerOn->second);
476 setReadState(powerState, readState);
477 }
478
Vijay Khemka996bad12019-05-28 15:15:16 -0700479 /* Check if there are more sensors in the same interface */
480 int i = 1;
481 std::vector<std::string> psuNames;
482 do
483 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700484 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700485 psuNames.push_back(
486 escapeName(std::get<std::string>(findPSUName->second)));
Vijay Khemka996bad12019-05-28 15:15:16 -0700487 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
488 } while (findPSUName != baseConfig->second.end());
489
Cheng C Yange50345b2019-04-02 17:26:15 +0800490 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700491 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800492 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800493 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800494 continue;
495 }
496
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530497 /* read max value in sysfs for in, curr, power, temp, ... */
498 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
499 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700500 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530501 {
502 std::cerr << "No max name in PSU \n";
503 }
504 }
505
Lei YU7170a232021-02-04 16:19:27 +0800506 /* The poll rate for the sensors */
507 double pollRate = 0.0;
508 auto pollRateObj = baseConfig->second.find("PollRate");
509
510 if (pollRateObj != baseConfig->second.end())
511 {
512 pollRate =
513 std::visit(VariantToDoubleVisitor(), pollRateObj->second);
514 if (pollRate <= 0.0)
515 {
516 pollRate = PSUSensor::defaultSensorPoll;
517 }
518 }
519
Vijay Khemka996bad12019-05-28 15:15:16 -0700520 /* Find array of labels to be exposed if it is defined in config */
521 std::vector<std::string> findLabels;
522 auto findLabelObj = baseConfig->second.find("Labels");
523 if (findLabelObj != baseConfig->second.end())
524 {
525 findLabels =
526 std::get<std::vector<std::string>>(findLabelObj->second);
527 }
528
Jason Ling5747fab2019-10-02 16:46:23 -0700529 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
530 std::smatch matches;
531
Cheng C Yange50345b2019-04-02 17:26:15 +0800532 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800533 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530534 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800535 std::string labelHead;
536 std::string sensorPathStr = sensorPath.string();
537 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700538 std::string sensorNameSubStr{""};
539 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
540 {
Josh Lehan06494452019-10-31 09:49:16 -0700541 // hwmon *_input filename without number:
542 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700543 sensorNameSubStr = matches[1];
544 }
545 else
546 {
Josh Lehan06494452019-10-31 09:49:16 -0700547 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700548 << sensorNameStr;
549 continue;
550 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800551
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530552 std::string labelPath;
553
554 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700555 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530556 if (pos != std::string::npos)
557 {
558
559 std::string sensorPathStrMax = sensorPathStr.substr(pos);
560 if (sensorPathStrMax.compare("_max") == 0)
561 {
562 labelPath =
563 boost::replace_all_copy(sensorPathStr, "max", "label");
564 maxLabel = true;
565 }
566 else
567 {
568 labelPath = boost::replace_all_copy(sensorPathStr, "input",
569 "label");
570 maxLabel = false;
571 }
572 }
573 else
574 {
575 continue;
576 }
577
Cheng C Yangecba9de2019-09-12 23:41:50 +0800578 std::ifstream labelFile(labelPath);
579 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800580 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700581 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800582 {
583 std::cerr << "Input file " << sensorPath
584 << " has no corresponding label file\n";
585 }
Josh Lehan06494452019-10-31 09:49:16 -0700586 // hwmon *_input filename with number:
587 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700588 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800589 }
590 else
591 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800592 std::string label;
593 std::getline(labelFile, label);
594 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800595 auto findSensor = sensors.find(label);
596 if (findSensor != sensors.end())
597 {
598 continue;
599 }
600
Josh Lehan06494452019-10-31 09:49:16 -0700601 // hwmon corresponding *_label file contents:
602 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700603 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800604 }
605
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530606 /* append "max" for labelMatch */
607 if (maxLabel)
608 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700609 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530610 }
611
Ed Tanous8a57ec02020-10-09 12:46:52 -0700612 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700613 {
614 std::cerr << "Sensor type=\"" << sensorNameSubStr
615 << "\" label=\"" << labelHead << "\"\n";
616 }
617
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530618 checkPWMSensor(sensorPath, labelHead, *interfacePath,
619 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800620
Vijay Khemka996bad12019-05-28 15:15:16 -0700621 if (!findLabels.empty())
622 {
623 /* Check if this labelHead is enabled in config file */
624 if (std::find(findLabels.begin(), findLabels.end(),
625 labelHead) == findLabels.end())
626 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700627 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800628 {
629 std::cerr << "could not find " << labelHead
630 << " in the Labels list\n";
631 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700632 continue;
633 }
634 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800635
Cheng C Yange50345b2019-04-02 17:26:15 +0800636 auto findProperty = labelMatch.find(labelHead);
637 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800638 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700639 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700640 {
641 std::cerr << "Could not find matching default property for "
642 << labelHead << "\n";
643 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800644 continue;
645 }
646
Josh Lehan74d9bd92019-10-31 08:51:58 -0700647 // Protect the hardcoded labelMatch list from changes,
648 // by making a copy and modifying that instead.
649 // Avoid bleedthrough of one device's customizations to
650 // the next device, as each should be independently customizable.
651 psuProperties.push_back(findProperty->second);
652 auto psuProperty = psuProperties.rbegin();
653
654 // Use label head as prefix for reading from config file,
655 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
656 std::string keyName = labelHead + "_Name";
657 std::string keyScale = labelHead + "_Scale";
658 std::string keyMin = labelHead + "_Min";
659 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800660 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800661 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700662
663 bool customizedName = false;
664 auto findCustomName = baseConfig->second.find(keyName);
665 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700666 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700667 try
668 {
669 psuProperty->labelTypeName = std::visit(
670 VariantToStringVisitor(), findCustomName->second);
671 }
Patrick Williams26601e82021-10-06 12:43:25 -0500672 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700673 {
674 std::cerr << "Unable to parse " << keyName << "\n";
675 continue;
676 }
677
678 // All strings are valid, including empty string
679 customizedName = true;
680 }
681
682 bool customizedScale = false;
683 auto findCustomScale = baseConfig->second.find(keyScale);
684 if (findCustomScale != baseConfig->second.end())
685 {
686 try
687 {
688 psuProperty->sensorScaleFactor = std::visit(
689 VariantToUnsignedIntVisitor(), findCustomScale->second);
690 }
Patrick Williams26601e82021-10-06 12:43:25 -0500691 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700692 {
693 std::cerr << "Unable to parse " << keyScale << "\n";
694 continue;
695 }
696
697 // Avoid later division by zero
698 if (psuProperty->sensorScaleFactor > 0)
699 {
700 customizedScale = true;
701 }
702 else
703 {
704 std::cerr << "Unable to accept " << keyScale << "\n";
705 continue;
706 }
707 }
708
709 auto findCustomMin = baseConfig->second.find(keyMin);
710 if (findCustomMin != baseConfig->second.end())
711 {
712 try
713 {
714 psuProperty->minReading = std::visit(
715 VariantToDoubleVisitor(), findCustomMin->second);
716 }
Patrick Williams26601e82021-10-06 12:43:25 -0500717 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700718 {
719 std::cerr << "Unable to parse " << keyMin << "\n";
720 continue;
721 }
722 }
723
724 auto findCustomMax = baseConfig->second.find(keyMax);
725 if (findCustomMax != baseConfig->second.end())
726 {
727 try
728 {
729 psuProperty->maxReading = std::visit(
730 VariantToDoubleVisitor(), findCustomMax->second);
731 }
Patrick Williams26601e82021-10-06 12:43:25 -0500732 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700733 {
734 std::cerr << "Unable to parse " << keyMax << "\n";
735 continue;
736 }
737 }
738
Jeff Line41d52f2021-04-07 19:38:51 +0800739 auto findCustomOffset = baseConfig->second.find(keyOffset);
740 if (findCustomOffset != baseConfig->second.end())
741 {
742 try
743 {
744 psuProperty->sensorOffset = std::visit(
745 VariantToDoubleVisitor(), findCustomOffset->second);
746 }
Patrick Williams26601e82021-10-06 12:43:25 -0500747 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800748 {
749 std::cerr << "Unable to parse " << keyOffset << "\n";
750 continue;
751 }
752 }
753
Lotus Xucb5af732021-09-10 15:18:50 +0800754 // if we find label head power state set ,override the powerstate.
755 auto findPowerState = baseConfig->second.find(keyPowerState);
756 if (findPowerState != baseConfig->second.end())
757 {
758 std::string powerState = std::visit(VariantToStringVisitor(),
759 findPowerState->second);
760 setReadState(powerState, readState);
761 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700762 if (!(psuProperty->minReading < psuProperty->maxReading))
763 {
764 std::cerr << "Min must be less than Max\n";
765 continue;
766 }
767
768 // If the sensor name is being customized by config file,
769 // then prefix/suffix composition becomes not necessary,
770 // and in fact not wanted, because it gets in the way.
771 std::string psuNameFromIndex;
772 if (!customizedName)
773 {
774 /* Find out sensor name index for this label */
775 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500776 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700777 if (std::regex_search(labelHead, matches, rgx))
778 {
779 nameIndex = std::stoi(matches[1]);
780
781 // Decrement to preserve alignment, because hwmon
782 // human-readable filenames and labels use 1-based
783 // numbering, but the "Name", "Name1", "Name2", etc. naming
784 // convention (the psuNames vector) uses 0-based numbering.
785 if (nameIndex > 0)
786 {
787 --nameIndex;
788 }
789 }
790 else
791 {
792 nameIndex = 0;
793 }
794
795 if (psuNames.size() <= nameIndex)
796 {
797 std::cerr << "Could not pair " << labelHead
798 << " with a Name field\n";
799 continue;
800 }
801
802 psuNameFromIndex = psuNames[nameIndex];
803
Ed Tanous8a57ec02020-10-09 12:46:52 -0700804 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700805 {
806 std::cerr << "Sensor label head " << labelHead
807 << " paired with " << psuNameFromIndex
808 << " at index " << nameIndex << "\n";
809 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700810 }
811
Cheng C Yang58b2b532019-05-31 00:19:45 +0800812 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
813
Josh Lehan74d9bd92019-10-31 08:51:58 -0700814 // Similarly, if sensor scaling factor is being customized,
815 // then the below power-of-10 constraint becomes unnecessary,
816 // as config should be able to specify an arbitrary divisor.
817 unsigned int factor = psuProperty->sensorScaleFactor;
818 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700819 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700820 // Preserve existing usage of hardcoded labelMatch table below
821 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700822
Josh Lehan74d9bd92019-10-31 08:51:58 -0700823 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700824 char firstChar =
825 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700826 std::string strScaleFactor =
827 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
828
829 // Preserve existing configs by accepting earlier syntax,
830 // example CurrScaleFactor, PowerScaleFactor, ...
831 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
832 if (findScaleFactor != baseConfig->second.end())
833 {
834 factor = std::visit(VariantToIntVisitor(),
835 findScaleFactor->second);
836 }
837
Ed Tanous8a57ec02020-10-09 12:46:52 -0700838 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700839 {
840 std::cerr << "Sensor scaling factor " << factor
841 << " string " << strScaleFactor << "\n";
842 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700843 }
844
Vijay Khemka996bad12019-05-28 15:15:16 -0700845 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530846 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
847 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800848 {
James Feist17ab6e02019-06-25 12:28:13 -0700849 std::cerr << "error populating thresholds for "
850 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800851 }
852
Zev Weiss6b6891c2021-04-22 02:46:21 -0500853 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
854 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800855 {
Jason Ling5747fab2019-10-02 16:46:23 -0700856 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700857 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800858 continue;
859 }
860
Ed Tanous8a57ec02020-10-09 12:46:52 -0700861 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700862 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700863 std::cerr << "Sensor properties: Name \""
864 << psuProperty->labelTypeName << "\" Scale "
865 << psuProperty->sensorScaleFactor << " Min "
866 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800867 << psuProperty->maxReading << " Offset "
868 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700869 }
870
871 std::string sensorName = psuProperty->labelTypeName;
872 if (customizedName)
873 {
874 if (sensorName.empty())
875 {
876 // Allow selective disabling of an individual sensor,
877 // by customizing its name to an empty string.
878 std::cerr << "Sensor disabled, empty string\n";
879 continue;
880 }
881 }
882 else
883 {
884 // Sensor name not customized, do prefix/suffix composition,
885 // preserving default behavior by using psuNameFromIndex.
886 sensorName =
887 psuNameFromIndex + " " + psuProperty->labelTypeName;
888 }
889
Ed Tanous8a57ec02020-10-09 12:46:52 -0700890 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700891 {
892 std::cerr << "Sensor name \"" << sensorName << "\" path \""
893 << sensorPathStr << "\" type \"" << sensorType
894 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700895 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800896 // destruct existing one first if already created
897 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800898 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800899 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800900 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000901 readState, findSensorUnit->second, factor,
902 psuProperty->maxReading, psuProperty->minReading,
903 psuProperty->sensorOffset, labelHead, thresholdConfSize,
904 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800905 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700906 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700907 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700908 {
909 std::cerr << "Created " << numCreated << " sensors so far\n";
910 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800911 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800912
913 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800914 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800915 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000916 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
917 *psuName, readState,
918 eventPathList, groupEventPathList,
919 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800920 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700921
Ed Tanous8a57ec02020-10-09 12:46:52 -0700922 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700923 {
924 std::cerr << "Created total of " << numCreated << " sensors\n";
925 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800926 return;
927}
928
Zhikui Ren23c96e72020-11-05 22:32:28 -0800929void createSensors(
930 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
931 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
932 const std::shared_ptr<boost::container::flat_set<std::string>>&
933 sensorsChanged)
934{
935 auto getter = std::make_shared<GetSensorConfiguration>(
936 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
937 const ManagedObjectType& sensorConfigs) {
938 createSensorsCallback(io, objectServer, dbusConnection,
939 sensorConfigs, sensorsChanged);
940 });
941 getter->getConfiguration(
942 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
943}
944
Cheng C Yang916360b2019-05-07 18:47:16 +0800945void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800946{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500947 sensorTable = {{"power", sensor_paths::unitWatts},
948 {"curr", sensor_paths::unitAmperes},
949 {"temp", sensor_paths::unitDegreesC},
950 {"in", sensor_paths::unitVolts},
951 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800952
Jeff Line41d52f2021-04-07 19:38:51 +0800953 labelMatch = {
954 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
955 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
956 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
957 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
958 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
959 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
960 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
961 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
962 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
963 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
964 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
965 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
966 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
967 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
968 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
969 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
970 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
971 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
972 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
973 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
974 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
975 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
976 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
977 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
978 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
979 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
980 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
981 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
982 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
985 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
986 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
987 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
988 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
989 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
993 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
994 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
995 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
997 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
998 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
999 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1000 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1001 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1002 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1003 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1004 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1005 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1006 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1007 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1008 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1009 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1010 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1011 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
1012 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1013 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1014 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1015 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1016 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1017 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1018 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1019 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1020 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
1021 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001022
1023 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001024
1025 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1026 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1027
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001028 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1029 {"Failure", {"in2_alarm"}},
1030 {"ACLost", {"in1_beep"}},
1031 {"ConfigureError", {"in1_fault"}}};
1032
1033 groupEventMatch = {{"FanFault",
1034 {{"fan1", {"fan1_alarm", "fan1_fault"}},
1035 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001036}
1037
James Feistb6c0b912019-07-09 12:21:44 -07001038int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001039{
1040 boost::asio::io_service io;
1041 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1042
1043 systemBus->request_name("xyz.openbmc_project.PSUSensor");
1044 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001045 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -08001046 auto sensorsChanged =
1047 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001048
Cheng C Yang916360b2019-05-07 18:47:16 +08001049 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001050
Zhikui Ren23c96e72020-11-05 22:32:28 -08001051 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +08001052 boost::asio::deadline_timer filterTimer(io);
1053 std::function<void(sdbusplus::message::message&)> eventHandler =
1054 [&](sdbusplus::message::message& message) {
1055 if (message.is_method_error())
1056 {
1057 std::cerr << "callback method error\n";
1058 return;
1059 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001060 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +08001061 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +08001062 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1063 if (ec == boost::asio::error::operation_aborted)
1064 {
1065 return;
1066 }
Ed Tanous8a57ec02020-10-09 12:46:52 -07001067 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +08001068 {
1069 std::cerr << "timer error\n";
1070 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001071 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +08001072 });
1073 };
1074
1075 for (const char* type : sensorTypes)
1076 {
1077 auto match = std::make_unique<sdbusplus::bus::match::match>(
1078 static_cast<sdbusplus::bus::bus&>(*systemBus),
1079 "type='signal',member='PropertiesChanged',path_namespace='" +
1080 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
1081 eventHandler);
1082 matches.emplace_back(std::move(match));
1083 }
Bruce Lee1263c3d2021-06-04 15:16:33 +08001084
1085 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001086 io.run();
1087}