event: Remove in favor of sdeventplus

Use the new library provided to all openbmc projects instead of rolling
our own managed pointer.

Tested: Compiled

Change-Id: I4993d4340e0e0aa5898e73ef815baa81b0e8a2bc
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/Makefile.am b/Makefile.am
index 8fa771c..28b5178 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -6,11 +6,13 @@
 	-lstdc++fs \
 	$(PHOSPHOR_LOGGING_LIBS) \
 	$(SDBUSPLUS_LIBS) \
+	$(SDEVENTPLUS_LIBS) \
 	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
 	$(OPENPOWER_DBUS_INTERFACES_LIBS)
 libpower_la_CXXFLAGS = \
 	$(PHOSPHOR_LOGGING_CFLAGS) \
 	$(SDBUSPLUS_CFLAGS) \
+	$(SDEVENTPLUS_CFLAGS) \
 	$(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
 	$(OPENPOWER_DBUS_INTERFACES_CFLAGS)
 
diff --git a/configure.ac b/configure.ac
index 94be4ee..3a6cff8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -26,6 +26,8 @@
 
 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_LOGGING], [phosphor-logging],,\
     AC_MSG_ERROR(["Requires phosphor-logging package."]))
 PKG_CHECK_MODULES([PHOSPHOR_DBUS_INTERFACES], [phosphor-dbus-interfaces],,\
diff --git a/device_monitor.hpp b/device_monitor.hpp
index 3214afb..0813737 100644
--- a/device_monitor.hpp
+++ b/device_monitor.hpp
@@ -2,8 +2,8 @@
 #include <phosphor-logging/log.hpp>
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server.hpp>
+#include <sdeventplus/event.hpp>
 #include "device.hpp"
-#include "event.hpp"
 #include "timer.hpp"
 
 namespace witherspoon
@@ -39,7 +39,7 @@
          * @param[in] i - polling interval in ms
          */
         DeviceMonitor(std::unique_ptr<Device>&& d,
-                      event::Event& e,
+                      const sdeventplus::Event& e,
                       std::chrono::milliseconds i) :
             device(std::move(d)),
             event(e),
@@ -57,15 +57,7 @@
         virtual int run()
         {
             timer.start(interval, Timer::TimerType::repeating);
-
-            auto r = sd_event_loop(event.get());
-            if (r < 0)
-            {
-                log<level::ERR>("sd_event_loop() failed",
-                                entry("ERROR=%s", strerror(-r)));
-            }
-
-            return r;
+            return event.loop();
         }
 
     protected:
@@ -88,9 +80,9 @@
         std::unique_ptr<Device> device;
 
         /**
-         * The sd_event structure used by the timer
+         * The event loop used by the timer
          */
-        event::Event& event;
+        sdeventplus::Event event;
 
         /**
          * The polling interval in milliseconds
diff --git a/event.hpp b/event.hpp
deleted file mode 100644
index 6b2c95f..0000000
--- a/event.hpp
+++ /dev/null
@@ -1,41 +0,0 @@
-#pragma once
-
-#include <memory>
-#include <systemd/sd-event.h>
-
-namespace witherspoon
-{
-namespace power
-{
-namespace event
-{
-
-/**
- * Custom deleter for sd_event_source
- */
-struct EventSourceDeleter
-{
-    void operator()(sd_event_source* eventSource) const
-    {
-        sd_event_source_unref(eventSource);
-    }
-};
-
-using EventSource = std::unique_ptr<sd_event_source, EventSourceDeleter>;
-
-/**
- * Customer deleter for sd_event
- */
-struct EventDeleter
-{
-    void operator()(sd_event* event) const
-    {
-        sd_event_unref(event);
-    }
-};
-
-using Event = std::unique_ptr<sd_event, EventDeleter>;
-
-}
-}
-}
diff --git a/gpio.cpp b/gpio.cpp
index 076dee4..a2f1f3e 100644
--- a/gpio.cpp
+++ b/gpio.cpp
@@ -13,6 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#include <cassert>
 #include <fcntl.h>
 #include <phosphor-logging/elog.hpp>
 #include <phosphor-logging/elog-errors.hpp>
