blob: 2c8d7c0a4a7d13b23791da9a4a47ce22ea552619 [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"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070020#include "PwmSensor.hpp"
21#include "SensorPaths.hpp"
22#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103023#include "Utils.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070024#include "VariantVisitors.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103025
Matt Simmeringe4e6a282023-03-03 14:41:04 -080026#include <boost/algorithm/string/case_conv.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080027#include <boost/algorithm/string/replace.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070028#include <boost/asio/error.hpp>
29#include <boost/asio/io_context.hpp>
30#include <boost/asio/post.hpp>
31#include <boost/asio/steady_timer.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <boost/container/flat_map.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080033#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070034#include <sdbusplus/asio/connection.hpp>
35#include <sdbusplus/asio/object_server.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070036#include <sdbusplus/bus.hpp>
James Feist38fb5982020-05-28 10:09:54 -070037#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070038#include <sdbusplus/exception.hpp>
39#include <sdbusplus/message.hpp>
40#include <sdbusplus/message/native_types.hpp>
James Feist38fb5982020-05-28 10:09:54 -070041
Ed Tanouseacbfdd2024-04-04 12:00:24 -070042#include <algorithm>
James Feist38fb5982020-05-28 10:09:54 -070043#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070044#include <cctype>
45#include <chrono>
Josh Lehan74d9bd92019-10-31 08:51:58 -070046#include <cmath>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070047#include <cstddef>
48#include <cstdint>
49#include <exception>
James Feist24f02f22019-04-15 11:05:39 -070050#include <filesystem>
Cheng C Yang209ec562019-03-12 16:37:44 +080051#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070052#include <functional>
Cheng C Yang58b2b532019-05-31 00:19:45 +080053#include <iostream>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070054#include <memory>
Cheng C Yang209ec562019-03-12 16:37:44 +080055#include <regex>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070056#include <stdexcept>
Patrick Venture96e97db2019-10-31 13:44:38 -070057#include <string>
Lei YUa2c7cea2020-12-23 14:07:28 +080058#include <string_view>
Patrick Venture96e97db2019-10-31 13:44:38 -070059#include <utility>
60#include <variant>
61#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080062
Ed Tanous8a57ec02020-10-09 12:46:52 -070063static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070064
Matt Simmering786efb82023-01-18 14:09:21 -080065static const I2CDeviceTypeMap sensorTypes{
Delphine CC Chiu436c5bd2023-09-22 09:47:43 +080066 {"ADC128D818", I2CDeviceType{"adc128d818", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080067 {"ADM1266", I2CDeviceType{"adm1266", true}},
68 {"ADM1272", I2CDeviceType{"adm1272", true}},
69 {"ADM1275", I2CDeviceType{"adm1275", true}},
70 {"ADM1278", I2CDeviceType{"adm1278", true}},
71 {"ADM1293", I2CDeviceType{"adm1293", true}},
Joseph Fub844e972023-03-25 10:24:30 +080072 {"ADS1015", I2CDeviceType{"ads1015", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080073 {"ADS7830", I2CDeviceType{"ads7830", true}},
74 {"AHE50DC_FAN", I2CDeviceType{"ahe50dc_fan", true}},
75 {"BMR490", I2CDeviceType{"bmr490", true}},
Alexander Hansen90ef14d2023-08-01 17:12:17 +020076 {"cffps", I2CDeviceType{"cffps", true}},
77 {"cffps1", I2CDeviceType{"cffps", true}},
78 {"cffps2", I2CDeviceType{"cffps", true}},
79 {"cffps3", I2CDeviceType{"cffps", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080080 {"DPS800", I2CDeviceType{"dps800", true}},
81 {"INA219", I2CDeviceType{"ina219", true}},
82 {"INA230", I2CDeviceType{"ina230", true}},
Potin Lai5674ac92023-11-20 14:33:56 +080083 {"INA238", I2CDeviceType{"ina238", true}},
Jian Zhangaff100c2023-07-26 01:22:50 +080084 {"IPSPS1", I2CDeviceType{"ipsps1", true}},
Matt Simmering786efb82023-01-18 14:09:21 -080085 {"IR38060", I2CDeviceType{"ir38060", true}},
86 {"IR38164", I2CDeviceType{"ir38164", true}},
87 {"IR38263", I2CDeviceType{"ir38263", true}},
88 {"ISL68137", I2CDeviceType{"isl68137", true}},
89 {"ISL68220", I2CDeviceType{"isl68220", true}},
90 {"ISL68223", I2CDeviceType{"isl68223", true}},
91 {"ISL69225", I2CDeviceType{"isl69225", true}},
92 {"ISL69243", I2CDeviceType{"isl69243", true}},
93 {"ISL69260", I2CDeviceType{"isl69260", true}},
94 {"LM25066", I2CDeviceType{"lm25066", true}},
Yi-Shum77e87692023-11-30 10:27:45 +080095 {"LTC2945", I2CDeviceType{"ltc2945", true}},
Potin Lai43847a82024-02-27 16:29:45 +080096 {"LTC4286", I2CDeviceType{"ltc4286", true}},
97 {"LTC4287", I2CDeviceType{"ltc4287", true}},
Patrick Rudolph8d691e22023-09-11 10:43:28 +020098 {"MAX5970", I2CDeviceType{"max5970", true}},
Joseph Fu15a39df2023-12-27 09:45:49 +080099 {"MAX11607", I2CDeviceType{"max11607", false}},
Matt Simmering786efb82023-01-18 14:09:21 -0800100 {"MAX16601", I2CDeviceType{"max16601", true}},
101 {"MAX20710", I2CDeviceType{"max20710", true}},
102 {"MAX20730", I2CDeviceType{"max20730", true}},
103 {"MAX20734", I2CDeviceType{"max20734", true}},
104 {"MAX20796", I2CDeviceType{"max20796", true}},
105 {"MAX34451", I2CDeviceType{"max34451", true}},
Potin Lai4cfa64f2023-12-18 13:35:05 +0800106 {"MP2856", I2CDeviceType{"mp2856", true}},
107 {"MP2857", I2CDeviceType{"mp2857", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800108 {"MP2971", I2CDeviceType{"mp2971", true}},
109 {"MP2973", I2CDeviceType{"mp2973", true}},
110 {"MP2975", I2CDeviceType{"mp2975", true}},
111 {"MP5023", I2CDeviceType{"mp5023", true}},
Potin Lai7f2b77d2023-12-13 16:53:10 +0800112 {"MP5990", I2CDeviceType{"mp5990", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800113 {"NCP4200", I2CDeviceType{"ncp4200", true}},
114 {"PLI1209BC", I2CDeviceType{"pli1209bc", true}},
115 {"pmbus", I2CDeviceType{"pmbus", true}},
116 {"PXE1610", I2CDeviceType{"pxe1610", true}},
117 {"RAA228000", I2CDeviceType{"raa228000", true}},
118 {"RAA228228", I2CDeviceType{"raa228228", true}},
119 {"RAA228620", I2CDeviceType{"raa228620", true}},
120 {"RAA229001", I2CDeviceType{"raa229001", true}},
121 {"RAA229004", I2CDeviceType{"raa229004", true}},
122 {"RAA229126", I2CDeviceType{"raa229126", true}},
cchouxb2a0f2e2024-03-02 19:32:15 +0800123 {"SBRMI", I2CDeviceType{"sbrmi", true}},
Patrick Rudolph86c9e212023-04-19 09:04:19 +0200124 {"TDA38640", I2CDeviceType{"tda38640", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800125 {"TPS53679", I2CDeviceType{"tps53679", true}},
126 {"TPS546D24", I2CDeviceType{"tps546d24", true}},
127 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
128 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800129 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800130};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700131
Joseph Fub844e972023-03-25 10:24:30 +0800132enum class DevTypes
133{
134 Unknown = 0,
135 HWMON,
136 IIO
137};
138
139struct DevParams
140{
141 unsigned int matchIndex = 0;
142 std::string matchRegEx;
143 std::string nameRegEx;
144};
145
Cheng C Yang209ec562019-03-12 16:37:44 +0800146namespace fs = std::filesystem;
147
Yong Libf8b1da2020-04-15 16:32:50 +0800148static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800149 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800150static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
151 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800152static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
153 pwmSensors;
154static boost::container::flat_map<std::string, std::string> sensorTable;
155static boost::container::flat_map<std::string, PSUProperty> labelMatch;
156static boost::container::flat_map<std::string, std::string> pwmTable;
George Liu5b3542e2023-10-31 17:27:12 +0800157static EventPathList eventMatch;
158static GroupEventPathList groupEventMatch;
159static EventPathList limitEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800160
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800161static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800162static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700163
Cheng C Yang58b2b532019-05-31 00:19:45 +0800164// Function CheckEvent will check each attribute from eventMatch table in the
165// sysfs. If the attributes exists in sysfs, then store the complete path
166// of the attribute into eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800167void checkEvent(const std::string& directory, const EventPathList& eventMatch,
168 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800169{
170 for (const auto& match : eventMatch)
171 {
172 const std::vector<std::string>& eventAttrs = match.second;
173 const std::string& eventName = match.first;
174 for (const auto& eventAttr : eventAttrs)
175 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700176 std::string eventPath = directory;
177 eventPath += "/";
178 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800179
180 std::ifstream eventFile(eventPath);
181 if (!eventFile.good())
182 {
183 continue;
184 }
185
186 eventPathList[eventName].push_back(eventPath);
187 }
188 }
189}
190
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800191// Check Group Events which contains more than one targets in each combine
192// events.
George Liu5b3542e2023-10-31 17:27:12 +0800193void checkGroupEvent(const std::string& directory,
194 const GroupEventPathList& groupEventMatch,
195 GroupEventPathList& groupEventPathList)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800196{
197 for (const auto& match : groupEventMatch)
198 {
199 const std::string& groupEventName = match.first;
George Liu5b3542e2023-10-31 17:27:12 +0800200 const EventPathList events = match.second;
201 EventPathList pathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800202 for (const auto& match : events)
203 {
204 const std::string& eventName = match.first;
205 const std::vector<std::string>& eventAttrs = match.second;
206 for (const auto& eventAttr : eventAttrs)
207 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700208 std::string eventPath = directory;
209 eventPath += "/";
210 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800211 std::ifstream eventFile(eventPath);
212 if (!eventFile.good())
213 {
214 continue;
215 }
216
217 pathList[eventName].push_back(eventPath);
218 }
219 }
220 groupEventPathList[groupEventName] = pathList;
221 }
222}
223
Cheng C Yang58b2b532019-05-31 00:19:45 +0800224// Function checkEventLimits will check all the psu related xxx_input attributes
225// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
226// xxx_min_alarm exist, then store the existing paths of the alarm attributes
227// to eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800228void checkEventLimits(const std::string& sensorPathStr,
229 const EventPathList& limitEventMatch,
230 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800231{
Lei YUa2c7cea2020-12-23 14:07:28 +0800232 auto attributePartPos = sensorPathStr.find_last_of('_');
233 if (attributePartPos == std::string::npos)
234 {
235 // There is no '_' in the string, skip it
236 return;
237 }
238 auto attributePart =
239 std::string_view(sensorPathStr).substr(attributePartPos + 1);
240 if (attributePart != "input")
241 {
242 // If the sensor is not xxx_input, skip it
243 return;
244 }
245
246 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800247 for (const auto& limitMatch : limitEventMatch)
248 {
249 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
250 const std::string& eventName = limitMatch.first;
251 for (const auto& limitEventAttr : limitEventAttrs)
252 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800253 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800254 std::ifstream eventFile(limitEventPath);
255 if (!eventFile.good())
256 {
257 continue;
258 }
259 eventPathList[eventName].push_back(limitEventPath);
260 }
261 }
262}
Cheng C Yang916360b2019-05-07 18:47:16 +0800263
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530264static void
265 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
266 const std::string& interfacePath,
267 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
268 sdbusplus::asio::object_server& objectServer,
269 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800270{
Zev Weiss741f26e2022-08-12 18:21:02 -0700271 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800272 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700273 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800274 {
275 continue;
276 }
277
278 const std::string& sensorPathStr = sensorPath.string();
279 const std::string& pwmPathStr =
280 boost::replace_all_copy(sensorPathStr, "input", "target");
281 std::ifstream pwmFile(pwmPathStr);
282 if (!pwmFile.good())
283 {
284 continue;
285 }
286
287 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
288 if (findPWMSensor != pwmSensors.end())
289 {
290 continue;
291 }
292
Zev Weissd8c293a2022-08-15 18:58:41 -0700293 std::string name = "Pwm_";
294 name += psuName;
295 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700296 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700297
298 std::string objPath = interfacePath;
299 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700300 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700301
Cheng C Yang916360b2019-05-07 18:47:16 +0800302 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700303 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800304 }
305}
306
Zhikui Ren23c96e72020-11-05 22:32:28 -0800307static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800308 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800309 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
310 const ManagedObjectType& sensorConfigs,
311 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800312 sensorsChanged,
313 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800314{
Josh Lehan49cfba92019-10-08 16:50:42 -0700315 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800316 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800317
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800318 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
319
Cheng C Yang209ec562019-03-12 16:37:44 +0800320 std::vector<fs::path> pmbusPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800321 findFiles(fs::path("/sys/bus/iio/devices"), "name", pmbusPaths);
322 findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths);
323 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800324 {
325 std::cerr << "No PSU sensors in system\n";
326 return;
327 }
328
329 boost::container::flat_set<std::string> directories;
330 for (const auto& pmbusPath : pmbusPaths)
331 {
George Liu5b3542e2023-10-31 17:27:12 +0800332 EventPathList eventPathList;
333 GroupEventPathList groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800334
335 std::ifstream nameFile(pmbusPath);
336 if (!nameFile.good())
337 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700338 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800339 continue;
340 }
341
342 std::string pmbusName;
343 std::getline(nameFile, pmbusName);
344 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700345
Matt Simmering786efb82023-01-18 14:09:21 -0800346 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800347 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700348 // To avoid this error message, add your driver name to
349 // the pmbusNames vector at the top of this file.
350 std::cerr << "Driver name " << pmbusName
351 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800352 continue;
353 }
354
Cheng C Yang209ec562019-03-12 16:37:44 +0800355 auto directory = pmbusPath.parent_path();
356
357 auto ret = directories.insert(directory.string());
358 if (!ret.second)
359 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700360 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800361 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800362 }
363
Joseph Fub844e972023-03-25 10:24:30 +0800364 DevTypes devType = DevTypes::HWMON;
365 std::string deviceName;
366 if (directory.parent_path() == "/sys/class/hwmon")
367 {
368 deviceName = fs::canonical(directory / "device").stem();
369 }
370 else
371 {
372 deviceName = fs::canonical(directory).parent_path().stem();
373 devType = DevTypes::IIO;
374 }
375
Cheng C Yang209ec562019-03-12 16:37:44 +0800376 size_t bus = 0;
377 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000378 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800379 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800380 continue;
381 }
382
Zev Weisse8b97ee2022-08-12 15:23:51 -0700383 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800384 const SensorData* sensorData = nullptr;
385 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800386 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800387 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800388
Zev Weiss741f26e2022-08-12 18:21:02 -0700389 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800390 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700391 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800392 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800393 {
Zev Weiss054aad82022-08-18 01:37:34 -0700394 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800395 if (sensorBase != sensorData->end())
396 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700397 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800398 sensorType = type;
399 break;
400 }
401 }
402 if (baseConfig == nullptr)
403 {
404 std::cerr << "error finding base configuration for "
405 << deviceName << "\n";
406 continue;
407 }
408
Zev Weisse8b97ee2022-08-12 15:23:51 -0700409 auto configBus = baseConfig->find("Bus");
410 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800411
Zev Weisse8b97ee2022-08-12 15:23:51 -0700412 if (configBus == baseConfig->end() ||
413 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800414 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800415 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800416 continue;
417 }
418
Matt Simmering6747eba2023-01-18 16:03:55 -0800419 const uint64_t* confBus =
420 std::get_if<uint64_t>(&(configBus->second));
421 const uint64_t* confAddr =
422 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800423 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800424 {
425 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700426 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800427 continue;
428 }
429
430 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800431 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200432 if constexpr (debug)
433 {
434 std::cerr << "Configuration skipping " << *confBus << "-"
435 << *confAddr << " because not " << bus << "-"
436 << addr << "\n";
437 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800438 continue;
439 }
440
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800441 std::vector<thresholds::Threshold> confThresholds;
442 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
443 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800444 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800445 }
446 thresholdConfSize = confThresholds.size();
447
Zev Weiss741f26e2022-08-12 18:21:02 -0700448 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800449 break;
450 }
451 if (interfacePath == nullptr)
452 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700453 // To avoid this error message, add your export map entry,
454 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800455 std::cerr << "failed to find match for " << deviceName << "\n";
456 continue;
457 }
458
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800459 auto findI2CDev = devices.find(*interfacePath);
460
461 std::shared_ptr<I2CDevice> i2cDev;
462 if (findI2CDev != devices.end())
463 {
464 if (activateOnly && !findI2CDev->second.second)
465 {
466 continue;
467 }
468 i2cDev = findI2CDev->second.first;
469 }
470
Zev Weisse8b97ee2022-08-12 15:23:51 -0700471 auto findPSUName = baseConfig->find("Name");
472 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800473 {
474 std::cerr << "could not determine configuration name for "
475 << deviceName << "\n";
476 continue;
477 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800478 const std::string* psuName =
479 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800480 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800481 {
482 std::cerr << "Cannot find psu name, invalid configuration\n";
483 continue;
484 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800485
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800486 auto findCPU = baseConfig->find("CPURequired");
487 if (findCPU != baseConfig->end())
488 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500489 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800490 auto presenceFind = cpuPresence.find(index);
491 if (presenceFind == cpuPresence.end() || !presenceFind->second)
492 {
493 continue;
494 }
495 }
496
Zhikui Ren23c96e72020-11-05 22:32:28 -0800497 // on rescans, only update sensors we were signaled by
498 if (!firstScan)
499 {
Zhikui Renda98f092021-11-01 09:41:08 -0700500 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500501 auto it = std::find_if(sensorsChanged->begin(),
502 sensorsChanged->end(),
503 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800504 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500505 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800506
507 if (it == sensorsChanged->end())
508 {
509 continue;
510 }
511 sensorsChanged->erase(it);
512 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800513 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800514 checkGroupEvent(directory.string(), groupEventMatch,
515 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800516
Zev Weisse8b97ee2022-08-12 15:23:51 -0700517 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000518
Vijay Khemka996bad12019-05-28 15:15:16 -0700519 /* Check if there are more sensors in the same interface */
520 int i = 1;
521 std::vector<std::string> psuNames;
522 do
523 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700524 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700525 psuNames.push_back(
526 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700527 findPSUName = baseConfig->find("Name" + std::to_string(i++));
528 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700529
Cheng C Yange50345b2019-04-02 17:26:15 +0800530 std::vector<fs::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800531 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
532 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800533 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800534 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800535 continue;
536 }
537
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530538 /* read max value in sysfs for in, curr, power, temp, ... */
539 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
540 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700541 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530542 {
543 std::cerr << "No max name in PSU \n";
544 }
545 }
546
Zev Weiss8569bf22022-10-11 15:37:44 -0700547 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800548
Vijay Khemka996bad12019-05-28 15:15:16 -0700549 /* Find array of labels to be exposed if it is defined in config */
550 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700551 auto findLabelObj = baseConfig->find("Labels");
552 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700553 {
554 findLabels =
555 std::get<std::vector<std::string>>(findLabelObj->second);
556 }
557
Joseph Fub844e972023-03-25 10:24:30 +0800558 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700559 std::smatch matches;
560
Cheng C Yange50345b2019-04-02 17:26:15 +0800561 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800562 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530563 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800564 std::string labelHead;
565 std::string sensorPathStr = sensorPath.string();
566 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700567 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700568 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
569 {
Josh Lehan06494452019-10-31 09:49:16 -0700570 // hwmon *_input filename without number:
571 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800572 // iio in_*_raw filename without number:
573 // voltage, temp, pressure, ...
574 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700575 }
576 else
577 {
Josh Lehan06494452019-10-31 09:49:16 -0700578 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700579 << sensorNameStr;
580 continue;
581 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800582
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530583 std::string labelPath;
584
Joseph Fub844e972023-03-25 10:24:30 +0800585 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530586 {
Joseph Fub844e972023-03-25 10:24:30 +0800587 /* find and differentiate _max and _input to replace "label" */
588 size_t pos = sensorPathStr.find('_');
589 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530590 {
Joseph Fub844e972023-03-25 10:24:30 +0800591 std::string sensorPathStrMax = sensorPathStr.substr(pos);
592 if (sensorPathStrMax == "_max")
593 {
594 labelPath = boost::replace_all_copy(sensorPathStr,
595 "max", "label");
596 maxLabel = true;
597 }
598 else
599 {
600 labelPath = boost::replace_all_copy(sensorPathStr,
601 "input", "label");
602 maxLabel = false;
603 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530604 }
605 else
606 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800607 continue;
608 }
609
Joseph Fub844e972023-03-25 10:24:30 +0800610 std::ifstream labelFile(labelPath);
611 if (!labelFile.good())
612 {
613 if constexpr (debug)
614 {
615 std::cerr << "Input file " << sensorPath
616 << " has no corresponding label file\n";
617 }
618 // hwmon *_input filename with number:
619 // temp1, temp2, temp3, ...
620 labelHead = sensorNameStr.substr(0,
621 sensorNameStr.find('_'));
622 }
623 else
624 {
625 std::string label;
626 std::getline(labelFile, label);
627 labelFile.close();
628 auto findSensor = sensors.find(label);
629 if (findSensor != sensors.end())
630 {
631 continue;
632 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800633
Joseph Fub844e972023-03-25 10:24:30 +0800634 // hwmon corresponding *_label file contents:
635 // vin1, vout1, ...
636 labelHead = label.substr(0, label.find(' '));
637 }
638
639 /* append "max" for labelMatch */
640 if (maxLabel)
641 {
642 labelHead.insert(0, "max");
643 }
644
645 checkPWMSensor(sensorPath, labelHead, *interfacePath,
646 dbusConnection, objectServer, psuNames[0]);
647 }
648 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530649 {
Joseph Fub844e972023-03-25 10:24:30 +0800650 auto findIIOHyphen = sensorNameStr.find_last_of('_');
651 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530652 }
653
Ed Tanous8a57ec02020-10-09 12:46:52 -0700654 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700655 {
656 std::cerr << "Sensor type=\"" << sensorNameSubStr
657 << "\" label=\"" << labelHead << "\"\n";
658 }
659
Vijay Khemka996bad12019-05-28 15:15:16 -0700660 if (!findLabels.empty())
661 {
662 /* Check if this labelHead is enabled in config file */
663 if (std::find(findLabels.begin(), findLabels.end(),
664 labelHead) == findLabels.end())
665 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700666 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800667 {
668 std::cerr << "could not find " << labelHead
669 << " in the Labels list\n";
670 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700671 continue;
672 }
673 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800674
Cheng C Yange50345b2019-04-02 17:26:15 +0800675 auto findProperty = labelMatch.find(labelHead);
676 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800677 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700678 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700679 {
680 std::cerr << "Could not find matching default property for "
681 << labelHead << "\n";
682 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800683 continue;
684 }
685
Josh Lehan74d9bd92019-10-31 08:51:58 -0700686 // Protect the hardcoded labelMatch list from changes,
687 // by making a copy and modifying that instead.
688 // Avoid bleedthrough of one device's customizations to
689 // the next device, as each should be independently customizable.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800690 PSUProperty psuProperty = findProperty->second;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700691
692 // Use label head as prefix for reading from config file,
693 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
694 std::string keyName = labelHead + "_Name";
695 std::string keyScale = labelHead + "_Scale";
696 std::string keyMin = labelHead + "_Min";
697 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800698 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800699 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700700
701 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700702 auto findCustomName = baseConfig->find(keyName);
703 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700704 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700705 try
706 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800707 psuProperty.labelTypeName = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700708 VariantToStringVisitor(), findCustomName->second);
709 }
Patrick Williams26601e82021-10-06 12:43:25 -0500710 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700711 {
712 std::cerr << "Unable to parse " << keyName << "\n";
713 continue;
714 }
715
716 // All strings are valid, including empty string
717 customizedName = true;
718 }
719
720 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700721 auto findCustomScale = baseConfig->find(keyScale);
722 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700723 {
724 try
725 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800726 psuProperty.sensorScaleFactor = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700727 VariantToUnsignedIntVisitor(), findCustomScale->second);
728 }
Patrick Williams26601e82021-10-06 12:43:25 -0500729 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700730 {
731 std::cerr << "Unable to parse " << keyScale << "\n";
732 continue;
733 }
734
735 // Avoid later division by zero
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800736 if (psuProperty.sensorScaleFactor > 0)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700737 {
738 customizedScale = true;
739 }
740 else
741 {
742 std::cerr << "Unable to accept " << keyScale << "\n";
743 continue;
744 }
745 }
746
Zev Weisse8b97ee2022-08-12 15:23:51 -0700747 auto findCustomMin = baseConfig->find(keyMin);
748 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700749 {
750 try
751 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800752 psuProperty.minReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700753 VariantToDoubleVisitor(), findCustomMin->second);
754 }
Patrick Williams26601e82021-10-06 12:43:25 -0500755 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700756 {
757 std::cerr << "Unable to parse " << keyMin << "\n";
758 continue;
759 }
760 }
761
Zev Weisse8b97ee2022-08-12 15:23:51 -0700762 auto findCustomMax = baseConfig->find(keyMax);
763 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700764 {
765 try
766 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800767 psuProperty.maxReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700768 VariantToDoubleVisitor(), findCustomMax->second);
769 }
Patrick Williams26601e82021-10-06 12:43:25 -0500770 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700771 {
772 std::cerr << "Unable to parse " << keyMax << "\n";
773 continue;
774 }
775 }
776
Zev Weisse8b97ee2022-08-12 15:23:51 -0700777 auto findCustomOffset = baseConfig->find(keyOffset);
778 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800779 {
780 try
781 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800782 psuProperty.sensorOffset = std::visit(
Jeff Line41d52f2021-04-07 19:38:51 +0800783 VariantToDoubleVisitor(), findCustomOffset->second);
784 }
Patrick Williams26601e82021-10-06 12:43:25 -0500785 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800786 {
787 std::cerr << "Unable to parse " << keyOffset << "\n";
788 continue;
789 }
790 }
791
Lotus Xucb5af732021-09-10 15:18:50 +0800792 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700793 auto findPowerState = baseConfig->find(keyPowerState);
794 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800795 {
796 std::string powerState = std::visit(VariantToStringVisitor(),
797 findPowerState->second);
798 setReadState(powerState, readState);
799 }
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800800 if (!(psuProperty.minReading < psuProperty.maxReading))
Josh Lehan74d9bd92019-10-31 08:51:58 -0700801 {
802 std::cerr << "Min must be less than Max\n";
803 continue;
804 }
805
806 // If the sensor name is being customized by config file,
807 // then prefix/suffix composition becomes not necessary,
808 // and in fact not wanted, because it gets in the way.
809 std::string psuNameFromIndex;
810 if (!customizedName)
811 {
812 /* Find out sensor name index for this label */
813 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500814 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700815 if (std::regex_search(labelHead, matches, rgx))
816 {
817 nameIndex = std::stoi(matches[1]);
818
819 // Decrement to preserve alignment, because hwmon
820 // human-readable filenames and labels use 1-based
821 // numbering, but the "Name", "Name1", "Name2", etc. naming
822 // convention (the psuNames vector) uses 0-based numbering.
823 if (nameIndex > 0)
824 {
825 --nameIndex;
826 }
827 }
828 else
829 {
830 nameIndex = 0;
831 }
832
833 if (psuNames.size() <= nameIndex)
834 {
835 std::cerr << "Could not pair " << labelHead
836 << " with a Name field\n";
837 continue;
838 }
839
840 psuNameFromIndex = psuNames[nameIndex];
841
Ed Tanous8a57ec02020-10-09 12:46:52 -0700842 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700843 {
844 std::cerr << "Sensor label head " << labelHead
845 << " paired with " << psuNameFromIndex
846 << " at index " << nameIndex << "\n";
847 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700848 }
849
Joseph Fub844e972023-03-25 10:24:30 +0800850 if (devType == DevTypes::HWMON)
851 {
852 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
853 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800854
Josh Lehan74d9bd92019-10-31 08:51:58 -0700855 // Similarly, if sensor scaling factor is being customized,
856 // then the below power-of-10 constraint becomes unnecessary,
857 // as config should be able to specify an arbitrary divisor.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800858 unsigned int factor = psuProperty.sensorScaleFactor;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700859 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700860 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700861 // Preserve existing usage of hardcoded labelMatch table below
862 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700863
Josh Lehan74d9bd92019-10-31 08:51:58 -0700864 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700865 char firstChar =
866 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700867 std::string strScaleFactor =
868 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
869
870 // Preserve existing configs by accepting earlier syntax,
871 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700872 auto findScaleFactor = baseConfig->find(strScaleFactor);
873 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700874 {
875 factor = std::visit(VariantToIntVisitor(),
876 findScaleFactor->second);
877 }
878
Ed Tanous8a57ec02020-10-09 12:46:52 -0700879 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700880 {
881 std::cerr << "Sensor scaling factor " << factor
882 << " string " << strScaleFactor << "\n";
883 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700884 }
885
Vijay Khemka996bad12019-05-28 15:15:16 -0700886 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530887 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
888 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800889 {
James Feist17ab6e02019-06-25 12:28:13 -0700890 std::cerr << "error populating thresholds for "
891 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800892 }
893
Zev Weiss6b6891c2021-04-22 02:46:21 -0500894 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
895 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800896 {
Jason Ling5747fab2019-10-02 16:46:23 -0700897 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700898 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800899 continue;
900 }
901
Ed Tanous8a57ec02020-10-09 12:46:52 -0700902 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700903 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700904 std::cerr << "Sensor properties: Name \""
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800905 << psuProperty.labelTypeName << "\" Scale "
906 << psuProperty.sensorScaleFactor << " Min "
907 << psuProperty.minReading << " Max "
908 << psuProperty.maxReading << " Offset "
909 << psuProperty.sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700910 }
911
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800912 std::string sensorName = psuProperty.labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700913 if (customizedName)
914 {
915 if (sensorName.empty())
916 {
917 // Allow selective disabling of an individual sensor,
918 // by customizing its name to an empty string.
919 std::cerr << "Sensor disabled, empty string\n";
920 continue;
921 }
922 }
923 else
924 {
925 // Sensor name not customized, do prefix/suffix composition,
926 // preserving default behavior by using psuNameFromIndex.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800927 sensorName = psuNameFromIndex + " " + psuProperty.labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700928 }
929
Ed Tanous8a57ec02020-10-09 12:46:52 -0700930 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700931 {
932 std::cerr << "Sensor name \"" << sensorName << "\" path \""
933 << sensorPathStr << "\" type \"" << sensorType
934 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700935 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800936 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800937
938 auto& sensor = sensors[sensorName];
939 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700940 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800941 sensor = nullptr;
942 }
943
944 if (sensor != nullptr)
945 {
946 sensor->activate(sensorPathStr, i2cDev);
947 }
948 else
949 {
950 sensors[sensorName] = std::make_shared<PSUSensor>(
951 sensorPathStr, sensorType, objectServer, dbusConnection, io,
952 sensorName, std::move(sensorThresholds), *interfacePath,
953 readState, findSensorUnit->second, factor,
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800954 psuProperty.maxReading, psuProperty.minReading,
955 psuProperty.sensorOffset, labelHead, thresholdConfSize,
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800956 pollRate, i2cDev);
957 sensors[sensorName]->setupRead();
958 ++numCreated;
959 if constexpr (debug)
960 {
961 std::cerr << "Created " << numCreated
962 << " sensors so far\n";
963 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700964 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800965 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800966
Joseph Fub844e972023-03-25 10:24:30 +0800967 if (devType == DevTypes::HWMON)
968 {
969 // OperationalStatus event
970 combineEvents[*psuName + "OperationalStatus"] = nullptr;
971 combineEvents[*psuName + "OperationalStatus"] =
972 std::make_unique<PSUCombineEvent>(
973 objectServer, dbusConnection, io, *psuName, readState,
974 eventPathList, groupEventPathList, "OperationalStatus",
975 pollRate);
976 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800977 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700978
Ed Tanous8a57ec02020-10-09 12:46:52 -0700979 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700980 {
981 std::cerr << "Created total of " << numCreated << " sensors\n";
982 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800983}
Cheng C Yang209ec562019-03-12 16:37:44 +0800984
Matt Simmering39fadb92023-05-04 21:47:29 -0700985static void
986 getPresentCpus(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
987{
988 static const int depth = 2;
989 static const int numKeys = 1;
990 GetSubTreeType cpuSubTree;
991
992 try
993 {
994 auto getItems = dbusConnection->new_method_call(
995 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
996 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
997 std::array<const char*, numKeys>{
998 "xyz.openbmc_project.Inventory.Item"});
999 auto getItemsResp = dbusConnection->call(getItems);
1000 getItemsResp.read(cpuSubTree);
1001 }
1002 catch (sdbusplus::exception_t& e)
1003 {
1004 std::cerr << "error getting inventory item subtree: " << e.what()
1005 << "\n";
1006 return;
1007 }
1008
1009 for (const auto& [path, objDict] : cpuSubTree)
1010 {
1011 auto obj = sdbusplus::message::object_path(path).filename();
1012 if (!obj.starts_with("cpu") || objDict.empty())
1013 {
1014 continue;
1015 }
1016 const std::string& owner = objDict.begin()->first;
1017
1018 std::variant<bool> respValue;
1019 try
1020 {
1021 auto getPresence = dbusConnection->new_method_call(
1022 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1023 "Get");
1024 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1025 auto resp = dbusConnection->call(getPresence);
1026 resp.read(respValue);
1027 }
1028 catch (sdbusplus::exception_t& e)
1029 {
1030 std::cerr << "Error in getting CPU presence: " << e.what() << "\n";
1031 continue;
1032 }
1033
1034 auto* present = std::get_if<bool>(&respValue);
1035 if (present != nullptr && *present)
1036 {
1037 int cpuIndex = 0;
1038 try
1039 {
1040 cpuIndex = std::stoi(obj.substr(obj.find_last_of("cpu") + 1));
1041 }
1042 catch (const std::exception& e)
1043 {
1044 std::cerr << "Error converting CPU index, " << e.what() << '\n';
1045 continue;
1046 }
1047 cpuPresence[cpuIndex + 1] = *present;
1048 }
1049 }
1050}
1051
Zhikui Ren23c96e72020-11-05 22:32:28 -08001052void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001053 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001054 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1055 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001056 sensorsChanged,
1057 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001058{
1059 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001060 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1061 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams597e8422023-10-20 11:19:01 -05001062 createSensorsCallback(io, objectServer, dbusConnection, sensorConfigs,
1063 sensorsChanged, activateOnly);
1064 });
Matt Simmering786efb82023-01-18 14:09:21 -08001065 std::vector<std::string> types(sensorTypes.size());
1066 for (const auto& [type, dt] : sensorTypes)
1067 {
1068 types.push_back(type);
1069 }
1070 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001071}
1072
Ed Tanous201a1012024-04-03 18:07:28 -07001073void propertyInitialize()
Cheng C Yang209ec562019-03-12 16:37:44 +08001074{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001075 sensorTable = {{"power", sensor_paths::unitWatts},
1076 {"curr", sensor_paths::unitAmperes},
1077 {"temp", sensor_paths::unitDegreesC},
1078 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001079 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001080 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001081
Jeff Line41d52f2021-04-07 19:38:51 +08001082 labelMatch = {
1083 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001084 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
1085 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001086 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
1087 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1088 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1089 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001090 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1091 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1092 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001093 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1094 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Patrick Rudolph0874ebf2023-04-19 08:59:35 +02001095 {"vin1", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1096 {"vin2", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001097 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
Joseph Fub844e972023-03-25 10:24:30 +08001098 {"in_voltage0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1099 {"in_voltage1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1100 {"in_voltage2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1101 {"in_voltage3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001102 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1103 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1104 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1105 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1106 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1107 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1108 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1109 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1110 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1111 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1112 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1113 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1114 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1115 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1116 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1117 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1118 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1119 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1120 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1121 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1122 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1123 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1124 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1125 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1126 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1127 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1128 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1129 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1130 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1131 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1132 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1133 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1134 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001135 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001136 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001137 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1138 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1139 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1140 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1141 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1142 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001143 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001144 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1145 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001146 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1147 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1148 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1149 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1150 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1151 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1152 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1153 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1154 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1155 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1156 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1157 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1158 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1159 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1160 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001161 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1162 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1163 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001164 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1165 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1166 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1167 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1168 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1169 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1170 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1171 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1172 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001173 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1174 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1175 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001176
Matt Simmering6747eba2023-01-18 16:03:55 -08001177 pwmTable = {{"fan1", "Fan_1"},
1178 {"fan2", "Fan_2"},
1179 {"fan3", "Fan_3"},
1180 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001181
1182 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1183 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1184
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001185 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1186 {"Failure", {"in2_alarm"}},
1187 {"ACLost", {"in1_beep"}},
1188 {"ConfigureError", {"in1_fault"}}};
1189
1190 groupEventMatch = {{"FanFault",
1191 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001192 {"fan2", {"fan2_alarm", "fan2_fault"}},
1193 {"fan3", {"fan3_alarm", "fan3_fault"}},
1194 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Joseph Fub844e972023-03-25 10:24:30 +08001195
1196 devParamMap = {
1197 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1198 {DevTypes::IIO,
1199 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001200}
1201
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001202static void powerStateChanged(
1203 PowerState type, bool newState,
1204 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1205 sensors,
1206 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1207 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1208{
1209 if (newState)
1210 {
1211 createSensors(io, objectServer, dbusConnection, nullptr, true);
1212 }
1213 else
1214 {
1215 for (auto& [path, sensor] : sensors)
1216 {
1217 if (sensor != nullptr && sensor->readState == type)
1218 {
1219 sensor->deactivate();
1220 }
1221 }
1222 }
1223}
1224
James Feistb6c0b912019-07-09 12:21:44 -07001225int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001226{
Ed Tanous1f978632023-02-28 18:16:39 -08001227 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001228 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1229
Ed Tanous14ed5e92022-07-12 15:50:23 -07001230 sdbusplus::asio::object_server objectServer(systemBus, true);
1231 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001232 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001233 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001234 auto sensorsChanged =
1235 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001236
Cheng C Yang916360b2019-05-07 18:47:16 +08001237 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001238
Patrick Williams597e8422023-10-20 11:19:01 -05001239 auto powerCallBack = [&io, &objectServer, &systemBus](PowerState type,
1240 bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001241 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1242 };
1243
1244 setupPowerMatchCallback(systemBus, powerCallBack);
1245
1246 boost::asio::post(io, [&]() {
1247 createSensors(io, objectServer, systemBus, nullptr, false);
1248 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001249 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001250 std::function<void(sdbusplus::message_t&)> eventHandler =
1251 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001252 if (message.is_method_error())
1253 {
1254 std::cerr << "callback method error\n";
1255 return;
1256 }
1257 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001258 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001259 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1260 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001261 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001262 return;
1263 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001264 if (ec)
1265 {
1266 std::cerr << "timer error\n";
1267 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001268 createSensors(io, objectServer, systemBus, sensorsChanged, false);
Matt Simmering6747eba2023-01-18 16:03:55 -08001269 });
1270 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001271
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001272 boost::asio::steady_timer cpuFilterTimer(io);
1273 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1274 [&](sdbusplus::message_t& message) {
1275 std::string path = message.get_path();
1276 boost::to_lower(path);
1277
1278 sdbusplus::message::object_path cpuPath(path);
1279 std::string cpuName = cpuPath.filename();
1280 if (!cpuName.starts_with("cpu"))
1281 {
1282 return;
1283 }
1284 size_t index = 0;
1285 try
1286 {
1287 index = std::stoi(path.substr(path.size() - 1));
1288 }
1289 catch (const std::invalid_argument&)
1290 {
1291 std::cerr << "Found invalid path " << path << "\n";
1292 return;
1293 }
1294
1295 std::string objectName;
1296 boost::container::flat_map<std::string, std::variant<bool>> values;
1297 message.read(objectName, values);
1298 auto findPresence = values.find("Present");
1299 try
1300 {
1301 cpuPresence[index] = std::get<bool>(findPresence->second);
1302 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001303 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001304 {
1305 return;
1306 }
1307
Chris Sides3fce1bf2023-04-03 16:57:05 -05001308 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001309 {
1310 return;
1311 }
1312 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1313 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1314 if (ec == boost::asio::error::operation_aborted)
1315 {
1316 return;
1317 }
1318 if (ec)
1319 {
1320 std::cerr << "timer error\n";
1321 return;
1322 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001323 createSensors(io, objectServer, systemBus, nullptr, false);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001324 });
1325 };
1326
Zev Weiss214d9712022-08-12 12:54:31 -07001327 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1328 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001329
1330 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1331 static_cast<sdbusplus::bus_t&>(*systemBus),
1332 "type='signal',member='PropertiesChanged',path_namespace='" +
1333 std::string(cpuInventoryPath) +
1334 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1335 cpuPresenceHandler));
1336
Matt Simmering39fadb92023-05-04 21:47:29 -07001337 getPresentCpus(systemBus);
1338
Bruce Lee1263c3d2021-06-04 15:16:33 +08001339 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001340 io.run();
1341}