Debian Codegraph @

src/Network/Swift/dbusobject.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 "dbusconnection.h"
10 #include <deque>
11 #include <mutex>
12
13 namespace flightgear::swift {
14 //! DBus base object
15 class CDBusObject
16 {
17 public:
18 //! Constructor
19 CDBusObject() = default;
20
21 //! Destructor
22 virtual ~CDBusObject();
23
24 //! Set the assigned DBus connection.
25 //! \remark Currently one object can only manage one connection at a time
26 void setDBusConnection(const std::shared_ptr<CDBusConnection>& dbusConnection);
27
28 //! Register itself with interfaceName and objectPath
29 //! \warning Before calling this method, make sure that a valid DBus connection was set.
30 void registerDBusObjectPath(const std::string& interfaceName, const std::string& objectPath);
31
32 protected:
33 //! Handler which is called when DBusCconnection is established
34 virtual void dbusConnectedHandler() {}
35
36 //! DBus message handler
37 virtual DBusHandlerResult dbusMessageHandler(const CDBusMessage& message) = 0;
38
39 //! Handler which is called when DBusConnection disconnected
40 virtual void dbusDisconnectedHandler() {}
41
42 //! Send DBus signal
43 void sendDBusSignal(const std::string& name);
44
45 //! Send DBus message
46 void sendDBusMessage(const CDBusMessage& message);
47
48 //! Maybe sends an empty DBus reply (acknowledgement)
49 void maybeSendEmptyDBusReply(bool wantsReply, const std::string& destination, dbus_uint32_t serial);
50
51 //! Send DBus reply
52 template <typename T>
53 void sendDBusReply(const std::string& destination, dbus_uint32_t serial, const T& argument)
54 {
55 CDBusMessage reply = CDBusMessage::createReply(destination, serial);
56 reply.beginArgumentWrite();
57 reply.appendArgument(argument);
58 m_dbusConnection->sendMessage(reply);
59 }
60
61 //! Send DBus reply
62 template <typename T>
63 void sendDBusReply(const std::string& destination, dbus_uint32_t serial, const std::vector<T>& array)
64 {
65 CDBusMessage reply = CDBusMessage::createReply(destination, serial);
66 reply.beginArgumentWrite();
67 reply.appendArgument(array);
68 m_dbusConnection->sendMessage(reply);
69 }
70
71 //! Queue a DBus call to be executed in a different thread
72 void queueDBusCall(const std::function<void()>& func);
73
74 //! Invoke all pending DBus calls. They will be executed in the calling thread.
75 void invokeQueuedDBusCalls();
76
77 private:
78 static void dbusObjectPathUnregisterFunction(DBusConnection* connection, void* data);
79 static DBusHandlerResult dbusObjectPathMessageFunction(DBusConnection* connection, DBusMessage* message, void* data);
80
81 std::shared_ptr<CDBusConnection> m_dbusConnection;
82 std::string m_interfaceName;
83 std::string m_objectPath;
84
85 std::mutex m_mutex;
86 std::deque<std::function<void()>> m_queuedDBusCalls;
87
88 const DBusObjectPathVTable m_dbusObjectPathVTable = {dbusObjectPathUnregisterFunction, dbusObjectPathMessageFunction, nullptr, nullptr, nullptr, nullptr};
89 };
90
91 } // namespace flightgear::swift