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>>& |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 287 | sensorsChanged, |
| 288 | bool activateOnly) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 289 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 290 | int numCreated = 0; |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 291 | bool firstScan = sensorsChanged == nullptr; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 292 | |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 293 | auto devices = instantiateDevices(sensorConfigs, sensors, sensorTypes); |
| 294 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 295 | std::vector<fs::path> pmbusPaths; |
| 296 | if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths)) |
| 297 | { |
| 298 | std::cerr << "No PSU sensors in system\n"; |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | boost::container::flat_set<std::string> directories; |
| 303 | for (const auto& pmbusPath : pmbusPaths) |
| 304 | { |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 305 | boost::container::flat_map<std::string, std::vector<std::string>> |
| 306 | eventPathList; |
Cheng C Yang | 202a1ff | 2020-01-09 09:34:22 +0800 | [diff] [blame] | 307 | boost::container::flat_map< |
| 308 | std::string, |
| 309 | boost::container::flat_map<std::string, std::vector<std::string>>> |
| 310 | groupEventPathList; |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 311 | |
| 312 | std::ifstream nameFile(pmbusPath); |
| 313 | if (!nameFile.good()) |
| 314 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 315 | std::cerr << "Failure finding pmbus path " << pmbusPath << "\n"; |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 316 | continue; |
| 317 | } |
| 318 | |
| 319 | std::string pmbusName; |
| 320 | std::getline(nameFile, pmbusName); |
| 321 | nameFile.close(); |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 322 | |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 323 | if (sensorTypes.find(pmbusName) == sensorTypes.end()) |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 324 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 325 | // To avoid this error message, add your driver name to |
| 326 | // the pmbusNames vector at the top of this file. |
| 327 | std::cerr << "Driver name " << pmbusName |
| 328 | << " not found in sensor whitelist\n"; |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 329 | continue; |
| 330 | } |
| 331 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 332 | auto directory = pmbusPath.parent_path(); |
| 333 | |
| 334 | auto ret = directories.insert(directory.string()); |
| 335 | if (!ret.second) |
| 336 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 337 | std::cerr << "Duplicate path " << directory.string() << "\n"; |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 338 | continue; // check if path has already been searched |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 339 | } |
| 340 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 341 | fs::path device = directory / "device"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 342 | std::string deviceName = fs::canonical(device).stem(); |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 343 | auto findHyphen = deviceName.find('-'); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 344 | if (findHyphen == std::string::npos) |
| 345 | { |
| 346 | std::cerr << "found bad device" << deviceName << "\n"; |
| 347 | continue; |
| 348 | } |
| 349 | std::string busStr = deviceName.substr(0, findHyphen); |
| 350 | std::string addrStr = deviceName.substr(findHyphen + 1); |
| 351 | |
| 352 | size_t bus = 0; |
| 353 | size_t addr = 0; |
| 354 | |
| 355 | try |
| 356 | { |
| 357 | bus = std::stoi(busStr); |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 358 | addr = std::stoi(addrStr, nullptr, 16); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 359 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 360 | catch (const std::invalid_argument&) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 361 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 362 | std::cerr << "Error parsing bus " << busStr << " addr " << addrStr |
| 363 | << "\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 364 | continue; |
| 365 | } |
| 366 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 367 | const SensorBaseConfigMap* baseConfig = nullptr; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 368 | const SensorData* sensorData = nullptr; |
| 369 | const std::string* interfacePath = nullptr; |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 370 | std::string sensorType; |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 371 | size_t thresholdConfSize = 0; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 372 | |
Zev Weiss | 741f26e | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 373 | for (const auto& [path, cfgData] : sensorConfigs) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 374 | { |
Zev Weiss | 741f26e | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 375 | sensorData = &cfgData; |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 376 | for (const auto& [type, dt] : sensorTypes) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 377 | { |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 378 | auto sensorBase = sensorData->find(configInterfaceName(type)); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 379 | if (sensorBase != sensorData->end()) |
| 380 | { |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 381 | baseConfig = &sensorBase->second; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 382 | sensorType = type; |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | if (baseConfig == nullptr) |
| 387 | { |
| 388 | std::cerr << "error finding base configuration for " |
| 389 | << deviceName << "\n"; |
| 390 | continue; |
| 391 | } |
| 392 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 393 | auto configBus = baseConfig->find("Bus"); |
| 394 | auto configAddress = baseConfig->find("Address"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 395 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 396 | if (configBus == baseConfig->end() || |
| 397 | configAddress == baseConfig->end()) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 398 | { |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 399 | std::cerr << "error finding necessary entry in configuration\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 400 | continue; |
| 401 | } |
| 402 | |
Matt Simmering | 6747eba | 2023-01-18 16:03:55 -0800 | [diff] [blame] | 403 | const uint64_t* confBus = |
| 404 | std::get_if<uint64_t>(&(configBus->second)); |
| 405 | const uint64_t* confAddr = |
| 406 | std::get_if<uint64_t>(&(configAddress->second)); |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 407 | if (confBus == nullptr || confAddr == nullptr) |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 408 | { |
| 409 | std::cerr |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 410 | << "Cannot get bus or address, invalid configuration\n"; |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 411 | continue; |
| 412 | } |
| 413 | |
| 414 | if ((*confBus != bus) || (*confAddr != addr)) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 415 | { |
Josh Lehan | 432d1ed | 2019-10-16 12:23:31 -0700 | [diff] [blame] | 416 | std::cerr << "Configuration skipping " << *confBus << "-" |
| 417 | << *confAddr << " because not " << bus << "-" << addr |
| 418 | << "\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 419 | continue; |
| 420 | } |
| 421 | |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 422 | std::vector<thresholds::Threshold> confThresholds; |
| 423 | if (!parseThresholdsFromConfig(*sensorData, confThresholds)) |
| 424 | { |
Zhikui Ren | 85fa3c6 | 2022-02-26 23:03:34 -0800 | [diff] [blame] | 425 | std::cerr << "error populating total thresholds\n"; |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 426 | } |
| 427 | thresholdConfSize = confThresholds.size(); |
| 428 | |
Zev Weiss | 741f26e | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 429 | interfacePath = &path.str; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 430 | break; |
| 431 | } |
| 432 | if (interfacePath == nullptr) |
| 433 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 434 | // To avoid this error message, add your export map entry, |
| 435 | // from Entity Manager, to sensorTypes at the top of this file. |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 436 | std::cerr << "failed to find match for " << deviceName << "\n"; |
| 437 | continue; |
| 438 | } |
| 439 | |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 440 | auto findI2CDev = devices.find(*interfacePath); |
| 441 | |
| 442 | std::shared_ptr<I2CDevice> i2cDev; |
| 443 | if (findI2CDev != devices.end()) |
| 444 | { |
| 445 | if (activateOnly && !findI2CDev->second.second) |
| 446 | { |
| 447 | continue; |
| 448 | } |
| 449 | i2cDev = findI2CDev->second.first; |
| 450 | } |
| 451 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 452 | auto findPSUName = baseConfig->find("Name"); |
| 453 | if (findPSUName == baseConfig->end()) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 454 | { |
| 455 | std::cerr << "could not determine configuration name for " |
| 456 | << deviceName << "\n"; |
| 457 | continue; |
| 458 | } |
Matt Simmering | 6747eba | 2023-01-18 16:03:55 -0800 | [diff] [blame] | 459 | const std::string* psuName = |
| 460 | std::get_if<std::string>(&(findPSUName->second)); |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 461 | if (psuName == nullptr) |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 462 | { |
| 463 | std::cerr << "Cannot find psu name, invalid configuration\n"; |
| 464 | continue; |
| 465 | } |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 466 | |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 467 | auto findCPU = baseConfig->find("CPURequired"); |
| 468 | if (findCPU != baseConfig->end()) |
| 469 | { |
Chris Sides | 3fce1bf | 2023-04-03 16:57:05 -0500 | [diff] [blame] | 470 | size_t index = std::visit(VariantToIntVisitor(), findCPU->second); |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 471 | auto presenceFind = cpuPresence.find(index); |
| 472 | if (presenceFind == cpuPresence.end() || !presenceFind->second) |
| 473 | { |
| 474 | continue; |
| 475 | } |
| 476 | } |
| 477 | |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 478 | // on rescans, only update sensors we were signaled by |
| 479 | if (!firstScan) |
| 480 | { |
Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 481 | std::string psuNameStr = "/" + escapeName(*psuName); |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 482 | auto it = std::find_if(sensorsChanged->begin(), |
| 483 | sensorsChanged->end(), |
| 484 | [psuNameStr](std::string& s) { |
Matt Simmering | 6747eba | 2023-01-18 16:03:55 -0800 | [diff] [blame] | 485 | return s.ends_with(psuNameStr); |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 486 | }); |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 487 | |
| 488 | if (it == sensorsChanged->end()) |
| 489 | { |
| 490 | continue; |
| 491 | } |
| 492 | sensorsChanged->erase(it); |
| 493 | } |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 494 | checkEvent(directory.string(), eventMatch, eventPathList); |
Cheng C Yang | 202a1ff | 2020-01-09 09:34:22 +0800 | [diff] [blame] | 495 | checkGroupEvent(directory.string(), groupEventMatch, |
| 496 | groupEventPathList); |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 497 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 498 | PowerState readState = getPowerState(*baseConfig); |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 499 | |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 500 | /* Check if there are more sensors in the same interface */ |
| 501 | int i = 1; |
| 502 | std::vector<std::string> psuNames; |
| 503 | do |
| 504 | { |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 505 | // Individual string fields: Name, Name1, Name2, Name3, ... |
Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 506 | psuNames.push_back( |
| 507 | escapeName(std::get<std::string>(findPSUName->second))); |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 508 | findPSUName = baseConfig->find("Name" + std::to_string(i++)); |
| 509 | } while (findPSUName != baseConfig->end()); |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 510 | |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 511 | std::vector<fs::path> sensorPaths; |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 512 | if (!findFiles(directory, R"(\w\d+_input$)", sensorPaths, 0)) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 513 | { |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 514 | std::cerr << "No PSU non-label sensor in PSU\n"; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 515 | continue; |
| 516 | } |
| 517 | |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 518 | /* read max value in sysfs for in, curr, power, temp, ... */ |
| 519 | if (!findFiles(directory, R"(\w\d+_max$)", sensorPaths, 0)) |
| 520 | { |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 521 | if constexpr (debug) |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 522 | { |
| 523 | std::cerr << "No max name in PSU \n"; |
| 524 | } |
| 525 | } |
| 526 | |
Zev Weiss | 8569bf2 | 2022-10-11 15:37:44 -0700 | [diff] [blame] | 527 | float pollRate = getPollRate(*baseConfig, PSUSensor::defaultSensorPoll); |
Lei YU | 7170a23 | 2021-02-04 16:19:27 +0800 | [diff] [blame] | 528 | |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 529 | /* Find array of labels to be exposed if it is defined in config */ |
| 530 | std::vector<std::string> findLabels; |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 531 | auto findLabelObj = baseConfig->find("Labels"); |
| 532 | if (findLabelObj != baseConfig->end()) |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 533 | { |
| 534 | findLabels = |
| 535 | std::get<std::vector<std::string>>(findLabelObj->second); |
| 536 | } |
| 537 | |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 538 | std::regex sensorNameRegEx("([A-Za-z]+)[0-9]*_"); |
| 539 | std::smatch matches; |
| 540 | |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 541 | for (const auto& sensorPath : sensorPaths) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 542 | { |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 543 | bool maxLabel = false; |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 544 | std::string labelHead; |
| 545 | std::string sensorPathStr = sensorPath.string(); |
| 546 | std::string sensorNameStr = sensorPath.filename(); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 547 | std::string sensorNameSubStr; |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 548 | if (std::regex_search(sensorNameStr, matches, sensorNameRegEx)) |
| 549 | { |
Josh Lehan | 0649445 | 2019-10-31 09:49:16 -0700 | [diff] [blame] | 550 | // hwmon *_input filename without number: |
| 551 | // in, curr, power, temp, ... |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 552 | sensorNameSubStr = matches[1]; |
| 553 | } |
| 554 | else |
| 555 | { |
Josh Lehan | 0649445 | 2019-10-31 09:49:16 -0700 | [diff] [blame] | 556 | std::cerr << "Could not extract the alpha prefix from " |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 557 | << sensorNameStr; |
| 558 | continue; |
| 559 | } |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 560 | |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 561 | std::string labelPath; |
| 562 | |
| 563 | /* find and differentiate _max and _input to replace "label" */ |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 564 | size_t pos = sensorPathStr.find('_'); |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 565 | if (pos != std::string::npos) |
| 566 | { |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 567 | std::string sensorPathStrMax = sensorPathStr.substr(pos); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 568 | if (sensorPathStrMax == "_max") |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 569 | { |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 570 | labelPath = boost::replace_all_copy(sensorPathStr, "max", |
| 571 | "label"); |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 572 | maxLabel = true; |
| 573 | } |
| 574 | else |
| 575 | { |
| 576 | labelPath = boost::replace_all_copy(sensorPathStr, "input", |
| 577 | "label"); |
| 578 | maxLabel = false; |
| 579 | } |
| 580 | } |
| 581 | else |
| 582 | { |
| 583 | continue; |
| 584 | } |
| 585 | |
Cheng C Yang | ecba9de | 2019-09-12 23:41:50 +0800 | [diff] [blame] | 586 | std::ifstream labelFile(labelPath); |
| 587 | if (!labelFile.good()) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 588 | { |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 589 | if constexpr (debug) |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 590 | { |
| 591 | std::cerr << "Input file " << sensorPath |
| 592 | << " has no corresponding label file\n"; |
| 593 | } |
Josh Lehan | 0649445 | 2019-10-31 09:49:16 -0700 | [diff] [blame] | 594 | // hwmon *_input filename with number: |
| 595 | // temp1, temp2, temp3, ... |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 596 | labelHead = sensorNameStr.substr(0, sensorNameStr.find('_')); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 597 | } |
| 598 | else |
| 599 | { |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 600 | std::string label; |
| 601 | std::getline(labelFile, label); |
| 602 | labelFile.close(); |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 603 | auto findSensor = sensors.find(label); |
| 604 | if (findSensor != sensors.end()) |
| 605 | { |
| 606 | continue; |
| 607 | } |
| 608 | |
Josh Lehan | 0649445 | 2019-10-31 09:49:16 -0700 | [diff] [blame] | 609 | // hwmon corresponding *_label file contents: |
| 610 | // vin1, vout1, ... |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 611 | labelHead = label.substr(0, label.find(' ')); |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 612 | } |
| 613 | |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 614 | /* append "max" for labelMatch */ |
| 615 | if (maxLabel) |
| 616 | { |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 617 | labelHead.insert(0, "max"); |
Manikandan Elumalai | c7e9562 | 2020-06-03 20:22:01 +0530 | [diff] [blame] | 618 | } |
| 619 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 620 | if constexpr (debug) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 621 | { |
| 622 | std::cerr << "Sensor type=\"" << sensorNameSubStr |
| 623 | << "\" label=\"" << labelHead << "\"\n"; |
| 624 | } |
| 625 | |
AppaRao Puli | d9d8caf | 2020-02-27 20:56:59 +0530 | [diff] [blame] | 626 | checkPWMSensor(sensorPath, labelHead, *interfacePath, |
| 627 | dbusConnection, objectServer, psuNames[0]); |
Cheng C Yang | 916360b | 2019-05-07 18:47:16 +0800 | [diff] [blame] | 628 | |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 629 | if (!findLabels.empty()) |
| 630 | { |
| 631 | /* Check if this labelHead is enabled in config file */ |
| 632 | if (std::find(findLabels.begin(), findLabels.end(), |
| 633 | labelHead) == findLabels.end()) |
| 634 | { |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 635 | if constexpr (debug) |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 636 | { |
| 637 | std::cerr << "could not find " << labelHead |
| 638 | << " in the Labels list\n"; |
| 639 | } |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 640 | continue; |
| 641 | } |
| 642 | } |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 643 | |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 644 | auto findProperty = labelMatch.find(labelHead); |
| 645 | if (findProperty == labelMatch.end()) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 646 | { |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 647 | if constexpr (debug) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 648 | { |
| 649 | std::cerr << "Could not find matching default property for " |
| 650 | << labelHead << "\n"; |
| 651 | } |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 652 | continue; |
| 653 | } |
| 654 | |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 655 | // Protect the hardcoded labelMatch list from changes, |
| 656 | // by making a copy and modifying that instead. |
| 657 | // Avoid bleedthrough of one device's customizations to |
| 658 | // the next device, as each should be independently customizable. |
| 659 | psuProperties.push_back(findProperty->second); |
| 660 | auto psuProperty = psuProperties.rbegin(); |
| 661 | |
| 662 | // Use label head as prefix for reading from config file, |
| 663 | // example if temp1: temp1_Name, temp1_Scale, temp1_Min, ... |
| 664 | std::string keyName = labelHead + "_Name"; |
| 665 | std::string keyScale = labelHead + "_Scale"; |
| 666 | std::string keyMin = labelHead + "_Min"; |
| 667 | std::string keyMax = labelHead + "_Max"; |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 668 | std::string keyOffset = labelHead + "_Offset"; |
Lotus Xu | cb5af73 | 2021-09-10 15:18:50 +0800 | [diff] [blame] | 669 | std::string keyPowerState = labelHead + "_PowerState"; |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 670 | |
| 671 | bool customizedName = false; |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 672 | auto findCustomName = baseConfig->find(keyName); |
| 673 | if (findCustomName != baseConfig->end()) |
Josh Lehan | 432d1ed | 2019-10-16 12:23:31 -0700 | [diff] [blame] | 674 | { |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 675 | try |
| 676 | { |
| 677 | psuProperty->labelTypeName = std::visit( |
| 678 | VariantToStringVisitor(), findCustomName->second); |
| 679 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 680 | catch (const std::invalid_argument&) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 681 | { |
| 682 | std::cerr << "Unable to parse " << keyName << "\n"; |
| 683 | continue; |
| 684 | } |
| 685 | |
| 686 | // All strings are valid, including empty string |
| 687 | customizedName = true; |
| 688 | } |
| 689 | |
| 690 | bool customizedScale = false; |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 691 | auto findCustomScale = baseConfig->find(keyScale); |
| 692 | if (findCustomScale != baseConfig->end()) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 693 | { |
| 694 | try |
| 695 | { |
| 696 | psuProperty->sensorScaleFactor = std::visit( |
| 697 | VariantToUnsignedIntVisitor(), findCustomScale->second); |
| 698 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 699 | catch (const std::invalid_argument&) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 700 | { |
| 701 | std::cerr << "Unable to parse " << keyScale << "\n"; |
| 702 | continue; |
| 703 | } |
| 704 | |
| 705 | // Avoid later division by zero |
| 706 | if (psuProperty->sensorScaleFactor > 0) |
| 707 | { |
| 708 | customizedScale = true; |
| 709 | } |
| 710 | else |
| 711 | { |
| 712 | std::cerr << "Unable to accept " << keyScale << "\n"; |
| 713 | continue; |
| 714 | } |
| 715 | } |
| 716 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 717 | auto findCustomMin = baseConfig->find(keyMin); |
| 718 | if (findCustomMin != baseConfig->end()) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 719 | { |
| 720 | try |
| 721 | { |
| 722 | psuProperty->minReading = std::visit( |
| 723 | VariantToDoubleVisitor(), findCustomMin->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 " << keyMin << "\n"; |
| 728 | continue; |
| 729 | } |
| 730 | } |
| 731 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 732 | auto findCustomMax = baseConfig->find(keyMax); |
| 733 | if (findCustomMax != baseConfig->end()) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 734 | { |
| 735 | try |
| 736 | { |
| 737 | psuProperty->maxReading = std::visit( |
| 738 | VariantToDoubleVisitor(), findCustomMax->second); |
| 739 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 740 | catch (const std::invalid_argument&) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 741 | { |
| 742 | std::cerr << "Unable to parse " << keyMax << "\n"; |
| 743 | continue; |
| 744 | } |
| 745 | } |
| 746 | |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 747 | auto findCustomOffset = baseConfig->find(keyOffset); |
| 748 | if (findCustomOffset != baseConfig->end()) |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 749 | { |
| 750 | try |
| 751 | { |
| 752 | psuProperty->sensorOffset = std::visit( |
| 753 | VariantToDoubleVisitor(), findCustomOffset->second); |
| 754 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 755 | catch (const std::invalid_argument&) |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 756 | { |
| 757 | std::cerr << "Unable to parse " << keyOffset << "\n"; |
| 758 | continue; |
| 759 | } |
| 760 | } |
| 761 | |
Lotus Xu | cb5af73 | 2021-09-10 15:18:50 +0800 | [diff] [blame] | 762 | // if we find label head power state set ,override the powerstate. |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 763 | auto findPowerState = baseConfig->find(keyPowerState); |
| 764 | if (findPowerState != baseConfig->end()) |
Lotus Xu | cb5af73 | 2021-09-10 15:18:50 +0800 | [diff] [blame] | 765 | { |
| 766 | std::string powerState = std::visit(VariantToStringVisitor(), |
| 767 | findPowerState->second); |
| 768 | setReadState(powerState, readState); |
| 769 | } |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 770 | if (!(psuProperty->minReading < psuProperty->maxReading)) |
| 771 | { |
| 772 | std::cerr << "Min must be less than Max\n"; |
| 773 | continue; |
| 774 | } |
| 775 | |
| 776 | // If the sensor name is being customized by config file, |
| 777 | // then prefix/suffix composition becomes not necessary, |
| 778 | // and in fact not wanted, because it gets in the way. |
| 779 | std::string psuNameFromIndex; |
| 780 | if (!customizedName) |
| 781 | { |
| 782 | /* Find out sensor name index for this label */ |
| 783 | std::regex rgx("[A-Za-z]+([0-9]+)"); |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 784 | size_t nameIndex{0}; |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 785 | if (std::regex_search(labelHead, matches, rgx)) |
| 786 | { |
| 787 | nameIndex = std::stoi(matches[1]); |
| 788 | |
| 789 | // Decrement to preserve alignment, because hwmon |
| 790 | // human-readable filenames and labels use 1-based |
| 791 | // numbering, but the "Name", "Name1", "Name2", etc. naming |
| 792 | // convention (the psuNames vector) uses 0-based numbering. |
| 793 | if (nameIndex > 0) |
| 794 | { |
| 795 | --nameIndex; |
| 796 | } |
| 797 | } |
| 798 | else |
| 799 | { |
| 800 | nameIndex = 0; |
| 801 | } |
| 802 | |
| 803 | if (psuNames.size() <= nameIndex) |
| 804 | { |
| 805 | std::cerr << "Could not pair " << labelHead |
| 806 | << " with a Name field\n"; |
| 807 | continue; |
| 808 | } |
| 809 | |
| 810 | psuNameFromIndex = psuNames[nameIndex]; |
| 811 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 812 | if constexpr (debug) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 813 | { |
| 814 | std::cerr << "Sensor label head " << labelHead |
| 815 | << " paired with " << psuNameFromIndex |
| 816 | << " at index " << nameIndex << "\n"; |
| 817 | } |
Josh Lehan | 432d1ed | 2019-10-16 12:23:31 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 820 | checkEventLimits(sensorPathStr, limitEventMatch, eventPathList); |
| 821 | |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 822 | // Similarly, if sensor scaling factor is being customized, |
| 823 | // then the below power-of-10 constraint becomes unnecessary, |
| 824 | // as config should be able to specify an arbitrary divisor. |
| 825 | unsigned int factor = psuProperty->sensorScaleFactor; |
| 826 | if (!customizedScale) |
Vijay Khemka | 53ca444 | 2019-07-23 11:03:55 -0700 | [diff] [blame] | 827 | { |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 828 | // Preserve existing usage of hardcoded labelMatch table below |
| 829 | factor = std::pow(10.0, factor); |
Vijay Khemka | 53ca444 | 2019-07-23 11:03:55 -0700 | [diff] [blame] | 830 | |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 831 | /* Change first char of substring to uppercase */ |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 832 | char firstChar = |
| 833 | static_cast<char>(std::toupper(sensorNameSubStr[0])); |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 834 | std::string strScaleFactor = |
| 835 | firstChar + sensorNameSubStr.substr(1) + "ScaleFactor"; |
| 836 | |
| 837 | // Preserve existing configs by accepting earlier syntax, |
| 838 | // example CurrScaleFactor, PowerScaleFactor, ... |
Zev Weiss | e8b97ee | 2022-08-12 15:23:51 -0700 | [diff] [blame] | 839 | auto findScaleFactor = baseConfig->find(strScaleFactor); |
| 840 | if (findScaleFactor != baseConfig->end()) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 841 | { |
| 842 | factor = std::visit(VariantToIntVisitor(), |
| 843 | findScaleFactor->second); |
| 844 | } |
| 845 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 846 | if constexpr (debug) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 847 | { |
| 848 | std::cerr << "Sensor scaling factor " << factor |
| 849 | << " string " << strScaleFactor << "\n"; |
| 850 | } |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Vijay Khemka | 996bad1 | 2019-05-28 15:15:16 -0700 | [diff] [blame] | 853 | std::vector<thresholds::Threshold> sensorThresholds; |
Joshi, Mansi | 14f0ad8 | 2019-11-21 10:52:30 +0530 | [diff] [blame] | 854 | if (!parseThresholdsFromConfig(*sensorData, sensorThresholds, |
| 855 | &labelHead)) |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 856 | { |
James Feist | 17ab6e0 | 2019-06-25 12:28:13 -0700 | [diff] [blame] | 857 | std::cerr << "error populating thresholds for " |
| 858 | << sensorNameSubStr << "\n"; |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 859 | } |
| 860 | |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 861 | auto findSensorUnit = sensorTable.find(sensorNameSubStr); |
| 862 | if (findSensorUnit == sensorTable.end()) |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 863 | { |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 864 | std::cerr << sensorNameSubStr |
Josh Lehan | 0649445 | 2019-10-31 09:49:16 -0700 | [diff] [blame] | 865 | << " is not a recognized sensor type\n"; |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 866 | continue; |
| 867 | } |
| 868 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 869 | if constexpr (debug) |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 870 | { |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 871 | std::cerr << "Sensor properties: Name \"" |
| 872 | << psuProperty->labelTypeName << "\" Scale " |
| 873 | << psuProperty->sensorScaleFactor << " Min " |
| 874 | << psuProperty->minReading << " Max " |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 875 | << psuProperty->maxReading << " Offset " |
| 876 | << psuProperty->sensorOffset << "\n"; |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | std::string sensorName = psuProperty->labelTypeName; |
| 880 | if (customizedName) |
| 881 | { |
| 882 | if (sensorName.empty()) |
| 883 | { |
| 884 | // Allow selective disabling of an individual sensor, |
| 885 | // by customizing its name to an empty string. |
| 886 | std::cerr << "Sensor disabled, empty string\n"; |
| 887 | continue; |
| 888 | } |
| 889 | } |
| 890 | else |
| 891 | { |
| 892 | // Sensor name not customized, do prefix/suffix composition, |
| 893 | // preserving default behavior by using psuNameFromIndex. |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 894 | sensorName = psuNameFromIndex + " " + |
| 895 | psuProperty->labelTypeName; |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 898 | if constexpr (debug) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 899 | { |
| 900 | std::cerr << "Sensor name \"" << sensorName << "\" path \"" |
| 901 | << sensorPathStr << "\" type \"" << sensorType |
| 902 | << "\"\n"; |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 903 | } |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 904 | // destruct existing one first if already created |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 905 | |
| 906 | auto& sensor = sensors[sensorName]; |
| 907 | if (!activateOnly) |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 908 | { |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 909 | sensor = nullptr; |
| 910 | } |
| 911 | |
| 912 | if (sensor != nullptr) |
| 913 | { |
| 914 | sensor->activate(sensorPathStr, i2cDev); |
| 915 | } |
| 916 | else |
| 917 | { |
| 918 | sensors[sensorName] = std::make_shared<PSUSensor>( |
| 919 | sensorPathStr, sensorType, objectServer, dbusConnection, io, |
| 920 | sensorName, std::move(sensorThresholds), *interfacePath, |
| 921 | readState, findSensorUnit->second, factor, |
| 922 | psuProperty->maxReading, psuProperty->minReading, |
| 923 | psuProperty->sensorOffset, labelHead, thresholdConfSize, |
| 924 | pollRate, i2cDev); |
| 925 | sensors[sensorName]->setupRead(); |
| 926 | ++numCreated; |
| 927 | if constexpr (debug) |
| 928 | { |
| 929 | std::cerr << "Created " << numCreated |
| 930 | << " sensors so far\n"; |
| 931 | } |
Josh Lehan | 74d9bd9 | 2019-10-31 08:51:58 -0700 | [diff] [blame] | 932 | } |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 933 | } |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 934 | |
| 935 | // OperationalStatus event |
Cheng C Yang | 92498eb | 2019-09-26 21:59:25 +0800 | [diff] [blame] | 936 | combineEvents[*psuName + "OperationalStatus"] = nullptr; |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 937 | combineEvents[*psuName + "OperationalStatus"] = |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame] | 938 | std::make_unique<PSUCombineEvent>(objectServer, dbusConnection, io, |
| 939 | *psuName, readState, |
| 940 | eventPathList, groupEventPathList, |
| 941 | "OperationalStatus", pollRate); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 942 | } |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 943 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 944 | if constexpr (debug) |
Josh Lehan | 49cfba9 | 2019-10-08 16:50:42 -0700 | [diff] [blame] | 945 | { |
| 946 | std::cerr << "Created total of " << numCreated << " sensors\n"; |
| 947 | } |
Matt Simmering | 6747eba | 2023-01-18 16:03:55 -0800 | [diff] [blame] | 948 | } |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 949 | |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 950 | void createSensors( |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 951 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 952 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 953 | const std::shared_ptr<boost::container::flat_set<std::string>>& |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 954 | sensorsChanged, |
| 955 | bool activateOnly) |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 956 | { |
| 957 | auto getter = std::make_shared<GetSensorConfiguration>( |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 958 | dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged, |
| 959 | activateOnly](const ManagedObjectType& sensorConfigs) { |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 960 | createSensorsCallback(io, objectServer, dbusConnection, |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 961 | sensorConfigs, sensorsChanged, activateOnly); |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 962 | }); |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 963 | std::vector<std::string> types(sensorTypes.size()); |
| 964 | for (const auto& [type, dt] : sensorTypes) |
| 965 | { |
| 966 | types.push_back(type); |
| 967 | } |
| 968 | getter->getConfiguration(types); |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 969 | } |
| 970 | |
Cheng C Yang | 916360b | 2019-05-07 18:47:16 +0800 | [diff] [blame] | 971 | void propertyInitialize(void) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 972 | { |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 973 | sensorTable = {{"power", sensor_paths::unitWatts}, |
| 974 | {"curr", sensor_paths::unitAmperes}, |
| 975 | {"temp", sensor_paths::unitDegreesC}, |
| 976 | {"in", sensor_paths::unitVolts}, |
| 977 | {"fan", sensor_paths::unitRPMs}}; |
Cheng C Yang | e50345b | 2019-04-02 17:26:15 +0800 | [diff] [blame] | 978 | |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 979 | labelMatch = { |
| 980 | {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)}, |
Zhikui Ren | 85fa3c6 | 2022-02-26 23:03:34 -0800 | [diff] [blame] | 981 | {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)}, |
| 982 | {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)}, |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 983 | {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)}, |
| 984 | {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)}, |
| 985 | {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)}, |
| 986 | {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)}, |
Zhikui Ren | 85fa3c6 | 2022-02-26 23:03:34 -0800 | [diff] [blame] | 987 | {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)}, |
| 988 | {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)}, |
| 989 | {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)}, |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 990 | {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)}, |
| 991 | {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)}, |
Patrick Rudolph | 0874ebf | 2023-04-19 08:59:35 +0200 | [diff] [blame] | 992 | {"vin1", PSUProperty("Input Voltage", 300, 0, 3, 0)}, |
| 993 | {"vin2", PSUProperty("Input Voltage", 300, 0, 3, 0)}, |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 994 | {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)}, |
| 995 | {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 996 | {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 997 | {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 998 | {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 999 | {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1000 | {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1001 | {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1002 | {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1003 | {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1004 | {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1005 | {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1006 | {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1007 | {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1008 | {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1009 | {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1010 | {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1011 | {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1012 | {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1013 | {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1014 | {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1015 | {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1016 | {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1017 | {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1018 | {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1019 | {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1020 | {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1021 | {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1022 | {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1023 | {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1024 | {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1025 | {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1026 | {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1027 | {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)}, |
Tim Chao | 6f379ce | 2022-02-24 11:08:09 +0800 | [diff] [blame] | 1028 | {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 1029 | {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
Tim Chao | 6f379ce | 2022-02-24 11:08:09 +0800 | [diff] [blame] | 1030 | {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1031 | {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1032 | {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1033 | {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1034 | {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
| 1035 | {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)}, |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 1036 | {"iin", PSUProperty("Input Current", 20, 0, 3, 0)}, |
Zhikui Ren | 85fa3c6 | 2022-02-26 23:03:34 -0800 | [diff] [blame] | 1037 | {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)}, |
| 1038 | {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)}, |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 1039 | {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1040 | {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1041 | {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1042 | {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1043 | {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1044 | {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1045 | {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1046 | {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1047 | {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1048 | {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1049 | {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1050 | {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1051 | {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1052 | {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1053 | {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)}, |
Zhikui Ren | 85fa3c6 | 2022-02-26 23:03:34 -0800 | [diff] [blame] | 1054 | {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1055 | {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)}, |
| 1056 | {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)}, |
Jeff Lin | e41d52f | 2021-04-07 19:38:51 +0800 | [diff] [blame] | 1057 | {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)}, |
| 1058 | {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)}, |
| 1059 | {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)}, |
| 1060 | {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)}, |
| 1061 | {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)}, |
| 1062 | {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)}, |
| 1063 | {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)}, |
| 1064 | {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)}, |
| 1065 | {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)}, |
Zev Weiss | ea2db48 | 2022-06-20 18:12:53 -0700 | [diff] [blame] | 1066 | {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)}, |
| 1067 | {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)}, |
| 1068 | {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}}; |
Cheng C Yang | 916360b | 2019-05-07 18:47:16 +0800 | [diff] [blame] | 1069 | |
Matt Simmering | 6747eba | 2023-01-18 16:03:55 -0800 | [diff] [blame] | 1070 | pwmTable = {{"fan1", "Fan_1"}, |
| 1071 | {"fan2", "Fan_2"}, |
| 1072 | {"fan3", "Fan_3"}, |
| 1073 | {"fan4", "Fan_4"}}; |
Cheng C Yang | 58b2b53 | 2019-05-31 00:19:45 +0800 | [diff] [blame] | 1074 | |
| 1075 | limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}}, |
| 1076 | {"Failure", {"crit_alarm", "lcrit_alarm"}}}; |
| 1077 | |
Cheng C Yang | 202a1ff | 2020-01-09 09:34:22 +0800 | [diff] [blame] | 1078 | eventMatch = {{"PredictiveFailure", {"power1_alarm"}}, |
| 1079 | {"Failure", {"in2_alarm"}}, |
| 1080 | {"ACLost", {"in1_beep"}}, |
| 1081 | {"ConfigureError", {"in1_fault"}}}; |
| 1082 | |
| 1083 | groupEventMatch = {{"FanFault", |
| 1084 | {{"fan1", {"fan1_alarm", "fan1_fault"}}, |
Zev Weiss | ea2db48 | 2022-06-20 18:12:53 -0700 | [diff] [blame] | 1085 | {"fan2", {"fan2_alarm", "fan2_fault"}}, |
| 1086 | {"fan3", {"fan3_alarm", "fan3_fault"}}, |
| 1087 | {"fan4", {"fan4_alarm", "fan4_fault"}}}}}; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1088 | } |
| 1089 | |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 1090 | static void powerStateChanged( |
| 1091 | PowerState type, bool newState, |
| 1092 | boost::container::flat_map<std::string, std::shared_ptr<PSUSensor>>& |
| 1093 | sensors, |
| 1094 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 1095 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 1096 | { |
| 1097 | if (newState) |
| 1098 | { |
| 1099 | createSensors(io, objectServer, dbusConnection, nullptr, true); |
| 1100 | } |
| 1101 | else |
| 1102 | { |
| 1103 | for (auto& [path, sensor] : sensors) |
| 1104 | { |
| 1105 | if (sensor != nullptr && sensor->readState == type) |
| 1106 | { |
| 1107 | sensor->deactivate(); |
| 1108 | } |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 1113 | int main() |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1114 | { |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 1115 | boost::asio::io_context io; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1116 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 1117 | |
Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 1118 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 1119 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
Zhikui Ren | 3fe3f54 | 2022-11-02 16:37:54 -0700 | [diff] [blame] | 1120 | objectServer.add_manager("/xyz/openbmc_project/control"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1121 | systemBus->request_name("xyz.openbmc_project.PSUSensor"); |
Zhikui Ren | 23c96e7 | 2020-11-05 22:32:28 -0800 | [diff] [blame] | 1122 | auto sensorsChanged = |
| 1123 | std::make_shared<boost::container::flat_set<std::string>>(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1124 | |
Cheng C Yang | 916360b | 2019-05-07 18:47:16 +0800 | [diff] [blame] | 1125 | propertyInitialize(); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1126 | |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 1127 | auto powerCallBack = |
| 1128 | [&io, &objectServer, &systemBus](PowerState type, bool state) { |
| 1129 | powerStateChanged(type, state, sensors, io, objectServer, systemBus); |
| 1130 | }; |
| 1131 | |
| 1132 | setupPowerMatchCallback(systemBus, powerCallBack); |
| 1133 | |
| 1134 | boost::asio::post(io, [&]() { |
| 1135 | createSensors(io, objectServer, systemBus, nullptr, false); |
| 1136 | }); |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 1137 | boost::asio::steady_timer filterTimer(io); |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 1138 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 1139 | [&](sdbusplus::message_t& message) { |
Matt Simmering | 6747eba | 2023-01-18 16:03:55 -0800 | [diff] [blame] | 1140 | if (message.is_method_error()) |
| 1141 | { |
| 1142 | std::cerr << "callback method error\n"; |
| 1143 | return; |
| 1144 | } |
| 1145 | sensorsChanged->insert(message.get_path()); |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 1146 | filterTimer.expires_after(std::chrono::seconds(3)); |
Matt Simmering | 6747eba | 2023-01-18 16:03:55 -0800 | [diff] [blame] | 1147 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 1148 | if (ec == boost::asio::error::operation_aborted) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1149 | { |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1150 | return; |
| 1151 | } |
Matt Simmering | 6747eba | 2023-01-18 16:03:55 -0800 | [diff] [blame] | 1152 | if (ec) |
| 1153 | { |
| 1154 | std::cerr << "timer error\n"; |
| 1155 | } |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 1156 | createSensors(io, objectServer, systemBus, sensorsChanged, false); |
Matt Simmering | 6747eba | 2023-01-18 16:03:55 -0800 | [diff] [blame] | 1157 | }); |
| 1158 | }; |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1159 | |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 1160 | boost::asio::steady_timer cpuFilterTimer(io); |
| 1161 | std::function<void(sdbusplus::message_t&)> cpuPresenceHandler = |
| 1162 | [&](sdbusplus::message_t& message) { |
| 1163 | std::string path = message.get_path(); |
| 1164 | boost::to_lower(path); |
| 1165 | |
| 1166 | sdbusplus::message::object_path cpuPath(path); |
| 1167 | std::string cpuName = cpuPath.filename(); |
| 1168 | if (!cpuName.starts_with("cpu")) |
| 1169 | { |
| 1170 | return; |
| 1171 | } |
| 1172 | size_t index = 0; |
| 1173 | try |
| 1174 | { |
| 1175 | index = std::stoi(path.substr(path.size() - 1)); |
| 1176 | } |
| 1177 | catch (const std::invalid_argument&) |
| 1178 | { |
| 1179 | std::cerr << "Found invalid path " << path << "\n"; |
| 1180 | return; |
| 1181 | } |
| 1182 | |
| 1183 | std::string objectName; |
| 1184 | boost::container::flat_map<std::string, std::variant<bool>> values; |
| 1185 | message.read(objectName, values); |
| 1186 | auto findPresence = values.find("Present"); |
| 1187 | try |
| 1188 | { |
| 1189 | cpuPresence[index] = std::get<bool>(findPresence->second); |
| 1190 | } |
Chris Sides | 3fce1bf | 2023-04-03 16:57:05 -0500 | [diff] [blame] | 1191 | catch (const std::bad_variant_access& err) |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 1192 | { |
| 1193 | return; |
| 1194 | } |
| 1195 | |
Chris Sides | 3fce1bf | 2023-04-03 16:57:05 -0500 | [diff] [blame] | 1196 | if (!cpuPresence[index]) |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 1197 | { |
| 1198 | return; |
| 1199 | } |
| 1200 | cpuFilterTimer.expires_after(std::chrono::seconds(1)); |
| 1201 | cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 1202 | if (ec == boost::asio::error::operation_aborted) |
| 1203 | { |
| 1204 | return; |
| 1205 | } |
| 1206 | if (ec) |
| 1207 | { |
| 1208 | std::cerr << "timer error\n"; |
| 1209 | return; |
| 1210 | } |
Matt Simmering | cafd72f | 2022-12-16 15:35:12 -0800 | [diff] [blame] | 1211 | createSensors(io, objectServer, systemBus, nullptr, false); |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 1212 | }); |
| 1213 | }; |
| 1214 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 1215 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 1216 | setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler); |
Matt Simmering | e4e6a28 | 2023-03-03 14:41:04 -0800 | [diff] [blame] | 1217 | |
| 1218 | matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>( |
| 1219 | static_cast<sdbusplus::bus_t&>(*systemBus), |
| 1220 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 1221 | std::string(cpuInventoryPath) + |
| 1222 | "',arg0namespace='xyz.openbmc_project.Inventory.Item'", |
| 1223 | cpuPresenceHandler)); |
| 1224 | |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 1225 | setupManufacturingModeMatch(*systemBus); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1226 | io.run(); |
| 1227 | } |