16 dbus_threads_init_default();
21 m_connection.reset(connection);
22 dbus_connection_ref(connection);
24 dbus_connection_set_exit_on_disconnect(connection,
false);
25 dbus_connection_add_filter(connection, filterDisconnectedFunction,
this,
nullptr);
32 if (m_dispatcher) { m_dispatcher->remove(
this); }
39 dbus_error_init(&error);
41 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)) {
53 dbus_connection_set_exit_on_disconnect(m_connection.get(),
false);
61 m_dispatcher = dispatcher;
63 m_dispatcher->
add(
this);
65 dbus_connection_set_watch_functions(
67 dispatcher->m_watchCallbacks.
add,
68 dispatcher->m_watchCallbacks.
remove,
69 dispatcher->m_watchCallbacks.
toggled,
70 &dispatcher->m_watchCallbacks,
nullptr);
72 dbus_connection_set_timeout_functions(
74 dispatcher->m_timeoutCallbacks.
add,
75 dispatcher->m_timeoutCallbacks.
remove,
76 dispatcher->m_timeoutCallbacks.
toggled,
77 &dispatcher->m_timeoutCallbacks,
nullptr);
83 dbus_error_init(&error);
84 dbus_bus_request_name(m_connection.get(),
name.c_str(), 0, &error);
89 return m_connection && dbus_connection_get_is_connected(m_connection.get());
94 m_disconnectedCallbacks[obj] =
func;
99 auto it = m_disconnectedCallbacks.find(obj);
100 if (it == m_disconnectedCallbacks.end()) {
return; }
101 m_disconnectedCallbacks.erase(it);
107 if (!m_connection) {
return; }
109 dbus_connection_try_register_object_path(m_connection.get(), objectPath.c_str(), &dbusObjectPathVTable,
object,
nullptr);
115 dbus_uint32_t serial = message.
getSerial();
116 dbus_connection_send(m_connection.get(), message.m_message, &serial);
121 if (m_connection) { dbus_connection_close(m_connection.get()); }
126 dbus_connection_ref(m_connection.get());
127 if (dbus_connection_get_dispatch_status(m_connection.get()) == DBUS_DISPATCH_DATA_REMAINS) {
128 while (dbus_connection_dispatch(m_connection.get()) == DBUS_DISPATCH_DATA_REMAINS)
131 dbus_connection_unref(m_connection.get());
134void CDBusConnection::setDispatchStatus(DBusConnection* connection, DBusDispatchStatus
status)
136 if (dbus_connection_get_is_connected(connection) == FALSE) {
return; }
139 case DBUS_DISPATCH_DATA_REMAINS:
142 case DBUS_DISPATCH_COMPLETE:
143 case DBUS_DISPATCH_NEED_MEMORY:
148void CDBusConnection::setDispatchStatus(DBusConnection* connection, DBusDispatchStatus
status,
void* data)
151 obj->setDispatchStatus(connection,
status);
154DBusHandlerResult CDBusConnection::filterDisconnectedFunction(DBusConnection* connection, DBusMessage* message,
void* data)
161 dbus_error_init(&err);
163 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL,
"Disconnected")) {
164 for (
auto it = obj->m_disconnectedCallbacks.begin(); it != obj->m_disconnectedCallbacks.end(); ++it) {
167 return DBUS_HANDLER_RESULT_HANDLED;
169 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
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.
void close()
Close connection.
bool connect(BusType type)
Connect to bus.
CDBusConnection()
Default constructor.
void registerDisconnectedCallback(CDBusObject *obj, DisconnectedCallback func)
Register a disconnected callback.
void sendMessage(const CDBusMessage &message)
Send message to bus.
bool isConnected() const
Is connected?
void dispatch() override
Dispatch execution method.
~CDBusConnection() override
Destructor.
void add(IDispatchable *dispatchable)
Add dispatchable object.
dbus_uint32_t getSerial() const
Get the message serial. This is usally required for reply message.
static dbus_bool_t add(T *watch, void *refcon)
static void toggled(T *watch, void *refcon)
static void remove(T *watch, void *refcon)