Skip to content

Commit 94cfa71

Browse files
authored
Merge pull request adafruit#11050 from dhalbert/dhalbert/fix-espressif-ble-discovery
Fix slow espressif BLE discovery
2 parents 4ac2721 + daa8531 commit 94cfa71

2 files changed

Lines changed: 22 additions & 25 deletions

File tree

‎ports/espressif/common-hal/_bleio/Characteristic.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
9292
self->max_length = max_length;
9393
self->fixed_length = fixed_length;
9494

95-
if (initial_value_bufinfo != NULL) {
95+
if (!service->is_remote && initial_value_bufinfo != NULL) {
9696
common_hal_bleio_characteristic_set_value(self, initial_value_bufinfo);
9797
}
9898

‎ports/espressif/common-hal/_bleio/Connection.c‎

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -181,24 +181,21 @@ void common_hal_bleio_connection_set_connection_interval(bleio_connection_intern
181181
// Zero when discovery is in process. BLE_HS_EDONE or a BLE_HS_ error code when done.
182182
static volatile int _last_discovery_status;
183183

184-
static uint64_t _discovery_start_time;
185-
186-
// Give 20 seconds for discovery
184+
// Give 20 seconds for each step of discovery: services, characteristics, attributes.
187185
#define DISCOVERY_TIMEOUT_MS 20000
188186

189-
static void _start_discovery_timeout(void) {
190-
_discovery_start_time = common_hal_time_monotonic_ms();
191-
}
192-
193187
static int _wait_for_discovery_step_done(void) {
194-
const uint64_t timeout_time_ms = _discovery_start_time + DISCOVERY_TIMEOUT_MS;
188+
const uint64_t timeout_time_ms = common_hal_time_monotonic_ms() + DISCOVERY_TIMEOUT_MS;
195189
while ((_last_discovery_status == 0) && (common_hal_time_monotonic_ms() < timeout_time_ms)) {
196190
RUN_BACKGROUND_TASKS;
197191
if (mp_hal_is_interrupted()) {
198192
// Return prematurely. Then the interrupt will be raised.
199193
_last_discovery_status = BLE_HS_EDONE;
200194
}
201195
}
196+
if (_last_discovery_status == 0) {
197+
return BLE_HS_ETIMEOUT;
198+
}
202199
return _last_discovery_status;
203200
}
204201

@@ -207,6 +204,17 @@ static void _set_discovery_step_status(int status) {
207204
_last_discovery_status = status;
208205
}
209206

207+
static void _check_discovery_status(int status) {
208+
if (status == BLE_HS_EDONE) {
209+
return;
210+
}
211+
if (status < BLE_HS_ERR_ATT_BASE) {
212+
CHECK_NIMBLE_ERROR(status);
213+
return;
214+
}
215+
CHECK_BLE_ERROR(status);
216+
}
217+
210218
static int _discovered_service_cb(uint16_t conn_handle,
211219
const struct ble_gatt_error *error,
212220
const struct ble_gatt_svc *svc,
@@ -350,9 +358,6 @@ static void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
350358
// Start over with an empty list.
351359
self->remote_service_list = mp_obj_new_list(0, NULL);
352360

353-
// Start timeout in case discovery gets stuck.
354-
_start_discovery_timeout();
355-
356361
if (service_uuids_whitelist == mp_const_none) {
357362
// Reset discovery status before starting callbacks
358363
_set_discovery_step_status(0);
@@ -361,9 +366,7 @@ static void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
361366

362367
// Wait for _discovered_service_cb() to be called multiple times until it's done.
363368
int status = _wait_for_discovery_step_done();
364-
if (status != BLE_HS_EDONE) {
365-
CHECK_BLE_ERROR(status);
366-
}
369+
_check_discovery_status(status);
367370
} else {
368371
mp_obj_iter_buf_t iter_buf;
369372
mp_obj_t iterable = mp_getiter(service_uuids_whitelist, &iter_buf);
@@ -382,9 +385,7 @@ static void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
382385

383386
// Wait for _discovered_service_cb() to be called multiple times until it's done.
384387
int status = _wait_for_discovery_step_done();
385-
if (status != BLE_HS_EDONE) {
386-
CHECK_BLE_ERROR(status);
387-
}
388+
_check_discovery_status(status);
388389
}
389390
}
390391

@@ -404,9 +405,7 @@ static void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
404405

405406
// Wait for _discovered_characteristic_cb() to be called multiple times until it's done.
406407
int status = _wait_for_discovery_step_done();
407-
if (status != BLE_HS_EDONE) {
408-
CHECK_BLE_ERROR(status);
409-
}
408+
_check_discovery_status(status);
410409

411410
// Got characteristics for this service. Now discover descriptors for each characteristic.
412411
size_t char_list_len = service->characteristic_list->len;
@@ -423,7 +422,7 @@ static void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
423422

424423
uint16_t end_handle = next_characteristic == NULL
425424
? service->end_handle
426-
: next_characteristic->handle - 1;
425+
: next_characteristic->def_handle - 1;
427426

428427
// Pre-check if there are no descriptors to discover so descriptor discovery doesn't fail
429428
if (end_handle <= characteristic->handle) {
@@ -441,9 +440,7 @@ static void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
441440

442441
// Wait for _discovered_descriptor_cb to be called multiple times until it's done.
443442
status = _wait_for_discovery_step_done();
444-
if (status != BLE_HS_EDONE) {
445-
CHECK_BLE_ERROR(status);
446-
}
443+
_check_discovery_status(status);
447444
}
448445
}
449446
}

0 commit comments

Comments
 (0)