diff --git a/ports/espressif/common-hal/_bleio/Adapter.c b/ports/espressif/common-hal/_bleio/Adapter.c index 364a9dd5433f4..21cdd88b1dbcb 100644 --- a/ports/espressif/common-hal/_bleio/Adapter.c +++ b/ports/espressif/common-hal/_bleio/Adapter.c @@ -528,7 +528,6 @@ static int _advertising_event(struct ble_gap_event *event, void *self_in) { #endif break; } - background_callback_add_core(&bleio_background_callback); return 0; } @@ -559,7 +558,13 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, return rc; } - bool high_duty_directed = directed_to != NULL && interval <= 3.5 && timeout <= 1.3; + bool high_duty_directed = directed_to != NULL && interval <= 3.5 && timeout <= 1; // Really 1.3, but it's an int + + uint32_t timeout_ms = timeout * 1000; + if (timeout_ms == 0) { + timeout_ms = BLE_HS_FOREVER; + } + #if MYNEWT_VAL(BLE_EXT_ADV) bool extended = advertising_data_len > BLE_ADV_LEGACY_DATA_MAX_LEN || @@ -621,7 +626,7 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, } } - rc = ble_gap_ext_adv_start(0, timeout / 10, 0); + rc = ble_gap_ext_adv_start(0, timeout_ms, 0); #else uint8_t conn_mode = connectable ? BLE_GAP_CONN_MODE_UND : BLE_GAP_CONN_MODE_NON; if (directed_to != NULL) { @@ -650,7 +655,7 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, } } rc = ble_gap_adv_start(own_addr_type, directed_to != NULL ? &peer: NULL, - timeout / 10, + timeout_ms, &adv_params, _advertising_event, self); #endif @@ -694,11 +699,9 @@ void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool mp_raise_NotImplementedError(NULL); } - if (!timeout) { - timeout = BLE_HS_FOREVER; - } else if (timeout > INT32_MAX) { + if ((uint64_t)timeout * 1000ll >= BLE_HS_FOREVER) { mp_raise_bleio_BluetoothError(MP_ERROR_TEXT("Timeout is too long: Maximum timeout length is %d seconds"), - INT32_MAX / 1000); + BLE_HS_FOREVER / 1000 - 1); } CHECK_NIMBLE_ERROR(_common_hal_bleio_adapter_start_advertising(self, connectable, anonymous, timeout, interval, diff --git a/ports/nordic/common-hal/_bleio/Adapter.c b/ports/nordic/common-hal/_bleio/Adapter.c index 82d56b9c1b811..46db2ac488c59 100644 --- a/ports/nordic/common-hal/_bleio/Adapter.c +++ b/ports/nordic/common-hal/_bleio/Adapter.c @@ -742,6 +742,13 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, common_hal_bleio_adapter_stop_advertising(self); } + // A zero timeout means unlimited. Do the checking here + // rather than in common_hal_bleio_adapter_start_advertising(), because + // _common_hal_bleio_adapter_start_advertising() is called for BLE workflow with + // a zero (unlimited) timeout. + if (timeout == 0) { + timeout = BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED; + } uint32_t err_code; bool extended = advertising_data_len > BLE_GAP_ADV_SET_DATA_SIZE_MAX || scan_response_data_len > BLE_GAP_ADV_SET_DATA_SIZE_MAX; @@ -871,15 +878,11 @@ void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool // Anonymous mode requires a timeout so that we don't continue to broadcast // the same data while cycling the MAC address -- otherwise, what's the // point of randomizing the MAC address? - if (!timeout) { - if (anonymous) { - // The Nordic macro is in units of 10ms. Convert to seconds. - uint32_t adv_timeout_max_secs = UNITS_TO_SEC(BLE_GAP_ADV_TIMEOUT_LIMITED_MAX, UNIT_10_MS); - uint32_t rotate_timeout_max_secs = BLE_GAP_DEFAULT_PRIVATE_ADDR_CYCLE_INTERVAL_S; - timeout = MIN(adv_timeout_max_secs, rotate_timeout_max_secs); - } else { - timeout = BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED; - } + if (anonymous) { + // The Nordic macro is in units of 10ms. Convert to seconds. + uint32_t adv_timeout_max_secs = UNITS_TO_SEC(BLE_GAP_ADV_TIMEOUT_LIMITED_MAX, UNIT_10_MS); + uint32_t rotate_timeout_max_secs = BLE_GAP_DEFAULT_PRIVATE_ADDR_CYCLE_INTERVAL_S; + timeout = MIN(adv_timeout_max_secs, rotate_timeout_max_secs); } else { if (SEC_TO_UNITS(timeout, UNIT_10_MS) > BLE_GAP_ADV_TIMEOUT_LIMITED_MAX) { mp_raise_bleio_BluetoothError(MP_ERROR_TEXT("Timeout is too long: Maximum timeout length is %d seconds"), diff --git a/supervisor/shared/bluetooth/bluetooth.c b/supervisor/shared/bluetooth/bluetooth.c index 48b186bbdf12d..68b43871ce161 100644 --- a/supervisor/shared/bluetooth/bluetooth.c +++ b/supervisor/shared/bluetooth/bluetooth.c @@ -132,8 +132,8 @@ static void supervisor_bluetooth_start_advertising(void) { return; } #endif - uint32_t timeout = 0; - float interval = 0.1f; + const uint32_t timeout = 0; // 0 means advertise forever. + const float interval = 0.1f; int tx_power = 0; const uint8_t *adv = private_advertising_data; size_t adv_len = sizeof(private_advertising_data);