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