Add sd_event processing to host ipmid application

In order to support the timer function, it's required
to use the sd_event loop instead of sdbus loop

Change-Id: I3a30fee1a21cbfadd0cbb5478bf46bea4b5ca0b8
Signed-off-by: Andrew Geissler <andrewg@us.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index 1df5216..4daadcd 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -10,8 +10,8 @@
 CLEANFILES = $(BUILT_SOURCES)
 
 #TODO - Make this path a configure option (bitbake parameter)
-ipmid_CPPFLAGS = -DHOST_IPMI_LIB_PATH=\"/usr/lib/host-ipmid/\"
-ipmid_LDFLAGS = $(SYSTEMD_LIBS) $(libmapper_LIBS) $(LIBADD_DLOPEN) -export-dynamic
+ipmid_CPPFLAGS = -DHOST_IPMI_LIB_PATH=\"/usr/lib/host-ipmid/\" $(PHOSPHOR_LOGGING_CFLAGS)
+ipmid_LDFLAGS = $(SYSTEMD_LIBS) $(libmapper_LIBS) $(LIBADD_DLOPEN) $(PHOSPHOR_LOGGING_LIBS) -export-dynamic
 # TODO: Rather than use -export-dynamic, we should use -export-symbol to have a
 #       selective list of symbols.
 
diff --git a/host-ipmid/ipmid-api.h b/host-ipmid/ipmid-api.h
index 67a1000..cc3be36 100644
--- a/host-ipmid/ipmid-api.h
+++ b/host-ipmid/ipmid-api.h
@@ -122,6 +122,7 @@
 };
 
 sd_bus *ipmid_get_sd_bus_connection(void);
+sd_event *ipmid_get_sd_event_connection(void);
 sd_bus_slot *ipmid_get_sd_bus_slot(void);
 
 #ifdef __cplusplus
diff --git a/ipmid.cpp b/ipmid.cpp
index 0c9db6f..1d5c8cf 100644
--- a/ipmid.cpp
+++ b/ipmid.cpp
@@ -8,6 +8,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <map>
+#include <phosphor-logging/log.hpp>
 #include "ipmid.hpp"
 #include <sys/time.h>
 #include <errno.h>
@@ -18,8 +19,11 @@
 #include <iterator>
 #include <ipmiwhitelist.hpp>
 
+using namespace phosphor::logging;
+
 sd_bus *bus = NULL;
 sd_bus_slot *ipmid_slot = NULL;
+sd_event *events = nullptr;
 
 // Initialise restricted mode to true
 bool restricted_mode = true;
@@ -471,6 +475,10 @@
     return bus;
 }
 
+sd_event *ipmid_get_sd_event_connection(void) {
+    return events;
+}
+
 sd_bus_slot *ipmid_get_sd_bus_slot(void) {
     return ipmid_slot;
 }
@@ -516,6 +524,16 @@
         goto finish;
     }
 
+    /* Get an sd event handler */
+    r = sd_event_default(&events);
+    if (r < 0)
+    {
+        log<level::ERR>("Failure to create sd_event handler",
+                entry("ERROR=%s", strerror(-r)));
+        goto finish;
+    }
+
+
     // Register all the handlers that provider implementation to IPMI commands.
     ipmi_register_callback_handlers(HOST_IPMI_LIB_PATH);
 
@@ -533,28 +551,26 @@
         goto finish;
     }
 
-    // Initialise restricted mode
+    // Attach the bus to sd_event to service user requests
+    sd_bus_attach_event(bus, events, SD_EVENT_PRIORITY_NORMAL);
+
+    // Initialize restricted mode
     cache_restricted_mode();
 
     for (;;) {
         /* Process requests */
-        r = sd_bus_process(bus, NULL);
-        if (r < 0) {
-            fprintf(stderr, "Failed to process bus: %s\n", strerror(-r));
-            goto finish;
-        }
-        if (r > 0) {
-            continue;
-        }
-
-        r = sd_bus_wait(bus, (uint64_t) - 1);
-        if (r < 0) {
-            fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r));
+        r = sd_event_run(events, (uint64_t)-1);
+        if (r < 0)
+        {
+            log<level::ERR>("Failure in processing request",
+                    entry("ERROR=%s", strerror(-r)));
             goto finish;
         }
     }
 
 finish:
+    sd_event_unref(events);
+    sd_bus_detach_event(bus);
     sd_bus_slot_unref(ipmid_slot);
     sd_bus_unref(bus);
     return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;