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