Add the GetObject method to dbus_utility

There are currently many files that use the GetObject method.
Since they are a general method, they are defined in the
dbus_utility.hpp file and refactors them.

Tested: Built bmcweb successfully and Validator passes.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: If2af77294389b023b611987252ee6149906fcd25
diff --git a/redfish-core/lib/account_service.hpp b/redfish-core/lib/account_service.hpp
index abf255e..9f99d18 100644
--- a/redfish-core/lib/account_service.hpp
+++ b/redfish-core/lib/account_service.hpp
@@ -29,8 +29,10 @@
 #include <sdbusplus/asio/property.hpp>
 #include <sdbusplus/unpack_properties.hpp>
 
+#include <array>
 #include <optional>
 #include <string>
+#include <string_view>
 #include <vector>
 
 namespace redfish
@@ -425,11 +427,12 @@
                               CallbackFunc&& callback)
 {
 
-    const std::array<const char*, 2> interfaces = {ldapEnableInterface,
-                                                   ldapConfigInterface};
+    constexpr std::array<std::string_view, 2> interfaces = {
+        ldapEnableInterface, ldapConfigInterface};
 
-    crow::connections::systemBus->async_method_call(
-        [callback, ldapType](const boost::system::error_code ec,
+    dbus::utility::getDbusObject(
+        ldapConfigObjectName, interfaces,
+        [callback, ldapType](const boost::system::error_code& ec,
                              const dbus::utility::MapperGetObject& resp) {
         if (ec || resp.empty())
         {
@@ -580,9 +583,7 @@
             callback(true, confData, ldapType);
             },
             service, ldapRootObject, dbusObjManagerIntf, "GetManagedObjects");
-        },
-        mapperBusName, mapperObjectPath, mapperIntf, "GetObject",
-        ldapConfigObjectName, interfaces);
+        });
 }
 
 /**
diff --git a/redfish-core/lib/hypervisor_system.hpp b/redfish-core/lib/hypervisor_system.hpp
index c675be3..b558030 100644
--- a/redfish-core/lib/hypervisor_system.hpp
+++ b/redfish-core/lib/hypervisor_system.hpp
@@ -111,9 +111,12 @@
     getHypervisorActions(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
 {
     BMCWEB_LOG_DEBUG << "Get hypervisor actions.";
-    crow::connections::systemBus->async_method_call(
+    constexpr std::array<std::string_view, 1> interfaces = {
+        "xyz.openbmc_project.State.Host"};
+    dbus::utility::getDbusObject(
+        "/xyz/openbmc_project/state/hypervisor0", interfaces,
         [aResp](
-            const boost::system::error_code ec,
+            const boost::system::error_code& ec,
             const std::vector<std::pair<std::string, std::vector<std::string>>>&
                 objInfo) {
         if (ec)
@@ -146,12 +149,7 @@
             "/redfish/v1/Systems/hypervisor/Actions/ComputerSystem.Reset";
         reset["@Redfish.ActionInfo"] =
             "/redfish/v1/Systems/hypervisor/ResetActionInfo";
-        },
-        "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetObject",
-        "/xyz/openbmc_project/state/hypervisor0",
-        std::array<const char*, 1>{"xyz.openbmc_project.State.Host"});
+        });
 }
 
 inline bool extractHypervisorInterfaceData(
@@ -993,8 +991,11 @@
             return;
         }
         // Only return action info if hypervisor D-Bus object present
-        crow::connections::systemBus->async_method_call(
-            [asyncResp](const boost::system::error_code ec,
+        constexpr std::array<std::string_view, 1> interfaces = {
+            "xyz.openbmc_project.State.Host"};
+        dbus::utility::getDbusObject(
+            "/xyz/openbmc_project/state/hypervisor0", interfaces,
+            [asyncResp](const boost::system::error_code& ec,
                         const std::vector<std::pair<
                             std::string, std::vector<std::string>>>& objInfo) {
             if (ec)
@@ -1040,12 +1041,7 @@
             parameter["AllowableValues"] = std::move(allowed);
             parameters.push_back(std::move(parameter));
             asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
-            },
-            "xyz.openbmc_project.ObjectMapper",
-            "/xyz/openbmc_project/object_mapper",
-            "xyz.openbmc_project.ObjectMapper", "GetObject",
-            "/xyz/openbmc_project/state/hypervisor0",
-            std::array<const char*, 1>{"xyz.openbmc_project.State.Host"});
+            });
         });
 
     BMCWEB_ROUTE(app,
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index 1aaeaf1..315398e 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -2910,15 +2910,16 @@
 
     BMCWEB_LOG_DEBUG << "Sensor doGet enter";
 
-    const std::array<const char*, 1> interfaces = {
+    constexpr std::array<std::string_view, 1> interfaces = {
         "xyz.openbmc_project.Sensor.Value"};
     std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
                              '/' + nameType.second;
     // Get a list of all of the sensors that implement Sensor.Value
     // and get the path and service name associated with the sensor
-    crow::connections::systemBus->async_method_call(
+    ::dbus::utility::getDbusObject(
+        sensorPath, interfaces,
         [asyncResp,
-         sensorPath](const boost::system::error_code ec,
+         sensorPath](const boost::system::error_code& ec,
                      const ::dbus::utility::MapperGetObject& subtree) {
         BMCWEB_LOG_DEBUG << "respHandler1 enter";
         if (ec)
@@ -2930,11 +2931,7 @@
         }
         getSensorFromDbus(asyncResp, sensorPath, subtree);
         BMCWEB_LOG_DEBUG << "respHandler1 exit";
-        },
-        "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetObject", sensorPath,
-        interfaces);
+        });
 }
 
 } // namespace sensors
diff --git a/redfish-core/lib/update_service.hpp b/redfish-core/lib/update_service.hpp
index a9174a1..a8f0a6c 100644
--- a/redfish-core/lib/update_service.hpp
+++ b/redfish-core/lib/update_service.hpp
@@ -28,6 +28,9 @@
 #include <sdbusplus/bus/match.hpp>
 #include <sdbusplus/unpack_properties.hpp>
 
+#include <array>
+#include <string_view>
+
 namespace redfish
 {
 
@@ -87,9 +90,12 @@
         if (interface.first == "xyz.openbmc_project.Software.Activation")
         {
             // Retrieve service and activate
-            crow::connections::systemBus->async_method_call(
+            constexpr std::array<std::string_view, 1> interfaces = {
+                "xyz.openbmc_project.Software.Activation"};
+            dbus::utility::getDbusObject(
+                objPath.str, interfaces,
                 [objPath, asyncResp, payload(std::move(payload))](
-                    const boost::system::error_code errorCode,
+                    const boost::system::error_code& errorCode,
                     const std::vector<
                         std::pair<std::string, std::vector<std::string>>>&
                         objInfo) mutable {
@@ -246,12 +252,7 @@
                     task->payload.emplace(std::move(payload));
                 }
                 fwUpdateInProgress = false;
-                },
-                "xyz.openbmc_project.ObjectMapper",
-                "/xyz/openbmc_project/object_mapper",
-                "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str,
-                std::array<const char*, 1>{
-                    "xyz.openbmc_project.Software.Activation"});
+                });
 
             break;
         }
diff --git a/redfish-core/lib/virtual_media.hpp b/redfish-core/lib/virtual_media.hpp
index 7c53f7e..45f384b 100644
--- a/redfish-core/lib/virtual_media.hpp
+++ b/redfish-core/lib/virtual_media.hpp
@@ -17,6 +17,7 @@
 
 #include "account_service.hpp"
 #include "app.hpp"
+#include "dbus_utility.hpp"
 #include "query.hpp"
 #include "registries/privilege_registry.hpp"
 #include "utils/json_utils.hpp"
@@ -25,6 +26,9 @@
 #include <boost/type_traits/has_dereference.hpp>
 #include <boost/url/url_view.hpp>
 
+#include <array>
+#include <string_view>
+
 namespace redfish
 {
 /**
@@ -797,9 +801,10 @@
         return;
     }
 
-    crow::connections::systemBus->async_method_call(
+    dbus::utility::getDbusObject(
+        "/xyz/openbmc_project/VirtualMedia", {},
         [asyncResp, actionParams,
-         resName](const boost::system::error_code ec,
+         resName](const boost::system::error_code& ec,
                   const dbus::utility::MapperGetObject& getObjectType) mutable {
         if (ec)
         {
@@ -873,11 +878,7 @@
             },
             service, "/xyz/openbmc_project/VirtualMedia",
             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
-        },
-        "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetObject",
-        "/xyz/openbmc_project/VirtualMedia", std::array<const char*, 0>());
+        });
 }
 
 inline void handleManagersVirtualMediaActionEject(
@@ -897,9 +898,10 @@
         return;
     }
 
-    crow::connections::systemBus->async_method_call(
+    dbus::utility::getDbusObject(
+        "/xyz/openbmc_project/VirtualMedia", {},
         [asyncResp,
-         resName](const boost::system::error_code ec2,
+         resName](const boost::system::error_code& ec2,
                   const dbus::utility::MapperGetObject& getObjectType) {
         if (ec2)
         {
@@ -960,11 +962,7 @@
             },
             service, "/xyz/openbmc_project/VirtualMedia",
             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
-        },
-        "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetObject",
-        "/xyz/openbmc_project/VirtualMedia", std::array<const char*, 0>());
+        });
 }
 
 inline void handleManagersVirtualMediaCollectionGet(
@@ -989,8 +987,9 @@
     asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
         "redfish", "v1", "Managers", name, "VirtualMedia");
 
-    crow::connections::systemBus->async_method_call(
-        [asyncResp, name](const boost::system::error_code ec,
+    dbus::utility::getDbusObject(
+        "/xyz/openbmc_project/VirtualMedia", {},
+        [asyncResp, name](const boost::system::error_code& ec,
                           const dbus::utility::MapperGetObject& getObjectType) {
         if (ec)
         {
@@ -1003,11 +1002,7 @@
         BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
 
         getVmResourceList(asyncResp, service, name);
-        },
-        "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetObject",
-        "/xyz/openbmc_project/VirtualMedia", std::array<const char*, 0>());
+        });
 }
 
 inline void
@@ -1026,9 +1021,10 @@
         return;
     }
 
-    crow::connections::systemBus->async_method_call(
+    dbus::utility::getDbusObject(
+        "/xyz/openbmc_project/VirtualMedia", {},
         [asyncResp, name,
-         resName](const boost::system::error_code ec,
+         resName](const boost::system::error_code& ec,
                   const dbus::utility::MapperGetObject& getObjectType) {
         if (ec)
         {
@@ -1041,11 +1037,7 @@
         BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
 
         getVmData(asyncResp, service, name, resName);
-        },
-        "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetObject",
-        "/xyz/openbmc_project/VirtualMedia", std::array<const char*, 0>());
+        });
 }
 
 inline void requestNBDVirtualMediaRoutes(App& app)