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