Add modes to phosphor-fan-control

phosphor-fan-control can behave differently based
on its command line arguments

--init:  Set fans to full speed, delay for a
         configurable amount of time to allow fans to ramp up,
         start the fan control ready target, and then exit.

--control:  Start the control algorithm.  Never exits.
            Will be started as part of the fan control ready target.

Change-Id: I453daf8cc05a5c85a19c098e1cca64cac2ad9520
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/control/manager.cpp b/control/manager.cpp
index 9359a6b..6c773a7 100644
--- a/control/manager.cpp
+++ b/control/manager.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 #include <algorithm>
+#include <phosphor-logging/log.hpp>
+#include <unistd.h>
 #include "manager.hpp"
 
 namespace phosphor
@@ -23,7 +25,16 @@
 namespace control
 {
 
-Manager::Manager(sdbusplus::bus::bus& bus) :
+using namespace phosphor::logging;
+
+constexpr auto SYSTEMD_SERVICE   = "org.freedesktop.systemd1";
+constexpr auto SYSTEMD_OBJ_PATH  = "/org/freedesktop/systemd1";
+constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
+constexpr auto FAN_CONTROL_READY_TARGET = "obmc-fan-control-ready@0.target";
+
+//Note: Future code will check 'mode' before starting control algorithm
+Manager::Manager(sdbusplus::bus::bus& bus,
+                 Mode mode) :
     _bus(bus)
 {
     //Create the appropriate Zone objects based on the
@@ -55,14 +66,45 @@
         }
     }
 
-    //Set to full since we don't know state of system yet.
+}
+
+
+void Manager::doInit()
+{
     for (auto& z: _zones)
     {
         z.second->setFullSpeed();
     }
+
+    auto delay = _powerOnDelay;
+    while (delay > 0)
+    {
+        delay = sleep(delay);
+    }
+
+    startFanControlReadyTarget();
 }
 
 
+void Manager::startFanControlReadyTarget()
+{
+    auto method = _bus.new_method_call(SYSTEMD_SERVICE,
+            SYSTEMD_OBJ_PATH,
+            SYSTEMD_INTERFACE,
+            "StartUnit");
+
+    method.append(FAN_CONTROL_READY_TARGET);
+    method.append("replace");
+
+    auto response = _bus.call(method);
+    if (response.is_method_error())
+    {
+        //TODO openbmc/openbmc#1555 create an elog
+        log<level::ERR>("Failed to start fan control ready target");
+        throw std::runtime_error("Failed to start fan control ready target");
+    }
+}
+
 }
 }
 }