FlightGear next
dbusconnection.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: (C) 2019-2022 swift Project Community / Contributors (https://swift-project.org/)
3 * SPDX-FileCopyrightText: (C) 2019-2022 Lars Toenning <dev@ltoenning.de>
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
20namespace flightgear::swift {
21
22class CDBusObject;
23
26{
27public:
30
32 using DisconnectedCallback = std::function<void()>;
33
36
38 explicit CDBusConnection(DBusConnection* connection);
39
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
47
49 bool connect(BusType type);
50
52 void setDispatcher(CDBusDispatcher* dispatcher);
53
55 void requestName(const std::string& name);
56
58 bool isConnected() const;
59
62
65
71 void registerObjectPath(CDBusObject* object, const std::string& interfaceName, const std::string& objectPath, const DBusObjectPathVTable& dbusObjectPathVTable);
72
74 void sendMessage(const CDBusMessage& message);
75
77 void close();
78
80 CDBusError lastError() const { return m_lastError; }
81
82protected:
83 // cppcheck-suppress virtualCallInConstructor
84 void dispatch() override;
85
86private:
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
void unregisterDisconnectedCallback(CDBusObject *obj)
Register a disconnected callback.
void requestName(const std::string &name)
Request name to the bus.
void registerObjectPath(CDBusObject *object, const std::string &interfaceName, const std::string &objectPath, const DBusObjectPathVTable &dbusObjectPathVTable)
Register DBus object with interfaceName and objectPath.
void setDispatcher(CDBusDispatcher *dispatcher)
Set dispatcher.
std::function< void()> DisconnectedCallback
Disconnect Callback.
bool connect(BusType type)
Connect to bus.
CDBusConnection()
Default constructor.
CDBusConnection(const CDBusConnection &)=delete
void registerDisconnectedCallback(CDBusObject *obj, DisconnectedCallback func)
Register a disconnected callback.
void sendMessage(const CDBusMessage &message)
Send message to bus.
bool isConnected() const
Is connected?
CDBusConnection & operator=(const CDBusConnection &)=delete
CDBusError lastError() const
Get the last error.
void dispatch() override
Dispatch execution method.
~CDBusConnection() override
Destructor.
IDispatchable()=default
Default constructor.
static int status
const char * name
naCFunction func