Skip to content

Commit 43467b9

Browse files
jimmodpgeorge
authored andcommitted
extmod/modbluetooth: Add connection interval to gap_connect.
This forwards through directly to the NimBLE and BTStack connect functions. Signed-off-by: Jim Mussared <[email protected]>
1 parent 64e4bae commit 43467b9

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

docs/library/bluetooth.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,23 @@ Central Role
359359

360360
A central device can connect to peripherals that it has discovered using the observer role (see :meth:`gap_scan<BLE.gap_scan>`) or with a known address.
361361

362-
.. method:: BLE.gap_connect(addr_type, addr, scan_duration_ms=2000, /)
362+
.. method:: BLE.gap_connect(addr_type, addr, scan_duration_ms=2000, min_conn_interval_us=None, max_conn_interval_us=None, /)
363363

364364
Connect to a peripheral.
365365

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

368368
On success, the ``_IRQ_PERIPHERAL_CONNECT`` event will be raised.
369369

370+
The device will wait up to *scan_duration_ms* to receive an advertising
371+
payload from the device.
372+
373+
The connection interval can be configured in **micro**\ seconds using either
374+
or both of *min_conn_interval_us* and *max_conn_interval_us*. Otherwise a
375+
default interval will be chosen, typically between 30000 and 50000
376+
microseconds. A shorter interval will increase throughput, at the expense
377+
of power usage.
378+
370379

371380
Peripheral Role
372381
---------------

extmod/btstack/modbluetooth_btstack.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,13 +1265,13 @@ int mp_bluetooth_gap_scan_stop(void) {
12651265
return 0;
12661266
}
12671267

1268-
int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms) {
1268+
int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms, int32_t min_conn_interval_us, int32_t max_conn_interval_us) {
12691269
DEBUG_printf("mp_bluetooth_gap_peripheral_connect\n");
12701270

12711271
uint16_t conn_scan_interval = 60000 / 625;
12721272
uint16_t conn_scan_window = 30000 / 625;
1273-
uint16_t conn_interval_min = 10000 / 1250;
1274-
uint16_t conn_interval_max = 30000 / 1250;
1273+
uint16_t conn_interval_min = (min_conn_interval_us ? min_conn_interval_us : 10000) / 1250;
1274+
uint16_t conn_interval_max = (max_conn_interval_us ? max_conn_interval_us : 30000) / 1250;
12751275
uint16_t conn_latency = 4;
12761276
uint16_t supervision_timeout = duration_ms / 10; // default = 720
12771277
uint16_t min_ce_length = 10000 / 625;

extmod/modbluetooth.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,14 +637,22 @@ STATIC mp_obj_t bluetooth_ble_gap_connect(size_t n_args, const mp_obj_t *args) {
637637
mp_raise_ValueError(MP_ERROR_TEXT("invalid addr"));
638638
}
639639
mp_int_t scan_duration_ms = MP_BLUETOOTH_CONNECT_DEFAULT_SCAN_DURATION_MS;
640-
if (n_args == 4) {
640+
mp_int_t min_conn_interval_us = 0;
641+
mp_int_t max_conn_interval_us = 0;
642+
if (n_args >= 4 && args[3] != mp_const_none) {
641643
scan_duration_ms = mp_obj_get_int(args[3]);
642644
}
645+
if (n_args >= 5 && args[4] != mp_const_none) {
646+
min_conn_interval_us = mp_obj_get_int(args[4]);
647+
}
648+
if (n_args >= 6 && args[5] != mp_const_none) {
649+
max_conn_interval_us = mp_obj_get_int(args[5]);
650+
}
643651

644-
int err = mp_bluetooth_gap_peripheral_connect(addr_type, bufinfo.buf, scan_duration_ms);
652+
int err = mp_bluetooth_gap_peripheral_connect(addr_type, bufinfo.buf, scan_duration_ms, min_conn_interval_us, max_conn_interval_us);
645653
return bluetooth_handle_errno(err);
646654
}
647-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_connect_obj, 3, 4, bluetooth_ble_gap_connect);
655+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_connect_obj, 3, 6, bluetooth_ble_gap_connect);
648656

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

extmod/modbluetooth.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_
370370
int mp_bluetooth_gap_scan_stop(void);
371371

372372
// Connect to a found peripheral.
373-
int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms);
373+
int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms, int32_t min_conn_interval_us, int32_t max_conn_interval_us);
374374
#endif
375375

376376
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT

extmod/nimble/modbluetooth_nimble.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) {
12021202
return commmon_gap_event_cb(event, arg);
12031203
}
12041204

1205-
int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms) {
1205+
int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms, int32_t min_conn_interval_us, int32_t max_conn_interval_us) {
12061206
DEBUG_printf("mp_bluetooth_gap_peripheral_connect\n");
12071207
if (!mp_bluetooth_is_active()) {
12081208
return ERRNO_BLUETOOTH_NOT_ACTIVE;
@@ -1211,12 +1211,14 @@ int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr,
12111211
mp_bluetooth_gap_scan_stop();
12121212
}
12131213

1214-
// TODO: This is the same as ble_gap_conn_params_dflt (i.e. passing NULL).
1215-
STATIC const struct ble_gap_conn_params params = {
1214+
uint16_t conn_interval_min = min_conn_interval_us ? min_conn_interval_us / BLE_HCI_CONN_ITVL : BLE_GAP_INITIAL_CONN_ITVL_MIN;
1215+
uint16_t conn_interval_max = max_conn_interval_us ? max_conn_interval_us / BLE_HCI_CONN_ITVL : BLE_GAP_INITIAL_CONN_ITVL_MAX;
1216+
1217+
const struct ble_gap_conn_params params = {
12161218
.scan_itvl = 0x0010,
12171219
.scan_window = 0x0010,
1218-
.itvl_min = BLE_GAP_INITIAL_CONN_ITVL_MIN,
1219-
.itvl_max = BLE_GAP_INITIAL_CONN_ITVL_MAX,
1220+
.itvl_min = conn_interval_min,
1221+
.itvl_max = conn_interval_max,
12201222
.latency = BLE_GAP_INITIAL_CONN_LATENCY,
12211223
.supervision_timeout = BLE_GAP_INITIAL_SUPERVISION_TIMEOUT,
12221224
.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN,

ports/zephyr/modbluetooth_zephyr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ int mp_bluetooth_gap_scan_stop(void) {
400400
return bt_err_to_errno(err);
401401
}
402402

403-
int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms) {
403+
int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms, int32_t min_conn_interval_us, int32_t max_conn_interval_us) {
404404
DEBUG_printf("mp_bluetooth_gap_peripheral_connect\n");
405405
if (!mp_bluetooth_is_active()) {
406406
return ERRNO_BLUETOOTH_NOT_ACTIVE;

0 commit comments

Comments
 (0)