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