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