sdevent: Remove in favor of sdeventplus

This change removes the one off sd-event wrapper from the project and
uses the openbmc wide sdeventplus library.

Change-Id: I8e042f875d06082e15606c7191a8073974f93990
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/configure.ac b/configure.ac
index 7209565..1ff3c65 100644
--- a/configure.ac
+++ b/configure.ac
@@ -18,6 +18,7 @@
 
 # Checks for libraries.
 PKG_CHECK_MODULES([SDBUSPLUS], [sdbusplus],, AC_MSG_ERROR(["Requires sdbusplus package."]))
+PKG_CHECK_MODULES([SDEVENTPLUS], [sdeventplus],, AC_MSG_ERROR(["Requires sdeventplus package."]))
 PKG_CHECK_MODULES([PHOSPHOR_DBUS_INTERFACES], [phosphor-dbus-interfaces],, AC_MSG_ERROR(["Requires phosphor-dbus-interfaces package."]))
 PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging],, AC_MSG_ERROR(["Requires phosphor-logging."]))
 PKG_CHECK_MODULES([PHOSPHOR_SNMP], [phosphor-snmp],, AC_MSG_ERROR(["Requires phosphor-snmp."]))
diff --git a/src/Makefile.am b/src/Makefile.am
index 55a2d06..e742d19 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -18,12 +18,14 @@
 
 phosphor_dbus_monitor_LDADD = \
 	$(SDBUSPLUS_LIBS) \
+	$(SDEVENTPLUS_LIBS) \
 	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
 	$(PHOSPHOR_LOGGING_LIBS) \
 	$(PHOSPHOR_SNMP_LIBS) \
 	-lstdc++fs
 phosphor_dbus_monitor_CXXFLAGS = \
 	$(SDBUSPLUS_CFLAGS) \
