softoff: Convert to sdeventplus loop

Tested:
    Builds and passes unit tests

Change-Id: I6c37fa7088b7bcfbfc9f8b93185b826782a72c6a
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/configure.ac b/configure.ac
index 40be188..5cbdfc6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -31,6 +31,9 @@
     AC_MSG_ERROR(["Requires sdbusplus package."]))
 
 AS_IF([test "x$enable_softoff" != "xno"],
+    PKG_CHECK_MODULES([SDEVENTPLUS], [sdeventplus],,\
+        AC_MSG_ERROR(["Requires sdeventplus package."]))
+
     # Check for sdbus++ tool
     [AC_PATH_PROG([SDBUSPLUSPLUS], [sdbus++])]
     AS_IF([test "x$SDBUSPLUSPLUS" == "x"],
diff --git a/softoff/Makefile.am b/softoff/Makefile.am
index ed0c226..ecd8f86 100644
--- a/softoff/Makefile.am
+++ b/softoff/Makefile.am
@@ -19,10 +19,12 @@
 
 phosphor_softpoweroff_LDFLAGS = $(SYSTEMD_LIBS) \
 								$(SDBUSPLUS_LIBS) \
+								$(SDEVENTPLUS_LIBS) \
 								$(PHOSPHOR_LOGGING_LIBS) \
 								$(PHOSPHOR_DBUS_INTERFACES_LIBS)
 phosphor_softpoweroff_CXXFLAGS = $(SYSTEMD_CFLAGS) \
 								 $(SDBUSPLUS_CFLAGS) \
+								 $(SDEVENTPLUS_CFLAGS) \
 								 $(PHOSPHOR_LOGGING_CFLAGS) \
 								 $(PHOSPHOR_DBUS_INTERFACES_CFLAGS)
 
diff --git a/softoff/mainapp.cpp b/softoff/mainapp.cpp
index f6a52ac..70defb7 100644
--- a/softoff/mainapp.cpp
+++ b/softoff/mainapp.cpp
@@ -17,11 +17,10 @@
 
 #include "softoff.hpp"
 
-#include <systemd/sd-event.h>
-
 #include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/elog.hpp>
-#include <sdbusplus/timer.hpp>
+#include <sdeventplus/event.hpp>
+#include <sdeventplus/exception.hpp>
 #include <xyz/openbmc_project/State/Host/error.hpp>
 
 // Return -1 on any errors to ensure we follow the calling targets OnFailure=
@@ -30,26 +29,17 @@
 {
     using namespace phosphor::logging;
 
-    // systemd event handler
-    sd_event* events = nullptr;
-
     // Get a handle to system dbus.
     auto bus = sdbusplus::bus::new_default();
 
     // Add systemd object manager.
     sdbusplus::server::manager::manager(bus, SOFTOFF_OBJPATH);
 
-    // sd_event object
-    auto r = sd_event_default(&events);
-    if (r < 0)
-    {
-        log<level::ERR>("Failure to create sd_event handler",
-                        entry("ERRNO=0x%X", -r));
-        return -1;
-    }
+    // Get default event loop
+    auto event = sdeventplus::Event::get_default();
 
     // Attach the bus to sd_event to service user requests
-    bus.attach_event(events, SD_EVENT_PRIORITY_NORMAL);
+    bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
 
     // Claim the bus. Delaying it until sending SMS_ATN may result
     // in a race condition between this available and IPMI trying to send
@@ -57,19 +47,21 @@
     bus.request_name(SOFTOFF_BUSNAME);
 
     // Create the SoftPowerOff object.
-    phosphor::ipmi::SoftPowerOff powerObj(bus, events, SOFTOFF_OBJPATH);
+    phosphor::ipmi::SoftPowerOff powerObj(bus, event.get(), SOFTOFF_OBJPATH);
 
     // Wait for client requests until this application has processed
     // at least one successful SoftPowerOff or we timed out
     while (!powerObj.isCompleted() && !powerObj.isTimerExpired())
     {
-        // -1 denotes wait for ever
-        r = sd_event_run(events, (uint64_t)-1);
-        if (r < 0)
+        try
+        {
+            event.run(std::nullopt);
+        }
+        catch (const sdeventplus::SdEventError& e)
         {
             log<level::ERR>("Failure in processing request",
-                            entry("ERRNO=0x%X", -r));
-            return -1;
+                            entry("ERROR=%s", e.what()));
+            return 1;
         }
     }
 
@@ -86,8 +78,5 @@
         return -1;
     }
 
-    // Cleanup the event handler
-    events = sd_event_unref(events);
-
     return 0;
 }