From 6465d0339d755a739a9f0b33b5d6c4b2c8d59ee8 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 05:18:25 +0000 Subject: [PATCH] espressif: _bleio: end connect() when the link fails before it is reported NimBLE reports a central connection with BLE_GAP_EVENT_CONNECT only after it has read the peer's features and version. If the link fails with HCI 0x3E (connection failed to be established) before then, NimBLE retries it (BT_NIMBLE_ENABLE_CONN_REATTEMPT, on by default) and finally sends BLE_GAP_EVENT_DISCONNECT to the connect callback. No BLE_GAP_EVENT_CONNECT follows, and the connect timer was already cancelled when the controller reported the link, so connect() waited forever. Handle BLE_GAP_EVENT_DISCONNECT in _connect_event by ending the wait with the disconnect reason, so connect() raises instead of hanging. With CIRCUITPY_VERBOSE_BLE, also print the reattempts and the reason. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01619FtugLCtyvd5FEfJCyzo --- ports/espressif/common-hal/_bleio/Adapter.c | 28 +++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/ports/espressif/common-hal/_bleio/Adapter.c b/ports/espressif/common-hal/_bleio/Adapter.c index 63549fc2ee6..a47d7bc8a57 100644 --- a/ports/espressif/common-hal/_bleio/Adapter.c +++ b/ports/espressif/common-hal/_bleio/Adapter.c @@ -442,6 +442,29 @@ static int _connect_event(struct ble_gap_event *event, void *self_in) { } break; + case BLE_GAP_EVENT_DISCONNECT: + // The link failed before NimBLE reported the connection, e.g. with 0x3E (connection + // failed to be established) once its automatic reattempts were used up. No + // BLE_GAP_EVENT_CONNECT follows, so end the wait here. No connection slot was + // taken yet: that only happens on BLE_GAP_EVENT_CONNECT. + #if CIRCUITPY_VERBOSE_BLE + mp_printf(&mp_plat_print, "Disconnected while connecting: reason 0x%04x\n", + event->disconnect.reason); + #endif + _connection_status = event->disconnect.reason > 0 + ? -event->disconnect.reason + : -BLE_HS_ENOTCONN; + break; + + #if MYNEWT_VAL(BLE_ENABLE_CONN_REATTEMPT) + case BLE_GAP_EVENT_REATTEMPT_COUNT: + // NimBLE is retrying a link that failed to be established. Keep waiting. + #if CIRCUITPY_VERBOSE_BLE + mp_printf(&mp_plat_print, "Connect reattempt %d\n", event->reattempt_cnt.count); + #endif + break; + #endif + default: #if CIRCUITPY_VERBOSE_BLE // For debugging. @@ -487,8 +510,9 @@ mp_obj_t common_hal_bleio_adapter_connect(bleio_adapter_obj_t *self, bleio_addre &conn_params, _connect_event, self)); - // NimBLE always ends a connection attempt with a BLE_GAP_EVENT_CONNECT, including when - // timeout_ms expires, so wait for that instead of keeping a separate deadline here. + // NimBLE ends a connection attempt with a BLE_GAP_EVENT_CONNECT, including when timeout_ms + // expires, or with a BLE_GAP_EVENT_DISCONNECT if the link fails before the connection is + // reported. Wait for one of those instead of keeping a separate deadline here. // If we stopped waiting first, NimBLE would still be connecting, and the next connect() // would fail. while (_connection_status == BLEIO_HANDLE_INVALID) {