Read the slp service info.

Read the info from the services file which are in the
services dir.

Change-Id: If31b98af8eea55d75356d895afa42bf6c3cb6493
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/configure.ac b/configure.ac
index d20b4be..ae7b219 100755
--- a/configure.ac
+++ b/configure.ac
@@ -3,7 +3,6 @@
 AC_INIT([slpd-lite], [1.0], [https://github.com/openbmc/slpd-lite/issues])
 AC_CONFIG_HEADERS([config.h])
 AM_INIT_AUTOMAKE([subdir-objects -Wall -Werror foreign dist-xz])
-
 # Checks for programs.
 AC_PROG_CXX
 AX_CXX_COMPILE_STDCXX_14([noext])
@@ -18,5 +17,8 @@
 AC_CHECK_HEADER(systemd/sd-bus.h, ,[AC_MSG_ERROR([Could not find systemd/sd-bus.h...systemd developement package required])])
 # Checks for typedefs, structures, and compiler characteristics.
 
+AC_ARG_VAR(SERVICE_DIR, [Location of service files.])
+AS_IF([test "x$SERVICE_DIR" == "x"], [SERVICE_DIR="/etc/slp/services/"])
+AC_DEFINE_UNQUOTED([SERVICE_DIR], ["$SERVICE_DIR"], [Location of service files.])
 AC_CONFIG_FILES([Makefile])
 AC_OUTPUT
diff --git a/slp.hpp b/slp.hpp
index 5f9703c..432cfb6 100644
--- a/slp.hpp
+++ b/slp.hpp
@@ -254,7 +254,7 @@
  * @internal
  *
  */
-ServiceList readSLPServiceInfo(const std::string& filename);
+ServiceList readSLPServiceInfo();
 
 /**  Get all the interface address
  *
diff --git a/slp_message_handler.cpp b/slp_message_handler.cpp
index fd6670a..5fd3e38 100644
--- a/slp_message_handler.cpp
+++ b/slp_message_handler.cpp
@@ -1,12 +1,14 @@
 #include "slp.hpp"
 
 #include <arpa/inet.h>
+#include <dirent.h>
 #include <ifaddrs.h>
 #include <net/if.h>
 #include <string.h>
 
 #include <algorithm>
 
+#include "config.h"
 #include "endian.hpp"
 #include "slp_meta.hpp"
 
@@ -84,7 +86,7 @@
 
     //read the slp service info from conf and create the service type string
     slp::handler::internal::ServiceList svcList =
-        slp::handler::internal::readSLPServiceInfo(slp::CONF_FILE);
+        slp::handler::internal::readSLPServiceInfo();
     if (svcList.size() <= 0)
     {
         buff.resize(0);
@@ -175,7 +177,7 @@
     buffer buff;
     //Get all the services which are registered
     slp::handler::internal::ServiceList svcList =
-        slp::handler::internal::readSLPServiceInfo(slp::CONF_FILE);
+        slp::handler::internal::readSLPServiceInfo();
     if (svcList.size() <= 0)
     {
         buff.resize(0);
@@ -314,25 +316,42 @@
     return addrList;
 }
 
-slp::handler::internal::ServiceList  readSLPServiceInfo(
-    const std::string& filename)
+slp::handler::internal::ServiceList  readSLPServiceInfo()
 {
     using namespace std::string_literals;
-    /*Conf File format would be
-      ServiceName serviceType Port */
-
     slp::handler::internal::ServiceList svcLst;
-
-    std::ifstream readFile(filename);
     slp::ConfigData service;
-    //Read all the service from the file
-    while (readFile >> service)
-    {
-        std::string tmp = "service:"s + service.name;
-        service.name = tmp;
-        svcLst.emplace(service.name, service);
-    }
+    struct dirent* dent = nullptr;
 
+    // Open the services dir and get the service info
+    // from service files.
+    // Service File format would be "ServiceName serviceType Port"
+    DIR* dir = opendir(SERVICE_DIR);
+    // wrap the pointer into smart pointer.
+    slp::deleted_unique_ptr<DIR> dirPtr(dir, [](DIR * dir)
+    {
+        if (!dir)
+        {
+            closedir(dir);
+        }
+    });
+    dir = nullptr;
+
+    if (dirPtr.get())
+    {
+        while ((dent = readdir(dirPtr.get())) != NULL)
+        {
+            if (dent->d_type == DT_REG) //regular file
+            {
+                auto absFileName = std::string(SERVICE_DIR) + dent->d_name;
+                std::ifstream readFile(absFileName);
+                readFile >> service;
+                service.name = "service:"s + service.name;
+                svcLst.emplace(service.name, service);
+            }
+        }
+
+    }
     return svcLst;
 }
 }//namespace internal
diff --git a/slp_meta.hpp b/slp_meta.hpp
index 791d3d6..0cebfdf 100644
--- a/slp_meta.hpp
+++ b/slp_meta.hpp
@@ -2,8 +2,6 @@
 
 namespace slp
 {
-/** @brief SLP configuartion file which is having the service information */
-constexpr auto CONF_FILE = "/etc/slp_lite.conf";
 /** @brief SLP Version */
 constexpr size_t VERSION_2 = 2;
 constexpr auto SUCCESS = 0;