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