Switch sd_event loops to sdeventplus

This change is mostly focused around plumbing the sdeventplus::Event
object everywhere and using the member functions provided for the event.
No migration to the timer utility is performed yet.

Change-Id: I912ab82bc081239d3b7c3cf7c5caca6742ef9c87
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/control/Makefile.am b/control/Makefile.am
index 01ee9c9..e0b0865 100644
--- a/control/Makefile.am
+++ b/control/Makefile.am
@@ -18,11 +18,13 @@
 phosphor_fan_control_LDADD = \
 	$(top_builddir)/libfan.la \
 	$(SDBUSPLUS_LIBS) \
+	$(SDEVENTPLUS_LIBS) \
 	$(PHOSPHOR_LOGGING_LIBS) \
 	${PHOSPHOR_DBUS_INTERFACES_LIBS}
 
 phosphor_fan_control_CXXFLAGS = \
 	$(SDBUSPLUS_CFLAGS) \
+	$(SDEVENTPLUS_CFLAGS) \
 	$(PHOSPHOR_LOGGING_CFLAGS) \
 	${PHOSPHOR_DBUS_INTERFACES_CFLAGS} \
 	-flto
diff --git a/control/actions.cpp b/control/actions.cpp
index b162602..71f7f72 100644
--- a/control/actions.cpp
+++ b/control/actions.cpp
@@ -43,7 +43,7 @@
                 // Create/start timer and associate event data with it
                 std::unique_ptr<util::Timer> timer =
                     std::make_unique<util::Timer>(
-                            zone.getEventPtr(),
+                            zone.getEventLoop(),
                             [&zone,
                             actions = &actions,
                             group = &group]()
diff --git a/control/main.cpp b/control/main.cpp
index 8c13cf9..4420488 100644
--- a/control/main.cpp
+++ b/control/main.cpp
@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 #include <sdbusplus/bus.hpp>
+#include <sdeventplus/event.hpp>
 #include <phosphor-logging/log.hpp>
 #include "argument.hpp"
 #include "manager.hpp"
-#include "event.hpp"
 #include "sdbusplus.hpp"
 
 using namespace phosphor::fan::control;
@@ -25,8 +25,8 @@
 
 int main(int argc, char* argv[])
 {
+    auto event = sdeventplus::Event::get_default();
     auto bus = sdbusplus::bus::new_default();
-    sd_event* events = nullptr;
     phosphor::fan::util::ArgumentParser args(argc, argv);
 
     if (argc != 2)
@@ -51,23 +51,13 @@
         return 1;
     }
 
-    auto r = sd_event_default(&events);
-    if (r < 0)
-    {
-        log<level::ERR>("Failed call to sd_event_default()",
-                        entry("ERROR=%s", strerror(-r)));
-        return 1;
-    }
-
-    phosphor::fan::event::EventPtr eventPtr{events};
-
     //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);
+    bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
 
     try
     {
-        Manager manager(bus, eventPtr, mode);
+        Manager manager(bus, event, mode);
 
         //Init mode will just set fans to max and delay
         if (mode == Mode::init)
@@ -75,15 +65,8 @@
             manager.doInit();
             return 0;
         }
-        else
-        {
-            r = sd_event_loop(eventPtr.get());
-            if (r < 0)
-            {
-                log<level::ERR>("Failed call to sd_event_loop",
-                        entry("ERROR=%s", strerror(-r)));
-            }
-        }
+
+        return event.loop();
     }
     //Log the useful metadata on these exceptions and let the app
     //return 1 so it is restarted without a core dump.
diff --git a/control/manager.cpp b/control/manager.cpp
index 1078473..bc6b18e 100644
--- a/control/manager.cpp
+++ b/control/manager.cpp
@@ -75,7 +75,7 @@
 
 //Note: Future code will check 'mode' before starting control algorithm
 Manager::Manager(sdbusplus::bus::bus& bus,
-                 phosphor::fan::event::EventPtr& events,
+                 const sdeventplus::Event& event,
                  Mode mode) :
     _bus(bus)
 {
@@ -99,7 +99,7 @@
             for (auto& z : zones)
             {
                 _zones.emplace(std::get<zoneNumPos>(z),
-                               std::make_unique<Zone>(mode, _bus, events, z));
+                               std::make_unique<Zone>(mode, _bus, event, z));
             }
 
             break;
