blob: 26cf7d1f92122637964976e98f57dbaf183a0e72 [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",
Zhikui Ren842e5432022-03-09 16:18:24 -080048 "xyz.openbmc_project.Configuration.ADM1293",
Tim Chao6f379ce2022-02-24 11:08:09 +080049 "xyz.openbmc_project.Configuration.ADS7830",
linchuyuan5cf66df2021-12-21 17:38:55 -080050 "xyz.openbmc_project.Configuration.BMR490",
Brandon Kim66558232021-11-09 16:53:08 -080051 "xyz.openbmc_project.Configuration.DPS800",
52 "xyz.openbmc_project.Configuration.INA219",
53 "xyz.openbmc_project.Configuration.INA230",
54 "xyz.openbmc_project.Configuration.IPSPS",
55 "xyz.openbmc_project.Configuration.ISL68137",
56 "xyz.openbmc_project.Configuration.ISL68220",
57 "xyz.openbmc_project.Configuration.ISL68223",
Khang Kieu7d6b77d2022-02-10 10:43:36 +000058 "xyz.openbmc_project.Configuration.ISL69225",
Brandon Kim66558232021-11-09 16:53:08 -080059 "xyz.openbmc_project.Configuration.ISL69243",
60 "xyz.openbmc_project.Configuration.ISL69260",
61 "xyz.openbmc_project.Configuration.LM25066",
62 "xyz.openbmc_project.Configuration.MAX16601",
63 "xyz.openbmc_project.Configuration.MAX20710",
64 "xyz.openbmc_project.Configuration.MAX20730",
65 "xyz.openbmc_project.Configuration.MAX20734",
66 "xyz.openbmc_project.Configuration.MAX20796",
67 "xyz.openbmc_project.Configuration.MAX34451",
Shamim Alidccd1d42022-06-08 22:31:35 +053068 "xyz.openbmc_project.Configuration.MP2971",
69 "xyz.openbmc_project.Configuration.MP2973",
Howard Chiub58ac3a2021-12-07 17:12:56 +080070 "xyz.openbmc_project.Configuration.MP5023",
Brandon Kim66558232021-11-09 16:53:08 -080071 "xyz.openbmc_project.Configuration.pmbus",
72 "xyz.openbmc_project.Configuration.PXE1610",
73 "xyz.openbmc_project.Configuration.RAA228000",
74 "xyz.openbmc_project.Configuration.RAA228228",
Tom Tung75da5152021-11-24 09:38:07 +080075 "xyz.openbmc_project.Configuration.RAA228620",
76 "xyz.openbmc_project.Configuration.RAA229001",
Brandon Kim66558232021-11-09 16:53:08 -080077 "xyz.openbmc_project.Configuration.RAA229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -080078 "xyz.openbmc_project.Configuration.RAA229126",
Brandon Kim66558232021-11-09 16:53:08 -080079 "xyz.openbmc_project.Configuration.TPS546D24",
80 "xyz.openbmc_project.Configuration.XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080081
Ed Tanousa2df7862021-12-07 16:30:27 -080082// clang-format off
83static constexpr auto pmbusNames{std::to_array<const char*>({
84 "adm1266",
85 "adm1272",
86 "adm1275",
87 "adm1278",
Zhikui Ren842e5432022-03-09 16:18:24 -080088 "adm1293",
Tim Chao6f379ce2022-02-24 11:08:09 +080089 "ads7830",
linchuyuan5cf66df2021-12-21 17:38:55 -080090 "bmr490",
Ed Tanousa2df7862021-12-07 16:30:27 -080091 "dps800",
92 "ina219",
93 "ina230",
94 "ipsps1",
95 "isl68137",
96 "isl68220",
97 "isl68223",
Khang Kieu51ad6672022-02-09 01:26:25 +000098 "isl69225",
Ed Tanousa2df7862021-12-07 16:30:27 -080099 "isl69243",
100 "isl69260",
101 "lm25066",
102 "max16601",
103 "max20710",
104 "max20730",
105 "max20734",
106 "max20796",
107 "max34451",
Shamim Alidccd1d42022-06-08 22:31:35 +0530108 "mp2971",
109 "mp2973",
Howard Chiub58ac3a2021-12-07 17:12:56 +0800110 "mp5023",
Ed Tanousa2df7862021-12-07 16:30:27 -0800111 "pmbus",
112 "pxe1610",
113 "raa228000",
114 "raa228228",
Tom Tung75da5152021-11-24 09:38:07 +0800115 "raa228620",
116 "raa229001",
Ed Tanousa2df7862021-12-07 16:30:27 -0800117 "raa229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800118 "raa229126",
Ed Tanousa2df7862021-12-07 16:30:27 -0800119 "tps546d24",
120 "xdpe12284"
121})};
122//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700123
Cheng C Yang209ec562019-03-12 16:37:44 +0800124namespace fs = std::filesystem;
125
Yong Libf8b1da2020-04-15 16:32:50 +0800126static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800127 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800128static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
129 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800130static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
131 pwmSensors;
132static boost::container::flat_map<std::string, std::string> sensorTable;
133static boost::container::flat_map<std::string, PSUProperty> labelMatch;
134static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800135static boost::container::flat_map<std::string, std::vector<std::string>>
136 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800137static boost::container::flat_map<
138 std::string,
139 boost::container::flat_map<std::string, std::vector<std::string>>>
140 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800141static boost::container::flat_map<std::string, std::vector<std::string>>
142 limitEventMatch;
143
Josh Lehan74d9bd92019-10-31 08:51:58 -0700144static std::vector<PSUProperty> psuProperties;
145
Cheng C Yang58b2b532019-05-31 00:19:45 +0800146// Function CheckEvent will check each attribute from eventMatch table in the
147// sysfs. If the attributes exists in sysfs, then store the complete path
148// of the attribute into eventPathList.
149void checkEvent(
150 const std::string& directory,
151 const boost::container::flat_map<std::string, std::vector<std::string>>&
152 eventMatch,
153 boost::container::flat_map<std::string, std::vector<std::string>>&
154 eventPathList)
155{
156 for (const auto& match : eventMatch)
157 {
158 const std::vector<std::string>& eventAttrs = match.second;
159 const std::string& eventName = match.first;
160 for (const auto& eventAttr : eventAttrs)
161 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700162 std::string eventPath = directory;
163 eventPath += "/";
164 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800165
166 std::ifstream eventFile(eventPath);
167 if (!eventFile.good())
168 {
169 continue;
170 }
171
172 eventPathList[eventName].push_back(eventPath);
173 }
174 }
175}
176
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800177// Check Group Events which contains more than one targets in each combine
178// events.
179void checkGroupEvent(
180 const std::string& directory,
181 const boost::container::flat_map<
182 std::string,
183 boost::container::flat_map<std::string, std::vector<std::string>>>&
184 groupEventMatch,
185 boost::container::flat_map<
186 std::string,
187 boost::container::flat_map<std::string, std::vector<std::string>>>&
188 groupEventPathList)
189{
190 for (const auto& match : groupEventMatch)
191 {
192 const std::string& groupEventName = match.first;
193 const boost::container::flat_map<std::string, std::vector<std::string>>
194 events = match.second;
195 boost::container::flat_map<std::string, std::vector<std::string>>
196 pathList;
197 for (const auto& match : events)
198 {
199 const std::string& eventName = match.first;
200 const std::vector<std::string>& eventAttrs = match.second;
201 for (const auto& eventAttr : eventAttrs)
202 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700203 std::string eventPath = directory;
204 eventPath += "/";
205 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800206 std::ifstream eventFile(eventPath);
207 if (!eventFile.good())
208 {
209 continue;
210 }
211
212 pathList[eventName].push_back(eventPath);
213 }
214 }
215 groupEventPathList[groupEventName] = pathList;
216 }
217}
218
Cheng C Yang58b2b532019-05-31 00:19:45 +0800219// Function checkEventLimits will check all the psu related xxx_input attributes
220// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
221// xxx_min_alarm exist, then store the existing paths of the alarm attributes
222// to eventPathList.
223void checkEventLimits(
224 const std::string& sensorPathStr,
225 const boost::container::flat_map<std::string, std::vector<std::string>>&
226 limitEventMatch,
227 boost::container::flat_map<std::string, std::vector<std::string>>&
228 eventPathList)
229{
Lei YUa2c7cea2020-12-23 14:07:28 +0800230 auto attributePartPos = sensorPathStr.find_last_of('_');
231 if (attributePartPos == std::string::npos)
232 {
233 // There is no '_' in the string, skip it
234 return;
235 }
236 auto attributePart =
237 std::string_view(sensorPathStr).substr(attributePartPos + 1);
238 if (attributePart != "input")
239 {
240 // If the sensor is not xxx_input, skip it
241 return;
242 }
243
244 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800245 for (const auto& limitMatch : limitEventMatch)
246 {
247 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
248 const std::string& eventName = limitMatch.first;
249 for (const auto& limitEventAttr : limitEventAttrs)
250 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800251 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800252 std::ifstream eventFile(limitEventPath);
253 if (!eventFile.good())
254 {
255 continue;
256 }
257 eventPathList[eventName].push_back(limitEventPath);
258 }
259 }
260}
Cheng C Yang916360b2019-05-07 18:47:16 +0800261
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530262static void
263 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
264 const std::string& interfacePath,
265 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
266 sdbusplus::asio::object_server& objectServer,
267 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800268{
Zev Weiss741f26e2022-08-12 18:21:02 -0700269 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800270 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700271 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800272 {
273 continue;
274 }
275
276 const std::string& sensorPathStr = sensorPath.string();
277 const std::string& pwmPathStr =
278 boost::replace_all_copy(sensorPathStr, "input", "target");
279 std::ifstream pwmFile(pwmPathStr);
280 if (!pwmFile.good())
281 {
282 continue;
283 }
284
285 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
286 if (findPWMSensor != pwmSensors.end())
287 {
288 continue;
289 }
290
Zev Weissd8c293a2022-08-15 18:58:41 -0700291 std::string name = "Pwm_";
292 name += psuName;
293 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700294 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700295
296 std::string objPath = interfacePath;
297 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700298 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700299
Cheng C Yang916360b2019-05-07 18:47:16 +0800300 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700301 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800302 }
303}
304
Zhikui Ren23c96e72020-11-05 22:32:28 -0800305static void createSensorsCallback(
306 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
307 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
308 const ManagedObjectType& sensorConfigs,
309 const std::shared_ptr<boost::container::flat_set<std::string>>&
310 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800311{
Josh Lehan49cfba92019-10-08 16:50:42 -0700312 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800313 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800314
315 std::vector<fs::path> pmbusPaths;
316 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
317 {
318 std::cerr << "No PSU sensors in system\n";
319 return;
320 }
321
322 boost::container::flat_set<std::string> directories;
323 for (const auto& pmbusPath : pmbusPaths)
324 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800325 boost::container::flat_map<std::string, std::vector<std::string>>
326 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800327 boost::container::flat_map<
328 std::string,
329 boost::container::flat_map<std::string, std::vector<std::string>>>
330 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800331
332 std::ifstream nameFile(pmbusPath);
333 if (!nameFile.good())
334 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700335 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800336 continue;
337 }
338
339 std::string pmbusName;
340 std::getline(nameFile, pmbusName);
341 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700342
343 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
344 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800345 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700346 // To avoid this error message, add your driver name to
347 // the pmbusNames vector at the top of this file.
348 std::cerr << "Driver name " << pmbusName
349 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800350 continue;
351 }
352
Cheng C Yang209ec562019-03-12 16:37:44 +0800353 auto directory = pmbusPath.parent_path();
354
355 auto ret = directories.insert(directory.string());
356 if (!ret.second)
357 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700358 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800359 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800360 }
361
James Feistb6c0b912019-07-09 12:21:44 -0700362 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800363 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700364 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800365 if (findHyphen == std::string::npos)
366 {
367 std::cerr << "found bad device" << deviceName << "\n";
368 continue;
369 }
370 std::string busStr = deviceName.substr(0, findHyphen);
371 std::string addrStr = deviceName.substr(findHyphen + 1);
372
373 size_t bus = 0;
374 size_t addr = 0;
375
376 try
377 {
378 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700379 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800380 }
Patrick Williams26601e82021-10-06 12:43:25 -0500381 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800382 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700383 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
384 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800385 continue;
386 }
387
Zev Weisse8b97ee2022-08-12 15:23:51 -0700388 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800389 const SensorData* sensorData = nullptr;
390 const std::string* interfacePath = nullptr;
391 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800392 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800393
Zev Weiss741f26e2022-08-12 18:21:02 -0700394 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800395 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700396 sensorData = &cfgData;
Cheng C Yang209ec562019-03-12 16:37:44 +0800397 for (const char* type : sensorTypes)
398 {
399 auto sensorBase = sensorData->find(type);
400 if (sensorBase != sensorData->end())
401 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700402 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800403 sensorType = type;
404 break;
405 }
406 }
407 if (baseConfig == nullptr)
408 {
409 std::cerr << "error finding base configuration for "
410 << deviceName << "\n";
411 continue;
412 }
413
Zev Weisse8b97ee2022-08-12 15:23:51 -0700414 auto configBus = baseConfig->find("Bus");
415 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800416
Zev Weisse8b97ee2022-08-12 15:23:51 -0700417 if (configBus == baseConfig->end() ||
418 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800419 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800420 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800421 continue;
422 }
423
Ed Tanousa771f6a2022-01-14 09:36:51 -0800424 const uint64_t* confBus = std::get_if<uint64_t>(&(configBus->second));
425 const uint64_t* confAddr = std::get_if<uint64_t>(&(configAddress->second));
426 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800427 {
428 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700429 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800430 continue;
431 }
432
433 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800434 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700435 std::cerr << "Configuration skipping " << *confBus << "-"
436 << *confAddr << " because not " << bus << "-" << addr
437 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800438 continue;
439 }
440
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800441 std::vector<thresholds::Threshold> confThresholds;
442 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
443 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800444 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800445 }
446 thresholdConfSize = confThresholds.size();
447
Zev Weiss741f26e2022-08-12 18:21:02 -0700448 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800449 break;
450 }
451 if (interfacePath == nullptr)
452 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700453 // To avoid this error message, add your export map entry,
454 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800455 std::cerr << "failed to find match for " << deviceName << "\n";
456 continue;
457 }
458
Zev Weisse8b97ee2022-08-12 15:23:51 -0700459 auto findPSUName = baseConfig->find("Name");
460 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800461 {
462 std::cerr << "could not determine configuration name for "
463 << deviceName << "\n";
464 continue;
465 }
Ed Tanousa771f6a2022-01-14 09:36:51 -0800466 const std::string* psuName = std::get_if<std::string>(&(findPSUName->second));
467 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800468 {
469 std::cerr << "Cannot find psu name, invalid configuration\n";
470 continue;
471 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800472
473 // on rescans, only update sensors we were signaled by
474 if (!firstScan)
475 {
Zhikui Renda98f092021-11-01 09:41:08 -0700476 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800477 auto it =
478 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
479 [psuNameStr](std::string& s) {
480 return boost::ends_with(s, psuNameStr);
481 });
482
483 if (it == sensorsChanged->end())
484 {
485 continue;
486 }
487 sensorsChanged->erase(it);
488 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800489 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800490 checkGroupEvent(directory.string(), groupEventMatch,
491 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800492
Zev Weisse8b97ee2022-08-12 15:23:51 -0700493 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000494
Vijay Khemka996bad12019-05-28 15:15:16 -0700495 /* Check if there are more sensors in the same interface */
496 int i = 1;
497 std::vector<std::string> psuNames;
498 do
499 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700500 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700501 psuNames.push_back(
502 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700503 findPSUName = baseConfig->find("Name" + std::to_string(i++));
504 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700505
Cheng C Yange50345b2019-04-02 17:26:15 +0800506 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700507 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800508 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800509 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800510 continue;
511 }
512
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530513 /* read max value in sysfs for in, curr, power, temp, ... */
514 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
515 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700516 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530517 {
518 std::cerr << "No max name in PSU \n";
519 }
520 }
521
Lei YU7170a232021-02-04 16:19:27 +0800522 /* The poll rate for the sensors */
523 double pollRate = 0.0;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700524 auto pollRateObj = baseConfig->find("PollRate");
Lei YU7170a232021-02-04 16:19:27 +0800525
Zev Weisse8b97ee2022-08-12 15:23:51 -0700526 if (pollRateObj != baseConfig->end())
Lei YU7170a232021-02-04 16:19:27 +0800527 {
528 pollRate =
529 std::visit(VariantToDoubleVisitor(), pollRateObj->second);
530 if (pollRate <= 0.0)
531 {
532 pollRate = PSUSensor::defaultSensorPoll;
533 }
534 }
535
Vijay Khemka996bad12019-05-28 15:15:16 -0700536 /* Find array of labels to be exposed if it is defined in config */
537 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700538 auto findLabelObj = baseConfig->find("Labels");
539 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700540 {
541 findLabels =
542 std::get<std::vector<std::string>>(findLabelObj->second);
543 }
544
Jason Ling5747fab2019-10-02 16:46:23 -0700545 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
546 std::smatch matches;
547
Cheng C Yange50345b2019-04-02 17:26:15 +0800548 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800549 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530550 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800551 std::string labelHead;
552 std::string sensorPathStr = sensorPath.string();
553 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700554 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700555 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
556 {
Josh Lehan06494452019-10-31 09:49:16 -0700557 // hwmon *_input filename without number:
558 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700559 sensorNameSubStr = matches[1];
560 }
561 else
562 {
Josh Lehan06494452019-10-31 09:49:16 -0700563 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700564 << sensorNameStr;
565 continue;
566 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800567
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530568 std::string labelPath;
569
570 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700571 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530572 if (pos != std::string::npos)
573 {
574
575 std::string sensorPathStrMax = sensorPathStr.substr(pos);
Ed Tanous2049bd22022-07-09 07:20:26 -0700576 if (sensorPathStrMax == "_max")
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530577 {
578 labelPath =
579 boost::replace_all_copy(sensorPathStr, "max", "label");
580 maxLabel = true;
581 }
582 else
583 {
584 labelPath = boost::replace_all_copy(sensorPathStr, "input",
585 "label");
586 maxLabel = false;
587 }
588 }
589 else
590 {
591 continue;
592 }
593
Cheng C Yangecba9de2019-09-12 23:41:50 +0800594 std::ifstream labelFile(labelPath);
595 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800596 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700597 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800598 {
599 std::cerr << "Input file " << sensorPath
600 << " has no corresponding label file\n";
601 }
Josh Lehan06494452019-10-31 09:49:16 -0700602 // hwmon *_input filename with number:
603 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700604 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800605 }
606 else
607 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800608 std::string label;
609 std::getline(labelFile, label);
610 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800611 auto findSensor = sensors.find(label);
612 if (findSensor != sensors.end())
613 {
614 continue;
615 }
616
Josh Lehan06494452019-10-31 09:49:16 -0700617 // hwmon corresponding *_label file contents:
618 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700619 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800620 }
621
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530622 /* append "max" for labelMatch */
623 if (maxLabel)
624 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700625 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530626 }
627
Ed Tanous8a57ec02020-10-09 12:46:52 -0700628 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700629 {
630 std::cerr << "Sensor type=\"" << sensorNameSubStr
631 << "\" label=\"" << labelHead << "\"\n";
632 }
633
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530634 checkPWMSensor(sensorPath, labelHead, *interfacePath,
635 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800636
Vijay Khemka996bad12019-05-28 15:15:16 -0700637 if (!findLabels.empty())
638 {
639 /* Check if this labelHead is enabled in config file */
640 if (std::find(findLabels.begin(), findLabels.end(),
641 labelHead) == findLabels.end())
642 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700643 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800644 {
645 std::cerr << "could not find " << labelHead
646 << " in the Labels list\n";
647 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700648 continue;
649 }
650 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800651
Cheng C Yange50345b2019-04-02 17:26:15 +0800652 auto findProperty = labelMatch.find(labelHead);
653 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800654 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700655 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700656 {
657 std::cerr << "Could not find matching default property for "
658 << labelHead << "\n";
659 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800660 continue;
661 }
662
Josh Lehan74d9bd92019-10-31 08:51:58 -0700663 // Protect the hardcoded labelMatch list from changes,
664 // by making a copy and modifying that instead.
665 // Avoid bleedthrough of one device's customizations to
666 // the next device, as each should be independently customizable.
667 psuProperties.push_back(findProperty->second);
668 auto psuProperty = psuProperties.rbegin();
669
670 // Use label head as prefix for reading from config file,
671 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
672 std::string keyName = labelHead + "_Name";
673 std::string keyScale = labelHead + "_Scale";
674 std::string keyMin = labelHead + "_Min";
675 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800676 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800677 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700678
679 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700680 auto findCustomName = baseConfig->find(keyName);
681 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700682 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700683 try
684 {
685 psuProperty->labelTypeName = std::visit(
686 VariantToStringVisitor(), findCustomName->second);
687 }
Patrick Williams26601e82021-10-06 12:43:25 -0500688 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700689 {
690 std::cerr << "Unable to parse " << keyName << "\n";
691 continue;
692 }
693
694 // All strings are valid, including empty string
695 customizedName = true;
696 }
697
698 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700699 auto findCustomScale = baseConfig->find(keyScale);
700 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700701 {
702 try
703 {
704 psuProperty->sensorScaleFactor = std::visit(
705 VariantToUnsignedIntVisitor(), findCustomScale->second);
706 }
Patrick Williams26601e82021-10-06 12:43:25 -0500707 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700708 {
709 std::cerr << "Unable to parse " << keyScale << "\n";
710 continue;
711 }
712
713 // Avoid later division by zero
714 if (psuProperty->sensorScaleFactor > 0)
715 {
716 customizedScale = true;
717 }
718 else
719 {
720 std::cerr << "Unable to accept " << keyScale << "\n";
721 continue;
722 }
723 }
724
Zev Weisse8b97ee2022-08-12 15:23:51 -0700725 auto findCustomMin = baseConfig->find(keyMin);
726 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700727 {
728 try
729 {
730 psuProperty->minReading = std::visit(
731 VariantToDoubleVisitor(), findCustomMin->second);
732 }
Patrick Williams26601e82021-10-06 12:43:25 -0500733 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700734 {
735 std::cerr << "Unable to parse " << keyMin << "\n";
736 continue;
737 }
738 }
739
Zev Weisse8b97ee2022-08-12 15:23:51 -0700740 auto findCustomMax = baseConfig->find(keyMax);
741 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700742 {
743 try
744 {
745 psuProperty->maxReading = std::visit(
746 VariantToDoubleVisitor(), findCustomMax->second);
747 }
Patrick Williams26601e82021-10-06 12:43:25 -0500748 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700749 {
750 std::cerr << "Unable to parse " << keyMax << "\n";
751 continue;
752 }
753 }
754
Zev Weisse8b97ee2022-08-12 15:23:51 -0700755 auto findCustomOffset = baseConfig->find(keyOffset);
756 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800757 {
758 try
759 {
760 psuProperty->sensorOffset = std::visit(
761 VariantToDoubleVisitor(), findCustomOffset->second);
762 }
Patrick Williams26601e82021-10-06 12:43:25 -0500763 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800764 {
765 std::cerr << "Unable to parse " << keyOffset << "\n";
766 continue;
767 }
768 }
769
Lotus Xucb5af732021-09-10 15:18:50 +0800770 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700771 auto findPowerState = baseConfig->find(keyPowerState);
772 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800773 {
774 std::string powerState = std::visit(VariantToStringVisitor(),
775 findPowerState->second);
776 setReadState(powerState, readState);
777 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700778 if (!(psuProperty->minReading < psuProperty->maxReading))
779 {
780 std::cerr << "Min must be less than Max\n";
781 continue;
782 }
783
784 // If the sensor name is being customized by config file,
785 // then prefix/suffix composition becomes not necessary,
786 // and in fact not wanted, because it gets in the way.
787 std::string psuNameFromIndex;
788 if (!customizedName)
789 {
790 /* Find out sensor name index for this label */
791 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500792 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700793 if (std::regex_search(labelHead, matches, rgx))
794 {
795 nameIndex = std::stoi(matches[1]);
796
797 // Decrement to preserve alignment, because hwmon
798 // human-readable filenames and labels use 1-based
799 // numbering, but the "Name", "Name1", "Name2", etc. naming
800 // convention (the psuNames vector) uses 0-based numbering.
801 if (nameIndex > 0)
802 {
803 --nameIndex;
804 }
805 }
806 else
807 {
808 nameIndex = 0;
809 }
810
811 if (psuNames.size() <= nameIndex)
812 {
813 std::cerr << "Could not pair " << labelHead
814 << " with a Name field\n";
815 continue;
816 }
817
818 psuNameFromIndex = psuNames[nameIndex];
819
Ed Tanous8a57ec02020-10-09 12:46:52 -0700820 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700821 {
822 std::cerr << "Sensor label head " << labelHead
823 << " paired with " << psuNameFromIndex
824 << " at index " << nameIndex << "\n";
825 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700826 }
827
Cheng C Yang58b2b532019-05-31 00:19:45 +0800828 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
829
Josh Lehan74d9bd92019-10-31 08:51:58 -0700830 // Similarly, if sensor scaling factor is being customized,
831 // then the below power-of-10 constraint becomes unnecessary,
832 // as config should be able to specify an arbitrary divisor.
833 unsigned int factor = psuProperty->sensorScaleFactor;
834 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700835 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700836 // Preserve existing usage of hardcoded labelMatch table below
837 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700838
Josh Lehan74d9bd92019-10-31 08:51:58 -0700839 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700840 char firstChar =
841 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700842 std::string strScaleFactor =
843 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
844
845 // Preserve existing configs by accepting earlier syntax,
846 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700847 auto findScaleFactor = baseConfig->find(strScaleFactor);
848 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700849 {
850 factor = std::visit(VariantToIntVisitor(),
851 findScaleFactor->second);
852 }
853
Ed Tanous8a57ec02020-10-09 12:46:52 -0700854 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700855 {
856 std::cerr << "Sensor scaling factor " << factor
857 << " string " << strScaleFactor << "\n";
858 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700859 }
860
Vijay Khemka996bad12019-05-28 15:15:16 -0700861 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530862 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
863 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800864 {
James Feist17ab6e02019-06-25 12:28:13 -0700865 std::cerr << "error populating thresholds for "
866 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800867 }
868
Zev Weiss6b6891c2021-04-22 02:46:21 -0500869 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
870 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800871 {
Jason Ling5747fab2019-10-02 16:46:23 -0700872 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700873 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800874 continue;
875 }
876
Ed Tanous8a57ec02020-10-09 12:46:52 -0700877 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700878 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700879 std::cerr << "Sensor properties: Name \""
880 << psuProperty->labelTypeName << "\" Scale "
881 << psuProperty->sensorScaleFactor << " Min "
882 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800883 << psuProperty->maxReading << " Offset "
884 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700885 }
886
887 std::string sensorName = psuProperty->labelTypeName;
888 if (customizedName)
889 {
890 if (sensorName.empty())
891 {
892 // Allow selective disabling of an individual sensor,
893 // by customizing its name to an empty string.
894 std::cerr << "Sensor disabled, empty string\n";
895 continue;
896 }
897 }
898 else
899 {
900 // Sensor name not customized, do prefix/suffix composition,
901 // preserving default behavior by using psuNameFromIndex.
902 sensorName =
903 psuNameFromIndex + " " + psuProperty->labelTypeName;
904 }
905
Ed Tanous8a57ec02020-10-09 12:46:52 -0700906 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700907 {
908 std::cerr << "Sensor name \"" << sensorName << "\" path \""
909 << sensorPathStr << "\" type \"" << sensorType
910 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700911 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800912 // destruct existing one first if already created
913 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800914 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800915 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800916 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000917 readState, findSensorUnit->second, factor,
918 psuProperty->maxReading, psuProperty->minReading,
919 psuProperty->sensorOffset, labelHead, thresholdConfSize,
920 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800921 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700922 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700923 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700924 {
925 std::cerr << "Created " << numCreated << " sensors so far\n";
926 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800927 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800928
929 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800930 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800931 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000932 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
933 *psuName, readState,
934 eventPathList, groupEventPathList,
935 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800936 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700937
Ed Tanous8a57ec02020-10-09 12:46:52 -0700938 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700939 {
940 std::cerr << "Created total of " << numCreated << " sensors\n";
941 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700942 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800943
Zhikui Ren23c96e72020-11-05 22:32:28 -0800944void createSensors(
945 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
946 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
947 const std::shared_ptr<boost::container::flat_set<std::string>>&
948 sensorsChanged)
949{
950 auto getter = std::make_shared<GetSensorConfiguration>(
951 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
952 const ManagedObjectType& sensorConfigs) {
953 createSensorsCallback(io, objectServer, dbusConnection,
954 sensorConfigs, sensorsChanged);
955 });
956 getter->getConfiguration(
957 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
958}
959
Cheng C Yang916360b2019-05-07 18:47:16 +0800960void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800961{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500962 sensorTable = {{"power", sensor_paths::unitWatts},
963 {"curr", sensor_paths::unitAmperes},
964 {"temp", sensor_paths::unitDegreesC},
965 {"in", sensor_paths::unitVolts},
966 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800967
Jeff Line41d52f2021-04-07 19:38:51 +0800968 labelMatch = {
969 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800970 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
971 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800972 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
973 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
974 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
975 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800976 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
977 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
978 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800979 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
980 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
981 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
982 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
985 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
986 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
987 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
988 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
989 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
993 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
994 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
995 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
997 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
998 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
999 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1000 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1001 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1002 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1003 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1004 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1005 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1006 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1007 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1008 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1009 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1010 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1011 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1012 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1013 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1014 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001015 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001016 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001017 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1018 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1019 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1020 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1021 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1022 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001023 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001024 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1025 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001026 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1027 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1028 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1029 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1030 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1031 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1032 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1033 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1034 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1035 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1036 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1037 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1038 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1039 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1040 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001041 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1042 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1043 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001044 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1045 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1046 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1047 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1048 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1049 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1050 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1051 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1052 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
1053 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001054
1055 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001056
1057 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1058 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1059
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001060 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1061 {"Failure", {"in2_alarm"}},
1062 {"ACLost", {"in1_beep"}},
1063 {"ConfigureError", {"in1_fault"}}};
1064
1065 groupEventMatch = {{"FanFault",
1066 {{"fan1", {"fan1_alarm", "fan1_fault"}},
1067 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001068}
1069
James Feistb6c0b912019-07-09 12:21:44 -07001070int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001071{
1072 boost::asio::io_service io;
1073 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1074
1075 systemBus->request_name("xyz.openbmc_project.PSUSensor");
1076 sdbusplus::asio::object_server objectServer(systemBus);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001077 auto sensorsChanged =
1078 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001079
Cheng C Yang916360b2019-05-07 18:47:16 +08001080 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001081
Zhikui Ren23c96e72020-11-05 22:32:28 -08001082 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +08001083 boost::asio::deadline_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001084 std::function<void(sdbusplus::message_t&)> eventHandler =
1085 [&](sdbusplus::message_t& message) {
Cheng C Yang209ec562019-03-12 16:37:44 +08001086 if (message.is_method_error())
1087 {
1088 std::cerr << "callback method error\n";
1089 return;
1090 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001091 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +08001092 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +08001093 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1094 if (ec == boost::asio::error::operation_aborted)
1095 {
1096 return;
1097 }
Ed Tanous8a57ec02020-10-09 12:46:52 -07001098 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +08001099 {
1100 std::cerr << "timer error\n";
1101 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001102 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +08001103 });
1104 };
1105
Zev Weiss214d9712022-08-12 12:54:31 -07001106 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1107 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +08001108 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001109 io.run();
1110}