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>
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 Get service name from object path and interface
62  *
63  * @param[in] bus          - The Dbus bus object
64  * @param[in] path         - The Dbus object path
65  * @param[in] interface    - The Dbus interface
66  *
67  * @return The name of the service
68  */
69 std::string getService(sdbusplus::bus_t& bus, const char* path,
70                        const char* interface);
71 
72 /** @brief Get sub tree from root, depth and interfaces
73  *
74  * @param[in] bus           - The Dbus bus object
75  * @param[in] root          - The root of the tree to search
76  * @param[in] interfaces    - All interfaces in the subtree to search for
77  * @param[in] depth         - The number of path elements to descend
78  *
79  * @return The name of the service
80  *
81  * @throw sdbusplus::exception_t when it fails
82  */
83 MapperResponse getSubTree(sdbusplus::bus_t& bus, const std::string& root,
84                           const Interfaces& interfaces, int32_t depth);
85 
86 /** @brief Convert a string to enum Mode
87  *
88  * Convert the time mode string to enum.
89  * Valid strings are
90  *   "xyz.openbmc_project.Time.Synchronization.Method.NTP"
91  *   "xyz.openbmc_project.Time.Synchronization.Method.Manual"
92  * If it's not a valid time mode string, it means something
93  * goes wrong so raise exception.
94  *
95  * @param[in] mode - The string of time mode
96  *
97  * @return The Mode enum
98  */
99 Mode strToMode(const std::string& mode);
100 
101 /** @brief Convert a mode enum to mode string
102  *
103  * @param[in] mode - The Mode enum
104  *
105  * @return The string of the mode
106  */
107 std::string modeToStr(Mode mode);
108 
109 } // namespace utils
110 } // namespace time
111 } // namespace phosphor
112