blob: 87bc2bf0b9617b7bdd5ee49bd2bd2f0e61f85b87 [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.
111 * Valid strings are "NTP", "MANUAL"
112 * If it's not a valid time mode string, it means something
113 * goes wrong so raise exception.
114 *
115 * @param[in] mode - The string of time mode
116 *
117 * @return The Mode enum
118 */
119Mode strToMode(const std::string& mode);
120
121/** @brief Convert a string to enum Owner
122 *
123 * Convert the time owner string to enum.
124 * Valid strings are "BMC", "HOST", "SPLIT", "BOTH"
125 * If it's not a valid time owner string, it means something
126 * goes wrong so raise exception.
127 *
128 * @param[in] owner - The string of time owner
129 *
130 * @return The Owner enum
131 */
132Owner strToOwner(const std::string& owner);
133
134/** @brief Convert a mode enum to mode string
135 *
136 * @param[in] mode - The Mode enum
137 *
138 * @return The string of the mode
139 */
140const char* modeToStr(Mode mode);
141
142/** @brief Convert a owner enum to owner string
143 *
144 * @param[in] owner - The Owner enum
145 *
146 * @return The string of the owner
147 */
148const char* ownerToStr(Owner owner);
149
150} // namespace utils
151} // namespace time
152} // namespace phosphor