1 #pragma once
2 
3 #include "types.hpp"
4 
5 #include <phosphor-logging/log.hpp>
6 #include <sdbusplus/bus.hpp>
7 
8 namespace phosphor
9 {
10 namespace time
11 {
12 namespace utils
13 {
14 
15 using namespace phosphor::logging;
16 
17 /** @brief The template function to get property from the requested dbus path
18  *
19  * @param[in] bus          - The Dbus bus object
20  * @param[in] service      - The Dbus service name
21  * @param[in] path         - The Dbus object path
22  * @param[in] interface    - The Dbus interface
23  * @param[in] propertyName - The property name to get
24  *
25  * @return The value of the property
26  */
27 template <typename T>
28 T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path,
29               const char* interface, const char* propertyName)
30 {
31     auto method = bus.new_method_call(service, path,
32                                       "org.freedesktop.DBus.Properties", "Get");
33     method.append(interface, propertyName);
34     try
35     {
36         std::variant<T> value{};
37         auto reply = bus.call(method);
38         reply.read(value);
39         return std::get<T>(value);
40     }
41     catch (const sdbusplus::exception::SdBusError& ex)
42     {
43         log<level::ERR>("GetProperty call failed", entry("PATH=%s", path),
44                         entry("INTERFACE=%s", interface),
45                         entry("PROPERTY=%s", propertyName));
46         throw std::runtime_error("GetProperty call failed");
47     }
48 }
49 
50 /** @brief Get service name from object path and interface
51  *
52  * @param[in] bus          - The Dbus bus object
53  * @param[in] path         - The Dbus object path
54  * @param[in] interface    - The Dbus interface
55  *
56  * @return The name of the service
57  */
58 std::string getService(sdbusplus::bus::bus& bus, const char* path,
59                        const char* interface);
60 
61 /** @brief Convert a string to enum Mode
62  *
63  * Convert the time mode string to enum.
64  * Valid strings are
65  *   "xyz.openbmc_project.Time.Synchronization.Method.NTP"
66  *   "xyz.openbmc_project.Time.Synchronization.Method.Manual"
67  * If it's not a valid time mode string, it means something
68  * goes wrong so raise exception.
69  *
70  * @param[in] mode - The string of time mode
71  *
72  * @return The Mode enum
73  */
74 Mode strToMode(const std::string& mode);
75 
76 /** @brief Convert a mode enum to mode string
77  *
78  * @param[in] mode - The Mode enum
79  *
80  * @return The string of the mode
81  */
82 std::string modeToStr(Mode mode);
83 
84 } // namespace utils
85 } // namespace time
86 } // namespace phosphor
87