blob: b7067e4d26847a98e9725ae60c592c347cf1e91f [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
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "PSUEvent.hpp"
18#include "PSUSensor.hpp"
19#include "Utils.hpp"
20
Cheng C Yang209ec562019-03-12 16:37:44 +080021#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*>(
Zev Weissbc56cfb2022-11-21 16:40:09 -080044 {"ADM1266", "ADM1272", "ADM1275", "ADM1278", "ADM1293",
45 "ADS7830", "AHE50DC_FAN", "BMR490", "DPS800", "INA219",
46 "INA230", "IPSPS", "IR38060", "IR38164", "IR38263",
47 "ISL68137", "ISL68220", "ISL68223", "ISL69225", "ISL69243",
48 "ISL69260", "LM25066", "MAX16601", "MAX20710", "MAX20730",
49 "MAX20734", "MAX20796", "MAX34451", "MP2971", "MP2973",
Konstantin Aladyshevc61a4a42023-02-20 18:31:16 +030050 "MP2975", "MP5023", "PLI1209BC", "pmbus", "PXE1610",
51 "RAA228000", "RAA228228", "RAA228620", "RAA229001", "RAA229004",
52 "RAA229126", "TPS53679", "TPS546D24", "XDPE11280", "XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080053
Ed Tanousa2df7862021-12-07 16:30:27 -080054// clang-format off
55static constexpr auto pmbusNames{std::to_array<const char*>({
56 "adm1266",
57 "adm1272",
58 "adm1275",
59 "adm1278",
Zhikui Ren842e5432022-03-09 16:18:24 -080060 "adm1293",
Tim Chao6f379ce2022-02-24 11:08:09 +080061 "ads7830",
Zev Weissbc56cfb2022-11-21 16:40:09 -080062 "ahe50dc_fan",
linchuyuan5cf66df2021-12-21 17:38:55 -080063 "bmr490",
Ed Tanousa2df7862021-12-07 16:30:27 -080064 "dps800",
65 "ina219",
66 "ina230",
67 "ipsps1",
Naresh Solankiadbcd7c2022-11-02 10:07:26 +010068 "ir38060",
69 "ir38164",
70 "ir38263",
Ed Tanousa2df7862021-12-07 16:30:27 -080071 "isl68137",
72 "isl68220",
73 "isl68223",
Khang Kieu51ad6672022-02-09 01:26:25 +000074 "isl69225",
Ed Tanousa2df7862021-12-07 16:30:27 -080075 "isl69243",
76 "isl69260",
77 "lm25066",
78 "max16601",
79 "max20710",
80 "max20730",
81 "max20734",
82 "max20796",
83 "max34451",
Shamim Alidccd1d42022-06-08 22:31:35 +053084 "mp2971",
85 "mp2973",
Konstantin Aladyshevc61a4a42023-02-20 18:31:16 +030086 "mp2975",
Howard Chiub58ac3a2021-12-07 17:12:56 +080087 "mp5023",
Naresh Solankiadbcd7c2022-11-02 10:07:26 +010088 "pli1209bc",
Ed Tanousa2df7862021-12-07 16:30:27 -080089 "pmbus",
90 "pxe1610",
91 "raa228000",
92 "raa228228",
Tom Tung75da5152021-11-24 09:38:07 +080093 "raa228620",
94 "raa229001",
Ed Tanousa2df7862021-12-07 16:30:27 -080095 "raa229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -080096 "raa229126",
Konstantin Aladyshev21e12c32022-10-19 18:56:10 +030097 "tps53679",
Ed Tanousa2df7862021-12-07 16:30:27 -080098 "tps546d24",
Naresh Solankiadbcd7c2022-11-02 10:07:26 +010099 "xdpe11280",
Ed Tanousa2df7862021-12-07 16:30:27 -0800100 "xdpe12284"
101})};
102//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700103
Cheng C Yang209ec562019-03-12 16:37:44 +0800104namespace fs = std::filesystem;
105
Yong Libf8b1da2020-04-15 16:32:50 +0800106static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800107 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800108static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
109 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800110static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
111 pwmSensors;
112static boost::container::flat_map<std::string, std::string> sensorTable;
113static boost::container::flat_map<std::string, PSUProperty> labelMatch;
114static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800115static boost::container::flat_map<std::string, std::vector<std::string>>
116 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800117static boost::container::flat_map<
118 std::string,
119 boost::container::flat_map<std::string, std::vector<std::string>>>
120 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800121static boost::container::flat_map<std::string, std::vector<std::string>>
122 limitEventMatch;
123
Josh Lehan74d9bd92019-10-31 08:51:58 -0700124static std::vector<PSUProperty> psuProperties;
125
Cheng C Yang58b2b532019-05-31 00:19:45 +0800126// Function CheckEvent will check each attribute from eventMatch table in the
127// sysfs. If the attributes exists in sysfs, then store the complete path
128// of the attribute into eventPathList.
129void checkEvent(
130 const std::string& directory,
131 const boost::container::flat_map<std::string, std::vector<std::string>>&
132 eventMatch,
133 boost::container::flat_map<std::string, std::vector<std::string>>&
134 eventPathList)
135{
136 for (const auto& match : eventMatch)
137 {
138 const std::vector<std::string>& eventAttrs = match.second;
139 const std::string& eventName = match.first;
140 for (const auto& eventAttr : eventAttrs)
141 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700142 std::string eventPath = directory;
143 eventPath += "/";
144 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800145
146 std::ifstream eventFile(eventPath);
147 if (!eventFile.good())
148 {
149 continue;
150 }
151
152 eventPathList[eventName].push_back(eventPath);
153 }
154 }
155}
156
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800157// Check Group Events which contains more than one targets in each combine
158// events.
159void checkGroupEvent(
160 const std::string& directory,
161 const boost::container::flat_map<
162 std::string,
163 boost::container::flat_map<std::string, std::vector<std::string>>>&
164 groupEventMatch,
165 boost::container::flat_map<
166 std::string,
167 boost::container::flat_map<std::string, std::vector<std::string>>>&
168 groupEventPathList)
169{
170 for (const auto& match : groupEventMatch)
171 {
172 const std::string& groupEventName = match.first;
173 const boost::container::flat_map<std::string, std::vector<std::string>>
174 events = match.second;
175 boost::container::flat_map<std::string, std::vector<std::string>>
176 pathList;
177 for (const auto& match : events)
178 {
179 const std::string& eventName = match.first;
180 const std::vector<std::string>& eventAttrs = match.second;
181 for (const auto& eventAttr : eventAttrs)
182 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700183 std::string eventPath = directory;
184 eventPath += "/";
185 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800186 std::ifstream eventFile(eventPath);
187 if (!eventFile.good())
188 {
189 continue;
190 }
191
192 pathList[eventName].push_back(eventPath);
193 }
194 }
195 groupEventPathList[groupEventName] = pathList;
196 }
197}
198
Cheng C Yang58b2b532019-05-31 00:19:45 +0800199// Function checkEventLimits will check all the psu related xxx_input attributes
200// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
201// xxx_min_alarm exist, then store the existing paths of the alarm attributes
202// to eventPathList.
203void checkEventLimits(
204 const std::string& sensorPathStr,
205 const boost::container::flat_map<std::string, std::vector<std::string>>&
206 limitEventMatch,
207 boost::container::flat_map<std::string, std::vector<std::string>>&
208 eventPathList)
209{
Lei YUa2c7cea2020-12-23 14:07:28 +0800210 auto attributePartPos = sensorPathStr.find_last_of('_');
211 if (attributePartPos == std::string::npos)
212 {
213 // There is no '_' in the string, skip it
214 return;
215 }
216 auto attributePart =
217 std::string_view(sensorPathStr).substr(attributePartPos + 1);
218 if (attributePart != "input")
219 {
220 // If the sensor is not xxx_input, skip it
221 return;
222 }
223
224 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800225 for (const auto& limitMatch : limitEventMatch)
226 {
227 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
228 const std::string& eventName = limitMatch.first;
229 for (const auto& limitEventAttr : limitEventAttrs)
230 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800231 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800232 std::ifstream eventFile(limitEventPath);
233 if (!eventFile.good())
234 {
235 continue;
236 }
237 eventPathList[eventName].push_back(limitEventPath);
238 }
239 }
240}
Cheng C Yang916360b2019-05-07 18:47:16 +0800241
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530242static void
243 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
244 const std::string& interfacePath,
245 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
246 sdbusplus::asio::object_server& objectServer,
247 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800248{
Zev Weiss741f26e2022-08-12 18:21:02 -0700249 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800250 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700251 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800252 {
253 continue;
254 }
255
256 const std::string& sensorPathStr = sensorPath.string();
257 const std::string& pwmPathStr =
258 boost::replace_all_copy(sensorPathStr, "input", "target");
259 std::ifstream pwmFile(pwmPathStr);
260 if (!pwmFile.good())
261 {
262 continue;
263 }
264
265 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
266 if (findPWMSensor != pwmSensors.end())
267 {
268 continue;
269 }
270
Zev Weissd8c293a2022-08-15 18:58:41 -0700271 std::string name = "Pwm_";
272 name += psuName;
273 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700274 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700275
276 std::string objPath = interfacePath;
277 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700278 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700279
Cheng C Yang916360b2019-05-07 18:47:16 +0800280 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700281 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800282 }
283}
284
Zhikui Ren23c96e72020-11-05 22:32:28 -0800285static void createSensorsCallback(
286 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
287 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
288 const ManagedObjectType& sensorConfigs,
289 const std::shared_ptr<boost::container::flat_set<std::string>>&
290 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800291{
Josh Lehan49cfba92019-10-08 16:50:42 -0700292 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800293 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800294
295 std::vector<fs::path> pmbusPaths;
296 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
297 {
298 std::cerr << "No PSU sensors in system\n";
299 return;
300 }
301
302 boost::container::flat_set<std::string> directories;
303 for (const auto& pmbusPath : pmbusPaths)
304 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800305 boost::container::flat_map<std::string, std::vector<std::string>>
306 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800307 boost::container::flat_map<
308 std::string,
309 boost::container::flat_map<std::string, std::vector<std::string>>>
310 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800311
312 std::ifstream nameFile(pmbusPath);
313 if (!nameFile.good())
314 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700315 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800316 continue;
317 }
318
319 std::string pmbusName;
320 std::getline(nameFile, pmbusName);
321 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700322
323 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
324 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800325 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700326 // To avoid this error message, add your driver name to
327 // the pmbusNames vector at the top of this file.
328 std::cerr << "Driver name " << pmbusName
329 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800330 continue;
331 }
332
Cheng C Yang209ec562019-03-12 16:37:44 +0800333 auto directory = pmbusPath.parent_path();
334
335 auto ret = directories.insert(directory.string());
336 if (!ret.second)
337 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700338 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800339 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800340 }
341
James Feistb6c0b912019-07-09 12:21:44 -0700342 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800343 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700344 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800345 if (findHyphen == std::string::npos)
346 {
347 std::cerr << "found bad device" << deviceName << "\n";
348 continue;
349 }
350 std::string busStr = deviceName.substr(0, findHyphen);
351 std::string addrStr = deviceName.substr(findHyphen + 1);
352
353 size_t bus = 0;
354 size_t addr = 0;
355
356 try
357 {
358 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700359 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800360 }
Patrick Williams26601e82021-10-06 12:43:25 -0500361 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800362 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700363 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
364 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800365 continue;
366 }
367
Zev Weisse8b97ee2022-08-12 15:23:51 -0700368 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800369 const SensorData* sensorData = nullptr;
370 const std::string* interfacePath = nullptr;
371 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800372 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800373
Zev Weiss741f26e2022-08-12 18:21:02 -0700374 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800375 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700376 sensorData = &cfgData;
Cheng C Yang209ec562019-03-12 16:37:44 +0800377 for (const char* type : sensorTypes)
378 {
Zev Weiss054aad82022-08-18 01:37:34 -0700379 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800380 if (sensorBase != sensorData->end())
381 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700382 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800383 sensorType = type;
384 break;
385 }
386 }
387 if (baseConfig == nullptr)
388 {
389 std::cerr << "error finding base configuration for "
390 << deviceName << "\n";
391 continue;
392 }
393
Zev Weisse8b97ee2022-08-12 15:23:51 -0700394 auto configBus = baseConfig->find("Bus");
395 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800396
Zev Weisse8b97ee2022-08-12 15:23:51 -0700397 if (configBus == baseConfig->end() ||
398 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800399 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800400 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800401 continue;
402 }
403
Matt Simmering6747eba2023-01-18 16:03:55 -0800404 const uint64_t* confBus =
405 std::get_if<uint64_t>(&(configBus->second));
406 const uint64_t* confAddr =
407 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800408 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800409 {
410 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700411 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800412 continue;
413 }
414
415 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800416 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700417 std::cerr << "Configuration skipping " << *confBus << "-"
418 << *confAddr << " because not " << bus << "-" << addr
419 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800420 continue;
421 }
422
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800423 std::vector<thresholds::Threshold> confThresholds;
424 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
425 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800426 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800427 }
428 thresholdConfSize = confThresholds.size();
429
Zev Weiss741f26e2022-08-12 18:21:02 -0700430 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800431 break;
432 }
433 if (interfacePath == nullptr)
434 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700435 // To avoid this error message, add your export map entry,
436 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800437 std::cerr << "failed to find match for " << deviceName << "\n";
438 continue;
439 }
440
Zev Weisse8b97ee2022-08-12 15:23:51 -0700441 auto findPSUName = baseConfig->find("Name");
442 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800443 {
444 std::cerr << "could not determine configuration name for "
445 << deviceName << "\n";
446 continue;
447 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800448 const std::string* psuName =
449 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800450 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800451 {
452 std::cerr << "Cannot find psu name, invalid configuration\n";
453 continue;
454 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800455
456 // on rescans, only update sensors we were signaled by
457 if (!firstScan)
458 {
Zhikui Renda98f092021-11-01 09:41:08 -0700459 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800460 auto it =
461 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
462 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800463 return s.ends_with(psuNameStr);
464 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800465
466 if (it == sensorsChanged->end())
467 {
468 continue;
469 }
470 sensorsChanged->erase(it);
471 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800472 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800473 checkGroupEvent(directory.string(), groupEventMatch,
474 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800475
Zev Weisse8b97ee2022-08-12 15:23:51 -0700476 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000477
Vijay Khemka996bad12019-05-28 15:15:16 -0700478 /* Check if there are more sensors in the same interface */
479 int i = 1;
480 std::vector<std::string> psuNames;
481 do
482 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700483 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700484 psuNames.push_back(
485 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700486 findPSUName = baseConfig->find("Name" + std::to_string(i++));
487 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700488
Cheng C Yange50345b2019-04-02 17:26:15 +0800489 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700490 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800491 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800492 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800493 continue;
494 }
495
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530496 /* read max value in sysfs for in, curr, power, temp, ... */
497 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
498 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700499 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530500 {
501 std::cerr << "No max name in PSU \n";
502 }
503 }
504
Zev Weiss8569bf22022-10-11 15:37:44 -0700505 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800506
Vijay Khemka996bad12019-05-28 15:15:16 -0700507 /* Find array of labels to be exposed if it is defined in config */
508 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700509 auto findLabelObj = baseConfig->find("Labels");
510 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700511 {
512 findLabels =
513 std::get<std::vector<std::string>>(findLabelObj->second);
514 }
515
Jason Ling5747fab2019-10-02 16:46:23 -0700516 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
517 std::smatch matches;
518
Cheng C Yange50345b2019-04-02 17:26:15 +0800519 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800520 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530521 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800522 std::string labelHead;
523 std::string sensorPathStr = sensorPath.string();
524 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700525 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700526 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
527 {
Josh Lehan06494452019-10-31 09:49:16 -0700528 // hwmon *_input filename without number:
529 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700530 sensorNameSubStr = matches[1];
531 }
532 else
533 {
Josh Lehan06494452019-10-31 09:49:16 -0700534 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700535 << sensorNameStr;
536 continue;
537 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800538
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530539 std::string labelPath;
540
541 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700542 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530543 if (pos != std::string::npos)
544 {
545
546 std::string sensorPathStrMax = sensorPathStr.substr(pos);
Ed Tanous2049bd22022-07-09 07:20:26 -0700547 if (sensorPathStrMax == "_max")
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530548 {
549 labelPath =
550 boost::replace_all_copy(sensorPathStr, "max", "label");
551 maxLabel = true;
552 }
553 else
554 {
555 labelPath = boost::replace_all_copy(sensorPathStr, "input",
556 "label");
557 maxLabel = false;
558 }
559 }
560 else
561 {
562 continue;
563 }
564
Cheng C Yangecba9de2019-09-12 23:41:50 +0800565 std::ifstream labelFile(labelPath);
566 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800567 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700568 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800569 {
570 std::cerr << "Input file " << sensorPath
571 << " has no corresponding label file\n";
572 }
Josh Lehan06494452019-10-31 09:49:16 -0700573 // hwmon *_input filename with number:
574 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700575 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800576 }
577 else
578 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800579 std::string label;
580 std::getline(labelFile, label);
581 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800582 auto findSensor = sensors.find(label);
583 if (findSensor != sensors.end())
584 {
585 continue;
586 }
587
Josh Lehan06494452019-10-31 09:49:16 -0700588 // hwmon corresponding *_label file contents:
589 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700590 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800591 }
592
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530593 /* append "max" for labelMatch */
594 if (maxLabel)
595 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700596 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530597 }
598
Ed Tanous8a57ec02020-10-09 12:46:52 -0700599 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700600 {
601 std::cerr << "Sensor type=\"" << sensorNameSubStr
602 << "\" label=\"" << labelHead << "\"\n";
603 }
604
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530605 checkPWMSensor(sensorPath, labelHead, *interfacePath,
606 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800607
Vijay Khemka996bad12019-05-28 15:15:16 -0700608 if (!findLabels.empty())
609 {
610 /* Check if this labelHead is enabled in config file */
611 if (std::find(findLabels.begin(), findLabels.end(),
612 labelHead) == findLabels.end())
613 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700614 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800615 {
616 std::cerr << "could not find " << labelHead
617 << " in the Labels list\n";
618 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700619 continue;
620 }
621 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800622
Cheng C Yange50345b2019-04-02 17:26:15 +0800623 auto findProperty = labelMatch.find(labelHead);
624 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800625 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700626 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700627 {
628 std::cerr << "Could not find matching default property for "
629 << labelHead << "\n";
630 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800631 continue;
632 }
633
Josh Lehan74d9bd92019-10-31 08:51:58 -0700634 // Protect the hardcoded labelMatch list from changes,
635 // by making a copy and modifying that instead.
636 // Avoid bleedthrough of one device's customizations to
637 // the next device, as each should be independently customizable.
638 psuProperties.push_back(findProperty->second);
639 auto psuProperty = psuProperties.rbegin();
640
641 // Use label head as prefix for reading from config file,
642 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
643 std::string keyName = labelHead + "_Name";
644 std::string keyScale = labelHead + "_Scale";
645 std::string keyMin = labelHead + "_Min";
646 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800647 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800648 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700649
650 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700651 auto findCustomName = baseConfig->find(keyName);
652 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700653 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700654 try
655 {
656 psuProperty->labelTypeName = std::visit(
657 VariantToStringVisitor(), findCustomName->second);
658 }
Patrick Williams26601e82021-10-06 12:43:25 -0500659 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700660 {
661 std::cerr << "Unable to parse " << keyName << "\n";
662 continue;
663 }
664
665 // All strings are valid, including empty string
666 customizedName = true;
667 }
668
669 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700670 auto findCustomScale = baseConfig->find(keyScale);
671 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700672 {
673 try
674 {
675 psuProperty->sensorScaleFactor = std::visit(
676 VariantToUnsignedIntVisitor(), findCustomScale->second);
677 }
Patrick Williams26601e82021-10-06 12:43:25 -0500678 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700679 {
680 std::cerr << "Unable to parse " << keyScale << "\n";
681 continue;
682 }
683
684 // Avoid later division by zero
685 if (psuProperty->sensorScaleFactor > 0)
686 {
687 customizedScale = true;
688 }
689 else
690 {
691 std::cerr << "Unable to accept " << keyScale << "\n";
692 continue;
693 }
694 }
695
Zev Weisse8b97ee2022-08-12 15:23:51 -0700696 auto findCustomMin = baseConfig->find(keyMin);
697 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700698 {
699 try
700 {
701 psuProperty->minReading = std::visit(
702 VariantToDoubleVisitor(), findCustomMin->second);
703 }
Patrick Williams26601e82021-10-06 12:43:25 -0500704 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700705 {
706 std::cerr << "Unable to parse " << keyMin << "\n";
707 continue;
708 }
709 }
710
Zev Weisse8b97ee2022-08-12 15:23:51 -0700711 auto findCustomMax = baseConfig->find(keyMax);
712 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700713 {
714 try
715 {
716 psuProperty->maxReading = std::visit(
717 VariantToDoubleVisitor(), findCustomMax->second);
718 }
Patrick Williams26601e82021-10-06 12:43:25 -0500719 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700720 {
721 std::cerr << "Unable to parse " << keyMax << "\n";
722 continue;
723 }
724 }
725
Zev Weisse8b97ee2022-08-12 15:23:51 -0700726 auto findCustomOffset = baseConfig->find(keyOffset);
727 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800728 {
729 try
730 {
731 psuProperty->sensorOffset = std::visit(
732 VariantToDoubleVisitor(), findCustomOffset->second);
733 }
Patrick Williams26601e82021-10-06 12:43:25 -0500734 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800735 {
736 std::cerr << "Unable to parse " << keyOffset << "\n";
737 continue;
738 }
739 }
740
Lotus Xucb5af732021-09-10 15:18:50 +0800741 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700742 auto findPowerState = baseConfig->find(keyPowerState);
743 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800744 {
745 std::string powerState = std::visit(VariantToStringVisitor(),
746 findPowerState->second);
747 setReadState(powerState, readState);
748 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700749 if (!(psuProperty->minReading < psuProperty->maxReading))
750 {
751 std::cerr << "Min must be less than Max\n";
752 continue;
753 }
754
755 // If the sensor name is being customized by config file,
756 // then prefix/suffix composition becomes not necessary,
757 // and in fact not wanted, because it gets in the way.
758 std::string psuNameFromIndex;
759 if (!customizedName)
760 {
761 /* Find out sensor name index for this label */
762 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500763 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700764 if (std::regex_search(labelHead, matches, rgx))
765 {
766 nameIndex = std::stoi(matches[1]);
767
768 // Decrement to preserve alignment, because hwmon
769 // human-readable filenames and labels use 1-based
770 // numbering, but the "Name", "Name1", "Name2", etc. naming
771 // convention (the psuNames vector) uses 0-based numbering.
772 if (nameIndex > 0)
773 {
774 --nameIndex;
775 }
776 }
777 else
778 {
779 nameIndex = 0;
780 }
781
782 if (psuNames.size() <= nameIndex)
783 {
784 std::cerr << "Could not pair " << labelHead
785 << " with a Name field\n";
786 continue;
787 }
788
789 psuNameFromIndex = psuNames[nameIndex];
790
Ed Tanous8a57ec02020-10-09 12:46:52 -0700791 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700792 {
793 std::cerr << "Sensor label head " << labelHead
794 << " paired with " << psuNameFromIndex
795 << " at index " << nameIndex << "\n";
796 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700797 }
798
Cheng C Yang58b2b532019-05-31 00:19:45 +0800799 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
800
Josh Lehan74d9bd92019-10-31 08:51:58 -0700801 // Similarly, if sensor scaling factor is being customized,
802 // then the below power-of-10 constraint becomes unnecessary,
803 // as config should be able to specify an arbitrary divisor.
804 unsigned int factor = psuProperty->sensorScaleFactor;
805 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700806 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700807 // Preserve existing usage of hardcoded labelMatch table below
808 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700809
Josh Lehan74d9bd92019-10-31 08:51:58 -0700810 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700811 char firstChar =
812 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700813 std::string strScaleFactor =
814 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
815
816 // Preserve existing configs by accepting earlier syntax,
817 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700818 auto findScaleFactor = baseConfig->find(strScaleFactor);
819 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700820 {
821 factor = std::visit(VariantToIntVisitor(),
822 findScaleFactor->second);
823 }
824
Ed Tanous8a57ec02020-10-09 12:46:52 -0700825 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700826 {
827 std::cerr << "Sensor scaling factor " << factor
828 << " string " << strScaleFactor << "\n";
829 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700830 }
831
Vijay Khemka996bad12019-05-28 15:15:16 -0700832 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530833 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
834 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800835 {
James Feist17ab6e02019-06-25 12:28:13 -0700836 std::cerr << "error populating thresholds for "
837 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800838 }
839
Zev Weiss6b6891c2021-04-22 02:46:21 -0500840 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
841 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800842 {
Jason Ling5747fab2019-10-02 16:46:23 -0700843 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700844 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800845 continue;
846 }
847
Ed Tanous8a57ec02020-10-09 12:46:52 -0700848 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700849 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700850 std::cerr << "Sensor properties: Name \""
851 << psuProperty->labelTypeName << "\" Scale "
852 << psuProperty->sensorScaleFactor << " Min "
853 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800854 << psuProperty->maxReading << " Offset "
855 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700856 }
857
858 std::string sensorName = psuProperty->labelTypeName;
859 if (customizedName)
860 {
861 if (sensorName.empty())
862 {
863 // Allow selective disabling of an individual sensor,
864 // by customizing its name to an empty string.
865 std::cerr << "Sensor disabled, empty string\n";
866 continue;
867 }
868 }
869 else
870 {
871 // Sensor name not customized, do prefix/suffix composition,
872 // preserving default behavior by using psuNameFromIndex.
873 sensorName =
874 psuNameFromIndex + " " + psuProperty->labelTypeName;
875 }
876
Ed Tanous8a57ec02020-10-09 12:46:52 -0700877 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700878 {
879 std::cerr << "Sensor name \"" << sensorName << "\" path \""
880 << sensorPathStr << "\" type \"" << sensorType
881 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700882 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800883 // destruct existing one first if already created
884 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800885 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800886 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800887 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000888 readState, findSensorUnit->second, factor,
889 psuProperty->maxReading, psuProperty->minReading,
890 psuProperty->sensorOffset, labelHead, thresholdConfSize,
891 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800892 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700893 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700894 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700895 {
896 std::cerr << "Created " << numCreated << " sensors so far\n";
897 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800898 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800899
900 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800901 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800902 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000903 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
904 *psuName, readState,
905 eventPathList, groupEventPathList,
906 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800907 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700908
Ed Tanous8a57ec02020-10-09 12:46:52 -0700909 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700910 {
911 std::cerr << "Created total of " << numCreated << " sensors\n";
912 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800913}
Cheng C Yang209ec562019-03-12 16:37:44 +0800914
Zhikui Ren23c96e72020-11-05 22:32:28 -0800915void createSensors(
916 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
917 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
918 const std::shared_ptr<boost::container::flat_set<std::string>>&
919 sensorsChanged)
920{
921 auto getter = std::make_shared<GetSensorConfiguration>(
922 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
923 const ManagedObjectType& sensorConfigs) {
924 createSensorsCallback(io, objectServer, dbusConnection,
925 sensorConfigs, sensorsChanged);
926 });
927 getter->getConfiguration(
928 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
929}
930
Cheng C Yang916360b2019-05-07 18:47:16 +0800931void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800932{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500933 sensorTable = {{"power", sensor_paths::unitWatts},
934 {"curr", sensor_paths::unitAmperes},
935 {"temp", sensor_paths::unitDegreesC},
936 {"in", sensor_paths::unitVolts},
937 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800938
Jeff Line41d52f2021-04-07 19:38:51 +0800939 labelMatch = {
940 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800941 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
942 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800943 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
944 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
945 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
946 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800947 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
948 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
949 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800950 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
951 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
952 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
953 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
954 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
955 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
956 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
957 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
958 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
959 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
960 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
961 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
962 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
963 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
964 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
965 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
966 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
967 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
968 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
969 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
970 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
971 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
972 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
973 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
974 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
975 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
976 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
977 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
978 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
979 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
980 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
981 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
982 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
985 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +0800986 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800987 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +0800988 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
989 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
993 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800994 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800995 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
996 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800997 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
998 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
999 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1000 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1001 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1002 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1003 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1004 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1005 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1006 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1007 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1008 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1009 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1010 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1011 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001012 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1013 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1014 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001015 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1016 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1017 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1018 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1019 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1020 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1021 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1022 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1023 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001024 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1025 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1026 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001027
Matt Simmering6747eba2023-01-18 16:03:55 -08001028 pwmTable = {{"fan1", "Fan_1"},
1029 {"fan2", "Fan_2"},
1030 {"fan3", "Fan_3"},
1031 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001032
1033 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1034 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1035
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001036 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1037 {"Failure", {"in2_alarm"}},
1038 {"ACLost", {"in1_beep"}},
1039 {"ConfigureError", {"in1_fault"}}};
1040
1041 groupEventMatch = {{"FanFault",
1042 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001043 {"fan2", {"fan2_alarm", "fan2_fault"}},
1044 {"fan3", {"fan3_alarm", "fan3_fault"}},
1045 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001046}
1047
James Feistb6c0b912019-07-09 12:21:44 -07001048int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001049{
1050 boost::asio::io_service io;
1051 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1052
Ed Tanous14ed5e92022-07-12 15:50:23 -07001053 sdbusplus::asio::object_server objectServer(systemBus, true);
1054 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001055 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001056 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001057 auto sensorsChanged =
1058 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001059
Cheng C Yang916360b2019-05-07 18:47:16 +08001060 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001061
Zhikui Ren23c96e72020-11-05 22:32:28 -08001062 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001063 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001064 std::function<void(sdbusplus::message_t&)> eventHandler =
1065 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001066 if (message.is_method_error())
1067 {
1068 std::cerr << "callback method error\n";
1069 return;
1070 }
1071 sensorsChanged->insert(message.get_path());
1072 filterTimer.expires_from_now(std::chrono::seconds(3));
1073 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1074 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001075 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001076 return;
1077 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001078 if (ec)
1079 {
1080 std::cerr << "timer error\n";
1081 }
1082 createSensors(io, objectServer, systemBus, sensorsChanged);
1083 });
1084 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001085
Zev Weiss214d9712022-08-12 12:54:31 -07001086 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1087 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +08001088 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001089 io.run();
1090}