Skip to content

Commit 76f4741

Browse files
committed
extmod/modbluetooth: Use us instead of ms for advertising interval.
This is to more accurately match the BLE spec, where intervals are configured in units of channel hop time (625us). When it was specified in ms, not all "valid" intervals were able to be specified. Now that we're also allowing configuration of scan interval, this commit updates advertising to match.
1 parent b65cc38 commit 76f4741

File tree

3 files changed

+10
-16
lines changed

3 files changed

+10
-16
lines changed

extmod/modbluetooth.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,18 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bluetooth_ble_irq_obj, 1, bluetooth_ble_irq);
301301
// ----------------------------------------------------------------------------
302302

303303
STATIC mp_obj_t bluetooth_ble_gap_advertise(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
304-
enum { ARG_interval_ms, ARG_adv_data, ARG_resp_data, ARG_connectable };
304+
enum { ARG_interval_us, ARG_adv_data, ARG_resp_data, ARG_connectable };
305305
static const mp_arg_t allowed_args[] = {
306-
{ MP_QSTR_interval_ms, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(100)} },
306+
{ MP_QSTR_interval_us, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(100)} },
307307
{ MP_QSTR_adv_data, MP_ARG_OBJ, {.u_obj = mp_const_none } },
308308
{ MP_QSTR_resp_data, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none } },
309309
{ MP_QSTR_connectable, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_true } },
310310
};
311311
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
312312
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
313313

314-
mp_int_t interval_ms;
315-
if (args[ARG_interval_ms].u_obj == mp_const_none || (interval_ms = mp_obj_get_int(args[ARG_interval_ms].u_obj)) == 0) {
314+
mp_int_t interval_us;
315+
if (args[ARG_interval_us].u_obj == mp_const_none || (interval_us = mp_obj_get_int(args[ARG_interval_us].u_obj)) == 0) {
316316
mp_bluetooth_gap_advertise_stop();
317317
return mp_const_none;
318318
}
@@ -329,7 +329,7 @@ STATIC mp_obj_t bluetooth_ble_gap_advertise(size_t n_args, const mp_obj_t *pos_a
329329
mp_get_buffer_raise(args[ARG_resp_data].u_obj, &resp_bufinfo, MP_BUFFER_READ);
330330
}
331331

332-
return bluetooth_handle_errno(mp_bluetooth_gap_advertise_start(connectable, interval_ms, adv_bufinfo.buf, adv_bufinfo.len, resp_bufinfo.buf, resp_bufinfo.len));
332+
return bluetooth_handle_errno(mp_bluetooth_gap_advertise_start(connectable, interval_us, adv_bufinfo.buf, adv_bufinfo.len, resp_bufinfo.buf, resp_bufinfo.len));
333333
}
334334
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bluetooth_ble_gap_advertise_obj, 1, bluetooth_ble_gap_advertise);
335335

extmod/modbluetooth.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void mp_bluetooth_get_device_addr(uint8_t *addr);
157157

158158
// Start advertisement. Will re-start advertisement when already enabled.
159159
// Returns errno on failure.
160-
int mp_bluetooth_gap_advertise_start(bool connectable, uint16_t interval_ms, const uint8_t *adv_data, size_t adv_data_len, const uint8_t *sr_data, size_t sr_data_len);
160+
int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, const uint8_t *adv_data, size_t adv_data_len, const uint8_t *sr_data, size_t sr_data_len);
161161

162162
// Stop advertisement. No-op when already stopped.
163163
void mp_bluetooth_gap_advertise_stop(void);

extmod/modbluetooth_nimble.c

+4-10
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ void mp_bluetooth_get_device_addr(uint8_t *addr) {
341341
#endif
342342
}
343343

344-
int mp_bluetooth_gap_advertise_start(bool connectable, uint16_t interval_ms, const uint8_t *adv_data, size_t adv_data_len, const uint8_t *sr_data, size_t sr_data_len) {
344+
int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, const uint8_t *adv_data, size_t adv_data_len, const uint8_t *sr_data, size_t sr_data_len) {
345345
int ret;
346346

347347
mp_bluetooth_gap_advertise_stop();
@@ -360,18 +360,12 @@ int mp_bluetooth_gap_advertise_start(bool connectable, uint16_t interval_ms, con
360360
}
361361
}
362362

363-
// Convert from 1ms to 0.625ms units.
364-
interval_ms = interval_ms * 8 / 5;
365-
if (interval_ms < 0x20 || interval_ms > 0x4000) {
366-
return MP_EINVAL;
367-
}
368-
369363
struct ble_gap_adv_params adv_params = {
370364
.conn_mode = connectable ? BLE_GAP_CONN_MODE_UND : BLE_GAP_CONN_MODE_NON,
371365
.disc_mode = BLE_GAP_DISC_MODE_GEN,
372-
.itvl_min = interval_ms,
373-
.itvl_max = interval_ms,
374-
.channel_map = 7, // all 3 channels
366+
.itvl_min = interval_us / BLE_HCI_ADV_ITVL, // convert to 625us units.
367+
.itvl_max = interval_us / BLE_HCI_ADV_ITVL,
368+
.channel_map = 7, // all 3 channels.
375369
};
376370

377371
ret = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL);

0 commit comments

Comments
 (0)