blob: 24168ba0d49549139ede944c48e12e529c08ac55 [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*>(
Zev Weiss054aad82022-08-18 01:37:34 -070043 {"ADM1266", "ADM1272", "ADM1275", "ADM1278", "ADM1293",
44 "ADS7830", "BMR490", "DPS800", "INA219", "INA230",
45 "IPSPS", "ISL68137", "ISL68220", "ISL68223", "ISL69225",
46 "ISL69243", "ISL69260", "LM25066", "MAX16601", "MAX20710",
47 "MAX20730", "MAX20734", "MAX20796", "MAX34451", "MP2971",
48 "MP2973", "MP5023", "pmbus", "PXE1610", "RAA228000",
49 "RAA228228", "RAA228620", "RAA229001", "RAA229004", "RAA229126",
Konstantin Aladyshev21e12c32022-10-19 18:56:10 +030050 "TPS53679", "TPS546D24", "XDPE12284"})};
Cheng C Yang209ec562019-03-12 16:37:44 +080051
Ed Tanousa2df7862021-12-07 16:30:27 -080052// clang-format off
53static constexpr auto pmbusNames{std::to_array<const char*>({
54 "adm1266",
55 "adm1272",
56 "adm1275",
57 "adm1278",
Zhikui Ren842e5432022-03-09 16:18:24 -080058 "adm1293",
Tim Chao6f379ce2022-02-24 11:08:09 +080059 "ads7830",
linchuyuan5cf66df2021-12-21 17:38:55 -080060 "bmr490",
Ed Tanousa2df7862021-12-07 16:30:27 -080061 "dps800",
62 "ina219",
63 "ina230",
64 "ipsps1",
65 "isl68137",
66 "isl68220",
67 "isl68223",
Khang Kieu51ad6672022-02-09 01:26:25 +000068 "isl69225",
Ed Tanousa2df7862021-12-07 16:30:27 -080069 "isl69243",
70 "isl69260",
71 "lm25066",
72 "max16601",
73 "max20710",
74 "max20730",
75 "max20734",
76 "max20796",
77 "max34451",
Shamim Alidccd1d42022-06-08 22:31:35 +053078 "mp2971",
79 "mp2973",
Howard Chiub58ac3a2021-12-07 17:12:56 +080080 "mp5023",
Ed Tanousa2df7862021-12-07 16:30:27 -080081 "pmbus",
82 "pxe1610",
83 "raa228000",
84 "raa228228",
Tom Tung75da5152021-11-24 09:38:07 +080085 "raa228620",
86 "raa229001",
Ed Tanousa2df7862021-12-07 16:30:27 -080087 "raa229004",
Zhikui Ren85fa3c62022-02-26 23:03:34 -080088 "raa229126",
Konstantin Aladyshev21e12c32022-10-19 18:56:10 +030089 "tps53679",
Ed Tanousa2df7862021-12-07 16:30:27 -080090 "tps546d24",
91 "xdpe12284"
92})};
93//clang-format on
Josh Lehan0830c7b2019-10-08 16:35:09 -070094
Cheng C Yang209ec562019-03-12 16:37:44 +080095namespace fs = std::filesystem;
96
Yong Libf8b1da2020-04-15 16:32:50 +080097static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +080098 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +080099static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
100 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800101static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
102 pwmSensors;
103static boost::container::flat_map<std::string, std::string> sensorTable;
104static boost::container::flat_map<std::string, PSUProperty> labelMatch;
105static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800106static boost::container::flat_map<std::string, std::vector<std::string>>
107 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800108static boost::container::flat_map<
109 std::string,
110 boost::container::flat_map<std::string, std::vector<std::string>>>
111 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800112static boost::container::flat_map<std::string, std::vector<std::string>>
113 limitEventMatch;
114
Josh Lehan74d9bd92019-10-31 08:51:58 -0700115static std::vector<PSUProperty> psuProperties;
116
Cheng C Yang58b2b532019-05-31 00:19:45 +0800117// Function CheckEvent will check each attribute from eventMatch table in the
118// sysfs. If the attributes exists in sysfs, then store the complete path
119// of the attribute into eventPathList.
120void checkEvent(
121 const std::string& directory,
122 const boost::container::flat_map<std::string, std::vector<std::string>>&
123 eventMatch,
124 boost::container::flat_map<std::string, std::vector<std::string>>&
125 eventPathList)
126{
127 for (const auto& match : eventMatch)
128 {
129 const std::vector<std::string>& eventAttrs = match.second;
130 const std::string& eventName = match.first;
131 for (const auto& eventAttr : eventAttrs)
132 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700133 std::string eventPath = directory;
134 eventPath += "/";
135 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800136
137 std::ifstream eventFile(eventPath);
138 if (!eventFile.good())
139 {
140 continue;
141 }
142
143 eventPathList[eventName].push_back(eventPath);
144 }
145 }
146}
147
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800148// Check Group Events which contains more than one targets in each combine
149// events.
150void checkGroupEvent(
151 const std::string& directory,
152 const boost::container::flat_map<
153 std::string,
154 boost::container::flat_map<std::string, std::vector<std::string>>>&
155 groupEventMatch,
156 boost::container::flat_map<
157 std::string,
158 boost::container::flat_map<std::string, std::vector<std::string>>>&
159 groupEventPathList)
160{
161 for (const auto& match : groupEventMatch)
162 {
163 const std::string& groupEventName = match.first;
164 const boost::container::flat_map<std::string, std::vector<std::string>>
165 events = match.second;
166 boost::container::flat_map<std::string, std::vector<std::string>>
167 pathList;
168 for (const auto& match : events)
169 {
170 const std::string& eventName = match.first;
171 const std::vector<std::string>& eventAttrs = match.second;
172 for (const auto& eventAttr : eventAttrs)
173 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700174 std::string eventPath = directory;
175 eventPath += "/";
176 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800177 std::ifstream eventFile(eventPath);
178 if (!eventFile.good())
179 {
180 continue;
181 }
182
183 pathList[eventName].push_back(eventPath);
184 }
185 }
186 groupEventPathList[groupEventName] = pathList;
187 }
188}
189
Cheng C Yang58b2b532019-05-31 00:19:45 +0800190// Function checkEventLimits will check all the psu related xxx_input attributes
191// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
192// xxx_min_alarm exist, then store the existing paths of the alarm attributes
193// to eventPathList.
194void checkEventLimits(
195 const std::string& sensorPathStr,
196 const boost::container::flat_map<std::string, std::vector<std::string>>&
197 limitEventMatch,
198 boost::container::flat_map<std::string, std::vector<std::string>>&
199 eventPathList)
200{
Lei YUa2c7cea2020-12-23 14:07:28 +0800201 auto attributePartPos = sensorPathStr.find_last_of('_');
202 if (attributePartPos == std::string::npos)
203 {
204 // There is no '_' in the string, skip it
205 return;
206 }
207 auto attributePart =
208 std::string_view(sensorPathStr).substr(attributePartPos + 1);
209 if (attributePart != "input")
210 {
211 // If the sensor is not xxx_input, skip it
212 return;
213 }
214
215 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800216 for (const auto& limitMatch : limitEventMatch)
217 {
218 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
219 const std::string& eventName = limitMatch.first;
220 for (const auto& limitEventAttr : limitEventAttrs)
221 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800222 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800223 std::ifstream eventFile(limitEventPath);
224 if (!eventFile.good())
225 {
226 continue;
227 }
228 eventPathList[eventName].push_back(limitEventPath);
229 }
230 }
231}
Cheng C Yang916360b2019-05-07 18:47:16 +0800232
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530233static void
234 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
235 const std::string& interfacePath,
236 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
237 sdbusplus::asio::object_server& objectServer,
238 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800239{
Zev Weiss741f26e2022-08-12 18:21:02 -0700240 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800241 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700242 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800243 {
244 continue;
245 }
246
247 const std::string& sensorPathStr = sensorPath.string();
248 const std::string& pwmPathStr =
249 boost::replace_all_copy(sensorPathStr, "input", "target");
250 std::ifstream pwmFile(pwmPathStr);
251 if (!pwmFile.good())
252 {
253 continue;
254 }
255
256 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
257 if (findPWMSensor != pwmSensors.end())
258 {
259 continue;
260 }
261
Zev Weissd8c293a2022-08-15 18:58:41 -0700262 std::string name = "Pwm_";
263 name += psuName;
264 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700265 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700266
267 std::string objPath = interfacePath;
268 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700269 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700270
Cheng C Yang916360b2019-05-07 18:47:16 +0800271 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700272 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800273 }
274}
275
Zhikui Ren23c96e72020-11-05 22:32:28 -0800276static void createSensorsCallback(
277 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
278 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
279 const ManagedObjectType& sensorConfigs,
280 const std::shared_ptr<boost::container::flat_set<std::string>>&
281 sensorsChanged)
Cheng C Yang209ec562019-03-12 16:37:44 +0800282{
Josh Lehan49cfba92019-10-08 16:50:42 -0700283 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800284 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800285
286 std::vector<fs::path> pmbusPaths;
287 if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
288 {
289 std::cerr << "No PSU sensors in system\n";
290 return;
291 }
292
293 boost::container::flat_set<std::string> directories;
294 for (const auto& pmbusPath : pmbusPaths)
295 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800296 boost::container::flat_map<std::string, std::vector<std::string>>
297 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800298 boost::container::flat_map<
299 std::string,
300 boost::container::flat_map<std::string, std::vector<std::string>>>
301 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800302
303 std::ifstream nameFile(pmbusPath);
304 if (!nameFile.good())
305 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700306 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800307 continue;
308 }
309
310 std::string pmbusName;
311 std::getline(nameFile, pmbusName);
312 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700313
314 if (std::find(pmbusNames.begin(), pmbusNames.end(), pmbusName) ==
315 pmbusNames.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800316 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700317 // To avoid this error message, add your driver name to
318 // the pmbusNames vector at the top of this file.
319 std::cerr << "Driver name " << pmbusName
320 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800321 continue;
322 }
323
Cheng C Yang209ec562019-03-12 16:37:44 +0800324 auto directory = pmbusPath.parent_path();
325
326 auto ret = directories.insert(directory.string());
327 if (!ret.second)
328 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700329 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800330 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800331 }
332
James Feistb6c0b912019-07-09 12:21:44 -0700333 fs::path device = directory / "device";
Cheng C Yang209ec562019-03-12 16:37:44 +0800334 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700335 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800336 if (findHyphen == std::string::npos)
337 {
338 std::cerr << "found bad device" << deviceName << "\n";
339 continue;
340 }
341 std::string busStr = deviceName.substr(0, findHyphen);
342 std::string addrStr = deviceName.substr(findHyphen + 1);
343
344 size_t bus = 0;
345 size_t addr = 0;
346
347 try
348 {
349 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700350 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800351 }
Patrick Williams26601e82021-10-06 12:43:25 -0500352 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800353 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700354 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
355 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800356 continue;
357 }
358
Zev Weisse8b97ee2022-08-12 15:23:51 -0700359 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800360 const SensorData* sensorData = nullptr;
361 const std::string* interfacePath = nullptr;
362 const char* sensorType = nullptr;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800363 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800364
Zev Weiss741f26e2022-08-12 18:21:02 -0700365 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800366 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700367 sensorData = &cfgData;
Cheng C Yang209ec562019-03-12 16:37:44 +0800368 for (const char* type : sensorTypes)
369 {
Zev Weiss054aad82022-08-18 01:37:34 -0700370 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800371 if (sensorBase != sensorData->end())
372 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700373 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800374 sensorType = type;
375 break;
376 }
377 }
378 if (baseConfig == nullptr)
379 {
380 std::cerr << "error finding base configuration for "
381 << deviceName << "\n";
382 continue;
383 }
384
Zev Weisse8b97ee2022-08-12 15:23:51 -0700385 auto configBus = baseConfig->find("Bus");
386 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800387
Zev Weisse8b97ee2022-08-12 15:23:51 -0700388 if (configBus == baseConfig->end() ||
389 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800390 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800391 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800392 continue;
393 }
394
Ed Tanousa771f6a2022-01-14 09:36:51 -0800395 const uint64_t* confBus = std::get_if<uint64_t>(&(configBus->second));
396 const uint64_t* confAddr = std::get_if<uint64_t>(&(configAddress->second));
397 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800398 {
399 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700400 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800401 continue;
402 }
403
404 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800405 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700406 std::cerr << "Configuration skipping " << *confBus << "-"
407 << *confAddr << " because not " << bus << "-" << addr
408 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800409 continue;
410 }
411
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800412 std::vector<thresholds::Threshold> confThresholds;
413 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
414 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800415 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800416 }
417 thresholdConfSize = confThresholds.size();
418
Zev Weiss741f26e2022-08-12 18:21:02 -0700419 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800420 break;
421 }
422 if (interfacePath == nullptr)
423 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700424 // To avoid this error message, add your export map entry,
425 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800426 std::cerr << "failed to find match for " << deviceName << "\n";
427 continue;
428 }
429
Zev Weisse8b97ee2022-08-12 15:23:51 -0700430 auto findPSUName = baseConfig->find("Name");
431 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800432 {
433 std::cerr << "could not determine configuration name for "
434 << deviceName << "\n";
435 continue;
436 }
Ed Tanousa771f6a2022-01-14 09:36:51 -0800437 const std::string* psuName = std::get_if<std::string>(&(findPSUName->second));
438 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800439 {
440 std::cerr << "Cannot find psu name, invalid configuration\n";
441 continue;
442 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800443
444 // on rescans, only update sensors we were signaled by
445 if (!firstScan)
446 {
Zhikui Renda98f092021-11-01 09:41:08 -0700447 std::string psuNameStr = "/" + escapeName(*psuName);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800448 auto it =
449 std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
450 [psuNameStr](std::string& s) {
Zev Weiss6c106d62022-08-17 20:50:00 -0700451 return s.ends_with(psuNameStr);
Zhikui Ren23c96e72020-11-05 22:32:28 -0800452 });
453
454 if (it == sensorsChanged->end())
455 {
456 continue;
457 }
458 sensorsChanged->erase(it);
459 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800460 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800461 checkGroupEvent(directory.string(), groupEventMatch,
462 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800463
Zev Weisse8b97ee2022-08-12 15:23:51 -0700464 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000465
Vijay Khemka996bad12019-05-28 15:15:16 -0700466 /* Check if there are more sensors in the same interface */
467 int i = 1;
468 std::vector<std::string> psuNames;
469 do
470 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700471 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700472 psuNames.push_back(
473 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700474 findPSUName = baseConfig->find("Name" + std::to_string(i++));
475 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700476
Cheng C Yange50345b2019-04-02 17:26:15 +0800477 std::vector<fs::path> sensorPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700478 if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800479 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800480 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800481 continue;
482 }
483
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530484 /* read max value in sysfs for in, curr, power, temp, ... */
485 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
486 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700487 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530488 {
489 std::cerr << "No max name in PSU \n";
490 }
491 }
492
Zev Weiss8569bf22022-10-11 15:37:44 -0700493 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800494
Vijay Khemka996bad12019-05-28 15:15:16 -0700495 /* Find array of labels to be exposed if it is defined in config */
496 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700497 auto findLabelObj = baseConfig->find("Labels");
498 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700499 {
500 findLabels =
501 std::get<std::vector<std::string>>(findLabelObj->second);
502 }
503
Jason Ling5747fab2019-10-02 16:46:23 -0700504 std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_");
505 std::smatch matches;
506
Cheng C Yange50345b2019-04-02 17:26:15 +0800507 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800508 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530509 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800510 std::string labelHead;
511 std::string sensorPathStr = sensorPath.string();
512 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700513 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700514 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
515 {
Josh Lehan06494452019-10-31 09:49:16 -0700516 // hwmon *_input filename without number:
517 // in, curr, power, temp, ...
Jason Ling5747fab2019-10-02 16:46:23 -0700518 sensorNameSubStr = matches[1];
519 }
520 else
521 {
Josh Lehan06494452019-10-31 09:49:16 -0700522 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700523 << sensorNameStr;
524 continue;
525 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800526
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530527 std::string labelPath;
528
529 /* find and differentiate _max and _input to replace "label" */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700530 size_t pos = sensorPathStr.find('_');
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530531 if (pos != std::string::npos)
532 {
533
534 std::string sensorPathStrMax = sensorPathStr.substr(pos);
Ed Tanous2049bd22022-07-09 07:20:26 -0700535 if (sensorPathStrMax == "_max")
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530536 {
537 labelPath =
538 boost::replace_all_copy(sensorPathStr, "max", "label");
539 maxLabel = true;
540 }
541 else
542 {
543 labelPath = boost::replace_all_copy(sensorPathStr, "input",
544 "label");
545 maxLabel = false;
546 }
547 }
548 else
549 {
550 continue;
551 }
552
Cheng C Yangecba9de2019-09-12 23:41:50 +0800553 std::ifstream labelFile(labelPath);
554 if (!labelFile.good())
Cheng C Yang209ec562019-03-12 16:37:44 +0800555 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700556 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800557 {
558 std::cerr << "Input file " << sensorPath
559 << " has no corresponding label file\n";
560 }
Josh Lehan06494452019-10-31 09:49:16 -0700561 // hwmon *_input filename with number:
562 // temp1, temp2, temp3, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700563 labelHead = sensorNameStr.substr(0, sensorNameStr.find('_'));
Cheng C Yang209ec562019-03-12 16:37:44 +0800564 }
565 else
566 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800567 std::string label;
568 std::getline(labelFile, label);
569 labelFile.close();
Cheng C Yange50345b2019-04-02 17:26:15 +0800570 auto findSensor = sensors.find(label);
571 if (findSensor != sensors.end())
572 {
573 continue;
574 }
575
Josh Lehan06494452019-10-31 09:49:16 -0700576 // hwmon corresponding *_label file contents:
577 // vin1, vout1, ...
Ed Tanous8a57ec02020-10-09 12:46:52 -0700578 labelHead = label.substr(0, label.find(' '));
Cheng C Yange50345b2019-04-02 17:26:15 +0800579 }
580
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530581 /* append "max" for labelMatch */
582 if (maxLabel)
583 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700584 labelHead.insert(0, "max");
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530585 }
586
Ed Tanous8a57ec02020-10-09 12:46:52 -0700587 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700588 {
589 std::cerr << "Sensor type=\"" << sensorNameSubStr
590 << "\" label=\"" << labelHead << "\"\n";
591 }
592
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530593 checkPWMSensor(sensorPath, labelHead, *interfacePath,
594 dbusConnection, objectServer, psuNames[0]);
Cheng C Yang916360b2019-05-07 18:47:16 +0800595
Vijay Khemka996bad12019-05-28 15:15:16 -0700596 if (!findLabels.empty())
597 {
598 /* Check if this labelHead is enabled in config file */
599 if (std::find(findLabels.begin(), findLabels.end(),
600 labelHead) == findLabels.end())
601 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700602 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800603 {
604 std::cerr << "could not find " << labelHead
605 << " in the Labels list\n";
606 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700607 continue;
608 }
609 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800610
Cheng C Yange50345b2019-04-02 17:26:15 +0800611 auto findProperty = labelMatch.find(labelHead);
612 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800613 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700614 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700615 {
616 std::cerr << "Could not find matching default property for "
617 << labelHead << "\n";
618 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800619 continue;
620 }
621
Josh Lehan74d9bd92019-10-31 08:51:58 -0700622 // Protect the hardcoded labelMatch list from changes,
623 // by making a copy and modifying that instead.
624 // Avoid bleedthrough of one device's customizations to
625 // the next device, as each should be independently customizable.
626 psuProperties.push_back(findProperty->second);
627 auto psuProperty = psuProperties.rbegin();
628
629 // Use label head as prefix for reading from config file,
630 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
631 std::string keyName = labelHead + "_Name";
632 std::string keyScale = labelHead + "_Scale";
633 std::string keyMin = labelHead + "_Min";
634 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800635 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800636 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700637
638 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700639 auto findCustomName = baseConfig->find(keyName);
640 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700641 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700642 try
643 {
644 psuProperty->labelTypeName = std::visit(
645 VariantToStringVisitor(), findCustomName->second);
646 }
Patrick Williams26601e82021-10-06 12:43:25 -0500647 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700648 {
649 std::cerr << "Unable to parse " << keyName << "\n";
650 continue;
651 }
652
653 // All strings are valid, including empty string
654 customizedName = true;
655 }
656
657 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700658 auto findCustomScale = baseConfig->find(keyScale);
659 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700660 {
661 try
662 {
663 psuProperty->sensorScaleFactor = std::visit(
664 VariantToUnsignedIntVisitor(), findCustomScale->second);
665 }
Patrick Williams26601e82021-10-06 12:43:25 -0500666 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700667 {
668 std::cerr << "Unable to parse " << keyScale << "\n";
669 continue;
670 }
671
672 // Avoid later division by zero
673 if (psuProperty->sensorScaleFactor > 0)
674 {
675 customizedScale = true;
676 }
677 else
678 {
679 std::cerr << "Unable to accept " << keyScale << "\n";
680 continue;
681 }
682 }
683
Zev Weisse8b97ee2022-08-12 15:23:51 -0700684 auto findCustomMin = baseConfig->find(keyMin);
685 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700686 {
687 try
688 {
689 psuProperty->minReading = std::visit(
690 VariantToDoubleVisitor(), findCustomMin->second);
691 }
Patrick Williams26601e82021-10-06 12:43:25 -0500692 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700693 {
694 std::cerr << "Unable to parse " << keyMin << "\n";
695 continue;
696 }
697 }
698
Zev Weisse8b97ee2022-08-12 15:23:51 -0700699 auto findCustomMax = baseConfig->find(keyMax);
700 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700701 {
702 try
703 {
704 psuProperty->maxReading = std::visit(
705 VariantToDoubleVisitor(), findCustomMax->second);
706 }
Patrick Williams26601e82021-10-06 12:43:25 -0500707 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700708 {
709 std::cerr << "Unable to parse " << keyMax << "\n";
710 continue;
711 }
712 }
713
Zev Weisse8b97ee2022-08-12 15:23:51 -0700714 auto findCustomOffset = baseConfig->find(keyOffset);
715 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800716 {
717 try
718 {
719 psuProperty->sensorOffset = std::visit(
720 VariantToDoubleVisitor(), findCustomOffset->second);
721 }
Patrick Williams26601e82021-10-06 12:43:25 -0500722 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800723 {
724 std::cerr << "Unable to parse " << keyOffset << "\n";
725 continue;
726 }
727 }
728
Lotus Xucb5af732021-09-10 15:18:50 +0800729 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700730 auto findPowerState = baseConfig->find(keyPowerState);
731 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800732 {
733 std::string powerState = std::visit(VariantToStringVisitor(),
734 findPowerState->second);
735 setReadState(powerState, readState);
736 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700737 if (!(psuProperty->minReading < psuProperty->maxReading))
738 {
739 std::cerr << "Min must be less than Max\n";
740 continue;
741 }
742
743 // If the sensor name is being customized by config file,
744 // then prefix/suffix composition becomes not necessary,
745 // and in fact not wanted, because it gets in the way.
746 std::string psuNameFromIndex;
747 if (!customizedName)
748 {
749 /* Find out sensor name index for this label */
750 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500751 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700752 if (std::regex_search(labelHead, matches, rgx))
753 {
754 nameIndex = std::stoi(matches[1]);
755
756 // Decrement to preserve alignment, because hwmon
757 // human-readable filenames and labels use 1-based
758 // numbering, but the "Name", "Name1", "Name2", etc. naming
759 // convention (the psuNames vector) uses 0-based numbering.
760 if (nameIndex > 0)
761 {
762 --nameIndex;
763 }
764 }
765 else
766 {
767 nameIndex = 0;
768 }
769
770 if (psuNames.size() <= nameIndex)
771 {
772 std::cerr << "Could not pair " << labelHead
773 << " with a Name field\n";
774 continue;
775 }
776
777 psuNameFromIndex = psuNames[nameIndex];
778
Ed Tanous8a57ec02020-10-09 12:46:52 -0700779 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700780 {
781 std::cerr << "Sensor label head " << labelHead
782 << " paired with " << psuNameFromIndex
783 << " at index " << nameIndex << "\n";
784 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700785 }
786
Cheng C Yang58b2b532019-05-31 00:19:45 +0800787 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
788
Josh Lehan74d9bd92019-10-31 08:51:58 -0700789 // Similarly, if sensor scaling factor is being customized,
790 // then the below power-of-10 constraint becomes unnecessary,
791 // as config should be able to specify an arbitrary divisor.
792 unsigned int factor = psuProperty->sensorScaleFactor;
793 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700794 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700795 // Preserve existing usage of hardcoded labelMatch table below
796 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700797
Josh Lehan74d9bd92019-10-31 08:51:58 -0700798 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700799 char firstChar =
800 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700801 std::string strScaleFactor =
802 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
803
804 // Preserve existing configs by accepting earlier syntax,
805 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700806 auto findScaleFactor = baseConfig->find(strScaleFactor);
807 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700808 {
809 factor = std::visit(VariantToIntVisitor(),
810 findScaleFactor->second);
811 }
812
Ed Tanous8a57ec02020-10-09 12:46:52 -0700813 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700814 {
815 std::cerr << "Sensor scaling factor " << factor
816 << " string " << strScaleFactor << "\n";
817 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700818 }
819
Vijay Khemka996bad12019-05-28 15:15:16 -0700820 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530821 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
822 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800823 {
James Feist17ab6e02019-06-25 12:28:13 -0700824 std::cerr << "error populating thresholds for "
825 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800826 }
827
Zev Weiss6b6891c2021-04-22 02:46:21 -0500828 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
829 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800830 {
Jason Ling5747fab2019-10-02 16:46:23 -0700831 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700832 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800833 continue;
834 }
835
Ed Tanous8a57ec02020-10-09 12:46:52 -0700836 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700837 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700838 std::cerr << "Sensor properties: Name \""
839 << psuProperty->labelTypeName << "\" Scale "
840 << psuProperty->sensorScaleFactor << " Min "
841 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800842 << psuProperty->maxReading << " Offset "
843 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700844 }
845
846 std::string sensorName = psuProperty->labelTypeName;
847 if (customizedName)
848 {
849 if (sensorName.empty())
850 {
851 // Allow selective disabling of an individual sensor,
852 // by customizing its name to an empty string.
853 std::cerr << "Sensor disabled, empty string\n";
854 continue;
855 }
856 }
857 else
858 {
859 // Sensor name not customized, do prefix/suffix composition,
860 // preserving default behavior by using psuNameFromIndex.
861 sensorName =
862 psuNameFromIndex + " " + psuProperty->labelTypeName;
863 }
864
Ed Tanous8a57ec02020-10-09 12:46:52 -0700865 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700866 {
867 std::cerr << "Sensor name \"" << sensorName << "\" path \""
868 << sensorPathStr << "\" type \"" << sensorType
869 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700870 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800871 // destruct existing one first if already created
872 sensors[sensorName] = nullptr;
Yong Libf8b1da2020-04-15 16:32:50 +0800873 sensors[sensorName] = std::make_shared<PSUSensor>(
Cheng C Yange50345b2019-04-02 17:26:15 +0800874 sensorPathStr, sensorType, objectServer, dbusConnection, io,
Cheng C Yang209ec562019-03-12 16:37:44 +0800875 sensorName, std::move(sensorThresholds), *interfacePath,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000876 readState, findSensorUnit->second, factor,
877 psuProperty->maxReading, psuProperty->minReading,
878 psuProperty->sensorOffset, labelHead, thresholdConfSize,
879 pollRate);
Yong Libf8b1da2020-04-15 16:32:50 +0800880 sensors[sensorName]->setupRead();
Josh Lehan74d9bd92019-10-31 08:51:58 -0700881 ++numCreated;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700882 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700883 {
884 std::cerr << "Created " << numCreated << " sensors so far\n";
885 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800886 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800887
888 // OperationalStatus event
Cheng C Yang92498eb2019-09-26 21:59:25 +0800889 combineEvents[*psuName + "OperationalStatus"] = nullptr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800890 combineEvents[*psuName + "OperationalStatus"] =
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000891 std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io,
892 *psuName, readState,
893 eventPathList, groupEventPathList,
894 "OperationalStatus", pollRate);
Cheng C Yang209ec562019-03-12 16:37:44 +0800895 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700896
Ed Tanous8a57ec02020-10-09 12:46:52 -0700897 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700898 {
899 std::cerr << "Created total of " << numCreated << " sensors\n";
900 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700901 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800902
Zhikui Ren23c96e72020-11-05 22:32:28 -0800903void createSensors(
904 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
905 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
906 const std::shared_ptr<boost::container::flat_set<std::string>>&
907 sensorsChanged)
908{
909 auto getter = std::make_shared<GetSensorConfiguration>(
910 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
911 const ManagedObjectType& sensorConfigs) {
912 createSensorsCallback(io, objectServer, dbusConnection,
913 sensorConfigs, sensorsChanged);
914 });
915 getter->getConfiguration(
916 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
917}
918
Cheng C Yang916360b2019-05-07 18:47:16 +0800919void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +0800920{
Zev Weiss6b6891c2021-04-22 02:46:21 -0500921 sensorTable = {{"power", sensor_paths::unitWatts},
922 {"curr", sensor_paths::unitAmperes},
923 {"temp", sensor_paths::unitDegreesC},
924 {"in", sensor_paths::unitVolts},
925 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +0800926
Jeff Line41d52f2021-04-07 19:38:51 +0800927 labelMatch = {
928 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800929 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
930 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800931 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
932 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
933 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
934 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800935 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
936 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
937 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800938 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
939 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
940 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
941 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
942 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
943 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
944 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
945 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
946 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
947 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
948 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
949 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
950 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
951 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
952 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
953 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
954 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
955 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
956 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
957 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
958 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
959 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
960 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
961 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
962 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
963 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
964 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
965 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
966 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
967 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
968 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
969 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
970 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
971 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
972 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
973 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +0800974 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800975 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +0800976 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
977 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
978 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
979 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
980 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
981 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800982 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800983 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
984 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +0800985 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
986 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
987 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
988 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
989 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
990 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
991 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
992 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
993 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
994 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
995 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
996 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
997 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
998 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
999 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001000 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1001 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1002 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001003 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1004 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1005 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1006 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1007 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1008 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1009 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1010 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1011 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
1012 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001013
1014 pwmTable = {{"fan1", "Fan_1"}, {"fan2", "Fan_2"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001015
1016 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1017 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1018
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001019 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1020 {"Failure", {"in2_alarm"}},
1021 {"ACLost", {"in1_beep"}},
1022 {"ConfigureError", {"in1_fault"}}};
1023
1024 groupEventMatch = {{"FanFault",
1025 {{"fan1", {"fan1_alarm", "fan1_fault"}},
1026 {"fan2", {"fan2_alarm", "fan2_fault"}}}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001027}
1028
James Feistb6c0b912019-07-09 12:21:44 -07001029int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001030{
1031 boost::asio::io_service io;
1032 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1033
Ed Tanous14ed5e92022-07-12 15:50:23 -07001034 sdbusplus::asio::object_server objectServer(systemBus, true);
1035 objectServer.add_manager("/xyz/openbmc_project/sensors");
Cheng C Yang209ec562019-03-12 16:37:44 +08001036 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001037 auto sensorsChanged =
1038 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001039
Cheng C Yang916360b2019-05-07 18:47:16 +08001040 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001041
Zhikui Ren23c96e72020-11-05 22:32:28 -08001042 io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001043 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001044 std::function<void(sdbusplus::message_t&)> eventHandler =
1045 [&](sdbusplus::message_t& message) {
Cheng C Yang209ec562019-03-12 16:37:44 +08001046 if (message.is_method_error())
1047 {
1048 std::cerr << "callback method error\n";
1049 return;
1050 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001051 sensorsChanged->insert(message.get_path());
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001052 filterTimer.expires_from_now(std::chrono::seconds(3));
Cheng C Yang209ec562019-03-12 16:37:44 +08001053 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1054 if (ec == boost::asio::error::operation_aborted)
1055 {
1056 return;
1057 }
Ed Tanous8a57ec02020-10-09 12:46:52 -07001058 if (ec)
Cheng C Yang209ec562019-03-12 16:37:44 +08001059 {
1060 std::cerr << "timer error\n";
1061 }
Zhikui Ren23c96e72020-11-05 22:32:28 -08001062 createSensors(io, objectServer, systemBus, sensorsChanged);
Cheng C Yang209ec562019-03-12 16:37:44 +08001063 });
1064 };
1065
Zev Weiss214d9712022-08-12 12:54:31 -07001066 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1067 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +08001068 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001069 io.run();
1070}