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