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