Add timeout support to host control

On timeout, send error signal for all commands within the queue

Change-Id: Ic995fd4b057bd83f121a3deec405a26e0991e9a2
Signed-off-by: Andrew Geissler <andrewg@us.ibm.com>
diff --git a/softoff/Makefile.am b/softoff/Makefile.am
index 35734d2..b7f4c92 100644
--- a/softoff/Makefile.am
+++ b/softoff/Makefile.am
@@ -2,9 +2,11 @@
 AM_CPPFLAGS = -I$(top_srcdir)
 sbin_PROGRAMS = phosphor-softpoweroff
 
+# Using ../ instead of $(top_srcdir) due to automake bug in version 1.15.
+#  https://debbugs.gnu.org/cgi/bugreport.cgi?bug=13928
 phosphor_softpoweroff_SOURCES = \
                     softoff.cpp \
-                    timer.cpp   \
+                    ../timer.cpp   \
                     mainapp.cpp \
                     xyz/openbmc_project/Ipmi/Internal/SoftPowerOff/server.cpp
 
diff --git a/softoff/test/Makefile.am b/softoff/test/Makefile.am
index 2c65568..afdb620 100644
--- a/softoff/test/Makefile.am
+++ b/softoff/test/Makefile.am
@@ -9,4 +9,4 @@
 utest_CXXFLAGS = $(PTHREAD_CFLAGS)
 utest_LDFLAGS = -lgtest_main -lgtest $(PTHREAD_LIBS) $(OESDK_TESTCASE_FLAGS) $(SYSTEMD_LIBS) ${SDBUSPLUS_LIBS}
 utest_SOURCES = utest.cpp
