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