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