blob: eaf92c390863d78a7aa0263f500751d19c85b000 [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
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -05008#include <string_view>
George Liudc746c02022-09-02 11:10:55 +08009#include <vector>
10
Lei YU7f4fca52017-02-23 15:15:51 +080011namespace phosphor
12{
13namespace time
14{
15namespace utils
16{
17
George Liudc746c02022-09-02 11:10:55 +080018using Path = std::string;
19using Service = std::string;
20using Interface = std::string;
21using Interfaces = std::vector<Interface>;
22using MapperResponse =
23 std::vector<std::pair<Path, std::vector<std::pair<Service, Interfaces>>>>;
24
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -050025PHOSPHOR_LOG2_USING;
26
Lei YUa7417132017-02-23 15:24:05 +080027/** @brief The template function to get property from the requested dbus path
28 *
29 * @param[in] bus - The Dbus bus object
30 * @param[in] service - The Dbus service name
31 * @param[in] path - The Dbus object path
32 * @param[in] interface - The Dbus interface
33 * @param[in] propertyName - The property name to get
34 *
35 * @return The value of the property
36 */
37template <typename T>
Patrick Williams38679262022-07-22 19:26:55 -050038T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050039 const char* interface, const char* propertyName)
Lei YUa7417132017-02-23 15:24:05 +080040{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050041 auto method = bus.new_method_call(service, path,
42 "org.freedesktop.DBus.Properties", "Get");
Lei YUa7417132017-02-23 15:24:05 +080043 method.append(interface, propertyName);
Lei YU86c83f32018-07-13 15:14:56 +080044 try
Lei YUa7417132017-02-23 15:24:05 +080045 {
Patrick Williamsc09ac3f2020-05-13 18:01:29 -050046 std::variant<T> value{};
Lei YU86c83f32018-07-13 15:14:56 +080047 auto reply = bus.call(method);
Lei YUa7417132017-02-23 15:24:05 +080048 reply.read(value);
Patrick Williams5b746c72020-05-13 11:49:35 -050049 return std::get<T>(value);
Lei YUa7417132017-02-23 15:24:05 +080050 }
Patrick Williams38679262022-07-22 19:26:55 -050051 catch (const sdbusplus::exception_t& ex)
Lei YUa7417132017-02-23 15:24:05 +080052 {
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -050053 error("GetProperty call failed, path:{PATH}, interface:{INTF}, "
54 "propertyName:{NAME}, error:{ERROR}",
55 "PATH", path, "INTF", interface, "NAME", propertyName, "ERROR",
56 ex);
Lei YU86c83f32018-07-13 15:14:56 +080057 throw std::runtime_error("GetProperty call failed");
Lei YUa7417132017-02-23 15:24:05 +080058 }
Lei YUa7417132017-02-23 15:24:05 +080059}
60
Jason Zhue1010302024-04-02 04:20:43 +000061/** @brief The template function to set property to the requested dbus path
62 *
63 * @param[in] bus - The Dbus bus object
64 * @param[in] service - The Dbus service name
65 * @param[in] path - The Dbus object path
66 * @param[in] interface - The Dbus interface
67 * @param[in] propertyName - The property name to set
68 * @param[in] value - the value to set the property to
69 *
70 */
71template <typename T>
72void setProperty(sdbusplus::bus_t& bus, const std::string& service,
73 const std::string& path, const std::string& interface,
74 const std::string& propertyName, T& value)
75{
76 std::variant<T> propertyValue(value);
77
78 auto method = bus.new_method_call(service.c_str(), path.c_str(),
79 "org.freedesktop.DBus.Properties", "Set");
80
81 method.append(interface, propertyName, propertyValue);
82
83 try
84 {
85 auto reply = bus.call(method);
86 }
87 catch (const sdbusplus::exception_t& ex)
88 {
89 error("SetProperty call failed, path:{PATH}, interface:{INTF}, "
90 "propertyName:{NAME}, error:{ERROR}",
91 "PATH", path, "INTF", interface, "NAME", propertyName, "ERROR",
92 ex);
93 throw std::runtime_error("SetProperty call failed");
94 }
95}
96
Lei YUdd8e9e42017-04-19 17:46:58 +080097/** @brief Get service name from object path and interface
98 *
99 * @param[in] bus - The Dbus bus object
100 * @param[in] path - The Dbus object path
101 * @param[in] interface - The Dbus interface
102 *
103 * @return The name of the service
104 */
Patrick Williams38679262022-07-22 19:26:55 -0500105std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUdd8e9e42017-04-19 17:46:58 +0800106 const char* interface);
107
George Liudc746c02022-09-02 11:10:55 +0800108/** @brief Get sub tree from root, depth and interfaces
109 *
110 * @param[in] bus - The Dbus bus object
111 * @param[in] root - The root of the tree to search
112 * @param[in] interfaces - All interfaces in the subtree to search for
113 * @param[in] depth - The number of path elements to descend
114 *
115 * @return The name of the service
116 *
117 * @throw sdbusplus::exception_t when it fails
118 */
119MapperResponse getSubTree(sdbusplus::bus_t& bus, const std::string& root,
120 const Interfaces& interfaces, int32_t depth);
121
Lei YUddd54422017-04-18 16:38:44 +0800122/** @brief Convert a string to enum Mode
123 *
124 * Convert the time mode string to enum.
Lei YUad143542017-07-25 14:27:07 +0800125 * Valid strings are
126 * "xyz.openbmc_project.Time.Synchronization.Method.NTP"
127 * "xyz.openbmc_project.Time.Synchronization.Method.Manual"
Lei YUddd54422017-04-18 16:38:44 +0800128 * If it's not a valid time mode string, it means something
129 * goes wrong so raise exception.
130 *
131 * @param[in] mode - The string of time mode
132 *
133 * @return The Mode enum
134 */
135Mode strToMode(const std::string& mode);
136
Lei YUddd54422017-04-18 16:38:44 +0800137/** @brief Convert a mode enum to mode string
138 *
139 * @param[in] mode - The Mode enum
140 *
141 * @return The string of the mode
142 */
Lei YUad143542017-07-25 14:27:07 +0800143std::string modeToStr(Mode mode);
Lei YUddd54422017-04-18 16:38:44 +0800144
Lei YUddd54422017-04-18 16:38:44 +0800145} // namespace utils
146} // namespace time
147} // namespace phosphor