blob: 663567dc9310bacd4ff80ecdab053c21760beaa2 [file] [log] [blame]
Cheng C Yang209ec562019-03-12 16:37:44 +08001/*
2// Copyright (c) 2019 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
Josh Lehan0830c7b2019-10-08 16:35:09 -070016
Matt Simmering786efb82023-01-18 14:09:21 -080017#include "DeviceMgmt.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103018#include "PSUEvent.hpp"
19#include "PSUSensor.hpp"
20#include "Utils.hpp"
21
Matt Simmeringe4e6a282023-03-03 14:41:04 -080022#include <boost/algorithm/string/case_conv.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080023#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070024#include <boost/container/flat_map.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080025#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070026#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28#include <sdbusplus/bus/match.hpp>
29
30#include <array>
Josh Lehan74d9bd92019-10-31 08:51:58 -070031#include <cmath>
James Feist24f02f22019-04-15 11:05:39 -070032#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080033#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <functional>
Cheng C Yang58b2b532019-05-31 00:19:45 +080035#include <iostream>
Cheng C Yang209ec562019-03-12 16:37:44 +080036#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <string>
Lei YUa2c7cea2020-12-23 14:07:28 +080038#include <string_view>
Patrick Venture96e97db2019-10-31 13:44:38 -070039#include <utility>
40#include <variant>
41#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080042
Ed Tanous8a57ec02020-10-09 12:46:52 -070043static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070044
Matt Simmering786efb82023-01-18 14:09:21 -080045static const I2CDeviceTypeMap sensorTypes{
46 {"ADM1266", I2CDeviceType{"adm1266", true}},
47 {"ADM1272", I2CDeviceType{"adm1272", true}},
48 {"ADM1275", I2CDeviceType{"adm1275", true}},
49 {"ADM1278", I2CDeviceType{"adm1278", true}},
50 {"ADM1293", I2CDeviceType{"adm1293", true}},
Joseph Fub844e972023-03-25 10:24:30 +080051 {"ADS1015", I2CDeviceType{"ads1015", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080052 {"ADS7830", I2CDeviceType{"ads7830", true}},
53 {"AHE50DC_FAN", I2CDeviceType{"ahe50dc_fan", true}},
54 {"BMR490", I2CDeviceType{"bmr490", true}},
Alexander Hansen90ef14d2023-08-01 17:12:17 +020055 {"cffps", I2CDeviceType{"cffps", true}},
56 {"cffps1", I2CDeviceType{"cffps", true}},
57 {"cffps2", I2CDeviceType{"cffps", true}},
58 {"cffps3", I2CDeviceType{"cffps", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080059 {"DPS800", I2CDeviceType{"dps800", true}},
60 {"INA219", I2CDeviceType{"ina219", true}},
61 {"INA230", I2CDeviceType{"ina230", true}},
Jian Zhangaff100c2023-07-26 01:22:50 +080062 {"IPSPS1", I2CDeviceType{"ipsps1", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080063 {"IR38060", I2CDeviceType{"ir38060", true}},
64 {"IR38164", I2CDeviceType{"ir38164", true}},
65 {"IR38263", I2CDeviceType{"ir38263", true}},
66 {"ISL68137", I2CDeviceType{"isl68137", true}},
67 {"ISL68220", I2CDeviceType{"isl68220", true}},
68 {"ISL68223", I2CDeviceType{"isl68223", true}},
69 {"ISL69225", I2CDeviceType{"isl69225", true}},
70 {"ISL69243", I2CDeviceType{"isl69243", true}},
71 {"ISL69260", I2CDeviceType{"isl69260", true}},
72 {"LM25066", I2CDeviceType{"lm25066", true}},
73 {"MAX16601", I2CDeviceType{"max16601", true}},
74 {"MAX20710", I2CDeviceType{"max20710", true}},
75 {"MAX20730", I2CDeviceType{"max20730", true}},
76 {"MAX20734", I2CDeviceType{"max20734", true}},
77 {"MAX20796", I2CDeviceType{"max20796", true}},
78 {"MAX34451", I2CDeviceType{"max34451", true}},
79 {"MP2971", I2CDeviceType{"mp2971", true}},
80 {"MP2973", I2CDeviceType{"mp2973", true}},
81 {"MP2975", I2CDeviceType{"mp2975", true}},
82 {"MP5023", I2CDeviceType{"mp5023", true}},
83 {"NCP4200", I2CDeviceType{"ncp4200", true}},
84 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
85 {"pmbus", I2CDeviceType{"pmbus", true}},
86 {"PXE1610", I2CDeviceType{"pxe1610", true}},
87 {"RAA228000", I2CDeviceType{"raa228000", true}},
88 {"RAA228228", I2CDeviceType{"raa228228", true}},
89 {"RAA228620", I2CDeviceType{"raa228620", true}},
90 {"RAA229001", I2CDeviceType{"raa229001", true}},
91 {"RAA229004", I2CDeviceType{"raa229004", true}},
92 {"RAA229126", I2CDeviceType{"raa229126", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +020093 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080094 {"TPS53679", I2CDeviceType{"tps53679", true}},
95 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
96 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
97 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +080098 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080099};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700100
Joseph Fub844e972023-03-25 10:24:30 +0800101enum class DevTypes
102{
103 Unknown = 0,
104 HWMON,
105 IIO
106};
107
108struct DevParams
109{
110 unsigned int matchIndex = 0;
111 std::string matchRegEx;
112 std::string nameRegEx;
113};
114
Cheng C Yang209ec562019-03-12 16:37:44 +0800115namespace fs = std::filesystem;
116
Yong Libf8b1da2020-04-15 16:32:50 +0800117static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800118 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800119static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
120 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800121static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
122 pwmSensors;
123static boost::container::flat_map<std::string, std::string> sensorTable;
124static boost::container::flat_map<std::string, PSUProperty> labelMatch;
125static boost::container::flat_map<std::string, std::string> pwmTable;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800126static boost::container::flat_map<std::string, std::vector<std::string>>
127 eventMatch;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800128static boost::container::flat_map<
129 std::string,
130 boost::container::flat_map<std::string, std::vector<std::string>>>
131 groupEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800132static boost::container::flat_map<std::string, std::vector<std::string>>
133 limitEventMatch;
134
Josh Lehan74d9bd92019-10-31 08:51:58 -0700135static std::vector<PSUProperty> psuProperties;
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800136static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800137static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700138
Cheng C Yang58b2b532019-05-31 00:19:45 +0800139// Function CheckEvent will check each attribute from eventMatch table in the
140// sysfs. If the attributes exists in sysfs, then store the complete path
141// of the attribute into eventPathList.
142void checkEvent(
143 const std::string& directory,
144 const boost::container::flat_map<std::string, std::vector<std::string>>&
145 eventMatch,
146 boost::container::flat_map<std::string, std::vector<std::string>>&
147 eventPathList)
148{
149 for (const auto& match : eventMatch)
150 {
151 const std::vector<std::string>& eventAttrs = match.second;
152 const std::string& eventName = match.first;
153 for (const auto& eventAttr : eventAttrs)
154 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700155 std::string eventPath = directory;
156 eventPath += "/";
157 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800158
159 std::ifstream eventFile(eventPath);
160 if (!eventFile.good())
161 {
162 continue;
163 }
164
165 eventPathList[eventName].push_back(eventPath);
166 }
167 }
168}
169
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800170// Check Group Events which contains more than one targets in each combine
171// events.
172void checkGroupEvent(
173 const std::string& directory,
174 const boost::container::flat_map<
175 std::string,
176 boost::container::flat_map<std::string, std::vector<std::string>>>&
177 groupEventMatch,
178 boost::container::flat_map<
179 std::string,
180 boost::container::flat_map<std::string, std::vector<std::string>>>&
181 groupEventPathList)
182{
183 for (const auto& match : groupEventMatch)
184 {
185 const std::string& groupEventName = match.first;
186 const boost::container::flat_map<std::string, std::vector<std::string>>
187 events = match.second;
188 boost::container::flat_map<std::string, std::vector<std::string>>
189 pathList;
190 for (const auto& match : events)
191 {
192 const std::string& eventName = match.first;
193 const std::vector<std::string>& eventAttrs = match.second;
194 for (const auto& eventAttr : eventAttrs)
195 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700196 std::string eventPath = directory;
197 eventPath += "/";
198 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800199 std::ifstream eventFile(eventPath);
200 if (!eventFile.good())
201 {
202 continue;
203 }
204
205 pathList[eventName].push_back(eventPath);
206 }
207 }
208 groupEventPathList[groupEventName] = pathList;
209 }
210}
211
Cheng C Yang58b2b532019-05-31 00:19:45 +0800212// Function checkEventLimits will check all the psu related xxx_input attributes
213// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
214// xxx_min_alarm exist, then store the existing paths of the alarm attributes
215// to eventPathList.
216void checkEventLimits(
217 const std::string& sensorPathStr,
218 const boost::container::flat_map<std::string, std::vector<std::string>>&
219 limitEventMatch,
220 boost::container::flat_map<std::string, std::vector<std::string>>&
221 eventPathList)
222{
Lei YUa2c7cea2020-12-23 14:07:28 +0800223 auto attributePartPos = sensorPathStr.find_last_of('_');
224 if (attributePartPos == std::string::npos)
225 {
226 // There is no '_' in the string, skip it
227 return;
228 }
229 auto attributePart =
230 std::string_view(sensorPathStr).substr(attributePartPos + 1);
231 if (attributePart != "input")
232 {
233 // If the sensor is not xxx_input, skip it
234 return;
235 }
236
237 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800238 for (const auto& limitMatch : limitEventMatch)
239 {
240 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
241 const std::string& eventName = limitMatch.first;
242 for (const auto& limitEventAttr : limitEventAttrs)
243 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800244 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800245 std::ifstream eventFile(limitEventPath);
246 if (!eventFile.good())
247 {
248 continue;
249 }
250 eventPathList[eventName].push_back(limitEventPath);
251 }
252 }
253}
Cheng C Yang916360b2019-05-07 18:47:16 +0800254
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530255static void
256 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
257 const std::string& interfacePath,
258 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
259 sdbusplus::asio::object_server& objectServer,
260 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800261{
Zev Weiss741f26e2022-08-12 18:21:02 -0700262 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800263 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700264 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800265 {
266 continue;
267 }
268
269 const std::string& sensorPathStr = sensorPath.string();
270 const std::string& pwmPathStr =
271 boost::replace_all_copy(sensorPathStr, "input", "target");
272 std::ifstream pwmFile(pwmPathStr);
273 if (!pwmFile.good())
274 {
275 continue;
276 }
277
278 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
279 if (findPWMSensor != pwmSensors.end())
280 {
281 continue;
282 }
283
Zev Weissd8c293a2022-08-15 18:58:41 -0700284 std::string name = "Pwm_";
285 name += psuName;
286 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700287 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700288
289 std::string objPath = interfacePath;
290 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700291 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700292
Cheng C Yang916360b2019-05-07 18:47:16 +0800293 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700294 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800295 }
296}
297
Zhikui Ren23c96e72020-11-05 22:32:28 -0800298static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800299 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800300 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
301 const ManagedObjectType& sensorConfigs,
302 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800303 sensorsChanged,
304 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800305{
Josh Lehan49cfba92019-10-08 16:50:42 -0700306 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800307 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800308
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800309 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
310
Cheng C Yang209ec562019-03-12 16:37:44 +0800311 std::vector<fs::path> pmbusPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800312 findFiles(fs::path("/sys/bus/iio/devices"), "name", pmbusPaths);
313 findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths);
314 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800315 {
316 std::cerr << "No PSU sensors in system\n";
317 return;
318 }
319
320 boost::container::flat_set<std::string> directories;
321 for (const auto& pmbusPath : pmbusPaths)
322 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800323 boost::container::flat_map<std::string, std::vector<std::string>>
324 eventPathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800325 boost::container::flat_map<
326 std::string,
327 boost::container::flat_map<std::string, std::vector<std::string>>>
328 groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800329
330 std::ifstream nameFile(pmbusPath);
331 if (!nameFile.good())
332 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700333 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800334 continue;
335 }
336
337 std::string pmbusName;
338 std::getline(nameFile, pmbusName);
339 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700340
Matt Simmering786efb82023-01-18 14:09:21 -0800341 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800342 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700343 // To avoid this error message, add your driver name to
344 // the pmbusNames vector at the top of this file.
345 std::cerr << "Driver name " << pmbusName
346 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800347 continue;
348 }
349
Cheng C Yang209ec562019-03-12 16:37:44 +0800350 auto directory = pmbusPath.parent_path();
351
352 auto ret = directories.insert(directory.string());
353 if (!ret.second)
354 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700355 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800356 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800357 }
358
Joseph Fub844e972023-03-25 10:24:30 +0800359 DevTypes devType = DevTypes::HWMON;
360 std::string deviceName;
361 if (directory.parent_path() == "/sys/class/hwmon")
362 {
363 deviceName = fs::canonical(directory / "device").stem();
364 }
365 else
366 {
367 deviceName = fs::canonical(directory).parent_path().stem();
368 devType = DevTypes::IIO;
369 }
370
Ed Tanous8a57ec02020-10-09 12:46:52 -0700371 auto findHyphen = deviceName.find('-');
Cheng C Yang209ec562019-03-12 16:37:44 +0800372 if (findHyphen == std::string::npos)
373 {
374 std::cerr << "found bad device" << deviceName << "\n";
375 continue;
376 }
377 std::string busStr = deviceName.substr(0, findHyphen);
378 std::string addrStr = deviceName.substr(findHyphen + 1);
379
380 size_t bus = 0;
381 size_t addr = 0;
382
383 try
384 {
385 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700386 addr = std::stoi(addrStr, nullptr, 16);
Cheng C Yang209ec562019-03-12 16:37:44 +0800387 }
Patrick Williams26601e82021-10-06 12:43:25 -0500388 catch (const std::invalid_argument&)
Cheng C Yang209ec562019-03-12 16:37:44 +0800389 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700390 std::cerr << "Error parsing bus " << busStr << " addr " << addrStr
391 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800392 continue;
393 }
394
Zev Weisse8b97ee2022-08-12 15:23:51 -0700395 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800396 const SensorData* sensorData = nullptr;
397 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800398 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800399 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800400
Zev Weiss741f26e2022-08-12 18:21:02 -0700401 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800402 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700403 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800404 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800405 {
Zev Weiss054aad82022-08-18 01:37:34 -0700406 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800407 if (sensorBase != sensorData->end())
408 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700409 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800410 sensorType = type;
411 break;
412 }
413 }
414 if (baseConfig == nullptr)
415 {
416 std::cerr << "error finding base configuration for "
417 << deviceName << "\n";
418 continue;
419 }
420
Zev Weisse8b97ee2022-08-12 15:23:51 -0700421 auto configBus = baseConfig->find("Bus");
422 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800423
Zev Weisse8b97ee2022-08-12 15:23:51 -0700424 if (configBus == baseConfig->end() ||
425 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800426 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800427 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800428 continue;
429 }
430
Matt Simmering6747eba2023-01-18 16:03:55 -0800431 const uint64_t* confBus =
432 std::get_if<uint64_t>(&(configBus->second));
433 const uint64_t* confAddr =
434 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800435 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800436 {
437 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700438 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800439 continue;
440 }
441
442 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800443 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700444 std::cerr << "Configuration skipping " << *confBus << "-"
445 << *confAddr << " because not " << bus << "-" << addr
446 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800447 continue;
448 }
449
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800450 std::vector<thresholds::Threshold> confThresholds;
451 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
452 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800453 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800454 }
455 thresholdConfSize = confThresholds.size();
456
Zev Weiss741f26e2022-08-12 18:21:02 -0700457 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800458 break;
459 }
460 if (interfacePath == nullptr)
461 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700462 // To avoid this error message, add your export map entry,
463 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800464 std::cerr << "failed to find match for " << deviceName << "\n";
465 continue;
466 }
467
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800468 auto findI2CDev = devices.find(*interfacePath);
469
470 std::shared_ptr<I2CDevice> i2cDev;
471 if (findI2CDev != devices.end())
472 {
473 if (activateOnly && !findI2CDev->second.second)
474 {
475 continue;
476 }
477 i2cDev = findI2CDev->second.first;
478 }
479
Zev Weisse8b97ee2022-08-12 15:23:51 -0700480 auto findPSUName = baseConfig->find("Name");
481 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800482 {
483 std::cerr << "could not determine configuration name for "
484 << deviceName << "\n";
485 continue;
486 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800487 const std::string* psuName =
488 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800489 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800490 {
491 std::cerr << "Cannot find psu name, invalid configuration\n";
492 continue;
493 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800494
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800495 auto findCPU = baseConfig->find("CPURequired");
496 if (findCPU != baseConfig->end())
497 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500498 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800499 auto presenceFind = cpuPresence.find(index);
500 if (presenceFind == cpuPresence.end() || !presenceFind->second)
501 {
502 continue;
503 }
504 }
505
Zhikui Ren23c96e72020-11-05 22:32:28 -0800506 // on rescans, only update sensors we were signaled by
507 if (!firstScan)
508 {
Zhikui Renda98f092021-11-01 09:41:08 -0700509 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500510 auto it = std::find_if(sensorsChanged->begin(),
511 sensorsChanged->end(),
512 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800513 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500514 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800515
516 if (it == sensorsChanged->end())
517 {
518 continue;
519 }
520 sensorsChanged->erase(it);
521 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800522 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800523 checkGroupEvent(directory.string(), groupEventMatch,
524 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800525
Zev Weisse8b97ee2022-08-12 15:23:51 -0700526 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000527
Vijay Khemka996bad12019-05-28 15:15:16 -0700528 /* Check if there are more sensors in the same interface */
529 int i = 1;
530 std::vector<std::string> psuNames;
531 do
532 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700533 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700534 psuNames.push_back(
535 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700536 findPSUName = baseConfig->find("Name" + std::to_string(i++));
537 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700538
Cheng C Yange50345b2019-04-02 17:26:15 +0800539 std::vector<fs::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800540 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
541 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800542 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800543 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800544 continue;
545 }
546
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530547 /* read max value in sysfs for in, curr, power, temp, ... */
548 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
549 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700550 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530551 {
552 std::cerr << "No max name in PSU \n";
553 }
554 }
555
Zev Weiss8569bf22022-10-11 15:37:44 -0700556 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800557
Vijay Khemka996bad12019-05-28 15:15:16 -0700558 /* Find array of labels to be exposed if it is defined in config */
559 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700560 auto findLabelObj = baseConfig->find("Labels");
561 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700562 {
563 findLabels =
564 std::get<std::vector<std::string>>(findLabelObj->second);
565 }
566
Joseph Fub844e972023-03-25 10:24:30 +0800567 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700568 std::smatch matches;
569
Cheng C Yange50345b2019-04-02 17:26:15 +0800570 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800571 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530572 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800573 std::string labelHead;
574 std::string sensorPathStr = sensorPath.string();
575 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700576 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700577 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
578 {
Josh Lehan06494452019-10-31 09:49:16 -0700579 // hwmon *_input filename without number:
580 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800581 // iio in_*_raw filename without number:
582 // voltage, temp, pressure, ...
583 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700584 }
585 else
586 {
Josh Lehan06494452019-10-31 09:49:16 -0700587 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700588 << sensorNameStr;
589 continue;
590 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800591
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530592 std::string labelPath;
593
Joseph Fub844e972023-03-25 10:24:30 +0800594 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530595 {
Joseph Fub844e972023-03-25 10:24:30 +0800596 /* find and differentiate _max and _input to replace "label" */
597 size_t pos = sensorPathStr.find('_');
598 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530599 {
Joseph Fub844e972023-03-25 10:24:30 +0800600 std::string sensorPathStrMax = sensorPathStr.substr(pos);
601 if (sensorPathStrMax == "_max")
602 {
603 labelPath = boost::replace_all_copy(sensorPathStr,
604 "max", "label");
605 maxLabel = true;
606 }
607 else
608 {
609 labelPath = boost::replace_all_copy(sensorPathStr,
610 "input", "label");
611 maxLabel = false;
612 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530613 }
614 else
615 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800616 continue;
617 }
618
Joseph Fub844e972023-03-25 10:24:30 +0800619 std::ifstream labelFile(labelPath);
620 if (!labelFile.good())
621 {
622 if constexpr (debug)
623 {
624 std::cerr << "Input file " << sensorPath
625 << " has no corresponding label file\n";
626 }
627 // hwmon *_input filename with number:
628 // temp1, temp2, temp3, ...
629 labelHead = sensorNameStr.substr(0,
630 sensorNameStr.find('_'));
631 }
632 else
633 {
634 std::string label;
635 std::getline(labelFile, label);
636 labelFile.close();
637 auto findSensor = sensors.find(label);
638 if (findSensor != sensors.end())
639 {
640 continue;
641 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800642
Joseph Fub844e972023-03-25 10:24:30 +0800643 // hwmon corresponding *_label file contents:
644 // vin1, vout1, ...
645 labelHead = label.substr(0, label.find(' '));
646 }
647
648 /* append "max" for labelMatch */
649 if (maxLabel)
650 {
651 labelHead.insert(0, "max");
652 }
653
654 checkPWMSensor(sensorPath, labelHead, *interfacePath,
655 dbusConnection, objectServer, psuNames[0]);
656 }
657 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530658 {
Joseph Fub844e972023-03-25 10:24:30 +0800659 auto findIIOHyphen = sensorNameStr.find_last_of('_');
660 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530661 }
662
Ed Tanous8a57ec02020-10-09 12:46:52 -0700663 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700664 {
665 std::cerr << "Sensor type=\"" << sensorNameSubStr
666 << "\" label=\"" << labelHead << "\"\n";
667 }
668
Vijay Khemka996bad12019-05-28 15:15:16 -0700669 if (!findLabels.empty())
670 {
671 /* Check if this labelHead is enabled in config file */
672 if (std::find(findLabels.begin(), findLabels.end(),
673 labelHead) == findLabels.end())
674 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700675 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800676 {
677 std::cerr << "could not find " << labelHead
678 << " in the Labels list\n";
679 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700680 continue;
681 }
682 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800683
Cheng C Yange50345b2019-04-02 17:26:15 +0800684 auto findProperty = labelMatch.find(labelHead);
685 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800686 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700687 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700688 {
689 std::cerr << "Could not find matching default property for "
690 << labelHead << "\n";
691 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800692 continue;
693 }
694
Josh Lehan74d9bd92019-10-31 08:51:58 -0700695 // Protect the hardcoded labelMatch list from changes,
696 // by making a copy and modifying that instead.
697 // Avoid bleedthrough of one device's customizations to
698 // the next device, as each should be independently customizable.
699 psuProperties.push_back(findProperty->second);
700 auto psuProperty = psuProperties.rbegin();
701
702 // Use label head as prefix for reading from config file,
703 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
704 std::string keyName = labelHead + "_Name";
705 std::string keyScale = labelHead + "_Scale";
706 std::string keyMin = labelHead + "_Min";
707 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800708 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800709 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700710
711 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700712 auto findCustomName = baseConfig->find(keyName);
713 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700714 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700715 try
716 {
717 psuProperty->labelTypeName = std::visit(
718 VariantToStringVisitor(), findCustomName->second);
719 }
Patrick Williams26601e82021-10-06 12:43:25 -0500720 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700721 {
722 std::cerr << "Unable to parse " << keyName << "\n";
723 continue;
724 }
725
726 // All strings are valid, including empty string
727 customizedName = true;
728 }
729
730 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700731 auto findCustomScale = baseConfig->find(keyScale);
732 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700733 {
734 try
735 {
736 psuProperty->sensorScaleFactor = std::visit(
737 VariantToUnsignedIntVisitor(), findCustomScale->second);
738 }
Patrick Williams26601e82021-10-06 12:43:25 -0500739 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700740 {
741 std::cerr << "Unable to parse " << keyScale << "\n";
742 continue;
743 }
744
745 // Avoid later division by zero
746 if (psuProperty->sensorScaleFactor > 0)
747 {
748 customizedScale = true;
749 }
750 else
751 {
752 std::cerr << "Unable to accept " << keyScale << "\n";
753 continue;
754 }
755 }
756
Zev Weisse8b97ee2022-08-12 15:23:51 -0700757 auto findCustomMin = baseConfig->find(keyMin);
758 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700759 {
760 try
761 {
762 psuProperty->minReading = std::visit(
763 VariantToDoubleVisitor(), findCustomMin->second);
764 }
Patrick Williams26601e82021-10-06 12:43:25 -0500765 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700766 {
767 std::cerr << "Unable to parse " << keyMin << "\n";
768 continue;
769 }
770 }
771
Zev Weisse8b97ee2022-08-12 15:23:51 -0700772 auto findCustomMax = baseConfig->find(keyMax);
773 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700774 {
775 try
776 {
777 psuProperty->maxReading = std::visit(
778 VariantToDoubleVisitor(), findCustomMax->second);
779 }
Patrick Williams26601e82021-10-06 12:43:25 -0500780 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700781 {
782 std::cerr << "Unable to parse " << keyMax << "\n";
783 continue;
784 }
785 }
786
Zev Weisse8b97ee2022-08-12 15:23:51 -0700787 auto findCustomOffset = baseConfig->find(keyOffset);
788 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800789 {
790 try
791 {
792 psuProperty->sensorOffset = std::visit(
793 VariantToDoubleVisitor(), findCustomOffset->second);
794 }
Patrick Williams26601e82021-10-06 12:43:25 -0500795 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800796 {
797 std::cerr << "Unable to parse " << keyOffset << "\n";
798 continue;
799 }
800 }
801
Lotus Xucb5af732021-09-10 15:18:50 +0800802 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700803 auto findPowerState = baseConfig->find(keyPowerState);
804 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800805 {
806 std::string powerState = std::visit(VariantToStringVisitor(),
807 findPowerState->second);
808 setReadState(powerState, readState);
809 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700810 if (!(psuProperty->minReading < psuProperty->maxReading))
811 {
812 std::cerr << "Min must be less than Max\n";
813 continue;
814 }
815
816 // If the sensor name is being customized by config file,
817 // then prefix/suffix composition becomes not necessary,
818 // and in fact not wanted, because it gets in the way.
819 std::string psuNameFromIndex;
820 if (!customizedName)
821 {
822 /* Find out sensor name index for this label */
823 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500824 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700825 if (std::regex_search(labelHead, matches, rgx))
826 {
827 nameIndex = std::stoi(matches[1]);
828
829 // Decrement to preserve alignment, because hwmon
830 // human-readable filenames and labels use 1-based
831 // numbering, but the "Name", "Name1", "Name2", etc. naming
832 // convention (the psuNames vector) uses 0-based numbering.
833 if (nameIndex > 0)
834 {
835 --nameIndex;
836 }
837 }
838 else
839 {
840 nameIndex = 0;
841 }
842
843 if (psuNames.size() <= nameIndex)
844 {
845 std::cerr << "Could not pair " << labelHead
846 << " with a Name field\n";
847 continue;
848 }
849
850 psuNameFromIndex = psuNames[nameIndex];
851
Ed Tanous8a57ec02020-10-09 12:46:52 -0700852 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700853 {
854 std::cerr << "Sensor label head " << labelHead
855 << " paired with " << psuNameFromIndex
856 << " at index " << nameIndex << "\n";
857 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700858 }
859
Joseph Fub844e972023-03-25 10:24:30 +0800860 if (devType == DevTypes::HWMON)
861 {
862 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
863 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800864
Josh Lehan74d9bd92019-10-31 08:51:58 -0700865 // Similarly, if sensor scaling factor is being customized,
866 // then the below power-of-10 constraint becomes unnecessary,
867 // as config should be able to specify an arbitrary divisor.
868 unsigned int factor = psuProperty->sensorScaleFactor;
869 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700870 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700871 // Preserve existing usage of hardcoded labelMatch table below
872 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700873
Josh Lehan74d9bd92019-10-31 08:51:58 -0700874 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700875 char firstChar =
876 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700877 std::string strScaleFactor =
878 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
879
880 // Preserve existing configs by accepting earlier syntax,
881 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700882 auto findScaleFactor = baseConfig->find(strScaleFactor);
883 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700884 {
885 factor = std::visit(VariantToIntVisitor(),
886 findScaleFactor->second);
887 }
888
Ed Tanous8a57ec02020-10-09 12:46:52 -0700889 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700890 {
891 std::cerr << "Sensor scaling factor " << factor
892 << " string " << strScaleFactor << "\n";
893 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700894 }
895
Vijay Khemka996bad12019-05-28 15:15:16 -0700896 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530897 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
898 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800899 {
James Feist17ab6e02019-06-25 12:28:13 -0700900 std::cerr << "error populating thresholds for "
901 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800902 }
903
Zev Weiss6b6891c2021-04-22 02:46:21 -0500904 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
905 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800906 {
Jason Ling5747fab2019-10-02 16:46:23 -0700907 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700908 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800909 continue;
910 }
911
Ed Tanous8a57ec02020-10-09 12:46:52 -0700912 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700913 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700914 std::cerr << "Sensor properties: Name \""
915 << psuProperty->labelTypeName << "\" Scale "
916 << psuProperty->sensorScaleFactor << " Min "
917 << psuProperty->minReading << " Max "
Jeff Line41d52f2021-04-07 19:38:51 +0800918 << psuProperty->maxReading << " Offset "
919 << psuProperty->sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700920 }
921
922 std::string sensorName = psuProperty->labelTypeName;
923 if (customizedName)
924 {
925 if (sensorName.empty())
926 {
927 // Allow selective disabling of an individual sensor,
928 // by customizing its name to an empty string.
929 std::cerr << "Sensor disabled, empty string\n";
930 continue;
931 }
932 }
933 else
934 {
935 // Sensor name not customized, do prefix/suffix composition,
936 // preserving default behavior by using psuNameFromIndex.
Patrick Williams779c96a2023-05-10 07:50:42 -0500937 sensorName = psuNameFromIndex + " " +
938 psuProperty->labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700939 }
940
Ed Tanous8a57ec02020-10-09 12:46:52 -0700941 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700942 {
943 std::cerr << "Sensor name \"" << sensorName << "\" path \""
944 << sensorPathStr << "\" type \"" << sensorType
945 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700946 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800947 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800948
949 auto& sensor = sensors[sensorName];
950 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700951 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800952 sensor = nullptr;
953 }
954
955 if (sensor != nullptr)
956 {
957 sensor->activate(sensorPathStr, i2cDev);
958 }
959 else
960 {
961 sensors[sensorName] = std::make_shared<PSUSensor>(
962 sensorPathStr, sensorType, objectServer, dbusConnection, io,
963 sensorName, std::move(sensorThresholds), *interfacePath,
964 readState, findSensorUnit->second, factor,
965 psuProperty->maxReading, psuProperty->minReading,
966 psuProperty->sensorOffset, labelHead, thresholdConfSize,
967 pollRate, i2cDev);
968 sensors[sensorName]->setupRead();
969 ++numCreated;
970 if constexpr (debug)
971 {
972 std::cerr << "Created " << numCreated
973 << " sensors so far\n";
974 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700975 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800976 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800977
Joseph Fub844e972023-03-25 10:24:30 +0800978 if (devType == DevTypes::HWMON)
979 {
980 // OperationalStatus event
981 combineEvents[*psuName + "OperationalStatus"] = nullptr;
982 combineEvents[*psuName + "OperationalStatus"] =
983 std::make_unique<PSUCombineEvent>(
984 objectServer, dbusConnection, io, *psuName, readState,
985 eventPathList, groupEventPathList, "OperationalStatus",
986 pollRate);
987 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800988 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700989
Ed Tanous8a57ec02020-10-09 12:46:52 -0700990 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700991 {
992 std::cerr << "Created total of " << numCreated << " sensors\n";
993 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800994}
Cheng C Yang209ec562019-03-12 16:37:44 +0800995
Zhikui Ren23c96e72020-11-05 22:32:28 -0800996void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800997 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800998 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
999 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001000 sensorsChanged,
1001 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001002{
1003 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001004 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1005 activateOnly](const ManagedObjectType& sensorConfigs) {
Zhikui Ren23c96e72020-11-05 22:32:28 -08001006 createSensorsCallback(io, objectServer, dbusConnection,
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001007 sensorConfigs, sensorsChanged, activateOnly);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001008 });
Matt Simmering786efb82023-01-18 14:09:21 -08001009 std::vector<std::string> types(sensorTypes.size());
1010 for (const auto& [type, dt] : sensorTypes)
1011 {
1012 types.push_back(type);
1013 }
1014 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001015}
1016
Cheng C Yang916360b2019-05-07 18:47:16 +08001017void propertyInitialize(void)
Cheng C Yang209ec562019-03-12 16:37:44 +08001018{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001019 sensorTable = {{"power", sensor_paths::unitWatts},
1020 {"curr", sensor_paths::unitAmperes},
1021 {"temp", sensor_paths::unitDegreesC},
1022 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001023 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001024 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001025
Jeff Line41d52f2021-04-07 19:38:51 +08001026 labelMatch = {
1027 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001028 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
1029 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001030 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
1031 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1032 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1033 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001034 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1035 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1036 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001037 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1038 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Patrick Rudolph0874ebf2023-04-19 08:59:35 +02001039 {"vin1", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1040 {"vin2", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001041 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
Joseph Fub844e972023-03-25 10:24:30 +08001042 {"in_voltage0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1043 {"in_voltage1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1044 {"in_voltage2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1045 {"in_voltage3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001046 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1047 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1048 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1049 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1050 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1051 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1052 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1053 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1054 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1055 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1056 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1057 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1058 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1059 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1060 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1061 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1062 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1063 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1064 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1065 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1066 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1067 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1068 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1069 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1070 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1071 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1072 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1073 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1074 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1075 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1076 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1077 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1078 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001079 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001080 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001081 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1082 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1083 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1084 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1085 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1086 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001087 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001088 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1089 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001090 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1091 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1092 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1093 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1094 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1095 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1096 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1097 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1098 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1099 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1100 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1101 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1102 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1103 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1104 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001105 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1106 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1107 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001108 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1109 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1110 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1111 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1112 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1113 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1114 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1115 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1116 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001117 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1118 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1119 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001120
Matt Simmering6747eba2023-01-18 16:03:55 -08001121 pwmTable = {{"fan1", "Fan_1"},
1122 {"fan2", "Fan_2"},
1123 {"fan3", "Fan_3"},
1124 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001125
1126 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1127 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1128
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001129 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1130 {"Failure", {"in2_alarm"}},
1131 {"ACLost", {"in1_beep"}},
1132 {"ConfigureError", {"in1_fault"}}};
1133
1134 groupEventMatch = {{"FanFault",
1135 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001136 {"fan2", {"fan2_alarm", "fan2_fault"}},
1137 {"fan3", {"fan3_alarm", "fan3_fault"}},
1138 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Joseph Fub844e972023-03-25 10:24:30 +08001139
1140 devParamMap = {
1141 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1142 {DevTypes::IIO,
1143 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001144}
1145
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001146static void powerStateChanged(
1147 PowerState type, bool newState,
1148 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1149 sensors,
1150 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1151 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1152{
1153 if (newState)
1154 {
1155 createSensors(io, objectServer, dbusConnection, nullptr, true);
1156 }
1157 else
1158 {
1159 for (auto& [path, sensor] : sensors)
1160 {
1161 if (sensor != nullptr && sensor->readState == type)
1162 {
1163 sensor->deactivate();
1164 }
1165 }
1166 }
1167}
1168
James Feistb6c0b912019-07-09 12:21:44 -07001169int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001170{
Ed Tanous1f978632023-02-28 18:16:39 -08001171 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001172 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1173
Ed Tanous14ed5e92022-07-12 15:50:23 -07001174 sdbusplus::asio::object_server objectServer(systemBus, true);
1175 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001176 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001177 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001178 auto sensorsChanged =
1179 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001180
Cheng C Yang916360b2019-05-07 18:47:16 +08001181 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001182
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001183 auto powerCallBack =
1184 [&io, &objectServer, &systemBus](PowerState type, bool state) {
1185 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1186 };
1187
1188 setupPowerMatchCallback(systemBus, powerCallBack);
1189
1190 boost::asio::post(io, [&]() {
1191 createSensors(io, objectServer, systemBus, nullptr, false);
1192 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001193 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001194 std::function<void(sdbusplus::message_t&)> eventHandler =
1195 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001196 if (message.is_method_error())
1197 {
1198 std::cerr << "callback method error\n";
1199 return;
1200 }
1201 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001202 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001203 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1204 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001205 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001206 return;
1207 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001208 if (ec)
1209 {
1210 std::cerr << "timer error\n";
1211 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001212 createSensors(io, objectServer, systemBus, sensorsChanged, false);
Matt Simmering6747eba2023-01-18 16:03:55 -08001213 });
1214 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001215
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001216 boost::asio::steady_timer cpuFilterTimer(io);
1217 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1218 [&](sdbusplus::message_t& message) {
1219 std::string path = message.get_path();
1220 boost::to_lower(path);
1221
1222 sdbusplus::message::object_path cpuPath(path);
1223 std::string cpuName = cpuPath.filename();
1224 if (!cpuName.starts_with("cpu"))
1225 {
1226 return;
1227 }
1228 size_t index = 0;
1229 try
1230 {
1231 index = std::stoi(path.substr(path.size() - 1));
1232 }
1233 catch (const std::invalid_argument&)
1234 {
1235 std::cerr << "Found invalid path " << path << "\n";
1236 return;
1237 }
1238
1239 std::string objectName;
1240 boost::container::flat_map<std::string, std::variant<bool>> values;
1241 message.read(objectName, values);
1242 auto findPresence = values.find("Present");
1243 try
1244 {
1245 cpuPresence[index] = std::get<bool>(findPresence->second);
1246 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001247 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001248 {
1249 return;
1250 }
1251
Chris Sides3fce1bf2023-04-03 16:57:05 -05001252 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001253 {
1254 return;
1255 }
1256 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1257 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1258 if (ec == boost::asio::error::operation_aborted)
1259 {
1260 return;
1261 }
1262 if (ec)
1263 {
1264 std::cerr << "timer error\n";
1265 return;
1266 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001267 createSensors(io, objectServer, systemBus, nullptr, false);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001268 });
1269 };
1270
Zev Weiss214d9712022-08-12 12:54:31 -07001271 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1272 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001273
1274 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1275 static_cast<sdbusplus::bus_t&>(*systemBus),
1276 "type='signal',member='PropertiesChanged',path_namespace='" +
1277 std::string(cpuInventoryPath) +
1278 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1279 cpuPresenceHandler));
1280
Bruce Lee1263c3d2021-06-04 15:16:33 +08001281 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001282 io.run();
1283}