Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <experimental/filesystem> |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 4 | #include "fan_speed.hpp" |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 5 | |
| 6 | /** @class Targets |
| 7 | * @brief Target type traits. |
| 8 | * |
| 9 | * @tparam T - The target type. |
| 10 | */ |
| 11 | template <typename T> |
| 12 | struct Targets |
| 13 | { |
| 14 | static void fail() |
| 15 | { |
| 16 | static_assert(sizeof(Targets) == -1, "Unsupported Target type"); |
| 17 | } |
| 18 | }; |
| 19 | |
| 20 | /**@brief Targets specialization for fan speed. */ |
| 21 | template <> |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 22 | struct Targets<hwmon::FanSpeed> |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 23 | { |
| 24 | static constexpr InterfaceType type = InterfaceType::FAN_SPEED; |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | /** @brief addTarget |
| 28 | * |
| 29 | * Creates the target type interface |
| 30 | * |
| 31 | * @tparam T - The target type |
| 32 | * |
| 33 | * @param[in] sensor - A sensor type and name |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 34 | * @param[in] instance - The target instance path |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 35 | * @param[in] info - The sdbusplus server connection and interfaces |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 36 | * |
| 37 | * @return A shared pointer to the target interface object |
| 38 | * Will be empty if no interface was created |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 39 | */ |
| 40 | template <typename T> |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 41 | std::shared_ptr<T> addTarget(const SensorSet::key_type& sensor, |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 42 | const std::string& instancePath, |
| 43 | const std::string& devPath, |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 44 | ObjectInfo& info) |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 45 | { |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 46 | std::shared_ptr<T> target; |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 47 | namespace fs = std::experimental::filesystem; |
| 48 | static constexpr bool deferSignals = true; |
| 49 | |
| 50 | auto& bus = *std::get<sdbusplus::bus::bus*>(info); |
| 51 | auto& obj = std::get<Object>(info); |
| 52 | auto& objPath = std::get<std::string>(info); |
| 53 | |
| 54 | // Check if target sysfs file exists |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 55 | auto sysfsFullPath = sysfs::make_sysfs_path(instancePath, |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 56 | sensor.first, |
| 57 | sensor.second, |
| 58 | hwmon::entry::target); |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 59 | if (fs::exists(sysfsFullPath)) |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 60 | { |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 61 | target = std::make_shared<T>(instancePath, |
| 62 | devPath, |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 63 | sensor.second, |
| 64 | bus, |
| 65 | objPath.c_str(), |
| 66 | deferSignals); |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 67 | auto type = Targets<T>::type; |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 68 | obj[type] = target; |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 69 | } |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 70 | |
| 71 | return target; |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 72 | } |