Skip to content

Commit da40564

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents eb3632b + 1188d67 commit da40564

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

ports/espressif/common-hal/_bleio/Adapter.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,6 @@ static int _advertising_event(struct ble_gap_event *event, void *self_in) {
528528
#endif
529529
break;
530530
}
531-
background_callback_add_core(&bleio_background_callback);
532531
return 0;
533532
}
534533

@@ -559,7 +558,13 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
559558
return rc;
560559
}
561560

562-
bool high_duty_directed = directed_to != NULL && interval <= 3.5 && timeout <= 1.3;
561+
bool high_duty_directed = directed_to != NULL && interval <= 3.5 && timeout <= 1; // Really 1.3, but it's an int
562+
563+
uint32_t timeout_ms = timeout * 1000;
564+
if (timeout_ms == 0) {
565+
timeout_ms = BLE_HS_FOREVER;
566+
}
567+
563568

564569
#if MYNEWT_VAL(BLE_EXT_ADV)
565570
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,
621626
}
622627
}
623628

624-
rc = ble_gap_ext_adv_start(0, timeout / 10, 0);
629+
rc = ble_gap_ext_adv_start(0, timeout_ms, 0);
625630
#else
626631
uint8_t conn_mode = connectable ? BLE_GAP_CONN_MODE_UND : BLE_GAP_CONN_MODE_NON;
627632
if (directed_to != NULL) {
@@ -650,7 +655,7 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
650655
}
651656
}
652657
rc = ble_gap_adv_start(own_addr_type, directed_to != NULL ? &peer: NULL,
653-
timeout / 10,
658+
timeout_ms,
654659
&adv_params,
655660
_advertising_event, self);
656661
#endif
@@ -694,11 +699,9 @@ void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool
694699
mp_raise_NotImplementedError(NULL);
695700
}
696701

697-
if (!timeout) {
698-
timeout = BLE_HS_FOREVER;
699-
} else if (timeout > INT32_MAX) {
702+
if ((uint64_t)timeout * 1000ll >= BLE_HS_FOREVER) {
700703
mp_raise_bleio_BluetoothError(MP_ERROR_TEXT("Timeout is too long: Maximum timeout length is %d seconds"),
701-
INT32_MAX / 1000);
704+
BLE_HS_FOREVER / 1000 - 1);
702705
}
703706

704707
CHECK_NIMBLE_ERROR(_common_hal_bleio_adapter_start_advertising(self, connectable, anonymous, timeout, interval,

ports/nordic/common-hal/_bleio/Adapter.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,13 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
742742
common_hal_bleio_adapter_stop_advertising(self);
743743
}
744744

745+
// A zero timeout means unlimited. Do the checking here
746+
// rather than in common_hal_bleio_adapter_start_advertising(), because
747+
// _common_hal_bleio_adapter_start_advertising() is called for BLE workflow with
748+
// a zero (unlimited) timeout.
749+
if (timeout == 0) {
750+
timeout = BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED;
751+
}
745752
uint32_t err_code;
746753
bool extended = advertising_data_len > BLE_GAP_ADV_SET_DATA_SIZE_MAX ||
747754
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
871878
// Anonymous mode requires a timeout so that we don't continue to broadcast
872879
// the same data while cycling the MAC address -- otherwise, what's the
873880
// point of randomizing the MAC address?
874-
if (!timeout) {
875-
if (anonymous) {
876-
// The Nordic macro is in units of 10ms. Convert to seconds.
877-
uint32_t adv_timeout_max_secs = UNITS_TO_SEC(BLE_GAP_ADV_TIMEOUT_LIMITED_MAX, UNIT_10_MS);
878-
uint32_t rotate_timeout_max_secs = BLE_GAP_DEFAULT_PRIVATE_ADDR_CYCLE_INTERVAL_S;
879-
timeout = MIN(adv_timeout_max_secs, rotate_timeout_max_secs);
880-
} else {
881-
timeout = BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED;
882-
}
881+
if (anonymous) {
882+
// The Nordic macro is in units of 10ms. Convert to seconds.
883+
uint32_t adv_timeout_max_secs = UNITS_TO_SEC(BLE_GAP_ADV_TIMEOUT_LIMITED_MAX, UNIT_10_MS);
884+
uint32_t rotate_timeout_max_secs = BLE_GAP_DEFAULT_PRIVATE_ADDR_CYCLE_INTERVAL_S;
885+
timeout = MIN(adv_timeout_max_secs, rotate_timeout_max_secs);
883886
} else {
884887
if (SEC_TO_UNITS(timeout, UNIT_10_MS) > BLE_GAP_ADV_TIMEOUT_LIMITED_MAX) {
885888
mp_raise_bleio_BluetoothError(MP_ERROR_TEXT("Timeout is too long: Maximum timeout length is %d seconds"),

supervisor/shared/bluetooth/bluetooth.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ static void supervisor_bluetooth_start_advertising(void) {
132132
return;
133133
}
134134
#endif
135-
uint32_t timeout = 0;
136-
float interval = 0.1f;
135+
const uint32_t timeout = 0; // 0 means advertise forever.
136+
const float interval = 0.1f;
137137
int tx_power = 0;
138138
const uint8_t *adv = private_advertising_data;
139139
size_t adv_len = sizeof(private_advertising_data);

0 commit comments

Comments
 (0)