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