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