blob: 6f2fc625bb5112eb67119e9c1fb0fbad37b3ec35 [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
George Liu947b5342022-07-01 16:12:18 +08005#include <phosphor-logging/lg2.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 +080015/** @brief The template function to get property from the requested dbus path
16 *
17 * @param[in] bus - The Dbus bus object
18 * @param[in] service - The Dbus service name
19 * @param[in] path - The Dbus object path
20 * @param[in] interface - The Dbus interface
21 * @param[in] propertyName - The property name to get
22 *
23 * @return The value of the property
24 */
25template <typename T>
Patrick Williams38679262022-07-22 19:26:55 -050026T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050027 const char* interface, const char* propertyName)
Lei YUa7417132017-02-23 15:24:05 +080028{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050029 auto method = bus.new_method_call(service, path,
30 "org.freedesktop.DBus.Properties", "Get");
Lei YUa7417132017-02-23 15:24:05 +080031 method.append(interface, propertyName);
Lei YU86c83f32018-07-13 15:14:56 +080032 try
Lei YUa7417132017-02-23 15:24:05 +080033 {
Patrick Williamsc09ac3f2020-05-13 18:01:29 -050034 std::variant<T> value{};
Lei YU86c83f32018-07-13 15:14:56 +080035 auto reply = bus.call(method);
Lei YUa7417132017-02-23 15:24:05 +080036 reply.read(value);
Patrick Williams5b746c72020-05-13 11:49:35 -050037 return std::get<T>(value);
Lei YUa7417132017-02-23 15:24:05 +080038 }
Patrick Williams38679262022-07-22 19:26:55 -050039 catch (const sdbusplus::exception_t& ex)
Lei YUa7417132017-02-23 15:24:05 +080040 {
George Liu947b5342022-07-01 16:12:18 +080041 lg2::error("GetProperty call failed, path:{PATH}, interface:{INTF}, "
42 "propertyName:{NAME}, error:{ERROR}",
43 "PATH", path, "INTF", interface, "NAME", propertyName,
44 "ERROR", ex);
Lei YU86c83f32018-07-13 15:14:56 +080045 throw std::runtime_error("GetProperty call failed");
Lei YUa7417132017-02-23 15:24:05 +080046 }
Lei YUa7417132017-02-23 15:24:05 +080047}
48
Lei YUdd8e9e42017-04-19 17:46:58 +080049/** @brief Get service name from object path and interface
50 *
51 * @param[in] bus - The Dbus bus object
52 * @param[in] path - The Dbus object path
53 * @param[in] interface - The Dbus interface
54 *
55 * @return The name of the service
56 */
Patrick Williams38679262022-07-22 19:26:55 -050057std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUdd8e9e42017-04-19 17:46:58 +080058 const char* interface);
59
Lei YUddd54422017-04-18 16:38:44 +080060/** @brief Convert a string to enum Mode
61 *
62 * Convert the time mode string to enum.
Lei YUad143542017-07-25 14:27:07 +080063 * Valid strings are
64 * "xyz.openbmc_project.Time.Synchronization.Method.NTP"
65 * "xyz.openbmc_project.Time.Synchronization.Method.Manual"
Lei YUddd54422017-04-18 16:38:44 +080066 * If it's not a valid time mode string, it means something
67 * goes wrong so raise exception.
68 *
69 * @param[in] mode - The string of time mode
70 *
71 * @return The Mode enum
72 */
73Mode strToMode(const std::string& mode);
74
Lei YUddd54422017-04-18 16:38:44 +080075/** @brief Convert a mode enum to mode string
76 *
77 * @param[in] mode - The Mode enum
78 *
79 * @return The string of the mode
80 */
Lei YUad143542017-07-25 14:27:07 +080081std::string modeToStr(Mode mode);
Lei YUddd54422017-04-18 16:38:44 +080082
Lei YUddd54422017-04-18 16:38:44 +080083} // namespace utils
84} // namespace time
85} // namespace phosphor