blob: 57c3b5fe547aafe628f931ee315f8e4982a4ece3 [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";
19
Lei YUddd54422017-04-18 16:38:44 +080020/** @brief The map that maps the string to Mode */
21const std::map<std::string, Mode> modeMap =
22{
23 { "NTP", Mode::NTP },
24 { "MANUAL", Mode::MANUAL },
25};
26
27/** @brief The map that maps the string to Owner */
28const std::map<std::string, Owner> ownerMap =
29{
30 { "BMC", Owner::BMC },
31 { "HOST", Owner::HOST },
32 { "SPLIT", Owner::SPLIT },
33 { "BOTH", Owner::BOTH },
34};
35}
36
37namespace utils
38{
39
Lei YU86d80412017-07-12 13:12:12 +080040using InvalidArgumentError =
41 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
42
Lei YUddd54422017-04-18 16:38:44 +080043using namespace phosphor::logging;
44
Lei YUdd8e9e42017-04-19 17:46:58 +080045std::string getService(sdbusplus::bus::bus& bus,
46 const char* path,
47 const char* interface)
48{
49 auto mapper = bus.new_method_call(MAPPER_BUSNAME,
50 MAPPER_PATH,
51 MAPPER_INTERFACE,
52 "GetObject");
53
54 mapper.append(path, std::vector<std::string>({interface}));
55 auto mapperResponseMsg = bus.call(mapper);
56
57 if (mapperResponseMsg.is_method_error())
58 {
Lei YU86d80412017-07-12 13:12:12 +080059 using namespace xyz::openbmc_project::Time::Internal;
60 elog<MethodErr>(MethodError::METHOD_NAME("GetObject"),
61 MethodError::PATH(path),
62 MethodError::INTERFACE(interface),
63 MethodError::MISC({}));
Lei YUdd8e9e42017-04-19 17:46:58 +080064 }
65
66 std::map<std::string, std::vector<std::string>> mapperResponse;
67 mapperResponseMsg.read(mapperResponse);
68 if (mapperResponse.empty())
69 {
Lei YU86d80412017-07-12 13:12:12 +080070 using namespace xyz::openbmc_project::Time::Internal;
71 elog<MethodErr>(MethodError::METHOD_NAME("GetObject"),
72 MethodError::PATH(path),
73 MethodError::INTERFACE(interface),
74 MethodError::MISC("Error reading mapper response"));
Lei YUdd8e9e42017-04-19 17:46:58 +080075 }
76
77 return mapperResponse.begin()->first;
78}
79
Lei YUddd54422017-04-18 16:38:44 +080080Mode strToMode(const std::string& mode)
81{
82 auto it = modeMap.find(mode);
83 if (it == modeMap.end())
84 {
Lei YU86d80412017-07-12 13:12:12 +080085 using namespace xyz::openbmc_project::Common;
86 elog<InvalidArgumentError>(
87 InvalidArgument::ARGUMENT_NAME("TimeMode"),
88 InvalidArgument::ARGUMENT_VALUE(mode.c_str()));
Lei YUddd54422017-04-18 16:38:44 +080089 }
90 return it->second;
91}
92
93Owner strToOwner(const std::string& owner)
94{
95 auto it = ownerMap.find(owner);
96 if (it == ownerMap.end())
97 {
Lei YU86d80412017-07-12 13:12:12 +080098 using namespace xyz::openbmc_project::Common;
99 elog<InvalidArgumentError>(
100 InvalidArgument::ARGUMENT_NAME("TimeOwner"),
101 InvalidArgument::ARGUMENT_VALUE(owner.c_str()));
Lei YUddd54422017-04-18 16:38:44 +0800102 }
103 return it->second;
104}
105
106const char* modeToStr(Mode mode)
107{
108 const char* ret{};
109 switch (mode)
110 {
111 case Mode::NTP:
112 ret = "NTP";
113 break;
114 case Mode::MANUAL:
115 ret = "MANUAL";
116 break;
117 default:
Lei YU86d80412017-07-12 13:12:12 +0800118 using namespace xyz::openbmc_project::Common;
119 elog<InvalidArgumentError>(
120 InvalidArgument::ARGUMENT_NAME("Mode"),
121 InvalidArgument::ARGUMENT_VALUE(
122 std::to_string(static_cast<int>(mode)).c_str()));
Lei YUddd54422017-04-18 16:38:44 +0800123 break;
124 }
125 return ret;
126}
127
128const char* ownerToStr(Owner owner)
129{
130 const char* ret{};
131 switch (owner)
132 {
133 case Owner::BMC:
134 ret = "BMC";
135 break;
136 case Owner::HOST:
137 ret = "HOST";
138 break;
139 case Owner::SPLIT:
140 ret = "SPLIT";
141 break;
142 case Owner::BOTH:
143 ret = "BOTH";
144 break;
145 default:
Lei YU86d80412017-07-12 13:12:12 +0800146 using namespace xyz::openbmc_project::Common;
147 elog<InvalidArgumentError>(
148 InvalidArgument::ARGUMENT_NAME("Owner"),
149 InvalidArgument::ARGUMENT_VALUE(
150 std::to_string(static_cast<int>(owner)).c_str()));
Lei YUddd54422017-04-18 16:38:44 +0800151 break;
152 }
153 return ret;
154}
155
156} // namespace utils
157} // namespace time
158} // namespace phosphor