diff --git a/power-sequencer/Makefile.am b/power-sequencer/Makefile.am
index 8806a78..02f6466 100644
--- a/power-sequencer/Makefile.am
+++ b/power-sequencer/Makefile.am
@@ -16,9 +16,11 @@
 	$(top_builddir)/libpower.la \
 	$(PHOSPHOR_LOGGING_LIBS) \
 	${PHOSPHOR_DBUS_INTERFACES_LIBS} \
-	$(SDBUSPLUS_LIBS)
+	$(SDBUSPLUS_LIBS) \
+	$(SDEVENTPLUS_LIBS)
 
 witherspoon_pseq_monitor_CXXFLAGS = \
 	$(PHOSPHOR_LOGGING_CFLAGS) \
 	${PHOSPHOR_DBUS_INTERFACES_CFLAGS} \
-	$(SDBUSPLUS_CFLAGS)
+	$(SDBUSPLUS_CFLAGS) \
+	$(SDEVENTPLUS_CFLAGS)
diff --git a/power-sequencer/main.cpp b/power-sequencer/main.cpp
index 87b8070..e383fc1 100644
--- a/power-sequencer/main.cpp
+++ b/power-sequencer/main.cpp
@@ -16,6 +16,7 @@
 #include <chrono>
 #include <iostream>
 #include <phosphor-logging/log.hpp>
+#include <sdeventplus/event.hpp>
 #include "argument.hpp"
 #include "pgood_monitor.hpp"
 #include "runtime_monitor.hpp"
@@ -45,16 +46,7 @@
 
     std::chrono::milliseconds interval{i};
 
-    sd_event* e = nullptr;
-    auto r = sd_event_default(&e);
-    if (r < 0)
-    {
-        log<level::ERR>("sd_event_default() failed",
-                        entry("ERROR=%s", strerror(-r)));
-        exit(EXIT_FAILURE);
-    }
-
-    event::Event event{e};
+	auto event = sdeventplus::Event::get_default();
     auto bus = sdbusplus::bus::new_default();
     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
 
diff --git a/power-sequencer/pgood_monitor.cpp b/power-sequencer/pgood_monitor.cpp
index 02f1725..b9995c6 100644
--- a/power-sequencer/pgood_monitor.cpp
+++ b/power-sequencer/pgood_monitor.cpp
@@ -66,16 +66,6 @@
 }
 
 
-void PGOODMonitor::exitEventLoop()
-{
-    auto r = sd_event_exit(event.get(), EXIT_SUCCESS);
-    if (r < 0)
-    {
-        log<level::ERR>("sd_event_exit failed",
-                entry("RC = %d", r));
-    }
-}
-
 void PGOODMonitor::analyze()
 {
     //Timer callback.
@@ -93,8 +83,7 @@
 
     //The pgood-wait service (with a longer timeout)
     //will handle powering off the system.
-
-    exitEventLoop();
+    event.exit(EXIT_SUCCESS);
 }
 
 void PGOODMonitor::propertyChanged()
@@ -104,8 +93,7 @@
     if (!pgoodPending())
     {
         //PGOOD is on, or system is off, so we are done.
-        timer.stop();
-        exitEventLoop();
+        event.exit(EXIT_SUCCESS);
     }
 }
 
@@ -134,13 +122,7 @@
         }
 
         timer.start(interval);
