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