+	$(SDEVENTPLUS_CFLAGS) \
 	$(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
 	$(PHOSPHOR_LOGGING_CFLAGS) \
 	$(PHOSPHOR_SNMP_CFLAGS)
diff --git a/src/callback.hpp b/src/callback.hpp
index c79dcd0..0be7dc0 100644
--- a/src/callback.hpp
+++ b/src/callback.hpp
@@ -3,6 +3,9 @@
 #include "data_types.hpp"
 
 #include <chrono>
+#include <sdeventplus/clock.hpp>
+#include <sdeventplus/event.hpp>
+#include <sdeventplus/utility/timer.hpp>
 
 namespace phosphor
 {
@@ -202,17 +205,19 @@
  *  callback.
  *
  *  @tparam CallbackAccess - Provide access to callback group instances.
- *  @tparam TimerType - Delegated timer access methods.
  */
-template <typename CallbackAccess, typename TimerType>
+template <typename CallbackAccess>
 class DeferrableCallback : public ConditionalCallback<CallbackAccess>
 {
   public:
+    using TimerType =
+        sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
+
     DeferrableCallback() = delete;
     DeferrableCallback(const DeferrableCallback&) = delete;
-    DeferrableCallback(DeferrableCallback&&) = default;
+    DeferrableCallback(DeferrableCallback&&) = delete;
     DeferrableCallback& operator=(const DeferrableCallback&) = delete;
-    DeferrableCallback& operator=(DeferrableCallback&&) = default;
+    DeferrableCallback& operator=(DeferrableCallback&&) = delete;
     ~DeferrableCallback() = default;
 
     DeferrableCallback(const std::vector<size_t>& graphEntry, Conditional& cond,
@@ -227,28 +232,27 @@
         if (!timer)
         {
             timer = std::make_unique<TimerType>(
+                sdeventplus::Event::get_default(),
                 // **INDENT-OFF**
                 [ctx, this](auto& source) {
                     this->ConditionalCallback<CallbackAccess>::operator()(ctx);
                 });
             // **INDENT-ON**
-            timer->disable();
         }
 
         if (this->condition())
         {
-            if (!timer->enabled())
+            if (!timer->isEnabled())
             {
                 // This is the first time the condition evaluated.
                 // Start the countdown.
-                timer->update(timer->now() + delayInterval);
-                timer->enable();
+                timer->restartOnce(delayInterval);
             }
         }
         else
         {
             // The condition did not evaluate.  Stop the countdown.
-            timer->disable();
+            timer->setEnabled(false);
         }
     }
 
diff --git a/src/main.cpp b/src/main.cpp
index 0626081..29db39a 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -21,23 +21,13 @@
 
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server/manager.hpp>
+#include <sdeventplus/event.hpp>
 
 using namespace phosphor::dbus::monitoring;
 
-struct Loop
-{
-    /** @brief indefinitely process dbus traffic. */
-    static void run()
-    {
-        auto& bus = SDBusPlus::getBus();
-        auto& event = SDEvent::getEvent();
-        event.attach(bus);
-        event.loop();
-    }
-};
-
 int main(void)
 {
+    auto event = sdeventplus::Event::get_default();
     auto& bus = SDBusPlus::getBus();
 
     // Add sdbusplus Object Manager for the 'root' path of events.
@@ -65,7 +55,6 @@
         watch->callback(Context::START);
     }
 
-    Loop::run();
-
-    return -1;
+    bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
+    return event.loop();
 }
diff --git a/src/sdevent.hpp b/src/sdevent.hpp
deleted file mode 100644
index 4fed159..0000000
--- a/src/sdevent.hpp
+++ /dev/null
@@ -1,87 +0,0 @@
-#pragma once
-
-#include "sdevent/event.hpp"
-#include "sdevent/timer.hpp"
-
-struct Loop;
-
-namespace phosphor
-{
-namespace dbus
-{
-namespace monitoring
-{
-
-/** @class SDEvent
- *  @brief SDEventType access delegate implementation for sdevent.
- */
-class SDEvent
-{
-  protected:
-    /** @brief Share a single event loop amongst users. */
-    static auto& getEvent()
-    {
-        static auto event = sdevent::event::newDefault();
-        return event;
-    }
-
-  public:
-    /** @brief Wrapper for sd_event_now. */
-    static auto now()
-    {
-        return getEvent().now();
-    }
-
-    friend Loop;
-};
-
-/** @class SDEventTimer
- *  @brief TimerType access delegate implementation for sdevent.
- */
-class SDEventTimer : public SDEvent
-{
-  public:
-    SDEventTimer() = delete;
-    SDEventTimer(const SDEventTimer&) = default;
-    SDEventTimer(SDEventTimer&&) = default;
-    SDEventTimer& operator=(const SDEventTimer&) = default;
-    SDEventTimer& operator=(SDEventTimer&&) = default;
-    ~SDEventTimer() = default;
-
-    explicit SDEventTimer(
-        const sdevent::event::timer::Timer::Callback& callback) :
-        timer(getEvent(), callback)
-    {
-    }
-
-    /** @brief Update a timer expiration. */
-    void update(const std::chrono::steady_clock::time_point& expires)
-    {
-        timer.setTime(expires);
-    }
-
-    /** @brief Query timer state. */
-    auto enabled()
-    {
-        return timer.enabled() != SD_EVENT_OFF;
-    }
-
-    /** @brief Enable a timer. */
-    void enable()
-    {
-        timer.enable(SD_EVENT_ONESHOT);
-    }
-
-    /** @brief Disable a timer. */
-    void disable()
-    {
-        timer.enable(SD_EVENT_OFF);
-    }
-
-  private:
-    sdevent::event::timer::Timer timer;
-};
-
-} // namespace monitoring
-} // namespace dbus
-} // namespace phosphor
diff --git a/src/sdevent/event.hpp b/src/sdevent/event.hpp
deleted file mode 100644
index 9ef5791..0000000
--- a/src/sdevent/event.hpp
+++ /dev/null
@@ -1,138 +0,0 @@
-#pragma once
-
-#include <systemd/sd-event.h>
-
-#include <chrono>
-#include <memory>
-#include <sdbusplus/bus.hpp>
-
-// TODO: openbmc/openbmc#1720 - add error handling for sd_event API failures
-
-namespace sdevent
-{
-namespace event
-{
-namespace timer
-{
-class Timer;
-} // namespace timer
-} // namespace event
-namespace event
-{
-
-using EventPtr = sd_event*;
-class Event;
-
-/** @brief Get an instance of the 'default' event. */
-Event newDefault();
-
-namespace details
-{
-
-/** @brief unique_ptr functor to release an event reference. */
-struct EventDeleter
-{
-    void operator()(sd_event* ptr) const
-    {
-        deleter(ptr);
-    }
-
-    decltype(&sd_event_unref) deleter = sd_event_unref;
-};
-
-/* @brief Alias 'event' to a unique_ptr type for auto-release. */
-using Event = std::unique_ptr<sd_event, EventDeleter>;
-
-} // namespace details
-
-/** @class Event
- *  @brief Provides C++ bindings to the sd_event_* class functions.
- */
-class Event
-{
-  public:
-    /* Define all of the basic class operations:
-     *     Not allowed:
-     *         - Default constructor to avoid nullptrs.
-     *         - Copy operations due to internal unique_ptr.
-     *     Allowed:
-     *         - Move operations.
-     *         - Destructor.
-     */
-    Event() = delete;
-    Event(const Event&) = delete;
-    Event& operator=(const Event&) = delete;
-    Event(Event&&) = default;
-    Event& operator=(Event&&) = default;
-    ~Event() = default;
-
-    /** @brief Conversion constructor from 'EventPtr'.
-     *
-     *  Increments ref-count of the event-pointer and releases it when
-     *  done.
-     */
-    explicit Event(EventPtr e);
-
-    /** @brief Constructor for 'Event'.
-     *
-     *  Takes ownership of the event-pointer and releases it when done.
-     */
-    Event(EventPtr e, std::false_type);
-
-    /** @brief Release ownership of the stored event-pointer. */
-    EventPtr release()
-    {
-        return evt.release();
-    }
-
-    /** @brief Wait indefinitely for new event sources. */
-    void loop()
-    {
-        sd_event_loop(evt.get());
-    }
-
-    /** @brief Attach to a DBus loop. */
-    void attach(sdbusplus::bus::bus& bus)
-    {
-        bus.attach_event(evt.get(), SD_EVENT_PRIORITY_NORMAL);
-    }
-
-    /** @brief C++ wrapper for sd_event_now. */
-    auto now()
-    {
-        using namespace std::chrono;
-
-        uint64_t usec;
-        sd_event_now(evt.get(), CLOCK_MONOTONIC, &usec);
-        microseconds d(usec);
-        return steady_clock::time_point(d);
-    }
-
-    friend class timer::Timer;
-
-  private:
-    EventPtr get()
-    {
-        return evt.get();
-    }
-
-    details::Event evt;
-};
-
-inline Event::Event(EventPtr l) : evt(sd_event_ref(l))
-{
-}
-
-inline Event::Event(EventPtr l, std::false_type) : evt(l)
-{
-}
-
-inline Event newDefault()
-{
-    sd_event* e = nullptr;
-    sd_event_default(&e);
-    return Event(e, std::false_type());
-}
-
-} // namespace event
-} // namespace sdevent
diff --git a/src/sdevent/source.hpp b/src/sdevent/source.hpp
deleted file mode 100644
index 6b20c29..0000000
--- a/src/sdevent/source.hpp
+++ /dev/null
@@ -1,117 +0,0 @@
-#pragma once
-
-#include <systemd/sd-event.h>
-
-#include <chrono>
-#include <memory>
-
-// TODO: openbmc/openbmc#1720 - add error handling for sd_event API failures
-
-namespace sdevent
-{
-namespace source
-{
-
-using SourcePtr = sd_event_source*;
-
-namespace details
-{
-
-/** @brief unique_ptr functor to release a source reference. */
-struct SourceDeleter
-{
-    void operator()(sd_event_source* ptr) const
-    {
-        deleter(ptr);
-    }
-
-    decltype(&sd_event_source_unref) deleter = sd_event_source_unref;
-};
-
-/* @brief Alias 'source' to a unique_ptr type for auto-release. */
-using source = std::unique_ptr<sd_event_source, SourceDeleter>;
-
-} // namespace details
-
-/** @class Source
- *  @brief Provides C++ bindings to the sd_event_source* functions.
- */
-class Source
-{
-  public:
-    /* Define all of the basic class operations:
-     *     Not allowed:
-     *         - Default constructor to avoid nullptrs.
-     *         - Copy operations due to internal unique_ptr.
-     *     Allowed:
-     *         - Move operations.
-     *         - Destructor.
-     */
-    Source() = delete;
-    Source(const Source&) = delete;
-    Source& operator=(const Source&) = delete;
-    Source(Source&&) = default;
-    Source& operator=(Source&&) = default;
-    ~Source() = default;
-
-    /** @brief Conversion constructor from 'SourcePtr'.
-     *
-     *  Increments ref-count of the source-pointer and releases it
-     *  when done.
-     */
-    explicit Source(SourcePtr s) : src(sd_event_source_ref(s))
-    {
-    }
-
-    /** @brief Constructor for 'source'.
-     *
-     *  Takes ownership of the source-pointer and releases it when done.
-     */
-    Source(SourcePtr s, std::false_type) : src(s)
-    {
-    }
-
-    /** @brief Check if source contains a real pointer. (non-nullptr). */
-    explicit operator bool() const
-    {
-        return bool(src);
-    }
-
-    /** @brief Test whether or not the source can generate events. */
-    auto enabled()
-    {
-        int enabled;
-        sd_event_source_get_enabled(src.get(), &enabled);
-        return enabled;
-    }
-
-    /** @brief Allow the source to generate events. */
-    void enable(int enable)
-    {
-        sd_event_source_set_enabled(src.get(), enable);
-    }
-
-    /** @brief Set the expiration on a timer source. */
-    void setTime(const std::chrono::steady_clock::time_point& expires)
-    {
-        using namespace std::chrono;
-        auto epoch = expires.time_since_epoch();
-        auto time = duration_cast<microseconds>(epoch);
-        sd_event_source_set_time(src.get(), time.count());
-    }
-
-    /** @brief Get the expiration on a timer source. */
-    auto getTime()
-    {
-        using namespace std::chrono;
-        uint64_t usec;
-        sd_event_source_get_time(src.get(), &usec);
-        microseconds d(usec);
-        return steady_clock::time_point(d);
-    }
-
-  private:
-    details::source src;
-};
-} // namespace source
-} // namespace sdevent
diff --git a/src/sdevent/timer.hpp b/src/sdevent/timer.hpp
deleted file mode 100644
index 405da4e..0000000
--- a/src/sdevent/timer.hpp
+++ /dev/null
@@ -1,122 +0,0 @@
-#pragma once
-
-#include "sdevent/event.hpp"
-#include "sdevent/source.hpp"
-
-#include <systemd/sd-event.h>
-
-#include <chrono>
-#include <functional>
-#include <memory>
-
-// TODO: openbmc/openbmc#1720 - add error handling for sd_event API failures
-
-namespace sdevent
-{
-namespace event
-{
-namespace timer
-{
-
-/** @class Timer
- *  @brief Provides C++ bindings to the sd_event_source* timer functions.
- */
-class Timer
-{
-  public:
-    /* Define all of the basic class operations:
-     *     Not allowed:
-     *         - Default constructor to avoid nullptrs.
-     *         - Copy operations due to internal unique_ptr.
-     *     Allowed:
-     *         - Move operations.
-     *         - Destructor.
-     */
-    Timer() = delete;
-    Timer(const Timer&) = delete;
-    Timer& operator=(const Timer&) = delete;
-    Timer(Timer&&) = default;
-    Timer& operator=(Timer&&) = default;
-    ~Timer() = default;
-
-    using Callback = std::function<void(source::Source&)>;
-
-    /** @brief Register a timer callback.
-     *
-     *  @param[in] event - The event to register on.
-     *  @param[in] expires - The initial timer expiration time.
-     *  @param[in] callback - The callback method.
-     */
-    Timer(sdevent::event::Event& event,
-          const std::chrono::steady_clock::time_point& expires,
-          Callback callback) :
-        src(nullptr),
-        cb(std::make_unique<Callback>(std::move(callback)))
-    {
-        using namespace std::chrono;
-        auto epoch = expires.time_since_epoch();
-        auto time = duration_cast<microseconds>(epoch);
-        sd_event_source* source = nullptr;
-        sd_event_add_time(event.get(), &source, CLOCK_MONOTONIC, time.count(),
-                          0, callCallback, cb.get());
-        // **INDENT-OFF**
-        src = decltype(src){source, std::false_type()};
-        // **INDENT-ON**
-    }
-
-    /** @brief Register a disabled timer callback.
-     *
-     *  @param[in] event - The event to register on.
-     *  @param[in] callback - The callback method.
-     */
-    Timer(sdevent::event::Event& event, Callback callback) :
-        src(nullptr), cb(std::make_unique<Callback>(std::move(callback)))
-    {
-        sd_event_source* source = nullptr;
-        sd_event_add_time(event.get(), &source, CLOCK_MONOTONIC, ULLONG_MAX, 0,
-                          callCallback, cb.get());
-        // **INDENT-OFF**
-        src = decltype(src){source, std::false_type()};
-        // **INDENT-ON**
-    }
-
-    /** @brief Set the timer expiration time. */
-    void setTime(const std::chrono::steady_clock::time_point& expires)
-    {
-        src.setTime(expires);
-    }
-
-    /** @brief Get the timer expiration time. */
-    auto getTime()
-    {
-        return src.getTime();
-    }
-
-    /** @brief Set the timer source enable state. */
-    void enable(int enable)
-    {
-        src.enable(enable);
-    }
-
-    /** @brief Query timer state. */
-    auto enabled()
-    {
-        return src.enabled();
-    }
-
-  private:
-    source::Source src;
-    std::unique_ptr<Callback> cb = nullptr;
-
-    static int callCallback(sd_event_source* s, uint64_t usec, void* context)
-    {
-        source::Source source(s);
-        auto c = static_cast<Callback*>(context);
-        (*c)(source);
-
-        return 0;
-    }
-};
-} // namespace timer
-} // namespace event
-} // namespace sdevent
diff --git a/src/templates/conditional.mako.cpp b/src/templates/conditional.mako.cpp
index 229b3d6..d7d5015 100644
--- a/src/templates/conditional.mako.cpp
+++ b/src/templates/conditional.mako.cpp
@@ -1,5 +1,5 @@
 % if c.defer:
-std::make_unique<DeferrableCallback<ConfigPropertyCallbacks, SDEventTimer>>(
+std::make_unique<DeferrableCallback<ConfigPropertyCallbacks>>(
 ${indent(1)}ConfigPropertyCallbackGroups::get()[${c.graph}],
 ${indent(1)}*ConfigConditions::get()[${c.condition}],
 ${indent(1)}${c.defer})\
diff --git a/src/templates/generated.mako.hpp b/src/templates/generated.mako.hpp
index 8dbaa20..d0a5ead 100644
--- a/src/templates/generated.mako.hpp
+++ b/src/templates/generated.mako.hpp
@@ -17,7 +17,6 @@
 #include "sdbusplus.hpp"
 #include "event.hpp"
 #include "snmp_trap.hpp"
-#include "sdevent.hpp"
 
 using namespace std::string_literals;
 using namespace std::chrono_literals;
diff --git a/src/test/Makefile.am b/src/test/Makefile.am
index 08049fa..cbe9b82 100644
--- a/src/test/Makefile.am
+++ b/src/test/Makefile.am
@@ -187,6 +187,7 @@
 callbacktest_CXXFLAGS = \
 	$(gtest_cflags)
 	$(SDBUSPLUS_CFLAGS) \
+	$(SDEVENTPLUS_CFLAGS) \
 	$(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
 	$(PHOSPHOR_LOGGING_CFLAGS)
 callbacktest_LDFLAGS = \
@@ -194,6 +195,7 @@
 callbacktest_LDADD = \
 	${gtest_ldadd} \
 	${SDBUSPLUS_LIBS} \
+	${SDEVENTPLUS_LIBS} \
 	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
 	$(PHOSPHOR_LOGGING_LIBS) \
 	$(builddir)/../journal.o \