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