Use mapper to find service name

Use objectMapper to find the service name instead of using
hard-coded service name.

Change-Id: If436c65d9a9a4942eaf30ea20bc7b85e3e7694c1
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/utils.cpp b/utils.cpp
index 16cd1b4..6df6ed5 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -10,6 +10,10 @@
 
 namespace // anonymous
 {
+constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
+constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
+constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
+
 /** @brief The map that maps the string to Mode */
 const std::map<std::string, Mode> modeMap =
 {
@@ -32,6 +36,41 @@
 
 using namespace phosphor::logging;
 
+std::string getService(sdbusplus::bus::bus& bus,
+                       const char* path,
+                       const char* interface)
+{
+    auto mapper = bus.new_method_call(MAPPER_BUSNAME,
+                                      MAPPER_PATH,
+                                      MAPPER_INTERFACE,
+                                      "GetObject");
+
+    mapper.append(path, std::vector<std::string>({interface}));
+    auto mapperResponseMsg = bus.call(mapper);
+
+    if (mapperResponseMsg.is_method_error())
+    {
+        // TODO: define repo specific errors and use elog report()
+        log<level::ERR>("Error in mapper call",
+                        entry("PATH=%s", path),
+                        entry("INTERFACE=%s", interface));
+        return {};
+    }
+
+    std::map<std::string, std::vector<std::string>> mapperResponse;
+    mapperResponseMsg.read(mapperResponse);
+    if (mapperResponse.empty())
+    {
+        // TODO: define repo specific errors and use elog report()
+        log<level::ERR>("Error reading mapper response",
+                        entry("PATH=%s", path),
+                        entry("INTERFACE=%s", interface));
+        return {};
+    }
+
+    return mapperResponse.begin()->first;
+}
+
 Mode strToMode(const std::string& mode)
 {
     auto it = modeMap.find(mode);