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