diff --git a/common/gattlib_callback_connected_device.c b/common/gattlib_callback_connected_device.c index e564090..58e36b1 100644 --- a/common/gattlib_callback_connected_device.c +++ b/common/gattlib_callback_connected_device.c @@ -48,10 +48,12 @@ void gattlib_connected_device_python_callback(gattlib_adapter_t* adapter, const static gpointer _gattlib_connected_device_thread(gpointer data) { gattlib_connection_t* connection = data; + //puts("_gattlib_connected_device_thread A"); const gchar *device_mac_address = org_bluez_device1_get_address(connection->backend.device); // Mutex to ensure the handler is valid g_rec_mutex_lock(&m_gattlib_mutex); + //puts("_gattlib_connected_device_thread B"); if (!gattlib_connection_is_connected(connection)) { GATTLIB_LOG(GATTLIB_ERROR, "_gattlib_connected_device_thread: Device is not connected (state:%s)", @@ -66,18 +68,22 @@ static gpointer _gattlib_connected_device_thread(gpointer data) { return NULL; } + //puts("_gattlib_connected_device_thread C"); // Ensure we increment device reference counter to prevent the device/connection is freed during the execution gattlib_device_ref(connection->device); // We need to release the lock here to ensure the connection callback that is actually // doing the application sepcific work is not locking the BLE state. g_rec_mutex_unlock(&m_gattlib_mutex); + //puts("_gattlib_connected_device_thread D"); connection->on_connection.callback.connection_handler( connection->device->adapter, device_mac_address, connection, 0 /* no error */, connection->on_connection.user_data); + //puts("_gattlib_connected_device_thread E"); gattlib_device_unref(connection->device); + //puts("_gattlib_connected_device_thread DONE"); return NULL; } diff --git a/common/gattlib_callback_disconnected_device.c b/common/gattlib_callback_disconnected_device.c index dc59067..517b13b 100644 --- a/common/gattlib_callback_disconnected_device.c +++ b/common/gattlib_callback_disconnected_device.c @@ -31,7 +31,9 @@ void gattlib_disconnected_device_python_callback(gattlib_connection_t* connectio #endif void gattlib_on_disconnected_device(gattlib_connection_t* connection) { + //puts("--- gattlib_on_disconnected_device"); g_rec_mutex_lock(&m_gattlib_mutex); + //puts("--- gattlib_on_disconnected_device A"); if (!gattlib_connection_is_valid(connection)) { GATTLIB_LOG(GATTLIB_ERROR, "gattlib_on_disconnected_device: Device not valid"); @@ -62,4 +64,6 @@ void gattlib_on_disconnected_device(gattlib_connection_t* connection) { m_gattlib_signal.signals |= GATTLIB_SIGNAL_DEVICE_DISCONNECTION; g_cond_broadcast(&m_gattlib_signal.condition); g_mutex_unlock(&m_gattlib_signal.mutex); + + //puts("--- gattlib_on_disconnected_device DONE"); } diff --git a/common/gattlib_common.c b/common/gattlib_common.c index 20e1181..5fc1a59 100644 --- a/common/gattlib_common.c +++ b/common/gattlib_common.c @@ -266,6 +266,7 @@ void gattlib_free_mem(void *ptr) { int gattlib_device_ref(gattlib_device_t* device) { g_rec_mutex_lock(&m_gattlib_mutex); device->reference_counter++; + //printf("DEVICE INC REF_COUNTER:%d\n", device->reference_counter); g_rec_mutex_unlock(&m_gattlib_mutex); return GATTLIB_SUCCESS; } diff --git a/common/gattlib_common_adapter.c b/common/gattlib_common_adapter.c index d56ed5b..9fb19b8 100644 --- a/common/gattlib_common_adapter.c +++ b/common/gattlib_common_adapter.c @@ -110,6 +110,7 @@ struct _connection_is_valid { static gint _is_device_connection(gconstpointer a, gconstpointer b) { const gattlib_device_t* device = a; + //printf("- %s connection:%p / %p\n", device->device_id, &device->connection); return (&device->connection == b) ? 0 : -1; // We need to return 0 when it matches } @@ -152,12 +153,16 @@ static void _gattlib_connection_is_connected(gpointer data, gpointer user_data) gattlib_adapter_t* adapter = data; struct _connection_is_connected* connection_is_connected = user_data; + //printf("_gattlib_connection_is_connected: Check device in adapter:%s\n", adapter->id); + GSList *device_entry = g_slist_find_custom(adapter->devices, connection_is_connected->connection, _is_device_connection); if (device_entry == NULL) { + //printf("_gattlib_connection_is_connected: Did not find device %s\n", connection_is_connected->connection->device->device_id); return; } gattlib_device_t* device = device_entry->data; + //printf("_gattlib_connection_is_connected: device %s state is %s\n", device->device_id, device_state_str[device->state]); connection_is_connected->is_connected = (device->state == CONNECTED); } @@ -168,7 +173,9 @@ bool gattlib_connection_is_connected(gattlib_connection_t* connection) { }; g_rec_mutex_lock(&m_gattlib_mutex); + //printf("gattlib_connection_is_connected A"); g_slist_foreach(m_adapter_list, _gattlib_connection_is_connected, &connection_is_connected); + //printf("gattlib_connection_is_connected B"); g_rec_mutex_unlock(&m_gattlib_mutex); return connection_is_connected.is_connected; diff --git a/common/gattlib_device_state_management.c b/common/gattlib_device_state_management.c index 1eb6a79..73c5ee8 100644 --- a/common/gattlib_device_state_management.c +++ b/common/gattlib_device_state_management.c @@ -149,6 +149,7 @@ int gattlib_devices_free(gattlib_adapter_t* adapter) { int gattlib_device_unref(gattlib_device_t* device) { g_rec_mutex_lock(&m_gattlib_mutex); device->reference_counter--; + //printf("DEVICE DEC REF_COUNTER:%d\n", device->reference_counter); if (device->reference_counter > 0) { goto EXIT; } diff --git a/dbus/gattlib.c b/dbus/gattlib.c index a412d2d..08b2898 100644 --- a/dbus/gattlib.c +++ b/dbus/gattlib.c @@ -48,7 +48,9 @@ static void _on_device_connect(gattlib_connection_t* connection) { gattlib_device_set_state(connection->device->adapter, connection->device->device_id, CONNECTED); + //puts("Go gattlib_on_connected_device"); gattlib_on_connected_device(connection); + //puts("Go gattlib_on_connected_device DONE"); EXIT: g_rec_mutex_unlock(&m_gattlib_mutex); @@ -249,6 +251,7 @@ int gattlib_connect(gattlib_adapter_t* adapter, const char *dst, } // Register a handle for notification + //printf("DEVICE CONNECT property change 3\n"); device->connection.backend.on_handle_device_property_change_id = g_signal_connect(bluez_device, "g-properties-changed", G_CALLBACK(on_handle_device_property_change), @@ -316,6 +319,8 @@ int gattlib_connect(gattlib_adapter_t* adapter, const char *dst, void gattlib_connection_free(gattlib_connection_t* connection) { char* device_id; + //puts("gattlib_connection_free()"); + device_id = connection->device->device_id; // Remove signal @@ -350,6 +355,7 @@ void gattlib_connection_free(gattlib_connection_t* connection) { // Mark the device has disconnected gattlib_device_set_state(connection->device->adapter, device_id, DISCONNECTED); + //puts("gattlib_connection_free() DONE"); } int gattlib_disconnect(gattlib_connection_t* connection, bool wait_disconnection) { @@ -900,6 +906,8 @@ static void add_characteristics_from_service(struct _gattlib_connection_backend* continue; } + GATTLIB_LOG(GATTLIB_DEBUG, "- count %d with characteristic %s", *count, object_path); + // Sanity check to avoid buffer overflow if (*count >= count_max) { GATTLIB_LOG(GATTLIB_WARNING, "Skip GATT characteristic %s. Not enough space in the GATT characteristic array.", object_path); @@ -982,7 +990,9 @@ int gattlib_discover_char_range(gattlib_connection_t* connection, uint16_t start g_object_unref(interface); count_max++; + GATTLIB_LOG(GATTLIB_DEBUG, "- count_max %d with %s", count_max, object_path); } + GATTLIB_LOG(GATTLIB_DEBUG, "count_max:%d\n", count_max); gattlib_characteristic_t* characteristic_list = calloc(count_max * sizeof(gattlib_characteristic_t), 1); if (characteristic_list == NULL) { @@ -1017,6 +1027,7 @@ int gattlib_discover_char_range(gattlib_connection_t* connection, uint16_t start "00002a19-0000-1000-8000-00805f9b34fb", MAX_LEN_UUID_STR + 1, &characteristic_list[count].uuid); + GATTLIB_LOG(GATTLIB_DEBUG, "- count with (battery) %s", object_path); count++; } diff --git a/dbus/gattlib_adapter.c b/dbus/gattlib_adapter.c index d3d4b61..445b5c9 100644 --- a/dbus/gattlib_adapter.c +++ b/dbus/gattlib_adapter.c @@ -299,6 +299,11 @@ static void _wait_scan_loop_stop_scanning(gattlib_adapter_t* gattlib_adapter) { g_mutex_lock(&m_gattlib_signal.mutex); while (gattlib_adapter_is_scanning(gattlib_adapter)) { g_cond_wait(&m_gattlib_signal.condition, &m_gattlib_signal.mutex); + GATTLIB_LOG(GATTLIB_ERROR, "---- _wait_scan_loop_stop_scanning WAKE UP"); + +#ifdef DEBUG + gattlib_adapter_dump_state(gattlib_adapter); +#endif } g_mutex_unlock(&m_gattlib_signal.mutex); } @@ -640,10 +645,13 @@ int gattlib_adapter_scan_disable(gattlib_adapter_t* adapter) { gattlib_handler_free(&adapter->discovered_device_callback); // Stop BLE scan loop thread + GATTLIB_LOG(GATTLIB_DEBUG, "Is scanning:%d", adapter->backend.ble_scan.is_scanning); if (adapter->backend.ble_scan.is_scanning) { adapter->backend.ble_scan.is_scanning = false; + GATTLIB_LOG(GATTLIB_DEBUG, "Will signal end of scanning"); g_mutex_lock(&m_gattlib_signal.mutex); m_gattlib_signal.signals |= GATTLIB_SIGNAL_ADAPTER_STOP_SCANNING; + GATTLIB_LOG(GATTLIB_DEBUG, "Signalled end of scanning"); g_cond_broadcast(&m_gattlib_signal.condition); g_mutex_unlock(&m_gattlib_signal.mutex); } @@ -715,6 +723,7 @@ int gattlib_adapter_close(gattlib_adapter_t* adapter) { int gattlib_adapter_ref(gattlib_adapter_t* adapter) { g_rec_mutex_lock(&m_gattlib_mutex); adapter->reference_counter++; + //printf("ADAPTER INC REF_COUNTER:%d\n", adapter->reference_counter); g_rec_mutex_unlock(&m_gattlib_mutex); return GATTLIB_SUCCESS; } @@ -724,6 +733,7 @@ int gattlib_adapter_unref(gattlib_adapter_t* adapter) { g_rec_mutex_lock(&m_gattlib_mutex); adapter->reference_counter--; + //printf("ADAPTER DEC REF_COUNTER:%d\n", adapter->reference_counter); if (adapter->reference_counter > 0) { goto EXIT; diff --git a/dbus/gattlib_notification.c b/dbus/gattlib_notification.c index 51b8a1e..f282375 100644 --- a/dbus/gattlib_notification.c +++ b/dbus/gattlib_notification.c @@ -183,6 +183,7 @@ static int connect_signal_to_characteristic_uuid(gattlib_connection_t* connectio #if BLUEZ_VERSION > BLUEZ_VERSIONS(5, 40) else if (dbus_characteristic.type == TYPE_BATTERY_LEVEL) { // Register a handle for notification + //printf("DEVICE CONNECT property change 1\n"); g_signal_connect(dbus_characteristic.battery, "g-properties-changed", G_CALLBACK (on_handle_battery_level_property_change), @@ -196,6 +197,7 @@ static int connect_signal_to_characteristic_uuid(gattlib_connection_t* connectio #endif // Register a handle for notification + //printf("DEVICE CONNECT property change 2\n"); gulong signal_id = g_signal_connect(dbus_characteristic.gatt, "g-properties-changed", G_CALLBACK(callback),