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