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