xref: /openbmc/phosphor-hwmon/targets.hpp (revision 751043e962c3fcf01886a747dcca2a95a843039b)
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] instance - The target instance path
35  *  @param[in] info - The sdbusplus server connection and interfaces
36  *
37  *  @return A shared pointer to the target interface object
38  *          Will be empty if no interface was created
39  */
40 template <typename T>
41 std::shared_ptr<T> addTarget(const SensorSet::key_type& sensor,
42                              const std::string& instancePath,
43                              const std::string& devPath,
44                              ObjectInfo& info)
45 {
46     std::shared_ptr<T> target;
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
55     auto sysfsFullPath = sysfs::make_sysfs_path(instancePath,
56                                                 sensor.first,
57                                                 sensor.second,
58                                                 hwmon::entry::target);
59     if (fs::exists(sysfsFullPath))
60     {
61         target = std::make_shared<T>(instancePath,
62                                      devPath,
63                                      sensor.second,
64                                      bus,
65                                      objPath.c_str(),
66                                      deferSignals);
67         auto type = Targets<T>::type;
68         obj[type] = target;
69     }
70 
71     return target;
72 }
73