1 #include "utils.hpp"
2 
3 #include <phosphor-logging/elog.hpp>
4 #include <phosphor-logging/elog-errors.hpp>
5 #include <phosphor-logging/log.hpp>
6 #include <xyz/openbmc_project/Common/error.hpp>
7 
8 
9 namespace phosphor
10 {
11 namespace time
12 {
13 
14 namespace // anonymous
15 {
16 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
17 constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
18 constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
19 
20 /** @brief The map that maps the string to Mode */
21 const 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 */
28 const 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 
37 namespace utils
38 {
39 
40 using InvalidArgumentError =
41     sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
42 
43 using namespace phosphor::logging;
44 
45 std::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     {
59         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({}));
64     }
65 
66     std::map<std::string, std::vector<std::string>> mapperResponse;
67     mapperResponseMsg.read(mapperResponse);
68     if (mapperResponse.empty())
69     {
70         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"));
75     }
76 
77     return mapperResponse.begin()->first;
78 }
79 
80 Mode strToMode(const std::string& mode)
81 {
82     auto it = modeMap.find(mode);
83     if (it == modeMap.end())
84     {
85         using namespace xyz::openbmc_project::Common;
86         elog<InvalidArgumentError>(
87             InvalidArgument::ARGUMENT_NAME("TimeMode"),
88             InvalidArgument::ARGUMENT_VALUE(mode.c_str()));
89     }
90     return it->second;
91 }
92 
93 Owner strToOwner(const std::string& owner)
94 {
95     auto it = ownerMap.find(owner);
96     if (it == ownerMap.end())
97     {
98         using namespace xyz::openbmc_project::Common;
99         elog<InvalidArgumentError>(
100             InvalidArgument::ARGUMENT_NAME("TimeOwner"),
101             InvalidArgument::ARGUMENT_VALUE(owner.c_str()));
102     }
103     return it->second;
104 }
105 
106 const 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:
118         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()));
123         break;
124     }
125     return ret;
126 }
127 
128 const 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:
146         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()));
151         break;
152     }
153     return ret;
154 }
155 
156 } // namespace utils
157 } // namespace time
158 } // namespace phosphor
159