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