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 #include <dbus/dbus.h>
8 #include <event2/event.h>
9 
10 #include <memory>
11 #include <string>
12 #include <unordered_map>
13 
14 #include "dbuscallbacks.h"
15 #include "dbusdispatcher.h"
16 #include "dbuserror.h"
17 #include "dbusmessage.h"
18 
19 namespace XSwiftBus
20 {
21 
22  class CDBusObject;
23 
26  {
27  public:
29  enum BusType
30  {
31  SessionBus
32  };
33 
35  using DisconnectedCallback = std::function<void()>;
36 
39 
41  CDBusConnection(DBusConnection *connection);
42 
44  ~CDBusConnection() override;
45 
46  // The ones below are not implemented yet.
47  // If you need them, make sure that connection reference count is correct
48  CDBusConnection(const CDBusConnection &) = delete;
49  CDBusConnection &operator=(const CDBusConnection &) = delete;
50 
52  bool connect(BusType type);
53 
55  void setDispatcher(CDBusDispatcher *dispatcher);
56 
58  void requestName(const std::string &name);
59 
61  bool isConnected() const;
62 
65 
68 
74  void registerObjectPath(CDBusObject *object, const std::string &interfaceName, const std::string &objectPath,
75  const DBusObjectPathVTable &dbusObjectPathVTable);
76 
78  void sendMessage(const CDBusMessage &message);
79 
81  void close();
82 
84  CDBusError lastError() const { return m_lastError; }
85 
86  protected:
87  // cppcheck-suppress virtualCallInConstructor
88  virtual void dispatch() override final;
89 
90  private:
91  void setDispatchStatus(DBusConnection *connection, DBusDispatchStatus status);
92  static void setDispatchStatus(DBusConnection *connection, DBusDispatchStatus status, void *data);
93  static DBusHandlerResult filterDisconnectedFunction(DBusConnection *connection, DBusMessage *message,
94  void *data);
95 
96  struct DBusConnectionDeleter
97  {
98  void operator()(DBusConnection *obj) const { dbus_connection_unref(obj); }
99  };
100 
101  CDBusDispatcher *m_dispatcher = nullptr;
102  std::unique_ptr<DBusConnection, DBusConnectionDeleter> m_connection;
103  CDBusError m_lastError;
104  std::unordered_map<CDBusObject *, DisconnectedCallback> m_disconnectedCallbacks;
105  };
106 
107 } // namespace XSwiftBus
108 
109 #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