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