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> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 31 | #include <sdbusplus/asio/connection.hpp> |
| 32 | #include <sdbusplus/asio/object_server.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 33 | #include <sdbusplus/bus.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 34 | #include <sdbusplus/bus/match.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 35 | #include <sdbusplus/message.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 36 | |
| 37 | #include <array> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 38 | #include <chrono> |
| 39 | #include <cstddef> |
| 40 | #include <cstdint> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 41 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 42 | #include <fstream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 43 | #include <functional> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 44 | #include <ios> |
| 45 | #include <iostream> |
| 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 | { |
Chris Sides | 9a472e8 | 2023-04-03 15:13:37 -0500 | [diff] [blame] | 103 | std::cerr << "Error opening " << compatiblePath << "\n"; |
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 | { |
| 128 | std::cerr << "Error read/write " << filePath << "\n"; |
| 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 | { |
| 152 | std::cerr << "No PWMs are found!\n"; |
| 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 | { |
| 163 | std::cerr << "read_symlink() failed: " << ec.message() << " (" |
| 164 | << ec.value() << ")\n"; |
| 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 | { |
| 190 | std::cerr << "exists() failed: " << ec.message() << " (" |
| 191 | << ec.value() << ")\n"; |
| 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 | { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 236 | std::cerr << "Error calling entity manager \n"; |
| 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 | { |
| 249 | std::cerr << "Malformed redundancy record \n"; |
| 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 | { |
| 300 | std::cerr << "No fan sensors in system\n"; |
| 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 | { |
| 342 | std::cerr << baseConfiguration->first << " missing index\n"; |
| 343 | continue; |
| 344 | } |
| 345 | unsigned int configIndex = std::visit( |
| 346 | VariantToUnsignedIntVisitor(), findIndex->second); |
| 347 | if (configIndex != index) |
| 348 | { |
| 349 | continue; |
| 350 | } |
| 351 | if (fanType == FanTypes::aspeed || |
| 352 | fanType == FanTypes::nuvoton || fanType == FanTypes::hpe) |
| 353 | { |
| 354 | // there will be only 1 aspeed or nuvoton or hpe sensor |
| 355 | // object in sysfs, we found the fan |
| 356 | sensorData = &cfgData; |
| 357 | break; |
| 358 | } |
| 359 | if (fanType == FanTypes::i2c) |
| 360 | { |
| 361 | std::string deviceName = |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 362 | std::filesystem::read_symlink(directory / "device") |
| 363 | .filename(); |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 364 | |
| 365 | size_t bus = 0; |
| 366 | size_t addr = 0; |
| 367 | if (!getDeviceBusAddr(deviceName, bus, addr)) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 368 | { |
Akshit Shah | 03d333e | 2023-08-23 22:14:28 +0000 | [diff] [blame] | 369 | continue; |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 370 | } |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 371 | |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 372 | auto findBus = baseConfiguration->second.find("Bus"); |
| 373 | auto findAddress = |
| 374 | baseConfiguration->second.find("Address"); |
| 375 | if (findBus == baseConfiguration->second.end() || |
| 376 | findAddress == baseConfiguration->second.end()) |
| 377 | { |
| 378 | std::cerr << baseConfiguration->first |
| 379 | << " missing bus or address\n"; |
| 380 | continue; |
| 381 | } |
| 382 | unsigned int configBus = std::visit( |
| 383 | VariantToUnsignedIntVisitor(), findBus->second); |
| 384 | unsigned int configAddress = std::visit( |
| 385 | VariantToUnsignedIntVisitor(), findAddress->second); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 386 | |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 387 | if (configBus == bus && configAddress == addr) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 388 | { |
Zev Weiss | 77636ec | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 389 | sensorData = &cfgData; |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 390 | break; |
| 391 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | if (sensorData == nullptr) |
| 395 | { |
| 396 | std::cerr << "failed to find match for " << path.string() |
| 397 | << "\n"; |
| 398 | continue; |
| 399 | } |
| 400 | |
| 401 | auto findSensorName = baseConfiguration->second.find("Name"); |
| 402 | |
| 403 | if (findSensorName == baseConfiguration->second.end()) |
| 404 | { |
| 405 | std::cerr << "could not determine configuration name for " |
| 406 | << path.string() << "\n"; |
| 407 | continue; |
| 408 | } |
| 409 | std::string sensorName = |
| 410 | std::get<std::string>(findSensorName->second); |
| 411 | |
| 412 | // on rescans, only update sensors we were signaled by |
| 413 | auto findSensor = tachSensors.find(sensorName); |
| 414 | if (!firstScan && findSensor != tachSensors.end()) |
| 415 | { |
| 416 | bool found = false; |
| 417 | for (auto it = sensorsChanged->begin(); |
| 418 | it != sensorsChanged->end(); it++) |
| 419 | { |
| 420 | if (it->ends_with(findSensor->second->name)) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 421 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 422 | sensorsChanged->erase(it); |
| 423 | findSensor->second = nullptr; |
| 424 | found = true; |
| 425 | break; |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 426 | } |
| 427 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 428 | if (!found) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 429 | { |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 430 | continue; |
| 431 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 432 | } |
| 433 | std::vector<thresholds::Threshold> sensorThresholds; |
| 434 | if (!parseThresholdsFromConfig(*sensorData, sensorThresholds)) |
| 435 | { |
| 436 | std::cerr << "error populating thresholds for " << sensorName |
| 437 | << "\n"; |
| 438 | } |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 439 | |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 440 | auto presenceConfig = |
| 441 | sensorData->find(cfgIntf + std::string(".Presence")); |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 442 | |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 443 | std::shared_ptr<PresenceGpio> presenceGpio(nullptr); |
| 444 | |
| 445 | // presence sensors are optional |
| 446 | if (presenceConfig != sensorData->end()) |
| 447 | { |
| 448 | auto findPolarity = presenceConfig->second.find("Polarity"); |
| 449 | auto findPinName = presenceConfig->second.find("PinName"); |
| 450 | |
| 451 | if (findPinName == presenceConfig->second.end() || |
| 452 | findPolarity == presenceConfig->second.end()) |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 453 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 454 | std::cerr << "Malformed Presence Configuration\n"; |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 455 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 456 | else |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 457 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 458 | bool inverted = |
| 459 | std::get<std::string>(findPolarity->second) == "Low"; |
| 460 | const auto* pinName = |
| 461 | std::get_if<std::string>(&findPinName->second); |
| 462 | |
| 463 | if (pinName != nullptr) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 464 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 465 | auto findPresenceGpio = presenceGpios.find(*pinName); |
| 466 | if (findPresenceGpio != presenceGpios.end()) |
Patrick Rudolph | 8b34f2c | 2024-07-16 11:38:45 +0200 | [diff] [blame] | 467 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 468 | auto p = findPresenceGpio->second.lock(); |
| 469 | if (p) |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 470 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 471 | presenceGpio = 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 | } |
| 474 | if (!presenceGpio) |
| 475 | { |
Xiaochao Ma | 4bbd02d | 2022-06-21 16:54:27 +0800 | [diff] [blame] | 476 | auto findMonitorType = |
| 477 | presenceConfig->second.find("MonitorType"); |
| 478 | bool polling = false; |
| 479 | if (findMonitorType != presenceConfig->second.end()) |
| 480 | { |
| 481 | auto mType = std::get<std::string>( |
| 482 | findMonitorType->second); |
| 483 | if (mType == "Polling") |
| 484 | { |
| 485 | polling = true; |
| 486 | } |
| 487 | else if (mType != "Event") |
| 488 | { |
| 489 | std::cerr |
| 490 | << "Unsupported GPIO MonitorType of " |
| 491 | << mType << " for " << sensorName |
| 492 | << " (supported types: Polling, Event (default))\n"; |
| 493 | } |
| 494 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 495 | try |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 496 | { |
Xiaochao Ma | 4bbd02d | 2022-06-21 16:54:27 +0800 | [diff] [blame] | 497 | if (polling) |
| 498 | { |
| 499 | presenceGpio = |
| 500 | std::make_shared<PollingPresenceGpio>( |
| 501 | "Fan", sensorName, *pinName, |
| 502 | inverted, io); |
| 503 | } |
| 504 | else |
| 505 | { |
| 506 | presenceGpio = |
| 507 | std::make_shared<EventPresenceGpio>( |
| 508 | "Fan", sensorName, *pinName, |
| 509 | inverted, io); |
| 510 | } |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 511 | presenceGpios[*pinName] = presenceGpio; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 512 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 513 | catch (const std::system_error& e) |
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 | std::cerr |
| 516 | << "Failed to create GPIO monitor object for " |
| 517 | << *pinName << " / " << sensorName << ": " |
| 518 | << e.what() << "\n"; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 519 | } |
| 520 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 521 | } |
| 522 | else |
| 523 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 524 | std::cerr << "Malformed Presence pinName for sensor " |
| 525 | << sensorName << " \n"; |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | std::optional<RedundancySensor>* redundancy = nullptr; |
| 530 | if (fanType == FanTypes::aspeed) |
| 531 | { |
| 532 | redundancy = &systemRedundancy; |
| 533 | } |
| 534 | |
| 535 | PowerState powerState = getPowerState(baseConfiguration->second); |
| 536 | |
| 537 | constexpr double defaultMaxReading = 25000; |
| 538 | constexpr double defaultMinReading = 0; |
| 539 | std::pair<double, double> limits = |
| 540 | std::make_pair(defaultMinReading, defaultMaxReading); |
| 541 | |
| 542 | auto connector = |
| 543 | sensorData->find(cfgIntf + std::string(".Connector")); |
| 544 | |
| 545 | std::optional<std::string> led; |
| 546 | std::string pwmName; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 547 | std::filesystem::path pwmPath; |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 548 | |
| 549 | // The Mutable parameter is optional, defaulting to false |
| 550 | bool isValueMutable = false; |
| 551 | if (connector != sensorData->end()) |
| 552 | { |
| 553 | auto findPwm = connector->second.find("Pwm"); |
| 554 | if (findPwm != connector->second.end()) |
| 555 | { |
| 556 | size_t pwm = std::visit(VariantToUnsignedIntVisitor(), |
| 557 | findPwm->second); |
| 558 | if (!findPwmPath(directory, pwm, pwmPath)) |
| 559 | { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 560 | std::cerr << "Connector for " << sensorName |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 561 | << " no pwm channel found!\n"; |
| 562 | continue; |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 563 | } |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 564 | |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 565 | std::filesystem::path pwmEnableFile = |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 566 | "pwm" + std::to_string(pwm + 1) + "_enable"; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 567 | std::filesystem::path enablePath = |
| 568 | pwmPath.parent_path() / pwmEnableFile; |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 569 | enablePwm(enablePath); |
| 570 | |
| 571 | /* use pwm name override if found in configuration else |
| 572 | * use default */ |
| 573 | auto findOverride = connector->second.find("PwmName"); |
| 574 | if (findOverride != connector->second.end()) |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 575 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 576 | pwmName = std::visit(VariantToStringVisitor(), |
| 577 | findOverride->second); |
| 578 | } |
| 579 | else |
| 580 | { |
| 581 | pwmName = "Pwm_" + std::to_string(pwm + 1); |
| 582 | } |
| 583 | |
| 584 | // Check PWM sensor mutability |
| 585 | auto findMutable = connector->second.find("Mutable"); |
| 586 | if (findMutable != connector->second.end()) |
| 587 | { |
| 588 | const auto* ptrMutable = |
| 589 | std::get_if<bool>(&(findMutable->second)); |
| 590 | if (ptrMutable != nullptr) |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 591 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 592 | isValueMutable = *ptrMutable; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | } |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 596 | else |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 597 | { |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 598 | std::cerr |
| 599 | << "Connector for " << sensorName << " missing pwm!\n"; |
| 600 | } |
| 601 | |
| 602 | auto findLED = connector->second.find("LED"); |
| 603 | if (findLED != connector->second.end()) |
| 604 | { |
| 605 | const auto* ledName = |
| 606 | std::get_if<std::string>(&(findLED->second)); |
| 607 | if (ledName == nullptr) |
| 608 | { |
| 609 | std::cerr |
| 610 | << "Wrong format for LED of " << sensorName << "\n"; |
| 611 | } |
| 612 | else |
| 613 | { |
| 614 | led = *ledName; |
| 615 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 619 | findLimits(limits, baseConfiguration); |
| 620 | |
| 621 | enableFanInput(path); |
| 622 | |
| 623 | auto& tachSensor = tachSensors[sensorName]; |
| 624 | tachSensor = nullptr; |
| 625 | tachSensor = std::make_shared<TachSensor>( |
| 626 | path.string(), baseType, objectServer, dbusConnection, |
| 627 | presenceGpio, redundancy, io, sensorName, |
| 628 | std::move(sensorThresholds), *interfacePath, limits, powerState, |
| 629 | led); |
| 630 | tachSensor->setupRead(); |
| 631 | |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 632 | if (!pwmPath.empty() && std::filesystem::exists(pwmPath) && |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 633 | (pwmSensors.count(pwmPath) == 0U)) |
| 634 | { |
| 635 | pwmSensors[pwmPath] = std::make_unique<PwmSensor>( |
| 636 | pwmName, pwmPath, dbusConnection, objectServer, |
| 637 | *interfacePath, "Fan", isValueMutable); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | createRedundancySensor(tachSensors, dbusConnection, objectServer); |
| 642 | }); |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 643 | getter->getConfiguration( |
James Feist | f27a55c | 2020-08-04 14:27:30 -0700 | [diff] [blame] | 644 | std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()}, |
| 645 | retries); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 646 | } |
| 647 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 648 | int main() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 649 | { |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 650 | boost::asio::io_context io; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 651 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 652 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 653 | |
| 654 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
Ed Tanous | d906725 | 2022-10-22 15:35:52 -0700 | [diff] [blame] | 655 | objectServer.add_manager("/xyz/openbmc_project/control"); |
Lei YU | c2f83fe | 2022-12-02 14:49:47 +0800 | [diff] [blame] | 656 | objectServer.add_manager("/xyz/openbmc_project/inventory"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 657 | systemBus->request_name("xyz.openbmc_project.FanSensor"); |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 658 | boost::container::flat_map<std::string, std::shared_ptr<TachSensor>> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 659 | tachSensors; |
| 660 | boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>> |
| 661 | pwmSensors; |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 662 | boost::container::flat_map<std::string, std::weak_ptr<PresenceGpio>> |
| 663 | presenceGpios; |
James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 664 | auto sensorsChanged = |
| 665 | std::make_shared<boost::container::flat_set<std::string>>(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 666 | |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 667 | boost::asio::post(io, [&]() { |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 668 | createSensors(io, objectServer, tachSensors, pwmSensors, presenceGpios, |
| 669 | systemBus, nullptr); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 670 | }); |
| 671 | |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 672 | boost::asio::steady_timer filterTimer(io); |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 673 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 674 | [&](sdbusplus::message_t& message) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 675 | if (message.is_method_error()) |
| 676 | { |
| 677 | std::cerr << "callback method error\n"; |
| 678 | return; |
| 679 | } |
| 680 | sensorsChanged->insert(message.get_path()); |
| 681 | // this implicitly cancels the timer |
| 682 | filterTimer.expires_after(std::chrono::seconds(1)); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 683 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 684 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 685 | if (ec == boost::asio::error::operation_aborted) |
| 686 | { |
| 687 | /* we were canceled*/ |
| 688 | return; |
| 689 | } |
| 690 | if (ec) |
| 691 | { |
| 692 | std::cerr << "timer error\n"; |
| 693 | return; |
| 694 | } |
| 695 | createSensors(io, objectServer, tachSensors, pwmSensors, |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 696 | presenceGpios, systemBus, sensorsChanged, 5); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 697 | }); |
| 698 | }; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 699 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 700 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 701 | setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 702 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 703 | // redundancy sensor |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 704 | std::function<void(sdbusplus::message_t&)> redundancyHandler = |
| 705 | [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 706 | createRedundancySensor(tachSensors, systemBus, objectServer); |
| 707 | }; |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 708 | auto match = std::make_unique<sdbusplus::bus::match_t>( |
| 709 | static_cast<sdbusplus::bus_t&>(*systemBus), |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 710 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 711 | std::string(inventoryPath) + "',arg0namespace='" + |
| 712 | redundancyConfiguration + "'", |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 713 | std::move(redundancyHandler)); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 714 | matches.emplace_back(std::move(match)); |
| 715 | |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 716 | setupManufacturingModeMatch(*systemBus); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 717 | io.run(); |
Zhikui Ren | 8685b17 | 2021-06-29 15:16:52 -0700 | [diff] [blame] | 718 | return 0; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 719 | } |