1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
// Copyright (C) 2013-2015 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#if !defined (COMMONAPI_INTERNAL_COMPILATION)
#error "Only <CommonAPI/CommonAPI.hpp> can be included directly, this file may disappear or change contents."
#endif
#ifndef COMMONAPI_DBUS_DBUS_DAEMON_PROXY_HPP_
#define COMMONAPI_DBUS_DBUS_DAEMON_PROXY_HPP_
#include <functional>
#include <string>
#include <vector>
#include <CommonAPI/Address.hpp>
#include <CommonAPI/DBus/DBusAttribute.hpp>
#include <CommonAPI/DBus/DBusEvent.hpp>
#include <CommonAPI/DBus/DBusProxyBase.hpp>
namespace CommonAPI {
namespace DBus {
class StaticInterfaceVersionAttribute: public InterfaceVersionAttribute {
public:
StaticInterfaceVersionAttribute(const uint32_t& majorValue, const uint32_t& minorValue);
void getValue(CommonAPI::CallStatus& callStatus, Version &_version, const CommonAPI::CallInfo *_info) const;
std::future<CommonAPI::CallStatus> getValueAsync(AttributeAsyncCallback _callback, const CommonAPI::CallInfo *_info);
private:
Version version_;
};
#ifndef DBUS_DAEMON_PROXY_DEFAULT_SEND_TIMEOUT
#define DBUS_DAEMON_PROXY_DEFAULT_SEND_TIMEOUT 10000
#endif
static const CommonAPI::CallInfo daemonProxyInfo(DBUS_DAEMON_PROXY_DEFAULT_SEND_TIMEOUT);
class DBusDaemonProxy : public DBusProxyBase,
public std::enable_shared_from_this<DBusDaemonProxy> {
public:
typedef Event<std::string, std::string, std::string> NameOwnerChangedEvent;
typedef std::unordered_map<std::string, int> PropertyDictStub;
typedef std::unordered_map<std::string, PropertyDictStub> InterfaceToPropertyDict;
typedef std::unordered_map<std::string, InterfaceToPropertyDict> DBusObjectToInterfaceDict;
typedef std::function<void(const CommonAPI::CallStatus&, std::vector<std::string>)> ListNamesAsyncCallback;
typedef std::function<void(const CommonAPI::CallStatus&, bool)> NameHasOwnerAsyncCallback;
typedef std::function<void(const CommonAPI::CallStatus&, DBusObjectToInterfaceDict)> GetManagedObjectsAsyncCallback;
typedef std::function<void(const CommonAPI::CallStatus&, std::string)> GetNameOwnerAsyncCallback;
COMMONAPI_EXPORT DBusDaemonProxy(const std::shared_ptr<DBusProxyConnection>& dbusConnection);
COMMONAPI_EXPORT virtual ~DBusDaemonProxy() {}
COMMONAPI_EXPORT virtual bool isAvailable() const;
COMMONAPI_EXPORT virtual bool isAvailableBlocking() const;
COMMONAPI_EXPORT virtual std::future<AvailabilityStatus> isAvailableAsync(
isAvailableAsyncCallback _callback,
const CallInfo *_info) const;
COMMONAPI_EXPORT virtual ProxyStatusEvent& getProxyStatusEvent();
COMMONAPI_EXPORT virtual InterfaceVersionAttribute& getInterfaceVersionAttribute();
COMMONAPI_EXPORT void init();
COMMONAPI_EXPORT static const char* getInterfaceId();
COMMONAPI_EXPORT NameOwnerChangedEvent& getNameOwnerChangedEvent();
COMMONAPI_EXPORT void listNames(CommonAPI::CallStatus& callStatus, std::vector<std::string>& busNames) const;
template <typename DelegateObjectType>
COMMONAPI_EXPORT std::future<CallStatus> listNamesAsync(typename DBusProxyAsyncCallbackHandler<DelegateObjectType,
std::vector<std::string>>::Delegate& delegate) const {
DBusMessage dbusMessage = createMethodCall("ListNames", "");
return getDBusConnection()->sendDBusMessageWithReplyAsync(
dbusMessage,
DBusProxyAsyncCallbackHandler<DelegateObjectType, std::vector<std::string>>::create(delegate, std::tuple<std::vector<std::string>>()),
&daemonProxyInfo);
}
COMMONAPI_EXPORT void nameHasOwner(const std::string& busName, CommonAPI::CallStatus& callStatus, bool& hasOwner) const;
template <typename DelegateObjectType>
COMMONAPI_EXPORT std::future<CallStatus> nameHasOwnerAsync(const std::string& busName,
typename DBusProxyAsyncCallbackHandler<DelegateObjectType,
bool>::Delegate& delegate) const {
DBusMessage dbusMessage = createMethodCall("NameHasOwner", "s");
DBusOutputStream outputStream(dbusMessage);
const bool success = DBusSerializableArguments<std::string>::serialize(outputStream, busName);
if (!success) {
std::promise<CallStatus> promise;
promise.set_value(CallStatus::OUT_OF_MEMORY);
return promise.get_future();
}
outputStream.flush();
return getDBusConnection()->sendDBusMessageWithReplyAsync(
dbusMessage,
DBusProxyAsyncCallbackHandler<DelegateObjectType, bool>::create(delegate, std::tuple<bool>()),
&daemonProxyInfo);
}
template <typename DelegateObjectType>
COMMONAPI_EXPORT std::future<CallStatus> getManagedObjectsAsync(const std::string& forDBusServiceName,
typename DBusProxyAsyncCallbackHandler<DelegateObjectType,
DBusObjectToInterfaceDict>::Delegate& delegate) const {
static DBusAddress address(forDBusServiceName, "/", "org.freedesktop.DBus.ObjectManager");
auto dbusMethodCallMessage = DBusMessage::createMethodCall(address, "GetManagedObjects", "");
return getDBusConnection()->sendDBusMessageWithReplyAsync(
dbusMethodCallMessage,
DBusProxyAsyncCallbackHandler<DelegateObjectType, DBusObjectToInterfaceDict>::create(
delegate, std::tuple<DBusObjectToInterfaceDict>()
),
&daemonProxyInfo);
}
/**
* Get the unique connection/bus name of the primary owner of the name given
*
* @param busName Name to get the owner of
* @param getNameOwnerAsyncCallback callback functor
*
* @return CallStatus::REMOTE_ERROR if the name is unknown, otherwise CallStatus::SUCCESS and the uniq name of the owner
*/
template <typename DelegateObjectType>
std::future<CallStatus> getNameOwnerAsync(const std::string& busName,
typename DBusProxyAsyncCallbackHandler<DelegateObjectType,
std::string>::Delegate& delegate) const {
DBusMessage dbusMessage = createMethodCall("GetNameOwner", "s");
DBusOutputStream outputStream(dbusMessage);
const bool success = DBusSerializableArguments<std::string>::serialize(outputStream, busName);
if (!success) {
std::promise<CallStatus> promise;
promise.set_value(CallStatus::OUT_OF_MEMORY);
return promise.get_future();
}
outputStream.flush();
return getDBusConnection()->sendDBusMessageWithReplyAsync(
dbusMessage,
DBusProxyAsyncCallbackHandler<DelegateObjectType, std::string>::create(delegate, std::tuple<std::string>()),
&daemonProxyInfo);
}
private:
DBusEvent<NameOwnerChangedEvent, std::string, std::string, std::string> nameOwnerChangedEvent_;
StaticInterfaceVersionAttribute interfaceVersionAttribute_;
};
} // namespace DBus
} // namespace CommonAPI
#endif // COMMONAPI_DBUS_DBUS_DAEMON_PROXY_HPP_
|