blob: 07802cd49448e0c86e198f7f9e59f9a8c9edba6f [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
Matt Simmering786efb82023-01-18 14:09:21 -080017#include "DeviceMgmt.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103018#include "PSUEvent.hpp"
19#include "PSUSensor.hpp"
20#include "Utils.hpp"
21
Cheng C Yang209ec562019-03-12 16:37:44 +080022#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <boost/container/flat_map.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080024#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070025#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27#include <sdbusplus/bus/match.hpp>
28
29#include <array>
Josh Lehan74d9bd92019-10-31 08:51:58 -070030#include <cmath>
James Feist24f02f22019-04-15 11:05:39 -070031#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080032#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <functional>
Cheng C Yang58b2b532019-05-31 00:19:45 +080034#include <iostream>
Cheng C Yang209ec562019-03-12 16:37:44 +080035#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <string>
Lei YUa2c7cea2020-12-23 14:07:28 +080037#include <string_view>
Patrick Venture96e97db2019-10-31 13:44:38 -070038#include <utility>
39#include <variant>
40#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080041
Ed Tanous8a57ec02020-10-09 12:46:52 -070042static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070043
Matt Simmering786efb82023-01-18 14:09:21 -080044static const I2CDeviceTypeMap sensorTypes{
45 {"ADM1266", I2CDeviceType{"adm1266", true}},
46 {"ADM1272", I2CDeviceType{"adm1272", true}},
47 {"ADM1275", I2CDeviceType{"adm1275", true}},
48 {"ADM1278", I2CDeviceType{"adm1278", true}},
49 {"ADM1293", I2CDeviceType{"adm1293", true}},
50 {"ADS7830", I2CDeviceType{"ads7830", true}},
51 {"AHE50DC_FAN", I2CDeviceType{"ahe50dc_fan", true}},
52 {"BMR490", I2CDeviceType{"bmr490", true}},
53 {"DPS800", I2CDeviceType{"dps800", true}},
54 {"INA219", I2CDeviceType{"ina219", true}},
55 {"INA230", I2CDeviceType{"ina230", true}},
56 {"IPSPS", I2CDeviceType{"ipsps", true}},
57 {"IR38060", I2CDeviceType{"ir38060", true}},
58 {"IR38164", I2CDeviceType{"ir38164", true}},
59 {"IR38263", I2CDeviceType{"ir38263", true}},
60 {"ISL68137", I2CDeviceType{"isl68137", true}},
61 {"ISL68220", I2CDeviceType{"isl68220", true}},
62 {"ISL68223", I2CDeviceType{"isl68223", true}},
63 {"ISL69225", I2CDeviceType{"isl69225", true}},
64 {"ISL69243", I2CDeviceType{"isl69243", true}},
65 {"ISL69260", I2CDeviceType{"isl69260", true}},
66 {"LM25066", I2CDeviceType{"lm25066", true}},
67 {"MAX16601", I2CDeviceType{"max16601", true}},
68 {"MAX20710", I2CDeviceType{"max20710", true}},
69 {"MAX20730", I2CDeviceType{"max20730", true}},
70 {"MAX20734", I2CDeviceType{"max20734", true}},
71 {"MAX20796", I2CDeviceType{"max20796", true}},
72 {"MAX34451", I2CDeviceType{"max34451", true}},
73 {"MP2971", I2CDeviceType{"mp2971", true}},
74 {"MP2973", I2CDeviceType{"mp2973", true}},
75 {"MP2975", I2CDeviceType{"mp2975", true}},
76 {"MP5023", I2CDeviceType{"mp5023", true}},
77 {"NCP4200", I2CDeviceType{"ncp4200", true}},
78 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
79 {"pmbus", I2CDeviceType{"pmbus", true}},
80 {"PXE1610", I2CDeviceType{"pxe1610", true}},
81 {"RAA228000", I2CDeviceType{"raa228000", true}},
82 {"RAA228228", I2CDeviceType{"raa228228", true}},
83 {"RAA228620", I2CDeviceType{"raa228620", true}},
84 {"RAA229001", I2CDeviceType{"raa229001", true}},
85 {"RAA229004", I2CDeviceType{"raa229004", true}},
86 {"RAA229126", I2CDeviceType{"raa229126", true}},
87 {"TPS53679", I2CDeviceType{"tps53679", true}},
88 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
89 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
90 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
91};
Josh Lehan0830c7b2019-10-08 16:35:09 -070092
Cheng C Yang209ec562019-03-12 16:37:44 +080093namespace fs = std::filesystem;
94
Yong Libf8b1da2020-04-15 16:32:50 +080095static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +080096 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +080097static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
98 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +080099static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
100 pwmSensors;
101static boost::container::flat_map<std::string, std::string> sensorTable;
102static boost::container::flat_map<std::string, PSUProperty> labelMatch;
103static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800104static boost::container::flat_map<std::string, std::vector<std::string>>
105 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800106static boost::container::flat_map<
107 std::string,
108 boost::container::flat_map<std::string, std::vector<std::string>>>
109 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800110static boost::container::flat_map<std::string, std::vector<std::string>>
111 limitEventMatch;
112
Josh Lehan74d9bd92019-10-31 08:51:58 -0700113static std::vector<PSUProperty> psuProperties;
114
Cheng C Yang58b2b532019-05-31 00:19:45 +0800115// Function CheckEvent will check each attribute from eventMatch table in the
116// sysfs. If the attributes exists in sysfs, then store the complete path
117// of the attribute into eventPathList.
118void checkEvent(
119 const std::string& directory,
120 const boost::container::flat_map<std::string, std::vector<std::string>>&
121 eventMatch,
122 boost::container::flat_map<std::string, std::vector<std::string>>&
123 eventPathList)
124{
125 for (const auto& match : eventMatch)
126 {
127 const std::vector<std::string>& eventAttrs = match.second;
128 const std::string& eventName = match.first;
129 for (const auto& eventAttr : eventAttrs)
130 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700131 std::string eventPath = directory;
132 eventPath += "/";
133 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800134
135 std::ifstream eventFile(eventPath);
136 if (!eventFile.good())
137 {
138 continue;
139 }
140
141 eventPathList[eventName].push_back(eventPath);
142 }
143 }
144}
145
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800146// Check Group Events which contains more than one targets in each combine
147// events.
148void checkGroupEvent(
149 const std::string& directory,
150 const boost::container::flat_map<
151 std::string,
152 boost::container::flat_map<std::string, std::vector<std::string>>>&
153 groupEventMatch,
154 boost::container::flat_map<
155 std::string,
156 boost::container::flat_map<std::string, std::vector<std::string>>>&
157 groupEventPathList)
158{
159 for (const auto& match : groupEventMatch)
160 {
161 const std::string& groupEventName = match.first;
162 const boost::container::flat_map<std::string, std::vector<std::string>>
163 events = match.second;
164 boost::container::flat_map<std::string, std::vector<std::string>>
165 pathList;
166 for (const auto& match : events)
167 {
168 const std::string& eventName = match.first;
169 const std::vector<std::string>& eventAttrs = match.second;
170 for (const auto& eventAttr : eventAttrs)
171 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700172 std::string eventPath = directory;
173 eventPath += "/";
174 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800175 std::ifstream eventFile(eventPath);
176 if (!eventFile.good())
177 {
178 continue;
179 }
180
181 pathList[eventName].push_back(eventPath);
182 }
183 }
184 groupEventPathList[groupEventName] = pathList;
185 }
186}
187
Cheng C Yang58b2b532019-05-31 00:19:45 +0800188// Function checkEventLimits will check all the psu related xxx_input attributes
189// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
190// xxx_min_alarm exist, then store the existing paths of the alarm attributes
191// to eventPathList.
192void checkEventLimits(
193 const std::string& sensorPathStr,
194 const boost::container::flat_map<std::string, std::vector<std::string>>&
195 limitEventMatch,
196 boost::container::flat_map<std::string, std::vector<std::string>>&
197 eventPathList)
198{
Lei YUa2c7cea2020-12-23 14:07:28 +0800199 auto attributePartPos = sensorPathStr.find_last_of('_');
200 if (attributePartPos == std::string::npos)
201 {
202 // There is no '_' in the string, skip it
203 return;
204 }
205 auto attributePart =
206 std::string_view(sensorPathStr).substr(attributePartPos + 1);
207 if (attributePart != "input")
208 {
209 // If the sensor is not xxx_input, skip it
210 return;
211 }
212
213 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800214 for (const auto& limitMatch : limitEventMatch)
215 {
216 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
217 const std::string& eventName = limitMatch.first;
218 for (const auto& limitEventAttr : limitEventAttrs)
219 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800220 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800221 std::ifstream eventFile(limitEventPath);
222 if (!eventFile.good())
223 {
224 continue;
225 }
226 eventPathList[eventName].push_back(limitEventPath);
227 }
228 }
229}
Cheng C Yang916360b2019-05-07 18:47:16 +0800230
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530231static void
232 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
233 const std::string& interfacePath,
234 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
235 sdbusplus::asio::object_server& objectServer,
236 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800237{
Zev Weiss741f26e2022-08-12 18:21:02 -0700238 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800239 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700240 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800241 {
242 continue;
243 }
244
245 const std::string& sensorPathStr = sensorPath.string();
246 const std::string& pwmPathStr =
247 boost::replace_all_copy(sensorPathStr, "input", "target");
248 std::ifstream pwmFile(pwmPathStr);
249 if (!pwmFile.good())
250 {
251 continue;
252 }
253
254 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
255 if (findPWMSensor != pwmSensors.end())
256 {
257 continue;
258 }
259
Zev Weissd8c293a2022-08-15 18:58:41 -0700260 std::string name = "Pwm_";
261 name += psuName;
262 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700263 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700264
265 std::string objPath = interfacePath;
266 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700267 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700268
Cheng C Yang916360b2019-05-07 18:47:16 +0800269 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700270 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800271 }
272}
273
Zhikui Ren23c96e72020-11-05 22:32:28 -0800274static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800275 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800276 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
277 const ManagedObjectType& sensorConfigs,
278 const std::shared_ptr<boost::container::flat_set<std::string>>&
279 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800280{
Josh Lehan49cfba92019-10-08 16:50:42 -0700281 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800282 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800283
284 std::vector<fs::path> pmbusPaths;
285 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
286 {
287 std::cerr << "No PSU sensors in system\n";
288 return;
289 }
290
291 boost::container::flat_set<std::string> directories;
292 for (const auto& pmbusPath : pmbusPaths)
293 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800294 boost::container::flat_map<std::string, std::vector<std::string>>
295 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800296 boost::container::flat_map<
297 std::string,
298 boost::container::flat_map<std::string, std::vector<std::string>>>
299 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800300
301 std::ifstream nameFile(pmbusPath);
302 if (!nameFile.good())
303 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700304 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800305 continue;
306 }
307
308 std::string pmbusName;
309 std::getline(nameFile, pmbusName);
310 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700311
Matt Simmering786efb82023-01-18 14:09:21 -0800312 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800313 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700314 // To avoid this error message, add your driver name to
315 // the pmbusNames vector at the top of this file.
316 std::cerr << "Driver name " << pmbusName
317 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800318 continue;
319 }
320
Cheng C Yang209ec562019-03-12 16:37:44 +0800321 auto directory = pmbusPath.parent_path();
322
323 auto ret = directories.insert(directory.string());
324 if (!ret.second)
325 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700326 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800327 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800328 }
329
James Feistb6c0b912019-07-09 12:21:44 -0700330 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800331 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700332 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800333 if (findHyphen == std::string::npos)
334 {
335 std::cerr << "found bad device" << deviceName << "\n";
336 continue;
337 }
338 std::string busStr = deviceName.substr(0, findHyphen);
339 std::string addrStr = deviceName.substr(findHyphen + 1);
340
341 size_t bus = 0;
342 size_t addr = 0;
343
344 try
345 {
346 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700347 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800348 }
Patrick Williams26601e82021-10-06 12:43:25 -0500349 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800350 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700351 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
352 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800353 continue;
354 }
355
Zev Weisse8b97ee2022-08-12 15:23:51 -0700356 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800357 const SensorData* sensorData = nullptr;
358 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800359 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800360 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800361
Zev Weiss741f26e2022-08-12 18:21:02 -0700362 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800363 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700364 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800365 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800366 {
Zev Weiss054aad82022-08-18 01:37:34 -0700367 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800368 if (sensorBase != sensorData->end())
369 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700370 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800371 sensorType = type;
372 break;
373 }
374 }
375 if (baseConfig == nullptr)
376 {
377 std::cerr << "error finding base configuration for "
378 << deviceName << "\n";
379 continue;
380 }
381
Zev Weisse8b97ee2022-08-12 15:23:51 -0700382 auto configBus = baseConfig->find("Bus");
383 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800384
Zev Weisse8b97ee2022-08-12 15:23:51 -0700385 if (configBus == baseConfig->end() ||
386 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800387 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800388 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800389 continue;
390 }
391
Matt Simmering6747eba2023-01-18 16:03:55 -0800392 const uint64_t* confBus =
393 std::get_if<uint64_t>(&(configBus->second));
394 const uint64_t* confAddr =
395 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800396 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800397 {
398 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700399 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800400 continue;
401 }
402
403 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800404 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700405 std::cerr << "Configuration skipping " << *confBus << "-"
406 << *confAddr << " because not " << bus << "-" << addr
407 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800408 continue;
409 }
410
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800411 std::vector<thresholds::Threshold> confThresholds;
412 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
413 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800414 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800415 }
416 thresholdConfSize = confThresholds.size();
417
Zev Weiss741f26e2022-08-12 18:21:02 -0700418 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800419 break;
420 }
421 if (interfacePath == nullptr)
422 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700423 // To avoid this error message, add your export map entry,
424 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800425 std::cerr << "failed to find match for " << deviceName << "\n";
426 continue;
427 }
428
Zev Weisse8b97ee2022-08-12 15:23:51 -0700429 auto findPSUName = baseConfig->find("Name");
430 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800431 {
432 std::cerr << "could not determine configuration name for "
433 << deviceName << "\n";
434 continue;
435 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800436 const std::string* psuName =
437 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800438 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800439 {
440 std::cerr << "Cannot find psu name, invalid configuration\n";
441 continue;
442 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800443
444 // on rescans, only update sensors we were signaled by
445 if (!firstScan)
446 {
Zhikui Renda98f092021-11-01 09:41:08 -0700447 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800448 auto it =
449 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
450 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800451 return s.ends_with(psuNameStr);
452 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800453
454 if (it == sensorsChanged->end())
455 {
456 continue;
457 }
458 sensorsChanged->erase(it);
459 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800460 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800461 checkGroupEvent(directory.string(), groupEventMatch,
462 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800463
Zev Weisse8b97ee2022-08-12 15:23:51 -0700464 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000465
Vijay Khemka996bad12019-05-28 15:15:16 -0700466 /* Check if there are more sensors in the same interface */
467 int i = 1;
468 std::vector<std::string> psuNames;
469 do
470 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700471 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700472 psuNames.push_back(
473 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700474 findPSUName = baseConfig->find("Name" + std::to_string(i++));
475 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700476
Cheng C Yange50345b2019-04-02 17:26:15 +0800477 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700478 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800479 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800480 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800481 continue;
482 }
483
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530484 /* read max value in sysfs for in, curr, power, temp, ... */
485 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
486 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700487 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530488 {
489 std::cerr << "No max name in PSU \n";
490 }
491 }
492
Zev Weiss8569bf22022-10-11 15:37:44 -0700493 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800494
Vijay Khemka996bad12019-05-28 15:15:16 -0700495 /* Find array of labels to be exposed if it is defined in config */
496 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700497 auto findLabelObj = baseConfig->find("Labels");
498 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700499 {
500 findLabels =
501 std::get<std::vector<std::string>>(findLabelObj->second);
502 }
503
Jason Ling5747fab2019-10-02 16:46:23 -0700504 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
505 std::smatch matches;
506
Cheng C Yange50345b2019-04-02 17:26:15 +0800507 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800508 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530509 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800510 std::string labelHead;
511 std::string sensorPathStr = sensorPath.string();
512 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700513 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700514 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
515 {
Josh Lehan06494452019-10-31 09:49:16 -0700516 // hwmon *_input filename without number:
517 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700518 sensorNameSubStr = matches[1];
519 }
520 else
521 {
Josh Lehan06494452019-10-31 09:49:16 -0700522 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700523 << sensorNameStr;
524 continue;
525 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800526
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530527 std::string labelPath;
528
529 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700530 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530531 if (pos != std::string::npos)
532 {
533
534 std::string sensorPathStrMax = sensorPathStr.substr(pos);
Ed Tanous2049bd22022-07-09 07:20:26 -0700535 if (sensorPathStrMax == "_max")
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530536 {
537 labelPath =
538 boost::replace_all_copy(sensorPathStr, "max", "label");
539 maxLabel = true;
540 }
541 else
542 {
543 labelPath = boost::replace_all_copy(sensorPathStr, "input",
544 "label");
545 maxLabel = false;
546 }
547 }
548 else
549 {
550 continue;
551 }
552
Cheng C Yangecba9de2019-09-12 23:41:50 +0800553 std::ifstream labelFile(labelPath);
554 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800555 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700556 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800557 {
558 std::cerr << "Input file " << sensorPath
559 << " has no corresponding label file\n";
560 }
Josh Lehan06494452019-10-31 09:49:16 -0700561 // hwmon *_input filename with number:
562 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700563 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800564 }
565 else
566 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800567 std::string label;
568 std::getline(labelFile, label);
569 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800570 auto findSensor = sensors.find(label);
571 if (findSensor != sensors.end())
572 {
573 continue;
574 }
575
Josh Lehan06494452019-10-31 09:49:16 -0700576 // hwmon corresponding *_label file contents:
577 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700578 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800579 }
580
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530581 /* append "max" for labelMatch */
582 if (maxLabel)
583 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700584 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530585 }
586
Ed Tanous8a57ec02020-10-09 12:46:52 -0700587 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700588 {
589 std::cerr << "Sensor type=\"" << sensorNameSubStr
590 << "\" label=\"" << labelHead << "\"\n";
591 }
592
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530593 checkPWMSensor(sensorPath, labelHead, *interfacePath,
594 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800595
Vijay Khemka996bad12019-05-28 15:15:16 -0700596 if (!findLabels.empty())
597 {
598 /* Check if this labelHead is enabled in config file */
599 if (std::find(findLabels.begin(), findLabels.end(),
600 labelHead) == findLabels.end())
601 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700602 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800603 {
604 std::cerr << "could not find " << labelHead
605 << " in the Labels list\n";
606 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700607 continue;
608 }
609 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800610
Cheng C Yange50345b2019-04-02 17:26:15 +0800611 auto findProperty = labelMatch.find(labelHead);
612 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800613 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700614 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700615 {
616 std::cerr << "Could not find matching default property for "
617 << labelHead << "\n";
618 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800619 continue;
620 }
621
Josh Lehan74d9bd92019-10-31 08:51:58 -0700622 // Protect the hardcoded labelMatch list from changes,
623 // by making a copy and modifying that instead.
624 // Avoid bleedthrough of one device's customizations to
625 // the next device, as each should be independently customizable.
626 psuProperties.push_back(findProperty->second);
627 auto psuProperty = psuProperties.rbegin();
628
629 // Use label head as prefix for reading from config file,
630 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
631 std::string keyName = labelHead + "_Name";
632 std::string keyScale = labelHead + "_Scale";
633 std::string keyMin = labelHead + "_Min";
634 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800635 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800636 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700637
638 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700639 auto findCustomName = baseConfig->find(keyName);
640 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700641 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700642 try
643 {
644 psuProperty->labelTypeName = std::visit(
645 VariantToStringVisitor(), findCustomName->second);
646 }
Patrick Williams26601e82021-10-06 12:43:25 -0500647 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700648 {
649 std::cerr << "Unable to parse " << keyName << "\n";
650 continue;
651 }
652
653 // All strings are valid, including empty string
654 customizedName = true;
655 }
656
657 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700658 auto findCustomScale = baseConfig->find(keyScale);
659 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700660 {
661 try
662 {
663 psuProperty->sensorScaleFactor = std::visit(
664 VariantToUnsignedIntVisitor(), findCustomScale->second);
665 }
Patrick Williams26601e82021-10-06 12:43:25 -0500666 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700667 {
668 std::cerr << "Unable to parse " << keyScale << "\n";
669 continue;
670 }
671
672 // Avoid later division by zero
673 if (psuProperty->sensorScaleFactor > 0)
674 {
675 customizedScale = true;
676 }
677 else
678 {
679 std::cerr << "Unable to accept " << keyScale << "\n";
680 continue;
681 }
682 }
683
Zev Weisse8b97ee2022-08-12 15:23:51 -0700684 auto findCustomMin = baseConfig->find(keyMin);
685 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700686 {
687 try
688 {
689 psuProperty->minReading = std::visit(
690 VariantToDoubleVisitor(), findCustomMin->second);
691 }
Patrick Williams26601e82021-10-06 12:43:25 -0500692 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700693 {
694 std::cerr << "Unable to parse " << keyMin << "\n";
695 continue;
696 }
697 }
698
Zev Weisse8b97ee2022-08-12 15:23:51 -0700699 auto findCustomMax = baseConfig->find(keyMax);
700 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700701 {
702 try
703 {
704 psuProperty->maxReading = std::visit(
705 VariantToDoubleVisitor(), findCustomMax->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 " << keyMax << "\n";
710 continue;
711 }
712 }
713
Zev Weisse8b97ee2022-08-12 15:23:51 -0700714 auto findCustomOffset = baseConfig->find(keyOffset);
715 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800716 {
717 try
718 {
719 psuProperty->sensorOffset = std::visit(
720 VariantToDoubleVisitor(), findCustomOffset->second);
721 }
Patrick Williams26601e82021-10-06 12:43:25 -0500722 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800723 {
724 std::cerr << "Unable to parse " << keyOffset << "\n";
725 continue;
726 }
727 }
728
Lotus Xucb5af732021-09-10 15:18:50 +0800729 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700730 auto findPowerState = baseConfig->find(keyPowerState);
731 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800732 {
733 std::string powerState = std::visit(VariantToStringVisitor(),
734 findPowerState->second);
735 setReadState(powerState, readState);
736 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700737 if (!(psuProperty->minReading < psuProperty->maxReading))
738 {
739 std::cerr << "Min must be less than Max\n";
740 continue;
741 }
742
743 // If the sensor name is being customized by config file,
744 // then prefix/suffix composition becomes not necessary,
745 // and in fact not wanted, because it gets in the way.
746 std::string psuNameFromIndex;
747 if (!customizedName)
748 {
749 /* Find out sensor name index for this label */
750 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500751 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700752 if (std::regex_search(labelHead, matches, rgx))
753 {
754 nameIndex = std::stoi(matches[1]);
755
756 // Decrement to preserve alignment, because hwmon
757 // human-readable filenames and labels use 1-based
758 // numbering, but the "Name", "Name1", "Name2", etc. naming
759 // convention (the psuNames vector) uses 0-based numbering.
760 if (nameIndex > 0)
761 {
762 --nameIndex;
763 }
764 }
765 else
766 {
767 nameIndex = 0;
768 }
769
770 if (psuNames.size() <= nameIndex)
771 {
772 std::cerr << "Could not pair " << labelHead
773 << " with a Name field\n";
774 continue;
775 }
776
777 psuNameFromIndex = psuNames[nameIndex];
778
Ed Tanous8a57ec02020-10-09 12:46:52 -0700779 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700780 {
781 std::cerr << "Sensor label head " << labelHead
782 << " paired with " << psuNameFromIndex
783 << " at index " << nameIndex << "\n";
784 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700785 }
786
Cheng C Yang58b2b532019-05-31 00:19:45 +0800787 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
788
Josh Lehan74d9bd92019-10-31 08:51:58 -0700789 // Similarly, if sensor scaling factor is being customized,
790 // then the below power-of-10 constraint becomes unnecessary,
791 // as config should be able to specify an arbitrary divisor.
792 unsigned int factor = psuProperty->sensorScaleFactor;
793 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700794 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700795 // Preserve existing usage of hardcoded labelMatch table below
796 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700797
Josh Lehan74d9bd92019-10-31 08:51:58 -0700798 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700799 char firstChar =
800 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700801 std::string strScaleFactor =
802 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
803
804 // Preserve existing configs by accepting earlier syntax,
805 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700806 auto findScaleFactor = baseConfig->find(strScaleFactor);
807 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700808 {
809 factor = std::visit(VariantToIntVisitor(),
810 findScaleFactor->second);
811 }
812
Ed Tanous8a57ec02020-10-09 12:46:52 -0700813 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700814 {
815 std::cerr << "Sensor scaling factor " << factor
816 << " string " << strScaleFactor << "\n";
817 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700818 }
819
Vijay Khemka996bad12019-05-28 15:15:16 -0700820 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530821 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
822 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800823 {
James Feist17ab6e02019-06-25 12:28:13 -0700824 std::cerr << "error populating thresholds for "
825 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800826 }
827
Zev Weiss6b6891c2021-04-22 02:46:21 -0500828 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
829 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800830 {
Jason Ling5747fab2019-10-02 16:46:23 -0700831 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700832 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800833 continue;
834 }
835
Ed Tanous8a57ec02020-10-09 12:46:52 -0700836 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700837 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700838 std::cerr << "Sensor properties: Name \""
839 << psuProperty->labelTypeName << "\" Scale "
840 << psuProperty->sensorScaleFactor << " Min "
841 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800842 << psuProperty->maxReading << " Offset "
843 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700844 }
845
846 std::string sensorName = psuProperty->labelTypeName;
847 if (customizedName)
848 {
849 if (sensorName.empty())
850 {
851 // Allow selective disabling of an individual sensor,
852 // by customizing its name to an empty string.
853 std::cerr << "Sensor disabled, empty string\n";
854 continue;
855 }
856 }
857 else
858 {
859 // Sensor name not customized, do prefix/suffix composition,
860 // preserving default behavior by using psuNameFromIndex.
861 sensorName =
862 psuNameFromIndex + " " + psuProperty->labelTypeName;
863 }
864
Ed Tanous8a57ec02020-10-09 12:46:52 -0700865 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700866 {
867 std::cerr << "Sensor name \"" << sensorName << "\" path \""
868 << sensorPathStr << "\" type \"" << sensorType
869 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700870 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800871 // destruct existing one first if already created
872 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800873 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800874 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800875 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000876 readState, findSensorUnit->second, factor,
877 psuProperty->maxReading, psuProperty->minReading,
878 psuProperty->sensorOffset, labelHead, thresholdConfSize,
879 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800880 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700881 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700882 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700883 {
884 std::cerr << "Created " << numCreated << " sensors so far\n";
885 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800886 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800887
888 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800889 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800890 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000891 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
892 *psuName, readState,
893 eventPathList, groupEventPathList,
894 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800895 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700896
Ed Tanous8a57ec02020-10-09 12:46:52 -0700897 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700898 {
899 std::cerr << "Created total of " << numCreated << " sensors\n";
900 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800901}
Cheng C Yang209ec562019-03-12 16:37:44 +0800902
Zhikui Ren23c96e72020-11-05 22:32:28 -0800903void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800904 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800905 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
906 const std::shared_ptr<boost::container::flat_set<std::string>>&
907 sensorsChanged)
908{
909 auto getter = std::make_shared<GetSensorConfiguration>(
910 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
911 const ManagedObjectType& sensorConfigs) {
912 createSensorsCallback(io, objectServer, dbusConnection,
913 sensorConfigs, sensorsChanged);
914 });
Matt Simmering786efb82023-01-18 14:09:21 -0800915 std::vector<std::string> types(sensorTypes.size());
916 for (const auto& [type, dt] : sensorTypes)
917 {
918 types.push_back(type);
919 }
920 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800921}
922
Cheng C Yang916360b2019-05-07 18:47:16 +0800923void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800924{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500925 sensorTable = {{"power", sensor_paths::unitWatts},
926 {"curr", sensor_paths::unitAmperes},
927 {"temp", sensor_paths::unitDegreesC},
928 {"in", sensor_paths::unitVolts},
929 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800930
Jeff Line41d52f2021-04-07 19:38:51 +0800931 labelMatch = {
932 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800933 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
934 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800935 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
936 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
937 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
938 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800939 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
940 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
941 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800942 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
943 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
944 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
945 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
946 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
947 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
948 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
949 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
950 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
951 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
952 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
953 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
954 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
955 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
956 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
957 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
958 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
959 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
960 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
961 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
962 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
963 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
964 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
965 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
966 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
967 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
968 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
969 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
970 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
971 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
972 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
973 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
974 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
975 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
976 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
977 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +0800978 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800979 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +0800980 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
981 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
982 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
985 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800986 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800987 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
988 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800989 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
990 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
991 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
992 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
993 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
994 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
995 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
996 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
997 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
998 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
999 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1000 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1001 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1002 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1003 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001004 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1005 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1006 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001007 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1008 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1009 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1010 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1011 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1012 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1013 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1014 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1015 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001016 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1017 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1018 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001019
Matt Simmering6747eba2023-01-18 16:03:55 -08001020 pwmTable = {{"fan1", "Fan_1"},
1021 {"fan2", "Fan_2"},
1022 {"fan3", "Fan_3"},
1023 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001024
1025 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1026 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1027
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001028 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1029 {"Failure", {"in2_alarm"}},
1030 {"ACLost", {"in1_beep"}},
1031 {"ConfigureError", {"in1_fault"}}};
1032
1033 groupEventMatch = {{"FanFault",
1034 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001035 {"fan2", {"fan2_alarm", "fan2_fault"}},
1036 {"fan3", {"fan3_alarm", "fan3_fault"}},
1037 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001038}
1039
James Feistb6c0b912019-07-09 12:21:44 -07001040int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001041{
Ed Tanous1f978632023-02-28 18:16:39 -08001042 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001043 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1044
Ed Tanous14ed5e92022-07-12 15:50:23 -07001045 sdbusplus::asio::object_server objectServer(systemBus, true);
1046 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001047 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001048 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001049 auto sensorsChanged =
1050 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001051
Cheng C Yang916360b2019-05-07 18:47:16 +08001052 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001053
Matt Simmering786efb82023-01-18 14:09:21 -08001054 boost::asio::post(
1055 io, [&]() { createSensors(io, objectServer, systemBus, nullptr); });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001056 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001057 std::function<void(sdbusplus::message_t&)> eventHandler =
1058 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001059 if (message.is_method_error())
1060 {
1061 std::cerr << "callback method error\n";
1062 return;
1063 }
1064 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001065 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001066 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1067 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001068 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001069 return;
1070 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001071 if (ec)
1072 {
1073 std::cerr << "timer error\n";
1074 }
1075 createSensors(io, objectServer, systemBus, sensorsChanged);
1076 });
1077 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001078
Zev Weiss214d9712022-08-12 12:54:31 -07001079 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1080 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +08001081 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001082 io.run();
1083}