swift
dbusconnection.h
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #ifndef SWIFT_SIM_XSWIFTBUS_DBUSCONNECTION_H
5 #define SWIFT_SIM_XSWIFTBUS_DBUSCONNECTION_H
6 
7 #ifndef NOMINMAX
8 // windows.h min/max might be imported via dbus.h
9 # define NOMINMAX
10 #endif
11 
12 #include <dbus/dbus.h>
13 #include <event2/event.h>
14 
15 #include <memory>
16 #include <string>
17 #include <unordered_map>
18 
19 #include "dbuscallbacks.h"
20 #include "dbusdispatcher.h"
21 #include "dbuserror.h"
22 #include "dbusmessage.h"
23 
24 namespace XSwiftBus
25 {
26 
27  class CDBusObject;
28 
31  {
32  public:
34  enum BusType
35  {
36  SessionBus
37  };
38 
40  using DisconnectedCallback = std::function<void()>;
41 
44 
46  CDBusConnection(DBusConnection *connection);
47 
49  ~CDBusConnection() override;
50 
51  // The ones below are not implemented yet.
52  // If you need them, make sure that connection reference count is correct
53  CDBusConnection(const CDBusConnection &) = delete;
54  CDBusConnection &operator=(const CDBusConnection &) = delete;
55 
57  bool connect(BusType type);
58 
60  void setDispatcher(CDBusDispatcher *dispatcher);
61 
63  void requestName(const std::string &name);
64 
66  bool isConnected() const;
67 
70 
73 
79  void registerObjectPath(CDBusObject *object, const std::string &interfaceName, const std::string &objectPath,
80  const DBusObjectPathVTable &dbusObjectPathVTable);
81 
83  void sendMessage(const CDBusMessage &message);
84 
86  void close();
87 
89  CDBusError lastError() const { return m_lastError; }
90 
91  protected:
92  // cppcheck-suppress virtualCallInConstructor
93  virtual void dispatch() override final;
94 
95  private:
96  void setDispatchStatus(DBusConnection *connection, DBusDispatchStatus status);
97  static void setDispatchStatus(DBusConnection *connection, DBusDispatchStatus status, void *data);
98  static DBusHandlerResult filterDisconnectedFunction(DBusConnection *connection, DBusMessage *message,
99  void *data);
100 
101  struct DBusConnectionDeleter
102  {
103  void operator()(DBusConnection *obj) const { dbus_connection_unref(obj); }
104  };
105 
106  CDBusDispatcher *m_dispatcher = nullptr;
107  std::unique_ptr<DBusConnection, DBusConnectionDeleter> m_connection;
108  CDBusError m_lastError;
109  std::unordered_map<CDBusObject *, DisconnectedCallback> m_disconnectedCallbacks;
110  };
111 
112 } // namespace XSwiftBus
113 
114 #endif // SWIFT_SIM_XSWIFTBUS_DBUSCONNECTION_H
virtual void dispatch() final
Dispatch execution method.
void registerDisconnectedCallback(CDBusObject *obj, DisconnectedCallback func)
Register a disconnected callback.
std::function< void()> DisconnectedCallback
Disconnect Callback.
void requestName(const std::string &name)
Request name to the bus.
void sendMessage(const CDBusMessage &message)
Send message to bus.
void setDispatcher(CDBusDispatcher *dispatcher)
Set dispatcher.
CDBusConnection()
Default constructor.
CDBusError lastError() const
Get the last error.
void close()
Close connection.
bool isConnected() const
Is connected?
void unregisterDisconnectedCallback(CDBusObject *obj)
Register a disconnected callback.
void registerObjectPath(CDBusObject *object, const std::string &interfaceName, const std::string &objectPath, const DBusObjectPathVTable &dbusObjectPathVTable)
Register DBus object with interfaceName and objectPath.
bool connect(BusType type)
Connect to bus.
DBus base object.
Definition: dbusobject.h:20
Dispatchable Interface.
Plugin loaded by X-Plane which publishes a DBus service.
Definition: command.h:14