blob: 56ba438acc99c409aa0485184900bd3041f19852 [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",
Tim Chao6f379ce2022-02-24 11:08:09 +080048 "xyz.openbmc_project.Configuration.ADS7830",
linchuyuan5cf66df2021-12-21 17:38:55 -080049 "xyz.openbmc_project.Configuration.BMR490",
Brandon Kim66558232021-11-09 16:53:08 -080050 "xyz.openbmc_project.Configuration.DPS800",
51 "xyz.openbmc_project.Configuration.INA219",
52 "xyz.openbmc_project.Configuration.INA230",
53 "xyz.openbmc_project.Configuration.IPSPS",
54 "xyz.openbmc_project.Configuration.ISL68137",
55 "xyz.openbmc_project.Configuration.ISL68220",
56 "xyz.openbmc_project.Configuration.ISL68223",
Khang Kieu7d6b77d2022-02-10 10:43:36 +000057 "xyz.openbmc_project.Configuration.ISL69225",
Brandon Kim66558232021-11-09 16:53:08 -080058 "xyz.openbmc_project.Configuration.ISL69243",
59 "xyz.openbmc_project.Configuration.ISL69260",
60 "xyz.openbmc_project.Configuration.LM25066",
61 "xyz.openbmc_project.Configuration.MAX16601",
62 "xyz.openbmc_project.Configuration.MAX20710",
63 "xyz.openbmc_project.Configuration.MAX20730",
64 "xyz.openbmc_project.Configuration.MAX20734",
65 "xyz.openbmc_project.Configuration.MAX20796",
66 "xyz.openbmc_project.Configuration.MAX34451",
Howard Chiub58ac3a2021-12-07 17:12:56 +080067 "xyz.openbmc_project.Configuration.MP5023",
Brandon Kim66558232021-11-09 16:53:08 -080068 "xyz.openbmc_project.Configuration.pmbus",
69 "xyz.openbmc_project.Configuration.PXE1610",
70 "xyz.openbmc_project.Configuration.RAA228000",
71 "xyz.openbmc_project.Configuration.RAA228228",
72 "xyz.openbmc_project.Configuration.RAA229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -080073 "xyz.openbmc_project.Configuration.RAA229126",
Brandon Kim66558232021-11-09 16:53:08 -080074 "xyz.openbmc_project.Configuration.TPS546D24",
75 "xyz.openbmc_project.Configuration.XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080076
Ed Tanousa2df7862021-12-07 16:30:27 -080077// clang-format off
78static constexpr auto pmbusNames{std::to_array<const char*>({
79 "adm1266",
80 "adm1272",
81 "adm1275",
82 "adm1278",
Tim Chao6f379ce2022-02-24 11:08:09 +080083 "ads7830",
linchuyuan5cf66df2021-12-21 17:38:55 -080084 "bmr490",
Ed Tanousa2df7862021-12-07 16:30:27 -080085 "dps800",
86 "ina219",
87 "ina230",
88 "ipsps1",
89 "isl68137",
90 "isl68220",
91 "isl68223",
Khang Kieu51ad6672022-02-09 01:26:25 +000092 "isl69225",
Ed Tanousa2df7862021-12-07 16:30:27 -080093 "isl69243",
94 "isl69260",
95 "lm25066",
96 "max16601",
97 "max20710",
98 "max20730",
99 "max20734",
100 "max20796",
101 "max34451",
Howard Chiub58ac3a2021-12-07 17:12:56 +0800102 "mp5023",
Ed Tanousa2df7862021-12-07 16:30:27 -0800103 "pmbus",
104 "pxe1610",
105 "raa228000",
106 "raa228228",
107 "raa229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800108 "raa229126",
Ed Tanousa2df7862021-12-07 16:30:27 -0800109 "tps546d24",
110 "xdpe12284"
111})};
112//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700113
Cheng C Yang209ec562019-03-12 16:37:44 +0800114namespace fs = std::filesystem;
115
Yong Libf8b1da2020-04-15 16:32:50 +0800116static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800117 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800118static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
119 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800120static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
121 pwmSensors;
122static boost::container::flat_map<std::string, std::string> sensorTable;
123static boost::container::flat_map<std::string, PSUProperty> labelMatch;
124static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800125static boost::container::flat_map<std::string, std::vector<std::string>>
126 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800127static boost::container::flat_map<
128 std::string,
129 boost::container::flat_map<std::string, std::vector<std::string>>>
130 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800131static boost::container::flat_map<std::string, std::vector<std::string>>
132 limitEventMatch;
133
Josh Lehan74d9bd92019-10-31 08:51:58 -0700134static std::vector<PSUProperty> psuProperties;
135
Cheng C Yang58b2b532019-05-31 00:19:45 +0800136// Function CheckEvent will check each attribute from eventMatch table in the
137// sysfs. If the attributes exists in sysfs, then store the complete path
138// of the attribute into eventPathList.
139void checkEvent(
140 const std::string& directory,
141 const boost::container::flat_map<std::string, std::vector<std::string>>&
142 eventMatch,
143 boost::container::flat_map<std::string, std::vector<std::string>>&
144 eventPathList)
145{
146 for (const auto& match : eventMatch)
147 {
148 const std::vector<std::string>& eventAttrs = match.second;
149 const std::string& eventName = match.first;
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 Yang58b2b532019-05-31 00:19:45 +0800155
156 std::ifstream eventFile(eventPath);
157 if (!eventFile.good())
158 {
159 continue;
160 }
161
162 eventPathList[eventName].push_back(eventPath);
163 }
164 }
165}
166
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800167// Check Group Events which contains more than one targets in each combine
168// events.
169void checkGroupEvent(
170 const std::string& directory,
171 const boost::container::flat_map<
172 std::string,
173 boost::container::flat_map<std::string, std::vector<std::string>>>&
174 groupEventMatch,
175 boost::container::flat_map<
176 std::string,
177 boost::container::flat_map<std::string, std::vector<std::string>>>&
178 groupEventPathList)
179{
180 for (const auto& match : groupEventMatch)
181 {
182 const std::string& groupEventName = match.first;
183 const boost::container::flat_map<std::string, std::vector<std::string>>
184 events = match.second;
185 boost::container::flat_map<std::string, std::vector<std::string>>
186 pathList;
187 for (const auto& match : events)
188 {
189 const std::string& eventName = match.first;
190 const std::vector<std::string>& eventAttrs = match.second;
191 for (const auto& eventAttr : eventAttrs)
192 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700193 std::string eventPath = directory;
194 eventPath += "/";
195 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800196 std::ifstream eventFile(eventPath);
197 if (!eventFile.good())
198 {
199 continue;
200 }
201
202 pathList[eventName].push_back(eventPath);
203 }
204 }
205 groupEventPathList[groupEventName] = pathList;
206 }
207}
208
Cheng C Yang58b2b532019-05-31 00:19:45 +0800209// Function checkEventLimits will check all the psu related xxx_input attributes
210// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
211// xxx_min_alarm exist, then store the existing paths of the alarm attributes
212// to eventPathList.
213void checkEventLimits(
214 const std::string& sensorPathStr,
215 const boost::container::flat_map<std::string, std::vector<std::string>>&
216 limitEventMatch,
217 boost::container::flat_map<std::string, std::vector<std::string>>&
218 eventPathList)
219{
Lei YUa2c7cea2020-12-23 14:07:28 +0800220 auto attributePartPos = sensorPathStr.find_last_of('_');
221 if (attributePartPos == std::string::npos)
222 {
223 // There is no '_' in the string, skip it
224 return;
225 }
226 auto attributePart =
227 std::string_view(sensorPathStr).substr(attributePartPos + 1);
228 if (attributePart != "input")
229 {
230 // If the sensor is not xxx_input, skip it
231 return;
232 }
233
234 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800235 for (const auto& limitMatch : limitEventMatch)
236 {
237 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
238 const std::string& eventName = limitMatch.first;
239 for (const auto& limitEventAttr : limitEventAttrs)
240 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800241 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800242 std::ifstream eventFile(limitEventPath);
243 if (!eventFile.good())
244 {
245 continue;
246 }
247 eventPathList[eventName].push_back(limitEventPath);
248 }
249 }
250}
Cheng C Yang916360b2019-05-07 18:47:16 +0800251
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530252static void
253 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
254 const std::string& interfacePath,
255 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
256 sdbusplus::asio::object_server& objectServer,
257 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800258{
259 for (const auto& pwmName : pwmTable)
260 {
261 if (pwmName.first != labelHead)
262 {
263 continue;
264 }
265
266 const std::string& sensorPathStr = sensorPath.string();
267 const std::string& pwmPathStr =
268 boost::replace_all_copy(sensorPathStr, "input", "target");
269 std::ifstream pwmFile(pwmPathStr);
270 if (!pwmFile.good())
271 {
272 continue;
273 }
274
275 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
276 if (findPWMSensor != pwmSensors.end())
277 {
278 continue;
279 }
280
281 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530282 "Pwm_" + psuName + "_" + pwmName.second, pwmPathStr, dbusConnection,
283 objectServer, interfacePath + "_" + pwmName.second, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800284 }
285}
286
Zhikui Ren23c96e72020-11-05 22:32:28 -0800287static void createSensorsCallback(
288 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
289 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
290 const ManagedObjectType& sensorConfigs,
291 const std::shared_ptr<boost::container::flat_set<std::string>>&
292 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800293{
Josh Lehan49cfba92019-10-08 16:50:42 -0700294 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800295 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800296
297 std::vector<fs::path> pmbusPaths;
298 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
299 {
300 std::cerr << "No PSU sensors in system\n";
301 return;
302 }
303
304 boost::container::flat_set<std::string> directories;
305 for (const auto& pmbusPath : pmbusPaths)
306 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800307 boost::container::flat_map<std::string, std::vector<std::string>>
308 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800309 boost::container::flat_map<
310 std::string,
311 boost::container::flat_map<std::string, std::vector<std::string>>>
312 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800313
314 std::ifstream nameFile(pmbusPath);
315 if (!nameFile.good())
316 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700317 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800318 continue;
319 }
320
321 std::string pmbusName;
322 std::getline(nameFile, pmbusName);
323 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700324
325 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
326 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800327 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700328 // To avoid this error message, add your driver name to
329 // the pmbusNames vector at the top of this file.
330 std::cerr << "Driver name " << pmbusName
331 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800332 continue;
333 }
334
Cheng C Yang209ec562019-03-12 16:37:44 +0800335 auto directory = pmbusPath.parent_path();
336
337 auto ret = directories.insert(directory.string());
338 if (!ret.second)
339 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700340 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800341 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800342 }
343
James Feistb6c0b912019-07-09 12:21:44 -0700344 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800345 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700346 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800347 if (findHyphen == std::string::npos)
348 {
349 std::cerr << "found bad device" << deviceName << "\n";
350 continue;
351 }
352 std::string busStr = deviceName.substr(0, findHyphen);
353 std::string addrStr = deviceName.substr(findHyphen + 1);
354
355 size_t bus = 0;
356 size_t addr = 0;
357
358 try
359 {
360 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700361 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800362 }
Patrick Williams26601e82021-10-06 12:43:25 -0500363 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800364 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700365 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
366 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800367 continue;
368 }
369
Cheng C Yang209ec562019-03-12 16:37:44 +0800370 const std::pair<std::string, boost::container::flat_map<
371 std::string, BasicVariantType>>*
372 baseConfig = nullptr;
373 const SensorData* sensorData = nullptr;
374 const std::string* interfacePath = nullptr;
375 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800376 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800377
378 for (const std::pair<sdbusplus::message::object_path, SensorData>&
379 sensor : sensorConfigs)
380 {
381 sensorData = &(sensor.second);
382 for (const char* type : sensorTypes)
383 {
384 auto sensorBase = sensorData->find(type);
385 if (sensorBase != sensorData->end())
386 {
387 baseConfig = &(*sensorBase);
388 sensorType = type;
389 break;
390 }
391 }
392 if (baseConfig == nullptr)
393 {
394 std::cerr << "error finding base configuration for "
395 << deviceName << "\n";
396 continue;
397 }
398
399 auto configBus = baseConfig->second.find("Bus");
400 auto configAddress = baseConfig->second.find("Address");
401
402 if (configBus == baseConfig->second.end() ||
403 configAddress == baseConfig->second.end())
404 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800405 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800406 continue;
407 }
408
Ed Tanousa771f6a2022-01-14 09:36:51 -0800409 const uint64_t* confBus = std::get_if<uint64_t>(&(configBus->second));
410 const uint64_t* confAddr = std::get_if<uint64_t>(&(configAddress->second));
411 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800412 {
413 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700414 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800415 continue;
416 }
417
418 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800419 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700420 std::cerr << "Configuration skipping " << *confBus << "-"
421 << *confAddr << " because not " << bus << "-" << addr
422 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800423 continue;
424 }
425
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800426 std::vector<thresholds::Threshold> confThresholds;
427 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
428 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800429 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800430 }
431 thresholdConfSize = confThresholds.size();
432
Cheng C Yang209ec562019-03-12 16:37:44 +0800433 interfacePath = &(sensor.first.str);
434 break;
435 }
436 if (interfacePath == nullptr)
437 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700438 // To avoid this error message, add your export map entry,
439 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800440 std::cerr << "failed to find match for " << deviceName << "\n";
441 continue;
442 }
443
Cheng C Yange50345b2019-04-02 17:26:15 +0800444 auto findPSUName = baseConfig->second.find("Name");
445 if (findPSUName == baseConfig->second.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800446 {
447 std::cerr << "could not determine configuration name for "
448 << deviceName << "\n";
449 continue;
450 }
Ed Tanousa771f6a2022-01-14 09:36:51 -0800451 const std::string* psuName = std::get_if<std::string>(&(findPSUName->second));
452 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800453 {
454 std::cerr << "Cannot find psu name, invalid configuration\n";
455 continue;
456 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800457
458 // on rescans, only update sensors we were signaled by
459 if (!firstScan)
460 {
Zhikui Renda98f092021-11-01 09:41:08 -0700461 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800462 auto it =
463 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
464 [psuNameStr](std::string& s) {
465 return boost::ends_with(s, psuNameStr);
466 });
467
468 if (it == sensorsChanged->end())
469 {
470 continue;
471 }
472 sensorsChanged->erase(it);
473 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800474 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800475 checkGroupEvent(directory.string(), groupEventMatch,
476 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800477
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000478 PowerState readState = PowerState::always;
479 auto findPowerOn = baseConfig->second.find("PowerState");
480 if (findPowerOn != baseConfig->second.end())
481 {
482 std::string powerState =
483 std::visit(VariantToStringVisitor(), findPowerOn->second);
484 setReadState(powerState, readState);
485 }
486
Vijay Khemka996bad12019-05-28 15:15:16 -0700487 /* Check if there are more sensors in the same interface */
488 int i = 1;
489 std::vector<std::string> psuNames;
490 do
491 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700492 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700493 psuNames.push_back(
494 escapeName(std::get<std::string>(findPSUName->second)));
Vijay Khemka996bad12019-05-28 15:15:16 -0700495 findPSUName = baseConfig->second.find("Name" + std::to_string(i++));
496 } while (findPSUName != baseConfig->second.end());
497
Cheng C Yange50345b2019-04-02 17:26:15 +0800498 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700499 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800500 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800501 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800502 continue;
503 }
504
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530505 /* read max value in sysfs for in, curr, power, temp, ... */
506 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
507 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700508 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530509 {
510 std::cerr << "No max name in PSU \n";
511 }
512 }
513
Lei YU7170a232021-02-04 16:19:27 +0800514 /* The poll rate for the sensors */
515 double pollRate = 0.0;
516 auto pollRateObj = baseConfig->second.find("PollRate");
517
518 if (pollRateObj != baseConfig->second.end())
519 {
520 pollRate =
521 std::visit(VariantToDoubleVisitor(), pollRateObj->second);
522 if (pollRate <= 0.0)
523 {
524 pollRate = PSUSensor::defaultSensorPoll;
525 }
526 }
527
Vijay Khemka996bad12019-05-28 15:15:16 -0700528 /* Find array of labels to be exposed if it is defined in config */
529 std::vector<std::string> findLabels;
530 auto findLabelObj = baseConfig->second.find("Labels");
531 if (findLabelObj != baseConfig->second.end())
532 {
533 findLabels =
534 std::get<std::vector<std::string>>(findLabelObj->second);
535 }
536
Jason Ling5747fab2019-10-02 16:46:23 -0700537 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
538 std::smatch matches;
539
Cheng C Yange50345b2019-04-02 17:26:15 +0800540 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800541 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530542 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800543 std::string labelHead;
544 std::string sensorPathStr = sensorPath.string();
545 std::string sensorNameStr = sensorPath.filename();
Jason Ling5747fab2019-10-02 16:46:23 -0700546 std::string sensorNameSubStr{""};
547 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
548 {
Josh Lehan06494452019-10-31 09:49:16 -0700549 // hwmon *_input filename without number:
550 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700551 sensorNameSubStr = matches[1];
552 }
553 else
554 {
Josh Lehan06494452019-10-31 09:49:16 -0700555 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700556 << sensorNameStr;
557 continue;
558 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800559
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530560 std::string labelPath;
561
562 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700563 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530564 if (pos != std::string::npos)
565 {
566
567 std::string sensorPathStrMax = sensorPathStr.substr(pos);
568 if (sensorPathStrMax.compare("_max") == 0)
569 {
570 labelPath =
571 boost::replace_all_copy(sensorPathStr, "max", "label");
572 maxLabel = true;
573 }
574 else
575 {
576 labelPath = boost::replace_all_copy(sensorPathStr, "input",
577 "label");
578 maxLabel = false;
579 }
580 }
581 else
582 {
583 continue;
584 }
585
Cheng C Yangecba9de2019-09-12 23:41:50 +0800586 std::ifstream labelFile(labelPath);
587 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800588 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700589 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800590 {
591 std::cerr << "Input file " << sensorPath
592 << " has no corresponding label file\n";
593 }
Josh Lehan06494452019-10-31 09:49:16 -0700594 // hwmon *_input filename with number:
595 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700596 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800597 }
598 else
599 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800600 std::string label;
601 std::getline(labelFile, label);
602 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800603 auto findSensor = sensors.find(label);
604 if (findSensor != sensors.end())
605 {
606 continue;
607 }
608
Josh Lehan06494452019-10-31 09:49:16 -0700609 // hwmon corresponding *_label file contents:
610 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700611 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800612 }
613
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530614 /* append "max" for labelMatch */
615 if (maxLabel)
616 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700617 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530618 }
619
Ed Tanous8a57ec02020-10-09 12:46:52 -0700620 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700621 {
622 std::cerr << "Sensor type=\"" << sensorNameSubStr
623 << "\" label=\"" << labelHead << "\"\n";
624 }
625
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530626 checkPWMSensor(sensorPath, labelHead, *interfacePath,
627 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800628
Vijay Khemka996bad12019-05-28 15:15:16 -0700629 if (!findLabels.empty())
630 {
631 /* Check if this labelHead is enabled in config file */
632 if (std::find(findLabels.begin(), findLabels.end(),
633 labelHead) == findLabels.end())
634 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700635 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800636 {
637 std::cerr << "could not find " << labelHead
638 << " in the Labels list\n";
639 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700640 continue;
641 }
642 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800643
Cheng C Yange50345b2019-04-02 17:26:15 +0800644 auto findProperty = labelMatch.find(labelHead);
645 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800646 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700647 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700648 {
649 std::cerr << "Could not find matching default property for "
650 << labelHead << "\n";
651 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800652 continue;
653 }
654
Josh Lehan74d9bd92019-10-31 08:51:58 -0700655 // Protect the hardcoded labelMatch list from changes,
656 // by making a copy and modifying that instead.
657 // Avoid bleedthrough of one device's customizations to
658 // the next device, as each should be independently customizable.
659 psuProperties.push_back(findProperty->second);
660 auto psuProperty = psuProperties.rbegin();
661
662 // Use label head as prefix for reading from config file,
663 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
664 std::string keyName = labelHead + "_Name";
665 std::string keyScale = labelHead + "_Scale";
666 std::string keyMin = labelHead + "_Min";
667 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800668 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800669 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700670
671 bool customizedName = false;
672 auto findCustomName = baseConfig->second.find(keyName);
673 if (findCustomName != baseConfig->second.end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700674 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700675 try
676 {
677 psuProperty->labelTypeName = std::visit(
678 VariantToStringVisitor(), findCustomName->second);
679 }
Patrick Williams26601e82021-10-06 12:43:25 -0500680 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700681 {
682 std::cerr << "Unable to parse " << keyName << "\n";
683 continue;
684 }
685
686 // All strings are valid, including empty string
687 customizedName = true;
688 }
689
690 bool customizedScale = false;
691 auto findCustomScale = baseConfig->second.find(keyScale);
692 if (findCustomScale != baseConfig->second.end())
693 {
694 try
695 {
696 psuProperty->sensorScaleFactor = std::visit(
697 VariantToUnsignedIntVisitor(), findCustomScale->second);
698 }
Patrick Williams26601e82021-10-06 12:43:25 -0500699 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700700 {
701 std::cerr << "Unable to parse " << keyScale << "\n";
702 continue;
703 }
704
705 // Avoid later division by zero
706 if (psuProperty->sensorScaleFactor > 0)
707 {
708 customizedScale = true;
709 }
710 else
711 {
712 std::cerr << "Unable to accept " << keyScale << "\n";
713 continue;
714 }
715 }
716
717 auto findCustomMin = baseConfig->second.find(keyMin);
718 if (findCustomMin != baseConfig->second.end())
719 {
720 try
721 {
722 psuProperty->minReading = std::visit(
723 VariantToDoubleVisitor(), findCustomMin->second);
724 }
Patrick Williams26601e82021-10-06 12:43:25 -0500725 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700726 {
727 std::cerr << "Unable to parse " << keyMin << "\n";
728 continue;
729 }
730 }
731
732 auto findCustomMax = baseConfig->second.find(keyMax);
733 if (findCustomMax != baseConfig->second.end())
734 {
735 try
736 {
737 psuProperty->maxReading = std::visit(
738 VariantToDoubleVisitor(), findCustomMax->second);
739 }
Patrick Williams26601e82021-10-06 12:43:25 -0500740 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700741 {
742 std::cerr << "Unable to parse " << keyMax << "\n";
743 continue;
744 }
745 }
746
Jeff Line41d52f2021-04-07 19:38:51 +0800747 auto findCustomOffset = baseConfig->second.find(keyOffset);
748 if (findCustomOffset != baseConfig->second.end())
749 {
750 try
751 {
752 psuProperty->sensorOffset = std::visit(
753 VariantToDoubleVisitor(), findCustomOffset->second);
754 }
Patrick Williams26601e82021-10-06 12:43:25 -0500755 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800756 {
757 std::cerr << "Unable to parse " << keyOffset << "\n";
758 continue;
759 }
760 }
761
Lotus Xucb5af732021-09-10 15:18:50 +0800762 // if we find label head power state set ,override the powerstate.
763 auto findPowerState = baseConfig->second.find(keyPowerState);
764 if (findPowerState != baseConfig->second.end())
765 {
766 std::string powerState = std::visit(VariantToStringVisitor(),
767 findPowerState->second);
768 setReadState(powerState, readState);
769 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700770 if (!(psuProperty->minReading < psuProperty->maxReading))
771 {
772 std::cerr << "Min must be less than Max\n";
773 continue;
774 }
775
776 // If the sensor name is being customized by config file,
777 // then prefix/suffix composition becomes not necessary,
778 // and in fact not wanted, because it gets in the way.
779 std::string psuNameFromIndex;
780 if (!customizedName)
781 {
782 /* Find out sensor name index for this label */
783 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500784 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700785 if (std::regex_search(labelHead, matches, rgx))
786 {
787 nameIndex = std::stoi(matches[1]);
788
789 // Decrement to preserve alignment, because hwmon
790 // human-readable filenames and labels use 1-based
791 // numbering, but the "Name", "Name1", "Name2", etc. naming
792 // convention (the psuNames vector) uses 0-based numbering.
793 if (nameIndex > 0)
794 {
795 --nameIndex;
796 }
797 }
798 else
799 {
800 nameIndex = 0;
801 }
802
803 if (psuNames.size() <= nameIndex)
804 {
805 std::cerr << "Could not pair " << labelHead
806 << " with a Name field\n";
807 continue;
808 }
809
810 psuNameFromIndex = psuNames[nameIndex];
811
Ed Tanous8a57ec02020-10-09 12:46:52 -0700812 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700813 {
814 std::cerr << "Sensor label head " << labelHead
815 << " paired with " << psuNameFromIndex
816 << " at index " << nameIndex << "\n";
817 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700818 }
819
Cheng C Yang58b2b532019-05-31 00:19:45 +0800820 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
821
Josh Lehan74d9bd92019-10-31 08:51:58 -0700822 // Similarly, if sensor scaling factor is being customized,
823 // then the below power-of-10 constraint becomes unnecessary,
824 // as config should be able to specify an arbitrary divisor.
825 unsigned int factor = psuProperty->sensorScaleFactor;
826 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700827 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700828 // Preserve existing usage of hardcoded labelMatch table below
829 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700830
Josh Lehan74d9bd92019-10-31 08:51:58 -0700831 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700832 char firstChar =
833 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700834 std::string strScaleFactor =
835 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
836
837 // Preserve existing configs by accepting earlier syntax,
838 // example CurrScaleFactor, PowerScaleFactor, ...
839 auto findScaleFactor = baseConfig->second.find(strScaleFactor);
840 if (findScaleFactor != baseConfig->second.end())
841 {
842 factor = std::visit(VariantToIntVisitor(),
843 findScaleFactor->second);
844 }
845
Ed Tanous8a57ec02020-10-09 12:46:52 -0700846 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700847 {
848 std::cerr << "Sensor scaling factor " << factor
849 << " string " << strScaleFactor << "\n";
850 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700851 }
852
Vijay Khemka996bad12019-05-28 15:15:16 -0700853 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530854 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
855 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800856 {
James Feist17ab6e02019-06-25 12:28:13 -0700857 std::cerr << "error populating thresholds for "
858 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800859 }
860
Zev Weiss6b6891c2021-04-22 02:46:21 -0500861 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
862 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800863 {
Jason Ling5747fab2019-10-02 16:46:23 -0700864 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700865 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800866 continue;
867 }
868
Ed Tanous8a57ec02020-10-09 12:46:52 -0700869 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700870 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700871 std::cerr << "Sensor properties: Name \""
872 << psuProperty->labelTypeName << "\" Scale "
873 << psuProperty->sensorScaleFactor << " Min "
874 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800875 << psuProperty->maxReading << " Offset "
876 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700877 }
878
879 std::string sensorName = psuProperty->labelTypeName;
880 if (customizedName)
881 {
882 if (sensorName.empty())
883 {
884 // Allow selective disabling of an individual sensor,
885 // by customizing its name to an empty string.
886 std::cerr << "Sensor disabled, empty string\n";
887 continue;
888 }
889 }
890 else
891 {
892 // Sensor name not customized, do prefix/suffix composition,
893 // preserving default behavior by using psuNameFromIndex.
894 sensorName =
895 psuNameFromIndex + " " + psuProperty->labelTypeName;
896 }
897
Ed Tanous8a57ec02020-10-09 12:46:52 -0700898 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700899 {
900 std::cerr << "Sensor name \"" << sensorName << "\" path \""
901 << sensorPathStr << "\" type \"" << sensorType
902 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700903 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800904 // destruct existing one first if already created
905 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800906 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800907 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800908 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000909 readState, findSensorUnit->second, factor,
910 psuProperty->maxReading, psuProperty->minReading,
911 psuProperty->sensorOffset, labelHead, thresholdConfSize,
912 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800913 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700914 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700915 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700916 {
917 std::cerr << "Created " << numCreated << " sensors so far\n";
918 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800919 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800920
921 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800922 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800923 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000924 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
925 *psuName, readState,
926 eventPathList, groupEventPathList,
927 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800928 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700929
Ed Tanous8a57ec02020-10-09 12:46:52 -0700930 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700931 {
932 std::cerr << "Created total of " << numCreated << " sensors\n";
933 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800934 return;
935}
936
Zhikui Ren23c96e72020-11-05 22:32:28 -0800937void createSensors(
938 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
939 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
940 const std::shared_ptr<boost::container::flat_set<std::string>>&
941 sensorsChanged)
942{
943 auto getter = std::make_shared<GetSensorConfiguration>(
944 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
945 const ManagedObjectType& sensorConfigs) {
946 createSensorsCallback(io, objectServer, dbusConnection,
947 sensorConfigs, sensorsChanged);
948 });
949 getter->getConfiguration(
950 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
951}
952
Cheng C Yang916360b2019-05-07 18:47:16 +0800953void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800954{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500955 sensorTable = {{"power", sensor_paths::unitWatts},
956 {"curr", sensor_paths::unitAmperes},
957 {"temp", sensor_paths::unitDegreesC},
958 {"in", sensor_paths::unitVolts},
959 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800960
Jeff Line41d52f2021-04-07 19:38:51 +0800961 labelMatch = {
962 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800963 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
964 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800965 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
966 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
967 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
968 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800969 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
970 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
971 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800972 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
973 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
974 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
975 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
976 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
977 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
978 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
979 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
980 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
981 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
982 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
985 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
986 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
987 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
988 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
989 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
993 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
994 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
995 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
997 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
998 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
999 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1000 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1001 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1002 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1003 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1004 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1005 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1006 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1007 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001008 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001009 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001010 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1011 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1012 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1013 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1014 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1015 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001016 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001017 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1018 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001019 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1020 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1021 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1022 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1023 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1024 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1025 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1026 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1027 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1028 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1029 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1030 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1031 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1032 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1033 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001034 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1035 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1036 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001037 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1038 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1039 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1040 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1041 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1042 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1043 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1044 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1045 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
1046 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001047
1048 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001049
1050 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1051 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1052
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001053 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1054 {"Failure", {"in2_alarm"}},
1055 {"ACLost", {"in1_beep"}},
1056 {"ConfigureError", {"in1_fault"}}};
1057
1058 groupEventMatch = {{"FanFault",
1059 {{"fan1", {"fan1_alarm", "fan1_fault"}},
1060 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001061}
1062
James Feistb6c0b912019-07-09 12:21:44 -07001063int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001064{
1065 boost::asio::io_service io;
1066 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1067
1068 systemBus->request_name("xyz.openbmc_project.PSUSensor");
1069 sdbusplus::asio::object_server objectServer(systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001070 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Zhikui Ren23c96e72020-11-05 22:32:28 -08001071 auto sensorsChanged =
1072 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001073
Cheng C Yang916360b2019-05-07 18:47:16 +08001074 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001075
Zhikui Ren23c96e72020-11-05 22:32:28 -08001076 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +08001077 boost::asio::deadline_timer filterTimer(io);
1078 std::function<void(sdbusplus::message::message&)> eventHandler =
1079 [&](sdbusplus::message::message& message) {
1080 if (message.is_method_error())
1081 {
1082 std::cerr << "callback method error\n";
1083 return;
1084 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001085 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +08001086 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +08001087 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1088 if (ec == boost::asio::error::operation_aborted)
1089 {
1090 return;
1091 }
Ed Tanous8a57ec02020-10-09 12:46:52 -07001092 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +08001093 {
1094 std::cerr << "timer error\n";
1095 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001096 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +08001097 });
1098 };
1099
1100 for (const char* type : sensorTypes)
1101 {
1102 auto match = std::make_unique<sdbusplus::bus::match::match>(
1103 static_cast<sdbusplus::bus::bus&>(*systemBus),
1104 "type='signal',member='PropertiesChanged',path_namespace='" +
1105 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
1106 eventHandler);
1107 matches.emplace_back(std::move(match));
1108 }
Bruce Lee1263c3d2021-06-04 15:16:33 +08001109
1110 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001111 io.run();
1112}