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