diff --git a/control/manager.hpp b/control/manager.hpp
index d049323..5b748a2 100644
--- a/control/manager.hpp
+++ b/control/manager.hpp
@@ -3,6 +3,7 @@
 #include <memory>
 #include <vector>
 #include <sdbusplus/bus.hpp>
+#include <sdeventplus/event.hpp>
 #include "types.hpp"
 #include "zone.hpp"
 
@@ -36,11 +37,11 @@
          * _zoneLayouts data.
          *
          * @param[in] bus - The dbus object
-         * @param[in] events - The sd_event pointer
+         * @param[in] event - The event loop
          * @param[in] mode - The control mode
          */
         Manager(sdbusplus::bus::bus& bus,
-                phosphor::fan::event::EventPtr& events,
+                const sdeventplus::Event& event,
                 Mode mode);
 
         /**
diff --git a/control/zone.cpp b/control/zone.cpp
index f4c50d9..0050899 100644
--- a/control/zone.cpp
+++ b/control/zone.cpp
@@ -37,7 +37,7 @@
 
 Zone::Zone(Mode mode,
            sdbusplus::bus::bus& bus,
-           phosphor::fan::event::EventPtr& events,
+           const sdeventplus::Event& event,
            const ZoneDefinition& def) :
     _bus(bus),
     _fullSpeed(std::get<fullSpeedPos>(def)),
@@ -46,9 +46,9 @@
     _defCeilingSpeed(std::get<fullSpeedPos>(def)),
     _incDelay(std::get<incDelayPos>(def)),
     _decInterval(std::get<decIntervalPos>(def)),
-    _incTimer(events, [this](){ this->incTimerExpired(); }),
-    _decTimer(events, [this](){ this->decTimerExpired(); }),
-    _sdEvents(events)
+    _incTimer(event, [this](){ this->incTimerExpired(); }),
+    _decTimer(event, [this](){ this->decTimerExpired(); }),
+    _eventLoop(event)
 {
     auto& fanDefs = std::get<fanListPos>(def);
 
@@ -347,7 +347,7 @@
             );
         std::unique_ptr<util::Timer> timer =
             std::make_unique<util::Timer>(
-                _sdEvents,
+                _eventLoop,
                 [this,
                  action = &(std::get<actionsPos>(event)),
                  group = &(std::get<groupPos>(event))]()
diff --git a/control/zone.hpp b/control/zone.hpp
index cd7e5d6..e035a64 100644
--- a/control/zone.hpp
+++ b/control/zone.hpp
@@ -4,6 +4,7 @@
 #include <cassert>
 #include <algorithm>
 #include <sdbusplus/bus.hpp>
+#include <sdeventplus/event.hpp>
 #include "fan.hpp"
 #include "types.hpp"
 #include "timer.hpp"
@@ -48,12 +49,12 @@
          *
          * @param[in] mode - mode of fan control
          * @param[in] bus - the dbus object
-         * @param[in] events - sd_event pointer
+         * @param[in] event - Event loop reference
          * @param[in] def - the fan zone definition data
          */
         Zone(Mode mode,
              sdbusplus::bus::bus& bus,
-             phosphor::fan::event::EventPtr& events,
+             const sdeventplus::Event& event,
              const ZoneDefinition& def);
 
         /**
@@ -332,13 +333,13 @@
         void decTimerExpired();
 
         /**
-         * @brief Get the event pointer used with this zone's timers
+         * @brief Get the event loop used with this zone's timers
          *
-         * @return - The Dbus event pointer for timers
+         * @return - The event loop for timers
          */
-        inline auto& getEventPtr()
+        inline auto& getEventLoop()
         {
-            return _sdEvents;
+            return _eventLoop;
         }
 
         /**
@@ -514,9 +515,9 @@
         phosphor::fan::util::Timer _decTimer;
 
         /**
-         * Dbus event used on set speed event timers
+         * Event loop used on set speed event timers
          */
-        phosphor::fan::event::EventPtr& _sdEvents;
+        sdeventplus::Event _eventLoop;
 
         /**
          * The vector of fans in this zone