blob: 646529ae9a51bc7016fcf9a6861d11c1d14ec50d [file] [log] [blame]
#include "util.hpp"
#include <fmt/format.h>
#include <phosphor-logging/elog.hpp>
#include <vector>
namespace openpower
{
namespace util
{
using namespace phosphor::logging;
std::string getService(sdbusplus::bus::bus& bus, const std::string& objectPath,
const std::string& interface)
{
constexpr auto mapperBusBame = "xyz.openbmc_project.ObjectMapper";
constexpr auto mapperObjectPath = "/xyz/openbmc_project/object_mapper";
constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper";
std::vector<std::pair<std::string, std::vector<std::string>>> response;
auto method = bus.new_method_call(mapperBusBame, mapperObjectPath,
mapperInterface, "GetObject");
method.append(objectPath, std::vector<std::string>({interface}));
try
{
auto reply = bus.call(method);
reply.read(response);
}
catch (const sdbusplus::exception::SdBusError& e)
{
log<level::ERR>(fmt::format("D-Bus call exception OBJPATH={}"
"INTERFACE={} EXCEPTION={}",
mapperObjectPath, mapperInterface, e.what())
.c_str());
throw std::runtime_error("Service name is not found");
}
if (response.empty())
{
throw std::runtime_error("Service name response is empty");
}
return response.begin()->first;
}
} // namespace util
} // namespace openpower