blob: 778487f2eaf1e8c86a3e8d97ebd403d7bc9f39e5 [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
Ed Tanous7aa715b2018-05-09 17:28:05 -070050 std::vector<std::pair<std::string, std::vector<std::string>>>
51 mapperResponse;
Lei YUdd8e9e42017-04-19 17:46:58 +080052 mapperResponseMsg.read(mapperResponse);
53 if (mapperResponse.empty())
54 {
Lei YU86d80412017-07-12 13:12:12 +080055 using namespace xyz::openbmc_project::Time::Internal;
56 elog<MethodErr>(MethodError::METHOD_NAME("GetObject"),
57 MethodError::PATH(path),
58 MethodError::INTERFACE(interface),
59 MethodError::MISC("Error reading mapper response"));
Lei YUdd8e9e42017-04-19 17:46:58 +080060 }
Ed Tanous7aa715b2018-05-09 17:28:05 -070061 if (mapperResponse.size() < 1){
62 return "";
63 }
64 return mapperResponse[0].first;
Lei YUdd8e9e42017-04-19 17:46:58 +080065}
66
Lei YUddd54422017-04-18 16:38:44 +080067Mode strToMode(const std::string& mode)
68{
Lei YUad143542017-07-25 14:27:07 +080069 return ModeSetting::convertMethodFromString(mode);
Lei YUddd54422017-04-18 16:38:44 +080070}
71
72Owner strToOwner(const std::string& owner)
73{
Lei YUad143542017-07-25 14:27:07 +080074 return OwnerSetting::convertOwnersFromString(owner);
Lei YUddd54422017-04-18 16:38:44 +080075}
76
Lei YUad143542017-07-25 14:27:07 +080077std::string modeToStr(Mode mode)
Lei YUddd54422017-04-18 16:38:44 +080078{
Lei YUad143542017-07-25 14:27:07 +080079 return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(mode);
Lei YUddd54422017-04-18 16:38:44 +080080}
81
Lei YUad143542017-07-25 14:27:07 +080082std::string ownerToStr(Owner owner)
Lei YUddd54422017-04-18 16:38:44 +080083{
Lei YUad143542017-07-25 14:27:07 +080084 return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(owner);
Lei YUddd54422017-04-18 16:38:44 +080085}
86
87} // namespace utils
88} // namespace time
89} // namespace phosphor