mainloop: Use sdeventplus

Convert to using sdeventplus instead of sd_event directly.

Change-Id: Ie8ee25c2d75ac4c56b88fb79f70eaa53c4f7b6e4
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/Makefile.am b/Makefile.am
index c0a30ea..3d5f786 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -9,11 +9,13 @@
 libhwmon_la_LIBADD = \
 	-lstdc++fs \
 	$(SDBUSPLUS_LIBS) \
+	$(SDEVENTPLUS_LIBS) \
 	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
 	$(PHOSPHOR_LOGGING_LIBS) \
 	$(GPIOPLUS_LIBS)
 libhwmon_la_CXXFLAGS = \
 	$(SDBUSPLUS_CFLAGS) \
+	$(SDEVENTPLUS_CFLAGS) \
 	$(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
 	$(PHOSPHOR_LOGGING_CFLAGS) \
 	$(GPIOPLUS_CFLAGS)
diff --git a/configure.ac b/configure.ac
index 36d10ec..c3a92c7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -14,6 +14,7 @@
 
 # Checks for libraries.
 PKG_CHECK_MODULES([SDBUSPLUS], [sdbusplus], [], [AC_MSG_ERROR(["sdbusplus required and not found."])])
+PKG_CHECK_MODULES([SDEVENTPLUS], [sdeventplus], [], [AC_MSG_ERROR(["sdeventplus required and not found."])])
 PKG_CHECK_MODULES([PHOSPHOR_DBUS_INTERFACES], [phosphor-dbus-interfaces], [], [AC_MSG_ERROR(["phosphor-dbus-interfaces required and not found."])])
 PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging], [], [AC_MSG_ERROR(["phosphor-logging required and not found."])])
 PKG_CHECK_MODULES([GPIOPLUS], [gpioplus],, [AC_MSG_ERROR([Could not find gpioplus...openbmc/gpioplus package required])])
diff --git a/mainloop.cpp b/mainloop.cpp
index 7435ef3..3c6d232 100644
--- a/mainloop.cpp
+++ b/mainloop.cpp
@@ -243,7 +243,8 @@
                    const char* prefix, const char* root) :
     _bus(std::move(bus)),
     _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
-    _devPath(devPath), _prefix(prefix), _root(root), state(), ioAccess(path)
+    _devPath(devPath), _prefix(prefix), _root(root), state(), ioAccess(path),
+    event(sdeventplus::Event::get_default())
 {
     // Strip off any trailing slashes.
     std::string p = path;
@@ -268,21 +269,18 @@
 void MainLoop::shutdown() noexcept
 {
     timer->state(phosphor::hwmon::timer::OFF);
-    sd_event_exit(loop, 0);
-    loop = nullptr;
+    event.exit(0);
 }
 
 void MainLoop::run()
 {
     init();
 
-    sd_event_default(&loop);
-
     std::function<void()> callback(std::bind(&MainLoop::read, this));
     try
     {
         timer = std::make_unique<phosphor::hwmon::Timer>(
-            loop, callback, std::chrono::microseconds(_interval),
+            event.get(), callback, std::chrono::microseconds(_interval),
             phosphor::hwmon::timer::ON);
 
         // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
@@ -290,10 +288,10 @@
         // TODO: Issue#7 - Should probably periodically check the SensorSet
         //       for new entries.
 
-        _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
-        sd_event_loop(loop);
+        _bus.attach_event(event.get(), SD_EVENT_PRIORITY_IMPORTANT);
+        event.loop();
     }
-    catch (const std::system_error& e)
+    catch (const std::exception& e)
     {
         log<level::ERR>("Error in sysfs polling loop",
                         entry("ERROR=%s", e.what()));
diff --git a/mainloop.hpp b/mainloop.hpp
index 9c50c78..87f6ed0 100644
--- a/mainloop.hpp
+++ b/mainloop.hpp
@@ -12,6 +12,7 @@
 #include <memory>
 #include <optional>
 #include <sdbusplus/server.hpp>
+#include <sdeventplus/event.hpp>
 #include <string>
 #include <vector>
 
@@ -99,8 +100,8 @@
     hwmonio::HwmonIO ioAccess;
     /** @brief Timer */
     std::unique_ptr<phosphor::hwmon::Timer> timer;
-    /** @brief the sd_event structure */
-    sd_event* loop = nullptr;
+    /** @brief the Event Loop structure */
+    sdeventplus::Event event;
     /** @brief Store the specifications of sensor objects */
     std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>>
         sensorObjects;