blob: af83d38485033402c34fd6f88ac677cbb57c8218 [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 +08008namespace phosphor
9{
10namespace time
11{
12namespace utils
13{
14
Lei YUa7417132017-02-23 15:24:05 +080015using namespace phosphor::logging;
16
Lei YUa7417132017-02-23 15:24:05 +080017/** @brief The template function to get property from the requested dbus path
18 *
19 * @param[in] bus - The Dbus bus object
20 * @param[in] service - The Dbus service name
21 * @param[in] path - The Dbus object path
22 * @param[in] interface - The Dbus interface
23 * @param[in] propertyName - The property name to get
24 *
25 * @return The value of the property
26 */
27template <typename T>
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050028T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path,
29 const char* interface, const char* propertyName)
Lei YUa7417132017-02-23 15:24:05 +080030{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050031 auto method = bus.new_method_call(service, path,
32 "org.freedesktop.DBus.Properties", "Get");
Lei YUa7417132017-02-23 15:24:05 +080033 method.append(interface, propertyName);
Lei YU86c83f32018-07-13 15:14:56 +080034 try
Lei YUa7417132017-02-23 15:24:05 +080035 {
Patrick Williamsc09ac3f2020-05-13 18:01:29 -050036 std::variant<T> value{};
Lei YU86c83f32018-07-13 15:14:56 +080037 auto reply = bus.call(method);
Lei YUa7417132017-02-23 15:24:05 +080038 reply.read(value);
Patrick Williams5b746c72020-05-13 11:49:35 -050039 return std::get<T>(value);
Lei YUa7417132017-02-23 15:24:05 +080040 }
Patrick Williams295b96b2021-09-02 09:50:14 -050041 catch (const sdbusplus::exception::exception& ex)
Lei YUa7417132017-02-23 15:24:05 +080042 {
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050043 log<level::ERR>("GetProperty call failed", entry("PATH=%s", path),
Lei YU86c83f32018-07-13 15:14:56 +080044 entry("INTERFACE=%s", interface),
45 entry("PROPERTY=%s", propertyName));
46 throw std::runtime_error("GetProperty call failed");
Lei YUa7417132017-02-23 15:24:05 +080047 }
Lei YUa7417132017-02-23 15:24:05 +080048}
49
Lei YUdd8e9e42017-04-19 17:46:58 +080050/** @brief Get service name from object path and interface
51 *
52 * @param[in] bus - The Dbus bus object
53 * @param[in] path - The Dbus object path
54 * @param[in] interface - The Dbus interface
55 *
56 * @return The name of the service
57 */
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050058std::string getService(sdbusplus::bus::bus& bus, const char* path,
Lei YUdd8e9e42017-04-19 17:46:58 +080059 const char* interface);
60
Lei YUddd54422017-04-18 16:38:44 +080061/** @brief Convert a string to enum Mode
62 *
63 * Convert the time mode string to enum.
Lei YUad143542017-07-25 14:27:07 +080064 * Valid strings are
65 * "xyz.openbmc_project.Time.Synchronization.Method.NTP"
66 * "xyz.openbmc_project.Time.Synchronization.Method.Manual"
Lei YUddd54422017-04-18 16:38:44 +080067 * If it's not a valid time mode string, it means something
68 * goes wrong so raise exception.
69 *
70 * @param[in] mode - The string of time mode
71 *
72 * @return The Mode enum
73 */
74Mode strToMode(const std::string& mode);
75
Lei YUddd54422017-04-18 16:38:44 +080076/** @brief Convert a mode enum to mode string
77 *
78 * @param[in] mode - The Mode enum
79 *
80 * @return The string of the mode
81 */
Lei YUad143542017-07-25 14:27:07 +080082std::string modeToStr(Mode mode);
Lei YUddd54422017-04-18 16:38:44 +080083
Lei YUddd54422017-04-18 16:38:44 +080084} // namespace utils
85} // namespace time
86} // namespace phosphor