blob: 08b10e8d5aa200d291da7f23cb5a979dc36f2220 [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
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05005#include <fstream>
Lei YUa7417132017-02-23 15:24:05 +08006#include <phosphor-logging/log.hpp>
Lei YUddd54422017-04-18 16:38:44 +08007#include <sdbusplus/bus.hpp>
Lei YUa7417132017-02-23 15:24:05 +08008
Lei YU7f4fca52017-02-23 15:15:51 +08009namespace phosphor
10{
11namespace time
12{
13namespace utils
14{
15
Lei YUa7417132017-02-23 15:24:05 +080016using namespace phosphor::logging;
17
Lei YU7f4fca52017-02-23 15:15:51 +080018/** @brief Read data with type T from file
19 *
20 * @param[in] fileName - The name of file to read from
21 *
22 * @return The data with type T
23 */
24template <typename T>
25T readData(const char* fileName)
26{
27 T data{};
28 std::ifstream fs(fileName);
29 if (fs.is_open())
30 {
31 fs >> data;
32 }
33 return data;
34}
35
36/** @brief Write data with type T to file
37 *
38 * @param[in] fileName - The name of file to write to
39 * @param[in] data - The data with type T to write to file
40 */
41template <typename T>
42void writeData(const char* fileName, T&& data)
43{
44 std::ofstream fs(fileName, std::ios::out);
45 if (fs.is_open())
46 {
47 fs << std::forward<T>(data);
48 }
49}
50
Lei YUa7417132017-02-23 15:24:05 +080051/** @brief The template function to get property from the requested dbus path
52 *
53 * @param[in] bus - The Dbus bus object
54 * @param[in] service - The Dbus service name
55 * @param[in] path - The Dbus object path
56 * @param[in] interface - The Dbus interface
57 * @param[in] propertyName - The property name to get
58 *
59 * @return The value of the property
60 */
61template <typename T>
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050062T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path,
63 const char* interface, const char* propertyName)
Lei YUa7417132017-02-23 15:24:05 +080064{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050065 auto method = bus.new_method_call(service, path,
66 "org.freedesktop.DBus.Properties", "Get");
Lei YUa7417132017-02-23 15:24:05 +080067 method.append(interface, propertyName);
Lei YU86c83f32018-07-13 15:14:56 +080068 try
Lei YUa7417132017-02-23 15:24:05 +080069 {
Lei YU86c83f32018-07-13 15:14:56 +080070 sdbusplus::message::variant<T> value{};
71 auto reply = bus.call(method);
Lei YUa7417132017-02-23 15:24:05 +080072 reply.read(value);
William A. Kennington III1f1d8e02018-11-06 16:06:34 -080073 return sdbusplus::message::variant_ns::get<T>(value);
Lei YUa7417132017-02-23 15:24:05 +080074 }
Lei YU86c83f32018-07-13 15:14:56 +080075 catch (const sdbusplus::exception::SdBusError& ex)
Lei YUa7417132017-02-23 15:24:05 +080076 {
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050077 log<level::ERR>("GetProperty call failed", entry("PATH=%s", path),
Lei YU86c83f32018-07-13 15:14:56 +080078 entry("INTERFACE=%s", interface),
79 entry("PROPERTY=%s", propertyName));
80 throw std::runtime_error("GetProperty call failed");
Lei YUa7417132017-02-23 15:24:05 +080081 }
Lei YUa7417132017-02-23 15:24:05 +080082}
83
Lei YUdd8e9e42017-04-19 17:46:58 +080084/** @brief Get service name from object path and interface
85 *
86 * @param[in] bus - The Dbus bus object
87 * @param[in] path - The Dbus object path
88 * @param[in] interface - The Dbus interface
89 *
90 * @return The name of the service
91 */
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050092std::string getService(sdbusplus::bus::bus& bus, const char* path,
Lei YUdd8e9e42017-04-19 17:46:58 +080093 const char* interface);
94
Lei YUddd54422017-04-18 16:38:44 +080095/** @brief Convert a string to enum Mode
96 *
97 * Convert the time mode string to enum.
Lei YUad143542017-07-25 14:27:07 +080098 * Valid strings are
99 * "xyz.openbmc_project.Time.Synchronization.Method.NTP"
100 * "xyz.openbmc_project.Time.Synchronization.Method.Manual"
Lei YUddd54422017-04-18 16:38:44 +0800101 * If it's not a valid time mode string, it means something
102 * goes wrong so raise exception.
103 *
104 * @param[in] mode - The string of time mode
105 *
106 * @return The Mode enum
107 */
108Mode strToMode(const std::string& mode);
109
110/** @brief Convert a string to enum Owner
111 *
112 * Convert the time owner string to enum.
Lei YUad143542017-07-25 14:27:07 +0800113 * Valid strings are
114 * "xyz.openbmc_project.Time.Owner.Owners.BMC"
115 * "xyz.openbmc_project.Time.Owner.Owners.Host"
116 * "xyz.openbmc_project.Time.Owner.Owners.Both"
117 * "xyz.openbmc_project.Time.Owner.Owners.Split"
Lei YUddd54422017-04-18 16:38:44 +0800118 * If it's not a valid time owner string, it means something
119 * goes wrong so raise exception.
120 *
121 * @param[in] owner - The string of time owner
122 *
123 * @return The Owner enum
124 */
125Owner strToOwner(const std::string& owner);
126
127/** @brief Convert a mode enum to mode string
128 *
129 * @param[in] mode - The Mode enum
130 *
131 * @return The string of the mode
132 */
Lei YUad143542017-07-25 14:27:07 +0800133std::string modeToStr(Mode mode);
Lei YUddd54422017-04-18 16:38:44 +0800134
135/** @brief Convert a owner enum to owner string
136 *
137 * @param[in] owner - The Owner enum
138 *
139 * @return The string of the owner
140 */
Lei YUad143542017-07-25 14:27:07 +0800141std::string ownerToStr(Owner owner);
Lei YUddd54422017-04-18 16:38:44 +0800142
143} // namespace utils
144} // namespace time
145} // namespace phosphor