xref: /openbmc/phosphor-hwmon/targets.hpp (revision 1e6324fa4fc233c3f2913b3d5278aa06e45ff42f)
1 #pragma once
2 
3 #include <experimental/filesystem>
4 #include "fan_speed.hpp"
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 <>
22 struct Targets<hwmon::FanSpeed>
23 {
24     static constexpr InterfaceType type = InterfaceType::FAN_SPEED;
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
34  *  @param[in] hwmonRoot - The root hwmon path
35  *  @param[in] instance - The target instance name
36  *  @param[in] info - The sdbusplus server connection and interfaces
37  *
38  *  @return A shared pointer to the target interface object
39  *          Will be empty if no interface was created
40  */
41 template <typename T>
42 std::shared_ptr<T> addTarget(const SensorSet::key_type& sensor,
43                              const std::string& hwmonRoot,
44                              const std::string& instance,
45                              ObjectInfo& info)
46 {
47     std::shared_ptr<T> target;
48     namespace fs = std::experimental::filesystem;
49     static constexpr bool deferSignals = true;
50 
51     auto& bus = *std::get<sdbusplus::bus::bus*>(info);
52     auto& obj = std::get<Object>(info);
53     auto& objPath = std::get<std::string>(info);
54 
55     // Check if target sysfs file exists
56     auto targetPath = hwmonRoot + '/' + instance;
57     auto sysfsFullPath = sysfs::make_sysfs_path(targetPath,
58                                                 sensor.first,
59                                                 sensor.second,
60                                                 hwmon::entry::target);
61     if (fs::exists(sysfsFullPath))
62     {
63         target = std::make_shared<T>(hwmonRoot,
64                                      instance,
65                                      sensor.second,
66                                      bus,
67                                      objPath.c_str(),
68                                      deferSignals);
69         auto type = Targets<T>::type;
70         obj[type] = target;
71     }
72 
73     return target;
74 }
75