Added openpower util file support

Created common file to keep all the common util functions.

In this commit
 - added getService function to get d-bus service for given
   interface.
 - updated create_pel.cpp to start using common util interface.
 - Moved formatting library to common build dependency for
   both meson and automake.

Tested: Manually verified.

Signed-off-by: Jayanth Othayoth <ojayanth@in.ibm.com>
Change-Id: I39a642710c0476e81fae020eb3b4216ca8ce9ad5
diff --git a/Makefile.am b/Makefile.am
index bdf512a..7b7f46d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -25,7 +25,8 @@
 	registration.cpp \
 	targeting.cpp \
 	openpower_procedures.cpp \
-	ext_interface.cpp
+	ext_interface.cpp \
+	util.cpp
 
 openpower_proc_nmi_SOURCES = \
 	nmi_main.cpp \
diff --git a/configure.ac b/configure.ac
index a9b6c53..95b057c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -77,9 +77,6 @@
        LIBS="$LIBS $LIBDT_API_LIBS"
        CFLAGS="$CFLAGS $LIBDT_API_CFLAGS"
 
-       PKG_CHECK_MODULES([FMT], [fmt])
-       LIBS="$LIBS $FMT_LIBS"
-
        CHIPS+=" phal common"
        AC_CONFIG_FILES([set-spi-mux.service])
        AC_CONFIG_FILES([phal-reinit-devtree.service])
@@ -99,6 +96,8 @@
 PKG_CHECK_MODULES([SYSTEMD], [libsystemd >= 221])
 PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging])
 PKG_CHECK_MODULES([PHOSPHOR_DBUS_INTERFACES], [phosphor-dbus-interfaces])
+PKG_CHECK_MODULES([FMT], [fmt])
+LIBS="$LIBS $FMT_LIBS"
 
 PKG_PROG_PKG_CONFIG
 AC_ARG_WITH([systemdsystemunitdir],
diff --git a/meson.build b/meson.build
index bb9c5e9..c338266 100644
--- a/meson.build
+++ b/meson.build
@@ -71,7 +71,6 @@
     ]
     extra_dependencies += [
         dependency('libdt-api'),
-        dependency('fmt'),
         cxx.find_library('ekb'),
         cxx.find_library('ipl'),
     ]
@@ -96,6 +95,7 @@
         'procedures/common/cfam_reset.cpp',
         'procedures/common/enter_mpreboot.cpp',
         'procedures/common/collect_sbe_hb_data.cpp',
+        'util.cpp',
     ] + extra_sources,
     dependencies: [
         dependency('libgpiodcxx'),
@@ -104,6 +104,7 @@
         dependency('phosphor-logging'),
         dependency('sdbusplus'),
         dependency('threads'),
+        dependency('fmt'),
     ] + extra_dependencies,
     install: true
 )
diff --git a/phalerror/create_pel.cpp b/phalerror/create_pel.cpp
index e3e6117..4ca8bb0 100644
--- a/phalerror/create_pel.cpp
+++ b/phalerror/create_pel.cpp
@@ -1,5 +1,7 @@
 #include "create_pel.hpp"
 
+#include "util.hpp"
+
 #include <fcntl.h>
 #include <fmt/format.h>
 #include <libekb.H>
