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