@@ -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.
182182static 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-
193187static 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+
210218static 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