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