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