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