blob: 4df1910ce4d9210711b26b94af73b26e25cde08c [file] [log] [blame]
Lei YUddd54422017-04-18 16:38:44 +08001#include "utils.hpp"
2
3#include <phosphor-logging/log.hpp>
4
Lei YUddd54422017-04-18 16:38:44 +08005namespace phosphor
6{
7namespace time
8{
9
10namespace // anonymous
11{
Lei YUdd8e9e42017-04-19 17:46:58 +080012constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
13constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
14constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050015} // namespace
Lei YUddd54422017-04-18 16:38:44 +080016
17namespace utils
18{
19
20using namespace phosphor::logging;
21
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050022std::string getService(sdbusplus::bus::bus& bus, const char* path,
Lei YUdd8e9e42017-04-19 17:46:58 +080023 const char* interface)
24{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050025 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
26 MAPPER_INTERFACE, "GetObject");
Lei YUdd8e9e42017-04-19 17:46:58 +080027
28 mapper.append(path, std::vector<std::string>({interface}));
Lei YU86c83f32018-07-13 15:14:56 +080029 try
Lei YUdd8e9e42017-04-19 17:46:58 +080030 {
Lei YU86c83f32018-07-13 15:14:56 +080031 auto mapperResponseMsg = bus.call(mapper);
Lei YUdd8e9e42017-04-19 17:46:58 +080032
Lei YU86c83f32018-07-13 15:14:56 +080033 std::vector<std::pair<std::string, std::vector<std::string>>>
34 mapperResponse;
35 mapperResponseMsg.read(mapperResponse);
36 if (mapperResponse.empty())
37 {
38 log<level::ERR>("Error reading mapper response");
39 throw std::runtime_error("Error reading mapper response");
40 }
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050041 if (mapperResponse.size() < 1)
42 {
Lei YU86c83f32018-07-13 15:14:56 +080043 return "";
44 }
45 return mapperResponse[0].first;
46 }
47 catch (const sdbusplus::exception::SdBusError& ex)
Lei YUdd8e9e42017-04-19 17:46:58 +080048 {
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050049 log<level::ERR>("Mapper call failed", entry("METHOD=%d", "GetObject"),
Lei YU86c83f32018-07-13 15:14:56 +080050 entry("PATH=%s", path),
51 entry("INTERFACE=%s", interface));
52 throw std::runtime_error("Mapper call failed");
Lei YUdd8e9e42017-04-19 17:46:58 +080053 }
Lei YUdd8e9e42017-04-19 17:46:58 +080054}
55
Lei YUddd54422017-04-18 16:38:44 +080056Mode strToMode(const std::string& mode)
57{
Lei YUad143542017-07-25 14:27:07 +080058 return ModeSetting::convertMethodFromString(mode);
Lei YUddd54422017-04-18 16:38:44 +080059}
60
61Owner strToOwner(const std::string& owner)
62{
Lei YUad143542017-07-25 14:27:07 +080063 return OwnerSetting::convertOwnersFromString(owner);
Lei YUddd54422017-04-18 16:38:44 +080064}
65
Lei YUad143542017-07-25 14:27:07 +080066std::string modeToStr(Mode mode)
Lei YUddd54422017-04-18 16:38:44 +080067{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050068 return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(
69 mode);
Lei YUddd54422017-04-18 16:38:44 +080070}
71
Lei YUad143542017-07-25 14:27:07 +080072std::string ownerToStr(Owner owner)
Lei YUddd54422017-04-18 16:38:44 +080073{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050074 return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(
75 owner);
Lei YUddd54422017-04-18 16:38:44 +080076}
77
78} // namespace utils
79} // namespace time
80} // namespace phosphor