Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <experimental/filesystem> |
Matt Spinler | b4e6557 | 2017-10-17 10:09:18 -0500 | [diff] [blame^] | 4 | #include <phosphor-logging/elog-errors.hpp> |
| 5 | #include <phosphor-logging/log.hpp> |
| 6 | #include <xyz/openbmc_project/Sensor/Device/error.hpp> |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 7 | #include "fan_speed.hpp" |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 8 | |
| 9 | /** @class Targets |
| 10 | * @brief Target type traits. |
| 11 | * |
| 12 | * @tparam T - The target type. |
| 13 | */ |
| 14 | template <typename T> |
| 15 | struct Targets |
| 16 | { |
| 17 | static void fail() |
| 18 | { |
| 19 | static_assert(sizeof(Targets) == -1, "Unsupported Target type"); |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | /**@brief Targets specialization for fan speed. */ |
| 24 | template <> |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 25 | struct Targets<hwmon::FanSpeed> |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 26 | { |
| 27 | static constexpr InterfaceType type = InterfaceType::FAN_SPEED; |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | /** @brief addTarget |
| 31 | * |
| 32 | * Creates the target type interface |
| 33 | * |
| 34 | * @tparam T - The target type |
| 35 | * |
| 36 | * @param[in] sensor - A sensor type and name |
Matt Spinler | b4e6557 | 2017-10-17 10:09:18 -0500 | [diff] [blame^] | 37 | * @param[in] ioAccess - hwmon sysfs access object |
| 38 | * @param[in] devPath - The /sys/devices sysfs path |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 39 | * @param[in] info - The sdbusplus server connection and interfaces |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 40 | * |
| 41 | * @return A shared pointer to the target interface object |
| 42 | * Will be empty if no interface was created |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 43 | */ |
| 44 | template <typename T> |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 45 | std::shared_ptr<T> addTarget(const SensorSet::key_type& sensor, |
Matt Spinler | b4e6557 | 2017-10-17 10:09:18 -0500 | [diff] [blame^] | 46 | const sysfs::hwmonio::HwmonIO& ioAccess, |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 47 | const std::string& devPath, |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 48 | ObjectInfo& info) |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 49 | { |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 50 | std::shared_ptr<T> target; |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 51 | namespace fs = std::experimental::filesystem; |
| 52 | static constexpr bool deferSignals = true; |
| 53 | |
| 54 | auto& bus = *std::get<sdbusplus::bus::bus*>(info); |
| 55 | auto& obj = std::get<Object>(info); |
| 56 | auto& objPath = std::get<std::string>(info); |
| 57 | |
| 58 | // Check if target sysfs file exists |
Matt Spinler | b4e6557 | 2017-10-17 10:09:18 -0500 | [diff] [blame^] | 59 | auto sysfsFullPath = sysfs::make_sysfs_path(ioAccess.path(), |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 60 | sensor.first, |
| 61 | sensor.second, |
| 62 | hwmon::entry::target); |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 63 | if (fs::exists(sysfsFullPath)) |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 64 | { |
Matt Spinler | b4e6557 | 2017-10-17 10:09:18 -0500 | [diff] [blame^] | 65 | uint32_t targetSpeed = 0; |
| 66 | |
| 67 | try |
| 68 | { |
| 69 | targetSpeed = ioAccess.read( |
| 70 | sensor.first, |
| 71 | sensor.second, |
| 72 | hwmon::entry::target, |
| 73 | sysfs::hwmonio::retries, |
| 74 | sysfs::hwmonio::delay); |
| 75 | |
| 76 | } |
| 77 | catch (const std::system_error& e) |
| 78 | { |
| 79 | using namespace phosphor::logging; |
| 80 | using namespace sdbusplus::xyz::openbmc_project:: |
| 81 | Sensor::Device::Error; |
| 82 | using metadata = xyz::openbmc_project::Sensor:: |
| 83 | Device::ReadFailure; |
| 84 | |
| 85 | report<ReadFailure>( |
| 86 | metadata::CALLOUT_ERRNO(e.code().value()), |
| 87 | metadata::CALLOUT_DEVICE_PATH(devPath.c_str())); |
| 88 | |
| 89 | log<level::INFO>("Logging failing sysfs file", |
| 90 | phosphor::logging::entry( |
| 91 | "FILE=%s", sysfsFullPath.c_str())); |
| 92 | } |
| 93 | |
| 94 | target = std::make_shared<T>(ioAccess.path(), |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 95 | devPath, |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 96 | sensor.second, |
| 97 | bus, |
| 98 | objPath.c_str(), |
Matt Spinler | b4e6557 | 2017-10-17 10:09:18 -0500 | [diff] [blame^] | 99 | deferSignals, |
| 100 | targetSpeed); |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 101 | auto type = Targets<T>::type; |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 102 | obj[type] = target; |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 103 | } |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 104 | |
| 105 | return target; |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 106 | } |