1 #pragma once
2
3 #include <boost/type_traits.hpp>
4 #include <sdbusplus/asio/connection.hpp>
5 #include <sdbusplus/utility/type_traits.hpp>
6
7 namespace sdbusplus::asio
8 {
9
10 template <typename VariantType>
getAllProperties(sdbusplus::asio::connection & bus,const std::string & service,const std::string & path,const std::string & interface,std::function<void (const boost::system::error_code,const std::vector<std::pair<std::string,VariantType>> &)> && handler)11 inline void getAllProperties(
12 sdbusplus::asio::connection& bus, const std::string& service,
13 const std::string& path, const std::string& interface,
14 std::function<void(
15 const boost::system::error_code,
16 const std::vector<std::pair<std::string, VariantType>>&)>&& handler)
17 {
18 static_assert(std::is_same_v<VariantType, std::decay_t<VariantType>>);
19
20 bus.async_method_call(
21 [handler = std::move(handler)](
22 const boost::system::error_code ec,
23 const std::vector<std::pair<std::string, VariantType>>&
24 data) mutable { handler(ec, data); },
25 service, path, "org.freedesktop.DBus.Properties", "GetAll", interface);
26 }
27
28 template <typename Handler>
getAllProperties(sdbusplus::asio::connection & bus,const std::string & service,const std::string & path,const std::string & interface,Handler && handler)29 inline void getAllProperties(
30 sdbusplus::asio::connection& bus, const std::string& service,
31 const std::string& path, const std::string& interface, Handler&& handler)
32 {
33 using arg1_type =
34 std::tuple_element_t<1, boost::callable_traits::args_t<Handler>>;
35 using arg1_pair_type = typename std::decay_t<arg1_type>::value_type;
36 using arg1_value_type = typename arg1_pair_type::second_type;
37 getAllProperties<arg1_value_type>(bus, service, path, interface,
38 std::forward<Handler>(handler));
39 }
40
41 template <typename PropertyType>
getProperty(sdbusplus::asio::connection & bus,const std::string & service,const std::string & path,const std::string & interface,const std::string & propertyName,std::function<void (boost::system::error_code,PropertyType)> && handler)42 inline void getProperty(
43 sdbusplus::asio::connection& bus, const std::string& service,
44 const std::string& path, const std::string& interface,
45 const std::string& propertyName,
46 std::function<void(boost::system::error_code, PropertyType)>&& handler)
47 {
48 static_assert(std::is_same_v<PropertyType, std::decay_t<PropertyType>>);
49
50 bus.async_method_call(
51 [handler = std::move(handler)](
52 boost::system::error_code ec,
53 std::variant<std::monostate, PropertyType>& ret) mutable {
54 if (ec)
55 {
56 handler(ec, {});
57 return;
58 }
59
60 if (PropertyType* value = std::get_if<PropertyType>(&ret))
61 {
62 handler(ec, std::move(*value));
63 return;
64 }
65
66 handler(boost::system::errc::make_error_code(
67 boost::system::errc::invalid_argument),
68 {});
69 },
70 service, path, "org.freedesktop.DBus.Properties", "Get", interface,
71 propertyName);
72 }
73
74 /* This method has been deprecated, and will be removed in a future revision.
75 * Use the getProperty overload above to make equivalent calls
76 */
77 template <typename T, typename OnError, typename OnSuccess>
getProperty(sdbusplus::asio::connection & bus,const std::string & service,const std::string & path,const std::string & interface,const std::string & propertyName,OnError && onError,OnSuccess && onSuccess)78 [[deprecated]] inline void getProperty(
79 sdbusplus::asio::connection& bus, const std::string& service,
80 const std::string& path, const std::string& interface,
81 const std::string& propertyName, OnError&& onError, OnSuccess&& onSuccess)
82 {
83 bus.async_method_call(
84 [onError = std::move(onError), onSuccess = std::move(onSuccess)](
85 boost::system::error_code ec,
86 std::variant<std::monostate, T>& ret) {
87 if (ec)
88 {
89 onError(ec);
90 return;
91 }
92
93 if (T* value = std::get_if<T>(&ret))
94 {
95 onSuccess(*value);
96 return;
97 }
98
99 onError(boost::system::errc::make_error_code(
100 boost::system::errc::invalid_argument));
101 },
102 service, path, "org.freedesktop.DBus.Properties", "Get", interface,
103 propertyName);
104 }
105
106 template <typename PropertyType, typename Handler>
setProperty(sdbusplus::asio::connection & bus,const std::string & service,const std::string & path,const std::string & interface,const std::string & propertyName,PropertyType && propertyValue,Handler && handler)107 inline void setProperty(sdbusplus::asio::connection& bus,
108 const std::string& service, const std::string& path,
109 const std::string& interface,
110 const std::string& propertyName,
111 PropertyType&& propertyValue, Handler&& handler)
112 {
113 bus.async_method_call(
114 std::forward<Handler>(handler), service, path,
115 "org.freedesktop.DBus.Properties", "Set", interface, propertyName,
116 std::variant<std::decay_t<PropertyType>>(
117 std::forward<PropertyType>(propertyValue)));
118 }
119
120 } // namespace sdbusplus::asio
121