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