1 #ifndef DALI_INTERNAL_ACCESSIBILITY_BRIDGE_DBUS_H
2 #define DALI_INTERNAL_ACCESSIBILITY_BRIDGE_DBUS_H
5 * Copyright 2020 Samsung Electronics Co., Ltd
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
31 #include <string_view>
34 #include <type_traits>
35 #include <unordered_map>
39 #include <dali/devel-api/adaptor-framework/accessibility.h>
40 #include <dali/public-api/common/dali-common.h>
42 #define ATSPI_PREFIX_PATH "/org/a11y/atspi/accessible/"
43 #define ATSPI_NULL_PATH "/org/a11y/atspi/null"
52 struct DALI_ADAPTOR_API DBusWrapper
54 virtual ~DBusWrapper() = default;
56 enum class ConnectionType
62 #define DEFINE_TYPE(name, eldbus_name, unref_call) \
65 virtual ~name() = default; \
67 using name##Ptr = std::shared_ptr<name>; \
68 using name##WeakPtr = std::weak_ptr<name>;
70 DEFINE_TYPE(Connection, Eldbus_Connection, )
71 DEFINE_TYPE(MessageIter, Eldbus_Message_Iter, eldbus_message_iter_container_close(Value))
72 DEFINE_TYPE(Message, Eldbus_Message, eldbus_message_unref(Value))
73 DEFINE_TYPE(Proxy, Eldbus_Proxy, eldbus_proxy_unref(Value))
74 DEFINE_TYPE(Object, Eldbus_Object, eldbus_object_unref(Value))
75 DEFINE_TYPE(Pending, Eldbus_Pending, )
76 DEFINE_TYPE(EventPropertyChanged, Eldbus_Proxy_Event_Property_Changed, )
79 virtual ConnectionPtr eldbus_address_connection_get_impl(const std::string& addr) = 0;
81 #define eldbus_message_iter_arguments_append_impl_basic_impl(type_set, type_get, sig) \
82 virtual void eldbus_message_iter_arguments_append_impl(const MessageIterPtr& it, type_set src) = 0; \
83 virtual bool eldbus_message_iter_get_and_next_impl(const MessageIterPtr& it, type_get& dst) = 0;
84 #define eldbus_message_iter_arguments_append_impl_basic(type, sig) \
85 eldbus_message_iter_arguments_append_impl_basic_impl(type, type, sig)
88 eldbus_message_iter_arguments_append_impl_basic(uint8_t, y)
89 eldbus_message_iter_arguments_append_impl_basic(uint16_t, q)
90 eldbus_message_iter_arguments_append_impl_basic(uint32_t, u)
91 eldbus_message_iter_arguments_append_impl_basic(uint64_t, t)
92 eldbus_message_iter_arguments_append_impl_basic(int16_t, n)
93 eldbus_message_iter_arguments_append_impl_basic(int32_t, i)
94 eldbus_message_iter_arguments_append_impl_basic(int64_t, x)
95 eldbus_message_iter_arguments_append_impl_basic(double, d)
96 eldbus_message_iter_arguments_append_impl_basic(bool, b)
97 eldbus_message_iter_arguments_append_impl_basic_impl(const std::string&, std::string, s)
98 eldbus_message_iter_arguments_append_impl_basic_impl(const ObjectPath&, ObjectPath, o)
100 #undef eldbus_message_iter_arguments_append_impl_basic
101 #undef eldbus_message_iter_arguments_append_impl_basic_impl
103 virtual MessageIterPtr eldbus_message_iter_container_new_impl(const MessageIterPtr& it, int type, const std::string& sig) = 0;
104 virtual MessageIterPtr eldbus_message_iter_get_and_next_by_type_impl(const MessageIterPtr& it, int type) = 0;
105 virtual MessageIterPtr eldbus_message_iter_get_impl(const MessagePtr& it, bool write) = 0;
106 virtual MessagePtr eldbus_proxy_method_call_new_impl(const ProxyPtr& proxy, const std::string& funcName) = 0;
107 virtual MessagePtr eldbus_proxy_send_and_block_impl(const ProxyPtr& proxy, const MessagePtr& msg) = 0;
108 virtual bool eldbus_message_error_get_impl(const MessagePtr& msg, std::string& name, std::string& text) = 0;
109 virtual std::string eldbus_message_signature_get_impl(const MessagePtr& msg) = 0;
112 using SendCallback = std::function<void(const MessagePtr& msg)>;
114 virtual PendingPtr eldbus_proxy_send_impl(const ProxyPtr& proxy, const MessagePtr& msg, const SendCallback& callback) = 0;
115 virtual std::string eldbus_proxy_interface_get_impl(const ProxyPtr&) = 0;
116 virtual void eldbus_proxy_signal_handler_add_impl(const ProxyPtr& proxy, const std::string& member, const std::function<void(const MessagePtr&)>& cb) = 0;
117 virtual std::string eldbus_message_iter_signature_get_impl(const MessageIterPtr& iter) = 0;
118 virtual MessagePtr eldbus_message_method_return_new_impl(const MessagePtr& msg) = 0;
119 virtual MessagePtr eldbus_message_error_new_impl(const MessagePtr& msg, const std::string& err, const std::string& txt) = 0;
120 virtual PendingPtr eldbus_connection_send_impl(const ConnectionPtr& conn, const MessagePtr& msg) = 0;
121 virtual MessagePtr eldbus_message_signal_new_impl(const std::string& path, const std::string& iface, const std::string& name) = 0;
122 virtual MessagePtr eldbus_message_ref_impl(const MessagePtr& msg) = 0;
123 virtual ConnectionPtr eldbus_connection_get_impl(ConnectionType type) = 0;
124 virtual std::string eldbus_connection_unique_name_get_impl(const ConnectionPtr& conn) = 0;
125 virtual ObjectPtr eldbus_object_get_impl(const ConnectionPtr& conn, const std::string& bus, const std::string& path) = 0;
126 virtual ProxyPtr eldbus_proxy_get_impl(const ObjectPtr& obj, const std::string& interface) = 0;
127 virtual ProxyPtr eldbus_proxy_copy_impl(const ProxyPtr& ptr) = 0;
132 struct char_ptr_deleter
134 void operator()(char* p)
139 std::vector<std::unique_ptr<char, char_ptr_deleter>> storage;
141 const char* add(const char* txt)
143 auto ptr = strdup(txt);
144 storage.push_back(std::unique_ptr<char, char_ptr_deleter>(ptr));
145 return storage.back().get();
147 const char* add(const std::string& txt)
149 return add(txt.c_str());
155 static std::atomic<unsigned int> LastId;
156 unsigned int id = ++LastId;
161 std::string memberName;
162 std::vector<std::pair<std::string, std::string>> in, out; // _Eldbus_Arg_Info
163 std::function<DBusWrapper::MessagePtr(const DBusWrapper::MessagePtr& msg)> callback;
168 std::string memberName;
169 std::vector<std::pair<std::string, std::string>> args;
170 unsigned int uniqueId;
174 CallId setterId, getterId;
175 std::string memberName, typeSignature;
176 std::function<std::string(const DBusWrapper::MessagePtr&src, const DBusWrapper::MessageIterPtr&dst)> getCallback, setCallback;
182 SignalId() = default;
188 virtual void add_interface_impl(bool fallback, const std::string& pathName, const ConnectionPtr& connection, std::vector<std::function<void()>>& destructors, const std::string& interfaceName, std::vector<MethodInfo>& dscrMethods, std::vector<PropertyInfo>& dscrProperties, std::vector<SignalInfo>& dscrSignals) = 0;
189 virtual void add_property_changed_event_listener_impl(const ProxyPtr& proxy, const std::string& interface, const std::string& name, std::function<void(const _Eina_Value*)> cb) = 0;
190 static DBusWrapper* Installed();
191 static void Install(std::unique_ptr<DBusWrapper>);
193 StringStorage Strings;
198 enum class MethodType
209 struct hash<std::tuple<std::string, std::string, std::string>>
211 size_t operator()(const std::tuple<std::string, std::string, std::string>& a) const
213 auto a1 = std::hash<std::string>()(std::get<0>(a));
214 auto a2 = std::hash<std::string>()(std::get<1>(a));
215 auto a3 = std::hash<std::string>()(std::get<2>(a));
217 v = (v * 11400714819323198485llu) + a2;
218 v = (v * 11400714819323198485llu) + a3;
223 struct hash<std::tuple<std::string, std::string, std::string, detail::MethodType>>
225 size_t operator()(const std::tuple<std::string, std::string, std::string, detail::MethodType>& a) const
227 auto a1 = std::hash<std::string>()(std::get<0>(a));
228 auto a2 = std::hash<std::string>()(std::get<1>(a));
229 auto a3 = std::hash<std::string>()(std::get<2>(a));
230 auto a4 = static_cast<size_t>(std::get<3>(a));
232 v = (v * 11400714819323198485llu) + a2;
233 v = (v * 11400714819323198485llu) + a3;
234 v = (v * 11400714819323198485llu) + a4;
239 struct hash<std::tuple<std::string, std::string, unsigned int>>
241 size_t operator()(const std::tuple<std::string, std::string, unsigned int>& a) const
243 auto a1 = std::hash<std::string>()(std::get<0>(a));
244 auto a2 = std::hash<std::string>()(std::get<1>(a));
245 auto a3 = std::get<2>(a);
247 v = (v * 11400714819323198485llu) + a2;
248 v = (v * 11400714819323198485llu) + a3;
270 value = DBusSigImpl<typename std::decay<T>::type>::value,
271 end = DBusSigImpl<typename std::decay<T>::type>::end
274 template<typename T, typename Q, size_t I, size_t S>
275 struct IndexFromTypeTupleImpl
279 value = std::is_same<typename std::decay<typename std::tuple_element<I, T>::type>::type, Q>::value ? I : IndexFromTypeTupleImpl<T, Q, I + 1, S>::value
282 template<typename T, typename Q, size_t S>
283 struct IndexFromTypeTupleImpl<T, Q, S, S>
290 template<typename T, typename Q>
291 struct IndexFromTypeTuple
295 value = IndexFromTypeTupleImpl<T, typename std::decay<Q>::type, 0, std::tuple_size<T>::value>::value
298 template<typename T, typename = void>
300 template<size_t I, size_t S, typename... ARGS>
302 } // namespace detail
307 struct DBusSigImpl<uint8_t>
316 struct DBusSigImpl<uint16_t>
325 struct DBusSigImpl<uint32_t>
334 struct DBusSigImpl<uint64_t>
343 struct DBusSigImpl<int16_t>
352 struct DBusSigImpl<int32_t>
361 struct DBusSigImpl<int64_t>
370 struct DBusSigImpl<double>
379 struct DBusSigImpl<bool>
388 struct DBusSigImpl<std::string>
397 struct DBusSigImpl<ObjectPath>
405 } // namespace detail
407 #define DBUS_DEBUG(...) \
410 DBus::debugPrint(__FILE__, __LINE__, __VA_ARGS__); \
413 #define DBUS_W DBusWrapper::Installed()
416 * @brief Template based, single file, wrapper library around eldbus for DBUS based communication.
418 * Main motivation was missing asynchronous calls in AT-SPI library and difficulties,
419 * when using eldbus from C++.
422 * - takes care of marshalling arguments to and from DBUS calls.
423 * - allows synchronous and asynchronous calls.
424 * - allows synchronous and asynchronous listeners on signals.
425 * - manages all involved objects' lifetimes.
426 * - errors are passed as optional-alike objects, no exceptions are used.
427 * - allows setting additional debug-print function for more details about
430 * DBUS's method signatures (and expected return values) are specified as template argument,
431 * using functor syntax. For example:
433 * auto dbus = DBusClient{ ... };
434 * auto v = dbus.method<std::tuple<int, float>(float, float, std::string)>("foo").call(1.0f, 2.0f, "qwe");
436 * means (synchronous) call on dbus object, which takes three arguments (thus making call signature \b dds)
437 * of types float, float and string (float will be automatically converted to double).
438 * Expected return value is std::tuple<int, float>, which gives signature <B>(id)</B> - std::tuple acts
439 * as struct container. Returned value v will be of type ValueOrError<std::tuple<int, float>>.\n
440 * Slightly different (asynchronous) example:
442 * auto dbus = DBusClient{ ... };
443 * std::function<void(ValueOrError<int, float>)> callback;
444 * dbus.method<ValueOrError<int, float>(float, float, std::string)>("foo").asyncCall(callback, 1.0f, 2.0f, "qwe");
446 * Now the call takes the same arguments and has the same signature. But expected values are different -
447 * now the signature is simply \b id. ValueOrError acts in this case as placeholder for list of values,
448 * which DBUS allows as return data. The call itself is asynchronous - instead of expecting reply
449 * you need to pass a callback, which will be called either with received data and error message.
451 * Library is not thread-safe, the same object shouldn't be called from different threads without
452 * synchronization. There's no guarantee, that callbacks will be executed on the same thread.
458 class DBusInterfaceDescription;
461 * @brief Formats debug message and calls debug printer (if any) with it
463 void debugPrint(const char* file, size_t line, const char* format, ...);
466 * @brief Sets debug printer callback, which will be called with debug messages
468 * Callback will be called in various moments of DBus activity. First value passed to callback
469 * is pointer to text, second it's length. Text is ended with 0 (not counted towards it's size),
470 * user can safely printf it.
472 void setDebugPrinter(std::function<void(const char*, size_t)>);
479 Error(std::string msg)
480 : message(std::move(msg))
482 assert(!message.empty());
491 * @brief Value representing data, that came from DBUS or error message
493 * Object of this class either helds series of values (of types ARGS...)
494 * or error message. This object will be true in boolean context, if has data
495 * and false, if an error occured.
496 * It's valid to create ValueOrError object with empty argument list or void:
499 * ValueOrError<void> v2;
501 * Both mean the same - ValueOrError containing no real data and being a marker,
502 * wherever operation successed or failed and containing possible error message.
504 template<typename... ARGS>
509 * @brief Empty constructor. Valid only, if all ARGS types are default constructible.
511 ValueOrError() = default;
514 * @brief Value constructor.
516 * This will be initialized as success with passed in values.
518 ValueOrError(ARGS... t)
519 : value(std::move(t)...)
524 * @brief Alternative Value constructor.
526 * This will be initialized as success with passed in values.
528 ValueOrError(std::tuple<ARGS...> t)
529 : value(std::move(t))
534 * @brief Error constructor. This will be initialized as failure with given error message.
536 ValueOrError(Error e)
537 : error(std::move(e))
539 assert(!error.message.empty());
543 * @brief bool operator.
545 * Returns true, if operation was successful (getValues member is callable), or false
546 * when operation failed (getError is callable).
548 explicit operator bool() const
550 return error.message.empty();
554 * @brief Returns error message object.
556 * Returns object containing error message associated with the failed operation.
557 * Only callable, if operation actually failed, otherwise will assert.
559 const Error& getError() const
565 * @brief Returns modifiable tuple of held data.
567 * Returns reference to the internal tuple containing held data.
568 * User can modify (or move) data safely.
569 * Only callable, if operation actually successed, otherwise will assert.
571 std::tuple<ARGS...>& getValues()
578 * @brief Returns const tuple of held data.
580 * Returns const reference to the internal tuple containing held data.
581 * Only callable, if operation actually successed, otherwise will assert.
583 const std::tuple<ARGS...>& getValues() const
590 std::tuple<ARGS...> value;
598 ValueOrError() = default;
599 ValueOrError(std::tuple<> t)
602 ValueOrError(Error e)
603 : error(std::move(e))
605 assert(!error.message.empty());
608 explicit operator bool() const
610 return error.message.empty();
612 const Error& getError() const
616 std::tuple<>& getValues()
619 static std::tuple<> t;
622 std::tuple<> getValues() const
633 class ValueOrError<void>
636 ValueOrError() = default;
637 ValueOrError(Success)
640 ValueOrError(Error e)
641 : error(std::move(e))
643 assert(!error.message.empty());
646 explicit operator bool() const
648 return error.message.empty();
650 const Error& getError() const
654 std::tuple<>& getValues()
657 static std::tuple<> t;
660 std::tuple<> getValues() const
670 using ObjectPath = ObjectPath;
673 * @brief Class used to marshall DBUS's variant type
675 * Minimalistic class, that allows user to specify DBUS variant type
676 * as argument or return value. You need to pass real type hidden under variant as
677 * template type \b A. At this point library doesn't allow to expected one of few classes
678 * as return data in variant. So for example user can't specify method call, which on return
679 * expects DBUS variant holding either string or int.
688 * @brief Namespace for private, internal functions and classes
692 inline namespace strings
694 template<std::size_t N>
695 using char_array = std::array<char, N>;
697 constexpr char* xcopy_n(const char* src, size_t n, char* dst)
699 for(std::size_t i = 0; i < n; ++i)
705 template<std::size_t N>
706 constexpr std::size_t xlen(const char (&)[N])
711 template<std::size_t N>
712 constexpr std::size_t xlen(const char_array<N>&)
717 template<typename... Args>
718 constexpr auto concat(const Args&... args)
720 char_array<(1U + ... + xlen(args))> arr{};
721 char* ptr = arr.data();
723 if constexpr(!arr.empty())
724 ((ptr = xcopy_n(std::data(args), xlen(args), ptr)), ...);
729 template<std::size_t... Digits>
732 static constexpr const char value[] = {('0' + static_cast<char>(Digits))..., '\0'};
734 static_assert((true && ... && (Digits < 10)));
737 template<std::size_t Remainder, std::size_t... Digits>
738 struct to_chars_helper : to_chars_helper<Remainder / 10, Remainder % 10, Digits...>
742 template<std::size_t... Digits>
743 struct to_chars_helper<0, Digits...> : to_chars<Digits...>
747 template<std::size_t N>
748 using to_string = to_chars_helper<N / 10, N % 10>;
749 } // namespace strings
751 template<typename T, typename = void>
753 template<typename... ARGS>
754 struct signature<std::tuple<ARGS...>>;
755 template<typename A, typename B>
756 struct signature<std::pair<A, B>>;
758 struct signature<std::vector<A>>;
759 template<typename A, size_t N>
760 struct signature<std::array<A, N>>;
761 template<typename A, typename B>
762 struct signature<std::unordered_map<A, B>>;
763 template<typename A, typename B>
764 struct signature<std::map<A, B>>;
767 struct signature_helper
770 * @brief Returns name of type marshalled, for informative purposes
772 static constexpr std::string_view name()
774 return {T::name_v.data()};
778 * @brief Returns DBUS' signature of type marshalled
780 static constexpr std::string_view sig()
782 return {T::sig_v.data()};
787 * @brief Signature class for marshalling uint8 type.
790 struct signature<uint8_t> : signature_helper<signature<uint8_t>>
792 static constexpr auto name_v = concat("uint8_t");
793 static constexpr auto sig_v = concat("y");
796 * @brief Marshals value v as marshalled type into message
798 static void set(const DBusWrapper::MessageIterPtr& iter, uint8_t v)
800 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
804 * @brief Marshals value from marshalled type into variable v
806 static bool get(const DBusWrapper::MessageIterPtr& iter, uint8_t& v)
808 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
813 * @brief Signature class for marshalling uint16 type.
816 struct signature<uint16_t> : signature_helper<signature<uint16_t>>
818 static constexpr auto name_v = concat("uint16_t");
819 static constexpr auto sig_v = concat("q");
822 * @brief Marshals value v as marshalled type into message
824 static void set(const DBusWrapper::MessageIterPtr& iter, uint16_t v)
826 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
830 * @brief Marshals value from marshalled type into variable v
832 static bool get(const DBusWrapper::MessageIterPtr& iter, uint16_t& v)
834 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
839 * @brief Signature class for marshalling uint32 type.
842 struct signature<uint32_t> : signature_helper<signature<uint32_t>>
844 static constexpr auto name_v = concat("uint32_t");
845 static constexpr auto sig_v = concat("u");
848 * @brief Marshals value v as marshalled type into message
850 static void set(const DBusWrapper::MessageIterPtr& iter, uint32_t v)
852 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
856 * @brief Marshals value from marshalled type into variable v
858 static bool get(const DBusWrapper::MessageIterPtr& iter, uint32_t& v)
860 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
865 * @brief Signature class for marshalling uint64 type.
868 struct signature<uint64_t> : signature_helper<signature<uint64_t>>
870 static constexpr auto name_v = concat("uint64_t");
871 static constexpr auto sig_v = concat("t");
874 * @brief Marshals value v as marshalled type into message
876 static void set(const DBusWrapper::MessageIterPtr& iter, uint64_t v)
878 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
882 * @brief Marshals value from marshalled type into variable v
884 static bool get(const DBusWrapper::MessageIterPtr& iter, uint64_t& v)
886 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
891 * @brief Signature class for marshalling int16 type.
894 struct signature<int16_t> : signature_helper<signature<int16_t>>
896 static constexpr auto name_v = concat("int16_t");
897 static constexpr auto sig_v = concat("n");
900 * @brief Marshals value v as marshalled type into message
902 static void set(const DBusWrapper::MessageIterPtr& iter, int16_t v)
904 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
908 * @brief Marshals value from marshalled type into variable v
910 static bool get(const DBusWrapper::MessageIterPtr& iter, int16_t& v)
912 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
917 * @brief Signature class for marshalling int32 type.
920 struct signature<int32_t> : signature_helper<signature<int32_t>>
922 static constexpr auto name_v = concat("int32_t");
923 static constexpr auto sig_v = concat("i");
926 * @brief Marshals value v as marshalled type into message
928 static void set(const DBusWrapper::MessageIterPtr& iter, int32_t v)
930 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
934 * @brief Marshals value from marshalled type into variable v
936 static bool get(const DBusWrapper::MessageIterPtr& iter, int32_t& v)
938 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
943 * @brief Signature class for marshalling int64 type.
946 struct signature<int64_t> : signature_helper<signature<int64_t>>
948 static constexpr auto name_v = concat("int64_t");
949 static constexpr auto sig_v = concat("x");
952 * @brief Marshals value v as marshalled type into message
954 static void set(const DBusWrapper::MessageIterPtr& iter, int64_t v)
956 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
960 * @brief Marshals value from marshalled type into variable v
962 static bool get(const DBusWrapper::MessageIterPtr& iter, int64_t& v)
964 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
969 * @brief Signature class for marshalling double type.
972 struct signature<double> : signature_helper<signature<double>>
974 static constexpr auto name_v = concat("double");
975 static constexpr auto sig_v = concat("d");
978 * @brief Marshals value v as marshalled type into message
980 static void set(const DBusWrapper::MessageIterPtr& iter, double v)
982 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
986 * @brief Marshals value from marshalled type into variable v
988 static bool get(const DBusWrapper::MessageIterPtr& iter, double& v)
990 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
994 * @brief Marshals value from marshalled type into variable v
996 static bool get(const DBusWrapper::MessageIterPtr& iter, float& v2)
999 auto r = DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
1000 v2 = static_cast<float>(v);
1006 * @brief Signature class for marshalling float type.
1009 struct signature<float> : signature_helper<signature<float>>
1011 static constexpr auto name_v = concat("float");
1012 static constexpr auto sig_v = concat("d");
1015 * @brief Marshals value v as marshalled type into message
1017 static void set(const DBusWrapper::MessageIterPtr& iter, float v)
1019 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
1023 * @brief Marshals value from marshalled type into variable v
1025 static bool get(const DBusWrapper::MessageIterPtr& iter, double& v)
1027 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
1031 * @brief Marshals value from marshalled type into variable v
1033 static bool get(const DBusWrapper::MessageIterPtr& iter, float& v2)
1036 auto r = DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
1037 v2 = static_cast<float>(v);
1043 * @brief Signature class for marshalling boolean type.
1046 struct signature<bool> : signature_helper<signature<bool>>
1048 static constexpr auto name_v = concat("bool");
1049 static constexpr auto sig_v = concat("b");
1052 * @brief Marshals value v as marshalled type into message
1054 static void set(const DBusWrapper::MessageIterPtr& iter, bool v)
1056 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
1060 * @brief Marshals value from marshalled type into variable v
1062 static bool get(const DBusWrapper::MessageIterPtr& iter, bool& v)
1064 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
1068 template<typename T>
1069 struct signature<T, typename std::enable_if_t<std::is_enum_v<T>, void>> : signature_helper<signature<T>>
1071 static constexpr auto name_v = concat("enum");
1072 static constexpr auto sig_v = signature<typename std::underlying_type<T>::type>::sig_v;
1075 * @brief Marshals value v as marshalled type into message
1077 static void set(const DBusWrapper::MessageIterPtr& iter, T v)
1079 signature<typename std::underlying_type<T>::type>::set(iter, static_cast<int64_t>(v));
1083 * @brief Marshals value from marshalled type into variable v
1085 static bool get(const DBusWrapper::MessageIterPtr& iter, T& v)
1087 typename std::underlying_type<T>::type q = 0;
1088 if(!signature<typename std::underlying_type<T>::type>::get(iter, q))
1091 v = static_cast<T>(q);
1097 * @brief Signature class for marshalling string type.
1099 * Both (const) char * and std::string types are accepted as value to send.
1100 * Only std::string is accepted as value to receive.
1103 struct signature<std::string> : signature_helper<signature<std::string>>
1105 static constexpr auto name_v = concat("string");
1106 static constexpr auto sig_v = concat("s");
1109 * @brief Marshals value v as marshalled type into message
1111 static void set(const DBusWrapper::MessageIterPtr& iter, const std::string& v)
1113 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
1117 * @brief Marshals value from marshalled type into variable v
1119 static bool get(const DBusWrapper::MessageIterPtr& iter, std::string& v)
1121 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
1126 struct signature<ObjectPath> : signature_helper<signature<ObjectPath>>
1128 static constexpr auto name_v = concat("path");
1129 static constexpr auto sig_v = concat("o");
1132 * @brief Marshals value v as marshalled type into message
1134 static void set(const DBusWrapper::MessageIterPtr& iter, const std::string& v)
1136 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, ObjectPath{v});
1140 * @brief Marshals value v as marshalled type into message
1142 static void set(const DBusWrapper::MessageIterPtr& iter, const ObjectPath& v)
1144 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
1148 * @brief Marshals value v as marshalled type into message
1150 static void set(const DBusWrapper::MessageIterPtr& iter, const char* v)
1152 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, ObjectPath{v});
1156 * @brief Marshals value from marshalled type into variable v
1158 static bool get(const DBusWrapper::MessageIterPtr& iter, ObjectPath& v)
1160 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
1164 * @brief Marshals value from marshalled type into variable v
1166 static bool get(const DBusWrapper::MessageIterPtr& iter, std::string& v)
1169 if(!DBUS_W->eldbus_message_iter_get_and_next_impl(iter, q)) return false;
1170 v = std::move(q.value);
1176 * @brief Signature class for marshalling (const) char * type.
1178 * Both (const) char * and std::string types are accepted as value to send.
1179 * You can't use (const) char * variable type to receive value.
1182 struct signature<char*> : signature_helper<signature<char*>>
1184 static constexpr auto name_v = concat("string");
1185 static constexpr auto sig_v = concat("s");
1188 * @brief Marshals value v as marshalled type into message
1190 static void set(const DBusWrapper::MessageIterPtr& iter, const std::string& v)
1192 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
1196 * @brief Marshals value v as marshalled type into message
1198 static void set(const DBusWrapper::MessageIterPtr& iter, const char* v)
1200 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, std::string{v});
1204 template<size_t INDEX, typename A, typename... ARGS>
1205 struct signature_tuple_element_type_helper
1207 using type = typename signature_tuple_element_type_helper<INDEX - 1, ARGS...>::type;
1210 template<typename A, typename... ARGS>
1211 struct signature_tuple_element_type_helper<0, A, ARGS...>
1217 * @brief Helper class to marshall tuples
1219 * This class marshals all elements of the tuple value starting at the index INDEX
1220 * and incrementing. This class recursively calls itself with increasing INDEX value
1221 * until INDEX is equal to SIZE, where recursive calling ends.
1223 template<size_t INDEX, size_t SIZE, typename... ARGS>
1224 struct signature_tuple_helper : signature_helper<signature_tuple_helper<INDEX, SIZE, ARGS...>>
1226 using current_type = typename signature_tuple_element_type_helper<INDEX, ARGS...>::type;
1228 static constexpr auto name_v = concat(signature<current_type>::name_v, ", ", signature_tuple_helper<INDEX + 1, SIZE, ARGS...>::name_v);
1229 static constexpr auto sig_v = concat(signature<current_type>::sig_v, signature_tuple_helper<INDEX + 1, SIZE, ARGS...>::sig_v);
1232 * @brief Marshals value v as marshalled type into message
1234 static void set(const DBusWrapper::MessageIterPtr& iter, const std::tuple<ARGS...>& args)
1236 signature<current_type>::set(iter, std::get<INDEX>(args));
1237 signature_tuple_helper<INDEX + 1, SIZE, ARGS...>::set(iter, args);
1241 * @brief Marshals value from marshalled type into variable v
1243 static bool get(const DBusWrapper::MessageIterPtr& iter, std::tuple<ARGS...>& args)
1245 return signature<current_type>::get(iter, std::get<INDEX>(args)) &&
1246 signature_tuple_helper<INDEX + 1, SIZE, ARGS...>::get(iter, args);
1251 * @brief Helper class to marshall tuples
1253 * This class marks end of the tuple marshalling. Members of this class are called
1254 * when INDEX value is equal to SIZE.
1256 template<size_t SIZE, typename... ARGS>
1257 struct signature_tuple_helper<SIZE, SIZE, ARGS...> : signature_helper<signature_tuple_helper<SIZE, SIZE, ARGS...>>
1259 static constexpr auto name_v = concat("");
1260 static constexpr auto sig_v = concat("");
1263 * @brief Marshals value v as marshalled type into message
1265 static void set(const DBusWrapper::MessageIterPtr& iter, const std::tuple<ARGS...>& args)
1270 * @brief Marshals value from marshalled type into variable v
1272 static bool get(const DBusWrapper::MessageIterPtr& iter, std::tuple<ARGS...>& args)
1279 * @brief Signature class for marshalling tuple of values
1281 * This class marshalls tuple of values. This represents
1282 * DBUS struct typle, encoded with character 'r'
1284 template<typename... ARGS>
1285 struct signature<std::tuple<ARGS...>> : signature_helper<signature<std::tuple<ARGS...>>>
1287 static constexpr auto name_v = concat("tuple<", signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::name_v, ">");
1288 static constexpr auto sig_v = concat("(", signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::sig_v, ")");
1291 * @brief Marshals value v as marshalled type into message
1293 static void set(const DBusWrapper::MessageIterPtr& iter, const std::tuple<ARGS...>& args)
1295 auto entry = DBUS_W->eldbus_message_iter_container_new_impl(iter, 'r', "");
1296 signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::set(entry, args);
1300 * @brief Marshals value from marshalled type into variable v
1302 static bool get(const DBusWrapper::MessageIterPtr& iter, std::tuple<ARGS...>& args)
1304 auto entry = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, 'r');
1305 if(!entry) return false;
1306 return signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::get(entry, args);
1311 * @brief Signature class for marshalling ValueOrError template type
1313 * ValueOrError template type is used to marshall list of values passed to
1314 * DBUS (or received from) at the "top" level. For example ss(s) is represented as
1315 * \code{.cpp} ValueOrError<std::string, std::string, std::tuple<std::string>> \endcode
1316 * While (ss(s)) is represented as
1317 * \code{.cpp} std::tuple<std::string, std::string, std::tuple<std::string>> \endcode
1319 * \code{.cpp} ValueOrError<std::tuple<std::string, std::string, std::tuple<std::string>>> \endcode
1321 template<typename... ARGS>
1322 struct signature<ValueOrError<ARGS...>> : signature_helper<signature<ValueOrError<ARGS...>>>
1324 static constexpr auto name_v = concat("ValueOrError<", signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::name_v, ">");
1325 static constexpr auto sig_v = signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::sig_v;
1328 * @brief Marshals value v as marshalled type into message
1330 static void set(const DBusWrapper::MessageIterPtr& iter, const ValueOrError<ARGS...>& args)
1332 signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::set(iter, args.getValues());
1336 * @brief Marshals value from marshalled type into variable v
1338 static bool get(const DBusWrapper::MessageIterPtr& iter, ValueOrError<ARGS...>& args)
1340 return signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::get(iter, args.getValues());
1345 * @brief Signature class for marshalling ValueOrError<void> type
1348 struct signature<ValueOrError<void>> : signature_helper<signature<ValueOrError<void>>>
1350 static constexpr auto name_v = concat("ValueOrError<void>");
1351 static constexpr auto sig_v = concat("");
1354 * @brief Marshals value v as marshalled type into message
1356 static void set(const DBusWrapper::MessageIterPtr& iter, const ValueOrError<void>& args)
1361 * @brief Marshals value from marshalled type into variable v
1363 static bool get(const DBusWrapper::MessageIterPtr& iter, ValueOrError<void>& args)
1370 * @brief Signature class for marshalling ValueOrError<> type
1373 struct signature<ValueOrError<>> : signature_helper<signature<ValueOrError<>>>
1375 static constexpr auto name_v = concat("ValueOrError<>");
1376 static constexpr auto sig_v = concat("");
1379 * @brief Marshals value v as marshalled type into message
1381 static void set(const DBusWrapper::MessageIterPtr& iter, const ValueOrError<>& args)
1386 * @brief Marshals value from marshalled type into variable v
1388 static bool get(const DBusWrapper::MessageIterPtr& iter, ValueOrError<>& args)
1395 * @brief Signature class for marshalling pair of types
1397 template<typename A, typename B>
1398 struct signature<std::pair<A, B>> : signature_helper<signature<std::pair<A, B>>>
1400 static constexpr auto name_v = concat("pair<", signature_tuple_helper<0, 2, A, B>::name_v, ">");
1401 static constexpr auto sig_v = concat("(", signature_tuple_helper<0, 2, A, B>::sig_v, ")");
1404 * @brief Marshals value v as marshalled type into message
1406 static void set(const DBusWrapper::MessageIterPtr& iter, const std::pair<A, B>& ab, bool dictionary = false)
1408 auto entry = DBUS_W->eldbus_message_iter_container_new_impl(iter, dictionary ? 'e' : 'r', "");
1409 signature_tuple_helper<0, 2, A, B>::set(entry, ab);
1413 * @brief Marshals value from marshalled type into variable v
1415 static bool get(const DBusWrapper::MessageIterPtr& iter, std::pair<A, B>& ab)
1417 auto entry = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, 'r');
1420 entry = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, '{');
1421 if(!entry) return false;
1424 std::tuple<A, B> ab_tmp;
1425 auto z = signature_tuple_helper<0, 2, A, B>::get(entry, ab_tmp);
1428 ab.first = std::move(std::get<0>(ab_tmp));
1429 ab.second = std::move(std::get<1>(ab_tmp));
1436 * @brief Signature class for marshalling std::vector template type
1438 * This marshals container's content as DBUS a ascii character type code.
1440 template<typename A>
1441 struct signature<std::vector<A>> : signature_helper<signature<std::vector<A>>>
1443 static constexpr auto name_v = concat("vector<", signature<A>::name_v, ">");
1444 static constexpr auto sig_v = concat("a", signature<A>::sig_v);
1447 * @brief Marshals value v as marshalled type into message
1449 static void set(const DBusWrapper::MessageIterPtr& iter, const std::vector<A>& v)
1451 auto lst = DBUS_W->eldbus_message_iter_container_new_impl(iter, 'a', std::string{signature<A>::sig()});
1455 signature<A>::set(lst, a);
1460 * @brief Marshals value from marshalled type into variable v
1462 static bool get(const DBusWrapper::MessageIterPtr& iter, std::vector<A>& v)
1464 auto s = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, 'a');
1465 if(!s) return false;
1468 while(signature<A>::get(s, a))
1469 v.push_back(std::move(a));
1476 * @brief Signature class for marshalling std::array template type
1478 * This marshals container's content as DBUS a ascii character type code.
1480 template<typename A, size_t N>
1481 struct signature<std::array<A, N>> : signature_helper<signature<std::array<A, N>>>
1483 static constexpr auto name_v = concat("array<", signature<A>::name_v, ", ", to_string<N>::value, ">");
1484 static constexpr auto sig_v = concat("a", signature<A>::sig_v);
1487 * @brief Marshals value v as marshalled type into message
1489 static void set(const DBusWrapper::MessageIterPtr& iter, const std::array<A, N>& v)
1491 auto lst = DBUS_W->eldbus_message_iter_container_new_impl(iter, 'a', std::string{signature<A>::sig()});
1495 signature<A>::set(lst, a);
1500 * @brief Marshals value from marshalled type into variable v
1502 static bool get(const DBusWrapper::MessageIterPtr& iter, std::array<A, N>& v)
1504 auto s = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, 'a');
1509 if(!signature<A>::get(s, a))
1517 * @brief Signature class for marshalling EldbusVariant type
1519 * This marshals variant's content as DBUS v ascii character type code.
1521 template<typename A>
1522 struct signature<EldbusVariant<A>> : signature_helper<signature<EldbusVariant<A>>>
1524 static constexpr auto name_v = concat("variant<", signature<A>::name_v, ">");
1525 static constexpr auto sig_v = concat("v");
1528 * @brief Marshals value v as marshalled type into message
1530 static void set(const DBusWrapper::MessageIterPtr& iter, const EldbusVariant<A>& v)
1536 * @brief Marshals value v as marshalled type into message
1538 static void set(const DBusWrapper::MessageIterPtr& iter, const A& v)
1540 auto var = DBUS_W->eldbus_message_iter_container_new_impl(iter, 'v', std::string{signature<A>::sig()});
1541 signature<A>::set(var, v);
1545 * @brief Marshals value from marshalled type into variable v
1547 static bool get(const DBusWrapper::MessageIterPtr& iter, EldbusVariant<A>& v)
1549 auto s = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, 'v');
1552 return signature<A>::get(s, v.value);
1557 * @brief Signature class for marshalling std::unordered_map template type
1559 * This marshals container's content as DBUS {} ascii character type code.
1560 * Note, that library doesnt check, if the key is basic type, as DBUS
1561 * specification mandates.
1562 * User can always exchange std::unordered_map for std::map and the reverse.
1563 * User can receive such values as std::vector of std::pair<key, value> values.
1564 * Order of such values is unspecified.
1566 template<typename A, typename B>
1567 struct signature<std::unordered_map<A, B>> : signature_helper<signature<std::unordered_map<A, B>>>
1569 static constexpr auto name_v = concat("unordered_map<", signature<A>::name_v, ", ", signature<B>::name_v, ">");
1570 static constexpr auto sig_v = concat("a{", signature_tuple_helper<0, 2, A, B>::sig_v, "}");
1573 * @brief Marshals value v as marshalled type into message
1575 static void set(const DBusWrapper::MessageIterPtr& iter, const std::unordered_map<A, B>& v)
1577 auto sig = "{" + std::string{signature_tuple_helper<0, 2, A, B>::sig()} + "}";
1578 auto lst = DBUS_W->eldbus_message_iter_container_new_impl(iter, 'a', sig);
1582 signature<std::pair<A, B>>::set(lst, a, true);
1587 * @brief Marshals value from marshalled type into variable v
1589 static bool get(const DBusWrapper::MessageIterPtr& iter, std::unordered_map<A, B>& v)
1591 auto s = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, 'a');
1596 while(signature<std::pair<A, B>>::get(s, a))
1597 v.insert(std::move(a));
1603 * @brief Signature class for marshalling std::unordered_map template type
1605 * This marshals container's content as DBUS {} ascii character type code.
1606 * Note, that library doesnt check, if the key is basic type, as DBUS
1607 * specification mandates.
1608 * User can always exchange std::unordered_map for std::map and the reverse.
1609 * User can receive such values as std::vector of std::pair<key, value> values.
1610 * Order of such values is unspecified.
1612 template<typename A, typename B>
1613 struct signature<std::map<A, B>> : signature_helper<signature<std::map<A, B>>>
1615 static constexpr auto name_v = concat("map<", signature<A>::name_v, ", ", signature<B>::name_v, ">");
1616 static constexpr auto sig_v = concat("a{", signature_tuple_helper<0, 2, A, B>::sig_v, "}");
1619 * @brief Marshals value v as marshalled type into message
1621 static void set(const DBusWrapper::MessageIterPtr& iter, const std::map<A, B>& v)
1623 auto sig = "{" + std::string{signature_tuple_helper<0, 2, A, B>::sig()} + "}";
1624 auto lst = DBUS_W->eldbus_message_iter_container_new_impl(iter, 'a', sig);
1628 signature<std::pair<A, B>>::set(lst, a, true);
1633 * @brief Marshals value from marshalled type into variable v
1635 static bool get(const DBusWrapper::MessageIterPtr& iter, std::map<A, B>& v)
1637 auto s = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, 'a');
1641 while(signature<std::pair<A, B>>::get(s, a))
1642 v.insert(std::move(a));
1648 * @brief Signature helper class for marshalling const reference types
1650 template<typename A>
1651 struct signature<const A&> : signature_helper<signature<const A&>>
1653 static constexpr auto name_v = concat("const ", signature<A>::name_v, "&");
1654 static constexpr auto sig_v = signature<A>::sig_v;
1657 * @brief Marshals value v as marshalled type into message
1659 static void set(const DBusWrapper::MessageIterPtr& iter, const A& v)
1661 signature<A>::set(iter, v);
1665 * @brief Marshals value from marshalled type into variable v
1667 static void get(const DBusWrapper::MessageIterPtr& iter, A& v)
1669 signature<A>::get(iter, v);
1674 * @brief Signature helper class for marshalling reference types
1676 template<typename A>
1677 struct signature<A&> : signature_helper<signature<A&>>
1679 static constexpr auto name_v = concat(signature<A>::name_v, "&");
1680 static constexpr auto sig_v = signature<A>::sig_v;
1683 * @brief Marshals value v as marshalled type into message
1685 static void set(const DBusWrapper::MessageIterPtr& iter, const A& v)
1687 signature<A>::set(iter, v);
1691 * @brief Marshals value from marshalled type into variable v
1693 static void get(const DBusWrapper::MessageIterPtr& iter, A& v)
1695 signature<A>::get(iter, v);
1700 * @brief Signature helper class for marshalling const types
1702 template<typename A>
1703 struct signature<const A> : signature_helper<signature<const A>>
1705 static constexpr auto name_v = concat("const ", signature<A>::name_v);
1706 static constexpr auto sig_v = signature<A>::sig_v;
1709 * @brief Marshals value v as marshalled type into message
1711 static void set(const DBusWrapper::MessageIterPtr& iter, const A& v)
1713 signature<A>::set(iter, v);
1717 * @brief Marshals value from marshalled type into variable v
1719 static void get(const DBusWrapper::MessageIterPtr& iter, A& v)
1721 signature<A>::get(iter, v);
1725 using CallId = DBusWrapper::CallId;
1727 template<typename ValueType>
1728 ValueType unpackValues(CallId callId, const DBusWrapper::MessagePtr& msg)
1730 static const std::string sig{signature<ValueType>::sig().data()};
1732 auto iter = DBUS_W->eldbus_message_iter_get_impl(msg, false);
1737 if(!signature<ValueType>::get(iter, r))
1739 DBUS_DEBUG("ValueType is %s", signature<ValueType>::name().data());
1740 r = Error{"call " + std::to_string(callId.id) + ": failed to unpack values, got signature '" +
1741 DBUS_W->eldbus_message_signature_get_impl(msg) + "', expected '" + sig + "'"};
1746 r = Error{"call " + std::to_string(callId.id) + ": failed to get iterator"};
1751 inline void packValues_helper(const DBusWrapper::MessageIterPtr&)
1755 template<typename A, typename... ARGS>
1756 void packValues_helper(const DBusWrapper::MessageIterPtr& iter, A&& a, ARGS&&... r)
1758 signature<A>::set(iter, std::forward<A>(a));
1759 packValues_helper(iter, std::forward<ARGS>(r)...);
1762 template<typename... ARGS>
1763 void packValues(CallId callId, const DBusWrapper::MessagePtr& msg, ARGS&&... r)
1765 auto iter = DBUS_W->eldbus_message_iter_get_impl(msg, true);
1766 packValues_helper(iter, std::forward<ARGS>(r)...);
1769 struct ConnectionState
1771 DBusWrapper::ConnectionPtr connection;
1772 DBusWrapper::ObjectPtr object;
1773 DBusWrapper::ProxyPtr proxy, propertiesProxy;
1776 template<typename RETTYPE, typename... ARGS>
1777 RETTYPE call(CallId callId, const ConnectionState& connectionState, bool property, const std::string& funcName, const ARGS&... args)
1779 const auto& proxy = property ? connectionState.propertiesProxy : connectionState.proxy;
1782 DBUS_DEBUG("call %d: not initialized", callId.id);
1783 return Error{"not initialized"};
1786 DBUS_DEBUG("call %d: calling '%s'", callId.id, funcName.c_str());
1787 auto msg = DBUS_W->eldbus_proxy_method_call_new_impl(proxy, funcName);
1790 DBUS_DEBUG("call %d: failed", callId.id);
1791 return Error{"failed to create message"};
1794 detail::packValues(callId, msg, args...);
1795 auto reply = DBUS_W->eldbus_proxy_send_and_block_impl(proxy, msg);
1796 DBUS_DEBUG("call %d: calling '%s' done", callId.id, funcName.c_str());
1799 DBUS_DEBUG("call %d: failed", callId.id);
1800 return Error{"eldbus returned null as reply"};
1802 std::string errname, errmsg;
1803 if(DBUS_W->eldbus_message_error_get_impl(reply, errname, errmsg))
1805 DBUS_DEBUG("call %d: %s: %s", callId.id, errname.c_str(), errmsg.c_str());
1806 return Error{errname + ": " + errmsg};
1808 DBUS_DEBUG("call %d: got reply with signature '%s'", callId.id, DBUS_W->eldbus_message_signature_get_impl(reply).c_str());
1809 return detail::unpackValues<RETTYPE>(callId, reply);
1812 template<typename RETTYPE, typename... ARGS>
1813 void asyncCall(CallId callId, const ConnectionState& connectionState, bool property, const std::string& funcName, std::function<void(RETTYPE)> callback, const ARGS&... args)
1815 const auto& proxy = property ? connectionState.propertiesProxy : connectionState.proxy;
1818 DBUS_DEBUG("call %d: not initialized", callId.id);
1819 callback(Error{"not initialized"});
1823 auto msg = DBUS_W->eldbus_proxy_method_call_new_impl(proxy, funcName);
1826 DBUS_DEBUG("call %d: failed", callId.id);
1827 callback(Error{"failed to create message"});
1831 detail::packValues(callId, msg, args...);
1832 auto pending = DBUS_W->eldbus_proxy_send_impl(proxy, msg, [callback, callId, proxy](const DBusWrapper::MessagePtr& reply) {
1833 DBUS_DEBUG("call %d: calling done", callId.id);
1836 DBUS_DEBUG("call %d: failed", callId.id);
1837 callback(Error{"eldbus returned null as reply"});
1841 std::string errname, errmsg;
1842 if(DBUS_W->eldbus_message_error_get_impl(reply, errname, errmsg))
1844 DBUS_DEBUG("call %d: %s: %s", callId.id, errname.c_str(), errmsg.c_str());
1845 callback(Error{errname + ": " + errmsg});
1849 DBUS_DEBUG("call %d: got reply with signature '%s'", callId.id, DBUS_W->eldbus_message_signature_get_impl(reply).c_str());
1850 callback(detail::unpackValues<RETTYPE>(callId, reply));
1856 DBUS_DEBUG("call %d: call sent", callId.id);
1860 DBUS_DEBUG("call %d: failed to send call", callId.id);
1861 callback(Error{"failed to send call"});
1865 inline void displayDebugCallInfo(CallId callId, const std::string& funcName, const std::string& info, const std::string& interfaceName)
1867 DBUS_DEBUG("call %d: %s iname = %s fname = %s", callId.id, info.c_str(), interfaceName.c_str(), funcName.c_str());
1870 inline void displayDebugCallInfoSignal(CallId callId, const std::string& funcName, const std::string& info, const std::string& interfaceName)
1872 DBUS_DEBUG("call %d: %s signal iname = %s fname = %s", callId.id, info.c_str(), interfaceName.c_str(), funcName.c_str());
1875 inline void displayDebugCallInfoProperty(CallId callId, const std::string& funcName, std::string info, const std::string& interfaceName, const std::string& propertyName)
1877 DBUS_DEBUG("call %d: %s iname = %s pname = %s", callId.id, info.c_str(), interfaceName.c_str(), propertyName.c_str());
1880 using StringStorage = DBusWrapper::StringStorage;
1882 template<typename A, typename... ARGS>
1883 struct EldbusArgGenerator_Helper
1885 static void add(std::vector<std::pair<std::string, std::string>>& r)
1888 auto sig = std::string{signature<A>::sig()};
1889 assert(!sig.empty());
1890 auto name = "p" + std::to_string(s + 1);
1891 r.push_back({std::move(sig), std::move(name)});
1892 EldbusArgGenerator_Helper<ARGS...>::add(r);
1897 struct EldbusArgGenerator_Helper<void>
1899 static void add(std::vector<std::pair<std::string, std::string>>&)
1905 struct EldbusArgGenerator_Helper<ValueOrError<void>, void>
1907 static void add(std::vector<std::pair<std::string, std::string>>&)
1912 struct EldbusArgGenerator_Helper<ValueOrError<>, void>
1914 static void add(std::vector<std::pair<std::string, std::string>>&)
1919 template<typename... ARGS>
1920 struct EldbusArgGenerator_Helper<std::tuple<ARGS...>>
1922 static void add(std::vector<std::pair<std::string, std::string>>& r)
1924 EldbusArgGenerator_Helper<ARGS..., void>::add(r);
1928 template<typename RetType>
1929 struct dbus_interface_return_type_traits
1931 using type = ValueOrError<RetType>;
1934 template<typename... ARGS>
1935 struct dbus_interface_return_type_traits<ValueOrError<ARGS...>>
1937 using type = ValueOrError<ARGS...>;
1940 template<typename T>
1941 struct dbus_interface_traits;
1942 template<typename RetType, typename... ARGS>
1943 struct dbus_interface_traits<RetType(ARGS...)>
1945 using Ret = typename dbus_interface_return_type_traits<RetType>::type;
1946 using SyncCB = std::function<Ret(ARGS...)>;
1947 using AsyncCB = std::function<void(std::function<void(Ret)>, ARGS...)>;
1948 using VEArgs = ValueOrError<ARGS...>;
1951 template<typename T>
1952 struct EldbusArgGenerator_Args;
1953 template<typename RetType, typename... ARGS>
1954 struct EldbusArgGenerator_Args<RetType(ARGS...)>
1956 static std::string name()
1958 return std::string{signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::name()};
1960 static std::vector<std::pair<std::string, std::string>> get()
1962 std::vector<std::pair<std::string, std::string>> tmp;
1963 EldbusArgGenerator_Helper<ARGS..., void>::add(tmp);
1968 template<typename T>
1969 struct EldbusArgGenerator_ReturnType;
1970 template<typename RetType, typename... ARGS>
1971 struct EldbusArgGenerator_ReturnType<RetType(ARGS...)>
1973 static std::string name()
1975 return std::string{signature<RetType>::name()};
1977 static std::vector<std::pair<std::string, std::string>> get()
1979 std::vector<std::pair<std::string, std::string>> tmp;
1980 EldbusArgGenerator_Helper<RetType, void>::add(tmp);
1985 template<typename T>
1986 struct EldbusArgGenerator_ReturnType;
1987 template<typename... ARGS>
1988 struct EldbusArgGenerator_ReturnType<void(ARGS...)>
1990 static std::string name()
1994 static std::vector<std::pair<std::string, std::string>> get()
2000 } // namespace detail
2002 using ConnectionType = DBusWrapper::ConnectionType;
2005 * @brief Class representing client's end of DBUS connection
2007 * Allows calling (synchronous and asynchronous) methods on selected interface
2008 * Allows (synchronous and asynchronous) setting / getting properties.
2009 * Allows registering signals.
2013 struct ConnectionInfo
2015 std::string interfaceName, busName, pathName;
2020 * @brief Default constructor, creates non-connected object.
2022 DBusClient() = default;
2025 * @brief Connects to dbus choosen by tp, using given arguments
2027 * @param bus_name name of the bus to connect to
2028 * @param path_name object's path
2029 * @param interface_name interface name
2031 DBusClient(std::string busName_, std::string pathName_, std::string interfaceName_, ConnectionType tp);
2034 * @brief Connects to dbus using connection conn
2036 * @param bus_name name of the bus to connect to
2037 * @param path_name object's path
2038 * @param interface_name interface name
2039 * @param conn connection object from getDBusConnectionByType call
2041 DBusClient(std::string busName_, std::string pathName_, std::string interfaceName_, const DBusWrapper::ConnectionPtr& conn = {});
2044 * @brief Destructor object.
2046 * All signals added will be disconnected.
2047 * All asynchronous calls will be cancelled, their callback's will be called
2048 * with failure message.
2050 ~DBusClient() = default;
2051 DBusClient(const DBusClient&) = delete;
2052 DBusClient(DBusClient&&) = default;
2054 DBusClient& operator=(DBusClient&&) = default;
2055 DBusClient& operator=(const DBusClient&) = delete;
2058 * @brief bool operator
2060 * Returns true, if object is connected to DBUS
2062 explicit operator bool() const
2064 return bool(connectionState->proxy);
2068 * @brief Helper class for calling a method
2070 * Template type T defines both arguments sent to the method
2071 * and expected values. Receiving different values will be reported as
2072 * error. For example:
2073 * \code{.cpp} Method<int(float, float)> \endcode
2074 * defines method, which takes two arguments (two floats) and return
2075 * single value of type int.
2077 template<typename T>
2080 using RetType = typename detail::dbus_interface_traits<T>::Ret;
2081 const detail::ConnectionState& connectionState;
2082 std::string funcName;
2084 std::shared_ptr<ConnectionInfo> connectionInfo;
2087 * @brief Executes synchronous call on DBUS's method
2089 * The function returns ValueOrError<...> object, which
2090 * contains either received values or error message.
2092 * @param args arguments to pass to the method
2094 template<typename... ARGS>
2095 RetType call(const ARGS&... args)
2097 detail::CallId callId;
2098 detail::displayDebugCallInfo(callId, funcName, info, connectionInfo->interfaceName);
2099 return detail::call<RetType>(callId, connectionState, false, funcName, args...);
2103 * @brief Executes asynchronous call on DBUS's method
2105 * The function calls callback with either received values or error message.
2107 * @param callback callback functor, which will be called with return value(s) or error message
2108 * @param args arguments to pass to the method
2110 template<typename... ARGS>
2111 void asyncCall(std::function<void(RetType)> callback, const ARGS&... args)
2113 detail::CallId callId;
2114 detail::displayDebugCallInfo(callId, funcName, info, connectionInfo->interfaceName);
2115 detail::asyncCall<RetType>(callId, connectionState, false, funcName, std::move(callback), args...);
2120 * @brief Helper class for calling a property
2122 * Template type T defines type of the value hidden under property.
2123 * Note, that library automatically wraps both sent and received value into
2124 * DBUS's wrapper type.
2126 template<typename T>
2129 using RetType = typename detail::dbus_interface_return_type_traits<T>::type;
2130 using VariantRetType = typename detail::dbus_interface_return_type_traits<EldbusVariant<T>>::type;
2131 const detail::ConnectionState& connectionState;
2132 std::string propName;
2134 std::shared_ptr<ConnectionInfo> connectionInfo;
2137 * @brief executes synchronous get on property
2139 * The function returns ValueOrError<...> object, which
2140 * contains either received values or error message.
2144 detail::CallId callId;
2145 detail::displayDebugCallInfoProperty(callId, "Get", info, connectionInfo->interfaceName, propName);
2146 auto z = detail::call<VariantRetType>(callId, connectionState, true, "Get", connectionInfo->interfaceName, propName);
2148 return z.getError();
2149 return {std::get<0>(z.getValues()).value};
2153 * @brief executes asynchronous get on property
2155 * The function calls callback with either received values or error message.
2157 * @param callback callback functor, which will be called with return value(s) or error message
2159 void asyncGet(std::function<void(RetType)> callback)
2161 detail::CallId callId;
2162 detail::displayDebugCallInfoProperty(callId, "Get", info, connectionInfo->interfaceName, propName);
2163 auto cc = [callback](VariantRetType reply) {
2165 callback(std::move(std::get<0>(reply.getValues()).value));
2167 callback(reply.getError());
2169 detail::asyncCall<VariantRetType>(callId, connectionState, true, "Get", std::move(cc), connectionInfo->interfaceName, propName);
2173 * @brief executes synchronous set on property
2175 * The function returns ValueOrError<void> object, with
2176 * possible error message.
2178 ValueOrError<void> set(const T& r)
2180 detail::CallId callId;
2181 detail::displayDebugCallInfoProperty(callId, "Set", info, connectionInfo->interfaceName, propName);
2182 EldbusVariant<T> variantValue{std::move(r)};
2183 return detail::call<ValueOrError<void>>(callId, connectionState, true, "Set", connectionInfo->interfaceName, propName, variantValue);
2187 * @brief executes asynchronous get on property
2189 * The function calls callback with either received values or error message.
2191 * @param callback callback functor, which will be called with return value(s) or error message
2193 void asyncSet(std::function<void(ValueOrError<void>)> callback, const T& r)
2195 detail::CallId callId;
2196 detail::displayDebugCallInfoProperty(callId, "Set", info, connectionInfo->interfaceName, propName);
2197 EldbusVariant<T> variantValue{std::move(r)};
2198 detail::asyncCall<ValueOrError<void>>(callId, connectionState, true, "Set", std::move(callback), connectionInfo->interfaceName, propName, variantValue);
2203 * @brief Constructs Property<...> object for calling properties
2205 * The function constructs and returns proxy object for calling property.
2207 * @param propName property name to set and / or query
2209 template<typename PropertyType>
2210 Property<PropertyType> property(std::string propName)
2212 return Property<PropertyType>{*connectionState, std::move(propName), info, connectionInfo};
2216 * @brief Constructs Method<...> object for calling methods
2218 * The function constructs and returns proxy object for calling method.
2220 * @param funcName function name to call
2222 template<typename MethodType>
2223 Method<MethodType> method(std::string funcName)
2225 return Method<MethodType>{*connectionState, std::move(funcName), info, connectionInfo};
2229 * @brief Registers notification callback, when property has changed
2231 * The callback will be called with new value, when property's value has changed.
2232 * Note, that template type V must match expected type, otherwise undefined behavior will occur,
2233 * there's no check for this.
2235 template<typename V>
2236 void addPropertyChangedEvent(std::string propertyName, std::function<void(V)> callback)
2238 detail::CallId callId;
2239 detail::displayDebugCallInfoSignal(callId, propertyName, info, connectionInfo->interfaceName);
2240 DBUS_DEBUG("call %d: adding property", callId.id);
2241 auto& cI = this->connectionInfo;
2242 DBUS_W->add_property_changed_event_listener_impl(connectionState->proxy, cI->interfaceName, propertyName, [callback](const _Eina_Value* newValue) {
2244 if(!getFromEinaValue(newValue, &val))
2246 DBUS_DEBUG("unable to get property's value");
2254 * @brief Registers callback on the DBUS' signal
2256 * The function registers callback signalName. When signal comes, callback will be called.
2257 * Callback object will exists as long as signal is registered. You can unregister signal
2258 * by destroying DBusClient object.
2260 * @param signalName name of the signal to register
2261 * @param callback callback to call
2263 template<typename SignalType>
2264 void addSignal(std::string signalName, std::function<SignalType> callback)
2266 detail::CallId callId;
2267 detail::displayDebugCallInfoSignal(callId, signalName, info, connectionInfo->interfaceName);
2268 DBUS_W->eldbus_proxy_signal_handler_add_impl(connectionState->proxy, signalName, [callId, callback, signalName](const DBusWrapper::MessagePtr& msg) -> void {
2269 std::string errname, aux;
2270 if(DBUS_W->eldbus_message_error_get_impl(msg, errname, aux))
2272 DBUS_DEBUG("call %d: Eldbus error: %s %s", callId.id, errname.c_str(), aux.c_str());
2275 DBUS_DEBUG("call %d: received signal with signature '%s'", callId.id, DBUS_W->eldbus_message_signature_get_impl(msg).c_str());
2276 using ParamsType = typename detail::dbus_interface_traits<SignalType>::VEArgs;
2277 auto params = detail::unpackValues<ParamsType>(callId, msg);
2280 DBUS_DEBUG("call %d: failed: %s", callId.id, params.getError().message.c_str());
2285 std::apply(callback, params.getValues());
2287 catch(const Dali::DaliException& e)
2289 DBUS_DEBUG("unhandled exception");
2296 std::unique_ptr<detail::ConnectionState> connectionState{new detail::ConnectionState};
2298 std::shared_ptr<ConnectionInfo> connectionInfo;
2300 static bool getFromEinaValue(const _Eina_Value* v, void* dst);
2304 * @brief Helper class describing DBUS's server interface
2307 class DBusInterfaceDescription
2309 friend class DBusServer;
2312 using MethodInfo = DBusWrapper::MethodInfo;
2313 using SignalInfo = DBusWrapper::SignalInfo;
2314 using PropertyInfo = DBusWrapper::PropertyInfo;
2315 using SignalId = DBusWrapper::SignalId;
2318 * @brief Creates empty interface description with given name
2320 * @param interfaceName name of the interface
2322 DBusInterfaceDescription(std::string interfaceName);
2325 * @brief adds new, synchronous method to the interface
2327 * When method memberName is called on DBUS, callback functor will be called
2328 * with values received from DBUS. callback won't be called, if method was
2329 * called with invalid signature. Value returned from functor (or error message)
2330 * will be marshalled back to the caller.
2332 * Template type T defines both arguments sent to the method
2333 * and expected values. Receiving different values will be reported as
2334 * error. For example:
2335 * \code{.cpp} Method<int(float, float)> \endcode
2336 * defines method, which takes two arguments (two floats) and return
2337 * single value of type int.
2339 * @param memberName name of the method
2340 * @param callback functor, which will be called
2342 template<typename T>
2343 void addMethod(const std::string& memberName, typename detail::dbus_interface_traits<T>::SyncCB callback)
2345 detail::CallId callId;
2347 methods.push_back(std::move(mi));
2348 auto& z = methods.back();
2349 z.in = detail::EldbusArgGenerator_Args<T>::get();
2350 z.out = detail::EldbusArgGenerator_ReturnType<T>::get();
2351 z.memberName = memberName;
2352 DBUS_DEBUG("call %d: method %s, in %s, out %s", callId.id, memberName.c_str(), detail::EldbusArgGenerator_Args<T>::name().c_str(), detail::EldbusArgGenerator_ReturnType<T>::name().c_str());
2353 z.callback = construct<T>(callId, callback);
2358 * @brief adds new, synchronous property to the interface
2360 * When property memberName is called on DBUS, respective callback functor will be called
2361 * with values received from DBUS. callback won't be called, if method was
2362 * called with invalid signature. Value returned from functor (or error message)
2363 * will be marshalled back to the caller.
2365 * Template type T defines type of the value hidden under property.
2366 * Note, that library automatically wraps both sent and received value into
2367 * DBUS's wrapper type.
2369 * @param memberName name of the method
2370 * @param getter functor, which will be called when property is being read
2371 * @param setter functor, which will be called when property is being set
2373 template<typename T>
2374 void addProperty(const std::string& memberName, std::function<ValueOrError<T>()> getter, std::function<ValueOrError<void>(T)> setter)
2376 properties.push_back({});
2377 auto& z = properties.back();
2378 z.memberName = memberName;
2379 z.typeSignature = detail::signature<T>::sig();
2382 detail::CallId getterId;
2383 z.getterId = getterId;
2384 DBUS_DEBUG("call %d: property %s (get) type %s", getterId.id, memberName.c_str(), detail::signature<T>::name().data());
2385 z.getCallback = [=](const DBusWrapper::MessagePtr& src, const DBusWrapper::MessageIterPtr& dst) -> std::string {
2388 auto v = std::apply(getter, std::tuple<>{});
2391 detail::signature<T>::set(dst, std::get<0>(v.getValues()));
2392 DBUS_DEBUG("call %d: success", getterId.id);
2395 DBUS_DEBUG("call %d: failed: %s", getterId.id, v.getError().message.c_str());
2396 return v.getError().message;
2398 catch(std::exception& e)
2400 return std::string("unhandled exception (") + e.what() + ")";
2402 catch(const Dali::DaliException& e)
2404 return std::string("unhandled exception (") + e.condition + ")";
2410 detail::CallId setterId;
2411 z.setterId = setterId;
2412 DBUS_DEBUG("call %d: property %s (set) type %s", setterId.id, memberName.c_str(), detail::signature<T>::name().data());
2413 z.setCallback = [=](const DBusWrapper::MessagePtr& src, const DBusWrapper::MessageIterPtr& src_iter) -> std::string {
2414 std::tuple<T> value;
2415 auto src_signature = DBUS_W->eldbus_message_iter_signature_get_impl(src_iter);
2416 if(detail::signature<T>::get(src_iter, std::get<0>(value)))
2420 auto v = std::apply(setter, std::move(value));
2423 DBUS_DEBUG("call %d: success", setterId.id);
2426 DBUS_DEBUG("call %d: failed: %s", setterId.id, v.getError().message.c_str());
2427 return v.getError().message;
2429 catch(std::exception& e)
2431 return std::string("unhandled exception (") + e.what() + ")";
2433 catch(const Dali::DaliException& e)
2435 return std::string("unhandled exception (") + e.condition + ")";
2438 DBUS_DEBUG("call %d: failed to unpack values, got signature '%s', expected '%s'", setterId.id, src_signature.c_str(), detail::signature<T>::sig().data());
2439 return "call " + std::to_string(setterId.id) + ": failed to unpack values, got signature '" +
2440 src_signature + "', expected '" + std::string{detail::signature<T>::sig()} + "'";
2446 * @brief adds new signal to the interface
2448 * Template types ARGS defines values, which will be emited with the signal
2450 * @param memberName name of the method
2452 template<typename... ARGS>
2453 SignalId addSignal(const std::string& memberName)
2455 detail::CallId callId;
2456 signals.push_back({});
2457 auto& z = signals.back();
2458 z.memberName = memberName;
2459 z.args = detail::EldbusArgGenerator_Args<void(ARGS...)>::get(DBUS_W->Strings);
2461 DBUS_DEBUG("call %d: signal %s", callId.id, memberName.c_str());
2462 return SignalId{callId};
2466 std::vector<MethodInfo> methods;
2467 std::vector<PropertyInfo> properties;
2468 std::vector<SignalInfo> signals;
2469 std::string interfaceName;
2471 template<typename T>
2472 std::function<DBusWrapper::MessagePtr(const DBusWrapper::MessagePtr& msg)> construct(detail::CallId callId,
2473 typename detail::dbus_interface_traits<T>::SyncCB callback)
2475 using VEArgs = typename detail::dbus_interface_traits<T>::VEArgs;
2476 return [=](const DBusWrapper::MessagePtr& msg) -> DBusWrapper::MessagePtr {
2477 DBUS_DEBUG("call %d: entering", callId.id);
2478 DBusWrapper::MessagePtr ret = {};
2479 auto args = detail::unpackValues<VEArgs>(callId, msg);
2484 auto v = std::apply(callback, std::move(args.getValues()));
2487 DBUS_DEBUG("call %d: success", callId.id);
2488 ret = DBUS_W->eldbus_message_method_return_new_impl(msg);
2489 detail::packValues(callId, ret, v);
2493 DBUS_DEBUG("call %d: failed: %s", callId.id, v.getError().message.c_str());
2494 ret = DBUS_W->eldbus_message_error_new_impl(msg, "org.freedesktop.DBus.Error.Failed", v.getError().message);
2497 catch(std::exception& e)
2499 auto txt = std::string("unhandled exception (") + e.what() + ")";
2500 DBUS_DEBUG("call %d: failed: %s", callId.id, txt.c_str());
2501 ret = DBUS_W->eldbus_message_error_new_impl(msg, "org.freedesktop.DBus.Error.Failed", txt);
2503 catch(const Dali::DaliException& e)
2505 auto txt = std::string("unhandled exception (") + e.condition + ")";
2506 DBUS_DEBUG("call %d: failed: %s", callId.id, txt.c_str());
2507 ret = DBUS_W->eldbus_message_error_new_impl(msg, "org.freedesktop.DBus.Error.Failed", txt);
2512 std::ostringstream err;
2513 err << "expected signature '" << detail::signature<VEArgs>::sig() << "', got '" << DBUS_W->eldbus_message_signature_get_impl(msg) << "'";
2514 auto str = err.str();
2515 DBUS_DEBUG("call %d: failed: %s", callId.id, str.c_str());
2516 ret = DBUS_W->eldbus_message_error_new_impl(msg, "org.freedesktop.DBus.Error.InvalidArgs", str);
2524 * @brief Class representing server's end of DBUS connection
2526 * Allows listening (synchronously and asynchronously) on methods on selected interface
2527 * Allows listening (synchronously and asynchronously) on setting / getting properties.
2528 * Allows emiting signals.
2534 * @brief Constructs non-connected dbus server.
2536 DBusServer() = default;
2539 * @brief Constructs dbus server on either system or user dbus connection.
2541 DBusServer(ConnectionType tp);
2544 * @brief Constructs dbus server on connection from getDBusConnectionByType
2546 DBusServer(const DBusWrapper::ConnectionPtr& conn);
2551 * Destructor will properly destroy everything. Destructor will cancel
2554 ~DBusServer() = default;
2556 DBusServer(const DBusServer&) = delete;
2557 DBusServer(DBusServer&&) = default;
2559 DBusServer& operator=(DBusServer&&) = default;
2560 DBusServer& operator=(const DBusServer&) = delete;
2563 * @brief Registers interface on given path name
2565 * @param pathName path object to register interface on.
2569 void addInterface(const std::string& pathName, DBusInterfaceDescription& dscr, bool fallback = false);
2572 * @brief Gets bus name of the current connection (must be connected)
2574 std::string getBusName() const;
2577 * @brief Returns connection object for this dbus server object
2579 * @return connection object
2581 DBusWrapper::ConnectionPtr getConnection();
2584 * @brief Emits signal
2586 * Emits signal based only on data passed to the function
2588 * @param signal identifier of the signal
2589 * @param args values to emit
2591 template<typename... ARGS>
2592 void emit2(const std::string& path, const std::string& interfaceName, const std::string& signalName, const ARGS&... args)
2594 auto msg = DBUS_W->eldbus_message_signal_new_impl(path, interfaceName, signalName);
2596 detail::packValues(id, msg, args...);
2597 DBUS_W->eldbus_connection_send_impl(connection, msg);
2601 * @brief Returns current object path, when handling call to property / method
2603 * User can call this function from inside callback used to handle property / method calls.
2604 * It will retrieve object's path used in the call. Note, that in asynchronous handling
2605 * of those calls user need to retrieve and store the current object / current connection
2606 * as the value will change at the moment user's callback handling will exit. For example:
2608 * DBusInterfaceDescription interface{"name"};
2609 * auto handler_later = [](std::function<void(void)> done_cb) {
2610 * // process something later on
2611 * DBusServer::getCurrentObjectPath(); // this will return empty string
2613 * interface.addAsyncMethod<void()>("m", [=](std::function<void(void)> done_cb) {
2614 * DBusServer::getCurrentObjectPath(); // this will current object's path
2616 * // do some processing later on and call done_cb, when it's done
2617 * register_to_call_sometime_later_on(std::move(done_cb));
2621 static std::string getCurrentObjectPath()
2623 return currentObjectPath;
2627 * @brief Returns current connection object, when handling call to property / method
2629 * User can call this function from inside callback used to handle property / method calls.
2630 * It will retrieve object's path used in the call. Note, that in asynchronous handling
2631 * of those calls user need to retrieve and store the current object / current connection
2632 * as the value will change at the moment user's callback handling will exit. For example:
2634 * DBusInterfaceDescription interface{"name"};
2635 * auto handler_later = [](std::function<void(void)> done_cb) {
2636 * // process something later on
2637 * DBusServer::getCurrentObjectPath(); // this will return empty string
2639 * interface.addAsyncMethod<void()>("m", [=](std::function<void(void)> done_cb) {
2640 * DBusServer::getCurrentObjectPath(); // this will current object's path
2642 * // do some processing later on and call done_cb, when it's done
2643 * register_to_call_sometime_later_on(std::move(done_cb));
2647 static const DBusWrapper::ConnectionPtr& getCurrentConnection()
2649 return currentConnection;
2653 class CurrentObjectSetter
2656 CurrentObjectSetter(DBusWrapper::ConnectionPtr con, std::string path)
2658 currentObjectPath = std::move(path);
2659 currentConnection = std::move(con);
2661 ~CurrentObjectSetter()
2663 currentObjectPath = "";
2664 currentConnection = {};
2666 CurrentObjectSetter(const CurrentObjectSetter&) = delete;
2667 CurrentObjectSetter(CurrentObjectSetter&&) = delete;
2668 void operator=(const CurrentObjectSetter&) = delete;
2669 void operator=(CurrentObjectSetter&&) = delete;
2673 DBusWrapper::ConnectionPtr connection;
2674 struct DestructorObject
2676 std::vector<std::function<void()>> destructors;
2679 for(auto& a : destructors) a();
2683 std::unique_ptr<DestructorObject> destructorObject{new DestructorObject()};
2684 static thread_local std::string currentObjectPath;
2685 static thread_local DBusWrapper::ConnectionPtr currentConnection;
2688 DBusWrapper::ConnectionPtr getDBusConnectionByType(ConnectionType tp);
2689 DBusWrapper::ConnectionPtr getDBusConnectionByName(const std::string& name);
2690 std::string getConnectionName(const DBusWrapper::ConnectionPtr&);
2695 template<size_t INDEX, typename... ARGS>
2696 inline auto get(DBus::ValueOrError<ARGS...>& v) -> decltype(std::get<INDEX>(v.getValues()))&
2698 return std::get<INDEX>(v.getValues());
2701 template<size_t INDEX, typename... ARGS>
2702 inline auto get(const DBus::ValueOrError<ARGS...>& v) -> decltype(std::get<INDEX>(v.getValues()))
2704 return std::get<INDEX>(v.getValues());
2708 #endif // DALI_INTERNAL_ACCESSIBILITY_BRIDGE_DBUS_H