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.
This commit is contained in:
parent
b65cc387cd
commit
76f474129e
@ -301,9 +301,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bluetooth_ble_irq_obj, 1, bluetooth_ble_irq);
|
|||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
STATIC mp_obj_t bluetooth_ble_gap_advertise(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
STATIC mp_obj_t bluetooth_ble_gap_advertise(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||||
enum { ARG_interval_ms, ARG_adv_data, ARG_resp_data, ARG_connectable };
|
enum { ARG_interval_us, ARG_adv_data, ARG_resp_data, ARG_connectable };
|
||||||
static const mp_arg_t allowed_args[] = {
|
static const mp_arg_t allowed_args[] = {
|
||||||
{ MP_QSTR_interval_ms, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(100)} },
|
{ MP_QSTR_interval_us, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(100)} },
|
||||||
{ MP_QSTR_adv_data, MP_ARG_OBJ, {.u_obj = mp_const_none } },
|
{ MP_QSTR_adv_data, MP_ARG_OBJ, {.u_obj = mp_const_none } },
|
||||||
{ MP_QSTR_resp_data, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none } },
|
{ MP_QSTR_resp_data, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none } },
|
||||||
{ MP_QSTR_connectable, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_true } },
|
{ MP_QSTR_connectable, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_true } },
|
||||||
@ -311,8 +311,8 @@ STATIC mp_obj_t bluetooth_ble_gap_advertise(size_t n_args, const mp_obj_t *pos_a
|
|||||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||||
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||||
|
|
||||||
mp_int_t interval_ms;
|
mp_int_t interval_us;
|
||||||
if (args[ARG_interval_ms].u_obj == mp_const_none || (interval_ms = mp_obj_get_int(args[ARG_interval_ms].u_obj)) == 0) {
|
if (args[ARG_interval_us].u_obj == mp_const_none || (interval_us = mp_obj_get_int(args[ARG_interval_us].u_obj)) == 0) {
|
||||||
mp_bluetooth_gap_advertise_stop();
|
mp_bluetooth_gap_advertise_stop();
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
@ -329,7 +329,7 @@ STATIC mp_obj_t bluetooth_ble_gap_advertise(size_t n_args, const mp_obj_t *pos_a
|
|||||||
mp_get_buffer_raise(args[ARG_resp_data].u_obj, &resp_bufinfo, MP_BUFFER_READ);
|
mp_get_buffer_raise(args[ARG_resp_data].u_obj, &resp_bufinfo, MP_BUFFER_READ);
|
||||||
}
|
}
|
||||||
|
|
||||||
return bluetooth_handle_errno(mp_bluetooth_gap_advertise_start(connectable, interval_ms, adv_bufinfo.buf, adv_bufinfo.len, resp_bufinfo.buf, resp_bufinfo.len));
|
return bluetooth_handle_errno(mp_bluetooth_gap_advertise_start(connectable, interval_us, adv_bufinfo.buf, adv_bufinfo.len, resp_bufinfo.buf, resp_bufinfo.len));
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bluetooth_ble_gap_advertise_obj, 1, bluetooth_ble_gap_advertise);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bluetooth_ble_gap_advertise_obj, 1, bluetooth_ble_gap_advertise);
|
||||||
|
|
||||||
|
@ -157,7 +157,7 @@ void mp_bluetooth_get_device_addr(uint8_t *addr);
|
|||||||
|
|
||||||
// Start advertisement. Will re-start advertisement when already enabled.
|
// Start advertisement. Will re-start advertisement when already enabled.
|
||||||
// Returns errno on failure.
|
// Returns errno on failure.
|
||||||
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);
|
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);
|
||||||
|
|
||||||
// Stop advertisement. No-op when already stopped.
|
// Stop advertisement. No-op when already stopped.
|
||||||
void mp_bluetooth_gap_advertise_stop(void);
|
void mp_bluetooth_gap_advertise_stop(void);
|
||||||
|
@ -341,7 +341,7 @@ void mp_bluetooth_get_device_addr(uint8_t *addr) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
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) {
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
mp_bluetooth_gap_advertise_stop();
|
mp_bluetooth_gap_advertise_stop();
|
||||||
@ -360,18 +360,12 @@ int mp_bluetooth_gap_advertise_start(bool connectable, uint16_t interval_ms, con
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert from 1ms to 0.625ms units.
|
|
||||||
interval_ms = interval_ms * 8 / 5;
|
|
||||||
if (interval_ms < 0x20 || interval_ms > 0x4000) {
|
|
||||||
return MP_EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ble_gap_adv_params adv_params = {
|
struct ble_gap_adv_params adv_params = {
|
||||||
.conn_mode = connectable ? BLE_GAP_CONN_MODE_UND : BLE_GAP_CONN_MODE_NON,
|
.conn_mode = connectable ? BLE_GAP_CONN_MODE_UND : BLE_GAP_CONN_MODE_NON,
|
||||||
.disc_mode = BLE_GAP_DISC_MODE_GEN,
|
.disc_mode = BLE_GAP_DISC_MODE_GEN,
|
||||||
.itvl_min = interval_ms,
|
.itvl_min = interval_us / BLE_HCI_ADV_ITVL, // convert to 625us units.
|
||||||
.itvl_max = interval_ms,
|
.itvl_max = interval_us / BLE_HCI_ADV_ITVL,
|
||||||
.channel_map = 7, // all 3 channels
|
.channel_map = 7, // all 3 channels.
|
||||||
};
|
};
|
||||||
|
|
||||||
ret = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL);
|
ret = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL);
|
||||||
|
Loading…
Reference in New Issue
Block a user