blob: 25b8f8c9ff189d2c9d63ebf8f164c0e780ced0a7 [file] [log] [blame]
Lei YUddd54422017-04-18 16:38:44 +08001#include "utils.hpp"
2
Lei YU86d80412017-07-12 13:12:12 +08003#include <phosphor-logging/elog.hpp>
4#include <phosphor-logging/elog-errors.hpp>
Lei YUddd54422017-04-18 16:38:44 +08005#include <phosphor-logging/log.hpp>
Lei YU86d80412017-07-12 13:12:12 +08006#include <xyz/openbmc_project/Common/error.hpp>
Lei YUddd54422017-04-18 16:38:44 +08007
8
9namespace phosphor
10{
11namespace time
12{
13
14namespace // anonymous
15{
Lei YUdd8e9e42017-04-19 17:46:58 +080016constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
17constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
18constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
Lei YUddd54422017-04-18 16:38:44 +080019}
20
21namespace utils
22{
23
Lei YU86d80412017-07-12 13:12:12 +080024using InvalidArgumentError =
25 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
26
Lei YUddd54422017-04-18 16:38:44 +080027using namespace phosphor::logging;
28
Lei YUdd8e9e42017-04-19 17:46:58 +080029std::string getService(sdbusplus::bus::bus& bus,
30 const char* path,
31 const char* interface)
32{
33 auto mapper = bus.new_method_call(MAPPER_BUSNAME,
34 MAPPER_PATH,
35 MAPPER_INTERFACE,
36 "GetObject");
37
38 mapper.append(path, std::vector<std::string>({interface}));
39 auto mapperResponseMsg = bus.call(mapper);
40
41 if (mapperResponseMsg.is_method_error())
42 {
Lei YU86d80412017-07-12 13:12:12 +080043 using namespace xyz::openbmc_project::Time::Internal;
44 elog<MethodErr>(MethodError::METHOD_NAME("GetObject"),
45 MethodError::PATH(path),
46 MethodError::INTERFACE(interface),
47 MethodError::MISC({}));
Lei YUdd8e9e42017-04-19 17:46:58 +080048 }
49
50 std::map<std::string, std::vector<std::string>> mapperResponse;
51 mapperResponseMsg.read(mapperResponse);
52 if (mapperResponse.empty())
53 {
Lei YU86d80412017-07-12 13:12:12 +080054 using namespace xyz::openbmc_project::Time::Internal;
55 elog<MethodErr>(MethodError::METHOD_NAME("GetObject"),
56 MethodError::PATH(path),
57 MethodError::INTERFACE(interface),
58 MethodError::MISC("Error reading mapper response"));
Lei YUdd8e9e42017-04-19 17:46:58 +080059 }
60
61 return mapperResponse.begin()->first;
62}
63
Lei YUddd54422017-04-18 16:38:44 +080064Mode strToMode(const std::string& mode)
65{
Lei YUad143542017-07-25 14:27:07 +080066 return ModeSetting::convertMethodFromString(mode);
Lei YUddd54422017-04-18 16:38:44 +080067}
68
69Owner strToOwner(const std::string& owner)
70{
Lei YUad143542017-07-25 14:27:07 +080071 return OwnerSetting::convertOwnersFromString(owner);
Lei YUddd54422017-04-18 16:38:44 +080072}
73
Lei YUad143542017-07-25 14:27:07 +080074std::string modeToStr(Mode mode)
Lei YUddd54422017-04-18 16:38:44 +080075{
Lei YUad143542017-07-25 14:27:07 +080076 return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(mode);
Lei YUddd54422017-04-18 16:38:44 +080077}
78
Lei YUad143542017-07-25 14:27:07 +080079std::string ownerToStr(Owner owner)
Lei YUddd54422017-04-18 16:38:44 +080080{
Lei YUad143542017-07-25 14:27:07 +080081 return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(owner);
Lei YUddd54422017-04-18 16:38:44 +080082}
83
84} // namespace utils
85} // namespace time
86} // namespace phosphor