1 #include "utils.hpp"
2 
3 namespace phosphor
4 {
5 namespace time
6 {
7 
8 namespace // anonymous
9 {
10 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
11 constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
12 constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
13 } // namespace
14 
15 namespace utils
16 {
17 
18 std::string getService(sdbusplus::bus_t& bus, const char* path,
19                        const char* interface)
20 {
21     auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
22                                       MAPPER_INTERFACE, "GetObject");
23 
24     mapper.append(path, std::vector<std::string>({interface}));
25     try
26     {
27         auto mapperResponseMsg = bus.call(mapper);
28 
29         std::vector<std::pair<std::string, std::vector<std::string>>>
30             mapperResponse;
31         mapperResponseMsg.read(mapperResponse);
32         if (mapperResponse.empty())
33         {
34             lg2::error("Error reading mapper response");
35             throw std::runtime_error("Error reading mapper response");
36         }
37 
38         return mapperResponse[0].first;
39     }
40     catch (const sdbusplus::exception_t& ex)
41     {
42         lg2::error(
43             "Mapper call failed: path:{PATH}, interface:{INTF}, error:{ERROR}",
44             "PATH", path, "INTF", interface, "ERROR", ex);
45         throw std::runtime_error("Mapper call failed");
46     }
47 }
48 
49 MapperResponse getSubTree(sdbusplus::bus_t& bus, const std::string& root,
50                           const Interfaces& interfaces, int32_t depth)
51 {
52     auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
53                                           MAPPER_INTERFACE, "GetSubTree");
54     mapperCall.append(root);
55     mapperCall.append(depth);
56     mapperCall.append(interfaces);
57 
58     auto response = bus.call(mapperCall);
59 
60     MapperResponse result;
61     response.read(result);
62     return result;
63 }
64 
65 Mode strToMode(const std::string& mode)
66 {
67     return ModeSetting::convertMethodFromString(mode);
68 }
69 
70 std::string modeToStr(Mode mode)
71 {
72     return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(
73         mode);
74 }
75 
76 } // namespace utils
77 } // namespace time
78 } // namespace phosphor
79