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)>);
475 * @brief Enumeration indicatng DBus error type
479 DEFAULT, ///< default
480 INVALID_REPLY ///< reply message has error
486 ErrorType errorType{ErrorType::DEFAULT};
489 Error(std::string msg, ErrorType errorType = ErrorType::DEFAULT)
490 : message(std::move(msg)),
493 assert(!message.empty());
502 * @brief Value representing data, that came from DBUS or error message
504 * Object of this class either helds series of values (of types ARGS...)
505 * or error message. This object will be true in boolean context, if has data
506 * and false, if an error occured.
507 * It's valid to create ValueOrError object with empty argument list or void:
510 * ValueOrError<void> v2;
512 * Both mean the same - ValueOrError containing no real data and being a marker,
513 * wherever operation successed or failed and containing possible error message.
515 template<typename... ARGS>
520 * @brief Empty constructor. Valid only, if all ARGS types are default constructible.
522 ValueOrError() = default;
525 * @brief Value constructor.
527 * This will be initialized as success with passed in values.
529 ValueOrError(ARGS... t)
530 : value(std::move(t)...)
535 * @brief Alternative Value constructor.
537 * This will be initialized as success with passed in values.
539 ValueOrError(std::tuple<ARGS...> t)
540 : value(std::move(t))
545 * @brief Error constructor. This will be initialized as failure with given error message.
547 ValueOrError(Error e)
548 : error(std::move(e))
550 assert(!error.message.empty());
554 * @brief bool operator.
556 * Returns true, if operation was successful (getValues member is callable), or false
557 * when operation failed (getError is callable).
559 explicit operator bool() const
561 return error.message.empty();
565 * @brief Returns error message object.
567 * Returns object containing error message associated with the failed operation.
568 * Only callable, if operation actually failed, otherwise will assert.
570 const Error& getError() const
576 * @brief Returns modifiable tuple of held data.
578 * Returns reference to the internal tuple containing held data.
579 * User can modify (or move) data safely.
580 * Only callable, if operation actually successed, otherwise will assert.
582 std::tuple<ARGS...>& getValues()
589 * @brief Returns const tuple of held data.
591 * Returns const reference to the internal tuple containing held data.
592 * Only callable, if operation actually successed, otherwise will assert.
594 const std::tuple<ARGS...>& getValues() const
601 std::tuple<ARGS...> value;
609 ValueOrError() = default;
610 ValueOrError(std::tuple<> t)
613 ValueOrError(Error e)
614 : error(std::move(e))
616 assert(!error.message.empty());
619 explicit operator bool() const
621 return error.message.empty();
623 const Error& getError() const
627 std::tuple<>& getValues()
630 static std::tuple<> t;
633 std::tuple<> getValues() const
644 class ValueOrError<void>
647 ValueOrError() = default;
648 ValueOrError(Success)
651 ValueOrError(Error e)
652 : error(std::move(e))
654 assert(!error.message.empty());
657 explicit operator bool() const
659 return error.message.empty();
661 const Error& getError() const
665 std::tuple<>& getValues()
668 static std::tuple<> t;
671 std::tuple<> getValues() const
681 using ObjectPath = ObjectPath;
684 * @brief Class used to marshall DBUS's variant type
686 * Minimalistic class, that allows user to specify DBUS variant type
687 * as argument or return value. You need to pass real type hidden under variant as
688 * template type \b A. At this point library doesn't allow to expected one of few classes
689 * as return data in variant. So for example user can't specify method call, which on return
690 * expects DBUS variant holding either string or int.
699 * @brief Namespace for private, internal functions and classes
703 inline namespace strings
705 template<std::size_t N>
706 using char_array = std::array<char, N>;
708 constexpr char* xcopy_n(const char* src, size_t n, char* dst)
710 for(std::size_t i = 0; i < n; ++i)
716 template<std::size_t N>
717 constexpr std::size_t xlen(const char (&)[N])
722 template<std::size_t N>
723 constexpr std::size_t xlen(const char_array<N>&)
728 template<typename... Args>
729 constexpr auto concat(const Args&... args)
731 char_array<(1U + ... + xlen(args))> arr{};
732 char* ptr = arr.data();
734 if constexpr(!arr.empty())
735 ((ptr = xcopy_n(std::data(args), xlen(args), ptr)), ...);
740 template<std::size_t... Digits>
743 static constexpr const char value[] = {('0' + static_cast<char>(Digits))..., '\0'};
745 static_assert((true && ... && (Digits < 10)));
748 template<std::size_t Remainder, std::size_t... Digits>
749 struct to_chars_helper : to_chars_helper<Remainder / 10, Remainder % 10, Digits...>
753 template<std::size_t... Digits>
754 struct to_chars_helper<0, Digits...> : to_chars<Digits...>
758 template<std::size_t N>
759 using to_string = to_chars_helper<N / 10, N % 10>;
760 } // namespace strings
762 template<typename T, typename = void>
764 template<typename... ARGS>
765 struct signature<std::tuple<ARGS...>>;
766 template<typename A, typename B>
767 struct signature<std::pair<A, B>>;
769 struct signature<std::vector<A>>;
770 template<typename A, size_t N>
771 struct signature<std::array<A, N>>;
772 template<typename A, typename B>
773 struct signature<std::unordered_map<A, B>>;
774 template<typename A, typename B>
775 struct signature<std::map<A, B>>;
778 struct signature_helper
781 * @brief Returns name of type marshalled, for informative purposes
783 static constexpr std::string_view name()
785 return {T::name_v.data()};
789 * @brief Returns DBUS' signature of type marshalled
791 static constexpr std::string_view sig()
793 return {T::sig_v.data()};
798 * @brief Signature class for marshalling uint8 type.
801 struct signature<uint8_t> : signature_helper<signature<uint8_t>>
803 static constexpr auto name_v = concat("uint8_t");
804 static constexpr auto sig_v = concat("y");
807 * @brief Marshals value v as marshalled type into message
809 static void set(const DBusWrapper::MessageIterPtr& iter, uint8_t v)
811 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
815 * @brief Marshals value from marshalled type into variable v
817 static bool get(const DBusWrapper::MessageIterPtr& iter, uint8_t& v)
819 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
824 * @brief Signature class for marshalling uint16 type.
827 struct signature<uint16_t> : signature_helper<signature<uint16_t>>
829 static constexpr auto name_v = concat("uint16_t");
830 static constexpr auto sig_v = concat("q");
833 * @brief Marshals value v as marshalled type into message
835 static void set(const DBusWrapper::MessageIterPtr& iter, uint16_t v)
837 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
841 * @brief Marshals value from marshalled type into variable v
843 static bool get(const DBusWrapper::MessageIterPtr& iter, uint16_t& v)
845 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
850 * @brief Signature class for marshalling uint32 type.
853 struct signature<uint32_t> : signature_helper<signature<uint32_t>>
855 static constexpr auto name_v = concat("uint32_t");
856 static constexpr auto sig_v = concat("u");
859 * @brief Marshals value v as marshalled type into message
861 static void set(const DBusWrapper::MessageIterPtr& iter, uint32_t v)
863 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
867 * @brief Marshals value from marshalled type into variable v
869 static bool get(const DBusWrapper::MessageIterPtr& iter, uint32_t& v)
871 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
876 * @brief Signature class for marshalling uint64 type.
879 struct signature<uint64_t> : signature_helper<signature<uint64_t>>
881 static constexpr auto name_v = concat("uint64_t");
882 static constexpr auto sig_v = concat("t");
885 * @brief Marshals value v as marshalled type into message
887 static void set(const DBusWrapper::MessageIterPtr& iter, uint64_t v)
889 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
893 * @brief Marshals value from marshalled type into variable v
895 static bool get(const DBusWrapper::MessageIterPtr& iter, uint64_t& v)
897 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
902 * @brief Signature class for marshalling int16 type.
905 struct signature<int16_t> : signature_helper<signature<int16_t>>
907 static constexpr auto name_v = concat("int16_t");
908 static constexpr auto sig_v = concat("n");
911 * @brief Marshals value v as marshalled type into message
913 static void set(const DBusWrapper::MessageIterPtr& iter, int16_t v)
915 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
919 * @brief Marshals value from marshalled type into variable v
921 static bool get(const DBusWrapper::MessageIterPtr& iter, int16_t& v)
923 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
928 * @brief Signature class for marshalling int32 type.
931 struct signature<int32_t> : signature_helper<signature<int32_t>>
933 static constexpr auto name_v = concat("int32_t");
934 static constexpr auto sig_v = concat("i");
937 * @brief Marshals value v as marshalled type into message
939 static void set(const DBusWrapper::MessageIterPtr& iter, int32_t v)
941 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
945 * @brief Marshals value from marshalled type into variable v
947 static bool get(const DBusWrapper::MessageIterPtr& iter, int32_t& v)
949 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
954 * @brief Signature class for marshalling int64 type.
957 struct signature<int64_t> : signature_helper<signature<int64_t>>
959 static constexpr auto name_v = concat("int64_t");
960 static constexpr auto sig_v = concat("x");
963 * @brief Marshals value v as marshalled type into message
965 static void set(const DBusWrapper::MessageIterPtr& iter, int64_t v)
967 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
971 * @brief Marshals value from marshalled type into variable v
973 static bool get(const DBusWrapper::MessageIterPtr& iter, int64_t& v)
975 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
980 * @brief Signature class for marshalling double type.
983 struct signature<double> : signature_helper<signature<double>>
985 static constexpr auto name_v = concat("double");
986 static constexpr auto sig_v = concat("d");
989 * @brief Marshals value v as marshalled type into message
991 static void set(const DBusWrapper::MessageIterPtr& iter, double v)
993 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
997 * @brief Marshals value from marshalled type into variable v
999 static bool get(const DBusWrapper::MessageIterPtr& iter, double& v)
1001 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
1005 * @brief Marshals value from marshalled type into variable v
1007 static bool get(const DBusWrapper::MessageIterPtr& iter, float& v2)
1010 auto r = DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
1011 v2 = static_cast<float>(v);
1017 * @brief Signature class for marshalling float type.
1020 struct signature<float> : signature_helper<signature<float>>
1022 static constexpr auto name_v = concat("float");
1023 static constexpr auto sig_v = concat("d");
1026 * @brief Marshals value v as marshalled type into message
1028 static void set(const DBusWrapper::MessageIterPtr& iter, float v)
1030 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
1034 * @brief Marshals value from marshalled type into variable v
1036 static bool get(const DBusWrapper::MessageIterPtr& iter, double& v)
1038 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
1042 * @brief Marshals value from marshalled type into variable v
1044 static bool get(const DBusWrapper::MessageIterPtr& iter, float& v2)
1047 auto r = DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
1048 v2 = static_cast<float>(v);
1054 * @brief Signature class for marshalling boolean type.
1057 struct signature<bool> : signature_helper<signature<bool>>
1059 static constexpr auto name_v = concat("bool");
1060 static constexpr auto sig_v = concat("b");
1063 * @brief Marshals value v as marshalled type into message
1065 static void set(const DBusWrapper::MessageIterPtr& iter, bool v)
1067 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
1071 * @brief Marshals value from marshalled type into variable v
1073 static bool get(const DBusWrapper::MessageIterPtr& iter, bool& v)
1075 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
1079 template<typename T>
1080 struct signature<T, typename std::enable_if_t<std::is_enum_v<T>, void>> : signature_helper<signature<T>>
1082 static constexpr auto name_v = concat("enum");
1083 static constexpr auto sig_v = signature<typename std::underlying_type<T>::type>::sig_v;
1086 * @brief Marshals value v as marshalled type into message
1088 static void set(const DBusWrapper::MessageIterPtr& iter, T v)
1090 signature<typename std::underlying_type<T>::type>::set(iter, static_cast<int64_t>(v));
1094 * @brief Marshals value from marshalled type into variable v
1096 static bool get(const DBusWrapper::MessageIterPtr& iter, T& v)
1098 typename std::underlying_type<T>::type q = 0;
1099 if(!signature<typename std::underlying_type<T>::type>::get(iter, q))
1102 v = static_cast<T>(q);
1108 * @brief Signature class for marshalling string type.
1110 * Both (const) char * and std::string types are accepted as value to send.
1111 * Only std::string is accepted as value to receive.
1114 struct signature<std::string> : signature_helper<signature<std::string>>
1116 static constexpr auto name_v = concat("string");
1117 static constexpr auto sig_v = concat("s");
1120 * @brief Marshals value v as marshalled type into message
1122 static void set(const DBusWrapper::MessageIterPtr& iter, const std::string& v)
1124 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
1128 * @brief Marshals value from marshalled type into variable v
1130 static bool get(const DBusWrapper::MessageIterPtr& iter, std::string& v)
1132 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
1137 struct signature<ObjectPath> : signature_helper<signature<ObjectPath>>
1139 static constexpr auto name_v = concat("path");
1140 static constexpr auto sig_v = concat("o");
1143 * @brief Marshals value v as marshalled type into message
1145 static void set(const DBusWrapper::MessageIterPtr& iter, const std::string& v)
1147 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, ObjectPath{v});
1151 * @brief Marshals value v as marshalled type into message
1153 static void set(const DBusWrapper::MessageIterPtr& iter, const ObjectPath& v)
1155 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
1159 * @brief Marshals value v as marshalled type into message
1161 static void set(const DBusWrapper::MessageIterPtr& iter, const char* v)
1163 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, ObjectPath{v});
1167 * @brief Marshals value from marshalled type into variable v
1169 static bool get(const DBusWrapper::MessageIterPtr& iter, ObjectPath& v)
1171 return DBUS_W->eldbus_message_iter_get_and_next_impl(iter, v);
1175 * @brief Marshals value from marshalled type into variable v
1177 static bool get(const DBusWrapper::MessageIterPtr& iter, std::string& v)
1180 if(!DBUS_W->eldbus_message_iter_get_and_next_impl(iter, q)) return false;
1181 v = std::move(q.value);
1187 * @brief Signature class for marshalling (const) char * type.
1189 * Both (const) char * and std::string types are accepted as value to send.
1190 * You can't use (const) char * variable type to receive value.
1193 struct signature<char*> : signature_helper<signature<char*>>
1195 static constexpr auto name_v = concat("string");
1196 static constexpr auto sig_v = concat("s");
1199 * @brief Marshals value v as marshalled type into message
1201 static void set(const DBusWrapper::MessageIterPtr& iter, const std::string& v)
1203 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, v);
1207 * @brief Marshals value v as marshalled type into message
1209 static void set(const DBusWrapper::MessageIterPtr& iter, const char* v)
1211 DBUS_W->eldbus_message_iter_arguments_append_impl(iter, std::string{v});
1215 template<size_t INDEX, typename A, typename... ARGS>
1216 struct signature_tuple_element_type_helper
1218 using type = typename signature_tuple_element_type_helper<INDEX - 1, ARGS...>::type;
1221 template<typename A, typename... ARGS>
1222 struct signature_tuple_element_type_helper<0, A, ARGS...>
1228 * @brief Helper class to marshall tuples
1230 * This class marshals all elements of the tuple value starting at the index INDEX
1231 * and incrementing. This class recursively calls itself with increasing INDEX value
1232 * until INDEX is equal to SIZE, where recursive calling ends.
1234 template<size_t INDEX, size_t SIZE, typename... ARGS>
1235 struct signature_tuple_helper : signature_helper<signature_tuple_helper<INDEX, SIZE, ARGS...>>
1237 using current_type = typename signature_tuple_element_type_helper<INDEX, ARGS...>::type;
1239 static constexpr auto name_v = concat(signature<current_type>::name_v, ", ", signature_tuple_helper<INDEX + 1, SIZE, ARGS...>::name_v);
1240 static constexpr auto sig_v = concat(signature<current_type>::sig_v, signature_tuple_helper<INDEX + 1, SIZE, ARGS...>::sig_v);
1243 * @brief Marshals value v as marshalled type into message
1245 static void set(const DBusWrapper::MessageIterPtr& iter, const std::tuple<ARGS...>& args)
1247 signature<current_type>::set(iter, std::get<INDEX>(args));
1248 signature_tuple_helper<INDEX + 1, SIZE, ARGS...>::set(iter, args);
1252 * @brief Marshals value from marshalled type into variable v
1254 static bool get(const DBusWrapper::MessageIterPtr& iter, std::tuple<ARGS...>& args)
1256 return signature<current_type>::get(iter, std::get<INDEX>(args)) &&
1257 signature_tuple_helper<INDEX + 1, SIZE, ARGS...>::get(iter, args);
1262 * @brief Helper class to marshall tuples
1264 * This class marks end of the tuple marshalling. Members of this class are called
1265 * when INDEX value is equal to SIZE.
1267 template<size_t SIZE, typename... ARGS>
1268 struct signature_tuple_helper<SIZE, SIZE, ARGS...> : signature_helper<signature_tuple_helper<SIZE, SIZE, ARGS...>>
1270 static constexpr auto name_v = concat("");
1271 static constexpr auto sig_v = concat("");
1274 * @brief Marshals value v as marshalled type into message
1276 static void set(const DBusWrapper::MessageIterPtr& iter, const std::tuple<ARGS...>& args)
1281 * @brief Marshals value from marshalled type into variable v
1283 static bool get(const DBusWrapper::MessageIterPtr& iter, std::tuple<ARGS...>& args)
1290 * @brief Signature class for marshalling tuple of values
1292 * This class marshalls tuple of values. This represents
1293 * DBUS struct typle, encoded with character 'r'
1295 template<typename... ARGS>
1296 struct signature<std::tuple<ARGS...>> : signature_helper<signature<std::tuple<ARGS...>>>
1298 static constexpr auto name_v = concat("tuple<", signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::name_v, ">");
1299 static constexpr auto sig_v = concat("(", signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::sig_v, ")");
1302 * @brief Marshals value v as marshalled type into message
1304 static void set(const DBusWrapper::MessageIterPtr& iter, const std::tuple<ARGS...>& args)
1306 auto entry = DBUS_W->eldbus_message_iter_container_new_impl(iter, 'r', "");
1307 signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::set(entry, args);
1311 * @brief Marshals value from marshalled type into variable v
1313 static bool get(const DBusWrapper::MessageIterPtr& iter, std::tuple<ARGS...>& args)
1315 auto entry = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, 'r');
1316 if(!entry) return false;
1317 return signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::get(entry, args);
1322 * @brief Signature class for marshalling ValueOrError template type
1324 * ValueOrError template type is used to marshall list of values passed to
1325 * DBUS (or received from) at the "top" level. For example ss(s) is represented as
1326 * \code{.cpp} ValueOrError<std::string, std::string, std::tuple<std::string>> \endcode
1327 * While (ss(s)) is represented as
1328 * \code{.cpp} std::tuple<std::string, std::string, std::tuple<std::string>> \endcode
1330 * \code{.cpp} ValueOrError<std::tuple<std::string, std::string, std::tuple<std::string>>> \endcode
1332 template<typename... ARGS>
1333 struct signature<ValueOrError<ARGS...>> : signature_helper<signature<ValueOrError<ARGS...>>>
1335 static constexpr auto name_v = concat("ValueOrError<", signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::name_v, ">");
1336 static constexpr auto sig_v = signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::sig_v;
1339 * @brief Marshals value v as marshalled type into message
1341 static void set(const DBusWrapper::MessageIterPtr& iter, const ValueOrError<ARGS...>& args)
1343 signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::set(iter, args.getValues());
1347 * @brief Marshals value from marshalled type into variable v
1349 static bool get(const DBusWrapper::MessageIterPtr& iter, ValueOrError<ARGS...>& args)
1351 return signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::get(iter, args.getValues());
1356 * @brief Signature class for marshalling ValueOrError<void> type
1359 struct signature<ValueOrError<void>> : signature_helper<signature<ValueOrError<void>>>
1361 static constexpr auto name_v = concat("ValueOrError<void>");
1362 static constexpr auto sig_v = concat("");
1365 * @brief Marshals value v as marshalled type into message
1367 static void set(const DBusWrapper::MessageIterPtr& iter, const ValueOrError<void>& args)
1372 * @brief Marshals value from marshalled type into variable v
1374 static bool get(const DBusWrapper::MessageIterPtr& iter, ValueOrError<void>& args)
1381 * @brief Signature class for marshalling ValueOrError<> type
1384 struct signature<ValueOrError<>> : signature_helper<signature<ValueOrError<>>>
1386 static constexpr auto name_v = concat("ValueOrError<>");
1387 static constexpr auto sig_v = concat("");
1390 * @brief Marshals value v as marshalled type into message
1392 static void set(const DBusWrapper::MessageIterPtr& iter, const ValueOrError<>& args)
1397 * @brief Marshals value from marshalled type into variable v
1399 static bool get(const DBusWrapper::MessageIterPtr& iter, ValueOrError<>& args)
1406 * @brief Signature class for marshalling pair of types
1408 template<typename A, typename B>
1409 struct signature<std::pair<A, B>> : signature_helper<signature<std::pair<A, B>>>
1411 static constexpr auto name_v = concat("pair<", signature_tuple_helper<0, 2, A, B>::name_v, ">");
1412 static constexpr auto sig_v = concat("(", signature_tuple_helper<0, 2, A, B>::sig_v, ")");
1415 * @brief Marshals value v as marshalled type into message
1417 static void set(const DBusWrapper::MessageIterPtr& iter, const std::pair<A, B>& ab, bool dictionary = false)
1419 auto entry = DBUS_W->eldbus_message_iter_container_new_impl(iter, dictionary ? 'e' : 'r', "");
1420 signature_tuple_helper<0, 2, A, B>::set(entry, ab);
1424 * @brief Marshals value from marshalled type into variable v
1426 static bool get(const DBusWrapper::MessageIterPtr& iter, std::pair<A, B>& ab)
1428 auto entry = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, 'r');
1431 entry = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, '{');
1432 if(!entry) return false;
1435 std::tuple<A, B> ab_tmp;
1436 auto z = signature_tuple_helper<0, 2, A, B>::get(entry, ab_tmp);
1439 ab.first = std::move(std::get<0>(ab_tmp));
1440 ab.second = std::move(std::get<1>(ab_tmp));
1447 * @brief Signature class for marshalling std::vector template type
1449 * This marshals container's content as DBUS a ascii character type code.
1451 template<typename A>
1452 struct signature<std::vector<A>> : signature_helper<signature<std::vector<A>>>
1454 static constexpr auto name_v = concat("vector<", signature<A>::name_v, ">");
1455 static constexpr auto sig_v = concat("a", signature<A>::sig_v);
1458 * @brief Marshals value v as marshalled type into message
1460 static void set(const DBusWrapper::MessageIterPtr& iter, const std::vector<A>& v)
1462 auto lst = DBUS_W->eldbus_message_iter_container_new_impl(iter, 'a', std::string{signature<A>::sig()});
1466 signature<A>::set(lst, a);
1471 * @brief Marshals value from marshalled type into variable v
1473 static bool get(const DBusWrapper::MessageIterPtr& iter, std::vector<A>& v)
1475 auto s = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, 'a');
1476 if(!s) return false;
1479 while(signature<A>::get(s, a))
1480 v.push_back(std::move(a));
1487 * @brief Signature class for marshalling std::array template type
1489 * This marshals container's content as DBUS a ascii character type code.
1491 template<typename A, size_t N>
1492 struct signature<std::array<A, N>> : signature_helper<signature<std::array<A, N>>>
1494 static constexpr auto name_v = concat("array<", signature<A>::name_v, ", ", to_string<N>::value, ">");
1495 static constexpr auto sig_v = concat("a", signature<A>::sig_v);
1498 * @brief Marshals value v as marshalled type into message
1500 static void set(const DBusWrapper::MessageIterPtr& iter, const std::array<A, N>& v)
1502 auto lst = DBUS_W->eldbus_message_iter_container_new_impl(iter, 'a', std::string{signature<A>::sig()});
1506 signature<A>::set(lst, a);
1511 * @brief Marshals value from marshalled type into variable v
1513 static bool get(const DBusWrapper::MessageIterPtr& iter, std::array<A, N>& v)
1515 auto s = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, 'a');
1520 if(!signature<A>::get(s, a))
1528 * @brief Signature class for marshalling EldbusVariant type
1530 * This marshals variant's content as DBUS v ascii character type code.
1532 template<typename A>
1533 struct signature<EldbusVariant<A>> : signature_helper<signature<EldbusVariant<A>>>
1535 static constexpr auto name_v = concat("variant<", signature<A>::name_v, ">");
1536 static constexpr auto sig_v = concat("v");
1539 * @brief Marshals value v as marshalled type into message
1541 static void set(const DBusWrapper::MessageIterPtr& iter, const EldbusVariant<A>& v)
1547 * @brief Marshals value v as marshalled type into message
1549 static void set(const DBusWrapper::MessageIterPtr& iter, const A& v)
1551 auto var = DBUS_W->eldbus_message_iter_container_new_impl(iter, 'v', std::string{signature<A>::sig()});
1552 signature<A>::set(var, v);
1556 * @brief Marshals value from marshalled type into variable v
1558 static bool get(const DBusWrapper::MessageIterPtr& iter, EldbusVariant<A>& v)
1560 auto s = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, 'v');
1563 return signature<A>::get(s, v.value);
1568 * @brief Signature class for marshalling std::unordered_map template type
1570 * This marshals container's content as DBUS {} ascii character type code.
1571 * Note, that library doesnt check, if the key is basic type, as DBUS
1572 * specification mandates.
1573 * User can always exchange std::unordered_map for std::map and the reverse.
1574 * User can receive such values as std::vector of std::pair<key, value> values.
1575 * Order of such values is unspecified.
1577 template<typename A, typename B>
1578 struct signature<std::unordered_map<A, B>> : signature_helper<signature<std::unordered_map<A, B>>>
1580 static constexpr auto name_v = concat("unordered_map<", signature<A>::name_v, ", ", signature<B>::name_v, ">");
1581 static constexpr auto sig_v = concat("a{", signature_tuple_helper<0, 2, A, B>::sig_v, "}");
1584 * @brief Marshals value v as marshalled type into message
1586 static void set(const DBusWrapper::MessageIterPtr& iter, const std::unordered_map<A, B>& v)
1588 auto sig = "{" + std::string{signature_tuple_helper<0, 2, A, B>::sig()} + "}";
1589 auto lst = DBUS_W->eldbus_message_iter_container_new_impl(iter, 'a', sig);
1593 signature<std::pair<A, B>>::set(lst, a, true);
1598 * @brief Marshals value from marshalled type into variable v
1600 static bool get(const DBusWrapper::MessageIterPtr& iter, std::unordered_map<A, B>& v)
1602 auto s = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, 'a');
1607 while(signature<std::pair<A, B>>::get(s, a))
1608 v.insert(std::move(a));
1614 * @brief Signature class for marshalling std::unordered_map template type
1616 * This marshals container's content as DBUS {} ascii character type code.
1617 * Note, that library doesnt check, if the key is basic type, as DBUS
1618 * specification mandates.
1619 * User can always exchange std::unordered_map for std::map and the reverse.
1620 * User can receive such values as std::vector of std::pair<key, value> values.
1621 * Order of such values is unspecified.
1623 template<typename A, typename B>
1624 struct signature<std::map<A, B>> : signature_helper<signature<std::map<A, B>>>
1626 static constexpr auto name_v = concat("map<", signature<A>::name_v, ", ", signature<B>::name_v, ">");
1627 static constexpr auto sig_v = concat("a{", signature_tuple_helper<0, 2, A, B>::sig_v, "}");
1630 * @brief Marshals value v as marshalled type into message
1632 static void set(const DBusWrapper::MessageIterPtr& iter, const std::map<A, B>& v)
1634 auto sig = "{" + std::string{signature_tuple_helper<0, 2, A, B>::sig()} + "}";
1635 auto lst = DBUS_W->eldbus_message_iter_container_new_impl(iter, 'a', sig);
1639 signature<std::pair<A, B>>::set(lst, a, true);
1644 * @brief Marshals value from marshalled type into variable v
1646 static bool get(const DBusWrapper::MessageIterPtr& iter, std::map<A, B>& v)
1648 auto s = DBUS_W->eldbus_message_iter_get_and_next_by_type_impl(iter, 'a');
1652 while(signature<std::pair<A, B>>::get(s, a))
1653 v.insert(std::move(a));
1659 * @brief Signature helper class for marshalling const reference types
1661 template<typename A>
1662 struct signature<const A&> : signature_helper<signature<const A&>>
1664 static constexpr auto name_v = concat("const ", signature<A>::name_v, "&");
1665 static constexpr auto sig_v = signature<A>::sig_v;
1668 * @brief Marshals value v as marshalled type into message
1670 static void set(const DBusWrapper::MessageIterPtr& iter, const A& v)
1672 signature<A>::set(iter, v);
1676 * @brief Marshals value from marshalled type into variable v
1678 static void get(const DBusWrapper::MessageIterPtr& iter, A& v)
1680 signature<A>::get(iter, v);
1685 * @brief Signature helper class for marshalling reference types
1687 template<typename A>
1688 struct signature<A&> : signature_helper<signature<A&>>
1690 static constexpr auto name_v = concat(signature<A>::name_v, "&");
1691 static constexpr auto sig_v = signature<A>::sig_v;
1694 * @brief Marshals value v as marshalled type into message
1696 static void set(const DBusWrapper::MessageIterPtr& iter, const A& v)
1698 signature<A>::set(iter, v);
1702 * @brief Marshals value from marshalled type into variable v
1704 static void get(const DBusWrapper::MessageIterPtr& iter, A& v)
1706 signature<A>::get(iter, v);
1711 * @brief Signature helper class for marshalling const types
1713 template<typename A>
1714 struct signature<const A> : signature_helper<signature<const A>>
1716 static constexpr auto name_v = concat("const ", signature<A>::name_v);
1717 static constexpr auto sig_v = signature<A>::sig_v;
1720 * @brief Marshals value v as marshalled type into message
1722 static void set(const DBusWrapper::MessageIterPtr& iter, const A& v)
1724 signature<A>::set(iter, v);
1728 * @brief Marshals value from marshalled type into variable v
1730 static void get(const DBusWrapper::MessageIterPtr& iter, A& v)
1732 signature<A>::get(iter, v);
1736 using CallId = DBusWrapper::CallId;
1738 template<typename ValueType>
1739 ValueType unpackValues(CallId callId, const DBusWrapper::MessagePtr& msg)
1741 static const std::string sig{signature<ValueType>::sig().data()};
1743 auto iter = DBUS_W->eldbus_message_iter_get_impl(msg, false);
1748 if(!signature<ValueType>::get(iter, r))
1750 DBUS_DEBUG("ValueType is %s", signature<ValueType>::name().data());
1751 r = Error{"call " + std::to_string(callId.id) + ": failed to unpack values, got signature '" +
1752 DBUS_W->eldbus_message_signature_get_impl(msg) + "', expected '" + sig + "'"};
1757 r = Error{"call " + std::to_string(callId.id) + ": failed to get iterator"};
1762 inline void packValues_helper(const DBusWrapper::MessageIterPtr&)
1766 template<typename A, typename... ARGS>
1767 void packValues_helper(const DBusWrapper::MessageIterPtr& iter, A&& a, ARGS&&... r)
1769 signature<A>::set(iter, std::forward<A>(a));
1770 packValues_helper(iter, std::forward<ARGS>(r)...);
1773 template<typename... ARGS>
1774 void packValues(CallId callId, const DBusWrapper::MessagePtr& msg, ARGS&&... r)
1776 auto iter = DBUS_W->eldbus_message_iter_get_impl(msg, true);
1777 packValues_helper(iter, std::forward<ARGS>(r)...);
1780 struct ConnectionState
1782 DBusWrapper::ConnectionPtr connection;
1783 DBusWrapper::ObjectPtr object;
1784 DBusWrapper::ProxyPtr proxy, propertiesProxy;
1787 template<typename RETTYPE, typename... ARGS>
1788 RETTYPE call(CallId callId, const ConnectionState& connectionState, bool property, const std::string& funcName, const ARGS&... args)
1790 const auto& proxy = property ? connectionState.propertiesProxy : connectionState.proxy;
1793 DBUS_DEBUG("call %d: not initialized", callId.id);
1794 return Error{"not initialized"};
1797 DBUS_DEBUG("call %d: calling '%s'", callId.id, funcName.c_str());
1798 auto msg = DBUS_W->eldbus_proxy_method_call_new_impl(proxy, funcName);
1801 DBUS_DEBUG("call %d: failed", callId.id);
1802 return Error{"failed to create message"};
1805 detail::packValues(callId, msg, args...);
1806 auto reply = DBUS_W->eldbus_proxy_send_and_block_impl(proxy, msg);
1807 DBUS_DEBUG("call %d: calling '%s' done", callId.id, funcName.c_str());
1810 DBUS_DEBUG("call %d: failed", callId.id);
1811 return Error{"eldbus returned null as reply"};
1813 std::string errname, errmsg;
1814 if(DBUS_W->eldbus_message_error_get_impl(reply, errname, errmsg))
1816 DBUS_DEBUG("call %d: %s: %s", callId.id, errname.c_str(), errmsg.c_str());
1817 return Error{errname + ": " + errmsg};
1819 DBUS_DEBUG("call %d: got reply with signature '%s'", callId.id, DBUS_W->eldbus_message_signature_get_impl(reply).c_str());
1820 return detail::unpackValues<RETTYPE>(callId, reply);
1823 template<typename RETTYPE, typename... ARGS>
1824 void asyncCall(CallId callId, const ConnectionState& connectionState, bool property, const std::string& funcName, std::function<void(RETTYPE)> callback, const ARGS&... args)
1826 const auto& proxy = property ? connectionState.propertiesProxy : connectionState.proxy;
1829 DBUS_DEBUG("call %d: not initialized", callId.id);
1830 callback(Error{"not initialized"});
1834 auto msg = DBUS_W->eldbus_proxy_method_call_new_impl(proxy, funcName);
1837 DBUS_DEBUG("call %d: failed", callId.id);
1838 callback(Error{"failed to create message"});
1842 detail::packValues(callId, msg, args...);
1843 auto pending = DBUS_W->eldbus_proxy_send_impl(proxy, msg, [callback, callId, proxy](const DBusWrapper::MessagePtr& reply) {
1844 DBUS_DEBUG("call %d: calling done", callId.id);
1847 DBUS_DEBUG("call %d: failed", callId.id);
1848 callback(Error{"eldbus returned null as reply"});
1852 std::string errname, errmsg;
1853 if(DBUS_W->eldbus_message_error_get_impl(reply, errname, errmsg))
1855 DBUS_DEBUG("call %d: %s: %s", callId.id, errname.c_str(), errmsg.c_str());
1856 callback(Error{errname + ": " + errmsg, ErrorType::INVALID_REPLY});
1860 DBUS_DEBUG("call %d: got reply with signature '%s'", callId.id, DBUS_W->eldbus_message_signature_get_impl(reply).c_str());
1861 callback(detail::unpackValues<RETTYPE>(callId, reply));
1867 DBUS_DEBUG("call %d: call sent", callId.id);
1871 DBUS_DEBUG("call %d: failed to send call", callId.id);
1872 callback(Error{"failed to send call"});
1876 inline void displayDebugCallInfo(CallId callId, const std::string& funcName, const std::string& info, const std::string& interfaceName)
1878 DBUS_DEBUG("call %d: %s iname = %s fname = %s", callId.id, info.c_str(), interfaceName.c_str(), funcName.c_str());
1881 inline void displayDebugCallInfoSignal(CallId callId, const std::string& funcName, const std::string& info, const std::string& interfaceName)
1883 DBUS_DEBUG("call %d: %s signal iname = %s fname = %s", callId.id, info.c_str(), interfaceName.c_str(), funcName.c_str());
1886 inline void displayDebugCallInfoProperty(CallId callId, const std::string& funcName, std::string info, const std::string& interfaceName, const std::string& propertyName)
1888 DBUS_DEBUG("call %d: %s iname = %s pname = %s", callId.id, info.c_str(), interfaceName.c_str(), propertyName.c_str());
1891 using StringStorage = DBusWrapper::StringStorage;
1893 template<typename A, typename... ARGS>
1894 struct EldbusArgGenerator_Helper
1896 static void add(std::vector<std::pair<std::string, std::string>>& r)
1899 auto sig = std::string{signature<A>::sig()};
1900 assert(!sig.empty());
1901 auto name = "p" + std::to_string(s + 1);
1902 r.push_back({std::move(sig), std::move(name)});
1903 EldbusArgGenerator_Helper<ARGS...>::add(r);
1908 struct EldbusArgGenerator_Helper<void>
1910 static void add(std::vector<std::pair<std::string, std::string>>&)
1916 struct EldbusArgGenerator_Helper<ValueOrError<void>, void>
1918 static void add(std::vector<std::pair<std::string, std::string>>&)
1923 struct EldbusArgGenerator_Helper<ValueOrError<>, void>
1925 static void add(std::vector<std::pair<std::string, std::string>>&)
1930 template<typename... ARGS>
1931 struct EldbusArgGenerator_Helper<std::tuple<ARGS...>>
1933 static void add(std::vector<std::pair<std::string, std::string>>& r)
1935 EldbusArgGenerator_Helper<ARGS..., void>::add(r);
1939 template<typename RetType>
1940 struct dbus_interface_return_type_traits
1942 using type = ValueOrError<RetType>;
1945 template<typename... ARGS>
1946 struct dbus_interface_return_type_traits<ValueOrError<ARGS...>>
1948 using type = ValueOrError<ARGS...>;
1951 template<typename T>
1952 struct dbus_interface_traits;
1953 template<typename RetType, typename... ARGS>
1954 struct dbus_interface_traits<RetType(ARGS...)>
1956 using Ret = typename dbus_interface_return_type_traits<RetType>::type;
1957 using SyncCB = std::function<Ret(ARGS...)>;
1958 using AsyncCB = std::function<void(std::function<void(Ret)>, ARGS...)>;
1959 using VEArgs = ValueOrError<ARGS...>;
1962 template<typename T>
1963 struct EldbusArgGenerator_Args;
1964 template<typename RetType, typename... ARGS>
1965 struct EldbusArgGenerator_Args<RetType(ARGS...)>
1967 static std::string name()
1969 return std::string{signature_tuple_helper<0, sizeof...(ARGS), ARGS...>::name()};
1971 static std::vector<std::pair<std::string, std::string>> get()
1973 std::vector<std::pair<std::string, std::string>> tmp;
1974 EldbusArgGenerator_Helper<ARGS..., void>::add(tmp);
1979 template<typename T>
1980 struct EldbusArgGenerator_ReturnType;
1981 template<typename RetType, typename... ARGS>
1982 struct EldbusArgGenerator_ReturnType<RetType(ARGS...)>
1984 static std::string name()
1986 return std::string{signature<RetType>::name()};
1988 static std::vector<std::pair<std::string, std::string>> get()
1990 std::vector<std::pair<std::string, std::string>> tmp;
1991 EldbusArgGenerator_Helper<RetType, void>::add(tmp);
1996 template<typename T>
1997 struct EldbusArgGenerator_ReturnType;
1998 template<typename... ARGS>
1999 struct EldbusArgGenerator_ReturnType<void(ARGS...)>
2001 static std::string name()
2005 static std::vector<std::pair<std::string, std::string>> get()
2011 } // namespace detail
2013 using ConnectionType = DBusWrapper::ConnectionType;
2016 * @brief Class representing client's end of DBUS connection
2018 * Allows calling (synchronous and asynchronous) methods on selected interface
2019 * Allows (synchronous and asynchronous) setting / getting properties.
2020 * Allows registering signals.
2024 struct ConnectionInfo
2026 std::string interfaceName, busName, pathName;
2031 * @brief Default constructor, creates non-connected object.
2033 DBusClient() = default;
2036 * @brief Connects to dbus choosen by tp, using given arguments
2038 * @param bus_name name of the bus to connect to
2039 * @param path_name object's path
2040 * @param interface_name interface name
2042 DBusClient(std::string busName_, std::string pathName_, std::string interfaceName_, ConnectionType tp);
2045 * @brief Connects to dbus using connection conn
2047 * @param bus_name name of the bus to connect to
2048 * @param path_name object's path
2049 * @param interface_name interface name
2050 * @param conn connection object from getDBusConnectionByType call
2052 DBusClient(std::string busName_, std::string pathName_, std::string interfaceName_, const DBusWrapper::ConnectionPtr& conn = {});
2055 * @brief Destructor object.
2057 * All signals added will be disconnected.
2058 * All asynchronous calls will be cancelled, their callback's will be called
2059 * with failure message.
2061 ~DBusClient() = default;
2062 DBusClient(const DBusClient&) = delete;
2063 DBusClient(DBusClient&&) = default;
2065 DBusClient& operator=(DBusClient&&) = default;
2066 DBusClient& operator=(const DBusClient&) = delete;
2069 * @brief bool operator
2071 * Returns true, if object is connected to DBUS
2073 explicit operator bool() const
2075 return bool(connectionState->proxy);
2079 * @brief Helper class for calling a method
2081 * Template type T defines both arguments sent to the method
2082 * and expected values. Receiving different values will be reported as
2083 * error. For example:
2084 * \code{.cpp} Method<int(float, float)> \endcode
2085 * defines method, which takes two arguments (two floats) and return
2086 * single value of type int.
2088 template<typename T>
2091 using RetType = typename detail::dbus_interface_traits<T>::Ret;
2092 const detail::ConnectionState& connectionState;
2093 std::string funcName;
2095 std::shared_ptr<ConnectionInfo> connectionInfo;
2098 * @brief Executes synchronous call on DBUS's method
2100 * The function returns ValueOrError<...> object, which
2101 * contains either received values or error message.
2103 * @param args arguments to pass to the method
2105 template<typename... ARGS>
2106 RetType call(const ARGS&... args)
2108 detail::CallId callId;
2109 detail::displayDebugCallInfo(callId, funcName, info, connectionInfo->interfaceName);
2110 return detail::call<RetType>(callId, connectionState, false, funcName, args...);
2114 * @brief Executes asynchronous call on DBUS's method
2116 * The function calls callback with either received values or error message.
2118 * @param callback callback functor, which will be called with return value(s) or error message
2119 * @param args arguments to pass to the method
2121 template<typename... ARGS>
2122 void asyncCall(std::function<void(RetType)> callback, const ARGS&... args)
2124 detail::CallId callId;
2125 detail::displayDebugCallInfo(callId, funcName, info, connectionInfo->interfaceName);
2126 detail::asyncCall<RetType>(callId, connectionState, false, funcName, std::move(callback), args...);
2131 * @brief Helper class for calling a property
2133 * Template type T defines type of the value hidden under property.
2134 * Note, that library automatically wraps both sent and received value into
2135 * DBUS's wrapper type.
2137 template<typename T>
2140 using RetType = typename detail::dbus_interface_return_type_traits<T>::type;
2141 using VariantRetType = typename detail::dbus_interface_return_type_traits<EldbusVariant<T>>::type;
2142 const detail::ConnectionState& connectionState;
2143 std::string propName;
2145 std::shared_ptr<ConnectionInfo> connectionInfo;
2148 * @brief executes synchronous get on property
2150 * The function returns ValueOrError<...> object, which
2151 * contains either received values or error message.
2155 detail::CallId callId;
2156 detail::displayDebugCallInfoProperty(callId, "Get", info, connectionInfo->interfaceName, propName);
2157 auto z = detail::call<VariantRetType>(callId, connectionState, true, "Get", connectionInfo->interfaceName, propName);
2159 return z.getError();
2160 return {std::get<0>(z.getValues()).value};
2164 * @brief executes asynchronous get on property
2166 * The function calls callback with either received values or error message.
2168 * @param callback callback functor, which will be called with return value(s) or error message
2170 void asyncGet(std::function<void(RetType)> callback)
2172 detail::CallId callId;
2173 detail::displayDebugCallInfoProperty(callId, "Get", info, connectionInfo->interfaceName, propName);
2174 auto cc = [callback](VariantRetType reply) {
2176 callback(std::move(std::get<0>(reply.getValues()).value));
2178 callback(reply.getError());
2180 detail::asyncCall<VariantRetType>(callId, connectionState, true, "Get", std::move(cc), connectionInfo->interfaceName, propName);
2184 * @brief executes synchronous set on property
2186 * The function returns ValueOrError<void> object, with
2187 * possible error message.
2189 ValueOrError<void> set(const T& r)
2191 detail::CallId callId;
2192 detail::displayDebugCallInfoProperty(callId, "Set", info, connectionInfo->interfaceName, propName);
2193 EldbusVariant<T> variantValue{std::move(r)};
2194 return detail::call<ValueOrError<void>>(callId, connectionState, true, "Set", connectionInfo->interfaceName, propName, variantValue);
2198 * @brief executes asynchronous get on property
2200 * The function calls callback with either received values or error message.
2202 * @param callback callback functor, which will be called with return value(s) or error message
2204 void asyncSet(std::function<void(ValueOrError<void>)> callback, const T& r)
2206 detail::CallId callId;
2207 detail::displayDebugCallInfoProperty(callId, "Set", info, connectionInfo->interfaceName, propName);
2208 EldbusVariant<T> variantValue{std::move(r)};
2209 detail::asyncCall<ValueOrError<void>>(callId, connectionState, true, "Set", std::move(callback), connectionInfo->interfaceName, propName, variantValue);
2214 * @brief Constructs Property<...> object for calling properties
2216 * The function constructs and returns proxy object for calling property.
2218 * @param propName property name to set and / or query
2220 template<typename PropertyType>
2221 Property<PropertyType> property(std::string propName)
2223 return Property<PropertyType>{*connectionState, std::move(propName), info, connectionInfo};
2227 * @brief Constructs Method<...> object for calling methods
2229 * The function constructs and returns proxy object for calling method.
2231 * @param funcName function name to call
2233 template<typename MethodType>
2234 Method<MethodType> method(std::string funcName)
2236 return Method<MethodType>{*connectionState, std::move(funcName), info, connectionInfo};
2240 * @brief Registers notification callback, when property has changed
2242 * The callback will be called with new value, when property's value has changed.
2243 * Note, that template type V must match expected type, otherwise undefined behavior will occur,
2244 * there's no check for this.
2246 template<typename V>
2247 void addPropertyChangedEvent(std::string propertyName, std::function<void(V)> callback)
2249 detail::CallId callId;
2250 detail::displayDebugCallInfoSignal(callId, propertyName, info, connectionInfo->interfaceName);
2251 DBUS_DEBUG("call %d: adding property", callId.id);
2252 auto& cI = this->connectionInfo;
2253 DBUS_W->add_property_changed_event_listener_impl(connectionState->proxy, cI->interfaceName, propertyName, [callback](const _Eina_Value* newValue) {
2255 if(!getFromEinaValue(newValue, &val))
2257 DBUS_DEBUG("unable to get property's value");
2265 * @brief Registers callback on the DBUS' signal
2267 * The function registers callback signalName. When signal comes, callback will be called.
2268 * Callback object will exists as long as signal is registered. You can unregister signal
2269 * by destroying DBusClient object.
2271 * @param signalName name of the signal to register
2272 * @param callback callback to call
2274 template<typename SignalType>
2275 void addSignal(std::string signalName, std::function<SignalType> callback)
2277 detail::CallId callId;
2278 detail::displayDebugCallInfoSignal(callId, signalName, info, connectionInfo->interfaceName);
2279 DBUS_W->eldbus_proxy_signal_handler_add_impl(connectionState->proxy, signalName, [callId, callback, signalName](const DBusWrapper::MessagePtr& msg) -> void {
2280 std::string errname, aux;
2281 if(DBUS_W->eldbus_message_error_get_impl(msg, errname, aux))
2283 DBUS_DEBUG("call %d: Eldbus error: %s %s", callId.id, errname.c_str(), aux.c_str());
2286 DBUS_DEBUG("call %d: received signal with signature '%s'", callId.id, DBUS_W->eldbus_message_signature_get_impl(msg).c_str());
2287 using ParamsType = typename detail::dbus_interface_traits<SignalType>::VEArgs;
2288 auto params = detail::unpackValues<ParamsType>(callId, msg);
2291 DBUS_DEBUG("call %d: failed: %s", callId.id, params.getError().message.c_str());
2296 std::apply(callback, params.getValues());
2298 catch(const Dali::DaliException& e)
2300 DBUS_DEBUG("unhandled exception");
2307 std::unique_ptr<detail::ConnectionState> connectionState{new detail::ConnectionState};
2309 std::shared_ptr<ConnectionInfo> connectionInfo;
2311 static bool getFromEinaValue(const _Eina_Value* v, void* dst);
2315 * @brief Helper class describing DBUS's server interface
2318 class DBusInterfaceDescription
2320 friend class DBusServer;
2323 using MethodInfo = DBusWrapper::MethodInfo;
2324 using SignalInfo = DBusWrapper::SignalInfo;
2325 using PropertyInfo = DBusWrapper::PropertyInfo;
2326 using SignalId = DBusWrapper::SignalId;
2329 * @brief Creates empty interface description with given name
2331 * @param interfaceName name of the interface
2333 DBusInterfaceDescription(std::string interfaceName);
2336 * @brief adds new, synchronous method to the interface
2338 * When method memberName is called on DBUS, callback functor will be called
2339 * with values received from DBUS. callback won't be called, if method was
2340 * called with invalid signature. Value returned from functor (or error message)
2341 * will be marshalled back to the caller.
2343 * Template type T defines both arguments sent to the method
2344 * and expected values. Receiving different values will be reported as
2345 * error. For example:
2346 * \code{.cpp} Method<int(float, float)> \endcode
2347 * defines method, which takes two arguments (two floats) and return
2348 * single value of type int.
2350 * @param memberName name of the method
2351 * @param callback functor, which will be called
2353 template<typename T>
2354 void addMethod(const std::string& memberName, typename detail::dbus_interface_traits<T>::SyncCB callback)
2356 detail::CallId callId;
2358 methods.push_back(std::move(mi));
2359 auto& z = methods.back();
2360 z.in = detail::EldbusArgGenerator_Args<T>::get();
2361 z.out = detail::EldbusArgGenerator_ReturnType<T>::get();
2362 z.memberName = memberName;
2363 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());
2364 z.callback = construct<T>(callId, callback);
2369 * @brief adds new, synchronous property to the interface
2371 * When property memberName is called on DBUS, respective callback functor will be called
2372 * with values received from DBUS. callback won't be called, if method was
2373 * called with invalid signature. Value returned from functor (or error message)
2374 * will be marshalled back to the caller.
2376 * Template type T defines type of the value hidden under property.
2377 * Note, that library automatically wraps both sent and received value into
2378 * DBUS's wrapper type.
2380 * @param memberName name of the method
2381 * @param getter functor, which will be called when property is being read
2382 * @param setter functor, which will be called when property is being set
2384 template<typename T>
2385 void addProperty(const std::string& memberName, std::function<ValueOrError<T>()> getter, std::function<ValueOrError<void>(T)> setter)
2387 properties.push_back({});
2388 auto& z = properties.back();
2389 z.memberName = memberName;
2390 z.typeSignature = detail::signature<T>::sig();
2393 detail::CallId getterId;
2394 z.getterId = getterId;
2395 DBUS_DEBUG("call %d: property %s (get) type %s", getterId.id, memberName.c_str(), detail::signature<T>::name().data());
2396 z.getCallback = [=](const DBusWrapper::MessagePtr& src, const DBusWrapper::MessageIterPtr& dst) -> std::string {
2399 auto v = std::apply(getter, std::tuple<>{});
2402 detail::signature<T>::set(dst, std::get<0>(v.getValues()));
2403 DBUS_DEBUG("call %d: success", getterId.id);
2406 DBUS_DEBUG("call %d: failed: %s", getterId.id, v.getError().message.c_str());
2407 return v.getError().message;
2409 catch(std::exception& e)
2411 return std::string("unhandled exception (") + e.what() + ")";
2413 catch(const Dali::DaliException& e)
2415 return std::string("unhandled exception (") + e.condition + ")";
2421 detail::CallId setterId;
2422 z.setterId = setterId;
2423 DBUS_DEBUG("call %d: property %s (set) type %s", setterId.id, memberName.c_str(), detail::signature<T>::name().data());
2424 z.setCallback = [=](const DBusWrapper::MessagePtr& src, const DBusWrapper::MessageIterPtr& src_iter) -> std::string {
2425 std::tuple<T> value;
2426 auto src_signature = DBUS_W->eldbus_message_iter_signature_get_impl(src_iter);
2427 if(detail::signature<T>::get(src_iter, std::get<0>(value)))
2431 auto v = std::apply(setter, std::move(value));
2434 DBUS_DEBUG("call %d: success", setterId.id);
2437 DBUS_DEBUG("call %d: failed: %s", setterId.id, v.getError().message.c_str());
2438 return v.getError().message;
2440 catch(std::exception& e)
2442 return std::string("unhandled exception (") + e.what() + ")";
2444 catch(const Dali::DaliException& e)
2446 return std::string("unhandled exception (") + e.condition + ")";
2449 DBUS_DEBUG("call %d: failed to unpack values, got signature '%s', expected '%s'", setterId.id, src_signature.c_str(), detail::signature<T>::sig().data());
2450 return "call " + std::to_string(setterId.id) + ": failed to unpack values, got signature '" +
2451 src_signature + "', expected '" + std::string{detail::signature<T>::sig()} + "'";
2457 * @brief adds new signal to the interface
2459 * Template types ARGS defines values, which will be emited with the signal
2461 * @param memberName name of the method
2463 template<typename... ARGS>
2464 SignalId addSignal(const std::string& memberName)
2466 detail::CallId callId;
2467 signals.push_back({});
2468 auto& z = signals.back();
2469 z.memberName = memberName;
2470 z.args = detail::EldbusArgGenerator_Args<void(ARGS...)>::get(DBUS_W->Strings);
2472 DBUS_DEBUG("call %d: signal %s", callId.id, memberName.c_str());
2473 return SignalId{callId};
2477 std::vector<MethodInfo> methods;
2478 std::vector<PropertyInfo> properties;
2479 std::vector<SignalInfo> signals;
2480 std::string interfaceName;
2482 template<typename T>
2483 std::function<DBusWrapper::MessagePtr(const DBusWrapper::MessagePtr& msg)> construct(detail::CallId callId,
2484 typename detail::dbus_interface_traits<T>::SyncCB callback)
2486 using VEArgs = typename detail::dbus_interface_traits<T>::VEArgs;
2487 return [=](const DBusWrapper::MessagePtr& msg) -> DBusWrapper::MessagePtr {
2488 DBUS_DEBUG("call %d: entering", callId.id);
2489 DBusWrapper::MessagePtr ret = {};
2490 auto args = detail::unpackValues<VEArgs>(callId, msg);
2495 auto v = std::apply(callback, std::move(args.getValues()));
2498 DBUS_DEBUG("call %d: success", callId.id);
2499 ret = DBUS_W->eldbus_message_method_return_new_impl(msg);
2500 detail::packValues(callId, ret, v);
2504 DBUS_DEBUG("call %d: failed: %s", callId.id, v.getError().message.c_str());
2505 ret = DBUS_W->eldbus_message_error_new_impl(msg, "org.freedesktop.DBus.Error.Failed", v.getError().message);
2508 catch(std::exception& e)
2510 auto txt = std::string("unhandled exception (") + e.what() + ")";
2511 DBUS_DEBUG("call %d: failed: %s", callId.id, txt.c_str());
2512 ret = DBUS_W->eldbus_message_error_new_impl(msg, "org.freedesktop.DBus.Error.Failed", txt);
2514 catch(const Dali::DaliException& e)
2516 auto txt = std::string("unhandled exception (") + e.condition + ")";
2517 DBUS_DEBUG("call %d: failed: %s", callId.id, txt.c_str());
2518 ret = DBUS_W->eldbus_message_error_new_impl(msg, "org.freedesktop.DBus.Error.Failed", txt);
2523 std::ostringstream err;
2524 err << "expected signature '" << detail::signature<VEArgs>::sig() << "', got '" << DBUS_W->eldbus_message_signature_get_impl(msg) << "'";
2525 auto str = err.str();
2526 DBUS_DEBUG("call %d: failed: %s", callId.id, str.c_str());
2527 ret = DBUS_W->eldbus_message_error_new_impl(msg, "org.freedesktop.DBus.Error.InvalidArgs", str);
2535 * @brief Class representing server's end of DBUS connection
2537 * Allows listening (synchronously and asynchronously) on methods on selected interface
2538 * Allows listening (synchronously and asynchronously) on setting / getting properties.
2539 * Allows emiting signals.
2545 * @brief Constructs non-connected dbus server.
2547 DBusServer() = default;
2550 * @brief Constructs dbus server on either system or user dbus connection.
2552 DBusServer(ConnectionType tp);
2555 * @brief Constructs dbus server on connection from getDBusConnectionByType
2557 DBusServer(const DBusWrapper::ConnectionPtr& conn);
2562 * Destructor will properly destroy everything. Destructor will cancel
2565 ~DBusServer() = default;
2567 DBusServer(const DBusServer&) = delete;
2568 DBusServer(DBusServer&&) = default;
2570 DBusServer& operator=(DBusServer&&) = default;
2571 DBusServer& operator=(const DBusServer&) = delete;
2574 * @brief Registers interface on given path name
2576 * @param pathName path object to register interface on.
2580 void addInterface(const std::string& pathName, DBusInterfaceDescription& dscr, bool fallback = false);
2583 * @brief Gets bus name of the current connection (must be connected)
2585 std::string getBusName() const;
2588 * @brief Returns connection object for this dbus server object
2590 * @return connection object
2592 DBusWrapper::ConnectionPtr getConnection();
2595 * @brief Emits signal
2597 * Emits signal based only on data passed to the function
2599 * @param signal identifier of the signal
2600 * @param args values to emit
2602 template<typename... ARGS>
2603 void emit2(const std::string& path, const std::string& interfaceName, const std::string& signalName, const ARGS&... args)
2605 auto msg = DBUS_W->eldbus_message_signal_new_impl(path, interfaceName, signalName);
2607 detail::packValues(id, msg, args...);
2608 DBUS_W->eldbus_connection_send_impl(connection, msg);
2612 * @brief Returns current object path, when handling call to property / method
2614 * User can call this function from inside callback used to handle property / method calls.
2615 * It will retrieve object's path used in the call. Note, that in asynchronous handling
2616 * of those calls user need to retrieve and store the current object / current connection
2617 * as the value will change at the moment user's callback handling will exit. For example:
2619 * DBusInterfaceDescription interface{"name"};
2620 * auto handler_later = [](std::function<void(void)> done_cb) {
2621 * // process something later on
2622 * DBusServer::getCurrentObjectPath(); // this will return empty string
2624 * interface.addAsyncMethod<void()>("m", [=](std::function<void(void)> done_cb) {
2625 * DBusServer::getCurrentObjectPath(); // this will current object's path
2627 * // do some processing later on and call done_cb, when it's done
2628 * register_to_call_sometime_later_on(std::move(done_cb));
2632 static std::string getCurrentObjectPath()
2634 return currentObjectPath;
2638 * @brief Returns current connection object, when handling call to property / method
2640 * User can call this function from inside callback used to handle property / method calls.
2641 * It will retrieve object's path used in the call. Note, that in asynchronous handling
2642 * of those calls user need to retrieve and store the current object / current connection
2643 * as the value will change at the moment user's callback handling will exit. For example:
2645 * DBusInterfaceDescription interface{"name"};
2646 * auto handler_later = [](std::function<void(void)> done_cb) {
2647 * // process something later on
2648 * DBusServer::getCurrentObjectPath(); // this will return empty string
2650 * interface.addAsyncMethod<void()>("m", [=](std::function<void(void)> done_cb) {
2651 * DBusServer::getCurrentObjectPath(); // this will current object's path
2653 * // do some processing later on and call done_cb, when it's done
2654 * register_to_call_sometime_later_on(std::move(done_cb));
2658 static const DBusWrapper::ConnectionPtr& getCurrentConnection()
2660 return currentConnection;
2664 class CurrentObjectSetter
2667 CurrentObjectSetter(DBusWrapper::ConnectionPtr con, std::string path)
2669 currentObjectPath = std::move(path);
2670 currentConnection = std::move(con);
2672 ~CurrentObjectSetter()
2674 currentObjectPath = "";
2675 currentConnection = {};
2677 CurrentObjectSetter(const CurrentObjectSetter&) = delete;
2678 CurrentObjectSetter(CurrentObjectSetter&&) = delete;
2679 void operator=(const CurrentObjectSetter&) = delete;
2680 void operator=(CurrentObjectSetter&&) = delete;
2684 DBusWrapper::ConnectionPtr connection;
2685 struct DestructorObject
2687 std::vector<std::function<void()>> destructors;
2690 for(auto& a : destructors) a();
2694 std::unique_ptr<DestructorObject> destructorObject{new DestructorObject()};
2695 static thread_local std::string currentObjectPath;
2696 static thread_local DBusWrapper::ConnectionPtr currentConnection;
2699 DBusWrapper::ConnectionPtr getDBusConnectionByType(ConnectionType tp);
2700 DBusWrapper::ConnectionPtr getDBusConnectionByName(const std::string& name);
2701 std::string getConnectionName(const DBusWrapper::ConnectionPtr&);
2706 template<size_t INDEX, typename... ARGS>
2707 inline auto get(DBus::ValueOrError<ARGS...>& v) -> decltype(std::get<INDEX>(v.getValues()))&
2709 return std::get<INDEX>(v.getValues());
2712 template<size_t INDEX, typename... ARGS>
2713 inline auto get(const DBus::ValueOrError<ARGS...>& v) -> decltype(std::get<INDEX>(v.getValues()))
2715 return std::get<INDEX>(v.getValues());
2719 #endif // DALI_INTERNAL_ACCESSIBILITY_BRIDGE_DBUS_H