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