James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2017 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 | */ |
| 16 | |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 17 | #include "PresenceGpio.hpp" |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 18 | #include "PwmSensor.hpp" |
| 19 | #include "TachSensor.hpp" |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 20 | #include "Thresholds.hpp" |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 21 | #include "Utils.hpp" |
| 22 | #include "VariantVisitors.hpp" |
| 23 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 24 | #include <boost/algorithm/string/replace.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 25 | #include <boost/asio/error.hpp> |
| 26 | #include <boost/asio/io_context.hpp> |
| 27 | #include <boost/asio/post.hpp> |
| 28 | #include <boost/asio/steady_timer.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 29 | #include <boost/container/flat_map.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 30 | #include <boost/container/flat_set.hpp> |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 31 | #include <phosphor-logging/lg2.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 32 | #include <sdbusplus/asio/connection.hpp> |
| 33 | #include <sdbusplus/asio/object_server.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 34 | #include <sdbusplus/bus.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 35 | #include <sdbusplus/bus/match.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 36 | #include <sdbusplus/message.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 37 | |
| 38 | #include <array> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 39 | #include <chrono> |
| 40 | #include <cstddef> |
| 41 | #include <cstdint> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 42 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 43 | #include <fstream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 44 | #include <functional> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 45 | #include <ios> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 46 | #include <map> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 47 | #include <memory> |
| 48 | #include <optional> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 49 | #include <regex> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 50 | #include <string> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 51 | #include <system_error> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 52 | #include <utility> |
| 53 | #include <variant> |
| 54 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 55 | |
Yong Zhao | a3e8f2a | 2021-01-09 02:22:43 +0000 | [diff] [blame] | 56 | // The following two structures need to be consistent |
Chris Sides | c361e22 | 2023-04-05 15:18:20 -0500 | [diff] [blame] | 57 | static auto sensorTypes{std::to_array<const char*>( |
| 58 | {"AspeedFan", "I2CFan", "NuvotonFan", "HPEFan"})}; |
Yong Zhao | a3e8f2a | 2021-01-09 02:22:43 +0000 | [diff] [blame] | 59 | |
| 60 | enum FanTypes |
| 61 | { |
| 62 | aspeed = 0, |
| 63 | i2c, |
| 64 | nuvoton, |
Chris Sides | c361e22 | 2023-04-05 15:18:20 -0500 | [diff] [blame] | 65 | hpe, |
Yong Zhao | a3e8f2a | 2021-01-09 02:22:43 +0000 | [diff] [blame] | 66 | max, |
| 67 | }; |
| 68 | |
| 69 | static_assert(std::tuple_size<decltype(sensorTypes)>::value == FanTypes::max, |
| 70 | "sensorTypes element number is not equal to FanTypes number"); |
| 71 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 72 | constexpr const char* redundancyConfiguration = |
| 73 | "xyz.openbmc_project.Configuration.FanRedundancy"; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 74 | static std::regex inputRegex(R"(fan(\d+)_input)"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 75 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 76 | // todo: power supply fan redundancy |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 77 | std::optional<RedundancySensor> systemRedundancy; |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 78 | |
Chris Sides | 3d5260d | 2023-04-10 15:45:00 -0500 | [diff] [blame] | 79 | static const std::map<std::string, FanTypes> compatibleFanTypes = { |
| 80 | {"aspeed,ast2400-pwm-tacho", FanTypes::aspeed}, |
| 81 | {"aspeed,ast2500-pwm-tacho", FanTypes::aspeed}, |
Potin Lai | 40c4d68 | 2023-05-19 17:19:03 +0800 | [diff] [blame] | 82 | {"aspeed,ast2600-pwm-tach", FanTypes::aspeed}, |
Chris Sides | c361e22 | 2023-04-05 15:18:20 -0500 | [diff] [blame] | 83 | {"nuvoton,npcm750-pwm-fan", FanTypes::nuvoton}, |
Brian Ma | d37e1db | 2023-05-08 13:36:53 +0800 | [diff] [blame] | 84 | {"nuvoton,npcm845-pwm-fan", FanTypes::nuvoton}, |
Chris Sides | c361e22 | 2023-04-05 15:18:20 -0500 | [diff] [blame] | 85 | {"hpe,gxp-fan-ctrl", FanTypes::hpe} |
Chris Sides | 3d5260d | 2023-04-10 15:45:00 -0500 | [diff] [blame] | 86 | // add compatible string here for new fan type |
| 87 | }; |
| 88 | |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 89 | FanTypes getFanType(const std::filesystem::path& parentPath) |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 90 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 91 | std::filesystem::path linkPath = parentPath / "of_node"; |
| 92 | if (!std::filesystem::exists(linkPath)) |
Zhikui Ren | 3996322 | 2023-05-04 17:06:50 -0700 | [diff] [blame] | 93 | { |
| 94 | return FanTypes::i2c; |
| 95 | } |
Chris Sides | 9a472e8 | 2023-04-03 15:13:37 -0500 | [diff] [blame] | 96 | |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 97 | std::string canonical = std::filesystem::canonical(linkPath); |
Chris Sides | 9a472e8 | 2023-04-03 15:13:37 -0500 | [diff] [blame] | 98 | std::string compatiblePath = canonical + "/compatible"; |
| 99 | std::ifstream compatibleStream(compatiblePath); |
| 100 | |
Chris Sides | 3d5260d | 2023-04-10 15:45:00 -0500 | [diff] [blame] | 101 | if (!compatibleStream) |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 102 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 103 | lg2::error("Error opening '{PATH}'", "PATH", compatiblePath); |
Chris Sides | 3d5260d | 2023-04-10 15:45:00 -0500 | [diff] [blame] | 104 | return FanTypes::i2c; |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 105 | } |
Chris Sides | 9a472e8 | 2023-04-03 15:13:37 -0500 | [diff] [blame] | 106 | |
| 107 | std::string compatibleString; |
Chris Sides | 3d5260d | 2023-04-10 15:45:00 -0500 | [diff] [blame] | 108 | while (std::getline(compatibleStream, compatibleString)) |
Peter Lundgren | 8843b62 | 2019-09-12 10:33:41 -0700 | [diff] [blame] | 109 | { |
Chris Sides | 9a472e8 | 2023-04-03 15:13:37 -0500 | [diff] [blame] | 110 | compatibleString.pop_back(); // trim EOL before comparisons |
| 111 | |
Chris Sides | 3d5260d | 2023-04-10 15:45:00 -0500 | [diff] [blame] | 112 | std::map<std::string, FanTypes>::const_iterator compatibleIterator = |
| 113 | compatibleFanTypes.find(compatibleString); |
| 114 | |
| 115 | if (compatibleIterator != compatibleFanTypes.end()) |
Chris Sides | 9a472e8 | 2023-04-03 15:13:37 -0500 | [diff] [blame] | 116 | { |
Chris Sides | 3d5260d | 2023-04-10 15:45:00 -0500 | [diff] [blame] | 117 | return compatibleIterator->second; |
Chris Sides | 9a472e8 | 2023-04-03 15:13:37 -0500 | [diff] [blame] | 118 | } |
Peter Lundgren | 8843b62 | 2019-09-12 10:33:41 -0700 | [diff] [blame] | 119 | } |
Chris Sides | 9a472e8 | 2023-04-03 15:13:37 -0500 | [diff] [blame] | 120 | |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 121 | return FanTypes::i2c; |
| 122 | } |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 123 | void enablePwm(const std::filesystem::path& filePath) |
Jeff Lin | abf91de | 2020-12-23 10:55:42 +0800 | [diff] [blame] | 124 | { |
| 125 | std::fstream enableFile(filePath, std::ios::in | std::ios::out); |
| 126 | if (!enableFile.good()) |
| 127 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 128 | lg2::error("Error read/write '{PATH}'", "PATH", filePath); |
Jeff Lin | abf91de | 2020-12-23 10:55:42 +0800 | [diff] [blame] | 129 | return; |
| 130 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 131 | |
Jeff Lin | abf91de | 2020-12-23 10:55:42 +0800 | [diff] [blame] | 132 | std::string regulateMode; |
| 133 | std::getline(enableFile, regulateMode); |
| 134 | if (regulateMode == "0") |
| 135 | { |
| 136 | enableFile << 1; |
| 137 | } |
| 138 | } |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 139 | bool findPwmfanPath(unsigned int configPwmfanIndex, |
| 140 | std::filesystem::path& pwmPath) |
Howard Chiu | ddf25d1 | 2021-12-02 15:14:44 +0800 | [diff] [blame] | 141 | { |
| 142 | /* Search PWM since pwm-fan had separated |
| 143 | * PWM from tach directory and 1 channel only*/ |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 144 | std::vector<std::filesystem::path> pwmfanPaths; |
Howard Chiu | ddf25d1 | 2021-12-02 15:14:44 +0800 | [diff] [blame] | 145 | std::string pwnfanDevName("pwm-fan"); |
| 146 | |
| 147 | pwnfanDevName += std::to_string(configPwmfanIndex); |
| 148 | |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 149 | if (!findFiles(std::filesystem::path("/sys/class/hwmon"), R"(pwm\d+)", |
| 150 | pwmfanPaths)) |
Howard Chiu | ddf25d1 | 2021-12-02 15:14:44 +0800 | [diff] [blame] | 151 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 152 | lg2::error("No PWMs are found!"); |
Howard Chiu | ddf25d1 | 2021-12-02 15:14:44 +0800 | [diff] [blame] | 153 | return false; |
| 154 | } |
| 155 | for (const auto& path : pwmfanPaths) |
| 156 | { |
| 157 | std::error_code ec; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 158 | std::filesystem::path link = |
| 159 | std::filesystem::read_symlink(path.parent_path() / "device", ec); |
Howard Chiu | ddf25d1 | 2021-12-02 15:14:44 +0800 | [diff] [blame] | 160 | |
| 161 | if (ec) |
| 162 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 163 | lg2::error("read_symlink() failed: '{ERROR_MESSAGE}'", |
| 164 | "ERROR_MESSAGE", ec.message()); |
Howard Chiu | ddf25d1 | 2021-12-02 15:14:44 +0800 | [diff] [blame] | 165 | continue; |
| 166 | } |
| 167 | |
| 168 | if (link.filename().string() == pwnfanDevName) |
| 169 | { |
| 170 | pwmPath = path; |
| 171 | return true; |
| 172 | } |
| 173 | } |
| 174 | return false; |
| 175 | } |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 176 | bool findPwmPath(const std::filesystem::path& directory, unsigned int pwm, |
| 177 | std::filesystem::path& pwmPath) |
Howard Chiu | ddf25d1 | 2021-12-02 15:14:44 +0800 | [diff] [blame] | 178 | { |
| 179 | std::error_code ec; |
| 180 | |
| 181 | /* Assuming PWM file is appeared in the same directory as fanX_input */ |
| 182 | auto path = directory / ("pwm" + std::to_string(pwm + 1)); |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 183 | bool exists = std::filesystem::exists(path, ec); |
Howard Chiu | ddf25d1 | 2021-12-02 15:14:44 +0800 | [diff] [blame] | 184 | |
| 185 | if (ec || !exists) |
| 186 | { |
| 187 | /* PWM file not exist or error happened */ |
| 188 | if (ec) |
| 189 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 190 | lg2::error("exists() failed: '{ERROR_MESSAGE}'", "ERROR_MESSAGE", |
| 191 | ec.message()); |
Howard Chiu | ddf25d1 | 2021-12-02 15:14:44 +0800 | [diff] [blame] | 192 | } |
| 193 | /* try search form pwm-fanX directory */ |
| 194 | return findPwmfanPath(pwm, pwmPath); |
| 195 | } |
| 196 | |
| 197 | pwmPath = path; |
| 198 | return true; |
| 199 | } |
Justin Ledford | 9c47bd7 | 2022-08-27 01:02:09 +0000 | [diff] [blame] | 200 | |
| 201 | // The argument to this function should be the fanN_input file that we want to |
| 202 | // enable. The function will locate the corresponding fanN_enable file if it |
| 203 | // exists. Note that some drivers don't provide this file if the sensors are |
| 204 | // always enabled. |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 205 | void enableFanInput(const std::filesystem::path& fanInputPath) |
Justin Ledford | 9c47bd7 | 2022-08-27 01:02:09 +0000 | [diff] [blame] | 206 | { |
| 207 | std::error_code ec; |
| 208 | std::string path(fanInputPath.string()); |
| 209 | boost::replace_last(path, "input", "enable"); |
| 210 | |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 211 | bool exists = std::filesystem::exists(path, ec); |
Justin Ledford | 9c47bd7 | 2022-08-27 01:02:09 +0000 | [diff] [blame] | 212 | if (ec || !exists) |
| 213 | { |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | std::fstream enableFile(path, std::ios::out); |
| 218 | if (!enableFile.good()) |
| 219 | { |
| 220 | return; |
| 221 | } |
| 222 | enableFile << 1; |
| 223 | } |
| 224 | |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 225 | void createRedundancySensor( |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 226 | const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>& |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 227 | sensors, |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 228 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 229 | sdbusplus::asio::object_server& objectServer) |
| 230 | { |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 231 | conn->async_method_call( |
| 232 | [&objectServer, &sensors](boost::system::error_code& ec, |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 233 | const ManagedObjectType& managedObj) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 234 | if (ec) |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 235 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 236 | lg2::error("Error calling entity manager"); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 237 | return; |
| 238 | } |
| 239 | for (const auto& [path, interfaces] : managedObj) |
| 240 | { |
| 241 | for (const auto& [intf, cfg] : interfaces) |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 242 | { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 243 | if (intf == redundancyConfiguration) |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 244 | { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 245 | // currently only support one |
| 246 | auto findCount = cfg.find("AllowedFailures"); |
| 247 | if (findCount == cfg.end()) |
| 248 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 249 | lg2::error("Malformed redundancy record"); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 250 | return; |
| 251 | } |
| 252 | std::vector<std::string> sensorList; |
| 253 | |
| 254 | for (const auto& [name, sensor] : sensors) |
| 255 | { |
| 256 | sensorList.push_back( |
| 257 | "/xyz/openbmc_project/sensors/fan_tach/" + |
| 258 | sensor->name); |
| 259 | } |
| 260 | systemRedundancy.reset(); |
| 261 | systemRedundancy.emplace(RedundancySensor( |
| 262 | std::get<uint64_t>(findCount->second), sensorList, |
| 263 | objectServer, path)); |
| 264 | |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | } |
| 268 | } |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 269 | }, |
Nan Zhou | 3e620af | 2022-09-20 22:28:31 +0000 | [diff] [blame] | 270 | "xyz.openbmc_project.EntityManager", "/xyz/openbmc_project/inventory", |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 271 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 272 | } |
| 273 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 274 | void createSensors( |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 275 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 276 | boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>& |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 277 | tachSensors, |
| 278 | boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>& |
| 279 | pwmSensors, |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 280 | boost::container::flat_map<std::string, std::weak_ptr<PresenceGpio>>& |
| 281 | presenceGpios, |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 282 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 283 | const std::shared_ptr<boost::container::flat_set<std::string>>& |
James Feist | f27a55c | 2020-08-04 14:27:30 -0700 | [diff] [blame] | 284 | sensorsChanged, |
| 285 | size_t retries = 0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 286 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 287 | auto getter = std::make_shared< |
| 288 | GetSensorConfiguration>(dbusConnection, [&io, &objectServer, |
| 289 | &tachSensors, &pwmSensors, |
| 290 | &presenceGpios, |
| 291 | &dbusConnection, |
| 292 | sensorsChanged]( |
| 293 | const ManagedObjectType& |
| 294 | sensorConfigurations) { |
| 295 | bool firstScan = sensorsChanged == nullptr; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 296 | std::vector<std::filesystem::path> paths; |
| 297 | if (!findFiles(std::filesystem::path("/sys/class/hwmon"), |
| 298 | R"(fan\d+_input)", paths)) |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 299 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 300 | lg2::error("No fan sensors in system"); |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 301 | return; |
| 302 | } |
| 303 | |
| 304 | // iterate through all found fan sensors, and try to match them with |
| 305 | // configuration |
| 306 | for (const auto& path : paths) |
| 307 | { |
| 308 | std::smatch match; |
| 309 | std::string pathStr = path.string(); |
| 310 | |
| 311 | std::regex_search(pathStr, match, inputRegex); |
| 312 | std::string indexStr = *(match.begin() + 1); |
| 313 | |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 314 | std::filesystem::path directory = path.parent_path(); |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 315 | FanTypes fanType = getFanType(directory); |
| 316 | std::string cfgIntf = configInterfaceName(sensorTypes[fanType]); |
| 317 | |
| 318 | // convert to 0 based |
| 319 | size_t index = std::stoul(indexStr) - 1; |
| 320 | |
| 321 | const char* baseType = nullptr; |
| 322 | const SensorData* sensorData = nullptr; |
| 323 | const std::string* interfacePath = nullptr; |
| 324 | const SensorBaseConfiguration* baseConfiguration = nullptr; |
| 325 | for (const auto& [path, cfgData] : sensorConfigurations) |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 326 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 327 | // find the base of the configuration to see if indexes |
| 328 | // match |
| 329 | auto sensorBaseFind = cfgData.find(cfgIntf); |
| 330 | if (sensorBaseFind == cfgData.end()) |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 331 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 332 | continue; |
| 333 | } |
| 334 | |
| 335 | baseConfiguration = &(*sensorBaseFind); |
| 336 | interfacePath = &path.str; |
| 337 | baseType = sensorTypes[fanType]; |
| 338 | |
| 339 | auto findIndex = baseConfiguration->second.find("Index"); |
| 340 | if (findIndex == baseConfiguration->second.end()) |
| 341 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 342 | lg2::error("'{INTERFACE}' missing index", "INTERFACE", |
| 343 | baseConfiguration->first); |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 344 | continue; |
| 345 | } |
| 346 | unsigned int configIndex = std::visit( |
| 347 | VariantToUnsignedIntVisitor(), findIndex->second); |
| 348 | if (configIndex != index) |
| 349 | { |
| 350 | continue; |
| 351 | } |
| 352 | if (fanType == FanTypes::aspeed || |
| 353 | fanType == FanTypes::nuvoton || fanType == FanTypes::hpe) |
| 354 | { |
| 355 | // there will be only 1 aspeed or nuvoton or hpe sensor |
| 356 | // object in sysfs, we found the fan |
| 357 | sensorData = &cfgData; |
| 358 | break; |
| 359 | } |
| 360 | if (fanType == FanTypes::i2c) |
| 361 | { |
| 362 | std::string deviceName = |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 363 | std::filesystem::read_symlink(directory / "device") |
| 364 | .filename(); |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 365 | |
| 366 | size_t bus = 0; |
| 367 | size_t addr = 0; |
| 368 | if (!getDeviceBusAddr(deviceName, bus, addr)) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 369 | { |
Akshit Shah | 03d333e | 2023-08-23 22:14:28 +0000 | [diff] [blame] | 370 | continue; |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 371 | } |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 372 | |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 373 | auto findBus = baseConfiguration->second.find("Bus"); |
| 374 | auto findAddress = |
| 375 | baseConfiguration->second.find("Address"); |
| 376 | if (findBus == baseConfiguration->second.end() || |
| 377 | findAddress == baseConfiguration->second.end()) |
| 378 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 379 | lg2::error("'{INTERFACE}' missing bus or address", |
| 380 | "INTERFACE", baseConfiguration->first); |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 381 | continue; |
| 382 | } |
| 383 | unsigned int configBus = std::visit( |
| 384 | VariantToUnsignedIntVisitor(), findBus->second); |
| 385 | unsigned int configAddress = std::visit( |
| 386 | VariantToUnsignedIntVisitor(), findAddress->second); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 387 | |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 388 | if (configBus == bus && configAddress == addr) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 389 | { |
Zev Weiss | 77636ec | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 390 | sensorData = &cfgData; |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 391 | break; |
| 392 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | if (sensorData == nullptr) |
| 396 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 397 | lg2::error("failed to find match for '{PATH}'", "PATH", |
| 398 | path.string()); |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 399 | continue; |
| 400 | } |
| 401 | |
| 402 | auto findSensorName = baseConfiguration->second.find("Name"); |
| 403 | |
| 404 | if (findSensorName == baseConfiguration->second.end()) |
| 405 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 406 | lg2::error( |
| 407 | "could not determine configuration name for '{PATH}'", |
| 408 | "PATH", path.string()); |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 409 | continue; |
| 410 | } |
| 411 | std::string sensorName = |
| 412 | std::get<std::string>(findSensorName->second); |
| 413 | |
| 414 | // on rescans, only update sensors we were signaled by |
| 415 | auto findSensor = tachSensors.find(sensorName); |
| 416 | if (!firstScan && findSensor != tachSensors.end()) |
| 417 | { |
| 418 | bool found = false; |
| 419 | for (auto it = sensorsChanged->begin(); |
| 420 | it != sensorsChanged->end(); it++) |
| 421 | { |
| 422 | if (it->ends_with(findSensor->second->name)) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 423 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 424 | sensorsChanged->erase(it); |
| 425 | findSensor->second = nullptr; |
| 426 | found = true; |
| 427 | break; |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 430 | if (!found) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 431 | { |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 432 | continue; |
| 433 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 434 | } |
| 435 | std::vector<thresholds::Threshold> sensorThresholds; |
| 436 | if (!parseThresholdsFromConfig(*sensorData, sensorThresholds)) |
| 437 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 438 | lg2::error("error populating thresholds for '{NAME}'", "NAME", |
| 439 | sensorName); |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 440 | } |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 441 | |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 442 | auto presenceConfig = |
| 443 | sensorData->find(cfgIntf + std::string(".Presence")); |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 444 | |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 445 | std::shared_ptr<PresenceGpio> presenceGpio(nullptr); |
| 446 | |
| 447 | // presence sensors are optional |
| 448 | if (presenceConfig != sensorData->end()) |
| 449 | { |
| 450 | auto findPolarity = presenceConfig->second.find("Polarity"); |
| 451 | auto findPinName = presenceConfig->second.find("PinName"); |
| 452 | |
| 453 | if (findPinName == presenceConfig->second.end() || |
| 454 | findPolarity == presenceConfig->second.end()) |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 455 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 456 | lg2::error("Malformed Presence Configuration"); |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 457 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 458 | else |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 459 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 460 | bool inverted = |
| 461 | std::get<std::string>(findPolarity->second) == "Low"; |
| 462 | const auto* pinName = |
| 463 | std::get_if<std::string>(&findPinName->second); |
| 464 | |
| 465 | if (pinName != nullptr) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 466 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 467 | auto findPresenceGpio = presenceGpios.find(*pinName); |
| 468 | if (findPresenceGpio != presenceGpios.end()) |
Patrick Rudolph | 8b34f2c | 2024-07-16 11:38:45 +0200 | [diff] [blame] | 469 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 470 | auto p = findPresenceGpio->second.lock(); |
| 471 | if (p) |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 472 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 473 | presenceGpio = p; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 474 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 475 | } |
| 476 | if (!presenceGpio) |
| 477 | { |
Xiaochao Ma | 4bbd02d | 2022-06-21 16:54:27 +0800 | [diff] [blame] | 478 | auto findMonitorType = |
| 479 | presenceConfig->second.find("MonitorType"); |
| 480 | bool polling = false; |
| 481 | if (findMonitorType != presenceConfig->second.end()) |
| 482 | { |
| 483 | auto mType = std::get<std::string>( |
| 484 | findMonitorType->second); |
| 485 | if (mType == "Polling") |
| 486 | { |
| 487 | polling = true; |
| 488 | } |
| 489 | else if (mType != "Event") |
| 490 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 491 | lg2::error( |
| 492 | "Unsupported GPIO MonitorType of '{TYPE}' for '{NAME}', " |
| 493 | "supported types: Polling, Event default", |
| 494 | "TYPE", mType, "NAME", sensorName); |
Xiaochao Ma | 4bbd02d | 2022-06-21 16:54:27 +0800 | [diff] [blame] | 495 | } |
| 496 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 497 | try |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 498 | { |
Xiaochao Ma | 4bbd02d | 2022-06-21 16:54:27 +0800 | [diff] [blame] | 499 | if (polling) |
| 500 | { |
| 501 | presenceGpio = |
| 502 | std::make_shared<PollingPresenceGpio>( |
| 503 | "Fan", sensorName, *pinName, |
| 504 | inverted, io); |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | presenceGpio = |
| 509 | std::make_shared<EventPresenceGpio>( |
| 510 | "Fan", sensorName, *pinName, |
| 511 | inverted, io); |
| 512 | } |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 513 | presenceGpios[*pinName] = presenceGpio; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 514 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 515 | catch (const std::system_error& e) |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 516 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 517 | lg2::error( |
| 518 | "Failed to create GPIO monitor object for " |
| 519 | "'{PIN_NAME}' / '{SENSOR_NAME}': '{ERROR}'", |
| 520 | "PIN_NAME", *pinName, "SENSOR_NAME", |
| 521 | sensorName, "ERROR", e); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 522 | } |
| 523 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 524 | } |
| 525 | else |
| 526 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 527 | lg2::error( |
| 528 | "Malformed Presence pinName for sensor '{NAME}'", |
| 529 | "NAME", sensorName); |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | } |
| 533 | std::optional<RedundancySensor>* redundancy = nullptr; |
| 534 | if (fanType == FanTypes::aspeed) |
| 535 | { |
| 536 | redundancy = &systemRedundancy; |
| 537 | } |
| 538 | |
| 539 | PowerState powerState = getPowerState(baseConfiguration->second); |
| 540 | |
| 541 | constexpr double defaultMaxReading = 25000; |
| 542 | constexpr double defaultMinReading = 0; |
| 543 | std::pair<double, double> limits = |
| 544 | std::make_pair(defaultMinReading, defaultMaxReading); |
| 545 | |
| 546 | auto connector = |
| 547 | sensorData->find(cfgIntf + std::string(".Connector")); |
| 548 | |
| 549 | std::optional<std::string> led; |
| 550 | std::string pwmName; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 551 | std::filesystem::path pwmPath; |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 552 | |
| 553 | // The Mutable parameter is optional, defaulting to false |
| 554 | bool isValueMutable = false; |
| 555 | if (connector != sensorData->end()) |
| 556 | { |
| 557 | auto findPwm = connector->second.find("Pwm"); |
| 558 | if (findPwm != connector->second.end()) |
| 559 | { |
| 560 | size_t pwm = std::visit(VariantToUnsignedIntVisitor(), |
| 561 | findPwm->second); |
| 562 | if (!findPwmPath(directory, pwm, pwmPath)) |
| 563 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 564 | lg2::error( |
| 565 | "Connector for '{NAME}' no pwm channel found!", |
| 566 | "NAME", sensorName); |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 567 | continue; |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 568 | } |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 569 | |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 570 | std::filesystem::path pwmEnableFile = |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 571 | "pwm" + std::to_string(pwm + 1) + "_enable"; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 572 | std::filesystem::path enablePath = |
| 573 | pwmPath.parent_path() / pwmEnableFile; |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 574 | enablePwm(enablePath); |
| 575 | |
| 576 | /* use pwm name override if found in configuration else |
| 577 | * use default */ |
| 578 | auto findOverride = connector->second.find("PwmName"); |
| 579 | if (findOverride != connector->second.end()) |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 580 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 581 | pwmName = std::visit(VariantToStringVisitor(), |
| 582 | findOverride->second); |
| 583 | } |
| 584 | else |
| 585 | { |
| 586 | pwmName = "Pwm_" + std::to_string(pwm + 1); |
| 587 | } |
| 588 | |
| 589 | // Check PWM sensor mutability |
| 590 | auto findMutable = connector->second.find("Mutable"); |
| 591 | if (findMutable != connector->second.end()) |
| 592 | { |
| 593 | const auto* ptrMutable = |
| 594 | std::get_if<bool>(&(findMutable->second)); |
| 595 | if (ptrMutable != nullptr) |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 596 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 597 | isValueMutable = *ptrMutable; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 601 | else |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 602 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 603 | lg2::error("Connector for '{NAME}' missing pwm!", "NAME", |
| 604 | sensorName); |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | auto findLED = connector->second.find("LED"); |
| 608 | if (findLED != connector->second.end()) |
| 609 | { |
| 610 | const auto* ledName = |
| 611 | std::get_if<std::string>(&(findLED->second)); |
| 612 | if (ledName == nullptr) |
| 613 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 614 | lg2::error("Wrong format for LED of '{NAME}'", "NAME", |
| 615 | sensorName); |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 616 | } |
| 617 | else |
| 618 | { |
| 619 | led = *ledName; |
| 620 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 624 | findLimits(limits, baseConfiguration); |
| 625 | |
| 626 | enableFanInput(path); |
| 627 | |
| 628 | auto& tachSensor = tachSensors[sensorName]; |
| 629 | tachSensor = nullptr; |
| 630 | tachSensor = std::make_shared<TachSensor>( |
| 631 | path.string(), baseType, objectServer, dbusConnection, |
| 632 | presenceGpio, redundancy, io, sensorName, |
| 633 | std::move(sensorThresholds), *interfacePath, limits, powerState, |
| 634 | led); |
| 635 | tachSensor->setupRead(); |
| 636 | |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 637 | if (!pwmPath.empty() && std::filesystem::exists(pwmPath) && |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 638 | (pwmSensors.count(pwmPath) == 0U)) |
| 639 | { |
| 640 | pwmSensors[pwmPath] = std::make_unique<PwmSensor>( |
| 641 | pwmName, pwmPath, dbusConnection, objectServer, |
| 642 | *interfacePath, "Fan", isValueMutable); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | createRedundancySensor(tachSensors, dbusConnection, objectServer); |
| 647 | }); |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 648 | getter->getConfiguration( |
James Feist | f27a55c | 2020-08-04 14:27:30 -0700 | [diff] [blame] | 649 | std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()}, |
| 650 | retries); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 651 | } |
| 652 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 653 | int main() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 654 | { |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 655 | boost::asio::io_context io; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 656 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 657 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 658 | |
| 659 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
Ed Tanous | d906725 | 2022-10-22 15:35:52 -0700 | [diff] [blame] | 660 | objectServer.add_manager("/xyz/openbmc_project/control"); |
Lei YU | c2f83fe | 2022-12-02 14:49:47 +0800 | [diff] [blame] | 661 | objectServer.add_manager("/xyz/openbmc_project/inventory"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 662 | systemBus->request_name("xyz.openbmc_project.FanSensor"); |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 663 | boost::container::flat_map<std::string, std::shared_ptr<TachSensor>> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 664 | tachSensors; |
| 665 | boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>> |
| 666 | pwmSensors; |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 667 | boost::container::flat_map<std::string, std::weak_ptr<PresenceGpio>> |
| 668 | presenceGpios; |
James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 669 | auto sensorsChanged = |
| 670 | std::make_shared<boost::container::flat_set<std::string>>(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 671 | |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 672 | boost::asio::post(io, [&]() { |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 673 | createSensors(io, objectServer, tachSensors, pwmSensors, presenceGpios, |
| 674 | systemBus, nullptr); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 675 | }); |
| 676 | |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 677 | boost::asio::steady_timer filterTimer(io); |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 678 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 679 | [&](sdbusplus::message_t& message) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 680 | if (message.is_method_error()) |
| 681 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 682 | lg2::error("callback method error"); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 683 | return; |
| 684 | } |
| 685 | sensorsChanged->insert(message.get_path()); |
| 686 | // this implicitly cancels the timer |
| 687 | filterTimer.expires_after(std::chrono::seconds(1)); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 688 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 689 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 690 | if (ec == boost::asio::error::operation_aborted) |
| 691 | { |
| 692 | /* we were canceled*/ |
| 693 | return; |
| 694 | } |
| 695 | if (ec) |
| 696 | { |
George Liu | e34e123 | 2025-02-20 11:27:48 +0800 | [diff] [blame^] | 697 | lg2::error("timer error"); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 698 | return; |
| 699 | } |
| 700 | createSensors(io, objectServer, tachSensors, pwmSensors, |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 701 | presenceGpios, systemBus, sensorsChanged, 5); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 702 | }); |
| 703 | }; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 704 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 705 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 706 | setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 707 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 708 | // redundancy sensor |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 709 | std::function<void(sdbusplus::message_t&)> redundancyHandler = |
| 710 | [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 711 | createRedundancySensor(tachSensors, systemBus, objectServer); |
| 712 | }; |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 713 | auto match = std::make_unique<sdbusplus::bus::match_t>( |
| 714 | static_cast<sdbusplus::bus_t&>(*systemBus), |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 715 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 716 | std::string(inventoryPath) + "',arg0namespace='" + |
| 717 | redundancyConfiguration + "'", |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 718 | std::move(redundancyHandler)); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 719 | matches.emplace_back(std::move(match)); |
| 720 | |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 721 | setupManufacturingModeMatch(*systemBus); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 722 | io.run(); |
Zhikui Ren | 8685b17 | 2021-06-29 15:16:52 -0700 | [diff] [blame] | 723 | return 0; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 724 | } |