blob: b5af1769ace2e986705b2c877212db1d0177f97e [file] [log] [blame]
Cheng C Yang209ec562019-03-12 16:37:44 +08001/*
2// Copyright (c) 2019 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
Josh Lehan0830c7b2019-10-08 16:35:09 -070016
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "PSUEvent.hpp"
18#include "PSUSensor.hpp"
19#include "Utils.hpp"
20
Cheng C Yang209ec562019-03-12 16:37:44 +080021#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070022#include <boost/container/flat_map.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080023#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26#include <sdbusplus/bus/match.hpp>
27
28#include <array>
Josh Lehan74d9bd92019-10-31 08:51:58 -070029#include <cmath>
James Feist24f02f22019-04-15 11:05:39 -070030#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080031#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <functional>
Cheng C Yang58b2b532019-05-31 00:19:45 +080033#include <iostream>
Cheng C Yang209ec562019-03-12 16:37:44 +080034#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <string>
Lei YUa2c7cea2020-12-23 14:07:28 +080036#include <string_view>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <utility>
38#include <variant>
39#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080040
Ed Tanous8a57ec02020-10-09 12:46:52 -070041static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070042
Brandon Kim66558232021-11-09 16:53:08 -080043static constexpr auto sensorTypes{std::to_array<const char*>(
Zev Weissbc56cfb2022-11-21 16:40:09 -080044 {"ADM1266", "ADM1272", "ADM1275", "ADM1278", "ADM1293",
45 "ADS7830", "AHE50DC_FAN", "BMR490", "DPS800", "INA219",
46 "INA230", "IPSPS", "IR38060", "IR38164", "IR38263",
47 "ISL68137", "ISL68220", "ISL68223", "ISL69225", "ISL69243",
48 "ISL69260", "LM25066", "MAX16601", "MAX20710", "MAX20730",
49 "MAX20734", "MAX20796", "MAX34451", "MP2971", "MP2973",
50 "MP5023", "PLI1209BC", "pmbus", "PXE1610", "RAA228000",
51 "RAA228228", "RAA228620", "RAA229001", "RAA229004", "RAA229126",
52 "TPS53679", "TPS546D24", "XDPE11280", "XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080053
Ed Tanousa2df7862021-12-07 16:30:27 -080054// clang-format off
55static constexpr auto pmbusNames{std::to_array<const char*>({
56 "adm1266",
57 "adm1272",
58 "adm1275",
59 "adm1278",
Zhikui Ren842e5432022-03-09 16:18:24 -080060 "adm1293",
Tim Chao6f379ce2022-02-24 11:08:09 +080061 "ads7830",
Zev Weissbc56cfb2022-11-21 16:40:09 -080062 "ahe50dc_fan",
linchuyuan5cf66df2021-12-21 17:38:55 -080063 "bmr490",
Ed Tanousa2df7862021-12-07 16:30:27 -080064 "dps800",
65 "ina219",
66 "ina230",
67 "ipsps1",
Naresh Solankiadbcd7c2022-11-02 10:07:26 +010068 "ir38060",
69 "ir38164",
70 "ir38263",
Ed Tanousa2df7862021-12-07 16:30:27 -080071 "isl68137",
72 "isl68220",
73 "isl68223",
Khang Kieu51ad6672022-02-09 01:26:25 +000074 "isl69225",
Ed Tanousa2df7862021-12-07 16:30:27 -080075 "isl69243",
76 "isl69260",
77 "lm25066",
78 "max16601",
79 "max20710",
80 "max20730",
81 "max20734",
82 "max20796",
83 "max34451",
Shamim Alidccd1d42022-06-08 22:31:35 +053084 "mp2971",
85 "mp2973",
Howard Chiub58ac3a2021-12-07 17:12:56 +080086 "mp5023",
Naresh Solankiadbcd7c2022-11-02 10:07:26 +010087 "pli1209bc",
Ed Tanousa2df7862021-12-07 16:30:27 -080088 "pmbus",
89 "pxe1610",
90 "raa228000",
91 "raa228228",
Tom Tung75da5152021-11-24 09:38:07 +080092 "raa228620",
93 "raa229001",
Ed Tanousa2df7862021-12-07 16:30:27 -080094 "raa229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -080095 "raa229126",
Konstantin Aladyshev21e12c32022-10-19 18:56:10 +030096 "tps53679",
Ed Tanousa2df7862021-12-07 16:30:27 -080097 "tps546d24",
Naresh Solankiadbcd7c2022-11-02 10:07:26 +010098 "xdpe11280",
Ed Tanousa2df7862021-12-07 16:30:27 -080099 "xdpe12284"
100})};
101//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -0700102
Cheng C Yang209ec562019-03-12 16:37:44 +0800103namespace fs = std::filesystem;
104
Yong Libf8b1da2020-04-15 16:32:50 +0800105static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800106 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800107static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
108 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800109static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
110 pwmSensors;
111static boost::container::flat_map<std::string, std::string> sensorTable;
112static boost::container::flat_map<std::string, PSUProperty> labelMatch;
113static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800114static boost::container::flat_map<std::string, std::vector<std::string>>
115 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800116static boost::container::flat_map<
117 std::string,
118 boost::container::flat_map<std::string, std::vector<std::string>>>
119 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800120static boost::container::flat_map<std::string, std::vector<std::string>>
121 limitEventMatch;
122
Josh Lehan74d9bd92019-10-31 08:51:58 -0700123static std::vector<PSUProperty> psuProperties;
124
Cheng C Yang58b2b532019-05-31 00:19:45 +0800125// Function CheckEvent will check each attribute from eventMatch table in the
126// sysfs. If the attributes exists in sysfs, then store the complete path
127// of the attribute into eventPathList.
128void checkEvent(
129 const std::string& directory,
130 const boost::container::flat_map<std::string, std::vector<std::string>>&
131 eventMatch,
132 boost::container::flat_map<std::string, std::vector<std::string>>&
133 eventPathList)
134{
135 for (const auto& match : eventMatch)
136 {
137 const std::vector<std::string>& eventAttrs = match.second;
138 const std::string& eventName = match.first;
139 for (const auto& eventAttr : eventAttrs)
140 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700141 std::string eventPath = directory;
142 eventPath += "/";
143 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800144
145 std::ifstream eventFile(eventPath);
146 if (!eventFile.good())
147 {
148 continue;
149 }
150
151 eventPathList[eventName].push_back(eventPath);
152 }
153 }
154}
155
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800156// Check Group Events which contains more than one targets in each combine
157// events.
158void checkGroupEvent(
159 const std::string& directory,
160 const boost::container::flat_map<
161 std::string,
162 boost::container::flat_map<std::string, std::vector<std::string>>>&
163 groupEventMatch,
164 boost::container::flat_map<
165 std::string,
166 boost::container::flat_map<std::string, std::vector<std::string>>>&
167 groupEventPathList)
168{
169 for (const auto& match : groupEventMatch)
170 {
171 const std::string& groupEventName = match.first;
172 const boost::container::flat_map<std::string, std::vector<std::string>>
173 events = match.second;
174 boost::container::flat_map<std::string, std::vector<std::string>>
175 pathList;
176 for (const auto& match : events)
177 {
178 const std::string& eventName = match.first;
179 const std::vector<std::string>& eventAttrs = match.second;
180 for (const auto& eventAttr : eventAttrs)
181 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700182 std::string eventPath = directory;
183 eventPath += "/";
184 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800185 std::ifstream eventFile(eventPath);
186 if (!eventFile.good())
187 {
188 continue;
189 }
190
191 pathList[eventName].push_back(eventPath);
192 }
193 }
194 groupEventPathList[groupEventName] = pathList;
195 }
196}
197
Cheng C Yang58b2b532019-05-31 00:19:45 +0800198// Function checkEventLimits will check all the psu related xxx_input attributes
199// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
200// xxx_min_alarm exist, then store the existing paths of the alarm attributes
201// to eventPathList.
202void checkEventLimits(
203 const std::string& sensorPathStr,
204 const boost::container::flat_map<std::string, std::vector<std::string>>&
205 limitEventMatch,
206 boost::container::flat_map<std::string, std::vector<std::string>>&
207 eventPathList)
208{
Lei YUa2c7cea2020-12-23 14:07:28 +0800209 auto attributePartPos = sensorPathStr.find_last_of('_');
210 if (attributePartPos == std::string::npos)
211 {
212 // There is no '_' in the string, skip it
213 return;
214 }
215 auto attributePart =
216 std::string_view(sensorPathStr).substr(attributePartPos + 1);
217 if (attributePart != "input")
218 {
219 // If the sensor is not xxx_input, skip it
220 return;
221 }
222
223 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800224 for (const auto& limitMatch : limitEventMatch)
225 {
226 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
227 const std::string& eventName = limitMatch.first;
228 for (const auto& limitEventAttr : limitEventAttrs)
229 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800230 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800231 std::ifstream eventFile(limitEventPath);
232 if (!eventFile.good())
233 {
234 continue;
235 }
236 eventPathList[eventName].push_back(limitEventPath);
237 }
238 }
239}
Cheng C Yang916360b2019-05-07 18:47:16 +0800240
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530241static void
242 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
243 const std::string& interfacePath,
244 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
245 sdbusplus::asio::object_server& objectServer,
246 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800247{
Zev Weiss741f26e2022-08-12 18:21:02 -0700248 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800249 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700250 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800251 {
252 continue;
253 }
254
255 const std::string& sensorPathStr = sensorPath.string();
256 const std::string& pwmPathStr =
257 boost::replace_all_copy(sensorPathStr, "input", "target");
258 std::ifstream pwmFile(pwmPathStr);
259 if (!pwmFile.good())
260 {
261 continue;
262 }
263
264 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
265 if (findPWMSensor != pwmSensors.end())
266 {
267 continue;
268 }
269
Zev Weissd8c293a2022-08-15 18:58:41 -0700270 std::string name = "Pwm_";
271 name += psuName;
272 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700273 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700274
275 std::string objPath = interfacePath;
276 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700277 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700278
Cheng C Yang916360b2019-05-07 18:47:16 +0800279 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700280 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800281 }
282}
283
Zhikui Ren23c96e72020-11-05 22:32:28 -0800284static void createSensorsCallback(
285 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
286 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
287 const ManagedObjectType& sensorConfigs,
288 const std::shared_ptr<boost::container::flat_set<std::string>>&
289 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800290{
Josh Lehan49cfba92019-10-08 16:50:42 -0700291 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800292 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800293
294 std::vector<fs::path> pmbusPaths;
295 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
296 {
297 std::cerr << "No PSU sensors in system\n";
298 return;
299 }
300
301 boost::container::flat_set<std::string> directories;
302 for (const auto& pmbusPath : pmbusPaths)
303 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800304 boost::container::flat_map<std::string, std::vector<std::string>>
305 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800306 boost::container::flat_map<
307 std::string,
308 boost::container::flat_map<std::string, std::vector<std::string>>>
309 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800310
311 std::ifstream nameFile(pmbusPath);
312 if (!nameFile.good())
313 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700314 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800315 continue;
316 }
317
318 std::string pmbusName;
319 std::getline(nameFile, pmbusName);
320 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700321
322 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
323 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800324 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700325 // To avoid this error message, add your driver name to
326 // the pmbusNames vector at the top of this file.
327 std::cerr << "Driver name " << pmbusName
328 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800329 continue;
330 }
331
Cheng C Yang209ec562019-03-12 16:37:44 +0800332 auto directory = pmbusPath.parent_path();
333
334 auto ret = directories.insert(directory.string());
335 if (!ret.second)
336 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700337 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800338 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800339 }
340
James Feistb6c0b912019-07-09 12:21:44 -0700341 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800342 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700343 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800344 if (findHyphen == std::string::npos)
345 {
346 std::cerr << "found bad device" << deviceName << "\n";
347 continue;
348 }
349 std::string busStr = deviceName.substr(0, findHyphen);
350 std::string addrStr = deviceName.substr(findHyphen + 1);
351
352 size_t bus = 0;
353 size_t addr = 0;
354
355 try
356 {
357 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700358 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800359 }
Patrick Williams26601e82021-10-06 12:43:25 -0500360 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800361 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700362 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
363 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800364 continue;
365 }
366
Zev Weisse8b97ee2022-08-12 15:23:51 -0700367 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800368 const SensorData* sensorData = nullptr;
369 const std::string* interfacePath = nullptr;
370 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800371 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800372
Zev Weiss741f26e2022-08-12 18:21:02 -0700373 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800374 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700375 sensorData = &cfgData;
Cheng C Yang209ec562019-03-12 16:37:44 +0800376 for (const char* type : sensorTypes)
377 {
Zev Weiss054aad82022-08-18 01:37:34 -0700378 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800379 if (sensorBase != sensorData->end())
380 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700381 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800382 sensorType = type;
383 break;
384 }
385 }
386 if (baseConfig == nullptr)
387 {
388 std::cerr << "error finding base configuration for "
389 << deviceName << "\n";
390 continue;
391 }
392
Zev Weisse8b97ee2022-08-12 15:23:51 -0700393 auto configBus = baseConfig->find("Bus");
394 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800395
Zev Weisse8b97ee2022-08-12 15:23:51 -0700396 if (configBus == baseConfig->end() ||
397 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800398 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800399 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800400 continue;
401 }
402
Matt Simmering6747eba2023-01-18 16:03:55 -0800403 const uint64_t* confBus =
404 std::get_if<uint64_t>(&(configBus->second));
405 const uint64_t* confAddr =
406 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800407 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800408 {
409 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700410 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800411 continue;
412 }
413
414 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800415 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700416 std::cerr << "Configuration skipping " << *confBus << "-"
417 << *confAddr << " because not " << bus << "-" << addr
418 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800419 continue;
420 }
421
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800422 std::vector<thresholds::Threshold> confThresholds;
423 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
424 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800425 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800426 }
427 thresholdConfSize = confThresholds.size();
428
Zev Weiss741f26e2022-08-12 18:21:02 -0700429 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800430 break;
431 }
432 if (interfacePath == nullptr)
433 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700434 // To avoid this error message, add your export map entry,
435 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800436 std::cerr << "failed to find match for " << deviceName << "\n";
437 continue;
438 }
439
Zev Weisse8b97ee2022-08-12 15:23:51 -0700440 auto findPSUName = baseConfig->find("Name");
441 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800442 {
443 std::cerr << "could not determine configuration name for "
444 << deviceName << "\n";
445 continue;
446 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800447 const std::string* psuName =
448 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800449 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800450 {
451 std::cerr << "Cannot find psu name, invalid configuration\n";
452 continue;
453 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800454
455 // on rescans, only update sensors we were signaled by
456 if (!firstScan)
457 {
Zhikui Renda98f092021-11-01 09:41:08 -0700458 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800459 auto it =
460 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
461 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800462 return s.ends_with(psuNameStr);
463 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800464
465 if (it == sensorsChanged->end())
466 {
467 continue;
468 }
469 sensorsChanged->erase(it);
470 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800471 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800472 checkGroupEvent(directory.string(), groupEventMatch,
473 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800474
Zev Weisse8b97ee2022-08-12 15:23:51 -0700475 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000476
Vijay Khemka996bad12019-05-28 15:15:16 -0700477 /* Check if there are more sensors in the same interface */
478 int i = 1;
479 std::vector<std::string> psuNames;
480 do
481 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700482 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700483 psuNames.push_back(
484 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700485 findPSUName = baseConfig->find("Name" + std::to_string(i++));
486 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700487
Cheng C Yange50345b2019-04-02 17:26:15 +0800488 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700489 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800490 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800491 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800492 continue;
493 }
494
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530495 /* read max value in sysfs for in, curr, power, temp, ... */
496 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
497 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700498 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530499 {
500 std::cerr << "No max name in PSU \n";
501 }
502 }
503
Zev Weiss8569bf22022-10-11 15:37:44 -0700504 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800505
Vijay Khemka996bad12019-05-28 15:15:16 -0700506 /* Find array of labels to be exposed if it is defined in config */
507 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700508 auto findLabelObj = baseConfig->find("Labels");
509 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700510 {
511 findLabels =
512 std::get<std::vector<std::string>>(findLabelObj->second);
513 }
514
Jason Ling5747fab2019-10-02 16:46:23 -0700515 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
516 std::smatch matches;
517
Cheng C Yange50345b2019-04-02 17:26:15 +0800518 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800519 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530520 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800521 std::string labelHead;
522 std::string sensorPathStr = sensorPath.string();
523 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700524 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700525 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
526 {
Josh Lehan06494452019-10-31 09:49:16 -0700527 // hwmon *_input filename without number:
528 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700529 sensorNameSubStr = matches[1];
530 }
531 else
532 {
Josh Lehan06494452019-10-31 09:49:16 -0700533 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700534 << sensorNameStr;
535 continue;
536 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800537
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530538 std::string labelPath;
539
540 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700541 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530542 if (pos != std::string::npos)
543 {
544
545 std::string sensorPathStrMax = sensorPathStr.substr(pos);
Ed Tanous2049bd22022-07-09 07:20:26 -0700546 if (sensorPathStrMax == "_max")
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530547 {
548 labelPath =
549 boost::replace_all_copy(sensorPathStr, "max", "label");
550 maxLabel = true;
551 }
552 else
553 {
554 labelPath = boost::replace_all_copy(sensorPathStr, "input",
555 "label");
556 maxLabel = false;
557 }
558 }
559 else
560 {
561 continue;
562 }
563
Cheng C Yangecba9de2019-09-12 23:41:50 +0800564 std::ifstream labelFile(labelPath);
565 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800566 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700567 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800568 {
569 std::cerr << "Input file " << sensorPath
570 << " has no corresponding label file\n";
571 }
Josh Lehan06494452019-10-31 09:49:16 -0700572 // hwmon *_input filename with number:
573 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700574 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800575 }
576 else
577 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800578 std::string label;
579 std::getline(labelFile, label);
580 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800581 auto findSensor = sensors.find(label);
582 if (findSensor != sensors.end())
583 {
584 continue;
585 }
586
Josh Lehan06494452019-10-31 09:49:16 -0700587 // hwmon corresponding *_label file contents:
588 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700589 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800590 }
591
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530592 /* append "max" for labelMatch */
593 if (maxLabel)
594 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700595 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530596 }
597
Ed Tanous8a57ec02020-10-09 12:46:52 -0700598 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700599 {
600 std::cerr << "Sensor type=\"" << sensorNameSubStr
601 << "\" label=\"" << labelHead << "\"\n";
602 }
603
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530604 checkPWMSensor(sensorPath, labelHead, *interfacePath,
605 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800606
Vijay Khemka996bad12019-05-28 15:15:16 -0700607 if (!findLabels.empty())
608 {
609 /* Check if this labelHead is enabled in config file */
610 if (std::find(findLabels.begin(), findLabels.end(),
611 labelHead) == findLabels.end())
612 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700613 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800614 {
615 std::cerr << "could not find " << labelHead
616 << " in the Labels list\n";
617 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700618 continue;
619 }
620 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800621
Cheng C Yange50345b2019-04-02 17:26:15 +0800622 auto findProperty = labelMatch.find(labelHead);
623 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800624 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700625 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700626 {
627 std::cerr << "Could not find matching default property for "
628 << labelHead << "\n";
629 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800630 continue;
631 }
632
Josh Lehan74d9bd92019-10-31 08:51:58 -0700633 // Protect the hardcoded labelMatch list from changes,
634 // by making a copy and modifying that instead.
635 // Avoid bleedthrough of one device's customizations to
636 // the next device, as each should be independently customizable.
637 psuProperties.push_back(findProperty->second);
638 auto psuProperty = psuProperties.rbegin();
639
640 // Use label head as prefix for reading from config file,
641 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
642 std::string keyName = labelHead + "_Name";
643 std::string keyScale = labelHead + "_Scale";
644 std::string keyMin = labelHead + "_Min";
645 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800646 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800647 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700648
649 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700650 auto findCustomName = baseConfig->find(keyName);
651 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700652 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700653 try
654 {
655 psuProperty->labelTypeName = std::visit(
656 VariantToStringVisitor(), findCustomName->second);
657 }
Patrick Williams26601e82021-10-06 12:43:25 -0500658 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700659 {
660 std::cerr << "Unable to parse " << keyName << "\n";
661 continue;
662 }
663
664 // All strings are valid, including empty string
665 customizedName = true;
666 }
667
668 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700669 auto findCustomScale = baseConfig->find(keyScale);
670 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700671 {
672 try
673 {
674 psuProperty->sensorScaleFactor = std::visit(
675 VariantToUnsignedIntVisitor(), findCustomScale->second);
676 }
Patrick Williams26601e82021-10-06 12:43:25 -0500677 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700678 {
679 std::cerr << "Unable to parse " << keyScale << "\n";
680 continue;
681 }
682
683 // Avoid later division by zero
684 if (psuProperty->sensorScaleFactor > 0)
685 {
686 customizedScale = true;
687 }
688 else
689 {
690 std::cerr << "Unable to accept " << keyScale << "\n";
691 continue;
692 }
693 }
694
Zev Weisse8b97ee2022-08-12 15:23:51 -0700695 auto findCustomMin = baseConfig->find(keyMin);
696 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700697 {
698 try
699 {
700 psuProperty->minReading = std::visit(
701 VariantToDoubleVisitor(), findCustomMin->second);
702 }
Patrick Williams26601e82021-10-06 12:43:25 -0500703 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700704 {
705 std::cerr << "Unable to parse " << keyMin << "\n";
706 continue;
707 }
708 }
709
Zev Weisse8b97ee2022-08-12 15:23:51 -0700710 auto findCustomMax = baseConfig->find(keyMax);
711 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700712 {
713 try
714 {
715 psuProperty->maxReading = std::visit(
716 VariantToDoubleVisitor(), findCustomMax->second);
717 }
Patrick Williams26601e82021-10-06 12:43:25 -0500718 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700719 {
720 std::cerr << "Unable to parse " << keyMax << "\n";
721 continue;
722 }
723 }
724
Zev Weisse8b97ee2022-08-12 15:23:51 -0700725 auto findCustomOffset = baseConfig->find(keyOffset);
726 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800727 {
728 try
729 {
730 psuProperty->sensorOffset = std::visit(
731 VariantToDoubleVisitor(), findCustomOffset->second);
732 }
Patrick Williams26601e82021-10-06 12:43:25 -0500733 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800734 {
735 std::cerr << "Unable to parse " << keyOffset << "\n";
736 continue;
737 }
738 }
739
Lotus Xucb5af732021-09-10 15:18:50 +0800740 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700741 auto findPowerState = baseConfig->find(keyPowerState);
742 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800743 {
744 std::string powerState = std::visit(VariantToStringVisitor(),
745 findPowerState->second);
746 setReadState(powerState, readState);
747 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700748 if (!(psuProperty->minReading < psuProperty->maxReading))
749 {
750 std::cerr << "Min must be less than Max\n";
751 continue;
752 }
753
754 // If the sensor name is being customized by config file,
755 // then prefix/suffix composition becomes not necessary,
756 // and in fact not wanted, because it gets in the way.
757 std::string psuNameFromIndex;
758 if (!customizedName)
759 {
760 /* Find out sensor name index for this label */
761 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500762 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700763 if (std::regex_search(labelHead, matches, rgx))
764 {
765 nameIndex = std::stoi(matches[1]);
766
767 // Decrement to preserve alignment, because hwmon
768 // human-readable filenames and labels use 1-based
769 // numbering, but the "Name", "Name1", "Name2", etc. naming
770 // convention (the psuNames vector) uses 0-based numbering.
771 if (nameIndex > 0)
772 {
773 --nameIndex;
774 }
775 }
776 else
777 {
778 nameIndex = 0;
779 }
780
781 if (psuNames.size() <= nameIndex)
782 {
783 std::cerr << "Could not pair " << labelHead
784 << " with a Name field\n";
785 continue;
786 }
787
788 psuNameFromIndex = psuNames[nameIndex];
789
Ed Tanous8a57ec02020-10-09 12:46:52 -0700790 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700791 {
792 std::cerr << "Sensor label head " << labelHead
793 << " paired with " << psuNameFromIndex
794 << " at index " << nameIndex << "\n";
795 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700796 }
797
Cheng C Yang58b2b532019-05-31 00:19:45 +0800798 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
799
Josh Lehan74d9bd92019-10-31 08:51:58 -0700800 // Similarly, if sensor scaling factor is being customized,
801 // then the below power-of-10 constraint becomes unnecessary,
802 // as config should be able to specify an arbitrary divisor.
803 unsigned int factor = psuProperty->sensorScaleFactor;
804 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700805 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700806 // Preserve existing usage of hardcoded labelMatch table below
807 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700808
Josh Lehan74d9bd92019-10-31 08:51:58 -0700809 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700810 char firstChar =
811 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700812 std::string strScaleFactor =
813 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
814
815 // Preserve existing configs by accepting earlier syntax,
816 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700817 auto findScaleFactor = baseConfig->find(strScaleFactor);
818 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700819 {
820 factor = std::visit(VariantToIntVisitor(),
821 findScaleFactor->second);
822 }
823
Ed Tanous8a57ec02020-10-09 12:46:52 -0700824 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700825 {
826 std::cerr << "Sensor scaling factor " << factor
827 << " string " << strScaleFactor << "\n";
828 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700829 }
830
Vijay Khemka996bad12019-05-28 15:15:16 -0700831 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530832 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
833 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800834 {
James Feist17ab6e02019-06-25 12:28:13 -0700835 std::cerr << "error populating thresholds for "
836 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800837 }
838
Zev Weiss6b6891c2021-04-22 02:46:21 -0500839 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
840 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800841 {
Jason Ling5747fab2019-10-02 16:46:23 -0700842 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700843 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800844 continue;
845 }
846
Ed Tanous8a57ec02020-10-09 12:46:52 -0700847 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700848 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700849 std::cerr << "Sensor properties: Name \""
850 << psuProperty->labelTypeName << "\" Scale "
851 << psuProperty->sensorScaleFactor << " Min "
852 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800853 << psuProperty->maxReading << " Offset "
854 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700855 }
856
857 std::string sensorName = psuProperty->labelTypeName;
858 if (customizedName)
859 {
860 if (sensorName.empty())
861 {
862 // Allow selective disabling of an individual sensor,
863 // by customizing its name to an empty string.
864 std::cerr << "Sensor disabled, empty string\n";
865 continue;
866 }
867 }
868 else
869 {
870 // Sensor name not customized, do prefix/suffix composition,
871 // preserving default behavior by using psuNameFromIndex.
872 sensorName =
873 psuNameFromIndex + " " + psuProperty->labelTypeName;
874 }
875
Ed Tanous8a57ec02020-10-09 12:46:52 -0700876 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700877 {
878 std::cerr << "Sensor name \"" << sensorName << "\" path \""
879 << sensorPathStr << "\" type \"" << sensorType
880 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700881 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800882 // destruct existing one first if already created
883 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800884 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800885 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800886 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000887 readState, findSensorUnit->second, factor,
888 psuProperty->maxReading, psuProperty->minReading,
889 psuProperty->sensorOffset, labelHead, thresholdConfSize,
890 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800891 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700892 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700893 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700894 {
895 std::cerr << "Created " << numCreated << " sensors so far\n";
896 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800897 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800898
899 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800900 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800901 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000902 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
903 *psuName, readState,
904 eventPathList, groupEventPathList,
905 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800906 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700907
Ed Tanous8a57ec02020-10-09 12:46:52 -0700908 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700909 {
910 std::cerr << "Created total of " << numCreated << " sensors\n";
911 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800912}
Cheng C Yang209ec562019-03-12 16:37:44 +0800913
Zhikui Ren23c96e72020-11-05 22:32:28 -0800914void createSensors(
915 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
916 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
917 const std::shared_ptr<boost::container::flat_set<std::string>>&
918 sensorsChanged)
919{
920 auto getter = std::make_shared<GetSensorConfiguration>(
921 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
922 const ManagedObjectType& sensorConfigs) {
923 createSensorsCallback(io, objectServer, dbusConnection,
924 sensorConfigs, sensorsChanged);
925 });
926 getter->getConfiguration(
927 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
928}
929
Cheng C Yang916360b2019-05-07 18:47:16 +0800930void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800931{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500932 sensorTable = {{"power", sensor_paths::unitWatts},
933 {"curr", sensor_paths::unitAmperes},
934 {"temp", sensor_paths::unitDegreesC},
935 {"in", sensor_paths::unitVolts},
936 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800937
Jeff Line41d52f2021-04-07 19:38:51 +0800938 labelMatch = {
939 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800940 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
941 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800942 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
943 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
944 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
945 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800946 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
947 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
948 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800949 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
950 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
951 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
952 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
953 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
954 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
955 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
956 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
957 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
958 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
959 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
960 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
961 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
962 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
963 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
964 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
965 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
966 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
967 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
968 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
969 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
970 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
971 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
972 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
973 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
974 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
975 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
976 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
977 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
978 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
979 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
980 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
981 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
982 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
983 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
984 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +0800985 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800986 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +0800987 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
988 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
989 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
990 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
991 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
992 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800993 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800994 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
995 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800996 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
997 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
998 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
999 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1000 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1001 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1002 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1003 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1004 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1005 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1006 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1007 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1008 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1009 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1010 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001011 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1012 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1013 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001014 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1015 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1016 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1017 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1018 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1019 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1020 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1021 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1022 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001023 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1024 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1025 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001026
Matt Simmering6747eba2023-01-18 16:03:55 -08001027 pwmTable = {{"fan1", "Fan_1"},
1028 {"fan2", "Fan_2"},
1029 {"fan3", "Fan_3"},
1030 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001031
1032 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1033 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1034
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001035 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1036 {"Failure", {"in2_alarm"}},
1037 {"ACLost", {"in1_beep"}},
1038 {"ConfigureError", {"in1_fault"}}};
1039
1040 groupEventMatch = {{"FanFault",
1041 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001042 {"fan2", {"fan2_alarm", "fan2_fault"}},
1043 {"fan3", {"fan3_alarm", "fan3_fault"}},
1044 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001045}
1046
James Feistb6c0b912019-07-09 12:21:44 -07001047int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001048{
1049 boost::asio::io_service io;
1050 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1051
Ed Tanous14ed5e92022-07-12 15:50:23 -07001052 sdbusplus::asio::object_server objectServer(systemBus, true);
1053 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001054 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001055 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001056 auto sensorsChanged =
1057 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001058
Cheng C Yang916360b2019-05-07 18:47:16 +08001059 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001060
Zhikui Ren23c96e72020-11-05 22:32:28 -08001061 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001062 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001063 std::function<void(sdbusplus::message_t&)> eventHandler =
1064 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001065 if (message.is_method_error())
1066 {
1067 std::cerr << "callback method error\n";
1068 return;
1069 }
1070 sensorsChanged->insert(message.get_path());
1071 filterTimer.expires_from_now(std::chrono::seconds(3));
1072 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1073 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001074 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001075 return;
1076 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001077 if (ec)
1078 {
1079 std::cerr << "timer error\n";
1080 }
1081 createSensors(io, objectServer, systemBus, sensorsChanged);
1082 });
1083 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001084
Zev Weiss214d9712022-08-12 12:54:31 -07001085 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1086 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +08001087 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001088 io.run();
1089}