Refactor GetSubTree method

Since the GetSubTree method has been implemented in dbus_utility and
this commit is to integrate all the places where the GetSubTree
method is called, and use the method in dbus_utility uniformly.

Tested: Redfish Validator Passed

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: If3852b487d74e7cd8f123e0efffbd4affe92743c
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index 315398e..6be9e86 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -31,6 +31,7 @@
 #include <boost/algorithm/string/replace.hpp>
 #include <boost/algorithm/string/split.hpp>
 #include <boost/range/algorithm/replace_copy_if.hpp>
+#include <boost/system/error_code.hpp>
 #include <sdbusplus/asio/property.hpp>
 #include <sdbusplus/unpack_properties.hpp>
 
@@ -354,14 +355,16 @@
 {
     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
     const std::string path = "/xyz/openbmc_project/sensors";
-    const std::array<std::string, 1> interfaces = {
+    constexpr std::array<std::string_view, 1> interfaces = {
         "xyz.openbmc_project.Sensor.Value"};
 
-    // Response handler for parsing objects subtree
-    auto respHandler =
+    // Make call to ObjectMapper to find all sensors objects
+    dbus::utility::getSubTree(
+        path, 2, interfaces,
         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
-         sensorNames](const boost::system::error_code ec,
+         sensorNames](const boost::system::error_code& ec,
                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
+        // Response handler for parsing objects subtree
         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
         if (ec)
         {
@@ -404,12 +407,7 @@
         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
         callback(std::move(connections), std::move(objectsWithConnection));
         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
-    };
-    // Make call to ObjectMapper to find all sensors objects
-    crow::connections::systemBus->async_method_call(
-        std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
+        });
     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
 }
 
@@ -975,9 +973,12 @@
 inline void populateFanRedundancy(
     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
 {
-    crow::connections::systemBus->async_method_call(
+    constexpr std::array<std::string_view, 1> interfaces = {
+        "xyz.openbmc_project.Control.FanRedundancy"};
+    dbus::utility::getSubTree(
+        "/xyz/openbmc_project/control", 2, interfaces,
         [sensorsAsyncResp](
-            const boost::system::error_code ec,
+            const boost::system::error_code& ec,
             const dbus::utility::MapperGetSubTreeResponse& resp) {
         if (ec)
         {
@@ -1144,13 +1145,7 @@
                     });
                 });
         }
-        },
-        "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetSubTree",
-        "/xyz/openbmc_project/control", 2,
-        std::array<const char*, 1>{
-            "xyz.openbmc_project.Control.FanRedundancy"});
+        });
 }
 
 inline void
@@ -1520,18 +1515,20 @@
     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
 
     const std::string path = "/xyz/openbmc_project/inventory";
-    const std::array<std::string, 4> interfaces = {
+    constexpr std::array<std::string_view, 4> interfaces = {
         "xyz.openbmc_project.Inventory.Item",
         "xyz.openbmc_project.Inventory.Item.PowerSupply",
         "xyz.openbmc_project.Inventory.Decorator.Asset",
         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
 
-    // Response handler for parsing output from GetSubTree
-    auto respHandler =
+    // Make call to ObjectMapper to find all inventory items
+    dbus::utility::getSubTree(
+        path, 0, interfaces,
         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
          inventoryItems](
-            const boost::system::error_code ec,
+            const boost::system::error_code& ec,
             const dbus::utility::MapperGetSubTreeResponse& subtree) {
+        // Response handler for parsing output from GetSubTree
         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
         if (ec)
         {
@@ -1567,13 +1564,7 @@
 
         callback(invConnections);
         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
-    };
-
-    // Make call to ObjectMapper to find all inventory items
-    crow::connections::systemBus->async_method_call(
-        std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
+        });
     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
 }
 
@@ -1866,15 +1857,17 @@
     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
 
     const std::string path = "/xyz/openbmc_project";
-    const std::array<std::string, 1> interfaces = {
+    constexpr std::array<std::string_view, 1> interfaces = {
         "xyz.openbmc_project.Led.Physical"};
 
-    // Response handler for parsing output from GetSubTree
-    auto respHandler =
+    // Make call to ObjectMapper to find all inventory items
+    dbus::utility::getSubTree(
+        path, 0, interfaces,
         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
          inventoryItems](
-            const boost::system::error_code ec,
+            const boost::system::error_code& ec,
             const dbus::utility::MapperGetSubTreeResponse& subtree) {
+        // Response handler for parsing output from GetSubTree
         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
         if (ec)
         {
@@ -1910,12 +1903,7 @@
         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
                             std::move(callback));
         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
-    };
-    // Make call to ObjectMapper to find all inventory items
-    crow::connections::systemBus->async_method_call(
-        std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
+        });
     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
 }
 
@@ -2043,15 +2031,17 @@
         return;
     }
 
-    const std::array<std::string, 1> interfaces = {
+    constexpr std::array<std::string_view, 1> interfaces = {
         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
 
-    // Response handler for parsing output from GetSubTree
-    auto respHandler =
+    // Make call to ObjectMapper to find the PowerSupplyAttributes service
+    dbus::utility::getSubTree(
+        "/xyz/openbmc_project", 0, interfaces,
         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
          inventoryItems](
-            const boost::system::error_code ec,
+            const boost::system::error_code& ec,
             const dbus::utility::MapperGetSubTreeResponse& subtree) {
+        // Response handler for parsing output from GetSubTree
         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
         if (ec)
         {
@@ -2097,13 +2087,7 @@
                                      psAttributesConnections,
                                      std::move(callback));
         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
-    };
-    // Make call to ObjectMapper to find the PowerSupplyAttributes service
-    crow::connections::systemBus->async_method_call(
-        std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetSubTree",
-        "/xyz/openbmc_project", 0, interfaces);
+        });
     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
 }