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