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