Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1 | /* |
| 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 Lehan | 0830c7b | 2019-10-08 16:35:09 -0700 | [diff] [blame] | 16 | |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 17 | #include "DeviceMgmt.hpp" |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 18 | #include "PSUEvent.hpp" |
| 19 | #include "PSUSensor.hpp" |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 20 | #include "PwmSensor.hpp" |
| 21 | #include "SensorPaths.hpp" |
| 22 | #include "Thresholds.hpp" |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 23 | #include "Utils.hpp" |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 24 | #include "VariantVisitors.hpp" |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 25 | |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 26 | #include <boost/algorithm/string/case_conv.hpp> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 27 | #include <boost/algorithm/string/replace.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 28 | #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 Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 32 | #include <boost/container/flat_map.hpp> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 33 | #include <boost/container/flat_set.hpp> |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 34 | #include <phosphor-logging/lg2.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 35 | #include <sdbusplus/asio/connection.hpp> |
| 36 | #include <sdbusplus/asio/object_server.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 37 | #include <sdbusplus/bus.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 38 | #include <sdbusplus/bus/match.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 39 | #include <sdbusplus/exception.hpp> |
| 40 | #include <sdbusplus/message.hpp> |
| 41 | #include <sdbusplus/message/native_types.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 42 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 43 | #include <algorithm> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 44 | #include <array> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 45 | #include <cctype> |
| 46 | #include <chrono> |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 47 | #include <cmath> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 48 | #include <cstddef> |
| 49 | #include <cstdint> |
| 50 | #include <exception> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 51 | #include <filesystem> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 52 | #include <fstream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 53 | #include <functional> |
Jayaprakash Mutyala | a205c6f | 2025-02-13 06:09:04 +0000 | [diff] [blame] | 54 | #include <iterator> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 55 | #include <memory> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 56 | #include <regex> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 57 | #include <stdexcept> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 58 | #include <string> |
Lei YU | a2c7cea | 2020-12-23 14:07:28 +0800 | [diff] [blame] | 59 | #include <string_view> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 60 | #include <utility> |
| 61 | #include <variant> |
| 62 | #include <vector> |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 63 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 64 | static constexpr bool debug = false; |
Chau Ly | c4ddf64 | 2024-07-09 05:01:06 +0000 | [diff] [blame] | 65 | static std::regex i2cDevRegex(R"((\/i2c\-\d+\/\d+-[a-fA-F0-9]{4,4})(\/|$))"); |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 66 | |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 67 | static const I2CDeviceTypeMap sensorTypes{ |
Delphine CC Chiu | 436c5bd | 2023-09-22 09:47:43 +0800 | [diff] [blame] | 68 | {"ADC128D818", I2CDeviceType{"adc128d818", true}}, |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 69 | {"ADM1266", I2CDeviceType{"adm1266", true}}, |
| 70 | {"ADM1272", I2CDeviceType{"adm1272", true}}, |
| 71 | {"ADM1275", I2CDeviceType{"adm1275", true}}, |
| 72 | {"ADM1278", I2CDeviceType{"adm1278", true}}, |
| 73 | {"ADM1293", I2CDeviceType{"adm1293", true}}, |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 74 | {"ADS1015", I2CDeviceType{"ads1015", true}}, |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 75 | {"ADS7830", I2CDeviceType{"ads7830", true}}, |
| 76 | {"AHE50DC_FAN", I2CDeviceType{"ahe50dc_fan", true}}, |
| 77 | {"BMR490", I2CDeviceType{"bmr490", true}}, |
Alexander Hansen | 90ef14d | 2023-08-01 17:12:17 +0200 | [diff] [blame] | 78 | {"cffps", I2CDeviceType{"cffps", true}}, |
| 79 | {"cffps1", I2CDeviceType{"cffps", true}}, |
| 80 | {"cffps2", I2CDeviceType{"cffps", true}}, |
| 81 | {"cffps3", I2CDeviceType{"cffps", true}}, |
Andrew Geissler | b2392ef | 2024-12-10 09:39:19 -0600 | [diff] [blame] | 82 | {"CRPS185", I2CDeviceType{"crps185", true}}, |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 83 | {"DPS800", I2CDeviceType{"dps800", true}}, |
| 84 | {"INA219", I2CDeviceType{"ina219", true}}, |
| 85 | {"INA230", I2CDeviceType{"ina230", true}}, |
Potin Lai | 5674ac9 | 2023-11-20 14:33:56 +0800 | [diff] [blame] | 86 | {"INA238", I2CDeviceType{"ina238", true}}, |
Jian Zhang | aff100c | 2023-07-26 01:22:50 +0800 | [diff] [blame] | 87 | {"IPSPS1", I2CDeviceType{"ipsps1", true}}, |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 88 | {"IR38060", I2CDeviceType{"ir38060", true}}, |
| 89 | {"IR38164", I2CDeviceType{"ir38164", true}}, |
| 90 | {"IR38263", I2CDeviceType{"ir38263", true}}, |
Yikai Tsai | 5b74aba | 2024-09-06 13:58:28 +0800 | [diff] [blame] | 91 | {"ISL28022", I2CDeviceType{"isl28022", true}}, |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 92 | {"ISL68137", I2CDeviceType{"isl68137", true}}, |
| 93 | {"ISL68220", I2CDeviceType{"isl68220", true}}, |
| 94 | {"ISL68223", I2CDeviceType{"isl68223", true}}, |
| 95 | {"ISL69225", I2CDeviceType{"isl69225", true}}, |
| 96 | {"ISL69243", I2CDeviceType{"isl69243", true}}, |
| 97 | {"ISL69260", I2CDeviceType{"isl69260", true}}, |
| 98 | {"LM25066", I2CDeviceType{"lm25066", true}}, |
Yi-Shum | 77e8769 | 2023-11-30 10:27:45 +0800 | [diff] [blame] | 99 | {"LTC2945", I2CDeviceType{"ltc2945", true}}, |
Potin Lai | 43847a8 | 2024-02-27 16:29:45 +0800 | [diff] [blame] | 100 | {"LTC4286", I2CDeviceType{"ltc4286", true}}, |
| 101 | {"LTC4287", I2CDeviceType{"ltc4287", true}}, |
Patrick Rudolph | 8d691e2 | 2023-09-11 10:43:28 +0200 | [diff] [blame] | 102 | {"MAX5970", I2CDeviceType{"max5970", true}}, |
Joseph Fu | 15a39df | 2023-12-27 09:45:49 +0800 | [diff] [blame] | 103 | {"MAX11607", I2CDeviceType{"max11607", false}}, |
Ian Chien | c82a409 | 2024-07-24 10:52:59 +0800 | [diff] [blame] | 104 | {"MAX11615", I2CDeviceType{"max11615", false}}, |
Allen.Wang | 8c13c28 | 2024-07-03 09:42:54 +0800 | [diff] [blame] | 105 | {"MAX11617", I2CDeviceType{"max11617", false}}, |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 106 | {"MAX16601", I2CDeviceType{"max16601", true}}, |
| 107 | {"MAX20710", I2CDeviceType{"max20710", true}}, |
| 108 | {"MAX20730", I2CDeviceType{"max20730", true}}, |
| 109 | {"MAX20734", I2CDeviceType{"max20734", true}}, |
| 110 | {"MAX20796", I2CDeviceType{"max20796", true}}, |
| 111 | {"MAX34451", I2CDeviceType{"max34451", true}}, |
Potin Lai | 4cfa64f | 2023-12-18 13:35:05 +0800 | [diff] [blame] | 112 | {"MP2856", I2CDeviceType{"mp2856", true}}, |
| 113 | {"MP2857", I2CDeviceType{"mp2857", true}}, |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 114 | {"MP2971", I2CDeviceType{"mp2971", true}}, |
| 115 | {"MP2973", I2CDeviceType{"mp2973", true}}, |
| 116 | {"MP2975", I2CDeviceType{"mp2975", true}}, |
| 117 | {"MP5023", I2CDeviceType{"mp5023", true}}, |
Potin Lai | 7f2b77d | 2023-12-13 16:53:10 +0800 | [diff] [blame] | 118 | {"MP5990", I2CDeviceType{"mp5990", true}}, |
Jeff Lin | d8e48df | 2024-06-07 09:06:56 +0800 | [diff] [blame] | 119 | {"MPQ8785", I2CDeviceType{"mpq8785", true}}, |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 120 | {"NCP4200", I2CDeviceType{"ncp4200", true}}, |
| 121 | {"PLI1209BC", I2CDeviceType{"pli1209bc", true}}, |
| 122 | {"pmbus", I2CDeviceType{"pmbus", true}}, |
| 123 | {"PXE1610", I2CDeviceType{"pxe1610", true}}, |
| 124 | {"RAA228000", I2CDeviceType{"raa228000", true}}, |
Potin Lai | 6e6618f | 2024-05-16 15:02:56 +0800 | [diff] [blame] | 125 | {"RAA228004", I2CDeviceType{"raa228004", true}}, |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 126 | {"RAA228228", I2CDeviceType{"raa228228", true}}, |
| 127 | {"RAA228620", I2CDeviceType{"raa228620", true}}, |
| 128 | {"RAA229001", I2CDeviceType{"raa229001", true}}, |
| 129 | {"RAA229004", I2CDeviceType{"raa229004", true}}, |
| 130 | {"RAA229126", I2CDeviceType{"raa229126", true}}, |
Ian Chien | 982d99f | 2024-08-20 17:16:56 +0800 | [diff] [blame] | 131 | {"RTQ6056", I2CDeviceType{"rtq6056", false}}, |
cchoux | b2a0f2e | 2024-03-02 19:32:15 +0800 | [diff] [blame] | 132 | {"SBRMI", I2CDeviceType{"sbrmi", true}}, |
Chau Ly | c4ddf64 | 2024-07-09 05:01:06 +0000 | [diff] [blame] | 133 | {"smpro_hwmon", I2CDeviceType{"smpro", false}}, |
Rush Chen | a9ea8e5 | 2025-01-02 11:28:57 +0800 | [diff] [blame] | 134 | {"SY24655", I2CDeviceType{"sy24655", true}}, |
Patrick Rudolph | 86c9e21 | 2023-04-19 09:04:19 +0200 | [diff] [blame] | 135 | {"TDA38640", I2CDeviceType{"tda38640", true}}, |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 136 | {"TPS53679", I2CDeviceType{"tps53679", true}}, |
| 137 | {"TPS546D24", I2CDeviceType{"tps546d24", true}}, |
Yi-Shum | 9110dfc | 2024-06-04 15:19:58 +0800 | [diff] [blame] | 138 | {"XDP710", I2CDeviceType{"xdp710", true}}, |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 139 | {"XDPE11280", I2CDeviceType{"xdpe11280", true}}, |
| 140 | {"XDPE12284", I2CDeviceType{"xdpe12284", true}}, |
Jeff Lin | 40bd671 | 2023-06-01 19:14:09 +0800 | [diff] [blame] | 141 | {"XDPE152C4", I2CDeviceType{"xdpe152c4", true}}, |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 142 | }; |
Josh Lehan | 0830c7b | 2019-10-08 16:35:09 -0700 | [diff] [blame] | 143 | |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 144 | enum class DevTypes |
| 145 | { |
| 146 | Unknown = 0, |
| 147 | HWMON, |
| 148 | IIO |
| 149 | }; |
| 150 | |
| 151 | struct DevParams |
| 152 | { |
| 153 | unsigned int matchIndex = 0; |
| 154 | std::string matchRegEx; |
| 155 | std::string nameRegEx; |
| 156 | }; |
| 157 | |
Yong Li | bf8b1da | 2020-04-15 16:32:50 +0800 | [diff] [blame] | 158 | static boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>> |
Cheng C Yang | 916360b | 2019-05-07 18:47:16 +0800 | [diff] [blame] | 159 | sensors; |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 160 | static boost::container::flat_map<std::string, std::unique_ptr<PSUCombineEvent>> |
| 161 | combineEvents; |
Cheng C Yang | 916360b | 2019-05-07 18:47:16 +0800 | [diff] [blame] | 162 | static boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>> |
| 163 | pwmSensors; |
| 164 | static boost::container::flat_map<std::string, std::string> sensorTable; |
| 165 | static boost::container::flat_map<std::string, PSUProperty> labelMatch; |
George Liu | 5b3542e | 2023-10-31 17:27:12 +0800 | [diff] [blame] | 166 | static EventPathList eventMatch; |
George Liu | 5b3542e | 2023-10-31 17:27:12 +0800 | [diff] [blame] | 167 | static EventPathList limitEventMatch; |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 168 | |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 169 | static boost::container::flat_map<size_t, bool> cpuPresence; |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 170 | static boost::container::flat_map<DevTypes, DevParams> devParamMap; |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 171 | |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 172 | // Function CheckEvent will check each attribute from eventMatch table in the |
| 173 | // sysfs. If the attributes exists in sysfs, then store the complete path |
| 174 | // of the attribute into eventPathList. |
George Liu | 5b3542e | 2023-10-31 17:27:12 +0800 | [diff] [blame] | 175 | void checkEvent(const std::string& directory, const EventPathList& eventMatch, |
| 176 | EventPathList& eventPathList) |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 177 | { |
| 178 | for (const auto& match : eventMatch) |
| 179 | { |
| 180 | const std::vector<std::string>& eventAttrs = match.second; |
| 181 | const std::string& eventName = match.first; |
| 182 | for (const auto& eventAttr : eventAttrs) |
| 183 | { |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 184 | std::string eventPath = directory; |
| 185 | eventPath += "/"; |
| 186 | eventPath += eventAttr; |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 187 | |
| 188 | std::ifstream eventFile(eventPath); |
| 189 | if (!eventFile.good()) |
| 190 | { |
| 191 | continue; |
| 192 | } |
| 193 | |
| 194 | eventPathList[eventName].push_back(eventPath); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
Cheng C Yang | 202a1ff | 2020-01-09 09:34:22 +0800 | [diff] [blame] | 199 | // Check Group Events which contains more than one targets in each combine |
| 200 | // events. |
George Liu | 5b3542e | 2023-10-31 17:27:12 +0800 | [diff] [blame] | 201 | void checkGroupEvent(const std::string& directory, |
George Liu | 5b3542e | 2023-10-31 17:27:12 +0800 | [diff] [blame] | 202 | GroupEventPathList& groupEventPathList) |
Cheng C Yang | 202a1ff | 2020-01-09 09:34:22 +0800 | [diff] [blame] | 203 | { |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 204 | EventPathList pathList; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 205 | std::vector<std::filesystem::path> eventPaths; |
| 206 | if (!findFiles(std::filesystem::path(directory), R"(fan\d+_(alarm|fault))", |
| 207 | eventPaths)) |
Cheng C Yang | 202a1ff | 2020-01-09 09:34:22 +0800 | [diff] [blame] | 208 | { |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 209 | return; |
Cheng C Yang | 202a1ff | 2020-01-09 09:34:22 +0800 | [diff] [blame] | 210 | } |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 211 | |
| 212 | for (const auto& eventPath : eventPaths) |
| 213 | { |
| 214 | std::string attrName = eventPath.filename(); |
| 215 | pathList[attrName.substr(0, attrName.find('_'))].push_back(eventPath); |
| 216 | } |
| 217 | groupEventPathList["FanFault"] = pathList; |
Cheng C Yang | 202a1ff | 2020-01-09 09:34:22 +0800 | [diff] [blame] | 218 | } |
| 219 | |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 220 | // Function checkEventLimits will check all the psu related xxx_input attributes |
| 221 | // in sysfs to see if xxx_crit_alarm xxx_lcrit_alarm xxx_max_alarm |
| 222 | // xxx_min_alarm exist, then store the existing paths of the alarm attributes |
| 223 | // to eventPathList. |
George Liu | 5b3542e | 2023-10-31 17:27:12 +0800 | [diff] [blame] | 224 | void checkEventLimits(const std::string& sensorPathStr, |
| 225 | const EventPathList& limitEventMatch, |
| 226 | EventPathList& eventPathList) |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 227 | { |
Lei YU | a2c7cea | 2020-12-23 14:07:28 +0800 | [diff] [blame] | 228 | auto attributePartPos = sensorPathStr.find_last_of('_'); |
| 229 | if (attributePartPos == std::string::npos) |
| 230 | { |
| 231 | // There is no '_' in the string, skip it |
| 232 | return; |
| 233 | } |
| 234 | auto attributePart = |
| 235 | std::string_view(sensorPathStr).substr(attributePartPos + 1); |
| 236 | if (attributePart != "input") |
| 237 | { |
| 238 | // If the sensor is not xxx_input, skip it |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | auto prefixPart = sensorPathStr.substr(0, attributePartPos + 1); |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 243 | for (const auto& limitMatch : limitEventMatch) |
| 244 | { |
| 245 | const std::vector<std::string>& limitEventAttrs = limitMatch.second; |
| 246 | const std::string& eventName = limitMatch.first; |
| 247 | for (const auto& limitEventAttr : limitEventAttrs) |
| 248 | { |
Lei YU | a2c7cea | 2020-12-23 14:07:28 +0800 | [diff] [blame] | 249 | auto limitEventPath = prefixPart + limitEventAttr; |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 250 | std::ifstream eventFile(limitEventPath); |
| 251 | if (!eventFile.good()) |
| 252 | { |
| 253 | continue; |
| 254 | } |
| 255 | eventPathList[eventName].push_back(limitEventPath); |
| 256 | } |
| 257 | } |
| 258 | } |
Cheng C Yang | 916360b | 2019-05-07 18:47:16 +0800 | [diff] [blame] | 259 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 260 | static void checkPWMSensor( |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 261 | const std::filesystem::path& sensorPath, std::string& labelHead, |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 262 | const std::string& interfacePath, |
| 263 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 264 | sdbusplus::asio::object_server& objectServer, const std::string& psuName) |
Cheng C Yang | 916360b | 2019-05-07 18:47:16 +0800 | [diff] [blame] | 265 | { |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 266 | if (!labelHead.starts_with("fan")) |
Cheng C Yang | 916360b | 2019-05-07 18:47:16 +0800 | [diff] [blame] | 267 | { |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 268 | return; |
Cheng C Yang | 916360b | 2019-05-07 18:47:16 +0800 | [diff] [blame] | 269 | } |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 270 | std::string labelHeadIndex = labelHead.substr(3); |
| 271 | |
| 272 | const std::string& sensorPathStr = sensorPath.string(); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 273 | const std::string& pwmPathStr = |
| 274 | boost::replace_all_copy(sensorPathStr, "input", "target"); |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 275 | std::ifstream pwmFile(pwmPathStr); |
| 276 | if (!pwmFile.good()) |
| 277 | { |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | auto findPWMSensor = pwmSensors.find(psuName + labelHead); |
| 282 | if (findPWMSensor != pwmSensors.end()) |
| 283 | { |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | std::string name = "Pwm_"; |
| 288 | name += psuName; |
| 289 | name += "_Fan_"; |
| 290 | name += labelHeadIndex; |
| 291 | |
| 292 | std::string objPath = interfacePath; |
| 293 | objPath += "_Fan_"; |
| 294 | objPath += labelHeadIndex; |
| 295 | |
| 296 | pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>( |
| 297 | name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU"); |
Cheng C Yang | 916360b | 2019-05-07 18:47:16 +0800 | [diff] [blame] | 298 | } |
| 299 | |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 300 | static void createSensorsCallback( |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 301 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 302 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 303 | const ManagedObjectType& sensorConfigs, |
| 304 | const std::shared_ptr<boost::container::flat_set<std::string>>& |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 305 | sensorsChanged, |
| 306 | bool activateOnly) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 307 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 308 | int numCreated = 0; |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 309 | bool firstScan = sensorsChanged == nullptr; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 310 | |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 311 | auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes); |
| 312 | |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 313 | std::vector<std::filesystem::path> pmbusPaths; |
| 314 | findFiles(std::filesystem::path("/sys/bus/iio/devices"), "name", |
| 315 | pmbusPaths); |
| 316 | findFiles(std::filesystem::path("/sys/class/hwmon"), "name", pmbusPaths); |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 317 | if (pmbusPaths.empty()) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 318 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 319 | lg2::error("No PSU sensors in system"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 320 | return; |
| 321 | } |
| 322 | |
| 323 | boost::container::flat_set<std::string> directories; |
| 324 | for (const auto& pmbusPath : pmbusPaths) |
| 325 | { |
George Liu | 5b3542e | 2023-10-31 17:27:12 +0800 | [diff] [blame] | 326 | EventPathList eventPathList; |
| 327 | GroupEventPathList groupEventPathList; |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 328 | |
| 329 | std::ifstream nameFile(pmbusPath); |
| 330 | if (!nameFile.good()) |
| 331 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 332 | lg2::error("Failure finding '{PATH}'", "PATH", pmbusPath); |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 333 | continue; |
| 334 | } |
| 335 | |
| 336 | std::string pmbusName; |
| 337 | std::getline(nameFile, pmbusName); |
| 338 | nameFile.close(); |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 339 | |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 340 | if (sensorTypes.find(pmbusName) == sensorTypes.end()) |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 341 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 342 | // To avoid this error message, add your driver name to |
| 343 | // the pmbusNames vector at the top of this file. |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 344 | lg2::error("'{NAME}' not found in sensor whitelist", "NAME", |
| 345 | pmbusName); |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 346 | continue; |
| 347 | } |
| 348 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 349 | auto directory = pmbusPath.parent_path(); |
| 350 | |
| 351 | auto ret = directories.insert(directory.string()); |
| 352 | if (!ret.second) |
| 353 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 354 | lg2::error("Duplicate path: '{PATH}'", "PATH", directory); |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 355 | continue; // check if path has already been searched |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 356 | } |
| 357 | |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 358 | DevTypes devType = DevTypes::HWMON; |
| 359 | std::string deviceName; |
| 360 | if (directory.parent_path() == "/sys/class/hwmon") |
| 361 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 362 | std::string devicePath = |
| 363 | std::filesystem::canonical(directory / "device"); |
Chau Ly | c4ddf64 | 2024-07-09 05:01:06 +0000 | [diff] [blame] | 364 | std::smatch match; |
| 365 | // Find /i2c-<bus>/<bus>-<address> match in device path |
| 366 | std::regex_search(devicePath, match, i2cDevRegex); |
| 367 | if (match.empty()) |
| 368 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 369 | lg2::error("Found bad device path: '{PATH}'", "PATH", |
| 370 | devicePath); |
Chau Ly | c4ddf64 | 2024-07-09 05:01:06 +0000 | [diff] [blame] | 371 | continue; |
| 372 | } |
| 373 | // Extract <bus>-<address> |
| 374 | std::string matchStr = match[1]; |
| 375 | deviceName = matchStr.substr(matchStr.find_last_of('/') + 1); |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 376 | } |
| 377 | else |
| 378 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 379 | deviceName = |
| 380 | std::filesystem::canonical(directory).parent_path().stem(); |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 381 | devType = DevTypes::IIO; |
| 382 | } |
| 383 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 384 | size_t bus = 0; |
| 385 | size_t addr = 0; |
Akshit Shah | 03d333e | 2023-08-23 22:14:28 +0000 | [diff] [blame] | 386 | if (!getDeviceBusAddr(deviceName, bus, addr)) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 387 | { |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 388 | continue; |
| 389 | } |
| 390 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 391 | const SensorBaseConfigMap* baseConfig = nullptr; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 392 | const SensorData* sensorData = nullptr; |
| 393 | const std::string* interfacePath = nullptr; |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 394 | std::string sensorType; |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 395 | size_t thresholdConfSize = 0; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 396 | |
Zev Weiss | 741f26e | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 397 | for (const auto& [path, cfgData] : sensorConfigs) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 398 | { |
Zev Weiss | 741f26e | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 399 | sensorData = &cfgData; |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 400 | for (const auto& [type, dt] : sensorTypes) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 401 | { |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 402 | auto sensorBase = sensorData->find(configInterfaceName(type)); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 403 | if (sensorBase != sensorData->end()) |
| 404 | { |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 405 | baseConfig = &sensorBase->second; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 406 | sensorType = type; |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | if (baseConfig == nullptr) |
| 411 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 412 | lg2::error("error finding base configuration for '{NAME}'", |
| 413 | "NAME", deviceName); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 414 | continue; |
| 415 | } |
| 416 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 417 | auto configBus = baseConfig->find("Bus"); |
| 418 | auto configAddress = baseConfig->find("Address"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 419 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 420 | if (configBus == baseConfig->end() || |
| 421 | configAddress == baseConfig->end()) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 422 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 423 | lg2::error("error finding necessary entry in configuration"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 424 | continue; |
| 425 | } |
| 426 | |
Matt Simmering | 6747eba | 2023-01-18 16:03:55 -0800 | [diff] [blame] | 427 | const uint64_t* confBus = |
| 428 | std::get_if<uint64_t>(&(configBus->second)); |
| 429 | const uint64_t* confAddr = |
| 430 | std::get_if<uint64_t>(&(configAddress->second)); |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 431 | if (confBus == nullptr || confAddr == nullptr) |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 432 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 433 | lg2::error("Cannot get bus or address, invalid configuration"); |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 434 | continue; |
| 435 | } |
| 436 | |
| 437 | if ((*confBus != bus) || (*confAddr != addr)) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 438 | { |
Patrick Rudolph | 9f27087 | 2023-10-25 09:13:09 +0200 | [diff] [blame] | 439 | if constexpr (debug) |
| 440 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 441 | lg2::error( |
| 442 | "Configuration skipping '{CONFBUS}'-'{CONFADDR}' because not {BUS}-{ADDR}", |
| 443 | "CONFBUS", *confBus, "CONFADDR", *confAddr, "BUS", bus, |
| 444 | "ADDR", addr); |
Patrick Rudolph | 9f27087 | 2023-10-25 09:13:09 +0200 | [diff] [blame] | 445 | } |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 446 | continue; |
| 447 | } |
| 448 | |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 449 | std::vector<thresholds::Threshold> confThresholds; |
| 450 | if (!parseThresholdsFromConfig(*sensorData, confThresholds)) |
| 451 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 452 | lg2::error("error populating total thresholds"); |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 453 | } |
| 454 | thresholdConfSize = confThresholds.size(); |
| 455 | |
Zev Weiss | 741f26e | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 456 | interfacePath = &path.str; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 457 | break; |
| 458 | } |
| 459 | if (interfacePath == nullptr) |
| 460 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 461 | // To avoid this error message, add your export map entry, |
| 462 | // from Entity Manager, to sensorTypes at the top of this file. |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 463 | lg2::error("failed to find match for '{NAME}'", "NAME", deviceName); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 464 | continue; |
| 465 | } |
| 466 | |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 467 | auto findI2CDev = devices.find(*interfacePath); |
| 468 | |
| 469 | std::shared_ptr<I2CDevice> i2cDev; |
| 470 | if (findI2CDev != devices.end()) |
| 471 | { |
| 472 | if (activateOnly && !findI2CDev->second.second) |
| 473 | { |
| 474 | continue; |
| 475 | } |
| 476 | i2cDev = findI2CDev->second.first; |
| 477 | } |
| 478 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 479 | auto findPSUName = baseConfig->find("Name"); |
| 480 | if (findPSUName == baseConfig->end()) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 481 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 482 | lg2::error("could not determine configuration name for '{NAME}'", |
| 483 | "NAME", deviceName); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 484 | continue; |
| 485 | } |
Matt Simmering | 6747eba | 2023-01-18 16:03:55 -0800 | [diff] [blame] | 486 | const std::string* psuName = |
| 487 | std::get_if<std::string>(&(findPSUName->second)); |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 488 | if (psuName == nullptr) |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 489 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 490 | lg2::error("Cannot find psu name, invalid configuration"); |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 491 | continue; |
| 492 | } |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 493 | |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 494 | auto findCPU = baseConfig->find("CPURequired"); |
| 495 | if (findCPU != baseConfig->end()) |
| 496 | { |
Chris Sides | 3fce1bf | 2023-04-03 16:57:05 -0500 | [diff] [blame] | 497 | size_t index = std::visit(VariantToIntVisitor(), findCPU->second); |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 498 | auto presenceFind = cpuPresence.find(index); |
| 499 | if (presenceFind == cpuPresence.end() || !presenceFind->second) |
| 500 | { |
| 501 | continue; |
| 502 | } |
| 503 | } |
| 504 | |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 505 | // on rescans, only update sensors we were signaled by |
| 506 | if (!firstScan) |
| 507 | { |
Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 508 | std::string psuNameStr = "/" + escapeName(*psuName); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 509 | auto it = |
| 510 | std::find_if(sensorsChanged->begin(), sensorsChanged->end(), |
| 511 | [psuNameStr](std::string& s) { |
| 512 | return s.ends_with(psuNameStr); |
| 513 | }); |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 514 | |
| 515 | if (it == sensorsChanged->end()) |
| 516 | { |
| 517 | continue; |
| 518 | } |
| 519 | sensorsChanged->erase(it); |
| 520 | } |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 521 | checkEvent(directory.string(), eventMatch, eventPathList); |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 522 | checkGroupEvent(directory.string(), groupEventPathList); |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 523 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 524 | PowerState readState = getPowerState(*baseConfig); |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 525 | |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 526 | /* Check if there are more sensors in the same interface */ |
| 527 | int i = 1; |
| 528 | std::vector<std::string> psuNames; |
| 529 | do |
| 530 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 531 | // Individual string fields: Name, Name1, Name2, Name3, ... |
Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 532 | psuNames.push_back( |
| 533 | escapeName(std::get<std::string>(findPSUName->second))); |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 534 | findPSUName = baseConfig->find("Name" + std::to_string(i++)); |
| 535 | } while (findPSUName != baseConfig->end()); |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 536 | |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 537 | std::vector<std::filesystem::path> sensorPaths; |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 538 | if (!findFiles(directory, devParamMap[devType].matchRegEx, sensorPaths, |
| 539 | 0)) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 540 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 541 | lg2::error("No PSU non-label sensor in PSU"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 542 | continue; |
| 543 | } |
| 544 | |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 545 | /* read max value in sysfs for in, curr, power, temp, ... */ |
| 546 | if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0)) |
| 547 | { |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 548 | if constexpr (debug) |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 549 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 550 | lg2::error("No max name in PSU"); |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
Zev Weiss | 8569bf2 | 2022-10-11 15:37:44 -0700 | [diff] [blame] | 554 | float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll); |
Lei YU | 7170a23 | 2021-02-04 16:19:27 +0800 | [diff] [blame] | 555 | |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 556 | /* Find array of labels to be exposed if it is defined in config */ |
| 557 | std::vector<std::string> findLabels; |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 558 | auto findLabelObj = baseConfig->find("Labels"); |
| 559 | if (findLabelObj != baseConfig->end()) |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 560 | { |
| 561 | findLabels = |
| 562 | std::get<std::vector<std::string>>(findLabelObj->second); |
| 563 | } |
| 564 | |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 565 | std::regex sensorNameRegEx(devParamMap[devType].nameRegEx); |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 566 | std::smatch matches; |
| 567 | |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 568 | for (const auto& sensorPath : sensorPaths) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 569 | { |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 570 | bool maxLabel = false; |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 571 | std::string labelHead; |
| 572 | std::string sensorPathStr = sensorPath.string(); |
| 573 | std::string sensorNameStr = sensorPath.filename(); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 574 | std::string sensorNameSubStr; |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 575 | if (std::regex_search(sensorNameStr, matches, sensorNameRegEx)) |
| 576 | { |
Josh Lehan | 0649445 | 2019-10-31 09:49:16 -0700 | [diff] [blame] | 577 | // hwmon *_input filename without number: |
| 578 | // in, curr, power, temp, ... |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 579 | // iio in_*_raw filename without number: |
| 580 | // voltage, temp, pressure, ... |
| 581 | sensorNameSubStr = matches[devParamMap[devType].matchIndex]; |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 582 | } |
| 583 | else |
| 584 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 585 | lg2::error("Could not extract the alpha prefix from '{NAME}'", |
| 586 | "NAME", sensorNameStr); |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 587 | continue; |
| 588 | } |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 589 | |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 590 | std::string labelPath; |
| 591 | |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 592 | if (devType == DevTypes::HWMON) |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 593 | { |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 594 | /* find and differentiate _max and _input to replace "label" */ |
| 595 | size_t pos = sensorPathStr.find('_'); |
| 596 | if (pos != std::string::npos) |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 597 | { |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 598 | std::string sensorPathStrMax = sensorPathStr.substr(pos); |
| 599 | if (sensorPathStrMax == "_max") |
| 600 | { |
| 601 | labelPath = boost::replace_all_copy(sensorPathStr, |
| 602 | "max", "label"); |
| 603 | maxLabel = true; |
| 604 | } |
| 605 | else |
| 606 | { |
| 607 | labelPath = boost::replace_all_copy(sensorPathStr, |
| 608 | "input", "label"); |
| 609 | maxLabel = false; |
| 610 | } |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 611 | } |
| 612 | else |
| 613 | { |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 614 | continue; |
| 615 | } |
| 616 | |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 617 | std::ifstream labelFile(labelPath); |
| 618 | if (!labelFile.good()) |
| 619 | { |
| 620 | if constexpr (debug) |
| 621 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 622 | lg2::error( |
| 623 | "Input file '{PATH}' has no corresponding label file", |
| 624 | "PATH", sensorPath.string()); |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 625 | } |
| 626 | // hwmon *_input filename with number: |
| 627 | // temp1, temp2, temp3, ... |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 628 | labelHead = |
| 629 | sensorNameStr.substr(0, sensorNameStr.find('_')); |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 630 | } |
| 631 | else |
| 632 | { |
| 633 | std::string label; |
| 634 | std::getline(labelFile, label); |
| 635 | labelFile.close(); |
| 636 | auto findSensor = sensors.find(label); |
| 637 | if (findSensor != sensors.end()) |
| 638 | { |
| 639 | continue; |
| 640 | } |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 641 | |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 642 | // hwmon corresponding *_label file contents: |
| 643 | // vin1, vout1, ... |
| 644 | labelHead = label.substr(0, label.find(' ')); |
| 645 | } |
| 646 | |
| 647 | /* append "max" for labelMatch */ |
| 648 | if (maxLabel) |
| 649 | { |
| 650 | labelHead.insert(0, "max"); |
| 651 | } |
| 652 | |
| 653 | checkPWMSensor(sensorPath, labelHead, *interfacePath, |
| 654 | dbusConnection, objectServer, psuNames[0]); |
| 655 | } |
| 656 | else if (devType == DevTypes::IIO) |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 657 | { |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 658 | auto findIIOHyphen = sensorNameStr.find_last_of('_'); |
| 659 | labelHead = sensorNameStr.substr(0, findIIOHyphen); |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 660 | } |
| 661 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 662 | if constexpr (debug) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 663 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 664 | lg2::error("Sensor type: {NAME}, label: {LABEL}", "NAME", |
| 665 | sensorNameSubStr, "LABEL", labelHead); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 668 | if (!findLabels.empty()) |
| 669 | { |
| 670 | /* Check if this labelHead is enabled in config file */ |
| 671 | if (std::find(findLabels.begin(), findLabels.end(), |
| 672 | labelHead) == findLabels.end()) |
| 673 | { |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 674 | if constexpr (debug) |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 675 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 676 | lg2::error( |
| 677 | "could not find '{LABEL}' in the Labels list", |
| 678 | "LABEL", labelHead); |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 679 | } |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 680 | continue; |
| 681 | } |
| 682 | } |
Jayaprakash Mutyala | a205c6f | 2025-02-13 06:09:04 +0000 | [diff] [blame] | 683 | auto it = std::find_if(labelHead.begin(), labelHead.end(), |
| 684 | static_cast<int (*)(int)>(std::isdigit)); |
| 685 | std::string_view labelHeadView( |
| 686 | labelHead.data(), std::distance(labelHead.begin(), it)); |
| 687 | auto findProperty = |
| 688 | labelMatch.find(static_cast<std::string>(labelHeadView)); |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 689 | if (findProperty == labelMatch.end()) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 690 | { |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 691 | if constexpr (debug) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 692 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 693 | lg2::error( |
| 694 | "Could not find matching default property for '{LABEL}'", |
| 695 | "LABEL", labelHead); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 696 | } |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 697 | continue; |
| 698 | } |
| 699 | |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 700 | // Protect the hardcoded labelMatch list from changes, |
| 701 | // by making a copy and modifying that instead. |
| 702 | // Avoid bleedthrough of one device's customizations to |
| 703 | // the next device, as each should be independently customizable. |
Zhikui Ren | 7c2e7c9 | 2024-03-07 14:11:25 -0800 | [diff] [blame] | 704 | PSUProperty psuProperty = findProperty->second; |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 705 | |
| 706 | // Use label head as prefix for reading from config file, |
| 707 | // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ... |
| 708 | std::string keyName = labelHead + "_Name"; |
| 709 | std::string keyScale = labelHead + "_Scale"; |
| 710 | std::string keyMin = labelHead + "_Min"; |
| 711 | std::string keyMax = labelHead + "_Max"; |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 712 | std::string keyOffset = labelHead + "_Offset"; |
Lotus Xu | cb5af73 | 2021-09-10 15:18:50 +0800 | [diff] [blame] | 713 | std::string keyPowerState = labelHead + "_PowerState"; |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 714 | |
| 715 | bool customizedName = false; |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 716 | auto findCustomName = baseConfig->find(keyName); |
| 717 | if (findCustomName != baseConfig->end()) |
Josh Lehan | 432d1ed | 2019-10-16 12:23:31 -0700 | [diff] [blame] | 718 | { |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 719 | try |
| 720 | { |
Zhikui Ren | 7c2e7c9 | 2024-03-07 14:11:25 -0800 | [diff] [blame] | 721 | psuProperty.labelTypeName = std::visit( |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 722 | VariantToStringVisitor(), findCustomName->second); |
| 723 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 724 | catch (const std::invalid_argument&) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 725 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 726 | lg2::error("Unable to parse '{NAME}'", "NAME", keyName); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 727 | continue; |
| 728 | } |
| 729 | |
| 730 | // All strings are valid, including empty string |
| 731 | customizedName = true; |
| 732 | } |
| 733 | |
| 734 | bool customizedScale = false; |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 735 | auto findCustomScale = baseConfig->find(keyScale); |
| 736 | if (findCustomScale != baseConfig->end()) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 737 | { |
| 738 | try |
| 739 | { |
Zhikui Ren | 7c2e7c9 | 2024-03-07 14:11:25 -0800 | [diff] [blame] | 740 | psuProperty.sensorScaleFactor = std::visit( |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 741 | VariantToUnsignedIntVisitor(), findCustomScale->second); |
| 742 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 743 | catch (const std::invalid_argument&) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 744 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 745 | lg2::error("Unable to parse '{SCALE}'", "SCALE", keyScale); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 746 | continue; |
| 747 | } |
| 748 | |
| 749 | // Avoid later division by zero |
Zhikui Ren | 7c2e7c9 | 2024-03-07 14:11:25 -0800 | [diff] [blame] | 750 | if (psuProperty.sensorScaleFactor > 0) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 751 | { |
| 752 | customizedScale = true; |
| 753 | } |
| 754 | else |
| 755 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 756 | lg2::error("Unable to accept '{SCALE}'", "SCALE", keyScale); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 757 | continue; |
| 758 | } |
| 759 | } |
| 760 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 761 | auto findCustomMin = baseConfig->find(keyMin); |
| 762 | if (findCustomMin != baseConfig->end()) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 763 | { |
| 764 | try |
| 765 | { |
Zhikui Ren | 7c2e7c9 | 2024-03-07 14:11:25 -0800 | [diff] [blame] | 766 | psuProperty.minReading = std::visit( |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 767 | VariantToDoubleVisitor(), findCustomMin->second); |
| 768 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 769 | catch (const std::invalid_argument&) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 770 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 771 | lg2::error("Unable to parse '{MIN}'", "MIN", keyMin); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 772 | continue; |
| 773 | } |
| 774 | } |
| 775 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 776 | auto findCustomMax = baseConfig->find(keyMax); |
| 777 | if (findCustomMax != baseConfig->end()) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 778 | { |
| 779 | try |
| 780 | { |
Zhikui Ren | 7c2e7c9 | 2024-03-07 14:11:25 -0800 | [diff] [blame] | 781 | psuProperty.maxReading = std::visit( |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 782 | VariantToDoubleVisitor(), findCustomMax->second); |
| 783 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 784 | catch (const std::invalid_argument&) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 785 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 786 | lg2::error("Unable to parse '{MAX}'", "MAX", keyMax); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 787 | continue; |
| 788 | } |
| 789 | } |
| 790 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 791 | auto findCustomOffset = baseConfig->find(keyOffset); |
| 792 | if (findCustomOffset != baseConfig->end()) |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 793 | { |
| 794 | try |
| 795 | { |
Zhikui Ren | 7c2e7c9 | 2024-03-07 14:11:25 -0800 | [diff] [blame] | 796 | psuProperty.sensorOffset = std::visit( |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 797 | VariantToDoubleVisitor(), findCustomOffset->second); |
| 798 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 799 | catch (const std::invalid_argument&) |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 800 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 801 | lg2::error("Unable to parse '{OFFSET}'", "OFFSET", |
| 802 | keyOffset); |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 803 | continue; |
| 804 | } |
| 805 | } |
| 806 | |
Lotus Xu | cb5af73 | 2021-09-10 15:18:50 +0800 | [diff] [blame] | 807 | // if we find label head power state set ,override the powerstate. |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 808 | auto findPowerState = baseConfig->find(keyPowerState); |
| 809 | if (findPowerState != baseConfig->end()) |
Lotus Xu | cb5af73 | 2021-09-10 15:18:50 +0800 | [diff] [blame] | 810 | { |
| 811 | std::string powerState = std::visit(VariantToStringVisitor(), |
| 812 | findPowerState->second); |
| 813 | setReadState(powerState, readState); |
| 814 | } |
Zhikui Ren | 7c2e7c9 | 2024-03-07 14:11:25 -0800 | [diff] [blame] | 815 | if (!(psuProperty.minReading < psuProperty.maxReading)) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 816 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 817 | lg2::error("Min must be less than Max"); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 818 | continue; |
| 819 | } |
| 820 | |
| 821 | // If the sensor name is being customized by config file, |
| 822 | // then prefix/suffix composition becomes not necessary, |
| 823 | // and in fact not wanted, because it gets in the way. |
| 824 | std::string psuNameFromIndex; |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 825 | std::string nameIndexStr = "1"; |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 826 | if (!customizedName) |
| 827 | { |
| 828 | /* Find out sensor name index for this label */ |
| 829 | std::regex rgx("[A-Za-z]+([0-9]+)"); |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 830 | size_t nameIndex{0}; |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 831 | if (std::regex_search(labelHead, matches, rgx)) |
| 832 | { |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 833 | nameIndexStr = matches[1]; |
| 834 | nameIndex = std::stoi(nameIndexStr); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 835 | |
| 836 | // Decrement to preserve alignment, because hwmon |
| 837 | // human-readable filenames and labels use 1-based |
| 838 | // numbering, but the "Name", "Name1", "Name2", etc. naming |
| 839 | // convention (the psuNames vector) uses 0-based numbering. |
| 840 | if (nameIndex > 0) |
| 841 | { |
| 842 | --nameIndex; |
| 843 | } |
| 844 | } |
| 845 | else |
| 846 | { |
| 847 | nameIndex = 0; |
| 848 | } |
| 849 | |
| 850 | if (psuNames.size() <= nameIndex) |
| 851 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 852 | lg2::error("Could not pair '{LABEL}' with a Name field", |
| 853 | "LABEL", labelHead); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 854 | continue; |
| 855 | } |
| 856 | |
| 857 | psuNameFromIndex = psuNames[nameIndex]; |
| 858 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 859 | if constexpr (debug) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 860 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 861 | lg2::error( |
| 862 | "'{LABEL}' paired with '{NAME}' at index '{INDEX}'", |
| 863 | "LABEL", labelHead, "NAME", psuNameFromIndex, "INDEX", |
| 864 | nameIndex); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 865 | } |
Josh Lehan | 432d1ed | 2019-10-16 12:23:31 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 868 | if (devType == DevTypes::HWMON) |
| 869 | { |
| 870 | checkEventLimits(sensorPathStr, limitEventMatch, eventPathList); |
| 871 | } |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 872 | |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 873 | // Similarly, if sensor scaling factor is being customized, |
| 874 | // then the below power-of-10 constraint becomes unnecessary, |
| 875 | // as config should be able to specify an arbitrary divisor. |
Zhikui Ren | 7c2e7c9 | 2024-03-07 14:11:25 -0800 | [diff] [blame] | 876 | unsigned int factor = psuProperty.sensorScaleFactor; |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 877 | if (!customizedScale) |
Vijay Khemka | 53ca444 | 2019-07-23 11:03:55 -0700 | [diff] [blame] | 878 | { |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 879 | // Preserve existing usage of hardcoded labelMatch table below |
| 880 | factor = std::pow(10.0, factor); |
Vijay Khemka | 53ca444 | 2019-07-23 11:03:55 -0700 | [diff] [blame] | 881 | |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 882 | /* Change first char of substring to uppercase */ |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 883 | char firstChar = |
| 884 | static_cast<char>(std::toupper(sensorNameSubStr[0])); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 885 | std::string strScaleFactor = |
| 886 | firstChar + sensorNameSubStr.substr(1) + "ScaleFactor"; |
| 887 | |
| 888 | // Preserve existing configs by accepting earlier syntax, |
| 889 | // example CurrScaleFactor, PowerScaleFactor, ... |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 890 | auto findScaleFactor = baseConfig->find(strScaleFactor); |
| 891 | if (findScaleFactor != baseConfig->end()) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 892 | { |
| 893 | factor = std::visit(VariantToIntVisitor(), |
| 894 | findScaleFactor->second); |
| 895 | } |
| 896 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 897 | if constexpr (debug) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 898 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 899 | lg2::error( |
| 900 | "Sensor scaling factor '{FACTOR}' string '{SCALE_FACTOR}'", |
| 901 | "FACTOR", factor, "SCALE_FACTOR", strScaleFactor); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 902 | } |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 905 | std::vector<thresholds::Threshold> sensorThresholds; |
Joshi, Mansi | 14f0ad8 | 2019-11-21 10:52:30 +0530 | [diff] [blame] | 906 | if (!parseThresholdsFromConfig(*sensorData, sensorThresholds, |
| 907 | &labelHead)) |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 908 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 909 | lg2::error("error populating thresholds for '{NAME}'", "NAME", |
| 910 | sensorNameSubStr); |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 911 | } |
| 912 | |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 913 | auto findSensorUnit = sensorTable.find(sensorNameSubStr); |
| 914 | if (findSensorUnit == sensorTable.end()) |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 915 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 916 | lg2::error("'{NAME}' is not a recognized sensor type", "NAME", |
| 917 | sensorNameSubStr); |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 918 | continue; |
| 919 | } |
| 920 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 921 | if constexpr (debug) |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 922 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 923 | lg2::error("Sensor properties - Name: {NAME}, Scale: {SCALE}, " |
| 924 | "Min: {MIN}, Max: {MAX}, Offset: {OFFSET}", |
| 925 | "NAME", psuProperty.labelTypeName, "SCALE", |
| 926 | psuProperty.sensorScaleFactor, "MIN", |
| 927 | psuProperty.minReading, "MAX", |
| 928 | psuProperty.maxReading, "OFFSET", |
| 929 | psuProperty.sensorOffset); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Zhikui Ren | 7c2e7c9 | 2024-03-07 14:11:25 -0800 | [diff] [blame] | 932 | std::string sensorName = psuProperty.labelTypeName; |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 933 | if (customizedName) |
| 934 | { |
| 935 | if (sensorName.empty()) |
| 936 | { |
| 937 | // Allow selective disabling of an individual sensor, |
| 938 | // by customizing its name to an empty string. |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 939 | lg2::error("Sensor disabled, empty string"); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 940 | continue; |
| 941 | } |
| 942 | } |
| 943 | else |
| 944 | { |
| 945 | // Sensor name not customized, do prefix/suffix composition, |
| 946 | // preserving default behavior by using psuNameFromIndex. |
Zhikui Ren | 7c2e7c9 | 2024-03-07 14:11:25 -0800 | [diff] [blame] | 947 | sensorName = psuNameFromIndex + " " + psuProperty.labelTypeName; |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 948 | |
| 949 | // The labelTypeName of a fan can be: |
| 950 | // "Fan Speed 1", "Fan Speed 2", "Fan Speed 3" ... |
| 951 | if (labelHead == "fan" + nameIndexStr) |
| 952 | { |
| 953 | sensorName += nameIndexStr; |
| 954 | } |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 957 | if constexpr (debug) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 958 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 959 | lg2::error("Sensor name: {NAME}, path: {PATH}, type: {TYPE}", |
| 960 | "NAME", sensorName, "PATH", sensorPathStr, "TYPE", |
| 961 | sensorType); |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 962 | } |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 963 | // destruct existing one first if already created |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 964 | |
| 965 | auto& sensor = sensors[sensorName]; |
| 966 | if (!activateOnly) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 967 | { |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 968 | sensor = nullptr; |
| 969 | } |
| 970 | |
| 971 | if (sensor != nullptr) |
| 972 | { |
| 973 | sensor->activate(sensorPathStr, i2cDev); |
| 974 | } |
| 975 | else |
| 976 | { |
| 977 | sensors[sensorName] = std::make_shared<PSUSensor>( |
| 978 | sensorPathStr, sensorType, objectServer, dbusConnection, io, |
| 979 | sensorName, std::move(sensorThresholds), *interfacePath, |
| 980 | readState, findSensorUnit->second, factor, |
Zhikui Ren | 7c2e7c9 | 2024-03-07 14:11:25 -0800 | [diff] [blame] | 981 | psuProperty.maxReading, psuProperty.minReading, |
| 982 | psuProperty.sensorOffset, labelHead, thresholdConfSize, |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 983 | pollRate, i2cDev); |
| 984 | sensors[sensorName]->setupRead(); |
| 985 | ++numCreated; |
| 986 | if constexpr (debug) |
| 987 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 988 | lg2::error("Created '{NUM}' sensors so far", "NUM", |
| 989 | numCreated); |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 990 | } |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 991 | } |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 992 | } |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 993 | |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 994 | if (devType == DevTypes::HWMON) |
| 995 | { |
| 996 | // OperationalStatus event |
| 997 | combineEvents[*psuName + "OperationalStatus"] = nullptr; |
| 998 | combineEvents[*psuName + "OperationalStatus"] = |
| 999 | std::make_unique<PSUCombineEvent>( |
| 1000 | objectServer, dbusConnection, io, *psuName, readState, |
| 1001 | eventPathList, groupEventPathList, "OperationalStatus", |
| 1002 | pollRate); |
| 1003 | } |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1004 | } |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 1005 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 1006 | if constexpr (debug) |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 1007 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 1008 | lg2::error("Created total of '{NUM}' sensors", "NUM", numCreated); |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 1009 | } |
Matt Simmering | 6747eba | 2023-01-18 16:03:55 -0800 | [diff] [blame] | 1010 | } |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1011 | |
Patrick Williams | 556e04b | 2025-02-01 08:22:22 -0500 | [diff] [blame] | 1012 | static void getPresentCpus( |
| 1013 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
Matt Simmering | 39fadb9 | 2023-05-04 21:47:29 -0700 | [diff] [blame] | 1014 | { |
| 1015 | static const int depth = 2; |
| 1016 | static const int numKeys = 1; |
| 1017 | GetSubTreeType cpuSubTree; |
| 1018 | |
| 1019 | try |
| 1020 | { |
| 1021 | auto getItems = dbusConnection->new_method_call( |
| 1022 | mapper::busName, mapper::path, mapper::interface, mapper::subtree); |
| 1023 | getItems.append(cpuInventoryPath, static_cast<int32_t>(depth), |
| 1024 | std::array<const char*, numKeys>{ |
| 1025 | "xyz.openbmc_project.Inventory.Item"}); |
| 1026 | auto getItemsResp = dbusConnection->call(getItems); |
| 1027 | getItemsResp.read(cpuSubTree); |
| 1028 | } |
| 1029 | catch (sdbusplus::exception_t& e) |
| 1030 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 1031 | lg2::error("error getting inventory item subtree: '{ERR}'", "ERR", e); |
Matt Simmering | 39fadb9 | 2023-05-04 21:47:29 -0700 | [diff] [blame] | 1032 | return; |
| 1033 | } |
| 1034 | |
| 1035 | for (const auto& [path, objDict] : cpuSubTree) |
| 1036 | { |
| 1037 | auto obj = sdbusplus::message::object_path(path).filename(); |
Patrick Rudolph | 457715b | 2024-04-10 14:51:35 +0200 | [diff] [blame] | 1038 | boost::to_lower(obj); |
| 1039 | |
Matt Simmering | 39fadb9 | 2023-05-04 21:47:29 -0700 | [diff] [blame] | 1040 | if (!obj.starts_with("cpu") || objDict.empty()) |
| 1041 | { |
| 1042 | continue; |
| 1043 | } |
| 1044 | const std::string& owner = objDict.begin()->first; |
| 1045 | |
| 1046 | std::variant<bool> respValue; |
| 1047 | try |
| 1048 | { |
| 1049 | auto getPresence = dbusConnection->new_method_call( |
| 1050 | owner.c_str(), path.c_str(), "org.freedesktop.DBus.Properties", |
| 1051 | "Get"); |
| 1052 | getPresence.append("xyz.openbmc_project.Inventory.Item", "Present"); |
| 1053 | auto resp = dbusConnection->call(getPresence); |
| 1054 | resp.read(respValue); |
| 1055 | } |
| 1056 | catch (sdbusplus::exception_t& e) |
| 1057 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 1058 | lg2::error("Error in getting CPU presence: '{ERR}'", "ERR", e); |
Matt Simmering | 39fadb9 | 2023-05-04 21:47:29 -0700 | [diff] [blame] | 1059 | continue; |
| 1060 | } |
| 1061 | |
| 1062 | auto* present = std::get_if<bool>(&respValue); |
| 1063 | if (present != nullptr && *present) |
| 1064 | { |
| 1065 | int cpuIndex = 0; |
| 1066 | try |
| 1067 | { |
Patrick Rudolph | 457715b | 2024-04-10 14:51:35 +0200 | [diff] [blame] | 1068 | cpuIndex = std::stoi(obj.substr(obj.size() - 1)); |
Matt Simmering | 39fadb9 | 2023-05-04 21:47:29 -0700 | [diff] [blame] | 1069 | } |
| 1070 | catch (const std::exception& e) |
| 1071 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 1072 | lg2::error("Error converting CPU index: '{ERR}'", "ERR", e); |
Matt Simmering | 39fadb9 | 2023-05-04 21:47:29 -0700 | [diff] [blame] | 1073 | continue; |
| 1074 | } |
Patrick Rudolph | 457715b | 2024-04-10 14:51:35 +0200 | [diff] [blame] | 1075 | cpuPresence[cpuIndex] = *present; |
Matt Simmering | 39fadb9 | 2023-05-04 21:47:29 -0700 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 1080 | void createSensors( |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 1081 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 1082 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 1083 | const std::shared_ptr<boost::container::flat_set<std::string>>& |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 1084 | sensorsChanged, |
| 1085 | bool activateOnly) |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 1086 | { |
| 1087 | auto getter = std::make_shared<GetSensorConfiguration>( |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 1088 | dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged, |
| 1089 | activateOnly](const ManagedObjectType& sensorConfigs) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 1090 | createSensorsCallback(io, objectServer, dbusConnection, |
| 1091 | sensorConfigs, sensorsChanged, activateOnly); |
| 1092 | }); |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 1093 | std::vector<std::string> types(sensorTypes.size()); |
| 1094 | for (const auto& [type, dt] : sensorTypes) |
| 1095 | { |
| 1096 | types.push_back(type); |
| 1097 | } |
| 1098 | getter->getConfiguration(types); |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 1101 | void propertyInitialize() |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1102 | { |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 1103 | sensorTable = {{"power", sensor_paths::unitWatts}, |
| 1104 | {"curr", sensor_paths::unitAmperes}, |
| 1105 | {"temp", sensor_paths::unitDegreesC}, |
| 1106 | {"in", sensor_paths::unitVolts}, |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 1107 | {"voltage", sensor_paths::unitVolts}, |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 1108 | {"fan", sensor_paths::unitRPMs}}; |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 1109 | |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 1110 | labelMatch = { |
| 1111 | {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)}, |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 1112 | {"pout", PSUProperty("Output Power", 3000, 0, 6, 0)}, |
| 1113 | {"power", PSUProperty("Output Power", 3000, 0, 6, 0)}, |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 1114 | {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)}, |
| 1115 | {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)}, |
| 1116 | {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)}, |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 1117 | {"in_voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
Ian Chien | c82a409 | 2024-07-24 10:52:59 +0800 | [diff] [blame] | 1118 | {"voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 1119 | {"vout", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 1120 | {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)}, |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 1121 | {"in", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 1122 | {"iin", PSUProperty("Input Current", 20, 0, 3, 0)}, |
cchoux | e8a14e9 | 2023-10-25 23:26:27 +0800 | [diff] [blame] | 1123 | {"iout", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1124 | {"curr", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1125 | {"maxiout", PSUProperty("Max Output Current", 255, 0, 3, 0)}, |
| 1126 | {"temp", PSUProperty("Temperature", 127, -128, 3, 0)}, |
| 1127 | {"maxtemp", PSUProperty("Max Temperature", 127, -128, 3, 0)}, |
| 1128 | {"fan", PSUProperty("Fan Speed ", 30000, 0, 0, 0)}}; |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 1129 | |
| 1130 | limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}}, |
| 1131 | {"Failure", {"crit_alarm", "lcrit_alarm"}}}; |
| 1132 | |
Cheng C Yang | 202a1ff | 2020-01-09 09:34:22 +0800 | [diff] [blame] | 1133 | eventMatch = {{"PredictiveFailure", {"power1_alarm"}}, |
| 1134 | {"Failure", {"in2_alarm"}}, |
| 1135 | {"ACLost", {"in1_beep"}}, |
| 1136 | {"ConfigureError", {"in1_fault"}}}; |
| 1137 | |
Joseph Fu | b844e97 | 2023-03-25 10:24:30 +0800 | [diff] [blame] | 1138 | devParamMap = { |
| 1139 | {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}}, |
| 1140 | {DevTypes::IIO, |
| 1141 | {2, R"(\w+_(raw|input)$)", "^(in|out)_([A-Za-z]+)[0-9]*_"}}}; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1142 | } |
| 1143 | |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 1144 | static void powerStateChanged( |
| 1145 | PowerState type, bool newState, |
| 1146 | boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>& |
| 1147 | sensors, |
| 1148 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 1149 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 1150 | { |
| 1151 | if (newState) |
| 1152 | { |
| 1153 | createSensors(io, objectServer, dbusConnection, nullptr, true); |
| 1154 | } |
| 1155 | else |
| 1156 | { |
| 1157 | for (auto& [path, sensor] : sensors) |
| 1158 | { |
| 1159 | if (sensor != nullptr && sensor->readState == type) |
| 1160 | { |
| 1161 | sensor->deactivate(); |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 1167 | int main() |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1168 | { |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 1169 | boost::asio::io_context io; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1170 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 1171 | |
Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 1172 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 1173 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
Zhikui Ren | 3fe3f54 | 2022-11-02 16:37:54 -0700 | [diff] [blame] | 1174 | objectServer.add_manager("/xyz/openbmc_project/control"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1175 | systemBus->request_name("xyz.openbmc_project.PSUSensor"); |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 1176 | auto sensorsChanged = |
| 1177 | std::make_shared<boost::container::flat_set<std::string>>(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1178 | |
Cheng C Yang | 916360b | 2019-05-07 18:47:16 +0800 | [diff] [blame] | 1179 | propertyInitialize(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1180 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 1181 | auto powerCallBack = [&io, &objectServer, |
| 1182 | &systemBus](PowerState type, bool state) { |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 1183 | powerStateChanged(type, state, sensors, io, objectServer, systemBus); |
| 1184 | }; |
| 1185 | |
| 1186 | setupPowerMatchCallback(systemBus, powerCallBack); |
| 1187 | |
| 1188 | boost::asio::post(io, [&]() { |
| 1189 | createSensors(io, objectServer, systemBus, nullptr, false); |
| 1190 | }); |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 1191 | boost::asio::steady_timer filterTimer(io); |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 1192 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 1193 | [&](sdbusplus::message_t& message) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 1194 | if (message.is_method_error()) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1195 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 1196 | lg2::error("callback method error"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1197 | return; |
| 1198 | } |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 1199 | sensorsChanged->insert(message.get_path()); |
| 1200 | filterTimer.expires_after(std::chrono::seconds(3)); |
| 1201 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 1202 | if (ec == boost::asio::error::operation_aborted) |
| 1203 | { |
| 1204 | return; |
| 1205 | } |
| 1206 | if (ec) |
| 1207 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 1208 | lg2::error("timer error"); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 1209 | } |
| 1210 | createSensors(io, objectServer, systemBus, sensorsChanged, |
| 1211 | false); |
| 1212 | }); |
| 1213 | }; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1214 | |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 1215 | boost::asio::steady_timer cpuFilterTimer(io); |
| 1216 | std::function<void(sdbusplus::message_t&)> cpuPresenceHandler = |
| 1217 | [&](sdbusplus::message_t& message) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 1218 | std::string path = message.get_path(); |
| 1219 | boost::to_lower(path); |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 1220 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 1221 | sdbusplus::message::object_path cpuPath(path); |
| 1222 | std::string cpuName = cpuPath.filename(); |
| 1223 | if (!cpuName.starts_with("cpu")) |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 1224 | { |
| 1225 | return; |
| 1226 | } |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 1227 | size_t index = 0; |
| 1228 | try |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 1229 | { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 1230 | index = std::stoi(path.substr(path.size() - 1)); |
| 1231 | } |
| 1232 | catch (const std::invalid_argument&) |
| 1233 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 1234 | lg2::error("Found invalid path: '{PATH}'", "PATH", path); |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 1235 | return; |
| 1236 | } |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 1237 | |
| 1238 | std::string objectName; |
| 1239 | boost::container::flat_map<std::string, std::variant<bool>> values; |
| 1240 | message.read(objectName, values); |
| 1241 | auto findPresence = values.find("Present"); |
Vikash Chandola | f2fc1b3 | 2024-08-29 15:59:27 +0530 | [diff] [blame] | 1242 | if (findPresence == values.end()) |
| 1243 | { |
| 1244 | return; |
| 1245 | } |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 1246 | try |
| 1247 | { |
| 1248 | cpuPresence[index] = std::get<bool>(findPresence->second); |
| 1249 | } |
| 1250 | catch (const std::bad_variant_access& err) |
| 1251 | { |
| 1252 | return; |
| 1253 | } |
| 1254 | |
| 1255 | if (!cpuPresence[index]) |
| 1256 | { |
| 1257 | return; |
| 1258 | } |
| 1259 | cpuFilterTimer.expires_after(std::chrono::seconds(1)); |
| 1260 | cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 1261 | if (ec == boost::asio::error::operation_aborted) |
| 1262 | { |
| 1263 | return; |
| 1264 | } |
| 1265 | if (ec) |
| 1266 | { |
George Liu | 4155a5a | 2025-01-24 15:26:27 +0800 | [diff] [blame] | 1267 | lg2::error("timer error"); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 1268 | return; |
| 1269 | } |
| 1270 | createSensors(io, objectServer, systemBus, nullptr, false); |
| 1271 | }); |
| 1272 | }; |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 1273 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 1274 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 1275 | setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler); |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 1276 | |
| 1277 | matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>( |
| 1278 | static_cast<sdbusplus::bus_t&>(*systemBus), |
| 1279 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 1280 | std::string(cpuInventoryPath) + |
| 1281 | "',arg0namespace='xyz.openbmc_project.Inventory.Item'", |
| 1282 | cpuPresenceHandler)); |
| 1283 | |
Matt Simmering | 39fadb9 | 2023-05-04 21:47:29 -0700 | [diff] [blame] | 1284 | getPresentCpus(systemBus); |
| 1285 | |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 1286 | setupManufacturingModeMatch(*systemBus); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1287 | io.run(); |
| 1288 | } |