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