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