blob: 5d7215545ea7dfdf154083dd0e4901cebd6f6477 [file] [log] [blame]
Lei YU7f4fca52017-02-23 15:15:51 +08001#pragma once
2
Lei YU86d80412017-07-12 13:12:12 +08003#include "elog-errors.hpp"
Lei YUddd54422017-04-18 16:38:44 +08004#include "types.hpp"
Lei YU86d80412017-07-12 13:12:12 +08005#include "xyz/openbmc_project/Time/Internal/error.hpp"
Lei YUddd54422017-04-18 16:38:44 +08006
Lei YU86d80412017-07-12 13:12:12 +08007#include <phosphor-logging/elog.hpp>
Lei YUa7417132017-02-23 15:24:05 +08008#include <phosphor-logging/log.hpp>
Lei YUddd54422017-04-18 16:38:44 +08009#include <sdbusplus/bus.hpp>
Lei YUa7417132017-02-23 15:24:05 +080010
Lei YU7f4fca52017-02-23 15:15:51 +080011#include <fstream>
12
13namespace phosphor
14{
15namespace time
16{
17namespace utils
18{
19
Lei YUa7417132017-02-23 15:24:05 +080020using namespace phosphor::logging;
Lei YU86d80412017-07-12 13:12:12 +080021using MethodErr =
22 sdbusplus::xyz::openbmc_project::Time::Internal::Error::MethodError;
Lei YUa7417132017-02-23 15:24:05 +080023
Lei YU7f4fca52017-02-23 15:15:51 +080024/** @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 */
30template <typename T>
31T 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 */
47template <typename T>
48void 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
Lei YUa7417132017-02-23 15:24:05 +080057/** @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 */
67template <typename T>
68T 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 {
Lei YU86d80412017-07-12 13:12:12 +080087 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));
Lei YUa7417132017-02-23 15:24:05 +080092 }
93 return value.template get<T>();
94}
95
Lei YUdd8e9e42017-04-19 17:46:58 +080096/** @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 */
104std::string getService(sdbusplus::bus::bus& bus,
105 const char* path,
106 const char* interface);
107
Lei YUddd54422017-04-18 16:38:44 +0800108/** @brief Convert a string to enum Mode
109 *
110 * Convert the time mode string to enum.
Lei YUad143542017-07-25 14:27:07 +0800111 * Valid strings are
112 * "xyz.openbmc_project.Time.Synchronization.Method.NTP"
113 * "xyz.openbmc_project.Time.Synchronization.Method.Manual"
Lei YUddd54422017-04-18 16:38:44 +0800114 * 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 */
121Mode strToMode(const std::string& mode);
122
123/** @brief Convert a string to enum Owner
124 *
125 * Convert the time owner string to enum.
Lei YUad143542017-07-25 14:27:07 +0800126 * 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"
Lei YUddd54422017-04-18 16:38:44 +0800131 * 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 */
138Owner 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 */
Lei YUad143542017-07-25 14:27:07 +0800146std::string modeToStr(Mode mode);
Lei YUddd54422017-04-18 16:38:44 +0800147
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 */
Lei YUad143542017-07-25 14:27:07 +0800154std::string ownerToStr(Owner owner);
Lei YUddd54422017-04-18 16:38:44 +0800155
156} // namespace utils
157} // namespace time
158} // namespace phosphor