|
1 | 1 | /*
|
2 | 2 | * SPDX-License-Identifier: BSD-3-Clause
|
3 | 3 | *
|
4 |
| - * Copyright (c) 2016-2022, Olivier Martin <[email protected]> |
| 4 | + * Copyright (c) 2016-2024, Olivier Martin <[email protected]> |
5 | 5 | */
|
6 | 6 |
|
7 | 7 | #include "gattlib_internal.h"
|
@@ -124,13 +124,14 @@ static void device_manager_on_device1_signal(const char* device1_path, struct ga
|
124 | 124 | }
|
125 | 125 |
|
126 | 126 | // Check if the device is already part of the list
|
| 127 | + g_mutex_lock(&gattlib_adapter->ble_scan.discovered_devices_mutex); |
127 | 128 | GSList *item = g_slist_find_custom(gattlib_adapter->ble_scan.discovered_devices, address, (GCompareFunc)g_ascii_strcasecmp);
|
128 |
| - |
129 | 129 | // First time this device is in the list
|
130 | 130 | if (item == NULL) {
|
131 | 131 | // Add the device to the list
|
132 | 132 | gattlib_adapter->ble_scan.discovered_devices = g_slist_append(gattlib_adapter->ble_scan.discovered_devices, g_strdup(address));
|
133 | 133 | }
|
| 134 | + g_mutex_unlock(&gattlib_adapter->ble_scan.discovered_devices_mutex); |
134 | 135 |
|
135 | 136 | if ((item == NULL) || (gattlib_adapter->ble_scan.enabled_filters & GATTLIB_DISCOVER_FILTER_NOTIFY_CHANGE)) {
|
136 | 137 | #if defined(WITH_PYTHON)
|
@@ -219,9 +220,6 @@ static void* _ble_scan_loop(void* args) {
|
219 | 220 | // Ensure BLE device discovery is stopped
|
220 | 221 | gattlib_adapter_scan_disable(gattlib_adapter);
|
221 | 222 |
|
222 |
| - // Free discovered device list |
223 |
| - g_slist_foreach(gattlib_adapter->ble_scan.discovered_devices, (GFunc)g_free, NULL); |
224 |
| - g_slist_free(gattlib_adapter->ble_scan.discovered_devices); |
225 | 223 | return 0;
|
226 | 224 | }
|
227 | 225 |
|
@@ -354,27 +352,31 @@ int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t disco
|
354 | 352 |
|
355 | 353 | int gattlib_adapter_scan_disable(void* adapter) {
|
356 | 354 | struct gattlib_adapter *gattlib_adapter = adapter;
|
| 355 | + GError *error = NULL; |
357 | 356 |
|
358 |
| - if (gattlib_adapter->ble_scan.scan_loop) { |
359 |
| - GError *error = NULL; |
| 357 | + if (gattlib_adapter->ble_scan.is_scanning) { |
| 358 | + g_mutex_lock(&gattlib_adapter->ble_scan.scan_loop_mutex); |
| 359 | + gattlib_adapter->ble_scan.is_scanning = false; |
| 360 | + g_cond_broadcast(&gattlib_adapter->ble_scan.scan_loop_cond); |
| 361 | + g_mutex_unlock(&gattlib_adapter->ble_scan.scan_loop_mutex); |
| 362 | + } |
360 | 363 |
|
361 |
| - org_bluez_adapter1_call_stop_discovery_sync(gattlib_adapter->adapter_proxy, NULL, &error); |
362 |
| - // Ignore the error |
| 364 | + org_bluez_adapter1_call_stop_discovery_sync(gattlib_adapter->adapter_proxy, NULL, &error); |
| 365 | + // Ignore the error |
363 | 366 |
|
364 |
| - // Remove timeout |
365 |
| - if (gattlib_adapter->ble_scan.ble_scan_timeout_id) { |
366 |
| - g_source_remove(gattlib_adapter->ble_scan.ble_scan_timeout_id); |
367 |
| - gattlib_adapter->ble_scan.ble_scan_timeout_id = 0; |
368 |
| - } |
369 |
| - |
370 |
| - // Ensure the scan loop is quit |
371 |
| - if (g_main_loop_is_running(gattlib_adapter->ble_scan.scan_loop)) { |
372 |
| - g_main_loop_quit(gattlib_adapter->ble_scan.scan_loop); |
373 |
| - } |
374 |
| - g_main_loop_unref(gattlib_adapter->ble_scan.scan_loop); |
375 |
| - gattlib_adapter->ble_scan.scan_loop = NULL; |
| 367 | + // Remove timeout |
| 368 | + if (gattlib_adapter->ble_scan.ble_scan_timeout_id) { |
| 369 | + g_source_remove(gattlib_adapter->ble_scan.ble_scan_timeout_id); |
| 370 | + gattlib_adapter->ble_scan.ble_scan_timeout_id = 0; |
376 | 371 | }
|
377 | 372 |
|
| 373 | + // Free discovered device list |
| 374 | + g_mutex_lock(&gattlib_adapter->ble_scan.discovered_devices_mutex); |
| 375 | + g_slist_foreach(gattlib_adapter->ble_scan.discovered_devices, (GFunc)g_free, NULL); |
| 376 | + g_slist_free(gattlib_adapter->ble_scan.discovered_devices); |
| 377 | + gattlib_adapter->ble_scan.discovered_devices = NULL; |
| 378 | + g_mutex_unlock(&gattlib_adapter->ble_scan.discovered_devices_mutex); |
| 379 | + |
378 | 380 | return GATTLIB_SUCCESS;
|
379 | 381 | }
|
380 | 382 |
|
|
0 commit comments