Skip to content

Commit

Permalink
Fix gattlib connection release on disconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviermartin committed Mar 13, 2024
1 parent 785f4d1 commit be1b8e1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
3 changes: 3 additions & 0 deletions common/gattlib_callback_disconnected_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ void gattlib_on_disconnected_device(gatt_connection_t* connection) {

// For GATT disconnection we do not use thread to ensure the callback is synchronous.
connection->on_disconnection.callback.disconnection_handler(connection, connection->on_disconnection.user_data);

// Clean GATTLIB connection on disconnection
gattlib_connection_free(connection);
}
8 changes: 8 additions & 0 deletions common/gattlib_internal_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ bool gattlib_has_valid_handler(struct gattlib_handler* handler);

void gattlib_notification_device_thread(gpointer data, gpointer user_data);

/**
* Clean GATTLIB connection on disconnection
*
* This function is called by the disconnection callback to always be called on explicit
* and implicit disconnection.
*/
void gattlib_connection_free(gatt_connection_t* connection);

#if defined(WITH_PYTHON)
// Callback used by Python to create arguments used by native callback
void* gattlib_python_callback_args(PyObject* python_callback, PyObject* python_args);
Expand Down
63 changes: 40 additions & 23 deletions dbus/gattlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,44 @@ int gattlib_connect(void *adapter, const char *dst,
return ret;
}

/**
* Clean GATTLIB connection on disconnection
*
* This function is called by the disconnection callback to always be called on explicit
* and implicit disconnection.
*/
void gattlib_connection_free(gatt_connection_t* connection) {
gattlib_context_t* conn_context;

g_mutex_lock(&connection->connection_mutex);
conn_context = connection->context;

// Remove signal
if (conn_context->on_handle_device_property_change_id != 0) {
g_signal_handler_disconnect(conn_context->device, conn_context->on_handle_device_property_change_id);
conn_context->on_handle_device_property_change_id = 0;
}

free(conn_context->device_object_path);
if (conn_context->device != NULL) {
g_object_unref(conn_context->device);
conn_context->device = NULL;
}
g_list_free_full(conn_context->dbus_objects, g_object_unref);

disconnect_all_notifications(conn_context);

// Note: We do not free adapter as it might still be used by other devices

free(connection->context);
connection->context = NULL;

g_mutex_unlock(&connection->connection_mutex);

// And finally free the connection
free(connection);
}

int gattlib_disconnect(gatt_connection_t* connection) {
gattlib_context_t* conn_context;
int ret = GATTLIB_SUCCESS;
Expand All @@ -296,35 +334,14 @@ int gattlib_disconnect(gatt_connection_t* connection) {

GATTLIB_LOG(GATTLIB_DEBUG, "Disconnect bluetooth device %s", conn_context->device_object_path);

// Remove signal
if (conn_context->on_handle_device_property_change_id != 0) {
g_signal_handler_disconnect(conn_context->device, conn_context->on_handle_device_property_change_id);
conn_context->on_handle_device_property_change_id = 0;
}

org_bluez_device1_call_disconnect_sync(conn_context->device, NULL, &error);
if (error) {
GATTLIB_LOG(GATTLIB_ERROR, "Failed to disconnect DBus Bluez Device: %s", error->message);
g_error_free(error);
}

// Call disconnection callack. It should be called by signal but signal has been removed above
gattlib_on_disconnected_device(connection);

free(conn_context->device_object_path);
if (conn_context->device != NULL) {
g_object_unref(conn_context->device);
conn_context->device = NULL;
}
g_list_free_full(conn_context->dbus_objects, g_object_unref);

disconnect_all_notifications(conn_context);

// Note: We do not free adapter as it might still be used by other devices

free(connection->context);
connection->context = NULL;
free(connection);
//Note: Signals and memory will be removed/clean on disconnction callback
// See _gattlib_clean_on_disconnection()

EXIT:
g_mutex_unlock(&connection->connection_mutex);
Expand Down

0 comments on commit be1b8e1

Please sign in to comment.