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