blob: 2da8139028de209511c90dbb402c0c8b4510836a [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}},
Yi-Shum9110dfc2024-06-04 15:19:58 +0800127 {"XDP710", I2CDeviceType{"xdp710", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800128 {"XDPE11280", I2CDeviceType{"xdpe11280", true}},
129 {"XDPE12284", I2CDeviceType{"xdpe12284", true}},
Jeff Lin40bd6712023-06-01 19:14:09 +0800130 {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}},
Matt Simmering786efb82023-01-18 14:09:21 -0800131};
Josh Lehan0830c7b2019-10-08 16:35:09 -0700132
Joseph Fub844e972023-03-25 10:24:30 +0800133enum class DevTypes
134{
135 Unknown = 0,
136 HWMON,
137 IIO
138};
139
140struct DevParams
141{
142 unsigned int matchIndex = 0;
143 std::string matchRegEx;
144 std::string nameRegEx;
145};
146
Cheng C Yang209ec562019-03-12 16:37:44 +0800147namespace fs = std::filesystem;
148
Yong Libf8b1da2020-04-15 16:32:50 +0800149static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>
Cheng C Yang916360b2019-05-07 18:47:16 +0800150 sensors;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800151static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>>
152 combineEvents;
Cheng C Yang916360b2019-05-07 18:47:16 +0800153static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
154 pwmSensors;
155static boost::container::flat_map<std::string, std::string> sensorTable;
156static boost::container::flat_map<std::string, PSUProperty> labelMatch;
157static boost::container::flat_map<std::string, std::string> pwmTable;
George Liu5b3542e2023-10-31 17:27:12 +0800158static EventPathList eventMatch;
159static GroupEventPathList groupEventMatch;
160static EventPathList limitEventMatch;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800161
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800162static boost::container::flat_map<size_t, bool> cpuPresence;
Joseph Fub844e972023-03-25 10:24:30 +0800163static boost::container::flat_map<DevTypes, DevParams> devParamMap;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700164
Cheng C Yang58b2b532019-05-31 00:19:45 +0800165// Function CheckEvent will check each attribute from eventMatch table in the
166// sysfs. If the attributes exists in sysfs, then store the complete path
167// of the attribute into eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800168void checkEvent(const std::string& directory, const EventPathList& eventMatch,
169 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800170{
171 for (const auto& match : eventMatch)
172 {
173 const std::vector<std::string>& eventAttrs = match.second;
174 const std::string& eventName = match.first;
175 for (const auto& eventAttr : eventAttrs)
176 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700177 std::string eventPath = directory;
178 eventPath += "/";
179 eventPath += eventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800180
181 std::ifstream eventFile(eventPath);
182 if (!eventFile.good())
183 {
184 continue;
185 }
186
187 eventPathList[eventName].push_back(eventPath);
188 }
189 }
190}
191
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800192// Check Group Events which contains more than one targets in each combine
193// events.
George Liu5b3542e2023-10-31 17:27:12 +0800194void checkGroupEvent(const std::string& directory,
195 const GroupEventPathList& groupEventMatch,
196 GroupEventPathList& groupEventPathList)
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800197{
198 for (const auto& match : groupEventMatch)
199 {
200 const std::string& groupEventName = match.first;
George Liu5b3542e2023-10-31 17:27:12 +0800201 const EventPathList events = match.second;
202 EventPathList pathList;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800203 for (const auto& match : events)
204 {
205 const std::string& eventName = match.first;
206 const std::vector<std::string>& eventAttrs = match.second;
207 for (const auto& eventAttr : eventAttrs)
208 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700209 std::string eventPath = directory;
210 eventPath += "/";
211 eventPath += eventAttr;
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800212 std::ifstream eventFile(eventPath);
213 if (!eventFile.good())
214 {
215 continue;
216 }
217
218 pathList[eventName].push_back(eventPath);
219 }
220 }
221 groupEventPathList[groupEventName] = pathList;
222 }
223}
224
Cheng C Yang58b2b532019-05-31 00:19:45 +0800225// Function checkEventLimits will check all the psu related xxx_input attributes
226// in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm
227// xxx_min_alarm exist, then store the existing paths of the alarm attributes
228// to eventPathList.
George Liu5b3542e2023-10-31 17:27:12 +0800229void checkEventLimits(const std::string& sensorPathStr,
230 const EventPathList& limitEventMatch,
231 EventPathList& eventPathList)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800232{
Lei YUa2c7cea2020-12-23 14:07:28 +0800233 auto attributePartPos = sensorPathStr.find_last_of('_');
234 if (attributePartPos == std::string::npos)
235 {
236 // There is no '_' in the string, skip it
237 return;
238 }
239 auto attributePart =
240 std::string_view(sensorPathStr).substr(attributePartPos + 1);
241 if (attributePart != "input")
242 {
243 // If the sensor is not xxx_input, skip it
244 return;
245 }
246
247 auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800248 for (const auto& limitMatch : limitEventMatch)
249 {
250 const std::vector<std::string>& limitEventAttrs = limitMatch.second;
251 const std::string& eventName = limitMatch.first;
252 for (const auto& limitEventAttr : limitEventAttrs)
253 {
Lei YUa2c7cea2020-12-23 14:07:28 +0800254 auto limitEventPath = prefixPart + limitEventAttr;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800255 std::ifstream eventFile(limitEventPath);
256 if (!eventFile.good())
257 {
258 continue;
259 }
260 eventPathList[eventName].push_back(limitEventPath);
261 }
262 }
263}
Cheng C Yang916360b2019-05-07 18:47:16 +0800264
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530265static void
266 checkPWMSensor(const fs::path& sensorPath, std::string& labelHead,
267 const std::string& interfacePath,
268 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
269 sdbusplus::asio::object_server& objectServer,
270 const std::string& psuName)
Cheng C Yang916360b2019-05-07 18:47:16 +0800271{
Zev Weiss741f26e2022-08-12 18:21:02 -0700272 for (const auto& [pwmLabel, pwmName] : pwmTable)
Cheng C Yang916360b2019-05-07 18:47:16 +0800273 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700274 if (pwmLabel != labelHead)
Cheng C Yang916360b2019-05-07 18:47:16 +0800275 {
276 continue;
277 }
278
279 const std::string& sensorPathStr = sensorPath.string();
280 const std::string& pwmPathStr =
281 boost::replace_all_copy(sensorPathStr, "input", "target");
282 std::ifstream pwmFile(pwmPathStr);
283 if (!pwmFile.good())
284 {
285 continue;
286 }
287
288 auto findPWMSensor = pwmSensors.find(psuName + labelHead);
289 if (findPWMSensor != pwmSensors.end())
290 {
291 continue;
292 }
293
Zev Weissd8c293a2022-08-15 18:58:41 -0700294 std::string name = "Pwm_";
295 name += psuName;
296 name += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700297 name += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700298
299 std::string objPath = interfacePath;
300 objPath += "_";
Zev Weiss741f26e2022-08-12 18:21:02 -0700301 objPath += pwmName;
Zev Weissd8c293a2022-08-15 18:58:41 -0700302
Cheng C Yang916360b2019-05-07 18:47:16 +0800303 pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
Zev Weissd8c293a2022-08-15 18:58:41 -0700304 name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
Cheng C Yang916360b2019-05-07 18:47:16 +0800305 }
306}
307
Zhikui Ren23c96e72020-11-05 22:32:28 -0800308static void createSensorsCallback(
Ed Tanous1f978632023-02-28 18:16:39 -0800309 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -0800310 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
311 const ManagedObjectType& sensorConfigs,
312 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800313 sensorsChanged,
314 bool activateOnly)
Cheng C Yang209ec562019-03-12 16:37:44 +0800315{
Josh Lehan49cfba92019-10-08 16:50:42 -0700316 int numCreated = 0;
Zhikui Ren23c96e72020-11-05 22:32:28 -0800317 bool firstScan = sensorsChanged == nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800318
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800319 auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes);
320
Cheng C Yang209ec562019-03-12 16:37:44 +0800321 std::vector<fs::path> pmbusPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800322 findFiles(fs::path("/sys/bus/iio/devices"), "name", pmbusPaths);
323 findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths);
324 if (pmbusPaths.empty())
Cheng C Yang209ec562019-03-12 16:37:44 +0800325 {
326 std::cerr << "No PSU sensors in system\n";
327 return;
328 }
329
330 boost::container::flat_set<std::string> directories;
331 for (const auto& pmbusPath : pmbusPaths)
332 {
George Liu5b3542e2023-10-31 17:27:12 +0800333 EventPathList eventPathList;
334 GroupEventPathList groupEventPathList;
Cheng C Yang58b2b532019-05-31 00:19:45 +0800335
336 std::ifstream nameFile(pmbusPath);
337 if (!nameFile.good())
338 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700339 std::cerr << "Failure finding pmbus path " << pmbusPath << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800340 continue;
341 }
342
343 std::string pmbusName;
344 std::getline(nameFile, pmbusName);
345 nameFile.close();
Vijay Khemka996bad12019-05-28 15:15:16 -0700346
Matt Simmering786efb82023-01-18 14:09:21 -0800347 if (sensorTypes.find(pmbusName) == sensorTypes.end())
Cheng C Yang58b2b532019-05-31 00:19:45 +0800348 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700349 // To avoid this error message, add your driver name to
350 // the pmbusNames vector at the top of this file.
351 std::cerr << "Driver name " << pmbusName
352 << " not found in sensor whitelist\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800353 continue;
354 }
355
Cheng C Yang209ec562019-03-12 16:37:44 +0800356 auto directory = pmbusPath.parent_path();
357
358 auto ret = directories.insert(directory.string());
359 if (!ret.second)
360 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700361 std::cerr << "Duplicate path " << directory.string() << "\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800362 continue; // check if path has already been searched
Cheng C Yang209ec562019-03-12 16:37:44 +0800363 }
364
Joseph Fub844e972023-03-25 10:24:30 +0800365 DevTypes devType = DevTypes::HWMON;
366 std::string deviceName;
367 if (directory.parent_path() == "/sys/class/hwmon")
368 {
369 deviceName = fs::canonical(directory / "device").stem();
370 }
371 else
372 {
373 deviceName = fs::canonical(directory).parent_path().stem();
374 devType = DevTypes::IIO;
375 }
376
Cheng C Yang209ec562019-03-12 16:37:44 +0800377 size_t bus = 0;
378 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000379 if (!getDeviceBusAddr(deviceName, bus, addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800380 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800381 continue;
382 }
383
Zev Weisse8b97ee2022-08-12 15:23:51 -0700384 const SensorBaseConfigMap* baseConfig = nullptr;
Cheng C Yang209ec562019-03-12 16:37:44 +0800385 const SensorData* sensorData = nullptr;
386 const std::string* interfacePath = nullptr;
Matt Simmering786efb82023-01-18 14:09:21 -0800387 std::string sensorType;
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800388 size_t thresholdConfSize = 0;
Cheng C Yang209ec562019-03-12 16:37:44 +0800389
Zev Weiss741f26e2022-08-12 18:21:02 -0700390 for (const auto& [path, cfgData] : sensorConfigs)
Cheng C Yang209ec562019-03-12 16:37:44 +0800391 {
Zev Weiss741f26e2022-08-12 18:21:02 -0700392 sensorData = &cfgData;
Matt Simmering786efb82023-01-18 14:09:21 -0800393 for (const auto& [type, dt] : sensorTypes)
Cheng C Yang209ec562019-03-12 16:37:44 +0800394 {
Zev Weiss054aad82022-08-18 01:37:34 -0700395 auto sensorBase = sensorData->find(configInterfaceName(type));
Cheng C Yang209ec562019-03-12 16:37:44 +0800396 if (sensorBase != sensorData->end())
397 {
Zev Weisse8b97ee2022-08-12 15:23:51 -0700398 baseConfig = &sensorBase->second;
Cheng C Yang209ec562019-03-12 16:37:44 +0800399 sensorType = type;
400 break;
401 }
402 }
403 if (baseConfig == nullptr)
404 {
405 std::cerr << "error finding base configuration for "
406 << deviceName << "\n";
407 continue;
408 }
409
Zev Weisse8b97ee2022-08-12 15:23:51 -0700410 auto configBus = baseConfig->find("Bus");
411 auto configAddress = baseConfig->find("Address");
Cheng C Yang209ec562019-03-12 16:37:44 +0800412
Zev Weisse8b97ee2022-08-12 15:23:51 -0700413 if (configBus == baseConfig->end() ||
414 configAddress == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800415 {
Cheng C Yang58b2b532019-05-31 00:19:45 +0800416 std::cerr << "error finding necessary entry in configuration\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800417 continue;
418 }
419
Matt Simmering6747eba2023-01-18 16:03:55 -0800420 const uint64_t* confBus =
421 std::get_if<uint64_t>(&(configBus->second));
422 const uint64_t* confAddr =
423 std::get_if<uint64_t>(&(configAddress->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800424 if (confBus == nullptr || confAddr == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800425 {
426 std::cerr
Josh Lehan49cfba92019-10-08 16:50:42 -0700427 << "Cannot get bus or address, invalid configuration\n";
Cheng C Yang58b2b532019-05-31 00:19:45 +0800428 continue;
429 }
430
431 if ((*confBus != bus) || (*confAddr != addr))
Cheng C Yang209ec562019-03-12 16:37:44 +0800432 {
Patrick Rudolph9f270872023-10-25 09:13:09 +0200433 if constexpr (debug)
434 {
435 std::cerr << "Configuration skipping " << *confBus << "-"
436 << *confAddr << " because not " << bus << "-"
437 << addr << "\n";
438 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800439 continue;
440 }
441
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800442 std::vector<thresholds::Threshold> confThresholds;
443 if (!parseThresholdsFromConfig(*sensorData, confThresholds))
444 {
Zhikui Ren85fa3c62022-02-26 23:03:34 -0800445 std::cerr << "error populating total thresholds\n";
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800446 }
447 thresholdConfSize = confThresholds.size();
448
Zev Weiss741f26e2022-08-12 18:21:02 -0700449 interfacePath = &path.str;
Cheng C Yang209ec562019-03-12 16:37:44 +0800450 break;
451 }
452 if (interfacePath == nullptr)
453 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700454 // To avoid this error message, add your export map entry,
455 // from Entity Manager, to sensorTypes at the top of this file.
Cheng C Yang209ec562019-03-12 16:37:44 +0800456 std::cerr << "failed to find match for " << deviceName << "\n";
457 continue;
458 }
459
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800460 auto findI2CDev = devices.find(*interfacePath);
461
462 std::shared_ptr<I2CDevice> i2cDev;
463 if (findI2CDev != devices.end())
464 {
465 if (activateOnly && !findI2CDev->second.second)
466 {
467 continue;
468 }
469 i2cDev = findI2CDev->second.first;
470 }
471
Zev Weisse8b97ee2022-08-12 15:23:51 -0700472 auto findPSUName = baseConfig->find("Name");
473 if (findPSUName == baseConfig->end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800474 {
475 std::cerr << "could not determine configuration name for "
476 << deviceName << "\n";
477 continue;
478 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800479 const std::string* psuName =
480 std::get_if<std::string>(&(findPSUName->second));
Ed Tanousa771f6a2022-01-14 09:36:51 -0800481 if (psuName == nullptr)
Cheng C Yang58b2b532019-05-31 00:19:45 +0800482 {
483 std::cerr << "Cannot find psu name, invalid configuration\n";
484 continue;
485 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800486
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800487 auto findCPU = baseConfig->find("CPURequired");
488 if (findCPU != baseConfig->end())
489 {
Chris Sides3fce1bf2023-04-03 16:57:05 -0500490 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
Matt Simmeringe4e6a282023-03-03 14:41:04 -0800491 auto presenceFind = cpuPresence.find(index);
492 if (presenceFind == cpuPresence.end() || !presenceFind->second)
493 {
494 continue;
495 }
496 }
497
Zhikui Ren23c96e72020-11-05 22:32:28 -0800498 // on rescans, only update sensors we were signaled by
499 if (!firstScan)
500 {
Zhikui Renda98f092021-11-01 09:41:08 -0700501 std::string psuNameStr = "/" + escapeName(*psuName);
Patrick Williams779c96a2023-05-10 07:50:42 -0500502 auto it = std::find_if(sensorsChanged->begin(),
503 sensorsChanged->end(),
504 [psuNameStr](std::string& s) {
Matt Simmering6747eba2023-01-18 16:03:55 -0800505 return s.ends_with(psuNameStr);
Patrick Williams779c96a2023-05-10 07:50:42 -0500506 });
Zhikui Ren23c96e72020-11-05 22:32:28 -0800507
508 if (it == sensorsChanged->end())
509 {
510 continue;
511 }
512 sensorsChanged->erase(it);
513 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800514 checkEvent(directory.string(), eventMatch, eventPathList);
Cheng C Yang202a1ff2020-01-09 09:34:22 +0800515 checkGroupEvent(directory.string(), groupEventMatch,
516 groupEventPathList);
Cheng C Yang58b2b532019-05-31 00:19:45 +0800517
Zev Weisse8b97ee2022-08-12 15:23:51 -0700518 PowerState readState = getPowerState(*baseConfig);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000519
Vijay Khemka996bad12019-05-28 15:15:16 -0700520 /* Check if there are more sensors in the same interface */
521 int i = 1;
522 std::vector<std::string> psuNames;
523 do
524 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700525 // Individual string fields: Name, Name1, Name2, Name3, ...
Zhikui Renda98f092021-11-01 09:41:08 -0700526 psuNames.push_back(
527 escapeName(std::get<std::string>(findPSUName->second)));
Zev Weisse8b97ee2022-08-12 15:23:51 -0700528 findPSUName = baseConfig->find("Name" + std::to_string(i++));
529 } while (findPSUName != baseConfig->end());
Vijay Khemka996bad12019-05-28 15:15:16 -0700530
Cheng C Yange50345b2019-04-02 17:26:15 +0800531 std::vector<fs::path> sensorPaths;
Joseph Fub844e972023-03-25 10:24:30 +0800532 if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths,
533 0))
Cheng C Yang209ec562019-03-12 16:37:44 +0800534 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800535 std::cerr << "No PSU non-label sensor in PSU\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800536 continue;
537 }
538
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530539 /* read max value in sysfs for in, curr, power, temp, ... */
540 if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0))
541 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700542 if constexpr (debug)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530543 {
544 std::cerr << "No max name in PSU \n";
545 }
546 }
547
Zev Weiss8569bf22022-10-11 15:37:44 -0700548 float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll);
Lei YU7170a232021-02-04 16:19:27 +0800549
Vijay Khemka996bad12019-05-28 15:15:16 -0700550 /* Find array of labels to be exposed if it is defined in config */
551 std::vector<std::string> findLabels;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700552 auto findLabelObj = baseConfig->find("Labels");
553 if (findLabelObj != baseConfig->end())
Vijay Khemka996bad12019-05-28 15:15:16 -0700554 {
555 findLabels =
556 std::get<std::vector<std::string>>(findLabelObj->second);
557 }
558
Joseph Fub844e972023-03-25 10:24:30 +0800559 std::regex sensorNameRegEx(devParamMap[devType].nameRegEx);
Jason Ling5747fab2019-10-02 16:46:23 -0700560 std::smatch matches;
561
Cheng C Yange50345b2019-04-02 17:26:15 +0800562 for (const auto& sensorPath : sensorPaths)
Cheng C Yang209ec562019-03-12 16:37:44 +0800563 {
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530564 bool maxLabel = false;
Cheng C Yange50345b2019-04-02 17:26:15 +0800565 std::string labelHead;
566 std::string sensorPathStr = sensorPath.string();
567 std::string sensorNameStr = sensorPath.filename();
Ed Tanous2049bd22022-07-09 07:20:26 -0700568 std::string sensorNameSubStr;
Jason Ling5747fab2019-10-02 16:46:23 -0700569 if (std::regex_search(sensorNameStr, matches, sensorNameRegEx))
570 {
Josh Lehan06494452019-10-31 09:49:16 -0700571 // hwmon *_input filename without number:
572 // in, curr, power, temp, ...
Joseph Fub844e972023-03-25 10:24:30 +0800573 // iio in_*_raw filename without number:
574 // voltage, temp, pressure, ...
575 sensorNameSubStr = matches[devParamMap[devType].matchIndex];
Jason Ling5747fab2019-10-02 16:46:23 -0700576 }
577 else
578 {
Josh Lehan06494452019-10-31 09:49:16 -0700579 std::cerr << "Could not extract the alpha prefix from "
Jason Ling5747fab2019-10-02 16:46:23 -0700580 << sensorNameStr;
581 continue;
582 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800583
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530584 std::string labelPath;
585
Joseph Fub844e972023-03-25 10:24:30 +0800586 if (devType == DevTypes::HWMON)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530587 {
Joseph Fub844e972023-03-25 10:24:30 +0800588 /* find and differentiate _max and _input to replace "label" */
589 size_t pos = sensorPathStr.find('_');
590 if (pos != std::string::npos)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530591 {
Joseph Fub844e972023-03-25 10:24:30 +0800592 std::string sensorPathStrMax = sensorPathStr.substr(pos);
593 if (sensorPathStrMax == "_max")
594 {
595 labelPath = boost::replace_all_copy(sensorPathStr,
596 "max", "label");
597 maxLabel = true;
598 }
599 else
600 {
601 labelPath = boost::replace_all_copy(sensorPathStr,
602 "input", "label");
603 maxLabel = false;
604 }
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530605 }
606 else
607 {
Cheng C Yange50345b2019-04-02 17:26:15 +0800608 continue;
609 }
610
Joseph Fub844e972023-03-25 10:24:30 +0800611 std::ifstream labelFile(labelPath);
612 if (!labelFile.good())
613 {
614 if constexpr (debug)
615 {
616 std::cerr << "Input file " << sensorPath
617 << " has no corresponding label file\n";
618 }
619 // hwmon *_input filename with number:
620 // temp1, temp2, temp3, ...
621 labelHead = sensorNameStr.substr(0,
622 sensorNameStr.find('_'));
623 }
624 else
625 {
626 std::string label;
627 std::getline(labelFile, label);
628 labelFile.close();
629 auto findSensor = sensors.find(label);
630 if (findSensor != sensors.end())
631 {
632 continue;
633 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800634
Joseph Fub844e972023-03-25 10:24:30 +0800635 // hwmon corresponding *_label file contents:
636 // vin1, vout1, ...
637 labelHead = label.substr(0, label.find(' '));
638 }
639
640 /* append "max" for labelMatch */
641 if (maxLabel)
642 {
643 labelHead.insert(0, "max");
644 }
645
646 checkPWMSensor(sensorPath, labelHead, *interfacePath,
647 dbusConnection, objectServer, psuNames[0]);
648 }
649 else if (devType == DevTypes::IIO)
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530650 {
Joseph Fub844e972023-03-25 10:24:30 +0800651 auto findIIOHyphen = sensorNameStr.find_last_of('_');
652 labelHead = sensorNameStr.substr(0, findIIOHyphen);
Manikandan Elumalaic7e95622020-06-03 20:22:01 +0530653 }
654
Ed Tanous8a57ec02020-10-09 12:46:52 -0700655 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700656 {
657 std::cerr << "Sensor type=\"" << sensorNameSubStr
658 << "\" label=\"" << labelHead << "\"\n";
659 }
660
Vijay Khemka996bad12019-05-28 15:15:16 -0700661 if (!findLabels.empty())
662 {
663 /* Check if this labelHead is enabled in config file */
664 if (std::find(findLabels.begin(), findLabels.end(),
665 labelHead) == findLabels.end())
666 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700667 if constexpr (debug)
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800668 {
669 std::cerr << "could not find " << labelHead
670 << " in the Labels list\n";
671 }
Vijay Khemka996bad12019-05-28 15:15:16 -0700672 continue;
673 }
674 }
Cheng C Yange50345b2019-04-02 17:26:15 +0800675
Cheng C Yange50345b2019-04-02 17:26:15 +0800676 auto findProperty = labelMatch.find(labelHead);
677 if (findProperty == labelMatch.end())
Cheng C Yang209ec562019-03-12 16:37:44 +0800678 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700679 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700680 {
681 std::cerr << "Could not find matching default property for "
682 << labelHead << "\n";
683 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800684 continue;
685 }
686
Josh Lehan74d9bd92019-10-31 08:51:58 -0700687 // Protect the hardcoded labelMatch list from changes,
688 // by making a copy and modifying that instead.
689 // Avoid bleedthrough of one device's customizations to
690 // the next device, as each should be independently customizable.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800691 PSUProperty psuProperty = findProperty->second;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700692
693 // Use label head as prefix for reading from config file,
694 // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
695 std::string keyName = labelHead + "_Name";
696 std::string keyScale = labelHead + "_Scale";
697 std::string keyMin = labelHead + "_Min";
698 std::string keyMax = labelHead + "_Max";
Jeff Line41d52f2021-04-07 19:38:51 +0800699 std::string keyOffset = labelHead + "_Offset";
Lotus Xucb5af732021-09-10 15:18:50 +0800700 std::string keyPowerState = labelHead + "_PowerState";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700701
702 bool customizedName = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700703 auto findCustomName = baseConfig->find(keyName);
704 if (findCustomName != baseConfig->end())
Josh Lehan432d1ed2019-10-16 12:23:31 -0700705 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700706 try
707 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800708 psuProperty.labelTypeName = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700709 VariantToStringVisitor(), findCustomName->second);
710 }
Patrick Williams26601e82021-10-06 12:43:25 -0500711 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700712 {
713 std::cerr << "Unable to parse " << keyName << "\n";
714 continue;
715 }
716
717 // All strings are valid, including empty string
718 customizedName = true;
719 }
720
721 bool customizedScale = false;
Zev Weisse8b97ee2022-08-12 15:23:51 -0700722 auto findCustomScale = baseConfig->find(keyScale);
723 if (findCustomScale != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700724 {
725 try
726 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800727 psuProperty.sensorScaleFactor = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700728 VariantToUnsignedIntVisitor(), findCustomScale->second);
729 }
Patrick Williams26601e82021-10-06 12:43:25 -0500730 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700731 {
732 std::cerr << "Unable to parse " << keyScale << "\n";
733 continue;
734 }
735
736 // Avoid later division by zero
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800737 if (psuProperty.sensorScaleFactor > 0)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700738 {
739 customizedScale = true;
740 }
741 else
742 {
743 std::cerr << "Unable to accept " << keyScale << "\n";
744 continue;
745 }
746 }
747
Zev Weisse8b97ee2022-08-12 15:23:51 -0700748 auto findCustomMin = baseConfig->find(keyMin);
749 if (findCustomMin != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700750 {
751 try
752 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800753 psuProperty.minReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700754 VariantToDoubleVisitor(), findCustomMin->second);
755 }
Patrick Williams26601e82021-10-06 12:43:25 -0500756 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700757 {
758 std::cerr << "Unable to parse " << keyMin << "\n";
759 continue;
760 }
761 }
762
Zev Weisse8b97ee2022-08-12 15:23:51 -0700763 auto findCustomMax = baseConfig->find(keyMax);
764 if (findCustomMax != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700765 {
766 try
767 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800768 psuProperty.maxReading = std::visit(
Josh Lehan74d9bd92019-10-31 08:51:58 -0700769 VariantToDoubleVisitor(), findCustomMax->second);
770 }
Patrick Williams26601e82021-10-06 12:43:25 -0500771 catch (const std::invalid_argument&)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700772 {
773 std::cerr << "Unable to parse " << keyMax << "\n";
774 continue;
775 }
776 }
777
Zev Weisse8b97ee2022-08-12 15:23:51 -0700778 auto findCustomOffset = baseConfig->find(keyOffset);
779 if (findCustomOffset != baseConfig->end())
Jeff Line41d52f2021-04-07 19:38:51 +0800780 {
781 try
782 {
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800783 psuProperty.sensorOffset = std::visit(
Jeff Line41d52f2021-04-07 19:38:51 +0800784 VariantToDoubleVisitor(), findCustomOffset->second);
785 }
Patrick Williams26601e82021-10-06 12:43:25 -0500786 catch (const std::invalid_argument&)
Jeff Line41d52f2021-04-07 19:38:51 +0800787 {
788 std::cerr << "Unable to parse " << keyOffset << "\n";
789 continue;
790 }
791 }
792
Lotus Xucb5af732021-09-10 15:18:50 +0800793 // if we find label head power state set ,override the powerstate.
Zev Weisse8b97ee2022-08-12 15:23:51 -0700794 auto findPowerState = baseConfig->find(keyPowerState);
795 if (findPowerState != baseConfig->end())
Lotus Xucb5af732021-09-10 15:18:50 +0800796 {
797 std::string powerState = std::visit(VariantToStringVisitor(),
798 findPowerState->second);
799 setReadState(powerState, readState);
800 }
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800801 if (!(psuProperty.minReading < psuProperty.maxReading))
Josh Lehan74d9bd92019-10-31 08:51:58 -0700802 {
803 std::cerr << "Min must be less than Max\n";
804 continue;
805 }
806
807 // If the sensor name is being customized by config file,
808 // then prefix/suffix composition becomes not necessary,
809 // and in fact not wanted, because it gets in the way.
810 std::string psuNameFromIndex;
811 if (!customizedName)
812 {
813 /* Find out sensor name index for this label */
814 std::regex rgx("[A-Za-z]+([0-9]+)");
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500815 size_t nameIndex{0};
Josh Lehan74d9bd92019-10-31 08:51:58 -0700816 if (std::regex_search(labelHead, matches, rgx))
817 {
818 nameIndex = std::stoi(matches[1]);
819
820 // Decrement to preserve alignment, because hwmon
821 // human-readable filenames and labels use 1-based
822 // numbering, but the "Name", "Name1", "Name2", etc. naming
823 // convention (the psuNames vector) uses 0-based numbering.
824 if (nameIndex > 0)
825 {
826 --nameIndex;
827 }
828 }
829 else
830 {
831 nameIndex = 0;
832 }
833
834 if (psuNames.size() <= nameIndex)
835 {
836 std::cerr << "Could not pair " << labelHead
837 << " with a Name field\n";
838 continue;
839 }
840
841 psuNameFromIndex = psuNames[nameIndex];
842
Ed Tanous8a57ec02020-10-09 12:46:52 -0700843 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700844 {
845 std::cerr << "Sensor label head " << labelHead
846 << " paired with " << psuNameFromIndex
847 << " at index " << nameIndex << "\n";
848 }
Josh Lehan432d1ed2019-10-16 12:23:31 -0700849 }
850
Joseph Fub844e972023-03-25 10:24:30 +0800851 if (devType == DevTypes::HWMON)
852 {
853 checkEventLimits(sensorPathStr, limitEventMatch, eventPathList);
854 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800855
Josh Lehan74d9bd92019-10-31 08:51:58 -0700856 // Similarly, if sensor scaling factor is being customized,
857 // then the below power-of-10 constraint becomes unnecessary,
858 // as config should be able to specify an arbitrary divisor.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800859 unsigned int factor = psuProperty.sensorScaleFactor;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700860 if (!customizedScale)
Vijay Khemka53ca4442019-07-23 11:03:55 -0700861 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700862 // Preserve existing usage of hardcoded labelMatch table below
863 factor = std::pow(10.0, factor);
Vijay Khemka53ca4442019-07-23 11:03:55 -0700864
Josh Lehan74d9bd92019-10-31 08:51:58 -0700865 /* Change first char of substring to uppercase */
Ed Tanous8a57ec02020-10-09 12:46:52 -0700866 char firstChar =
867 static_cast<char>(std::toupper(sensorNameSubStr[0]));
Josh Lehan74d9bd92019-10-31 08:51:58 -0700868 std::string strScaleFactor =
869 firstChar + sensorNameSubStr.substr(1) + "ScaleFactor";
870
871 // Preserve existing configs by accepting earlier syntax,
872 // example CurrScaleFactor, PowerScaleFactor, ...
Zev Weisse8b97ee2022-08-12 15:23:51 -0700873 auto findScaleFactor = baseConfig->find(strScaleFactor);
874 if (findScaleFactor != baseConfig->end())
Josh Lehan74d9bd92019-10-31 08:51:58 -0700875 {
876 factor = std::visit(VariantToIntVisitor(),
877 findScaleFactor->second);
878 }
879
Ed Tanous8a57ec02020-10-09 12:46:52 -0700880 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700881 {
882 std::cerr << "Sensor scaling factor " << factor
883 << " string " << strScaleFactor << "\n";
884 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700885 }
886
Vijay Khemka996bad12019-05-28 15:15:16 -0700887 std::vector<thresholds::Threshold> sensorThresholds;
Joshi, Mansi14f0ad82019-11-21 10:52:30 +0530888 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
889 &labelHead))
Cheng C Yange50345b2019-04-02 17:26:15 +0800890 {
James Feist17ab6e02019-06-25 12:28:13 -0700891 std::cerr << "error populating thresholds for "
892 << sensorNameSubStr << "\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800893 }
894
Zev Weiss6b6891c2021-04-22 02:46:21 -0500895 auto findSensorUnit = sensorTable.find(sensorNameSubStr);
896 if (findSensorUnit == sensorTable.end())
Cheng C Yange50345b2019-04-02 17:26:15 +0800897 {
Jason Ling5747fab2019-10-02 16:46:23 -0700898 std::cerr << sensorNameSubStr
Josh Lehan06494452019-10-31 09:49:16 -0700899 << " is not a recognized sensor type\n";
Cheng C Yange50345b2019-04-02 17:26:15 +0800900 continue;
901 }
902
Ed Tanous8a57ec02020-10-09 12:46:52 -0700903 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700904 {
Josh Lehan74d9bd92019-10-31 08:51:58 -0700905 std::cerr << "Sensor properties: Name \""
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800906 << psuProperty.labelTypeName << "\" Scale "
907 << psuProperty.sensorScaleFactor << " Min "
908 << psuProperty.minReading << " Max "
909 << psuProperty.maxReading << " Offset "
910 << psuProperty.sensorOffset << "\n";
Josh Lehan74d9bd92019-10-31 08:51:58 -0700911 }
912
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800913 std::string sensorName = psuProperty.labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700914 if (customizedName)
915 {
916 if (sensorName.empty())
917 {
918 // Allow selective disabling of an individual sensor,
919 // by customizing its name to an empty string.
920 std::cerr << "Sensor disabled, empty string\n";
921 continue;
922 }
923 }
924 else
925 {
926 // Sensor name not customized, do prefix/suffix composition,
927 // preserving default behavior by using psuNameFromIndex.
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800928 sensorName = psuNameFromIndex + " " + psuProperty.labelTypeName;
Josh Lehan74d9bd92019-10-31 08:51:58 -0700929 }
930
Ed Tanous8a57ec02020-10-09 12:46:52 -0700931 if constexpr (debug)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700932 {
933 std::cerr << "Sensor name \"" << sensorName << "\" path \""
934 << sensorPathStr << "\" type \"" << sensorType
935 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -0700936 }
Zhikui Ren23c96e72020-11-05 22:32:28 -0800937 // destruct existing one first if already created
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800938
939 auto& sensor = sensors[sensorName];
940 if (!activateOnly)
Josh Lehan74d9bd92019-10-31 08:51:58 -0700941 {
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800942 sensor = nullptr;
943 }
944
945 if (sensor != nullptr)
946 {
947 sensor->activate(sensorPathStr, i2cDev);
948 }
949 else
950 {
951 sensors[sensorName] = std::make_shared<PSUSensor>(
952 sensorPathStr, sensorType, objectServer, dbusConnection, io,
953 sensorName, std::move(sensorThresholds), *interfacePath,
954 readState, findSensorUnit->second, factor,
Zhikui Ren7c2e7c92024-03-07 14:11:25 -0800955 psuProperty.maxReading, psuProperty.minReading,
956 psuProperty.sensorOffset, labelHead, thresholdConfSize,
Matt Simmeringcafd72f2022-12-16 15:35:12 -0800957 pollRate, i2cDev);
958 sensors[sensorName]->setupRead();
959 ++numCreated;
960 if constexpr (debug)
961 {
962 std::cerr << "Created " << numCreated
963 << " sensors so far\n";
964 }
Josh Lehan74d9bd92019-10-31 08:51:58 -0700965 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800966 }
Cheng C Yang58b2b532019-05-31 00:19:45 +0800967
Joseph Fub844e972023-03-25 10:24:30 +0800968 if (devType == DevTypes::HWMON)
969 {
970 // OperationalStatus event
971 combineEvents[*psuName + "OperationalStatus"] = nullptr;
972 combineEvents[*psuName + "OperationalStatus"] =
973 std::make_unique<PSUCombineEvent>(
974 objectServer, dbusConnection, io, *psuName, readState,
975 eventPathList, groupEventPathList, "OperationalStatus",
976 pollRate);
977 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800978 }
Josh Lehan49cfba92019-10-08 16:50:42 -0700979
Ed Tanous8a57ec02020-10-09 12:46:52 -0700980 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -0700981 {
982 std::cerr << "Created total of " << numCreated << " sensors\n";
983 }
Matt Simmering6747eba2023-01-18 16:03:55 -0800984}
Cheng C Yang209ec562019-03-12 16:37:44 +0800985
Matt Simmering39fadb92023-05-04 21:47:29 -0700986static void
987 getPresentCpus(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
988{
989 static const int depth = 2;
990 static const int numKeys = 1;
991 GetSubTreeType cpuSubTree;
992
993 try
994 {
995 auto getItems = dbusConnection->new_method_call(
996 mapper::busName, mapper::path, mapper::interface, mapper::subtree);
997 getItems.append(cpuInventoryPath, static_cast<int32_t>(depth),
998 std::array<const char*, numKeys>{
999 "xyz.openbmc_project.Inventory.Item"});
1000 auto getItemsResp = dbusConnection->call(getItems);
1001 getItemsResp.read(cpuSubTree);
1002 }
1003 catch (sdbusplus::exception_t& e)
1004 {
1005 std::cerr << "error getting inventory item subtree: " << e.what()
1006 << "\n";
1007 return;
1008 }
1009
1010 for (const auto& [path, objDict] : cpuSubTree)
1011 {
1012 auto obj = sdbusplus::message::object_path(path).filename();
1013 if (!obj.starts_with("cpu") || objDict.empty())
1014 {
1015 continue;
1016 }
1017 const std::string& owner = objDict.begin()->first;
1018
1019 std::variant<bool> respValue;
1020 try
1021 {
1022 auto getPresence = dbusConnection->new_method_call(
1023 owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
1024 "Get");
1025 getPresence.append("xyz.openbmc_project.Inventory.Item", "Present");
1026 auto resp = dbusConnection->call(getPresence);
1027 resp.read(respValue);
1028 }
1029 catch (sdbusplus::exception_t& e)
1030 {
1031 std::cerr << "Error in getting CPU presence: " << e.what() << "\n";
1032 continue;
1033 }
1034
1035 auto* present = std::get_if<bool>(&respValue);
1036 if (present != nullptr && *present)
1037 {
1038 int cpuIndex = 0;
1039 try
1040 {
1041 cpuIndex = std::stoi(obj.substr(obj.find_last_of("cpu") + 1));
1042 }
1043 catch (const std::exception& e)
1044 {
1045 std::cerr << "Error converting CPU index, " << e.what() << '\n';
1046 continue;
1047 }
1048 cpuPresence[cpuIndex + 1] = *present;
1049 }
1050 }
1051}
1052
Zhikui Ren23c96e72020-11-05 22:32:28 -08001053void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -08001054 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Zhikui Ren23c96e72020-11-05 22:32:28 -08001055 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
1056 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001057 sensorsChanged,
1058 bool activateOnly)
Zhikui Ren23c96e72020-11-05 22:32:28 -08001059{
1060 auto getter = std::make_shared<GetSensorConfiguration>(
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001061 dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged,
1062 activateOnly](const ManagedObjectType& sensorConfigs) {
Patrick Williams597e8422023-10-20 11:19:01 -05001063 createSensorsCallback(io, objectServer, dbusConnection, sensorConfigs,
1064 sensorsChanged, activateOnly);
1065 });
Matt Simmering786efb82023-01-18 14:09:21 -08001066 std::vector<std::string> types(sensorTypes.size());
1067 for (const auto& [type, dt] : sensorTypes)
1068 {
1069 types.push_back(type);
1070 }
1071 getter->getConfiguration(types);
Zhikui Ren23c96e72020-11-05 22:32:28 -08001072}
1073
Ed Tanous201a1012024-04-03 18:07:28 -07001074void propertyInitialize()
Cheng C Yang209ec562019-03-12 16:37:44 +08001075{
Zev Weiss6b6891c2021-04-22 02:46:21 -05001076 sensorTable = {{"power", sensor_paths::unitWatts},
1077 {"curr", sensor_paths::unitAmperes},
1078 {"temp", sensor_paths::unitDegreesC},
1079 {"in", sensor_paths::unitVolts},
Joseph Fub844e972023-03-25 10:24:30 +08001080 {"voltage", sensor_paths::unitVolts},
Zev Weiss6b6891c2021-04-22 02:46:21 -05001081 {"fan", sensor_paths::unitRPMs}};
Cheng C Yange50345b2019-04-02 17:26:15 +08001082
Jeff Line41d52f2021-04-07 19:38:51 +08001083 labelMatch = {
1084 {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001085 {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
1086 {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001087 {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
1088 {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1089 {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1090 {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001091 {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
1092 {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
1093 {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001094 {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
1095 {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Patrick Rudolph0874ebf2023-04-19 08:59:35 +02001096 {"vin1", PSUProperty("Input Voltage", 300, 0, 3, 0)},
1097 {"vin2", PSUProperty("Input Voltage", 300, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001098 {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
Joseph Fub844e972023-03-25 10:24:30 +08001099 {"in_voltage0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1100 {"in_voltage1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1101 {"in_voltage2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1102 {"in_voltage3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001103 {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1104 {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1105 {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1106 {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1107 {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1108 {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1109 {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1110 {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1111 {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1112 {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1113 {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1114 {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1115 {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1116 {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1117 {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1118 {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1119 {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1120 {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1121 {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1122 {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1123 {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1124 {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1125 {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1126 {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1127 {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1128 {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1129 {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1130 {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1131 {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1132 {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1133 {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1134 {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1135 {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001136 {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001137 {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Tim Chao6f379ce2022-02-24 11:08:09 +08001138 {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1139 {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1140 {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1141 {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1142 {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
1143 {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001144 {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001145 {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
1146 {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001147 {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
1148 {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
1149 {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
1150 {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
1151 {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
1152 {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
1153 {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
1154 {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
1155 {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
1156 {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
1157 {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
1158 {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
1159 {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
1160 {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
1161 {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
Zhikui Ren85fa3c62022-02-26 23:03:34 -08001162 {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
1163 {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
1164 {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
Jeff Line41d52f2021-04-07 19:38:51 +08001165 {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
1166 {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
1167 {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
1168 {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
1169 {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
1170 {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
1171 {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
1172 {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
1173 {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
Zev Weissea2db482022-06-20 18:12:53 -07001174 {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
1175 {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
1176 {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
Cheng C Yang916360b2019-05-07 18:47:16 +08001177
Matt Simmering6747eba2023-01-18 16:03:55 -08001178 pwmTable = {{"fan1", "Fan_1"},
1179 {"fan2", "Fan_2"},
1180 {"fan3", "Fan_3"},
1181 {"fan4", "Fan_4"}};
Cheng C Yang58b2b532019-05-31 00:19:45 +08001182
1183 limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
1184 {"Failure", {"crit_alarm", "lcrit_alarm"}}};
1185
Cheng C Yang202a1ff2020-01-09 09:34:22 +08001186 eventMatch = {{"PredictiveFailure", {"power1_alarm"}},
1187 {"Failure", {"in2_alarm"}},
1188 {"ACLost", {"in1_beep"}},
1189 {"ConfigureError", {"in1_fault"}}};
1190
1191 groupEventMatch = {{"FanFault",
1192 {{"fan1", {"fan1_alarm", "fan1_fault"}},
Zev Weissea2db482022-06-20 18:12:53 -07001193 {"fan2", {"fan2_alarm", "fan2_fault"}},
1194 {"fan3", {"fan3_alarm", "fan3_fault"}},
1195 {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
Joseph Fub844e972023-03-25 10:24:30 +08001196
1197 devParamMap = {
1198 {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
1199 {DevTypes::IIO,
1200 {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}};
Cheng C Yang209ec562019-03-12 16:37:44 +08001201}
1202
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001203static void powerStateChanged(
1204 PowerState type, bool newState,
1205 boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>&
1206 sensors,
1207 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
1208 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
1209{
1210 if (newState)
1211 {
1212 createSensors(io, objectServer, dbusConnection, nullptr, true);
1213 }
1214 else
1215 {
1216 for (auto& [path, sensor] : sensors)
1217 {
1218 if (sensor != nullptr && sensor->readState == type)
1219 {
1220 sensor->deactivate();
1221 }
1222 }
1223 }
1224}
1225
James Feistb6c0b912019-07-09 12:21:44 -07001226int main()
Cheng C Yang209ec562019-03-12 16:37:44 +08001227{
Ed Tanous1f978632023-02-28 18:16:39 -08001228 boost::asio::io_context io;
Cheng C Yang209ec562019-03-12 16:37:44 +08001229 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1230
Ed Tanous14ed5e92022-07-12 15:50:23 -07001231 sdbusplus::asio::object_server objectServer(systemBus, true);
1232 objectServer.add_manager("/xyz/openbmc_project/sensors");
Zhikui Ren3fe3f542022-11-02 16:37:54 -07001233 objectServer.add_manager("/xyz/openbmc_project/control");
Cheng C Yang209ec562019-03-12 16:37:44 +08001234 systemBus->request_name("xyz.openbmc_project.PSUSensor");
Zhikui Ren23c96e72020-11-05 22:32:28 -08001235 auto sensorsChanged =
1236 std::make_shared<boost::container::flat_set<std::string>>();
Cheng C Yang209ec562019-03-12 16:37:44 +08001237
Cheng C Yang916360b2019-05-07 18:47:16 +08001238 propertyInitialize();
Cheng C Yang209ec562019-03-12 16:37:44 +08001239
Patrick Williams597e8422023-10-20 11:19:01 -05001240 auto powerCallBack = [&io, &objectServer, &systemBus](PowerState type,
1241 bool state) {
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001242 powerStateChanged(type, state, sensors, io, objectServer, systemBus);
1243 };
1244
1245 setupPowerMatchCallback(systemBus, powerCallBack);
1246
1247 boost::asio::post(io, [&]() {
1248 createSensors(io, objectServer, systemBus, nullptr, false);
1249 });
Ed Tanous9b4a20e2022-09-06 08:47:11 -07001250 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -05001251 std::function<void(sdbusplus::message_t&)> eventHandler =
1252 [&](sdbusplus::message_t& message) {
Matt Simmering6747eba2023-01-18 16:03:55 -08001253 if (message.is_method_error())
1254 {
1255 std::cerr << "callback method error\n";
1256 return;
1257 }
1258 sensorsChanged->insert(message.get_path());
Ed Tanous83db50c2023-03-01 10:20:24 -08001259 filterTimer.expires_after(std::chrono::seconds(3));
Matt Simmering6747eba2023-01-18 16:03:55 -08001260 filterTimer.async_wait([&](const boost::system::error_code& ec) {
1261 if (ec == boost::asio::error::operation_aborted)
Cheng C Yang209ec562019-03-12 16:37:44 +08001262 {
Cheng C Yang209ec562019-03-12 16:37:44 +08001263 return;
1264 }
Matt Simmering6747eba2023-01-18 16:03:55 -08001265 if (ec)
1266 {
1267 std::cerr << "timer error\n";
1268 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001269 createSensors(io, objectServer, systemBus, sensorsChanged, false);
Matt Simmering6747eba2023-01-18 16:03:55 -08001270 });
1271 };
Cheng C Yang209ec562019-03-12 16:37:44 +08001272
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001273 boost::asio::steady_timer cpuFilterTimer(io);
1274 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
1275 [&](sdbusplus::message_t& message) {
1276 std::string path = message.get_path();
1277 boost::to_lower(path);
1278
1279 sdbusplus::message::object_path cpuPath(path);
1280 std::string cpuName = cpuPath.filename();
1281 if (!cpuName.starts_with("cpu"))
1282 {
1283 return;
1284 }
1285 size_t index = 0;
1286 try
1287 {
1288 index = std::stoi(path.substr(path.size() - 1));
1289 }
1290 catch (const std::invalid_argument&)
1291 {
1292 std::cerr << "Found invalid path " << path << "\n";
1293 return;
1294 }
1295
1296 std::string objectName;
1297 boost::container::flat_map<std::string, std::variant<bool>> values;
1298 message.read(objectName, values);
1299 auto findPresence = values.find("Present");
1300 try
1301 {
1302 cpuPresence[index] = std::get<bool>(findPresence->second);
1303 }
Chris Sides3fce1bf2023-04-03 16:57:05 -05001304 catch (const std::bad_variant_access& err)
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001305 {
1306 return;
1307 }
1308
Chris Sides3fce1bf2023-04-03 16:57:05 -05001309 if (!cpuPresence[index])
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001310 {
1311 return;
1312 }
1313 cpuFilterTimer.expires_after(std::chrono::seconds(1));
1314 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
1315 if (ec == boost::asio::error::operation_aborted)
1316 {
1317 return;
1318 }
1319 if (ec)
1320 {
1321 std::cerr << "timer error\n";
1322 return;
1323 }
Matt Simmeringcafd72f2022-12-16 15:35:12 -08001324 createSensors(io, objectServer, systemBus, nullptr, false);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001325 });
1326 };
1327
Zev Weiss214d9712022-08-12 12:54:31 -07001328 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
1329 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Matt Simmeringe4e6a282023-03-03 14:41:04 -08001330
1331 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
1332 static_cast<sdbusplus::bus_t&>(*systemBus),
1333 "type='signal',member='PropertiesChanged',path_namespace='" +
1334 std::string(cpuInventoryPath) +
1335 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
1336 cpuPresenceHandler));
1337
Matt Simmering39fadb92023-05-04 21:47:29 -07001338 getPresentCpus(systemBus);
1339
Bruce Lee1263c3d2021-06-04 15:16:33 +08001340 setupManufacturingModeMatch(*systemBus);
Cheng C Yang209ec562019-03-12 16:37:44 +08001341 io.run();
1342}