blob: ea2e3e4ff8079c7d720ac9b971ca710aa7b092f9 [file] [log] [blame]
Lei YU7f4fca52017-02-23 15:15:51 +08001#pragma once
2
Lei YUddd54422017-04-18 16:38:44 +08003#include "types.hpp"
4
Lei YUa7417132017-02-23 15:24:05 +08005#include <phosphor-logging/log.hpp>
Lei YUddd54422017-04-18 16:38:44 +08006#include <sdbusplus/bus.hpp>
Lei YUa7417132017-02-23 15:24:05 +08007
Lei YU7f4fca52017-02-23 15:15:51 +08008#include <fstream>
9
10namespace phosphor
11{
12namespace time
13{
14namespace utils
15{
16
Lei YUa7417132017-02-23 15:24:05 +080017using namespace phosphor::logging;
18
Lei YU7f4fca52017-02-23 15:15:51 +080019/** @brief Read data with type T from file
20 *
21 * @param[in] fileName - The name of file to read from
22 *
23 * @return The data with type T
24 */
25template <typename T>
26T readData(const char* fileName)
27{
28 T data{};
29 std::ifstream fs(fileName);
30 if (fs.is_open())
31 {
32 fs >> data;
33 }
34 return data;
35}
36
37/** @brief Write data with type T to file
38 *
39 * @param[in] fileName - The name of file to write to
40 * @param[in] data - The data with type T to write to file
41 */
42template <typename T>
43void writeData(const char* fileName, T&& data)
44{
45 std::ofstream fs(fileName, std::ios::out);
46 if (fs.is_open())
47 {
48 fs << std::forward<T>(data);
49 }
50}
51
Lei YUa7417132017-02-23 15:24:05 +080052/** @brief The template function to get property from the requested dbus path
53 *
54 * @param[in] bus - The Dbus bus object
55 * @param[in] service - The Dbus service name
56 * @param[in] path - The Dbus object path
57 * @param[in] interface - The Dbus interface
58 * @param[in] propertyName - The property name to get
59 *
60 * @return The value of the property
61 */
62template <typename T>
63T getProperty(sdbusplus::bus::bus& bus,
64 const char* service,
65 const char* path,
66 const char* interface,
67 const char* propertyName)
68{
69 sdbusplus::message::variant<T> value{};
70 auto method = bus.new_method_call(service,
71 path,
72 "org.freedesktop.DBus.Properties",
73 "Get");
74 method.append(interface, propertyName);
75 auto reply = bus.call(method);
76 if (reply)
77 {
78 reply.read(value);
79 }
80 else
81 {
82 // TODO: use elog to throw exception
83 log<level::ERR>("Failed to get property",
84 entry("SERVICE=%s", service),
85 entry("PATH=%s", path),
86 entry("INTERFACE=%s", interface),
87 entry("PROPERTY=%s", propertyName));
88 }
89 return value.template get<T>();
90}
91
Lei YUdd8e9e42017-04-19 17:46:58 +080092/** @brief Get service name from object path and interface
93 *
94 * @param[in] bus - The Dbus bus object
95 * @param[in] path - The Dbus object path
96 * @param[in] interface - The Dbus interface
97 *
98 * @return The name of the service
99 */
100std::string getService(sdbusplus::bus::bus& bus,
101 const char* path,
102 const char* interface);
103
Lei YUddd54422017-04-18 16:38:44 +0800104/** @brief Convert a string to enum Mode
105 *
106 * Convert the time mode string to enum.
107 * Valid strings are "NTP", "MANUAL"
108 * If it's not a valid time mode string, it means something
109 * goes wrong so raise exception.
110 *
111 * @param[in] mode - The string of time mode
112 *
113 * @return The Mode enum
114 */
115Mode strToMode(const std::string& mode);
116
117/** @brief Convert a string to enum Owner
118 *
119 * Convert the time owner string to enum.
120 * Valid strings are "BMC", "HOST", "SPLIT", "BOTH"
121 * If it's not a valid time owner string, it means something
122 * goes wrong so raise exception.
123 *
124 * @param[in] owner - The string of time owner
125 *
126 * @return The Owner enum
127 */
128Owner strToOwner(const std::string& owner);
129
130/** @brief Convert a mode enum to mode string
131 *
132 * @param[in] mode - The Mode enum
133 *
134 * @return The string of the mode
135 */
136const char* modeToStr(Mode mode);
137
138/** @brief Convert a owner enum to owner string
139 *
140 * @param[in] owner - The Owner enum
141 *
142 * @return The string of the owner
143 */
144const char* ownerToStr(Owner owner);
145
146} // namespace utils
147} // namespace time
148} // namespace phosphor