Debian Codegraph @

src/Network/Swift/dbusconnection.h

raw symbols SCIP
1 /*
2 * SPDX-FileCopyrightText: (C) 2019-2022 swift Project Community / Contributors (https://swift-project.org/)
3 * SPDX-FileCopyrightText: (C) 2019-2022 Lars Toenning <[email protected]>
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #pragma once
8
9 #include "dbuscallbacks.h"
10 #include "dbusdispatcher.h"
11 #include "dbuserror.h"
12 #include "dbusmessage.h"
13
14 #include <dbus/dbus.h>
15 #include <event2/event.h>
16 #include <memory>
17 #include <string>
18 #include <unordered_map>
19
20 namespace flightgear::swift {
21
22 class CDBusObject;
23
24 //! DBus connection
25 class CDBusConnection : public IDispatchable
26 {
27 public:
28 //! Bus type
29 enum BusType { SessionBus };
30
31 //! Disconnect Callback
32 using DisconnectedCallback = std::function<void()>;
33
34 //! Default constructor
35 CDBusConnection();
36
37 //! Constructor
38 explicit CDBusConnection(DBusConnection* connection);
39
40 //! Destructor
41 ~CDBusConnection() override;
42
43 // The ones below are not implemented yet.
44 // If you need them, make sure that connection reference count is correct
45 CDBusConnection(const CDBusConnection&) = delete;
46 CDBusConnection& operator=(const CDBusConnection&) = delete;
47
48 //! Connect to bus
49 bool connect(BusType type);
50
51 //! Set dispatcher
52 void setDispatcher(CDBusDispatcher* dispatcher);
53
54 //! Request name to the bus
55 void requestName(const std::string& name);
56
57 //! Is connected?
58 bool isConnected() const;
59
60 //! Register a disconnected callback
61 void registerDisconnectedCallback(CDBusObject* obj, DisconnectedCallback func);
62
63 //! Register a disconnected callback
64 void unregisterDisconnectedCallback(CDBusObject* obj);
65
66 //! Register DBus object with interfaceName and objectPath.
67 //! \param object
68 //! \param interfaceName
69 //! \param objectPath
70 //! \param dbusObjectPathVTable Virtual table handling DBus messages
71 void registerObjectPath(CDBusObject* object, const std::string& interfaceName, const std::string& objectPath, const DBusObjectPathVTable& dbusObjectPathVTable);
72
73 //! Send message to bus
74 void sendMessage(const CDBusMessage& message);
75
76 //! Close connection
77 void close();
78
79 //! Get the last error
80 CDBusError lastError() const { return m_lastError; }
81
82 protected:
83 // cppcheck-suppress virtualCallInConstructor
84 void dispatch() override;
85
86 private:
87 void setDispatchStatus(DBusConnection* connection, DBusDispatchStatus status);
88 static void setDispatchStatus(DBusConnection* connection, DBusDispatchStatus status, void* data);
89 static DBusHandlerResult filterDisconnectedFunction(DBusConnection* connection, DBusMessage* message, void* data);
90
91 struct DBusConnectionDeleter {
92 void operator()(DBusConnection* obj) const { dbus_connection_unref(obj); }
93 };
94
95 CDBusDispatcher* m_dispatcher = nullptr;
96 std::unique_ptr<DBusConnection, DBusConnectionDeleter> m_connection;
97 CDBusError m_lastError;
98 std::unordered_map<CDBusObject*, DisconnectedCallback> m_disconnectedCallbacks;
99 };
100
101 } // namespace flightgear::swift