4 #include "dbusconnection.h"
11 #include "dbusobject.h"
20 m_connection.reset(connection);
21 dbus_connection_ref(connection);
23 dbus_connection_set_exit_on_disconnect(connection,
false);
24 dbus_connection_add_filter(connection, filterDisconnectedFunction,
this,
nullptr);
31 if (m_dispatcher) { m_dispatcher->
remove(
this); }
36 assert(type == SessionBus);
38 dbus_error_init(&error);
40 DBusBusType dbusBusType;
43 case SessionBus: dbusBusType = DBUS_BUS_SESSION;
break;
46 m_connection.reset(dbus_bus_get_private(dbusBusType, &error));
47 if (dbus_error_is_set(&error))
54 dbus_connection_set_exit_on_disconnect(m_connection.get(),
false);
62 m_dispatcher = dispatcher;
64 m_dispatcher->
add(
this);
66 dbus_connection_set_watch_functions(m_connection.get(), dispatcher->m_watchCallbacks.add,
67 dispatcher->m_watchCallbacks.remove, dispatcher->m_watchCallbacks.toggled,
68 &dispatcher->m_watchCallbacks,
nullptr);
70 dbus_connection_set_timeout_functions(
71 m_connection.get(), dispatcher->m_timeoutCallbacks.add, dispatcher->m_timeoutCallbacks.remove,
72 dispatcher->m_timeoutCallbacks.toggled, &dispatcher->m_timeoutCallbacks,
nullptr);
78 dbus_error_init(&error);
79 dbus_bus_request_name(m_connection.get(), name.c_str(), 0, &error);
84 return m_connection && dbus_connection_get_is_connected(m_connection.get());
89 m_disconnectedCallbacks[obj] = func;
94 auto it = m_disconnectedCallbacks.find(obj);
95 if (it == m_disconnectedCallbacks.end()) {
return; }
96 m_disconnectedCallbacks.erase(it);
100 const std::string &objectPath,
101 const DBusObjectPathVTable &dbusObjectPathVTable)
104 if (!m_connection) {
return; }
106 dbus_connection_try_register_object_path(m_connection.get(), objectPath.c_str(), &dbusObjectPathVTable,
object,
113 dbus_uint32_t serial = message.
getSerial();
114 dbus_connection_send(m_connection.get(), message.m_message, &serial);
119 if (m_connection) { dbus_connection_close(m_connection.get()); }
124 dbus_connection_ref(m_connection.get());
125 if (dbus_connection_get_dispatch_status(m_connection.get()) == DBUS_DISPATCH_DATA_REMAINS)
127 while (dbus_connection_dispatch(m_connection.get()) == DBUS_DISPATCH_DATA_REMAINS) {}
129 dbus_connection_unref(m_connection.get());
132 void CDBusConnection::setDispatchStatus(DBusConnection *connection, DBusDispatchStatus status)
134 if (dbus_connection_get_is_connected(connection) == FALSE) {
return; }
138 case DBUS_DISPATCH_DATA_REMAINS:
141 case DBUS_DISPATCH_COMPLETE:
142 case DBUS_DISPATCH_NEED_MEMORY:
break;
146 void CDBusConnection::setDispatchStatus(DBusConnection *connection, DBusDispatchStatus status,
void *data)
149 obj->setDispatchStatus(connection, status);
152 DBusHandlerResult CDBusConnection::filterDisconnectedFunction(DBusConnection *connection, DBusMessage *message,
160 dbus_error_init(&err);
162 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL,
"Disconnected"))
164 for (
auto it = obj->m_disconnectedCallbacks.begin(); it != obj->m_disconnectedCallbacks.end(); ++it)
168 return DBUS_HANDLER_RESULT_HANDLED;
170 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
virtual void dispatch() final
Dispatch execution method.
~CDBusConnection()
Destructor.
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.
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.
void add(IDispatchable *dispatchable)
Add dispatchable object.
void remove(IDispatchable *dispatchable)
Remove dispatchable object.
dbus_uint32_t getSerial() const
Get the message serial. This is usally required for reply message.
Plugin loaded by X-Plane which publishes a DBus service.