From 49790a92bf20f3bf79e475cefcb83d816ef01497 Mon Sep 17 00:00:00 2001 From: Kevin Dewald Date: Tue, 2 Mar 2021 16:08:21 -0800 Subject: [PATCH] Fixed bug where notifications will be enabled but no events will be received. --- dbus/gattlib.c | 14 ++++++++++++++ dbus/gattlib_internal.h | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/dbus/gattlib.c b/dbus/gattlib.c index b30ecf9..04be73c 100644 --- a/dbus/gattlib.c +++ b/dbus/gattlib.c @@ -32,6 +32,12 @@ static const char *m_dbus_error_unknown_object = "GDBus.Error:org.freedesktop.DBus.Error.UnknownObject"; +static void* glib_event_thread(void* main_loop_p) { + GMainLoop** main_loop = (GMainLoop**) main_loop_p; + g_main_loop_run(*main_loop); + return NULL; +} + gboolean on_handle_device_property_change( OrgBluezGattCharacteristic1 *object, GVariant *arg_changed_properties, @@ -212,6 +218,11 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long device_manager = get_device_manager_from_adapter(conn_context->adapter); conn_context->dbus_objects = g_dbus_object_manager_get_objects(device_manager); + // Set up a new GMainLoop to handle notification/indication events. + conn_context->connection_loop = g_main_loop_new(NULL, 0); + fprintf(stderr, "Creating external thread\n"); + pthread_create(&conn_context->event_thread, NULL, glib_event_thread, &conn_context->connection_loop); + return connection; FREE_DEVICE: @@ -253,6 +264,9 @@ int gattlib_disconnect(gatt_connection_t* connection) { free(conn_context->device_object_path); g_object_unref(conn_context->device); g_list_free_full(conn_context->dbus_objects, g_object_unref); + g_main_loop_quit(conn_context->connection_loop); + pthread_join(conn_context->event_thread, NULL); + g_main_loop_unref(conn_context->connection_loop); disconnect_all_notifications(conn_context); free(connection->context); diff --git a/dbus/gattlib_internal.h b/dbus/gattlib_internal.h index 55c8bd9..9d80402 100644 --- a/dbus/gattlib_internal.h +++ b/dbus/gattlib_internal.h @@ -25,6 +25,7 @@ #define __GATTLIB_INTERNAL_H__ #include +#include #include "gattlib_internal_defs.h" #include "gattlib.h" @@ -52,8 +53,8 @@ typedef struct { char* device_object_path; OrgBluezDevice1* device; - // This attribute is only used during the connection stage. By placing the attribute here, we can pass - // `gatt_connection_t` to + // These attributes are needed to handle incoming events from GLib + pthread_t event_thread; GMainLoop *connection_loop; // ID of the timeout to know if we managed to connect to the device guint connection_timeout;