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