Skip to content

Commit 851ecb2

Browse files
jonathanhoggdpgeorge
authored andcommitted
extmod/modbluetooth: Support gap_connect(None) to cancel a connection.
Allow cancellation of in-progress peripheral connections.
1 parent de7e3cd commit 851ecb2

5 files changed

Lines changed: 32 additions & 2 deletions

File tree

docs/library/bluetooth.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,12 @@ A central device can connect to peripherals that it has discovered using the obs
365365

366366
See :meth:`gap_scan <BLE.gap_scan>` for details about address types.
367367

368-
On success, the ``_IRQ_PERIPHERAL_CONNECT`` event will be raised.
368+
To cancel an outstanding connection attempt early, call
369+
``gap_connect(None)``.
370+
371+
On success, the ``_IRQ_PERIPHERAL_CONNECT`` event will be raised. If
372+
cancelling a connection attempt, the ``_IRQ_PERIPHERAL_DISCONNECT`` event
373+
will be raised.
369374

370375
The device will wait up to *scan_duration_ms* to receive an advertising
371376
payload from the device.

extmod/btstack/modbluetooth_btstack.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,11 @@ int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr,
12841284
return btstack_error_to_errno(gap_connect(btstack_addr, addr_type));
12851285
}
12861286

1287+
int mp_bluetooth_gap_peripheral_connect_cancel(void) {
1288+
DEBUG_printf("mp_bluetooth_gap_peripheral_connect_cancel\n");
1289+
return btstack_error_to_errno(gap_connect_cancel());
1290+
}
1291+
12871292
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
12881293

12891294
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT

extmod/modbluetooth.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gatts_register_services_obj, blue
630630

631631
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
632632
STATIC mp_obj_t bluetooth_ble_gap_connect(size_t n_args, const mp_obj_t *args) {
633+
if (n_args == 2) {
634+
if (args[1] == mp_const_none) {
635+
int err = mp_bluetooth_gap_peripheral_connect_cancel();
636+
return bluetooth_handle_errno(err);
637+
}
638+
mp_raise_TypeError(MP_ERROR_TEXT("invalid addr"));
639+
}
633640
uint8_t addr_type = mp_obj_get_int(args[1]);
634641
mp_buffer_info_t bufinfo = {0};
635642
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
@@ -652,7 +659,7 @@ STATIC mp_obj_t bluetooth_ble_gap_connect(size_t n_args, const mp_obj_t *args) {
652659
int err = mp_bluetooth_gap_peripheral_connect(addr_type, bufinfo.buf, scan_duration_ms, min_conn_interval_us, max_conn_interval_us);
653660
return bluetooth_handle_errno(err);
654661
}
655-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_connect_obj, 3, 6, bluetooth_ble_gap_connect);
662+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_connect_obj, 2, 6, bluetooth_ble_gap_connect);
656663

657664
STATIC mp_obj_t bluetooth_ble_gap_scan(size_t n_args, const mp_obj_t *args) {
658665
// Default is indefinite scan, with the NimBLE "background scan" interval and window.

extmod/modbluetooth.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr,
374374
#endif
375375

376376
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
377+
378+
// Cancel in-progress connection to a peripheral.
379+
int mp_bluetooth_gap_peripheral_connect_cancel(void);
380+
377381
// Find all primary services on the connected peripheral.
378382
int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_obj_bluetooth_uuid_t *uuid);
379383

extmod/nimble/modbluetooth_nimble.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,15 @@ int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr,
12361236
return ble_hs_err_to_errno(err);
12371237
}
12381238

1239+
int mp_bluetooth_gap_peripheral_connect_cancel(void) {
1240+
DEBUG_printf("mp_bluetooth_gap_peripheral_connect_cancel\n");
1241+
if (!mp_bluetooth_is_active()) {
1242+
return ERRNO_BLUETOOTH_NOT_ACTIVE;
1243+
}
1244+
int err = ble_gap_conn_cancel();
1245+
return ble_hs_err_to_errno(err);
1246+
}
1247+
12391248
STATIC int ble_gattc_service_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *service, void *arg) {
12401249
DEBUG_printf("ble_gattc_service_cb: conn_handle=%d status=%d start_handle=%d\n", conn_handle, error->status, service ? service->start_handle : -1);
12411250
if (!mp_bluetooth_is_active()) {

0 commit comments

Comments
 (0)