-
-        auto r = sd_event_loop(event.get());
-        if (r < 0)
-        {
-            log<level::ERR>("sd_event_loop() failed",
-                    entry("ERROR=%d", r));
-        }
+        return event.loop();
     }
     catch (std::exception& e)
     {
diff --git a/power-sequencer/pgood_monitor.hpp b/power-sequencer/pgood_monitor.hpp
index 9cbda60..14a2735 100644
--- a/power-sequencer/pgood_monitor.hpp
+++ b/power-sequencer/pgood_monitor.hpp
@@ -2,8 +2,8 @@
 
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server.hpp>
+#include <sdeventplus/event.hpp>
 #include "device.hpp"
-#include "event.hpp"
 #include "device_monitor.hpp"
 #include "timer.hpp"
 
@@ -45,7 +45,7 @@
          */
         PGOODMonitor(std::unique_ptr<witherspoon::power::Device>&& d,
                 sdbusplus::bus::bus& b,
-                witherspoon::power::event::Event& e,
+                const sdeventplus::Event& e,
                 std::chrono::milliseconds& t) :
             DeviceMonitor(std::move(d), e, t),
             bus(b)
diff --git a/power-sequencer/runtime_monitor.hpp b/power-sequencer/runtime_monitor.hpp
index de71dcf..d37cdc0 100644
--- a/power-sequencer/runtime_monitor.hpp
+++ b/power-sequencer/runtime_monitor.hpp
@@ -2,8 +2,8 @@
 
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server.hpp>
+#include <sdeventplus/event.hpp>
 #include "device.hpp"
-#include "event.hpp"
 #include "device_monitor.hpp"
 #include "timer.hpp"
 
@@ -51,7 +51,7 @@
          */
         RuntimeMonitor(std::unique_ptr<witherspoon::power::Device>&& d,
                 sdbusplus::bus::bus& b,
-                witherspoon::power::event::Event& e,
+                const sdeventplus::Event& e,
                 std::chrono::milliseconds& i) :
             DeviceMonitor(std::move(d), e, i),
             bus(b),
diff --git a/power-supply/main.cpp b/power-supply/main.cpp
index ee93f74..62249c5 100644
--- a/power-supply/main.cpp
+++ b/power-supply/main.cpp
@@ -15,10 +15,9 @@
  */
 #include <iostream>
 #include <phosphor-logging/log.hpp>
-#include <systemd/sd-daemon.h>
+#include <sdeventplus/event.hpp>
 #include "argument.hpp"
 #include "config.h"
-#include "event.hpp"
 #include "power_supply.hpp"
 #include "device_monitor.hpp"
 
@@ -57,22 +56,12 @@
         return -4;
     }
 
-    sd_event* events = nullptr;
-
-    auto r = sd_event_default(&events);
-    if (r < 0)
-    {
-        log<level::ERR>("Failed call to sd_event_default()",
-                        entry("ERROR=%s", strerror(-r)));
-        return -5;
-    }
-
     auto bus = sdbusplus::bus::new_default();
-    witherspoon::power::event::Event eventPtr{events};
+    auto event = sdeventplus::Event::get_default();
 
-    //Attach the event object to the bus object so we can
-    //handle both sd_events (for the timers) and dbus signals.
-    bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
+    // Attach the event object to the bus object so we can
+    // handle both sd_events (for the timers) and dbus signals.
+    bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
 
     auto objname = "power_supply" + instnum;
     auto instance = std::stoul(instnum);
@@ -94,7 +83,7 @@
                                                         std::move(objpath),
                                                         std::move(invpath),
                                                         bus,
-                                                        eventPtr,
+                                                        event,
                                                         powerOnDelay,
                                                         presentDelay);
 
@@ -151,8 +140,5 @@
     }
 
     auto pollInterval = std::chrono::milliseconds(1000);
-    DeviceMonitor mainloop(std::move(psuDevice), eventPtr, pollInterval);
-    mainloop.run();
-
-    return 0;
+    return DeviceMonitor(std::move(psuDevice), event, pollInterval).run();
 }
diff --git a/power-supply/power_supply.cpp b/power-supply/power_supply.cpp
index d4c5f74..f5187f0 100644
--- a/power-supply/power_supply.cpp
+++ b/power-supply/power_supply.cpp
@@ -68,11 +68,11 @@
                          const std::string& objpath,
                          const std::string& invpath,
                          sdbusplus::bus::bus& bus,
-                         event::Event& e,
+                         const sdeventplus::Event& e,
                          std::chrono::seconds& t,
                          std::chrono::seconds& p)
     : Device(name, inst), monitorPath(objpath), pmbusIntf(objpath),
