1 #pragma once
2 
3 #include "elog-errors.hpp"
4 #include "types.hpp"
5 #include "xyz/openbmc_project/Time/Internal/error.hpp"
6 
7 #include <phosphor-logging/elog.hpp>
8 #include <phosphor-logging/log.hpp>
9 #include <sdbusplus/bus.hpp>
10 
11 #include <fstream>
12 
13 namespace phosphor
14 {
15 namespace time
16 {
17 namespace utils
18 {
19 
20 using namespace phosphor::logging;
21 using MethodErr =
22     sdbusplus::xyz::openbmc_project::Time::Internal::Error::MethodError;
23 
24 /** @brief Read data with type T from file
25  *
26  * @param[in] fileName - The name of file to read from
27  *
28  * @return The data with type T
29  */
30 template <typename T>
31 T readData(const char* fileName)
32 {
33     T data{};
34     std::ifstream fs(fileName);
35     if (fs.is_open())
36     {
37         fs >> data;
38     }
39     return data;
40 }
41 
42 /** @brief Write data with type T to file
43  *
44  * @param[in] fileName - The name of file to write to
45  * @param[in] data - The data with type T to write to file
46  */
47 template <typename T>
48 void writeData(const char* fileName, T&& data)
49 {
50     std::ofstream fs(fileName, std::ios::out);
51     if (fs.is_open())
52     {
53         fs << std::forward<T>(data);
54     }
55 }
56 
57 /** @brief The template function to get property from the requested dbus path
58  *
59  * @param[in] bus          - The Dbus bus object
60  * @param[in] service      - The Dbus service name
61  * @param[in] path         - The Dbus object path
62  * @param[in] interface    - The Dbus interface
63  * @param[in] propertyName - The property name to get
64  *
65  * @return The value of the property
66  */
67 template <typename T>
68 T getProperty(sdbusplus::bus::bus& bus,
69               const char* service,
70               const char* path,
71               const char* interface,
72               const char* propertyName)
73 {
74     sdbusplus::message::variant<T> value{};
75     auto method = bus.new_method_call(service,
76                                       path,
77                                       "org.freedesktop.DBus.Properties",
78                                       "Get");
79     method.append(interface, propertyName);
80     auto reply = bus.call(method);
81     if (reply)
82     {
83         reply.read(value);
84     }
85     else
86     {
87         using namespace xyz::openbmc_project::Time::Internal;
88         elog<MethodErr>(MethodError::METHOD_NAME("Get"),
89                           MethodError::PATH(path),
90                           MethodError::INTERFACE(interface),
91                           MethodError::MISC(propertyName));
92     }
93     return value.template get<T>();
94 }
95 
96 /** @brief Get service name from object path and interface
97  *
98  * @param[in] bus          - The Dbus bus object
99  * @param[in] path         - The Dbus object path
100  * @param[in] interface    - The Dbus interface
101  *
102  * @return The name of the service
103  */
104 std::string getService(sdbusplus::bus::bus& bus,
105                        const char* path,
106                        const char* interface);
107 
108 /** @brief Convert a string to enum Mode
109  *
110  * Convert the time mode string to enum.
111  * Valid strings are
112  *   "xyz.openbmc_project.Time.Synchronization.Method.NTP"
113  *   "xyz.openbmc_project.Time.Synchronization.Method.Manual"
114  * If it's not a valid time mode string, it means something
115  * goes wrong so raise exception.
116  *
117  * @param[in] mode - The string of time mode
118  *
119  * @return The Mode enum
120  */
121 Mode strToMode(const std::string& mode);
122 
123 /** @brief Convert a string to enum Owner
124  *
125  * Convert the time owner string to enum.
126  * Valid strings are
127  *   "xyz.openbmc_project.Time.Owner.Owners.BMC"
128  *   "xyz.openbmc_project.Time.Owner.Owners.Host"
129  *   "xyz.openbmc_project.Time.Owner.Owners.Both"
130  *   "xyz.openbmc_project.Time.Owner.Owners.Split"
131  * If it's not a valid time owner string, it means something
132  * goes wrong so raise exception.
133  *
134  * @param[in] owner - The string of time owner
135  *
136  * @return The Owner enum
137  */
138 Owner strToOwner(const std::string& owner);
139 
140 /** @brief Convert a mode enum to mode string
141  *
142  * @param[in] mode - The Mode enum
143  *
144  * @return The string of the mode
145  */
146 std::string modeToStr(Mode mode);
147 
148 /** @brief Convert a owner enum to owner string
149  *
150  * @param[in] owner - The Owner enum
151  *
152  * @return The string of the owner
153  */
154 std::string ownerToStr(Owner owner);
155 
156 } // namespace utils
157 } // namespace time
158 } // namespace phosphor
159