blob: 48bae2207b4ba7a34d0872fd5d26f4c60af47617 [file] [log] [blame]
Lei YUddd54422017-04-18 16:38:44 +08001#include "utils.hpp"
2
3#include <phosphor-logging/log.hpp>
4
5
6namespace phosphor
7{
8namespace time
9{
10
11namespace // anonymous
12{
Lei YUdd8e9e42017-04-19 17:46:58 +080013constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
14constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
15constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
Lei YUddd54422017-04-18 16:38:44 +080016}
17
18namespace utils
19{
20
21using namespace phosphor::logging;
22
Lei YUdd8e9e42017-04-19 17:46:58 +080023std::string getService(sdbusplus::bus::bus& bus,
24 const char* path,
25 const char* interface)
26{
27 auto mapper = bus.new_method_call(MAPPER_BUSNAME,
28 MAPPER_PATH,
29 MAPPER_INTERFACE,
30 "GetObject");
31
32 mapper.append(path, std::vector<std::string>({interface}));
Lei YU86c83f32018-07-13 15:14:56 +080033 try
Lei YUdd8e9e42017-04-19 17:46:58 +080034 {
Lei YU86c83f32018-07-13 15:14:56 +080035 auto mapperResponseMsg = bus.call(mapper);
Lei YUdd8e9e42017-04-19 17:46:58 +080036
Lei YU86c83f32018-07-13 15:14:56 +080037 std::vector<std::pair<std::string, std::vector<std::string>>>
38 mapperResponse;
39 mapperResponseMsg.read(mapperResponse);
40 if (mapperResponse.empty())
41 {
42 log<level::ERR>("Error reading mapper response");
43 throw std::runtime_error("Error reading mapper response");
44 }
45 if (mapperResponse.size() < 1){
46 return "";
47 }
48 return mapperResponse[0].first;
49 }
50 catch (const sdbusplus::exception::SdBusError& ex)
Lei YUdd8e9e42017-04-19 17:46:58 +080051 {
Lei YU86c83f32018-07-13 15:14:56 +080052 log<level::ERR>("Mapper call failed",
53 entry("METHOD=%d", "GetObject"),
54 entry("PATH=%s", path),
55 entry("INTERFACE=%s", interface));
56 throw std::runtime_error("Mapper call failed");
Lei YUdd8e9e42017-04-19 17:46:58 +080057 }
Lei YUdd8e9e42017-04-19 17:46:58 +080058}
59
Lei YUddd54422017-04-18 16:38:44 +080060Mode strToMode(const std::string& mode)
61{
Lei YUad143542017-07-25 14:27:07 +080062 return ModeSetting::convertMethodFromString(mode);
Lei YUddd54422017-04-18 16:38:44 +080063}
64
65Owner strToOwner(const std::string& owner)
66{
Lei YUad143542017-07-25 14:27:07 +080067 return OwnerSetting::convertOwnersFromString(owner);
Lei YUddd54422017-04-18 16:38:44 +080068}
69
Lei YUad143542017-07-25 14:27:07 +080070std::string modeToStr(Mode mode)
Lei YUddd54422017-04-18 16:38:44 +080071{
Lei YUad143542017-07-25 14:27:07 +080072 return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(mode);
Lei YUddd54422017-04-18 16:38:44 +080073}
74
Lei YUad143542017-07-25 14:27:07 +080075std::string ownerToStr(Owner owner)
Lei YUddd54422017-04-18 16:38:44 +080076{
Lei YUad143542017-07-25 14:27:07 +080077 return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(owner);
Lei YUddd54422017-04-18 16:38:44 +080078}
79
80} // namespace utils
81} // namespace time
82} // namespace phosphor