-      inventoryPath(INVENTORY_OBJ_PATH + invpath), bus(bus), event(e),
+      inventoryPath(INVENTORY_OBJ_PATH + invpath), bus(bus),
       presentInterval(p),
       presentTimer(e, [this]()
                    {
diff --git a/power-supply/power_supply.hpp b/power-supply/power_supply.hpp
index e88e257..30f660f 100644
--- a/power-supply/power_supply.hpp
+++ b/power-supply/power_supply.hpp
@@ -1,5 +1,6 @@
 #pragma once
 #include <sdbusplus/bus/match.hpp>
+#include <sdeventplus/event.hpp>
 #include "average.hpp"
 #include "device.hpp"
 #include "maximum.hpp"
@@ -51,7 +52,7 @@
                     const std::string& objpath,
                     const std::string& invpath,
                     sdbusplus::bus::bus& bus,
-                    event::Event& e,
+                    const sdeventplus::Event& e,
                     std::chrono::seconds& t,
                     std::chrono::seconds& p);
 
@@ -126,10 +127,6 @@
         /** @brief Used to subscribe to D-Bus property changes for Present */
         std::unique_ptr<sdbusplus::bus::match_t> presentMatch;
 
-        /** @brief The sd_event structure used by the power on and present
-         *  timers. */
-        event::Event& event;
-
         /**
          * @brief Interval for setting present to true.
          *
diff --git a/timer.cpp b/timer.cpp
index 7039338..fba2845 100644
--- a/timer.cpp
+++ b/timer.cpp
@@ -30,17 +30,15 @@
 using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
                             Error::InternalFailure;
 
-Timer::Timer(event::Event& events,
+Timer::Timer(const sdeventplus::Event& event,
              std::function<void()> callbackFunc) :
-    timeEvent(events),
+    eventSource(nullptr),
     callback(callbackFunc),
     timeout(0)
 {
-    sd_event_source* source = nullptr;
-
     // Start with an infinite expiration time
-    auto r = sd_event_add_time(timeEvent.get(),
-                               &source,
+    auto r = sd_event_add_time(event.get(),
+                               &eventSource,
                                CLOCK_MONOTONIC, // Time base
                                UINT64_MAX,      // Expire time - way long time
                                0,               // Use default event accuracy
@@ -53,8 +51,6 @@
         elog<InternalFailure>();
     }
 
-    eventSource.reset(source);
-
     //Ensure timer isn't running
     setTimer(SD_EVENT_OFF);
 }
@@ -63,6 +59,7 @@
 Timer::~Timer()
 {
     setTimer(SD_EVENT_OFF);
+    sd_event_source_unref(eventSource);
 }
 
 
@@ -93,7 +90,7 @@
 
 void Timer::setTimer(int action)
 {
-    auto r = sd_event_source_set_enabled(eventSource.get(), action);
+    auto r = sd_event_source_set_enabled(eventSource, action);
     if (r < 0)
     {
         log<level::ERR>("Failed call to sd_event_source_set_enabled",
@@ -115,7 +112,7 @@
     int status = 0;
 
     //returns SD_EVENT_OFF, SD_EVENT_ON, or SD_EVENT_ONESHOT
-    auto r = sd_event_source_get_enabled(eventSource.get(), &status);
+    auto r = sd_event_source_get_enabled(eventSource, &status);
     if (r < 0)
     {
         log<level::ERR>("Failed call to sd_event_source_get_enabled",
@@ -140,7 +137,7 @@
     auto expireTime = getTime() + timeout;
 
     //Set the time
-    auto r = sd_event_source_set_time(eventSource.get(), expireTime.count());
+    auto r = sd_event_source_set_time(eventSource, expireTime.count());
     if (r < 0)
     {
         log<level::ERR>("Failed call to sd_event_source_set_time",
diff --git a/timer.hpp b/timer.hpp
index 0f46de7..600fe42 100644
--- a/timer.hpp
+++ b/timer.hpp
@@ -3,7 +3,8 @@
 #include <chrono>
 #include <functional>
 #include <memory>
-#include "event.hpp"
+#include <sdeventplus/event.hpp>
+#include <systemd/sd-event.h>
 
 namespace witherspoon
 {
@@ -44,10 +45,10 @@
         /**
          * @brief Constructs timer object
          *
-         * @param[in] events - sd_event pointer, previously created
+         * @param[in] event - sd_event pointer, previously created
          * @param[in] callbackFunc - The function to call on timer expiration
          */
-        Timer(event::Event& events,
+        Timer(const sdeventplus::Event& event,
               std::function<void()> callbackFunc);
 
         /**
@@ -130,14 +131,9 @@
         void setTimeout();
 
         /**
-         * @brief The sd_event structure
-         */
-        event::Event& timeEvent;
-
-        /**
          * @brief Source of events
          */
-        event::EventSource eventSource;
+        sd_event_source* eventSource;
 
         /**
          * @brief Either 'repeating' or 'oneshot'