blob: e4855980fede0f12f7c076351931b4be33fc662b [file] [log] [blame]
Cheng C Yang209ec562019-03-12 16:37:44 +08001/*
2// Copyright (c) 2019 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
Josh Lehan0830c7b2019-10-08 16:35:09 -070016
Ed Tanous8a57ec02020-10-09 12:46:52 -070017#include <PSUEvent.hpp>
18#include <PSUSensor.hpp>
19#include <Utils.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080020#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070021#include <boost/container/flat_map.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080022#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25#include <sdbusplus/bus/match.hpp>
26
27#include <array>
Josh Lehan74d9bd92019-10-31 08:51:58 -070028#include <cmath>
James Feist24f02f22019-04-15 11:05:39 -070029#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080030#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <functional>
Cheng C Yang58b2b532019-05-31 00:19:45 +080032#include <iostream>
Cheng C Yang209ec562019-03-12 16:37:44 +080033#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <string>
Lei YUa2c7cea2020-12-23 14:07:28 +080035#include <string_view>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <utility>
37#include <variant>
38#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080039
Ed Tanous8a57ec02020-10-09 12:46:52 -070040static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070041
Brandon Kim66558232021-11-09 16:53:08 -080042static constexpr auto sensorTypes{std::to_array<const char*>(
43 {"xyz.openbmc_project.Configuration.ADM1266",
44 "xyz.openbmc_project.Configuration.ADM1272",
45 "xyz.openbmc_project.Configuration.ADM1275",
46 "xyz.openbmc_project.Configuration.ADM1278",
Zhikui Ren842e5432022-03-09 16:18:24 -080047 "xyz.openbmc_project.Configuration.ADM1293",
Tim Chao6f379ce2022-02-24 11:08:09 +080048 "xyz.openbmc_project.Configuration.ADS7830",
linchuyuan5cf66df2021-12-21 17:38:55 -080049 "xyz.openbmc_project.Configuration.BMR490",
Brandon Kim66558232021-11-09 16:53:08 -080050 "xyz.openbmc_project.Configuration.DPS800",
51 "xyz.openbmc_project.Configuration.INA219",
52 "xyz.openbmc_project.Configuration.INA230",
53 "xyz.openbmc_project.Configuration.IPSPS",
54 "xyz.openbmc_project.Configuration.ISL68137",
55 "xyz.openbmc_project.Configuration.ISL68220",
56 "xyz.openbmc_project.Configuration.ISL68223",
Khang Kieu7d6b77d2022-02-10 10:43:36 +000057 "xyz.openbmc_project.Configuration.ISL69225",
Brandon Kim66558232021-11-09 16:53:08 -080058 "xyz.openbmc_project.Configuration.ISL69243",
59 "xyz.openbmc_project.Configuration.ISL69260",
60 "xyz.openbmc_project.Configuration.LM25066",
61 "xyz.openbmc_project.Configuration.MAX16601",
62 "xyz.openbmc_project.Configuration.MAX20710",
63 "xyz.openbmc_project.Configuration.MAX20730",
64 "xyz.openbmc_project.Configuration.MAX20734",
65 "xyz.openbmc_project.Configuration.MAX20796",
66 "xyz.openbmc_project.Configuration.MAX34451",
Shamim Alidccd1d42022-06-08 22:31:35 +053067 "xyz.openbmc_project.Configuration.MP2971",
68 "xyz.openbmc_project.Configuration.MP2973",
Howard Chiub58ac3a2021-12-07 17:12:56 +080069 "xyz.openbmc_project.Configuration.MP5023",
Brandon Kim66558232021-11-09 16:53:08 -080070 "xyz.openbmc_project.Configuration.pmbus",
71 "xyz.openbmc_project.Configuration.PXE1610",
72 "xyz.openbmc_project.Configuration.RAA228000",
73 "xyz.openbmc_project.Configuration.RAA228228",
Tom Tung75da5152021-11-24 09:38:07 +080074 "xyz.openbmc_project.Configuration.RAA228620",
75 "xyz.openbmc_project.Configuration.RAA229001",
Brandon Kim66558232021-11-09 16:53:08 -080076 "xyz.openbmc_project.Configuration.RAA229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -080077 "xyz.openbmc_project.Configuration.RAA229126",
Brandon Kim66558232021-11-09 16:53:08 -080078 "xyz.openbmc_project.Configuration.TPS546D24",
79 "xyz.openbmc_project.Configuration.XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080080
Ed Tanousa2df7862021-12-07 16:30:27 -080081// clang-format off
82static constexpr auto pmbusNames{std::to_array<const char*>({
83 "adm1266",
84 "adm1272",
85 "adm1275",
86 "adm1278",
Zhikui Ren842e5432022-03-09 16:18:24 -080087 "adm1293",
Tim Chao6f379ce2022-02-24 11:08:09 +080088 "ads7830",
linchuyuan5cf66df2021-12-21 17:38:55 -080089 "bmr490",
Ed Tanousa2df7862021-12-07 16:30:27 -080090 "dps800",
91 "ina219",
92 "ina230",
93 "ipsps1",
94 "isl68137",
95 "isl68220",
96 "isl68223",
Khang Kieu51ad6672022-02-09 01:26:25 +000097 "isl69225",
Ed Tanousa2df7862021-12-07 16:30:27 -080098 "isl69243",
99 "isl69260",
100 "lm25066",
101 "max16601",
102 "max20710",
103 "max20730",
104 "max20734",
105 "max20796",
106 "max34451",
Shamim Alidccd1d42022-06-08 22:31:35 +0530107 "mp2971",
108 "mp2973",
Howard Chiub58ac3a2021-12-07 17:12:56 +0800109 "mp5023",
Ed Tanousa2df7862021-12-07 16:30:27 -0800110 "pmbus",
111 "pxe1610",
112 "raa228000",
113 "raa228228",
Tom Tung75da5152021-11-24 09:38:07 +0800114 "raa228620",
115 "raa229001",
Ed Tanousa2df7862021-12-07 16:30:27 -0800116 "raa229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800117 "raa229126",
Ed Tanousa2df7862021-12-07 16:30:27 -0800118 "tps546d24",
119 "xdpe12284"
120})};
121//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700122
Cheng C Yang209ec562019-03-12 16:37:44 +0800123namespace fs = std::filesystem;
124
Yong Libf8b1da2020-04-15 16:32:50 +0800125static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800126 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800127static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
128 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800129static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
130 pwmSensors;
131static boost::container::flat_map<std::string, std::string> sensorTable;
132static boost::container::flat_map<std::string, PSUProperty> labelMatch;
133static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800134static boost::container::flat_map<std::string, std::vector<std::string>>
135 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800136static boost::container::flat_map<
137 std::string,
138 boost::container::flat_map<std::string, std::vector<std::string>>>
139 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800140static boost::container::flat_map<std::string, std::vector<std::string>>
141 limitEventMatch;
142
Josh Lehan74d9bd92019-10-31 08:51:58 -0700143static std::vector<PSUProperty> psuProperties;
144
Cheng C Yang58b2b532019-05-31 00:19:45 +0800145// Function CheckEvent will check each attribute from eventMatch table in the
146// sysfs. If the attributes exists in sysfs, then store the complete path
147// of the attribute into eventPathList.
148void checkEvent(
149 const std::string& directory,
150 const boost::container::flat_map<std::string, std::vector<std::string>>&
151 eventMatch,
152 boost::container::flat_map<std::string, std::vector<std::string>>&
153 eventPathList)
154{
155 for (const auto& match : eventMatch)
156 {
157 const std::vector<std::string>& eventAttrs = match.second;
158 const std::string& eventName = match.first;
159 for (const auto& eventAttr : eventAttrs)
160 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700161 std::string eventPath = directory;
162 eventPath += "/";
163 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800164
165 std::ifstream eventFile(eventPath);
166 if (!eventFile.good())
167 {
168 continue;
169 }
170
171 eventPathList[eventName].push_back(eventPath);
172 }
173 }
174}
175
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800176// Check Group Events which contains more than one targets in each combine
177// events.
178void checkGroupEvent(
179 const std::string& directory,
180 const boost::container::flat_map<
181 std::string,
182 boost::container::flat_map<std::string, std::vector<std::string>>>&
183 groupEventMatch,
184 boost::container::flat_map<
185 std::string,
186 boost::container::flat_map<std::string, std::vector<std::string>>>&
187 groupEventPathList)
188{
189 for (const auto& match : groupEventMatch)
190 {
191 const std::string& groupEventName = match.first;
192 const boost::container::flat_map<std::string, std::vector<std::string>>
193 events = match.second;
194 boost::container::flat_map<std::string, std::vector<std::string>>
195 pathList;
196 for (const auto& match : events)
197 {
198 const std::string& eventName = match.first;
199 const std::vector<std::string>& eventAttrs = match.second;
200 for (const auto& eventAttr : eventAttrs)
201 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700202 std::string eventPath = directory;
203 eventPath += "/";
204 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800205 std::ifstream eventFile(eventPath);
206 if (!eventFile.good())
207 {
208 continue;
209 }
210
211 pathList[eventName].push_back(eventPath);
212 }
213 }
214 groupEventPathList[groupEventName] = pathList;
215 }
216}
217
Cheng C Yang58b2b532019-05-31 00:19:45 +0800218// Function checkEventLimits will check all the psu related xxx_input attributes
219// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
220// xxx_min_alarm exist, then store the existing paths of the alarm attributes
221// to eventPathList.
222void checkEventLimits(
223 const std::string& sensorPathStr,
224 const boost::container::flat_map<std::string, std::vector<std::string>>&
225 limitEventMatch,
226 boost::container::flat_map<std::string, std::vector<std::string>>&
227 eventPathList)
228{
Lei YUa2c7cea2020-12-23 14:07:28 +0800229 auto attributePartPos = sensorPathStr.find_last_of('_');
230 if (attributePartPos == std::string::npos)
231 {
232 // There is no '_' in the string, skip it
233 return;
234 }
235 auto attributePart =
236 std::string_view(sensorPathStr).substr(attributePartPos + 1);
237 if (attributePart != "input")
238 {
239 // If the sensor is not xxx_input, skip it
240 return;
241 }
242
243 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800244 for (const auto& limitMatch : limitEventMatch)
245 {
246 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
247 const std::string& eventName = limitMatch.first;
248 for (const auto& limitEventAttr : limitEventAttrs)
249 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800250 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800251 std::ifstream eventFile(limitEventPath);
252 if (!eventFile.good())
253 {
254 continue;
255 }
256 eventPathList[eventName].push_back(limitEventPath);
257 }
258 }
259}
Cheng C Yang916360b2019-05-07 18:47:16 +0800260
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530261static void
262 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
263 const std::string& interfacePath,
264 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
265 sdbusplus::asio::object_server& objectServer,
266 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800267{
Zev Weiss741f26e2022-08-12 18:21:02 -0700268 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800269 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700270 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800271 {
272 continue;
273 }
274
275 const std::string& sensorPathStr = sensorPath.string();
276 const std::string& pwmPathStr =
277 boost::replace_all_copy(sensorPathStr, "input", "target");
278 std::ifstream pwmFile(pwmPathStr);
279 if (!pwmFile.good())
280 {
281 continue;
282 }
283
284 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
285 if (findPWMSensor != pwmSensors.end())
286 {
287 continue;
288 }
289
Zev Weissd8c293a2022-08-15 18:58:41 -0700290 std::string name = "Pwm_";
291 name += psuName;
292 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700293 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700294
295 std::string objPath = interfacePath;
296 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700297 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700298
Cheng C Yang916360b2019-05-07 18:47:16 +0800299 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700300 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800301 }
302}
303
Zhikui Ren23c96e72020-11-05 22:32:28 -0800304static void createSensorsCallback(
305 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
306 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
307 const ManagedObjectType& sensorConfigs,
308 const std::shared_ptr<boost::container::flat_set<std::string>>&
309 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800310{
Josh Lehan49cfba92019-10-08 16:50:42 -0700311 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800312 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800313
314 std::vector<fs::path> pmbusPaths;
315 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
316 {
317 std::cerr << "No PSU sensors in system\n";
318 return;
319 }
320
321 boost::container::flat_set<std::string> directories;
322 for (const auto& pmbusPath : pmbusPaths)
323 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800324 boost::container::flat_map<std::string, std::vector<std::string>>
325 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800326 boost::container::flat_map<
327 std::string,
328 boost::container::flat_map<std::string, std::vector<std::string>>>
329 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800330
331 std::ifstream nameFile(pmbusPath);
332 if (!nameFile.good())
333 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700334 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800335 continue;
336 }
337
338 std::string pmbusName;
339 std::getline(nameFile, pmbusName);
340 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700341
342 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
343 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800344 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700345 // To avoid this error message, add your driver name to
346 // the pmbusNames vector at the top of this file.
347 std::cerr << "Driver name " << pmbusName
348 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800349 continue;
350 }
351
Cheng C Yang209ec562019-03-12 16:37:44 +0800352 auto directory = pmbusPath.parent_path();
353
354 auto ret = directories.insert(directory.string());
355 if (!ret.second)
356 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700357 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800358 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800359 }
360
James Feistb6c0b912019-07-09 12:21:44 -0700361 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800362 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700363 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800364 if (findHyphen == std::string::npos)
365 {
366 std::cerr << "found bad device" << deviceName << "\n";
367 continue;
368 }
369 std::string busStr = deviceName.substr(0, findHyphen);
370 std::string addrStr = deviceName.substr(findHyphen + 1);
371
372 size_t bus = 0;
373 size_t addr = 0;
374
375 try
376 {
377 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700378 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800379 }
Patrick Williams26601e82021-10-06 12:43:25 -0500380 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800381 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700382 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
383 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800384 continue;
385 }
386
Zev Weisse8b97ee2022-08-12 15:23:51 -0700387 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800388 const SensorData* sensorData = nullptr;
389 const std::string* interfacePath = nullptr;
390 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800391 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800392
Zev Weiss741f26e2022-08-12 18:21:02 -0700393 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800394 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700395 sensorData = &cfgData;
Cheng C Yang209ec562019-03-12 16:37:44 +0800396 for (const char* type : sensorTypes)
397 {
398 auto sensorBase = sensorData->find(type);
399 if (sensorBase != sensorData->end())
400 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700401 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800402 sensorType = type;
403 break;
404 }
405 }
406 if (baseConfig == nullptr)
407 {
408 std::cerr << "error finding base configuration for "
409 << deviceName << "\n";
410 continue;
411 }
412
Zev Weisse8b97ee2022-08-12 15:23:51 -0700413 auto configBus = baseConfig->find("Bus");
414 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800415
Zev Weisse8b97ee2022-08-12 15:23:51 -0700416 if (configBus == baseConfig->end() ||
417 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800418 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800419 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800420 continue;
421 }
422
Ed Tanousa771f6a2022-01-14 09:36:51 -0800423 const uint64_t* confBus = std::get_if<uint64_t>(&(configBus->second));
424 const uint64_t* confAddr = std::get_if<uint64_t>(&(configAddress->second));
425 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800426 {
427 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700428 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800429 continue;
430 }
431
432 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800433 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700434 std::cerr << "Configuration skipping " << *confBus << "-"
435 << *confAddr << " because not " << bus << "-" << addr
436 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800437 continue;
438 }
439
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800440 std::vector<thresholds::Threshold> confThresholds;
441 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
442 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800443 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800444 }
445 thresholdConfSize = confThresholds.size();
446
Zev Weiss741f26e2022-08-12 18:21:02 -0700447 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800448 break;
449 }
450 if (interfacePath == nullptr)
451 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700452 // To avoid this error message, add your export map entry,
453 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800454 std::cerr << "failed to find match for " << deviceName << "\n";
455 continue;
456 }
457
Zev Weisse8b97ee2022-08-12 15:23:51 -0700458 auto findPSUName = baseConfig->find("Name");
459 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800460 {
461 std::cerr << "could not determine configuration name for "
462 << deviceName << "\n";
463 continue;
464 }
Ed Tanousa771f6a2022-01-14 09:36:51 -0800465 const std::string* psuName = std::get_if<std::string>(&(findPSUName->second));
466 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800467 {
468 std::cerr << "Cannot find psu name, invalid configuration\n";
469 continue;
470 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800471
472 // on rescans, only update sensors we were signaled by
473 if (!firstScan)
474 {
Zhikui Renda98f092021-11-01 09:41:08 -0700475 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800476 auto it =
477 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
478 [psuNameStr](std::string& s) {
Zev Weiss6c106d62022-08-17 20:50:00 -0700479 return s.ends_with(psuNameStr);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800480 });
481
482 if (it == sensorsChanged->end())
483 {
484 continue;
485 }
486 sensorsChanged->erase(it);
487 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800488 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800489 checkGroupEvent(directory.string(), groupEventMatch,
490 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800491
Zev Weisse8b97ee2022-08-12 15:23:51 -0700492 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000493
Vijay Khemka996bad12019-05-28 15:15:16 -0700494 /* Check if there are more sensors in the same interface */
495 int i = 1;
496 std::vector<std::string> psuNames;
497 do
498 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700499 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700500 psuNames.push_back(
501 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700502 findPSUName = baseConfig->find("Name" + std::to_string(i++));
503 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700504
Cheng C Yange50345b2019-04-02 17:26:15 +0800505 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700506 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800507 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800508 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800509 continue;
510 }
511
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530512 /* read max value in sysfs for in, curr, power, temp, ... */
513 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
514 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700515 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530516 {
517 std::cerr << "No max name in PSU \n";
518 }
519 }
520
Lei YU7170a232021-02-04 16:19:27 +0800521 /* The poll rate for the sensors */
522 double pollRate = 0.0;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700523 auto pollRateObj = baseConfig->find("PollRate");
Lei YU7170a232021-02-04 16:19:27 +0800524
Zev Weisse8b97ee2022-08-12 15:23:51 -0700525 if (pollRateObj != baseConfig->end())
Lei YU7170a232021-02-04 16:19:27 +0800526 {
527 pollRate =
528 std::visit(VariantToDoubleVisitor(), pollRateObj->second);
529 if (pollRate <= 0.0)
530 {
531 pollRate = PSUSensor::defaultSensorPoll;
532 }
533 }
534
Vijay Khemka996bad12019-05-28 15:15:16 -0700535 /* Find array of labels to be exposed if it is defined in config */
536 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700537 auto findLabelObj = baseConfig->find("Labels");
538 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700539 {
540 findLabels =
541 std::get<std::vector<std::string>>(findLabelObj->second);
542 }
543
Jason Ling5747fab2019-10-02 16:46:23 -0700544 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
545 std::smatch matches;
546
Cheng C Yange50345b2019-04-02 17:26:15 +0800547 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800548 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530549 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800550 std::string labelHead;
551 std::string sensorPathStr = sensorPath.string();
552 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700553 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700554 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
555 {
Josh Lehan06494452019-10-31 09:49:16 -0700556 // hwmon *_input filename without number:
557 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700558 sensorNameSubStr = matches[1];
559 }
560 else
561 {
Josh Lehan06494452019-10-31 09:49:16 -0700562 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700563 << sensorNameStr;
564 continue;
565 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800566
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530567 std::string labelPath;
568
569 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700570 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530571 if (pos != std::string::npos)
572 {
573
574 std::string sensorPathStrMax = sensorPathStr.substr(pos);
Ed Tanous2049bd22022-07-09 07:20:26 -0700575 if (sensorPathStrMax == "_max")
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530576 {
577 labelPath =
578 boost::replace_all_copy(sensorPathStr, "max", "label");
579 maxLabel = true;
580 }
581 else
582 {
583 labelPath = boost::replace_all_copy(sensorPathStr, "input",
584 "label");
585 maxLabel = false;
586 }
587 }
588 else
589 {
590 continue;
591 }
592
Cheng C Yangecba9de2019-09-12 23:41:50 +0800593 std::ifstream labelFile(labelPath);
594 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800595 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700596 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800597 {
598 std::cerr << "Input file " << sensorPath
599 << " has no corresponding label file\n";
600 }
Josh Lehan06494452019-10-31 09:49:16 -0700601 // hwmon *_input filename with number:
602 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700603 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800604 }
605 else
606 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800607 std::string label;
608 std::getline(labelFile, label);
609 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800610 auto findSensor = sensors.find(label);
611 if (findSensor != sensors.end())
612 {
613 continue;
614 }
615
Josh Lehan06494452019-10-31 09:49:16 -0700616 // hwmon corresponding *_label file contents:
617 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700618 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800619 }
620
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530621 /* append "max" for labelMatch */
622 if (maxLabel)
623 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700624 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530625 }
626
Ed Tanous8a57ec02020-10-09 12:46:52 -0700627 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700628 {
629 std::cerr << "Sensor type=\"" << sensorNameSubStr
630 << "\" label=\"" << labelHead << "\"\n";
631 }
632
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530633 checkPWMSensor(sensorPath, labelHead, *interfacePath,
634 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800635
Vijay Khemka996bad12019-05-28 15:15:16 -0700636 if (!findLabels.empty())
637 {
638 /* Check if this labelHead is enabled in config file */
639 if (std::find(findLabels.begin(), findLabels.end(),
640 labelHead) == findLabels.end())
641 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700642 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800643 {
644 std::cerr << "could not find " << labelHead
645 << " in the Labels list\n";
646 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700647 continue;
648 }
649 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800650
Cheng C Yange50345b2019-04-02 17:26:15 +0800651 auto findProperty = labelMatch.find(labelHead);
652 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800653 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700654 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700655 {
656 std::cerr << "Could not find matching default property for "
657 << labelHead << "\n";
658 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800659 continue;
660 }
661
Josh Lehan74d9bd92019-10-31 08:51:58 -0700662 // Protect the hardcoded labelMatch list from changes,
663 // by making a copy and modifying that instead.
664 // Avoid bleedthrough of one device's customizations to
665 // the next device, as each should be independently customizable.
666 psuProperties.push_back(findProperty->second);
667 auto psuProperty = psuProperties.rbegin();
668
669 // Use label head as prefix for reading from config file,
670 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
671 std::string keyName = labelHead + "_Name";
672 std::string keyScale = labelHead + "_Scale";
673 std::string keyMin = labelHead + "_Min";
674 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800675 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800676 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700677
678 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700679 auto findCustomName = baseConfig->find(keyName);
680 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700681 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700682 try
683 {
684 psuProperty->labelTypeName = std::visit(
685 VariantToStringVisitor(), findCustomName->second);
686 }
Patrick Williams26601e82021-10-06 12:43:25 -0500687 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700688 {
689 std::cerr << "Unable to parse " << keyName << "\n";
690 continue;
691 }
692
693 // All strings are valid, including empty string
694 customizedName = true;
695 }
696
697 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700698 auto findCustomScale = baseConfig->find(keyScale);
699 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700700 {
701 try
702 {
703 psuProperty->sensorScaleFactor = std::visit(
704 VariantToUnsignedIntVisitor(), findCustomScale->second);
705 }
Patrick Williams26601e82021-10-06 12:43:25 -0500706 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700707 {
708 std::cerr << "Unable to parse " << keyScale << "\n";
709 continue;
710 }
711
712 // Avoid later division by zero
713 if (psuProperty->sensorScaleFactor > 0)
714 {
715 customizedScale = true;
716 }
717 else
718 {
719 std::cerr << "Unable to accept " << keyScale << "\n";
720 continue;
721 }
722 }
723
Zev Weisse8b97ee2022-08-12 15:23:51 -0700724 auto findCustomMin = baseConfig->find(keyMin);
725 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700726 {
727 try
728 {
729 psuProperty->minReading = std::visit(
730 VariantToDoubleVisitor(), findCustomMin->second);
731 }
Patrick Williams26601e82021-10-06 12:43:25 -0500732 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700733 {
734 std::cerr << "Unable to parse " << keyMin << "\n";
735 continue;
736 }
737 }
738
Zev Weisse8b97ee2022-08-12 15:23:51 -0700739 auto findCustomMax = baseConfig->find(keyMax);
740 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700741 {
742 try
743 {
744 psuProperty->maxReading = std::visit(
745 VariantToDoubleVisitor(), findCustomMax->second);
746 }
Patrick Williams26601e82021-10-06 12:43:25 -0500747 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700748 {
749 std::cerr << "Unable to parse " << keyMax << "\n";
750 continue;
751 }
752 }
753
Zev Weisse8b97ee2022-08-12 15:23:51 -0700754 auto findCustomOffset = baseConfig->find(keyOffset);
755 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800756 {
757 try
758 {
759 psuProperty->sensorOffset = std::visit(
760 VariantToDoubleVisitor(), findCustomOffset->second);
761 }
Patrick Williams26601e82021-10-06 12:43:25 -0500762 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800763 {
764 std::cerr << "Unable to parse " << keyOffset << "\n";
765 continue;
766 }
767 }
768
Lotus Xucb5af732021-09-10 15:18:50 +0800769 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700770 auto findPowerState = baseConfig->find(keyPowerState);
771 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800772 {
773 std::string powerState = std::visit(VariantToStringVisitor(),
774 findPowerState->second);
775 setReadState(powerState, readState);
776 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700777 if (!(psuProperty->minReading < psuProperty->maxReading))
778 {
779 std::cerr << "Min must be less than Max\n";
780 continue;
781 }
782
783 // If the sensor name is being customized by config file,
784 // then prefix/suffix composition becomes not necessary,
785 // and in fact not wanted, because it gets in the way.
786 std::string psuNameFromIndex;
787 if (!customizedName)
788 {
789 /* Find out sensor name index for this label */
790 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500791 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700792 if (std::regex_search(labelHead, matches, rgx))
793 {
794 nameIndex = std::stoi(matches[1]);
795
796 // Decrement to preserve alignment, because hwmon
797 // human-readable filenames and labels use 1-based
798 // numbering, but the "Name", "Name1", "Name2", etc. naming
799 // convention (the psuNames vector) uses 0-based numbering.
800 if (nameIndex > 0)
801 {
802 --nameIndex;
803 }
804 }
805 else
806 {
807 nameIndex = 0;
808 }
809
810 if (psuNames.size() <= nameIndex)
811 {
812 std::cerr << "Could not pair " << labelHead
813 << " with a Name field\n";
814 continue;
815 }
816
817 psuNameFromIndex = psuNames[nameIndex];
818
Ed Tanous8a57ec02020-10-09 12:46:52 -0700819 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700820 {
821 std::cerr << "Sensor label head " << labelHead
822 << " paired with " << psuNameFromIndex
823 << " at index " << nameIndex << "\n";
824 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700825 }
826
Cheng C Yang58b2b532019-05-31 00:19:45 +0800827 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
828
Josh Lehan74d9bd92019-10-31 08:51:58 -0700829 // Similarly, if sensor scaling factor is being customized,
830 // then the below power-of-10 constraint becomes unnecessary,
831 // as config should be able to specify an arbitrary divisor.
832 unsigned int factor = psuProperty->sensorScaleFactor;
833 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700834 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700835 // Preserve existing usage of hardcoded labelMatch table below
836 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700837
Josh Lehan74d9bd92019-10-31 08:51:58 -0700838 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700839 char firstChar =
840 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700841 std::string strScaleFactor =
842 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
843
844 // Preserve existing configs by accepting earlier syntax,
845 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700846 auto findScaleFactor = baseConfig->find(strScaleFactor);
847 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700848 {
849 factor = std::visit(VariantToIntVisitor(),
850 findScaleFactor->second);
851 }
852
Ed Tanous8a57ec02020-10-09 12:46:52 -0700853 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700854 {
855 std::cerr << "Sensor scaling factor " << factor
856 << " string " << strScaleFactor << "\n";
857 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700858 }
859
Vijay Khemka996bad12019-05-28 15:15:16 -0700860 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530861 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
862 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800863 {
James Feist17ab6e02019-06-25 12:28:13 -0700864 std::cerr << "error populating thresholds for "
865 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800866 }
867
Zev Weiss6b6891c2021-04-22 02:46:21 -0500868 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
869 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800870 {
Jason Ling5747fab2019-10-02 16:46:23 -0700871 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700872 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800873 continue;
874 }
875
Ed Tanous8a57ec02020-10-09 12:46:52 -0700876 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700877 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700878 std::cerr << "Sensor properties: Name \""
879 << psuProperty->labelTypeName << "\" Scale "
880 << psuProperty->sensorScaleFactor << " Min "
881 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800882 << psuProperty->maxReading << " Offset "
883 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700884 }
885
886 std::string sensorName = psuProperty->labelTypeName;
887 if (customizedName)
888 {
889 if (sensorName.empty())
890 {
891 // Allow selective disabling of an individual sensor,
892 // by customizing its name to an empty string.
893 std::cerr << "Sensor disabled, empty string\n";
894 continue;
895 }
896 }
897 else
898 {
899 // Sensor name not customized, do prefix/suffix composition,
900 // preserving default behavior by using psuNameFromIndex.
901 sensorName =
902 psuNameFromIndex + " " + psuProperty->labelTypeName;
903 }
904
Ed Tanous8a57ec02020-10-09 12:46:52 -0700905 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700906 {
907 std::cerr << "Sensor name \"" << sensorName << "\" path \""
908 << sensorPathStr << "\" type \"" << sensorType
909 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700910 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800911 // destruct existing one first if already created
912 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800913 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800914 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800915 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000916 readState, findSensorUnit->second, factor,
917 psuProperty->maxReading, psuProperty->minReading,
918 psuProperty->sensorOffset, labelHead, thresholdConfSize,
919 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800920 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700921 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700922 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700923 {
924 std::cerr << "Created " << numCreated << " sensors so far\n";
925 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800926 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800927
928 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800929 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800930 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000931 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
932 *psuName, readState,
933 eventPathList, groupEventPathList,
934 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800935 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700936
Ed Tanous8a57ec02020-10-09 12:46:52 -0700937 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700938 {
939 std::cerr << "Created total of " << numCreated << " sensors\n";
940 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700941 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800942
Zhikui Ren23c96e72020-11-05 22:32:28 -0800943void createSensors(
944 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
945 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
946 const std::shared_ptr<boost::container::flat_set<std::string>>&
947 sensorsChanged)
948{
949 auto getter = std::make_shared<GetSensorConfiguration>(
950 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
951 const ManagedObjectType& sensorConfigs) {
952 createSensorsCallback(io, objectServer, dbusConnection,
953 sensorConfigs, sensorsChanged);
954 });
955 getter->getConfiguration(
956 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
957}
958
Cheng C Yang916360b2019-05-07 18:47:16 +0800959void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800960{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500961 sensorTable = {{"power", sensor_paths::unitWatts},
962 {"curr", sensor_paths::unitAmperes},
963 {"temp", sensor_paths::unitDegreesC},
964 {"in", sensor_paths::unitVolts},
965 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800966
Jeff Line41d52f2021-04-07 19:38:51 +0800967 labelMatch = {
968 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800969 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
970 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800971 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
972 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
973 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
974 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800975 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
976 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
977 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800978 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
979 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
980 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
981 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
982 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
985 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
986 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
987 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
988 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
989 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
993 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
994 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
995 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
996 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
997 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
998 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
999 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1000 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1001 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1002 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1003 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1004 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1005 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1006 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1007 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1008 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1009 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1010 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1011 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1012 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1013 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001014 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001015 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001016 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1017 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1018 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1019 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1020 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1021 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001022 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001023 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1024 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001025 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1026 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1027 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1028 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1029 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1030 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1031 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1032 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1033 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1034 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1035 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1036 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1037 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1038 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1039 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001040 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1041 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1042 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001043 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1044 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1045 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1046 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1047 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1048 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1049 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1050 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1051 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
1052 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001053
1054 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001055
1056 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1057 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1058
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001059 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1060 {"Failure", {"in2_alarm"}},
1061 {"ACLost", {"in1_beep"}},
1062 {"ConfigureError", {"in1_fault"}}};
1063
1064 groupEventMatch = {{"FanFault",
1065 {{"fan1", {"fan1_alarm", "fan1_fault"}},
1066 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001067}
1068
James Feistb6c0b912019-07-09 12:21:44 -07001069int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001070{
1071 boost::asio::io_service io;
1072 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1073
1074 systemBus->request_name("xyz.openbmc_project.PSUSensor");
1075 sdbusplus::asio::object_server objectServer(systemBus);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001076 auto sensorsChanged =
1077 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001078
Cheng C Yang916360b2019-05-07 18:47:16 +08001079 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001080
Zhikui Ren23c96e72020-11-05 22:32:28 -08001081 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Cheng C Yang209ec562019-03-12 16:37:44 +08001082 boost::asio::deadline_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001083 std::function<void(sdbusplus::message_t&)> eventHandler =
1084 [&](sdbusplus::message_t& message) {
Cheng C Yang209ec562019-03-12 16:37:44 +08001085 if (message.is_method_error())
1086 {
1087 std::cerr << "callback method error\n";
1088 return;
1089 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001090 sensorsChanged->insert(message.get_path());
Cheng C Yanga97f1342020-02-11 15:10:41 +08001091 filterTimer.expires_from_now(boost::posix_time::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +08001092 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1093 if (ec == boost::asio::error::operation_aborted)
1094 {
1095 return;
1096 }
Ed Tanous8a57ec02020-10-09 12:46:52 -07001097 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +08001098 {
1099 std::cerr << "timer error\n";
1100 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001101 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +08001102 });
1103 };
1104
Zev Weiss214d9712022-08-12 12:54:31 -07001105 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1106 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +08001107 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001108 io.run();
1109}