@@ -23,41 +25,6 @@
 {
 using namespace phosphor::logging;
 
-namespace util
-{
-std::string getService(sdbusplus::bus::bus& bus, const std::string& objectPath,
-                       const std::string& interface)
-{
-    constexpr auto mapperBusBame = "xyz.openbmc_project.ObjectMapper";
-    constexpr auto mapperObjectPath = "/xyz/openbmc_project/object_mapper";
-    constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper";
-    std::vector<std::pair<std::string, std::vector<std::string>>> response;
-    auto method = bus.new_method_call(mapperBusBame, mapperObjectPath,
-                                      mapperInterface, "GetObject");
-    method.append(objectPath, std::vector<std::string>({interface}));
-    try
-    {
-        auto reply = bus.call(method);
-        reply.read(response);
-    }
-    catch (const sdbusplus::exception::SdBusError& e)
-    {
-        log<level::ERR>("D-Bus call exception",
-                        entry("OBJPATH=%s", mapperObjectPath),
-                        entry("INTERFACE=%s", mapperInterface),
-                        entry("EXCEPTION=%s", e.what()));
-
-        throw std::runtime_error("Service name is not found");
-    }
-
-    if (response.empty())
-    {
-        throw std::runtime_error("Service name response is empty");
-    }
-    return response.begin()->first;
-}
-} // namespace util
-
 namespace pel
 {
 void createBootErrorPEL(const FFDCData& ffdcData, const json& calloutData)
diff --git a/phalerror/create_pel.hpp b/phalerror/create_pel.hpp
index 0c6ee42..bf005c1 100644
--- a/phalerror/create_pel.hpp
+++ b/phalerror/create_pel.hpp
@@ -1,26 +1,11 @@
 #pragma once
 
 #include <nlohmann/json.hpp>
-#include <sdbusplus/bus.hpp>
 
 #include <string>
 #include <vector>
 namespace openpower
 {
-namespace util
-{
-/**
- * Get D-Bus service name for the specified object and interface
- *
- * @param[in] bus - sdbusplus D-Bus to attach to
- * @param[in] objectPath - D-Bus object path
- * @param[in] interface - D-Bus interface name
- *
- * @return service name on success and exception on failure
- */
-std::string getService(sdbusplus::bus::bus& bus, const std::string& objectPath,
-                       const std::string& interface);
-} // namespace util
 namespace pel
 {
 using FFDCData = std::vector<std::pair<std::string, std::string>>;
diff --git a/util.cpp b/util.cpp
new file mode 100644
index 0000000..646529a
--- /dev/null
+++ b/util.cpp
@@ -0,0 +1,47 @@
+#include "util.hpp"
+
+#include <fmt/format.h>
+
+#include <phosphor-logging/elog.hpp>
+
+#include <vector>
+
+namespace openpower
+{
+namespace util
+{
+using namespace phosphor::logging;
+
+std::string getService(sdbusplus::bus::bus& bus, const std::string& objectPath,
+                       const std::string& interface)
+{
+    constexpr auto mapperBusBame = "xyz.openbmc_project.ObjectMapper";
+    constexpr auto mapperObjectPath = "/xyz/openbmc_project/object_mapper";
+    constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper";
+    std::vector<std::pair<std::string, std::vector<std::string>>> response;
+    auto method = bus.new_method_call(mapperBusBame, mapperObjectPath,
+                                      mapperInterface, "GetObject");
+    method.append(objectPath, std::vector<std::string>({interface}));
+    try
+    {
+        auto reply = bus.call(method);
+        reply.read(response);
+    }
+    catch (const sdbusplus::exception::SdBusError& e)
+    {
+        log<level::ERR>(fmt::format("D-Bus call exception OBJPATH={}"
+                                    "INTERFACE={}  EXCEPTION={}",
+                                    mapperObjectPath, mapperInterface, e.what())
+                            .c_str());
+
+        throw std::runtime_error("Service name is not found");
+    }
+
+    if (response.empty())
+    {
+        throw std::runtime_error("Service name response is empty");
+    }
+    return response.begin()->first;
+}
+} // namespace util
+} // namespace openpower
diff --git a/util.hpp b/util.hpp
new file mode 100644
index 0000000..19a162e
--- /dev/null
+++ b/util.hpp
@@ -0,0 +1,23 @@
+#pragma once
+
+#include <sdbusplus/bus.hpp>
+
+#include <string>
+
+namespace openpower
+{
+namespace util
+{
+/**
+ * Get D-Bus service name for the specified object and interface
+ *
+ * @param[in] bus - sdbusplus D-Bus to attach to
+ * @param[in] objectPath - D-Bus object path
+ * @param[in] interface - D-Bus interface name
+ *
+ * @return service name on success and exception on failure
+ */
+std::string getService(sdbusplus::bus::bus& bus, const std::string& objectPath,
+                       const std::string& interface);
+} // namespace util
+} // namespace openpower