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> |
| 25 | #include <experimental/filesystem> |
| 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 | |
| 33 | namespace fs = std::experimental::filesystem; |
Yoo, Jae Hyun | 5093805 | 2018-10-17 18:19:02 -0700 | [diff] [blame] | 34 | namespace variant_ns = sdbusplus::message::variant_ns; |
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 | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 49 | std::shared_ptr<RedundancySensor> systemRedundancy = nullptr; |
| 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 | |
| 94 | // iterate through all found fan sensors, and try to match them with |
| 95 | // configuration |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 96 | for (const auto& path : paths) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 97 | { |
| 98 | std::smatch match; |
| 99 | std::string pathStr = path.string(); |
| 100 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 101 | std::regex_search(pathStr, match, inputRegex); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 102 | std::string indexStr = *(match.begin() + 1); |
| 103 | |
| 104 | auto directory = path.parent_path(); |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 105 | FanTypes fanType = getFanType(directory); |
| 106 | size_t bus = 0; |
| 107 | size_t address = 0; |
| 108 | if (fanType == FanTypes::i2c) |
| 109 | { |
| 110 | std::string link = |
| 111 | fs::read_symlink(directory / "device").filename(); |
| 112 | |
| 113 | size_t findDash = link.find("-"); |
| 114 | if (findDash == std::string::npos || link.size() <= findDash + 1) |
| 115 | { |
| 116 | std::cerr << "Error finding device from symlink"; |
| 117 | } |
| 118 | bus = std::stoi(link.substr(0, findDash)); |
| 119 | address = std::stoi(link.substr(findDash + 1), nullptr, 16); |
| 120 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 121 | // convert to 0 based |
| 122 | size_t index = std::stoul(indexStr) - 1; |
| 123 | |
| 124 | const char* baseType; |
| 125 | const SensorData* sensorData = nullptr; |
| 126 | const std::string* interfacePath = nullptr; |
| 127 | const std::pair<std::string, boost::container::flat_map< |
| 128 | std::string, BasicVariantType>>* |
| 129 | baseConfiguration = nullptr; |
| 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 | } |
Yoo, Jae Hyun | 5093805 | 2018-10-17 18:19:02 -0700 | [diff] [blame] | 155 | unsigned int configIndex = variant_ns::visit( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 156 | 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 | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 168 | else if (baseType == "xyz.openbmc_project.Configuration.I2CFan") |
| 169 | { |
| 170 | auto findBus = baseConfiguration->second.find("Bus"); |
| 171 | auto findAddress = baseConfiguration->second.find("Address"); |
| 172 | if (findBus == baseConfiguration->second.end() || |
| 173 | findAddress == baseConfiguration->second.end()) |
| 174 | { |
| 175 | std::cerr << baseConfiguration->first |
| 176 | << " missing bus or address\n"; |
| 177 | continue; |
| 178 | } |
| 179 | unsigned int configBus = variant_ns::visit( |
| 180 | VariantToUnsignedIntVisitor(), findBus->second); |
| 181 | unsigned int configAddress = variant_ns::visit( |
| 182 | VariantToUnsignedIntVisitor(), findAddress->second); |
| 183 | |
| 184 | if (configBus == bus && configAddress == configAddress) |
| 185 | { |
| 186 | sensorData = &(sensor.second); |
| 187 | break; |
| 188 | } |
| 189 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 190 | } |
| 191 | if (sensorData == nullptr) |
| 192 | { |
| 193 | std::cerr << "failed to find match for " << path.string() << "\n"; |
| 194 | continue; |
| 195 | } |
| 196 | |
| 197 | auto findSensorName = baseConfiguration->second.find("Name"); |
| 198 | if (findSensorName == baseConfiguration->second.end()) |
| 199 | { |
| 200 | std::cerr << "could not determine configuration name for " |
| 201 | << path.string() << "\n"; |
| 202 | continue; |
| 203 | } |
| 204 | std::string sensorName = |
| 205 | sdbusplus::message::variant_ns::get<std::string>( |
| 206 | findSensorName->second); |
| 207 | // on rescans, only update sensors we were signaled by |
| 208 | auto findSensor = tachSensors.find(sensorName); |
| 209 | if (!firstScan && findSensor != tachSensors.end()) |
| 210 | { |
| 211 | bool found = false; |
| 212 | for (auto it = sensorsChanged->begin(); it != sensorsChanged->end(); |
| 213 | it++) |
| 214 | { |
| 215 | if (boost::ends_with(*it, findSensor->second->name)) |
| 216 | { |
| 217 | sensorsChanged->erase(it); |
| 218 | findSensor->second = nullptr; |
| 219 | found = true; |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | if (!found) |
| 224 | { |
| 225 | continue; |
| 226 | } |
| 227 | } |
| 228 | std::vector<thresholds::Threshold> sensorThresholds; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 229 | if (!parseThresholdsFromConfig(*sensorData, sensorThresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 230 | { |
| 231 | std::cerr << "error populating thresholds for " << sensorName |
| 232 | << "\n"; |
| 233 | } |
| 234 | |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 235 | auto presenceConfig = |
| 236 | sensorData->find(baseType + std::string(".Presence")); |
| 237 | |
| 238 | std::unique_ptr<PresenceSensor> presenceSensor(nullptr); |
| 239 | |
| 240 | // presence sensors are optional |
| 241 | if (presenceConfig != sensorData->end()) |
| 242 | { |
| 243 | auto findIndex = presenceConfig->second.find("Index"); |
| 244 | auto findPolarity = presenceConfig->second.find("Polarity"); |
| 245 | |
| 246 | if (findIndex == presenceConfig->second.end() || |
| 247 | findPolarity == presenceConfig->second.end()) |
| 248 | { |
| 249 | std::cerr << "Malformed Presence Configuration\n"; |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | size_t index = variant_ns::get<uint64_t>(findIndex->second); |
| 254 | bool inverted = |
| 255 | variant_ns::get<std::string>(findPolarity->second) == "Low"; |
| 256 | presenceSensor = |
| 257 | std::make_unique<PresenceSensor>(index, inverted, io); |
| 258 | } |
| 259 | } |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 260 | std::shared_ptr<RedundancySensor> redundancy; |
| 261 | if (fanType == FanTypes::aspeed) |
| 262 | { |
| 263 | redundancy = systemRedundancy; |
| 264 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 265 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 266 | tachSensors[sensorName] = std::make_unique<TachSensor>( |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 267 | path.string(), objectServer, dbusConnection, |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 268 | std::move(presenceSensor), redundancy, io, sensorName, |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 269 | std::move(sensorThresholds), *interfacePath); |
| 270 | } |
| 271 | std::vector<fs::path> pwms; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 272 | if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwms)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 273 | { |
| 274 | std::cerr << "No pwm in system\n"; |
| 275 | return; |
| 276 | } |
| 277 | for (const fs::path& pwm : pwms) |
| 278 | { |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 279 | if (pwmSensors.find(pwm) != pwmSensors.end()) |
| 280 | { |
| 281 | continue; |
| 282 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 283 | // only add new elements |
| 284 | pwmSensors.insert(std::pair<std::string, std::unique_ptr<PwmSensor>>( |
| 285 | pwm.string(), |
| 286 | std::make_unique<PwmSensor>(pwm.string(), objectServer))); |
| 287 | } |
| 288 | } |
| 289 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 290 | void createRedundancySensor( |
| 291 | const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>& |
| 292 | sensors, |
| 293 | std::shared_ptr<sdbusplus::asio::connection> conn, |
| 294 | sdbusplus::asio::object_server& objectServer) |
| 295 | { |
| 296 | |
| 297 | conn->async_method_call( |
| 298 | [&objectServer, &sensors](boost::system::error_code& ec, |
| 299 | const ManagedObjectType managedObj) { |
| 300 | if (ec) |
| 301 | { |
| 302 | std::cerr << "Error calling entity manager \n"; |
| 303 | return; |
| 304 | } |
| 305 | for (const auto& pathPair : managedObj) |
| 306 | { |
| 307 | for (const auto& interfacePair : pathPair.second) |
| 308 | { |
| 309 | if (interfacePair.first == redundancyConfiguration) |
| 310 | { |
| 311 | // currently only support one |
| 312 | auto findCount = |
| 313 | interfacePair.second.find("AllowedFailures"); |
| 314 | if (findCount == interfacePair.second.end()) |
| 315 | { |
| 316 | std::cerr << "Malformed redundancy record \n"; |
| 317 | return; |
| 318 | } |
| 319 | std::vector<std::string> sensorList; |
| 320 | |
| 321 | for (const auto& sensor : sensors) |
| 322 | { |
| 323 | sensorList.push_back( |
| 324 | "/xyz/openbmc_project/sensors/fan_tach/" + |
| 325 | sensor.second->name); |
| 326 | } |
| 327 | systemRedundancy = std::make_unique<RedundancySensor>( |
| 328 | variant_ns::get<uint64_t>(findCount->second), |
| 329 | sensorList, objectServer); |
| 330 | |
| 331 | return; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | }, |
| 336 | "xyz.openbmc_project.EntityManager", "/", |
| 337 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 338 | } |
| 339 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 340 | int main(int argc, char** argv) |
| 341 | { |
| 342 | boost::asio::io_service io; |
| 343 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 344 | systemBus->request_name("xyz.openbmc_project.FanSensor"); |
| 345 | sdbusplus::asio::object_server objectServer(systemBus); |
| 346 | boost::container::flat_map<std::string, std::unique_ptr<TachSensor>> |
| 347 | tachSensors; |
| 348 | boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>> |
| 349 | pwmSensors; |
| 350 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; |
| 351 | std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged = |
| 352 | std::make_unique<boost::container::flat_set<std::string>>(); |
| 353 | |
| 354 | io.post([&]() { |
| 355 | createSensors(io, objectServer, tachSensors, pwmSensors, systemBus, |
| 356 | nullptr); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 357 | createRedundancySensor(tachSensors, systemBus, objectServer); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 358 | }); |
| 359 | |
| 360 | boost::asio::deadline_timer filterTimer(io); |
| 361 | std::function<void(sdbusplus::message::message&)> eventHandler = |
| 362 | [&](sdbusplus::message::message& message) { |
| 363 | if (message.is_method_error()) |
| 364 | { |
| 365 | std::cerr << "callback method error\n"; |
| 366 | return; |
| 367 | } |
| 368 | sensorsChanged->insert(message.get_path()); |
| 369 | // this implicitly cancels the timer |
| 370 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 371 | |
| 372 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 373 | if (ec == boost::asio::error::operation_aborted) |
| 374 | { |
| 375 | /* we were canceled*/ |
| 376 | return; |
| 377 | } |
| 378 | else if (ec) |
| 379 | { |
| 380 | std::cerr << "timer error\n"; |
| 381 | return; |
| 382 | } |
| 383 | createSensors(io, objectServer, tachSensors, pwmSensors, |
| 384 | systemBus, sensorsChanged); |
| 385 | }); |
| 386 | }; |
| 387 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 388 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 389 | { |
| 390 | auto match = std::make_unique<sdbusplus::bus::match::match>( |
| 391 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 392 | "type='signal',member='PropertiesChanged',path_namespace='" + |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 393 | std::string(inventoryPath) + "',arg0namespace='" + type + "'", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 394 | eventHandler); |
| 395 | matches.emplace_back(std::move(match)); |
| 396 | } |
| 397 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 398 | // redundancy sensor |
| 399 | std::function<void(sdbusplus::message::message&)> redundancyHandler = |
| 400 | [&tachSensors, &systemBus, |
| 401 | &objectServer](sdbusplus::message::message& message) { |
| 402 | createRedundancySensor(tachSensors, systemBus, objectServer); |
| 403 | }; |
| 404 | auto match = std::make_unique<sdbusplus::bus::match::match>( |
| 405 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 406 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 407 | std::string(inventoryPath) + "',arg0namespace='" + |
| 408 | redundancyConfiguration + "'", |
| 409 | redundancyHandler); |
| 410 | matches.emplace_back(std::move(match)); |
| 411 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 412 | io.run(); |
| 413 | } |