timer: Remove in favor of sdeventplus/timer

Lets use the new standard event loop timer utility that comes with
sdeventplus instead of the one copied from other projects.

Change-Id: Ie7c8f462f2c0e2e05ce14da58b30a1f91acbf75d
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/Makefile.am b/Makefile.am
index 3d5f786..b1052e8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -28,7 +28,6 @@
 	env.cpp \
 	fan_speed.cpp \
 	fan_pwm.cpp \
-	timer.cpp \
 	hwmon.cpp \
 	hwmonio.cpp \
 	sensor.cpp \
diff --git a/mainloop.cpp b/mainloop.cpp
index 81faa9b..5cae0d3 100644
--- a/mainloop.cpp
+++ b/mainloop.cpp
@@ -252,7 +252,8 @@
     _bus(std::move(bus)),
     _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
     _devPath(devPath), _prefix(prefix), _root(root), state(), ioAccess(path),
-    event(sdeventplus::Event::get_default())
+    event(sdeventplus::Event::get_default()),
+    timer(event, std::bind(&MainLoop::read, this))
 {
     // Strip off any trailing slashes.
     std::string p = path;
@@ -276,7 +277,6 @@
 
 void MainLoop::shutdown() noexcept
 {
-    timer->state(phosphor::hwmon::timer::OFF);
     event.exit(0);
 }
 
@@ -287,9 +287,7 @@
     std::function<void()> callback(std::bind(&MainLoop::read, this));
     try
     {
-        timer = std::make_unique<phosphor::hwmon::Timer>(
-            event.get(), callback, std::chrono::microseconds(_interval),
-            phosphor::hwmon::timer::ON);
+        timer.restart(std::chrono::microseconds(_interval));
 
         // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
 
diff --git a/mainloop.hpp b/mainloop.hpp
index 87f6ed0..dbf86fb 100644
--- a/mainloop.hpp
+++ b/mainloop.hpp
@@ -5,14 +5,15 @@
 #include "sensor.hpp"
 #include "sensorset.hpp"
 #include "sysfs.hpp"
-#include "timer.hpp"
 #include "types.hpp"
 
 #include <any>
 #include <memory>
 #include <optional>
 #include <sdbusplus/server.hpp>
+#include <sdeventplus/clock.hpp>
 #include <sdeventplus/event.hpp>
+#include <sdeventplus/utility/timer.hpp>
 #include <string>
 #include <vector>
 
@@ -31,8 +32,8 @@
     MainLoop() = delete;
     MainLoop(const MainLoop&) = delete;
     MainLoop& operator=(const MainLoop&) = delete;
-    MainLoop(MainLoop&&) = default;
-    MainLoop& operator=(MainLoop&&) = default;
+    MainLoop(MainLoop&&) = delete;
+    MainLoop& operator=(MainLoop&&) = delete;
     ~MainLoop() = default;
 
     /** @brief Constructor
@@ -98,10 +99,10 @@
     uint64_t _interval = default_interval;
     /** @brief Hwmon sysfs access. */
     hwmonio::HwmonIO ioAccess;
-    /** @brief Timer */
-    std::unique_ptr<phosphor::hwmon::Timer> timer;
     /** @brief the Event Loop structure */
     sdeventplus::Event event;
+    /** @brief Read Timer */
+    sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
     /** @brief Store the specifications of sensor objects */
     std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>>
         sensorObjects;
diff --git a/timer.cpp b/timer.cpp
deleted file mode 100644
index 72c9f96..0000000
--- a/timer.cpp
+++ /dev/null
@@ -1,67 +0,0 @@
-#include "timer.hpp"
-
-#include <chrono>
-#include <cstring>
-#include <system_error>
-
-namespace phosphor
-{
-namespace hwmon
-{
-
-static std::chrono::microseconds getTime()
-{
-    using namespace std::chrono;
-    auto usec = steady_clock::now().time_since_epoch();
-    return duration_cast<microseconds>(usec);
-}
-
-Timer::Timer(sd_event* event, std::function<void()> callback,
-             std::chrono::microseconds usec, timer::Action action) :
-    event(event),
-    callback(callback), duration(usec), action(action)
-{
-    auto r = sd_event_add_time(event, &eventSource,
-                               CLOCK_MONOTONIC,            // Time base
-                               (getTime() + usec).count(), // When to fire
-                               0,              // Use default event accuracy
-                               timeoutHandler, // Callback handler on timeout
-                               this);          // User data
-    if (r < 0)
-    {
-        throw std::system_error(r, std::generic_category(), std::strerror(-r));
-    }
-}
-
-int Timer::timeoutHandler(sd_event_source* eventSource, uint64_t usec,
-                          void* userData)
-{
-    auto timer = static_cast<Timer*>(userData);
-
-    if (timer->getAction() == timer::ON)
-    {
-        auto r = sd_event_source_set_time(
-            eventSource, (getTime() + timer->getDuration()).count());
-        if (r < 0)
-        {
-            throw std::system_error(r, std::generic_category(),
-                                    std::strerror(-r));
-        }
-        r = sd_event_source_set_enabled(eventSource, timer::ON);
-        if (r < 0)
-        {
-            throw std::system_error(r, std::generic_category(),
-                                    std::strerror(-r));
-        }
-    }
-
-    if (timer->callback)
-    {
-        timer->callback();
-    }
-
-    return 0;
-}
-
-} // namespace hwmon
-} // namespace phosphor
diff --git a/timer.hpp b/timer.hpp
deleted file mode 100644
index 03f642d..0000000
--- a/timer.hpp
+++ /dev/null
@@ -1,106 +0,0 @@
-#pragma once
-
-#include <systemd/sd-event.h>
-
-#include <chrono>
-#include <functional>
-
-namespace phosphor
-{
-namespace hwmon
-{
-namespace timer
-{
-
-enum Action
-{
-    OFF = SD_EVENT_OFF,
-    ON = SD_EVENT_ON,
-    ONESHOT = SD_EVENT_ONESHOT
-};
-}
-
-/** @class Timer
- *  @brief Provides a timer source and a mechanism to callback when the timer
- *         expires.
- *
- *  The timer is armed upon construction. The constructor requires a timeout
- *  handler function, the timer expiry duration, and the timer state (one-shot,
- *  repetitive, disabled).
- *  It's possible to change the state of the timer after it's been armed via the
- *  state() API.
- */
-class Timer
-{
-  public:
-    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] callback - function callback for timer expiry
-     *  @param[in] usec - timer duration, in micro seconds
-     *  @param[in] action - controls the timer's lifetime
-     */
-    Timer(sd_event* event, std::function<void()> userCallback,
-          std::chrono::microseconds usec, timer::Action action);
-
-    ~Timer()
-    {
-        if (eventSource)
-        {
-            eventSource = sd_event_source_unref(eventSource);
-        }
-    }
-
-    /** @brief Timer expiry handler - invokes callback
-     *
-     *  @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 Enables / disables the timer
-     *  @param[in] action - controls the timer's lifetime
-     */
-    int state(timer::Action action)
-    {
-        return sd_event_source_set_enabled(eventSource, action);
-    }
-
-    timer::Action getAction() const
-    {
-        return action;
-    }
-
-    std::chrono::microseconds getDuration() const
-    {
-        return duration;
-    }
-
-  private:
-    /** @brief the sd_event structure */
-    sd_event* event = nullptr;
-
-    /** @brief Source of events */
-    sd_event_source* eventSource = nullptr;
-
-    /** @brief Optional function to call on timer expiration */
-    std::function<void()> callback{};
-
-    /** @brief Duration of the timer */
-    std::chrono::microseconds duration{};
-
-    /** @brief whether the timer is enabled/disabled/one-shot */
-    timer::Action action = timer::OFF;
-};
-
-} // namespace hwmon
-} // namespace phosphor