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