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