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/include/dbus_utility.hpp b/include/dbus_utility.hpp
index c7fda6d..c9143de 100644
--- a/include/dbus_utility.hpp
+++ b/include/dbus_utility.hpp
@@ -175,5 +175,21 @@
         interfaces);
 }
 
+inline void
+    getDbusObject(const std::string& path,
+                  std::span<const std::string_view> interfaces,
+                  std::function<void(const boost::system::error_code&,
+                                     const MapperGetObject&)>&& callback)
+{
+    crow::connections::systemBus->async_method_call(
+        [callback{std::move(callback)}](const boost::system::error_code& ec,
+                                        const MapperGetObject& object) {
+        callback(ec, object);
+        },
+        "xyz.openbmc_project.ObjectMapper",
+        "/xyz/openbmc_project/object_mapper",
+        "xyz.openbmc_project.ObjectMapper", "GetObject", path, interfaces);
+}
+
 } // namespace utility
 } // namespace dbus
diff --git a/include/openbmc_dbus_rest.hpp b/include/openbmc_dbus_rest.hpp
index 61eaa6e..948bc36 100644
--- a/include/openbmc_dbus_rest.hpp
+++ b/include/openbmc_dbus_rest.hpp
@@ -406,8 +406,9 @@
 inline void getObjectAndEnumerate(
     const std::shared_ptr<InProgressEnumerateData>& transaction)
 {
-    crow::connections::systemBus->async_method_call(
-        [transaction](const boost::system::error_code ec,
+    dbus::utility::getDbusObject(
+        transaction->objectPath, {},
+        [transaction](const boost::system::error_code& ec,
                       const dbus::utility::MapperGetObject& objects) {
         if (ec)
         {
@@ -466,11 +467,7 @@
                     transaction->objectPath, connection.first, transaction);
             }
         }
-        },
-        "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetObject",
-        transaction->objectPath, std::array<const char*, 0>());
+        });
 }
 
 // Structure for storing data on an in progress action
@@ -1571,9 +1568,10 @@
     transaction->path = objectPath;
     transaction->methodName = methodName;
     transaction->arguments = std::move(*data);
-    crow::connections::systemBus->async_method_call(
+    dbus::utility::getDbusObject(
+        objectPath, {},
         [transaction](
-            const boost::system::error_code ec,
+            const boost::system::error_code& ec,
             const std::vector<std::pair<std::string, std::vector<std::string>>>&
                 interfaceNames) {
         if (ec || interfaceNames.empty())
@@ -1593,11 +1591,7 @@
         {
             findActionOnInterface(transaction, object.first);
         }
-        },
-        "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
-        std::array<std::string, 0>());
+        });
 }
 
 inline void handleDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
@@ -1605,9 +1599,10 @@
 {
     BMCWEB_LOG_DEBUG << "handleDelete on path: " << objectPath;
 
-    crow::connections::systemBus->async_method_call(
+    dbus::utility::getDbusObject(
+        objectPath, {},
         [asyncResp, objectPath](
-            const boost::system::error_code ec,
+            const boost::system::error_code& ec,
             const std::vector<std::pair<std::string, std::vector<std::string>>>&
                 interfaceNames) {
         if (ec || interfaceNames.empty())
@@ -1630,11 +1625,7 @@
         {
             findActionOnInterface(transaction, object.first);
         }
-        },
-        "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
-        std::array<const char*, 0>());
+        });
 }
 
 inline void handleList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
@@ -1710,9 +1701,10 @@
     std::shared_ptr<std::string> path =
         std::make_shared<std::string>(std::move(objectPath));
 
-    crow::connections::systemBus->async_method_call(
+    dbus::utility::getDbusObject(
+        *path, {},
         [asyncResp, path,
-         propertyName](const boost::system::error_code ec,
+         propertyName](const boost::system::error_code& ec,
                        const dbus::utility::MapperGetObject& objectNames) {
         if (ec || objectNames.empty())
         {
@@ -1801,11 +1793,7 @@
                     });
             }
         }
-        },
-        "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetObject", *path,
-        std::array<std::string, 0>());
+        });
 }
 
 struct AsyncPutRequest
@@ -1878,8 +1866,9 @@
     transaction->propertyName = destProperty;
     transaction->propertyValue = propertySetValue;
 
-    crow::connections::systemBus->async_method_call(
-        [transaction](const boost::system::error_code ec2,
+    dbus::utility::getDbusObject(
+        transaction->objectPath, {},
+        [transaction](const boost::system::error_code& ec2,
                       const dbus::utility::MapperGetObject& objectNames) {
         if (!ec2 && objectNames.empty())
         {
@@ -2016,11 +2005,7 @@
                 connectionName, transaction->objectPath,
                 "org.freedesktop.DBus.Introspectable", "Introspect");
         }
-        },
-        "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetObject",
-        transaction->objectPath, std::array<std::string, 0>());
+        });
 }
 
 inline void handleDBusUrl(const crow::Request& req,
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)