Skip to content

Commit d55fa46

Browse files
authored
Merge pull request #11036 from dhalbert/espressif-ble-fixes
Espressif BLE fixes: advertising duration, _bleio.adapter.connected
2 parents afcddee + bf1059b commit d55fa46

3 files changed

Lines changed: 19 additions & 17 deletions

File tree

ports/espressif/Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -843,16 +843,18 @@ ifeq ($(ENABLE_JTAG), 1)
843843
CFLAGS += -DENABLE_JTAG=1
844844
endif
845845

846-
SDKCONFIGS := esp-idf-config/sdkconfig.defaults;$(DEBUG_SDKCONFIG);$(FLASH_SIZE_SDKCONFIG);$(FLASH_MODE_SDKCONFIG);$(FLASH_SPEED_SDKCONFIG);$(PSRAM_SDKCONFIG);$(PSRAM_SIZE_SDKCONFIG);$(PSRAM_MODE_SDKCONFIG);$(PSRAM_SPEED_SDKCONFIG);$(TARGET_SDKCONFIG);boards/$(BOARD)/sdkconfig
847846
ifneq ($(CIRCUITPY_BLEIO_NATIVE),0)
848-
SDKCONFIGS := esp-idf-config/sdkconfig-ble.defaults;$(SDKCONFIGS)
847+
BLE_SDKCONFIG := ;esp-idf-config/sdkconfig-ble.defaults
849848
endif
849+
850+
SDKCONFIGS := esp-idf-config/sdkconfig.defaults;$(DEBUG_SDKCONFIG);$(FLASH_SIZE_SDKCONFIG);$(FLASH_MODE_SDKCONFIG);$(FLASH_SPEED_SDKCONFIG);$(PSRAM_SDKCONFIG);$(PSRAM_SIZE_SDKCONFIG);$(PSRAM_MODE_SDKCONFIG);$(PSRAM_SPEED_SDKCONFIG);$(BLE_SDKCONFIG);$(TARGET_SDKCONFIG);boards/$(BOARD)/sdkconfig
851+
850852
# create the config headers
851853
.PHONY: do-sdkconfig
852854
do-sdkconfig: $(BUILD)/esp-idf/config/sdkconfig.h
853855
QSTR_GLOBAL_REQUIREMENTS += $(BUILD)/esp-idf/config/sdkconfig.h
854856
$(BUILD)/esp-idf/config/sdkconfig.h: boards/$(BOARD)/sdkconfig boards/$(BOARD)/mpconfigboard.mk CMakeLists.txt | $(BUILD)/esp-idf
855-
$(STEPECHO) "LINK $@"
857+
$(STEPECHO) "Create $@"
856858
$(Q)env IDF_PATH=$(IDF_PATH) IDF_COMPONENT_MANAGER=0 cmake -S . -B $(BUILD)/esp-idf -DSDKCONFIG=$(BUILD)/esp-idf/sdkconfig -DSDKCONFIG_DEFAULTS="$(SDKCONFIGS)" -DCMAKE_TOOLCHAIN_FILE=$(IDF_PATH)/tools/cmake/toolchain-$(IDF_TARGET).cmake -DIDF_TARGET=$(IDF_TARGET) -GNinja
857859
$(Q)$(PYTHON) tools/check-sdkconfig.py \
858860
CIRCUITPY_DUALBANK=$(CIRCUITPY_DUALBANK) \

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -561,10 +561,6 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
561561
bool high_duty_directed = directed_to != NULL && interval <= 3.5 && timeout <= 1; // Really 1.3, but it's an int
562562

563563
uint32_t timeout_ms = timeout * 1000;
564-
if (timeout_ms == 0) {
565-
timeout_ms = BLE_HS_FOREVER;
566-
}
567-
568564

569565
#if MYNEWT_VAL(BLE_EXT_ADV)
570566
bool extended = advertising_data_len > BLE_ADV_LEGACY_DATA_MAX_LEN ||
@@ -626,8 +622,12 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
626622
}
627623
}
628624

625+
// If timeout_ms is zero, it means advertise forever. This is different than ble_gap_adv_start().
629626
rc = ble_gap_ext_adv_start(0, timeout_ms, 0);
627+
630628
#else
629+
// Extended advertising not enabled.
630+
631631
uint8_t conn_mode = connectable ? BLE_GAP_CONN_MODE_UND : BLE_GAP_CONN_MODE_NON;
632632
if (directed_to != NULL) {
633633
conn_mode = BLE_GAP_CONN_MODE_DIR;
@@ -654,8 +654,10 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
654654
return rc;
655655
}
656656
}
657-
rc = ble_gap_adv_start(own_addr_type, directed_to != NULL ? &peer: NULL,
658-
timeout_ms,
657+
// If timeout_ms is BLE_HS_FOREVER, it means advertise forever. This is different than ble_gap_ext_adv_start().
658+
rc = ble_gap_adv_start(own_addr_type,
659+
directed_to != NULL ? &peer: NULL,
660+
timeout_ms == 0 ? BLE_HS_FOREVER : timeout_ms,
659661
&adv_params,
660662
_advertising_event, self);
661663
#endif
@@ -739,7 +741,7 @@ bool common_hal_bleio_adapter_get_advertising(bleio_adapter_obj_t *self) {
739741
bool common_hal_bleio_adapter_get_connected(bleio_adapter_obj_t *self) {
740742
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
741743
bleio_connection_internal_t *connection = &bleio_connections[i];
742-
if (connection->conn_handle != BLEIO_HANDLE_INVALID && connection->mtu != 0) {
744+
if (connection->conn_handle != BLEIO_HANDLE_INVALID) {
743745
return true;
744746
}
745747
}
@@ -754,7 +756,7 @@ mp_obj_t common_hal_bleio_adapter_get_connections(bleio_adapter_obj_t *self) {
754756
mp_obj_t items[BLEIO_TOTAL_CONNECTION_COUNT];
755757
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
756758
bleio_connection_internal_t *connection = &bleio_connections[i];
757-
if (connection->conn_handle != BLEIO_HANDLE_INVALID && connection->mtu != 0) {
759+
if (connection->conn_handle != BLEIO_HANDLE_INVALID) {
758760
if (connection->connection_obj == mp_const_none) {
759761
connection->connection_obj = bleio_connection_new_from_internal(connection);
760762
}
@@ -818,9 +820,7 @@ void bleio_adapter_gc_collect(bleio_adapter_obj_t *adapter) {
818820

819821
void bleio_adapter_reset(bleio_adapter_obj_t *adapter) {
820822
common_hal_bleio_adapter_stop_scan(adapter);
821-
if (common_hal_bleio_adapter_get_advertising(adapter)) {
822-
common_hal_bleio_adapter_stop_advertising(adapter);
823-
}
823+
common_hal_bleio_adapter_stop_advertising(adapter);
824824

825825
adapter->connection_objs = NULL;
826826
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ static uint64_t _timeout_start_time;
3232
background_callback_t bleio_background_callback;
3333

3434
void bleio_user_reset(void) {
35-
// Stop any user scanning or advertising.
36-
common_hal_bleio_adapter_stop_scan(&common_hal_bleio_adapter_obj);
37-
common_hal_bleio_adapter_stop_advertising(&common_hal_bleio_adapter_obj);
35+
// Stop any user scanning or advertising, and stop all connections.
36+
// TODO: Don't stop BLE workflow connection.
37+
bleio_adapter_reset(&common_hal_bleio_adapter_obj);
3838

3939
ble_event_remove_heap_handlers();
4040

0 commit comments

Comments
 (0)