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/monitor/Makefile.am b/monitor/Makefile.am
index cb9b8a4..e8d9bbd 100644
--- a/monitor/Makefile.am
+++ b/monitor/Makefile.am
@@ -19,12 +19,14 @@
 phosphor_fan_monitor_LDADD = \
 	$(top_builddir)/libfan.la \
 	$(SDBUSPLUS_LIBS) \
+	$(SDEVENTPLUS_LIBS) \
 	$(PHOSPHOR_LOGGING_LIBS) \
 	${PHOSPHOR_DBUS_INTERFACES_LIBS} \
 	-lstdc++fs
 
 phosphor_fan_monitor_CXXFLAGS = \
 	$(SDBUSPLUS_CFLAGS) \
+	$(SDEVENTPLUS_CFLAGS) \
 	$(PHOSPHOR_LOGGING_CFLAGS) \
 	${PHOSPHOR_DBUS_INTERFACES_CFLAGS} \
 	-flto
diff --git a/monitor/fan.cpp b/monitor/fan.cpp
index 39b32bb..014080d 100644
--- a/monitor/fan.cpp
+++ b/monitor/fan.cpp
@@ -31,7 +31,7 @@
 
 Fan::Fan(Mode mode,
          sdbusplus::bus::bus& bus,
-         phosphor::fan::event::EventPtr&  events,
+         const sdeventplus::Event&  event,
          std::unique_ptr<trust::Manager>& trust,
          const FanDefinition& def) :
     _bus(bus),
@@ -58,7 +58,7 @@
                             std::get<factorField>(s),
                             std::get<offsetField>(s),
                             std::get<timeoutField>(def),
-                            events));
+                            event));
 
             _trustManager->registerSensor(_sensors.back());
         }
diff --git a/monitor/fan.hpp b/monitor/fan.hpp
index fb87b66..663e27e 100644
--- a/monitor/fan.hpp
+++ b/monitor/fan.hpp
@@ -1,9 +1,9 @@
 #pragma once
 
 #include <sdbusplus/bus.hpp>
+#include <sdeventplus/event.hpp>
 #include <tuple>
 #include <vector>
-#include "event.hpp"
 #include "tach_sensor.hpp"
 #include "trust_manager.hpp"
 #include "types.hpp"
@@ -78,13 +78,13 @@
          *
          * @param mode - mode of fan monitor
          * @param bus - the dbus object
-         * @param events - pointer to sd_event object
+         * @param event - event loop reference
          * @param trust - the tach trust manager
          * @param def - the fan definition structure
          */
         Fan(Mode mode,
             sdbusplus::bus::bus& bus,
-            phosphor::fan::event::EventPtr& events,
+            const sdeventplus::Event& event,
             std::unique_ptr<trust::Manager>& trust,
             const FanDefinition& def);
 
diff --git a/monitor/main.cpp b/monitor/main.cpp
index 512e3f7..cba3832 100644
--- a/monitor/main.cpp
+++ b/monitor/main.cpp
@@ -15,9 +15,8 @@
  */
 #include <phosphor-logging/log.hpp>
 #include <sdbusplus/bus.hpp>
-#include <systemd/sd-daemon.h>
+#include <sdeventplus/event.hpp>
 #include "argument.hpp"
-#include "event.hpp"
 #include "fan.hpp"
 #include "fan_defs.hpp"
 #include "trust_manager.hpp"
@@ -27,8 +26,8 @@
 
 int main(int argc, char* argv[])
 {
+    auto event = sdeventplus::Event::get_default();
     auto bus = sdbusplus::bus::new_default();
-    sd_event* events = nullptr;
     std::vector<std::unique_ptr<Fan>> fans;
     phosphor::fan::util::ArgumentParser args(argc, argv);
 
@@ -53,22 +52,12 @@
         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;
-    }
-
     std::unique_ptr<phosphor::fan::trust::Manager> trust =
             std::make_unique<phosphor::fan::trust::Manager>(trustGroups);
 
-    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);
 
     for (const auto& fanDef : fanDefinitions)
     {
@@ -83,7 +72,7 @@
             }
         }
         fans.emplace_back(std::make_unique<Fan>(
-                mode, bus, eventPtr, trust, fanDef));
+                mode, bus, event, trust, fanDef));
     }
 
     if (mode == Mode::init)
@@ -91,15 +80,6 @@
         // Fans were initialized to be functional, exit
         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 1;
+    return event.loop();
 }
diff --git a/monitor/tach_sensor.cpp b/monitor/tach_sensor.cpp
index 6789ed8..de1affb 100644
--- a/monitor/tach_sensor.cpp
+++ b/monitor/tach_sensor.cpp
@@ -76,7 +76,7 @@
                        size_t factor,
                        size_t offset,
                        size_t timeout,
-                       phosphor::fan::event::EventPtr& events) :
+                       const sdeventplus::Event& event) :
     _bus(bus),
     _fan(fan),
     _name(FAN_SENSOR_PATH + id),
@@ -88,7 +88,7 @@
     _offset(offset),
     _timeout(timeout),
     _timerMode(TimerMode::func),
-    _timer(events, [this, &fan](){ fan.timerExpired(*this); })
+    _timer(event, [this, &fan](){ fan.timerExpired(*this); })
 {
     // Start from a known state of functional
     setFunctional(true);
diff --git a/monitor/tach_sensor.hpp b/monitor/tach_sensor.hpp
index f59f6f5..c46aa00 100644
--- a/monitor/tach_sensor.hpp
+++ b/monitor/tach_sensor.hpp
@@ -3,7 +3,7 @@
 #include <chrono>
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server.hpp>
-#include "event.hpp"
+#include <sdeventplus/event.hpp>
 #include "timer.hpp"
 
 namespace phosphor
@@ -78,7 +78,7 @@
          * @param[in] factor - the factor of the sensor target
          * @param[in] offset - the offset of the sensor target
          * @param[in] timeout - Normal timeout value to use
-         * @param[in] events - sd_event pointer
+         * @param[in] event - Event loop reference
          */
         TachSensor(Mode mode,
                    sdbusplus::bus::bus& bus,
@@ -90,7 +90,7 @@
                    size_t factor,
                    size_t offset,
                    size_t timeout,
-                   phosphor::fan::event::EventPtr& events);
+                   const sdeventplus::Event& event);
 
         /**
          * @brief Returns the target speed value