1 // dbusconnection.h
2 //
3 // Copyright (C) 2019 - swift Project Community / Contributors (http://swift-project.org/)
4 // Adapted to Flightgear by Lars Toenning <dev@ltoenning.de>
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 
20 #ifndef BLACKSIM_FGSWIFTBUS_DBUSCONNECTION_H
21 #define BLACKSIM_FGSWIFTBUS_DBUSCONNECTION_H
22 
23 #include "dbusmessage.h"
24 #include "dbuserror.h"
25 #include "dbuscallbacks.h"
26 #include "dbusdispatcher.h"
27 
28 #include <event2/event.h>
29 #include <dbus/dbus.h>
30 #include <string>
31 #include <unordered_map>
32 #include <memory>
33 
34 namespace FGSwiftBus
35 {
36 
37     class CDBusObject;
38 
39     //! DBus connection
40     class CDBusConnection : public IDispatchable
41     {
42     public:
43         //! Bus type
44         enum BusType { SessionBus };
45 
46         //! Disconnect Callback
47         using DisconnectedCallback = std::function<void()>;
48 
49         //! Default constructor
50         CDBusConnection();
51 
52         //! Constructor
53         CDBusConnection(DBusConnection *connection);
54 
55         //! Destructor
56         ~CDBusConnection() override;
57 
58         // The ones below are not implemented yet.
59         // If you need them, make sure that connection reference count is correct
60         CDBusConnection(const CDBusConnection &) = delete;
61         CDBusConnection &operator=(const CDBusConnection &) = delete;
62 
63         //! Connect to bus
64         bool connect(BusType type);
65 
66         //! Set dispatcher
67         void setDispatcher(CDBusDispatcher *dispatcher);
68 
69         //! Request name to the bus
70         void requestName(const std::string &name);
71 
72         //! Is connected?
73         bool isConnected() const;
74 
75         //! Register a disconnected callback
76         void registerDisconnectedCallback(CDBusObject *obj, DisconnectedCallback func);
77 
78         //! Register a disconnected callback
79         void unregisterDisconnectedCallback(CDBusObject *obj);
80 
81         //! Register DBus object with interfaceName and objectPath.
82         //! \param object
83         //! \param interfaceName
84         //! \param objectPath
85         //! \param dbusObjectPathVTable Virtual table handling DBus messages
86         void registerObjectPath(CDBusObject *object, const std::string &interfaceName, const std::string &objectPath, const DBusObjectPathVTable &dbusObjectPathVTable);
87 
88         //! Send message to bus
89         void sendMessage(const CDBusMessage &message);
90 
91         //! Close connection
92         void close();
93 
94         //! Get the last error
lastError()95         CDBusError lastError() const { return m_lastError; }
96 
97     protected:
98         // cppcheck-suppress virtualCallInConstructor
99         virtual void dispatch() override final;
100 
101     private:
102         void setDispatchStatus(DBusConnection *connection, DBusDispatchStatus status);
103         static void setDispatchStatus(DBusConnection *connection, DBusDispatchStatus status, void *data);
104         static DBusHandlerResult filterDisconnectedFunction(DBusConnection *connection, DBusMessage *message, void *data);
105 
106         struct DBusConnectionDeleter
107         {
operatorDBusConnectionDeleter108             void operator()(DBusConnection *obj) const { dbus_connection_unref(obj); }
109         };
110 
111         CDBusDispatcher *m_dispatcher = nullptr;
112         std::unique_ptr<DBusConnection, DBusConnectionDeleter> m_connection;
113         CDBusError m_lastError;
114         std::unordered_map<CDBusObject *, DisconnectedCallback> m_disconnectedCallbacks;
115     };
116 
117 }
118 
119 #endif // guard
120