-utest_LDADD = $(top_builddir)/softoff/timer.o
+utest_LDADD = $(top_builddir)/timer.o
diff --git a/softoff/timer.cpp b/softoff/timer.cpp
deleted file mode 100644
index 4e8fd9a..0000000
--- a/softoff/timer.cpp
+++ /dev/null
@@ -1,108 +0,0 @@
-#include <chrono>
-#include <phosphor-logging/log.hpp>
-#include "timer.hpp"
-namespace phosphor
-{
-namespace ipmi
-{
-
-using namespace phosphor::logging;
-
-// Initializes the timer object
-void Timer::initialize()
-{
-    // This can not be called more than once.
-    if (eventSource)
-    {
-        throw std::runtime_error("Timer already initialized");
-    }
-
-    // Add infinite expiration time
-    auto r = sd_event_add_time(timeEvent, &eventSource,
-                               CLOCK_MONOTONIC, // Time base
-                               UINT64_MAX,      // Expire time - way long time
-                               0,               // Use default event accuracy
-                               timeoutHandler,  // Callback handler on timeout
-                               this);           // User data
-    if (r < 0)
-    {
-        log<level::ERR>("Failure to set initial expiration time value",
-                entry("ERROR=%s", strerror(-r)));
-
-        throw std::runtime_error("Timer initialization failed");
-    }
-
-    // Disable the timer for now
-    r = setTimer(SD_EVENT_OFF);
-    if (r < 0)
-    {
-        log<level::ERR>("Failure to disable timer",
-                entry("ERROR=%s", strerror(-r)));
-
-        throw std::runtime_error("Disabling the timer failed");
-    }
-    return;
-}
-
-/** @brief callback handler on timeout */
-int Timer::timeoutHandler(sd_event_source* eventSource,
-                          uint64_t usec, void* userData)
-{
-    auto timer = static_cast<Timer*>(userData);
-    timer->expired = true;
-
-    // Call optional user call back function if available
-    if(timer->userCallBack)
-    {
-        timer->userCallBack();
-    }
-
-    log<level::INFO>("Timer expired");
-    return 0;
-}
-
-// Gets the time from steady_clock
-std::chrono::microseconds Timer::getTime()
-{
-    using namespace std::chrono;
-    auto usec = steady_clock::now().time_since_epoch();
-    return duration_cast<microseconds>(usec);
-}
-
-// Enables or disables the timer
-int Timer::setTimer(int action)
-{
-    return sd_event_source_set_enabled(eventSource, action);
-}
-
-// Sets the time and arms the timer
-int Timer::startTimer(std::chrono::microseconds timeValue)
-{
-    // Disable the timer
-    setTimer(SD_EVENT_OFF);
-
-    // Get the current MONOTONIC time and add the delta
-    auto expireTime = getTime() + timeValue;
-
-    // Set the time
-    auto r = sd_event_source_set_time(eventSource, expireTime.count());
-    if (r < 0)
-    {
-        log<level::ERR>("Failure to set timer",
-                entry("ERROR=%s", strerror(-r)));
-        return r;
-    }
-
-    // A ONESHOT timer means that when the timer goes off,
-    // its moves to disabled state.
-    r = setTimer(SD_EVENT_ONESHOT);
-    if (r < 0)
-    {
-        log<level::ERR>("Failure to start timer",
-                entry("ERROR=%s", strerror(-r)));
-    }
-    return r;
-}
-
-} // namespace ipmi
-} // namespace phosphor
diff --git a/softoff/timer.hpp b/softoff/timer.hpp
deleted file mode 100644
index cbd3444..0000000
--- a/softoff/timer.hpp
+++ /dev/null
@@ -1,100 +0,0 @@
-#pragma once
-
-#include <functional>
-#include <systemd/sd-event.h>
-namespace phosphor
-{
-namespace ipmi
-{
-
-/** @class Timer
- *  @brief Manages starting watchdog timers and handling timeouts
- */
-class Timer
-{
-    public:
-        /** @brief Only need the default Timer */
-        Timer() = delete;
-        Timer(const Timer&) = delete;
-        Timer& operator=(const Timer&) = delete;
-        Timer(Timer&&) = delete;
-        Timer& operator=(Timer&&) = delete;
-
-        /** @brief Constructs timer object
-         *
-         *  @param[in] events - sd_event pointer
-         *  @param[in] funcCallBack - optional function callback for timer
-         *                            expirations
-         */
-        Timer(sd_event* events,
-              std::function<void()> userCallBack = nullptr)
-            : timeEvent(events), userCallBack(userCallBack)
-        {
-            // Initialize the timer
-            initialize();
-        }
-
-        ~Timer()
-        {
-            if (eventSource)
-            {
-                eventSource = sd_event_source_unref(eventSource);
-            }
-        }
-
-        inline auto isExpired() const
-        {
-            return expired;
-        }
-
-        /** @brief Starts the timer with specified expiration value.
-         *  input is an offset from the current steady_clock
-         */
-        int startTimer(std::chrono::microseconds usec);
-
-        /** @brief Enables / disables the timer */
-        int setTimer(int action);
-
-    private:
-        /** @brief the sd_event structure */
-        sd_event* timeEvent = nullptr;
-
-        /** @brief Source of events */
-        sd_event_source* eventSource = nullptr;
-
-        /** @brief Returns if the associated timer is expired
-         *
-         *  This is set to true when the timeoutHandler is called into
-         */
-        bool expired = false;
-
-        /** @brief Initializes the timer object with infinite
-         *         expiration time and sets up the callback handler
-         *
-         *  @return None.
-         *
-         *  @error std::runtime exception thrown
-         */
-        void initialize();
-
-        /** @brief Callback function when timer goes off
-         *
-         *  On getting the signal, initiate the hard power off request
-         *
-         *  @param[in] eventSource - Source of the event
-         *  @param[in] usec        - time in micro seconds
-         *  @param[in] userData    - User data pointer
-         *
-         */
-        static int timeoutHandler(sd_event_source* eventSource,
-                                  uint64_t usec, void* userData);
-
-        /** @brief Gets the current time from steady clock */
-        static std::chrono::microseconds getTime();
-
-        /** @brief Optional function to call on timer expiration */
-        std::function<void()> userCallBack;
-};
-
-} // namespace ipmi
-} // namespace phosphor