utils: Add the getSubTree method

The purpose of this patch is to prevent other methods from calling
ObjectMapper's `GetSubTree` property, but to obtain the correct the
objectTree values by calling the standard the getSubTree method.

Signed-off-by: George Liu <liuxiwei@ieisystem.com>
Change-Id: Ia719f92ca31fa75f83a7ffd07fdcb680bd4243b0
diff --git a/include/ipmid/utils.hpp b/include/ipmid/utils.hpp
index 1e2005f..e47c85d 100644
--- a/include/ipmid/utils.hpp
+++ b/include/ipmid/utils.hpp
@@ -102,6 +102,16 @@
 std::string getService(sdbusplus::bus_t& bus, const std::string& intf,
                        const std::string& path);
 
+/** @brief Gets the dbus sub tree implementing the given interface.
+ *  @param[in] bus - DBUS Bus Object.
+ *  @param[in] interfaces - Dbus interface.
+ *  @param[in] subtreePath - subtree from where the search should start.
+ *  @param[in] depth - Search depth
+ *  @return map of object path and service info.
+ */
+ObjectTree getSubTree(sdbusplus::bus_t& bus, const InterfaceList& interface,
+                      const std::string& subtreePath = ROOT, int32_t depth = 0);
+
 /** @brief Gets the dbus object info implementing the given interface
  *         from the given subtree.
  *  @param[in] bus - DBUS Bus Object.
@@ -216,6 +226,20 @@
                                      const std::string& path,
                                      std::string& service);
 
+/** @brief Gets the dbus sub tree implementing the given interface.
+ *  @param[in] ctx - ipmi::Context::ptr
+ *  @param[in] bus - DBUS Bus Object.
+ *  @param[in] interfaces - Dbus interface.
+ *  @param[in] subtreePath - subtree from where the search should start.
+ *  @param[in] depth - Search depth
+ *  @param[out] objectTree - map of object path and service info.
+ *  @return map of object path and service info.
+ */
+boost::system::error_code getSubTree(Context::ptr ctx,
+                                     const InterfaceList& interface,
+                                     const std::string& subtreePath,
+                                     int32_t depth, ObjectTree& objectTree);
+
 /** @brief Gets the D-Bus object info implementing the given interface
  *         from the given subtree.
  *  @param[in] ctx - ipmi::Context::ptr
diff --git a/libipmid/utils.cpp b/libipmid/utils.cpp
index f7e9802..81cc4c4 100644
--- a/libipmid/utils.cpp
+++ b/libipmid/utils.cpp
@@ -47,18 +47,7 @@
     std::vector<DbusInterface> interfaces;
     interfaces.emplace_back(interface);
 
-    auto depth = 0;
-
-    auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ,
-                                          MAPPER_INTF, "GetSubTree");
-
-    mapperCall.append(serviceRoot, depth, interfaces);
-
-    auto mapperReply = bus.call(mapperCall);
-
-    ObjectTree objectTree;
-    mapperReply.read(objectTree);
-
+    ObjectTree objectTree = getSubTree(bus, interfaces, serviceRoot);
     if (objectTree.empty())
     {
         log<level::ERR>("No Object has implemented the interface",
@@ -229,6 +218,21 @@
     return mapperResponse.begin()->first;
 }
 
+ObjectTree getSubTree(sdbusplus::bus_t& bus, const InterfaceList& interfaces,
+                      const std::string& subtreePath, int32_t depth)
+{
+    auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ,
+                                          MAPPER_INTF, "GetSubTree");
+
+    mapperCall.append(subtreePath, depth, interfaces);
+
+    auto mapperReply = bus.call(mapperCall);
+    ObjectTree objectTree;
+    mapperReply.read(objectTree);
+
+    return objectTree;
+}
+
 ipmi::ObjectTree getAllDbusObjects(sdbusplus::bus_t& bus,
                                    const std::string& serviceRoot,
                                    const std::string& interface,
@@ -237,17 +241,7 @@
     std::vector<std::string> interfaces;
     interfaces.emplace_back(interface);
 
-    auto depth = 0;
-
-    auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ,
-                                          MAPPER_INTF, "GetSubTree");
-
-    mapperCall.append(serviceRoot, depth, interfaces);
-
-    auto mapperReply = bus.call(mapperCall);
-    ObjectTree objectTree;
-    mapperReply.read(objectTree);
-
+    ObjectTree objectTree = getSubTree(bus, interfaces, serviceRoot);
     for (auto it = objectTree.begin(); it != objectTree.end();)
     {
         if (it->first.find(match) == std::string::npos)
@@ -356,6 +350,19 @@
     return ec;
 }
 
+boost::system::error_code getSubTree(Context::ptr ctx,
+                                     const InterfaceList& interfaces,
+                                     const std::string& subtreePath,
+                                     int32_t depth, ObjectTree& objectTree)
+{
+    boost::system::error_code ec;
+    objectTree = ctx->bus->yield_method_call<ObjectTree>(
+        ctx->yield, ec, MAPPER_BUS_NAME, MAPPER_OBJ, MAPPER_INTF, "GetSubTree",
+        subtreePath, depth, interfaces);
+
+    return ec;
+}
+
 boost::system::error_code getDbusObject(Context::ptr ctx,
                                         const std::string& interface,
                                         const std::string& subtreePath,
@@ -366,10 +373,9 @@
     interfaces.emplace_back(interface);
 
     auto depth = 0;
-    boost::system::error_code ec;
-    ObjectTree objectTree = ctx->bus->yield_method_call<ObjectTree>(
-        ctx->yield, ec, MAPPER_BUS_NAME, MAPPER_OBJ, MAPPER_INTF, "GetSubTree",
-        subtreePath, depth, interfaces);
+    ObjectTree objectTree;
+    boost::system::error_code ec = getSubTree(ctx, interfaces, subtreePath,
+                                              depth, objectTree);
 
     if (ec)
     {
@@ -448,16 +454,12 @@
                                             const std::string& match,
                                             ObjectTree& objectTree)
 {
-    boost::system::error_code ec;
     std::vector<std::string> interfaces;
     interfaces.emplace_back(interface);
 
     auto depth = 0;
-
-    objectTree = ctx->bus->yield_method_call<ObjectTree>(
-        ctx->yield, ec, MAPPER_BUS_NAME, MAPPER_OBJ, MAPPER_INTF, "GetSubTree",
-        serviceRoot, depth, interfaces);
-
+    boost::system::error_code ec = getSubTree(ctx, interfaces, serviceRoot,
+                                              depth, objectTree);
     if (ec)
     {
         return ec;