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