xref: /openbmc/phosphor-time-manager/utils.hpp (revision e101030b37eb8f64616a33eb0ef71d67a19bf7d7)
1 #pragma once
2 
3 #include "types.hpp"
4 
5 #include <phosphor-logging/lg2.hpp>
6 #include <sdbusplus/bus.hpp>
7 
8 #include <string_view>
9 #include <vector>
10 
11 namespace phosphor
12 {
13 namespace time
14 {
15 namespace utils
16 {
17 
18 using Path = std::string;
19 using Service = std::string;
20 using Interface = std::string;
21 using Interfaces = std::vector<Interface>;
22 using MapperResponse =
23     std::vector<std::pair<Path, std::vector<std::pair<Service, Interfaces>>>>;
24 
25 PHOSPHOR_LOG2_USING;
26 
27 /** @brief The template function to get property from the requested dbus path
28  *
29  * @param[in] bus          - The Dbus bus object
30  * @param[in] service      - The Dbus service name
31  * @param[in] path         - The Dbus object path
32  * @param[in] interface    - The Dbus interface
33  * @param[in] propertyName - The property name to get
34  *
35  * @return The value of the property
36  */
37 template <typename T>
getProperty(sdbusplus::bus_t & bus,const char * service,const char * path,const char * interface,const char * propertyName)38 T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
39               const char* interface, const char* propertyName)
40 {
41     auto method = bus.new_method_call(service, path,
42                                       "org.freedesktop.DBus.Properties", "Get");
43     method.append(interface, propertyName);
44     try
45     {
46         std::variant<T> value{};
47         auto reply = bus.call(method);
48         reply.read(value);
49         return std::get<T>(value);
50     }
51     catch (const sdbusplus::exception_t& ex)
52     {
53         error("GetProperty call failed, path:{PATH}, interface:{INTF}, "
54               "propertyName:{NAME}, error:{ERROR}",
55               "PATH", path, "INTF", interface, "NAME", propertyName, "ERROR",
56               ex);
57         throw std::runtime_error("GetProperty call failed");
58     }
59 }
60 
61 /** @brief The template function to set property to the requested dbus path
62  *
63  * @param[in] bus          - The Dbus bus object
64  * @param[in] service      - The Dbus service name
65  * @param[in] path         - The Dbus object path
66  * @param[in] interface    - The Dbus interface
67  * @param[in] propertyName - The property name to set
68  * @param[in] value - the value to set the property to
69  *
70  */
71 template <typename T>
setProperty(sdbusplus::bus_t & bus,const std::string & service,const std::string & path,const std::string & interface,const std::string & propertyName,T & value)72 void setProperty(sdbusplus::bus_t& bus, const std::string& service,
73                  const std::string& path, const std::string& interface,
74                  const std::string& propertyName, T& value)
75 {
76     std::variant<T> propertyValue(value);
77 
78     auto method = bus.new_method_call(service.c_str(), path.c_str(),
79                                       "org.freedesktop.DBus.Properties", "Set");
80 
81     method.append(interface, propertyName, propertyValue);
82 
83     try
84     {
85         auto reply = bus.call(method);
86     }
87     catch (const sdbusplus::exception_t& ex)
88     {
89         error("SetProperty call failed, path:{PATH}, interface:{INTF}, "
90               "propertyName:{NAME}, error:{ERROR}",
91               "PATH", path, "INTF", interface, "NAME", propertyName, "ERROR",
92               ex);
93         throw std::runtime_error("SetProperty call failed");
94     }
95 }
96 
97 /** @brief Get service name from object path and interface
98  *
99  * @param[in] bus          - The Dbus bus object
100  * @param[in] path         - The Dbus object path
101  * @param[in] interface    - The Dbus interface
102  *
103  * @return The name of the service
104  */
105 std::string getService(sdbusplus::bus_t& bus, const char* path,
106                        const char* interface);
107 
108 /** @brief Get sub tree from root, depth and interfaces
109  *
110  * @param[in] bus           - The Dbus bus object
111  * @param[in] root          - The root of the tree to search
112  * @param[in] interfaces    - All interfaces in the subtree to search for
113  * @param[in] depth         - The number of path elements to descend
114  *
115  * @return The name of the service
116  *
117  * @throw sdbusplus::exception_t when it fails
118  */
119 MapperResponse getSubTree(sdbusplus::bus_t& bus, const std::string& root,
120                           const Interfaces& interfaces, int32_t depth);
121 
122 /** @brief Convert a string to enum Mode
123  *
124  * Convert the time mode string to enum.
125  * Valid strings are
126  *   "xyz.openbmc_project.Time.Synchronization.Method.NTP"
127  *   "xyz.openbmc_project.Time.Synchronization.Method.Manual"
128  * If it's not a valid time mode string, it means something
129  * goes wrong so raise exception.
130  *
131  * @param[in] mode - The string of time mode
132  *
133  * @return The Mode enum
134  */
135 Mode strToMode(const std::string& mode);
136 
137 /** @brief Convert a mode enum to mode string
138  *
139  * @param[in] mode - The Mode enum
140  *
141  * @return The string of the mode
142  */
143 std::string modeToStr(Mode mode);
144 
145 } // namespace utils
146 } // namespace time
147 } // namespace phosphor
148