Create a FanSpeedObject interface

Create the FanSpeedObject target interface when the fanx_target sysfs
file exists. The "xyz.openbmc_project.Control.Target" interface is on
the /xyz/openbmc_project/sensors/fan_tac/fan[#] object paths.

Change-Id: Ib4a099cff17cacea501b474969f292516db212b5
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/hwmon.hpp b/hwmon.hpp
index 2202df0..6ae1885 100644
--- a/hwmon.hpp
+++ b/hwmon.hpp
@@ -8,9 +8,11 @@
 {
 static constexpr auto cinput = "input";
 static constexpr auto clabel = "label";
+static constexpr auto ctarget = "target";
 
 static const std::string input = cinput;
 static const std::string label = clabel;
+static const std::string target = ctarget;
 }
 
 namespace type
diff --git a/interface.hpp b/interface.hpp
index d5b0ca4..535d301 100644
--- a/interface.hpp
+++ b/interface.hpp
@@ -3,6 +3,7 @@
 #include "xyz/openbmc_project/Sensor/Value/server.hpp"
 #include "xyz/openbmc_project/Sensor/Threshold/Warning/server.hpp"
 #include "xyz/openbmc_project/Sensor/Threshold/Critical/server.hpp"
+#include "xyz/openbmc_project/Control/FanSpeed/server.hpp"
 #include <sdbusplus/server.hpp>
 
 template <typename... T>
@@ -16,12 +17,16 @@
 using CriticalInterface =
     sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical;
 using CriticalObject = ServerObject<CriticalInterface>;
+using FanSpeedInterface =
+    sdbusplus::xyz::openbmc_project::Control::server::FanSpeed;
+using FanSpeedObject = ServerObject<FanSpeedInterface>;
 
 enum class InterfaceType
 {
     VALUE,
     WARN,
     CRIT,
+    FAN_SPEED,
 };
 
 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
diff --git a/mainloop.cpp b/mainloop.cpp
index 399b17e..7af3096 100644
--- a/mainloop.cpp
+++ b/mainloop.cpp
@@ -24,6 +24,7 @@
 #include "mainloop.hpp"
 #include "env.hpp"
 #include "thresholds.hpp"
+#include "targets.hpp"
 
 // Initialization for Warning Objects
 decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
@@ -53,6 +54,12 @@
 decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
     &CriticalObject::criticalAlarmHigh;
 
+// Initialization for Target objects
+decltype(Targets<FanSpeedObject>::setTarget)
+    Targets<FanSpeedObject>::setTarget = &FanSpeedObject::target;
+decltype(Targets<FanSpeedObject>::getTarget)
+    Targets<FanSpeedObject>::getTarget = &FanSpeedObject::target;
+
 
 using namespace std::literals::chrono_literals;
 
@@ -238,6 +245,9 @@
         auto sensorValue = valueInterface->value();
         addThreshold<WarningObject>(i.first, sensorValue, info);
         addThreshold<CriticalObject>(i.first, sensorValue, info);
+        //TODO openbmc/openbmc#1347
+        //     Handle application restarts to set/refresh fan speed values
+        addTarget<FanSpeedObject>(i.first, _hwmonRoot, _instance, info);
 
         // All the interfaces have been created.  Go ahead
         // and emit InterfacesAdded.
diff --git a/targets.hpp b/targets.hpp
new file mode 100644
index 0000000..2b0feb3
--- /dev/null
+++ b/targets.hpp
@@ -0,0 +1,64 @@
+#pragma once
+
+#include <experimental/filesystem>
+
+/** @class Targets
+ *  @brief Target type traits.
+ *
+ *  @tparam T - The target type.
+ */
+template <typename T>
+struct Targets
+{
+    static void fail()
+    {
+        static_assert(sizeof(Targets) == -1, "Unsupported Target type");
+    }
+};
+
+/**@brief Targets specialization for fan speed. */
+template <>
+struct Targets<FanSpeedObject>
+{
+    static constexpr InterfaceType type = InterfaceType::FAN_SPEED;
+    static uint64_t (FanSpeedObject::*const setTarget)(uint64_t);
+    static uint64_t (FanSpeedObject::*const getTarget)() const;
+};
+
+/** @brief addTarget
+ *
+ *  Creates the target type interface
+ *
+ *  @tparam T - The target type
+ *
+ *  @param[in] sensor - A sensor type and name
+ *  @param[in] hwmonRoot - The root hwmon path
+ *  @param[in] instance - The target instance name
+ *  @param[in] info - The sdbusplus server connection and interfaces
+ */
+template <typename T>
+void addTarget(const SensorSet::key_type& sensor,
+               const std::string& hwmonRoot,
+               const std::string& instance,
+               ObjectInfo& info)
+{
+    namespace fs = std::experimental::filesystem;
+    static constexpr bool deferSignals = true;
+
+    auto& bus = *std::get<sdbusplus::bus::bus*>(info);
+    auto& obj = std::get<Object>(info);
+    auto& objPath = std::get<std::string>(info);
+
+    // Check if target sysfs file exists
+    auto targetPath = hwmonRoot + '/' + instance;
+    auto sysfsFile = make_sysfs_path(targetPath,
+                                     sensor.first,
+                                     sensor.second,
+                                     hwmon::entry::target);
+    if (fs::exists(sysfsFile))
+    {
+        auto iface = std::make_shared<T>(bus, objPath.c_str(), deferSignals);
+        auto type = Targets<T>::type;
+        obj[type] = iface;
+    }
+}