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