-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathgatt_client_discovery.c
More file actions
599 lines (493 loc) · 22.3 KB
/
gatt_client_discovery.c
File metadata and controls
599 lines (493 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/* SPDX-FileCopyrightText: 2025 Google LLC */
/* SPDX-License-Identifier: Apache-2.0 */
#include <bluetooth/gatt.h>
#include <host/ble_hs.h>
#include <system/logging.h>
#include <util/math.h>
#include <services/gatt/ble_svc_gatt.h>
#include <FreeRTOS.h>
#include <semphr.h>
#include "nimble_type_conversions.h"
static bool s_discovery_in_progress;
static bool s_stop_discovery_requested;
static SemaphoreHandle_t s_discovery_stopped;
// -------------------------------------------------------------------------------------------------
// Gatt Client Discovery API calls
typedef struct {
ListNode node;
struct ble_gatt_dsc descriptor;
} GATTServiceDiscoveryDescriptorNode;
typedef struct {
ListNode node;
struct ble_gatt_chr characteristic;
ListNode *descriptors;
} GATTServiceDiscoveryCharacteristicNode;
typedef struct {
ListNode node;
struct ble_gatt_svc service;
ListNode *characteristics;
uint16_t num_descriptors;
} GATTServiceDiscoveryServiceNode;
typedef struct {
GAPLEConnection *connection;
ATTHandleRange range;
ListNode *services;
ListNode *current_service;
ListNode *current_characteristic;
} GATTServiceDiscoveryContext;
// Sets s_discovery_in_progress to false and gives the stop semaphore if a stop has been
// requested. Called from every callback termination path so that bt_driver_gatt_stop_discovery
// is always unblocked, even when discovery completes (naturally or via error) after the stop
// request flag was set but before the next stop-check at the top of a callback runs.
static void prv_signal_discovery_done(void) {
s_discovery_in_progress = false;
if (s_stop_discovery_requested) {
xSemaphoreGive(s_discovery_stopped);
}
}
static bool prv_descriptor_free_cb(ListNode *node, void *context) {
kernel_free(node);
return true;
}
static bool prv_characteristic_free_cb(ListNode *node, void *context) {
GATTServiceDiscoveryCharacteristicNode *chr_node = (GATTServiceDiscoveryCharacteristicNode *)node;
list_foreach(chr_node->descriptors, prv_descriptor_free_cb, NULL);
kernel_free(node);
return true;
}
static bool prv_service_free_cb(ListNode *node, void *context) {
GATTServiceDiscoveryServiceNode *service_node = (GATTServiceDiscoveryServiceNode *)node;
list_foreach(service_node->characteristics, prv_characteristic_free_cb, NULL);
kernel_free(node);
return true;
}
static void prv_free_discovery_context(GATTServiceDiscoveryContext *context) {
list_foreach(context->services, prv_service_free_cb, NULL);
kernel_free(context);
}
/* TODO: the way this works is kinda inefficient, really we should notify the OS after we
discover all the characteristics/descriptors for a service, letting us free the allocations
related to that service.
*/
static bool prv_convert_service_and_notify_os_cb(ListNode *node, void *context) {
GATTServiceDiscoveryServiceNode *service_node = (GATTServiceDiscoveryServiceNode *)node;
GAPLEConnection *connection = context;
uint16_t num_characteristics = list_count(service_node->characteristics);
size_t size_bytes =
COMPUTE_GATTSERVICE_SIZE_BYTES(num_characteristics, service_node->num_descriptors, 0);
GATTService *gatt_service = kernel_zalloc_check(size_bytes);
gatt_service->size_bytes = size_bytes;
gatt_service->att_handle = service_node->service.start_handle;
gatt_service->num_characteristics = num_characteristics;
gatt_service->num_descriptors = service_node->num_descriptors;
gatt_service->num_att_handles_included_services = 0;
nimble_uuid_to_pebble(&service_node->service.uuid, &gatt_service->uuid);
GATTServiceDiscoveryCharacteristicNode *chr_node =
(GATTServiceDiscoveryCharacteristicNode *)service_node->characteristics;
uint8_t *end_ptr = (uint8_t *)gatt_service->characteristics;
while (chr_node != NULL) {
GATTCharacteristic *gatt_characteristic = (GATTCharacteristic *)end_ptr;
*gatt_characteristic = (GATTCharacteristic){
.att_handle_offset = chr_node->characteristic.val_handle - gatt_service->att_handle,
.properties = chr_node->characteristic.properties,
.num_descriptors = 0,
};
nimble_uuid_to_pebble(&chr_node->characteristic.uuid, &gatt_characteristic->uuid);
GATTServiceDiscoveryDescriptorNode *dsc_node =
(GATTServiceDiscoveryDescriptorNode *)chr_node->descriptors;
uint16_t dsc_index = 0;
while (dsc_node != NULL) {
GATTDescriptor *gatt_descriptor = &gatt_characteristic->descriptors[dsc_index];
*gatt_descriptor = (GATTDescriptor){
.att_handle_offset = dsc_node->descriptor.handle - gatt_service->att_handle,
};
nimble_uuid_to_pebble(&dsc_node->descriptor.uuid, &gatt_descriptor->uuid);
dsc_index++;
dsc_node = (GATTServiceDiscoveryDescriptorNode *)list_get_next(&dsc_node->node);
end_ptr += sizeof(GATTDescriptor);
}
gatt_characteristic->num_descriptors = dsc_index;
end_ptr += sizeof(GATTCharacteristic);
chr_node = (GATTServiceDiscoveryCharacteristicNode *)list_get_next(&chr_node->node);
}
char service_uuid_str[UUID_STRING_BUFFER_LENGTH];
uuid_to_string(&gatt_service->uuid, service_uuid_str);
bt_driver_cb_gatt_client_discovery_handle_indication(connection, gatt_service, BTErrnoOK);
return true;
}
static bool prv_find_svc_by_uuid(ListNode *node, void *data) {
GATTServiceDiscoveryServiceNode *service_node = (GATTServiceDiscoveryServiceNode *)node;
const ble_uuid_t *svc_uuid = (const ble_uuid_t *)data;
return ble_uuid_cmp(&service_node->service.uuid.u, svc_uuid) == 0;
}
static bool prv_find_chr_by_uuid(ListNode *node, void *data) {
GATTServiceDiscoveryCharacteristicNode *chr_node = (GATTServiceDiscoveryCharacteristicNode *)node;
const ble_uuid_t *chr_uuid = (const ble_uuid_t *)data;
return ble_uuid_cmp(&chr_node->characteristic.uuid.u, chr_uuid) == 0;
}
static bool prv_find_dsc_by_uuid(ListNode *node, void *data) {
GATTServiceDiscoveryDescriptorNode *dsc_node = (GATTServiceDiscoveryDescriptorNode *)node;
const ble_uuid_t *dsc_uuid = (const ble_uuid_t *)data;
return ble_uuid_cmp(&dsc_node->descriptor.uuid.u, dsc_uuid) == 0;
}
static bool prv_find_dsc_uuid(GATTServiceDiscoveryContext *context,
const ble_uuid_t *svc_uuid,
const ble_uuid_t *chr_uuid,
const ble_uuid_t *dsc_uuid,
uint16_t *chr_handle,
uint16_t *dsc_handle) {
GATTServiceDiscoveryServiceNode *service_node = (GATTServiceDiscoveryServiceNode *)list_find(
context->services, prv_find_svc_by_uuid, (void *)svc_uuid);
if (service_node == NULL) {
return false;
}
GATTServiceDiscoveryCharacteristicNode *chr_node =
(GATTServiceDiscoveryCharacteristicNode *)list_find(service_node->characteristics,
prv_find_chr_by_uuid, (void *)chr_uuid);
if (chr_node == NULL) {
return false;
}
GATTServiceDiscoveryDescriptorNode *dsc_node = (GATTServiceDiscoveryDescriptorNode *)list_find(
chr_node->descriptors, prv_find_dsc_by_uuid, (void *)dsc_uuid);
if (dsc_node == NULL) {
return false;
}
*chr_handle = chr_node->characteristic.val_handle;
*dsc_handle = dsc_node->descriptor.handle;
return true;
}
typedef struct {
GAPLEConnection *connection;
uint16_t chr_handle;
} GATTServiceDiscoveryDescriptorContext;
static int prv_on_svc_chgd_subscribe(uint16_t conn_handle, const struct ble_gatt_error *error,
struct ble_gatt_attr *attr, void *arg) {
if (error->status != 0) {
PBL_LOG_D_ERR(LOG_DOMAIN_BT, "Failed to subscribe to service changed: 0x%" PRIx16,
error->status);
} else {
GATTServiceDiscoveryDescriptorContext *ctx = arg;
bt_driver_cb_gatt_client_discovery_handle_service_changed(ctx->connection,
ctx->chr_handle);
PBL_LOG_D_DBG(LOG_DOMAIN_BT, "Subscribed to service changed");
}
kernel_free(arg);
return 0;
}
static void prv_convert_service_and_notify_os(uint16_t conn_handle, GATTServiceDiscoveryContext *context) {
list_foreach(context->services, prv_convert_service_and_notify_os_cb, context->connection);
bt_driver_cb_gatt_client_discovery_complete(context->connection, BTErrnoOK);
// Subscribe to service changed indications (BLE Core 6.0, part G 7.7.1)
uint16_t chr_handle, dsc_handle;
if (prv_find_dsc_uuid(context, BLE_UUID16_DECLARE(BLE_GATT_SVC_UUID16),
BLE_UUID16_DECLARE(BLE_SVC_GATT_CHR_SERVICE_CHANGED_UUID16),
BLE_UUID16_DECLARE(BLE_GATT_DSC_CLT_CFG_UUID16),
&chr_handle, &dsc_handle)) {
uint16_t value;
int ret;
GATTServiceDiscoveryDescriptorContext *ctx = kernel_zalloc_check(
sizeof(GATTServiceDiscoveryDescriptorContext));
ctx->connection = context->connection;
ctx->chr_handle = chr_handle;
value = 0x0002;
ret = ble_gattc_write_flat(conn_handle, dsc_handle,
&value, sizeof(value), prv_on_svc_chgd_subscribe, ctx);
if (ret != 0) {
PBL_LOG_D_ERR(LOG_DOMAIN_BT, "Failed to subscribe to service changed: %d", ret);
}
}
prv_free_discovery_context(context);
}
static uint16_t prv_get_last_dsc_handle(GATTServiceDiscoveryContext *context) {
const GATTServiceDiscoveryCharacteristicNode *next_chr =
(GATTServiceDiscoveryCharacteristicNode *)list_get_next(context->current_characteristic);
if (next_chr != NULL) {
return MIN(next_chr->characteristic.def_handle, next_chr->characteristic.val_handle) - 1;
} else {
return ((GATTServiceDiscoveryServiceNode *)context->current_service)->service.end_handle;
}
}
static int prv_find_dsc_cb(uint16_t conn_handle, const struct ble_gatt_error *error,
uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc, void *arg);
static int prv_find_chr_cb(uint16_t conn_handle, const struct ble_gatt_error *error,
const struct ble_gatt_chr *chr, void *arg);
static void prv_discover_next_dscs(uint16_t conn_handle, GATTServiceDiscoveryContext *context) {
GATTServiceDiscoveryCharacteristicNode *chr_node =
(GATTServiceDiscoveryCharacteristicNode *)context->current_characteristic;
uint16_t start_handle =
MIN(chr_node->characteristic.val_handle, chr_node->characteristic.def_handle);
uint16_t end_handle = prv_get_last_dsc_handle(context);
int rc = ble_gattc_disc_all_dscs(conn_handle, start_handle, end_handle, prv_find_dsc_cb, context);
if (rc != 0) {
PBL_LOG_D_ERR(LOG_DOMAIN_BT, "ble_gattc_disc_all_dscs rc=0x%04x (0x%04x -> 0x%04x)",
(uint16_t)rc, start_handle, end_handle);
}
}
static void prv_discover_next_chrs(uint16_t conn_handle, GATTServiceDiscoveryContext *context) {
GATTServiceDiscoveryServiceNode *service_node =
(GATTServiceDiscoveryServiceNode *)context->current_service;
int rc = ble_gattc_disc_all_chrs(conn_handle, service_node->service.start_handle,
service_node->service.end_handle, prv_find_chr_cb, context);
if (rc != 0) {
PBL_LOG_D_ERR(LOG_DOMAIN_BT, "ble_gattc_disc_all_chrs rc=0x%04x (0x%04x -> 0x%04x)",
(uint16_t)rc, service_node->service.start_handle, service_node->service.end_handle);
}
}
static GATTServiceDiscoveryDescriptorNode *prv_create_descriptor_node(
const struct ble_gatt_dsc *dsc) {
GATTServiceDiscoveryDescriptorNode *dsc_node =
kernel_zalloc_check(sizeof(GATTServiceDiscoveryDescriptorNode));
list_init(&dsc_node->node);
dsc_node->descriptor = *dsc;
return dsc_node;
}
static GATTServiceDiscoveryCharacteristicNode *prv_create_chr_node(const struct ble_gatt_chr *chr) {
GATTServiceDiscoveryCharacteristicNode *chr_node =
kernel_zalloc_check(sizeof(GATTServiceDiscoveryCharacteristicNode));
list_init(&chr_node->node);
chr_node->characteristic = *chr;
return chr_node;
}
static GATTServiceDiscoveryServiceNode *prv_create_service_node(const struct ble_gatt_svc *svc) {
GATTServiceDiscoveryServiceNode *service_node =
kernel_zalloc_check(sizeof(GATTServiceDiscoveryServiceNode));
list_init(&service_node->node);
service_node->service = *svc;
return service_node;
}
static GATTServiceDiscoveryCharacteristicNode *prv_get_current_chr(
GATTServiceDiscoveryContext *context) {
return (GATTServiceDiscoveryCharacteristicNode *)context->current_characteristic;
}
static GATTServiceDiscoveryServiceNode *prv_get_current_service(
GATTServiceDiscoveryContext *context) {
return (GATTServiceDiscoveryServiceNode *)context->current_service;
}
static void prv_list_append_or_set(ListNode **list, ListNode *node) {
if (*list == NULL) {
*list = node;
} else {
list_append(*list, node);
}
}
static int prv_find_dsc_cb(uint16_t conn_handle, const struct ble_gatt_error *error,
uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc, void *arg) {
GATTServiceDiscoveryContext *context = arg;
BTErrno errno;
if (s_stop_discovery_requested) {
prv_signal_discovery_done();
prv_free_discovery_context(context);
return BLE_HS_EDONE;
}
switch (error->status) {
case 0:
char chr_uuid_str[BLE_UUID_STR_LEN];
ble_uuid_to_str(&dsc->uuid.u, chr_uuid_str);
PBL_LOG_D_DBG(LOG_DOMAIN_BT, "Found descriptor %s (hdl: 0x%" PRIx16 ")",
chr_uuid_str, dsc->handle);
GATTServiceDiscoveryDescriptorNode *dsc_node = prv_create_descriptor_node(dsc);
GATTServiceDiscoveryCharacteristicNode *chr_node = prv_get_current_chr(context);
prv_list_append_or_set(&chr_node->descriptors, &dsc_node->node);
GATTServiceDiscoveryServiceNode *service_node = prv_get_current_service(context);
service_node->num_descriptors++;
break;
case BLE_HS_EDONE:
PBL_LOG_D_DBG(LOG_DOMAIN_BT, "Descriptor discovery done");
context->current_characteristic = list_get_next(context->current_characteristic);
if (context->current_characteristic != NULL) {
prv_discover_next_dscs(conn_handle, context);
} else {
context->current_service = list_get_next(context->current_service);
while (context->current_service != NULL) {
GATTServiceDiscoveryServiceNode *service_node = prv_get_current_service(context);
context->current_characteristic = list_get_head(service_node->characteristics);
if (context->current_characteristic != NULL) {
prv_discover_next_dscs(conn_handle, context);
break;
}
// Service has no characteristics, skip to next service
context->current_service = list_get_next(context->current_service);
}
if (context->current_service == NULL) {
// we're done!
prv_signal_discovery_done();
prv_convert_service_and_notify_os(conn_handle, context);
}
}
break;
default:
PBL_LOG_D_ERR(LOG_DOMAIN_BT, "Descriptor discovery error: %d",
error->status);
if (error->status == BLE_HS_ETIMEOUT) {
errno = BTErrnoServiceDiscoveryTimeout;
} else if (error->status == BLE_HS_ENOTCONN) {
errno = BTErrnoServiceDiscoveryDisconnected;
} else {
errno = BTErrnoInternalErrorBegin + error->status;
}
prv_signal_discovery_done();
bt_driver_cb_gatt_client_discovery_complete(context->connection, errno);
prv_free_discovery_context(context);
break;
}
return 0;
}
static int prv_find_chr_cb(uint16_t conn_handle, const struct ble_gatt_error *error,
const struct ble_gatt_chr *chr, void *arg) {
GATTServiceDiscoveryContext *context = arg;
BTErrno errno;
if (s_stop_discovery_requested) {
prv_signal_discovery_done();
prv_free_discovery_context(context);
return BLE_HS_EDONE;
}
switch (error->status) {
case 0:
char chr_uuid_str[BLE_UUID_STR_LEN];
ble_uuid_to_str(&chr->uuid.u, chr_uuid_str);
PBL_LOG_D_DBG(LOG_DOMAIN_BT, "Found characteristic %s (val hdl: 0x%" PRIx16 ", def hdl: 0x%" PRIx16 ")",
chr_uuid_str, chr->val_handle, chr->def_handle);
GATTServiceDiscoveryCharacteristicNode *chr_node = prv_create_chr_node(chr);
GATTServiceDiscoveryServiceNode *service_node = prv_get_current_service(context);
prv_list_append_or_set(&service_node->characteristics, &chr_node->node);
break;
case BLE_HS_EDONE:
PBL_LOG_D_DBG(LOG_DOMAIN_BT, "Characteristic discovery done");
context->current_service = list_get_next(context->current_service);
if (context->current_service != NULL) {
// we have another service to discover characteristics for
prv_discover_next_chrs(conn_handle, context);
} else {
// got all characteristics, now let's get descriptors
context->current_service = list_get_head(context->services);
GATTServiceDiscoveryServiceNode *service_node = prv_get_current_service(context);
context->current_characteristic = list_get_head(service_node->characteristics);
if (context->current_characteristic != NULL) {
prv_discover_next_dscs(conn_handle, context);
} else {
// No characteristics found, discovery complete
prv_signal_discovery_done();
prv_convert_service_and_notify_os(conn_handle, context);
}
}
break;
default:
PBL_LOG_D_DBG(LOG_DOMAIN_BT, "Characteristic discovery error: %d",
error->status);
if (error->status == BLE_HS_ETIMEOUT) {
errno = BTErrnoServiceDiscoveryTimeout;
} else if (error->status == BLE_HS_ENOTCONN) {
errno = BTErrnoServiceDiscoveryDisconnected;
} else {
errno = BTErrnoInternalErrorBegin + error->status;
}
prv_signal_discovery_done();
bt_driver_cb_gatt_client_discovery_complete(context->connection, errno);
prv_free_discovery_context(context);
break;
}
return 0;
}
static int prv_find_inc_svc_cb(uint16_t conn_handle, const struct ble_gatt_error *error,
const struct ble_gatt_svc *service, void *arg) {
GATTServiceDiscoveryContext *context = arg;
BTErrno errno;
if (s_stop_discovery_requested) {
prv_signal_discovery_done();
prv_free_discovery_context(context);
return BLE_HS_EDONE;
}
switch (error->status) {
case 0:
if (service->start_handle < context->range.start ||
service->end_handle > context->range.end) {
return 0; // skip this service
}
GATTServiceDiscoveryServiceNode *service_node = prv_create_service_node(service);
prv_list_append_or_set(&context->services, &service_node->node);
char service_uuid_str[BLE_UUID_STR_LEN];
ble_uuid_to_str(&service->uuid.u, service_uuid_str);
PBL_LOG_D_DBG(LOG_DOMAIN_BT, "Found service %s, 0x%" PRIx16 "-0x%" PRIx16 " (total %lu)",
service_uuid_str, service->start_handle, service->end_handle,
list_count(context->services));
break;
case BLE_HS_EDONE:
PBL_LOG_D_DBG(LOG_DOMAIN_BT, "Service discovery complete");
if (context->services != NULL) {
// got services, start discovering characteristics
context->current_service = list_get_head(context->services);
prv_discover_next_chrs(conn_handle, context);
} else {
// no services found
prv_signal_discovery_done();
bt_driver_cb_gatt_client_discovery_complete(context->connection, BTErrnoOK);
prv_free_discovery_context(context);
}
break;
default:
PBL_LOG_D_ERR(LOG_DOMAIN_BT, "Service discovery error: %d", error->status);
if (error->status == BLE_HS_ETIMEOUT) {
errno = BTErrnoServiceDiscoveryTimeout;
} else if (error->status == BLE_HS_ENOTCONN) {
errno = BTErrnoServiceDiscoveryDisconnected;
} else {
errno = BTErrnoInternalErrorBegin + error->status;
}
prv_signal_discovery_done();
bt_driver_cb_gatt_client_discovery_complete(context->connection, errno);
prv_free_discovery_context(context);
break;
}
return 0;
}
void nimble_discover_init(void) {
s_discovery_stopped = xSemaphoreCreateBinary();
}
BTErrno bt_driver_gatt_start_discovery_range(const GAPLEConnection *connection,
const ATTHandleRange *data) {
uint16_t conn_handle;
if (!pebble_device_to_nimble_conn_handle(&connection->device, &conn_handle)) {
return BTErrnoInvalidState;
}
GATTServiceDiscoveryContext *context = kernel_zalloc_check(sizeof(GATTServiceDiscoveryContext));
context->connection = (GAPLEConnection *)connection;
context->range = *data;
// Reset stop flag and mark discovery in progress BEFORE issuing the discovery so that
// callbacks (which run on the NimBLE host task) cannot observe stale flags from a prior
// stop request and silently abort the freshly-started discovery.
s_stop_discovery_requested = false;
s_discovery_in_progress = true;
int rc = ble_gattc_disc_all_svcs(conn_handle, prv_find_inc_svc_cb, (void *)context);
if (rc != 0) {
s_discovery_in_progress = false;
kernel_free(context);
return BTErrnoInternalErrorBegin + rc;
}
return BTErrnoOK;
}
// will need to implement this by returning a different value in the callback
// but not sure if this can get called multiple times in parallel, might need
// to stuff the flag in the connection struct
BTErrno bt_driver_gatt_stop_discovery(GAPLEConnection *connection) {
uint16_t conn_handle;
if (!pebble_device_to_nimble_conn_handle(&connection->device, &conn_handle)) {
return BTErrnoInvalidState;
}
// Drain any stale semaphore signal that might have been left by a previous race
// (e.g. a callback gave the semaphore but we observed in_progress=false and skipped
// the take). This guarantees the take below waits for a fresh give.
xSemaphoreTake(s_discovery_stopped, 0);
// Set the stop request BEFORE checking in_progress. A callback that runs after this
// point will observe the flag (either at its top check or via prv_signal_discovery_done
// at its termination path) and give the semaphore. Without this ordering, a callback
// that completed naturally between our check and our flag set would never give the
// semaphore, causing xSemaphoreTake below to block forever and hang KernelBG until
// the watchdog resets the watch into PRF.
s_stop_discovery_requested = true;
if (s_discovery_in_progress) {
xSemaphoreTake(s_discovery_stopped, portMAX_DELAY);
}
s_stop_discovery_requested = false;
return BTErrnoOK;
}
void bt_driver_gatt_handle_discovery_abandoned(void) {}