1 #include "util.hpp"
2 
3 #include <fmt/format.h>
4 
5 #include <phosphor-logging/elog.hpp>
6 
7 #include <vector>
8 
9 namespace openpower
10 {
11 namespace util
12 {
13 using namespace phosphor::logging;
14 
15 std::string getService(sdbusplus::bus::bus& bus, const std::string& objectPath,
16                        const std::string& interface)
17 {
18     constexpr auto mapperBusBame = "xyz.openbmc_project.ObjectMapper";
19     constexpr auto mapperObjectPath = "/xyz/openbmc_project/object_mapper";
20     constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper";
21     std::vector<std::pair<std::string, std::vector<std::string>>> response;
22     auto method = bus.new_method_call(mapperBusBame, mapperObjectPath,
23                                       mapperInterface, "GetObject");
24     method.append(objectPath, std::vector<std::string>({interface}));
25     try
26     {
27         auto reply = bus.call(method);
28         reply.read(response);
29     }
30     catch (const sdbusplus::exception::exception& e)
31     {
32         log<level::ERR>(fmt::format("D-Bus call exception OBJPATH={}"
33                                     "INTERFACE={}  EXCEPTION={}",
34                                     mapperObjectPath, mapperInterface, e.what())
35                             .c_str());
36 
37         throw std::runtime_error("Service name is not found");
38     }
39 
40     if (response.empty())
41     {
42         throw std::runtime_error("Service name response is empty");
43     }
44     return response.begin()->first;
45 }
46 } // namespace util
47 } // namespace openpower
48