pldm: Refactor DBusHandler interface

Add a static method to get the sdbusplus::bus in DBusHandler class and
instead of duplicate calls to sdbusplus::bus::new_default().

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I4a4ca84db7c301c9a82099de3b0361bbe9fa8c4c
diff --git a/libpldmresponder/bios.cpp b/libpldmresponder/bios.cpp
index 6776038..8b18333 100644
--- a/libpldmresponder/bios.cpp
+++ b/libpldmresponder/bios.cpp
@@ -124,22 +124,13 @@
     auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
     std::variant<EpochTimeUS> value;
 
-    auto bus = sdbusplus::bus::new_default();
     try
     {
-
-        auto service =
-            pldm::utils::getService(bus, hostTimePath, timeInterface);
-
-        auto method = bus.new_method_call(service.c_str(), hostTimePath,
-                                          dbusProperties, "Get");
-        method.append(timeInterface, "Elapsed");
-
-        auto reply = bus.call(method);
-        reply.read(value);
+        value = pldm::utils::DBusHandler()
+                    .getDbusProperty<std::variant<EpochTimeUS>>(
+                        hostTimePath, "Elapsed", timeInterface);
     }
-
-    catch (std::exception& e)
+    catch (const sdbusplus::exception::SdBusError& e)
     {
         std::cerr << "Error getting time, PATH=" << hostTimePath
                   << " TIME INTERACE=" << timeInterface << "\n";
diff --git a/libpldmresponder/bios_parser.cpp b/libpldmresponder/bios_parser.cpp
index 062a342..f9ddf46 100644
--- a/libpldmresponder/bios_parser.cpp
+++ b/libpldmresponder/bios_parser.cpp
@@ -234,7 +234,6 @@
             .getDbusPropertyVariant<internal::PropertyValue>(
                 dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(),
                 dBusMap->interface.c_str());
-
     auto iter = dbusValToValMap.find(propValue);
     if (iter != dbusValToValMap.end())
     {
diff --git a/oem/ibm/libpldmresponder/file_io_type_pel.cpp b/oem/ibm/libpldmresponder/file_io_type_pel.cpp
index ce9202a..3b43856 100644
--- a/oem/ibm/libpldmresponder/file_io_type_pel.cpp
+++ b/oem/ibm/libpldmresponder/file_io_type_pel.cpp
@@ -30,11 +30,12 @@
     static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
     static constexpr auto logInterface = "org.open_power.Logging.PEL";
 
-    static sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
+    auto& bus = pldm::utils::DBusHandler::getBus();
 
     try
     {
-        auto service = pldm::utils::getService(bus, logObjPath, logInterface);
+        auto service =
+            pldm::utils::DBusHandler().getService(logObjPath, logInterface);
         auto method = bus.new_method_call(service.c_str(), logObjPath,
                                           logInterface, "GetPEL");
         method.append(fileHandle);
@@ -58,11 +59,12 @@
 {
     static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
     static constexpr auto logInterface = "org.open_power.Logging.PEL";
-    static sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
+    auto& bus = pldm::utils::DBusHandler::getBus();
 
     try
     {
-        auto service = pldm::utils::getService(bus, logObjPath, logInterface);
+        auto service =
+            pldm::utils::DBusHandler().getService(logObjPath, logInterface);
         auto method = bus.new_method_call(service.c_str(), logObjPath,
                                           logInterface, "GetPEL");
         method.append(fileHandle);
@@ -145,11 +147,12 @@
 {
     static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
     static constexpr auto logInterface = "org.open_power.Logging.PEL";
-    static sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
+    auto& bus = pldm::utils::DBusHandler::getBus();
 
     try
     {
-        auto service = pldm::utils::getService(bus, logObjPath, logInterface);
+        auto service =
+            pldm::utils::DBusHandler().getService(logObjPath, logInterface);
         auto method = bus.new_method_call(service.c_str(), logObjPath,
                                           logInterface, "HostAck");
         method.append(fileHandle);
@@ -169,11 +172,12 @@
     static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
     static constexpr auto logInterface = "xyz.openbmc_project.Logging.Create";
 
-    static sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
+    auto& bus = pldm::utils::DBusHandler::getBus();
 
     try
     {
-        auto service = pldm::utils::getService(bus, logObjPath, logInterface);
+        auto service =
+            pldm::utils::DBusHandler().getService(logObjPath, logInterface);
         using namespace sdbusplus::xyz::openbmc_project::Logging::server;
         std::map<std::string, std::string> addlData{};
         addlData.emplace("RAWPEL", std::move(pelFileName));
diff --git a/pldmd.cpp b/pldmd.cpp
index dae5182..648ae6c 100644
--- a/pldmd.cpp
+++ b/pldmd.cpp
@@ -190,7 +190,7 @@
         exit(EXIT_FAILURE);
     }
 
-    auto bus = sdbusplus::bus::new_default();
+    auto& bus = pldm::utils::DBusHandler::getBus();
     dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
     auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd,
                                                       uint32_t revents) {
diff --git a/utils.cpp b/utils.cpp
index e664099..a69abe6 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -89,27 +89,19 @@
     return true;
 }
 
-std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
-                       const std::string& interface)
+std::string DBusHandler::getService(const char* path,
+                                    const char* interface) const
 {
     using DbusInterfaceList = std::vector<std::string>;
     std::map<std::string, std::vector<std::string>> mapperResponse;
+    auto& bus = DBusHandler::getBus();
 
-    try
-    {
-        auto mapper = bus.new_method_call(mapperBusName, mapperPath,
-                                          mapperInterface, "GetObject");
-        mapper.append(path, DbusInterfaceList({interface}));
+    auto mapper = bus.new_method_call(mapperBusName, mapperPath,
+                                      mapperInterface, "GetObject");
+    mapper.append(path, DbusInterfaceList({interface}));
 
-        auto mapperResponseMsg = bus.call(mapper);
-        mapperResponseMsg.read(mapperResponse);
-    }
-    catch (std::exception& e)
-    {
-        std::cerr << "gete dbus service, PATH=" << path
-                  << " INTERFACE=" << interface << e.what() << "\n";
-        throw;
-    }
+    auto mapperResponseMsg = bus.call(mapper);
+    mapperResponseMsg.read(mapperResponse);
     return mapperResponse.begin()->first;
 }
 
@@ -118,11 +110,11 @@
     static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
     static constexpr auto logInterface = "xyz.openbmc_project.Logging.Create";
 
-    static sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
+    auto& bus = pldm::utils::DBusHandler::getBus();
 
     try
     {
-        auto service = getService(bus, logObjPath, logInterface);
+        auto service = DBusHandler().getService(logObjPath, logInterface);
         using namespace sdbusplus::xyz::openbmc_project::Logging::server;
         auto severity =
             sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
diff --git a/utils.hpp b/utils.hpp
index bbdbc38..fecf12e 100644
--- a/utils.hpp
+++ b/utils.hpp
@@ -86,16 +86,6 @@
                         std::vector<set_effecter_state_field>& stateField);
 
 /**
- *  @brief Get the DBUS Service name for the input dbus path
- *  @param[in] bus - DBUS Bus Object
- *  @param[in] path - DBUS object path
- *  @param[in] interface - DBUS Interface
- *  @return std::string - the dbus service name
- */
-std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
-                       const std::string& interface);
-
-/**
  *  @brief creates an error log
  *  @param[in] errorMsg - the error message
  */
@@ -138,6 +128,21 @@
 class DBusHandler
 {
   public:
+    /** @brief Get the bus connection. */
+    static auto& getBus()
+    {
+        static auto bus = sdbusplus::bus::new_default();
+        return bus;
+    }
+
+    /**
+     *  @brief Get the DBUS Service name for the input dbus path
+     *  @param[in] path - DBUS object path
+     *  @param[in] interface - DBUS Interface
+     *  @return std::string - the dbus service name
+     */
+    std::string getService(const char* path, const char* interface) const;
+
     /** @brief API to set a D-Bus property
      *
      *  @param[in] objPath - Object path for the D-Bus object
@@ -151,8 +156,8 @@
                          const char* dbusInterface,
                          const std::variant<T>& value) const
     {
-        auto bus = sdbusplus::bus::new_default();
-        auto service = getService(bus, objPath, dbusInterface);
+        auto& bus = DBusHandler::getBus();
+        auto service = getService(objPath, dbusInterface);
         auto method = bus.new_method_call(service.c_str(), objPath,
                                           dbusProperties, "Set");
         method.append(dbusInterface, dbusProp, value);
@@ -164,23 +169,14 @@
                                 const char* dbusInterface)
     {
         Variant value;
-        auto bus = sdbusplus::bus::new_default();
-        auto service = getService(bus, objPath, dbusInterface);
+        auto& bus = DBusHandler::getBus();
+        auto service = getService(objPath, dbusInterface);
         auto method = bus.new_method_call(service.c_str(), objPath,
                                           dbusProperties, "Get");
         method.append(dbusInterface, dbusProp);
-        try
-        {
-            auto reply = bus.call(method);
-            reply.read(value);
-        }
-        catch (const sdbusplus::exception::SdBusError& e)
-        {
-            std::cerr << "dbus call exception, OBJPATH=" << objPath
-                      << " INTERFACE=" << dbusInterface
-                      << " PROPERTY=" << dbusProp << " EXCEPTION=" << e.what()
-                      << "\n";
-        }
+        auto reply = bus.call(method);
+        reply.read(value);
+
         return value;
     }