1 /* Copyright (C) 2013 BMW Group
2 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
3 * Author: Juergen Gehring (juergen.gehring@bmw.de)
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef COMMONAPI_DBUS_DBUS_CONNECTION_H_
8 #define COMMONAPI_DBUS_DBUS_CONNECTION_H_
10 #include "DBusProxyConnection.h"
11 #include "DBusDaemonProxy.h"
12 #include "DBusServiceRegistry.h"
13 #include "DBusObjectManager.h"
15 #include <dbus/dbus.h>
22 class DBusObjectManager;
24 class DBusConnectionStatusEvent: public DBusProxyConnection::ConnectionStatusEvent {
25 friend class DBusConnection;
29 class DBusConnection: public DBusProxyConnection, public std::enable_shared_from_this<DBusConnection> {
32 SESSION = DBUS_BUS_SESSION,
33 SYSTEM = DBUS_BUS_SYSTEM,
34 STARTER = DBUS_BUS_STARTER,
38 inline static std::shared_ptr<DBusConnection> getBus(const BusType& busType);
39 inline static std::shared_ptr<DBusConnection> wrapLibDBus(::DBusConnection* libDbusConnection);
40 inline static std::shared_ptr<DBusConnection> getSessionBus();
41 inline static std::shared_ptr<DBusConnection> getSystemBus();
42 inline static std::shared_ptr<DBusConnection> getStarterBus();
44 DBusConnection(const DBusConnection&) = delete;
45 DBusConnection(::DBusConnection* libDbusConnection);
47 DBusConnection& operator=(const DBusConnection&) = delete;
48 virtual ~DBusConnection();
50 BusType getBusType() const;
53 bool connect(DBusError& dbusError);
56 virtual bool isConnected() const;
58 virtual ConnectionStatusEvent& getConnectionStatusEvent();
60 virtual bool requestServiceNameAndBlock(const std::string& serviceName) const;
61 virtual bool releaseServiceName(const std::string& serviceName) const;
63 bool sendDBusMessage(const DBusMessage& dbusMessage, uint32_t* allocatedSerial = NULL) const;
65 static const int kDefaultSendTimeoutMs = 100 * 1000;
67 std::future<CallStatus> sendDBusMessageWithReplyAsync(
68 const DBusMessage& dbusMessage,
69 std::unique_ptr<DBusMessageReplyAsyncHandler> dbusMessageReplyAsyncHandler,
70 int timeoutMilliseconds = kDefaultSendTimeoutMs) const;
72 DBusMessage sendDBusMessageWithReplyAndBlock(const DBusMessage& dbusMessage,
74 int timeoutMilliseconds = kDefaultSendTimeoutMs) const;
76 DBusSignalHandlerToken addSignalMemberHandler(const std::string& objectPath,
77 const std::string& interfaceName,
78 const std::string& interfaceMemberName,
79 const std::string& interfaceMemberSignature,
80 DBusSignalHandler* dbusSignalHandler);
82 void registerObjectPath(const std::string& objectPath);
83 void unregisterObjectPath(const std::string& objectPath);
85 void removeSignalMemberHandler(const DBusSignalHandlerToken& dbusSignalHandlerToken);
87 bool readWriteDispatch(int timeoutMilliseconds = -1);
89 virtual const std::shared_ptr<DBusDaemonProxy>& getDBusDaemonProxy();
90 virtual const std::shared_ptr<DBusServiceRegistry>& getDBusServiceRegistry();
91 virtual const std::shared_ptr<DBusObjectManager>& getDBusObjectManager();
96 std::thread dispatchThread_;
97 bool stopDispatching_;
99 DBusConnection(BusType busType);
101 void addLibdbusSignalMatchRule(const std::string& objectPath,
102 const std::string& interfaceName,
103 const std::string& interfaceMemberName);
105 void removeLibdbusSignalMatchRule(const std::string& objectPath,
106 const std::string& interfaceName,
107 const std::string& interfaceMemberName);
109 void initLibdbusObjectPathHandlerAfterConnect();
111 void initLibdbusSignalFilterAfterConnect();
113 ::DBusHandlerResult onLibdbusObjectPathMessage(::DBusMessage* libdbusMessage) const;
115 ::DBusHandlerResult onLibdbusSignalFilter(::DBusMessage* libdbusMessage);
117 static void onLibdbusPendingCallNotifyThunk(::DBusPendingCall* libdbusPendingCall, void *userData);
118 static void onLibdbusDataCleanup(void* userData);
120 static ::DBusHandlerResult onLibdbusObjectPathMessageThunk(::DBusConnection* libdbusConnection,
121 ::DBusMessage* libdbusMessage,
124 static ::DBusHandlerResult onLibdbusSignalFilterThunk(::DBusConnection* libdbusConnection,
125 ::DBusMessage* libdbusMessage,
130 ::DBusConnection* libdbusConnection_;
132 std::shared_ptr<DBusDaemonProxy> dbusDaemonProxy_;
133 std::shared_ptr<DBusServiceRegistry> dbusServiceRegistry_;
134 std::shared_ptr<DBusObjectManager> dbusObjectManager_;
136 DBusConnectionStatusEvent dbusConnectionStatusEvent_;
138 typedef std::tuple<std::string, std::string, std::string> DBusSignalMatchRuleTuple;
139 typedef std::pair<uint32_t, std::string> DBusSignalMatchRuleMapping;
140 typedef std::unordered_map<DBusSignalMatchRuleTuple, DBusSignalMatchRuleMapping> DBusSignalMatchRulesMap;
141 DBusSignalMatchRulesMap dbusSignalMatchRulesMap_;
143 bool isLibdbusSignalFilterAdded_;
145 DBusSignalHandlerTable dbusSignalHandlerTable_;
147 // referenceCount, objectPath
148 typedef std::unordered_map<std::string, uint32_t> LibdbusRegisteredObjectPathHandlersTable;
149 LibdbusRegisteredObjectPathHandlersTable libdbusRegisteredObjectPaths_;
151 static DBusObjectPathVTable libdbusObjectPathVTable_;
154 std::shared_ptr<DBusConnection> DBusConnection::getBus(const BusType& busType) {
155 return std::shared_ptr<DBusConnection>(new DBusConnection(busType));
158 std::shared_ptr<DBusConnection> DBusConnection::wrapLibDBus(::DBusConnection* libDbusConnection) {
159 return std::shared_ptr<DBusConnection>(new DBusConnection(libDbusConnection));
162 std::shared_ptr<DBusConnection> DBusConnection::getSessionBus() {
163 return getBus(BusType::SESSION);
166 std::shared_ptr<DBusConnection> DBusConnection::getSystemBus() {
167 return getBus(BusType::SYSTEM);
170 std::shared_ptr<DBusConnection> DBusConnection::getStarterBus() {
171 return getBus(BusType::STARTER);
176 } // namespace CommonAPI
178 #endif // COMMONAPI_DBUS_DBUS_CONNECTION_H_