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