blob: 0ec55f39555f70a024738af3536b86f5a187b8fb [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 {
Patrick Williamsc09ac3f2020-05-13 18:01:29 -050070 std::variant<T> value{};
Lei YU86c83f32018-07-13 15:14:56 +080071 auto reply = bus.call(method);
Lei YUa7417132017-02-23 15:24:05 +080072 reply.read(value);
Patrick Williams5b746c72020-05-13 11:49:35 -050073 return std::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
Lei YUddd54422017-04-18 16:38:44 +0800110/** @brief Convert a mode enum to mode string
111 *
112 * @param[in] mode - The Mode enum
113 *
114 * @return The string of the mode
115 */
Lei YUad143542017-07-25 14:27:07 +0800116std::string modeToStr(Mode mode);
Lei YUddd54422017-04-18 16:38:44 +0800117
Lei YUddd54422017-04-18 16:38:44 +0800118} // namespace utils
119} // namespace time
120} // namespace phosphor