Implement startEventLoop and handler function for IPMI packets

The startEventLoop would start the sd_event_loop and register
the handler for IPMI incoming packets on UDP standard port 623.

Change-Id: Ia8ff44961686c1bf715413ff58bd60d7c71f1be1
Signed-off-by: Tom Joseph <tomjoseph@in.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index 4e96444..d1a1bd8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -43,11 +43,12 @@
 	sol/console_buffer.hpp \
 	sd_event_loop.hpp \
 	sol/sol_context.hpp \
-	sol/sol_manager.hpp
+	sol/sol_manager.hpp \
+	sd_event_loop.cpp
 
 netipmid_CPPFLAGS = -DNET_IPMID_LIB_PATH=\"/usr/lib/net-ipmid/\"
-netipmid_LDFLAGS = $(SYSTEMD_LIBS) $(CRYPTO_LIBS) $(libmapper_LIBS) $(LIBADD_DLOPEN) -export-dynamic
-netipmid_CXXFLAGS = $(SYSTEMD_CFLAGS) $(libmapper_CFLAGS)
+netipmid_LDFLAGS = $(SYSTEMD_LIBS) $(CRYPTO_LIBS) $(libmapper_LIBS) $(PHOSPHOR_LOGGING_LIBS) $(LIBADD_DLOPEN) -export-dynamic
+netipmid_CXXFLAGS = $(SYSTEMD_CFLAGS) $(libmapper_CFLAGS) $(PHOSPHOR_LOGGING_CFLAGS)
 
 SUBDIRS = test
 
diff --git a/configure.ac b/configure.ac
index 715bf56..3b7a75b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -42,6 +42,7 @@
 # Checks for libraries.
 PKG_CHECK_MODULES([SYSTEMD], [libsystemd >= 221])
 PKG_CHECK_MODULES([CRYPTO], [libcrypto >= 1.0.2g], ,[AC_MSG_ERROR([can't find openssl libcrypto])])
+PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging],, [AC_MSG_ERROR([Could not find phosphor-logging...openbmc/phosphor-logging package required])])
 AC_CHECK_LIB([mapper], [mapper_get_service], ,[AC_MSG_ERROR([Could not find libmapper...openbmc/phosphor-objmgr package required])])
 
 # Checks for header files.
diff --git a/sd_event_loop.cpp b/sd_event_loop.cpp
new file mode 100644
index 0000000..a99cf56
--- /dev/null
+++ b/sd_event_loop.cpp
@@ -0,0 +1,133 @@
+#include <sys/ioctl.h>
+#include <systemd/sd-daemon.h>
+#include <phosphor-logging/log.hpp>
+#include "main.hpp"
+#include "message_handler.hpp"
+#include "sd_event_loop.hpp"
+
+namespace eventloop
+{
+using namespace phosphor::logging;
+
+static int udp623Handler(sd_event_source* es, int fd, uint32_t revents,
+                         void* userdata)
+{
+    std::shared_ptr<udpsocket::Channel> channelPtr;
+    struct timeval timeout;
+    timeout.tv_sec = SELECT_CALL_TIMEOUT;
+    timeout.tv_usec = 0;
+
+    try
+    {
+        channelPtr.reset(new udpsocket::Channel(fd, timeout));
+
+        // Initialize the Message Handler with the socket channel
+        message::Handler msgHandler(channelPtr);
+
+
+        std::unique_ptr<message::Message> inMessage;
+
+        // Read the incoming IPMI packet
+        inMessage = msgHandler.receive();
+        if (inMessage == nullptr)
+        {
+            return 0;
+        }
+
+        // Execute the Command
+        auto outMessage = msgHandler.executeCommand(*(inMessage.get()));
+        if (outMessage == nullptr)
+        {
+            return 0;
+        }
+
+        // Send the response IPMI Message
+        msgHandler.send(*(outMessage.get()));
+    }
+    catch (std::exception& e)
+    {
+        log<level::ERR>("Executing the IPMI message failed");
+        log<level::ERR>(e.what());
+    }
+
+    return 0;
+}
+
+int EventLoop::startEventLoop()
+{
+    int fd = -1;
+    int r = 0;
+    sigset_t ss;
+    sd_event_source* source = nullptr;
+
+    r = sd_event_default(&event);
+    if (r < 0)
+    {
+        goto finish;
+    }
+
+    if (sigemptyset(&ss) < 0 || sigaddset(&ss, SIGTERM) < 0 ||
+        sigaddset(&ss, SIGINT) < 0)
+    {
+        r = -errno;
+        goto finish;
+    }
+
+    /* Block SIGTERM first, so that the event loop can handle it */
+    if (sigprocmask(SIG_BLOCK, &ss, nullptr) < 0)
+    {
+        r = -errno;
+        goto finish;
+    }
+
+    /* Let's make use of the default handler and "floating" reference features
+     * of sd_event_add_signal() */
+    r = sd_event_add_signal(event, nullptr, SIGTERM, nullptr, nullptr);
+    if (r < 0)
+    {
+        goto finish;
+    }
+
+    r = sd_event_add_signal(event, nullptr, SIGINT, nullptr, nullptr);
+    if (r < 0)
+    {
+        goto finish;
+    }
+
+    if (sd_listen_fds(0) != 1)
+    {
+        log<level::ERR>("No or too many file descriptors received");
+        goto finish;
+    }
+
+    fd = SD_LISTEN_FDS_START;
+
+    r = sd_event_add_io(event, &source, fd, EPOLLIN, udp623Handler, nullptr);
+    if (r < 0)
+    {
+        goto finish;
+    }
+
+    udpIPMI.reset(source);
+    source = nullptr;
+
+    r = sd_event_loop(event);
+
+finish:
+    event = sd_event_unref(event);
+
+    if (fd >= 0)
+    {
+        (void) close(fd);
+    }
+
+    if (r < 0)
+    {
+        log<level::ERR>("Event Loop Failure:",
+                entry("FAILURE=%s", strerror(-r)));
+    }
+
+    return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}
+
+} // namespace eventloop