blob: 863eae5d9481cc7a95fdbd72678abe5daf242cc7 [file] [log] [blame]
Matthew Barthbf7b7b12017-03-07 15:46:59 -06001#pragma once
2
3#include <experimental/filesystem>
Matthew Barth048ac872017-03-09 14:36:08 -06004#include "fan_speed.hpp"
Matthew Barthbf7b7b12017-03-07 15:46:59 -06005
6/** @class Targets
7 * @brief Target type traits.
8 *
9 * @tparam T - The target type.
10 */
11template <typename T>
12struct 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. */
21template <>
Matthew Barth048ac872017-03-09 14:36:08 -060022struct Targets<hwmon::FanSpeed>
Matthew Barthbf7b7b12017-03-07 15:46:59 -060023{
24 static constexpr InterfaceType type = InterfaceType::FAN_SPEED;
Matthew Barthbf7b7b12017-03-07 15:46:59 -060025};
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 Bishop751043e2017-08-29 11:13:46 -040034 * @param[in] instance - The target instance path
Matthew Barthbf7b7b12017-03-07 15:46:59 -060035 * @param[in] info - The sdbusplus server connection and interfaces
Matt Spinler0a8de642017-05-11 10:59:39 -050036 *
37 * @return A shared pointer to the target interface object
38 * Will be empty if no interface was created
Matthew Barthbf7b7b12017-03-07 15:46:59 -060039 */
40template <typename T>
Matt Spinler0a8de642017-05-11 10:59:39 -050041std::shared_ptr<T> addTarget(const SensorSet::key_type& sensor,
Brad Bishop751043e2017-08-29 11:13:46 -040042 const std::string& instancePath,
43 const std::string& devPath,
Matt Spinler0a8de642017-05-11 10:59:39 -050044 ObjectInfo& info)
Matthew Barthbf7b7b12017-03-07 15:46:59 -060045{
Matt Spinler0a8de642017-05-11 10:59:39 -050046 std::shared_ptr<T> target;
Matthew Barthbf7b7b12017-03-07 15:46:59 -060047 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 Bishop751043e2017-08-29 11:13:46 -040055 auto sysfsFullPath = sysfs::make_sysfs_path(instancePath,
Patrick Venture1e6324f2017-06-01 14:07:05 -070056 sensor.first,
57 sensor.second,
58 hwmon::entry::target);
Matthew Barth048ac872017-03-09 14:36:08 -060059 if (fs::exists(sysfsFullPath))
Matthew Barthbf7b7b12017-03-07 15:46:59 -060060 {
Brad Bishop751043e2017-08-29 11:13:46 -040061 target = std::make_shared<T>(instancePath,
62 devPath,
Matt Spinler0a8de642017-05-11 10:59:39 -050063 sensor.second,
64 bus,
65 objPath.c_str(),
66 deferSignals);
Matthew Barthbf7b7b12017-03-07 15:46:59 -060067 auto type = Targets<T>::type;
Matt Spinler0a8de642017-05-11 10:59:39 -050068 obj[type] = target;
Matthew Barthbf7b7b12017-03-07 15:46:59 -060069 }
Matt Spinler0a8de642017-05-11 10:59:39 -050070
71 return target;
Matthew Barthbf7b7b12017-03-07 15:46:59 -060072}