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