Move the getSubTree method to utils

Since the getSubTree method is a generic method that users may use
anywhere, It is better to move this method to utils.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: Ib6e1367b9885dac38a7ba6b63329977436e70c26
diff --git a/settings.cpp b/settings.cpp
index df16aeb..50081b7 100644
--- a/settings.cpp
+++ b/settings.cpp
@@ -17,24 +17,12 @@
 
 Objects::Objects(sdbusplus::bus_t& bus) : bus(bus)
 {
-    std::vector<std::string> settingsIntfs = {timeSyncIntf};
-    auto depth = 0;
-
-    auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
-                                          "GetSubTree");
-    mapperCall.append(root);
-    mapperCall.append(depth);
-    mapperCall.append(settingsIntfs);
-
-    using Interfaces = std::vector<Interface>;
-    using MapperResponse = std::vector<
-        std::pair<Path, std::vector<std::pair<Service, Interfaces>>>>;
+    Interfaces settingsIntfs = {timeSyncIntf};
     MapperResponse result;
 
     try
     {
-        auto response = bus.call(mapperCall);
-        response.read(result);
+        result = getSubTree(bus, root, settingsIntfs, 0);
     }
     catch (const sdbusplus::exception_t& ex)
     {
diff --git a/settings.hpp b/settings.hpp
index 8e5ead5..f45c910 100644
--- a/settings.hpp
+++ b/settings.hpp
@@ -1,5 +1,7 @@
 #pragma once
 
+#include "utils.hpp"
+
 #include <sdbusplus/bus.hpp>
 
 #include <string>
@@ -7,9 +9,7 @@
 namespace settings
 {
 
-using Path = std::string;
-using Service = std::string;
-using Interface = std::string;
+using namespace phosphor::time::utils;
 
 constexpr auto root = "/";
 constexpr auto timeSyncIntf = "xyz.openbmc_project.Time.Synchronization";
diff --git a/utils.cpp b/utils.cpp
index 9bf83cb..bc95feb 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -46,6 +46,22 @@
     }
 }
 
+MapperResponse getSubTree(sdbusplus::bus_t& bus, const std::string& root,
+                          const Interfaces& interfaces, int32_t depth)
+{
+    auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
+                                          MAPPER_INTERFACE, "GetSubTree");
+    mapperCall.append(root);
+    mapperCall.append(depth);
+    mapperCall.append(interfaces);
+
+    auto response = bus.call(mapperCall);
+
+    MapperResponse result;
+    response.read(result);
+    return result;
+}
+
 Mode strToMode(const std::string& mode)
 {
     return ModeSetting::convertMethodFromString(mode);
diff --git a/utils.hpp b/utils.hpp
index 6f2fc62..dd81314 100644
--- a/utils.hpp
+++ b/utils.hpp
@@ -5,6 +5,8 @@
 #include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/bus.hpp>
 
+#include <vector>
+
 namespace phosphor
 {
 namespace time
@@ -12,6 +14,13 @@
 namespace utils
 {
 
+using Path = std::string;
+using Service = std::string;
+using Interface = std::string;
+using Interfaces = std::vector<Interface>;
+using MapperResponse =
+    std::vector<std::pair<Path, std::vector<std::pair<Service, Interfaces>>>>;
+
 /** @brief The template function to get property from the requested dbus path
  *
  * @param[in] bus          - The Dbus bus object
@@ -57,6 +66,20 @@
 std::string getService(sdbusplus::bus_t& bus, const char* path,
                        const char* interface);
 
+/** @brief Get sub tree from root, depth and interfaces
+ *
+ * @param[in] bus           - The Dbus bus object
+ * @param[in] root          - The root of the tree to search
+ * @param[in] interfaces    - All interfaces in the subtree to search for
+ * @param[in] depth         - The number of path elements to descend
+ *
+ * @return The name of the service
+ *
+ * @throw sdbusplus::exception_t when it fails
+ */
+MapperResponse getSubTree(sdbusplus::bus_t& bus, const std::string& root,
+                          const Interfaces& interfaces, int32_t depth);
+
 /** @brief Convert a string to enum Mode
  *
  * Convert the time mode string to enum.