diff --git a/components/bt/controller/esp32c3/Kconfig.in b/components/bt/controller/esp32c3/Kconfig.in index 6465e9236f..62f4ee0047 100644 --- a/components/bt/controller/esp32c3/Kconfig.in +++ b/components/bt/controller/esp32c3/Kconfig.in @@ -134,7 +134,7 @@ config BT_CTRL_RX_ANTENNA_INDEX_EFF choice BT_CTRL_DFT_TX_POWER_LEVEL prompt "BLE default Tx power level" - default BT_CTRL_DFT_TX_POWER_LEVEL_P3 + default BT_CTRL_DFT_TX_POWER_LEVEL_P9 help Specify default Tx power level @@ -413,3 +413,11 @@ config BT_CTRL_CODED_AGC_RECORRECT_EN default n help Enable coded phy AGC recorrect + +config BT_CTRL_SCAN_BACKOFF_UPPERLIMITMAX + bool "Disable active scan backoff" + default n + help + Disable active scan backoff. The bluetooth spec requires that scanners should run a backoff procedure to + minimize collision of scan request PDUs from nultiple scanners. If scan backoff is disabled, in active + scanning, scan request PDU will be sent every time when HW receives scannable ADV PDU. diff --git a/components/bt/controller/esp32c3/bt.c b/components/bt/controller/esp32c3/bt.c index 5f08f7b8c2..b6c21c639d 100644 --- a/components/bt/controller/esp32c3/bt.c +++ b/components/bt/controller/esp32c3/bt.c @@ -249,24 +249,18 @@ extern void esp_mac_bb_power_up(void); extern void ets_backup_dma_copy(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_mem); #endif -extern char _bss_start_btdm; -extern char _bss_end_btdm; -extern char _data_start_btdm; -extern char _data_end_btdm; -extern uint32_t _data_start_btdm_rom; -extern uint32_t _data_end_btdm_rom; - extern uint32_t _bt_bss_start; extern uint32_t _bt_bss_end; extern uint32_t _btdm_bss_start; extern uint32_t _btdm_bss_end; +extern uint32_t _nimble_bss_start; +extern uint32_t _nimble_bss_end; extern uint32_t _bt_data_start; extern uint32_t _bt_data_end; extern uint32_t _btdm_data_start; extern uint32_t _btdm_data_end; - -extern char _bt_tmp_bss_start; -extern char _bt_tmp_bss_end; +extern uint32_t _nimble_data_start; +extern uint32_t _nimble_data_end; /* Local Function Declare ********************************************************************* @@ -316,6 +310,9 @@ static void btdm_hw_mac_power_down_wrapper(void); static void btdm_backup_dma_copy_wrapper(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_mem); static void btdm_slp_tmr_callback(void *arg); + +static esp_err_t try_heap_caps_add_region(intptr_t start, intptr_t end); + /* Local variable definition *************************************************************************** */ @@ -876,13 +873,142 @@ static void btdm_controller_mem_init(void) esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode) { - ESP_LOGW(BTDM_LOG_TAG, "%s not implemented, return OK", __func__); + intptr_t mem_start=(intptr_t) NULL, mem_end=(intptr_t) NULL; + if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) { + return ESP_ERR_INVALID_STATE; + } + + if (mode & ESP_BT_MODE_BLE) { + /* if the addresses of rom btdm .data and .bss are consecutive, + they are registered in the system heap as a piece of memory + */ + if(ets_rom_layout_p->data_end_btdm == ets_rom_layout_p->bss_start_btdm) { + mem_start = (intptr_t)ets_rom_layout_p->data_start_btdm; + mem_end = (intptr_t)ets_rom_layout_p->bss_end_btdm; + if (mem_start != mem_end) { + ESP_LOGD(BTDM_LOG_TAG, "Release rom btdm [0x%08x] - [0x%08x], len %d", mem_start, mem_end, mem_end - mem_start); + ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + } + } else { + mem_start = (intptr_t)ets_rom_layout_p->bss_start_btdm; + mem_end = (intptr_t)ets_rom_layout_p->bss_end_btdm; + if (mem_start != mem_end) { + ESP_LOGD(BTDM_LOG_TAG, "Release rom btdm BSS [0x%08x] - [0x%08x], len %d", mem_start, mem_end, mem_end - mem_start); + ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + } + + mem_start = (intptr_t)ets_rom_layout_p->data_start_btdm; + mem_end = (intptr_t)ets_rom_layout_p->data_end_btdm; + if (mem_start != mem_end) { + ESP_LOGD(BTDM_LOG_TAG, "Release rom btdm Data [0x%08x] - [0x%08x], len %d", mem_start, mem_end, mem_end - mem_start); + ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + } + } + /* if the addresses of rom interface btdm .data and .bss are consecutive, + they are registered in the system heap as a piece of memory + */ + if(ets_rom_layout_p->data_end_interface_btdm == ets_rom_layout_p->bss_start_interface_btdm) { + mem_start = (intptr_t)ets_rom_layout_p->data_start_interface_btdm; + mem_end = (intptr_t)ets_rom_layout_p->bss_end_interface_btdm; + if (mem_start != mem_end) { + ESP_LOGD(BTDM_LOG_TAG, "Release rom interface btdm [0x%08x] - [0x%08x], len %d", mem_start, mem_end, mem_end - mem_start); + ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + } + } else { + mem_start = (intptr_t)ets_rom_layout_p->data_start_interface_btdm; + mem_end = (intptr_t)ets_rom_layout_p->data_end_interface_btdm; + if (mem_start != mem_end) { + ESP_LOGD(BTDM_LOG_TAG, "Release rom interface btdm Data [0x%08x] - [0x%08x], len %d", mem_start, mem_end, mem_end - mem_start); + ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + } + + mem_start = (intptr_t)ets_rom_layout_p->bss_start_interface_btdm; + mem_end = (intptr_t)ets_rom_layout_p->bss_end_interface_btdm; + if (mem_start != mem_end) { + ESP_LOGD(BTDM_LOG_TAG, "Release rom interface btdm BSS [0x%08x] - [0x%08x], len %d", mem_start, mem_end, mem_end - mem_start); + ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + } + } + + } return ESP_OK; } esp_err_t esp_bt_mem_release(esp_bt_mode_t mode) { - ESP_LOGW(BTDM_LOG_TAG, "%s not implemented, return OK", __func__); + int ret; + intptr_t mem_start, mem_end; + + ret = esp_bt_controller_mem_release(mode); + if (ret != ESP_OK) { + return ret; + } + + if (mode & ESP_BT_MODE_BLE) { + /* if the addresses of btdm .bss and bt .bss are consecutive, + they are registered in the system heap as a piece of memory + */ + if(_bt_bss_end == _btdm_bss_start) { + mem_start = (intptr_t)&_bt_bss_start; + mem_end = (intptr_t)&_btdm_bss_end; + if (mem_start != mem_end) { + ESP_LOGD(BTDM_LOG_TAG, "Release BSS [0x%08x] - [0x%08x], len %d", mem_start, mem_end, mem_end - mem_start); + ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + } + } else { + mem_start = (intptr_t)&_bt_bss_start; + mem_end = (intptr_t)&_bt_bss_end; + if (mem_start != mem_end) { + ESP_LOGD(BTDM_LOG_TAG, "Release BT BSS [0x%08x] - [0x%08x], len %d", mem_start, mem_end, mem_end - mem_start); + ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + } + + mem_start = (intptr_t)&_btdm_bss_start; + mem_end = (intptr_t)&_btdm_bss_end; + if (mem_start != mem_end) { + ESP_LOGD(BTDM_LOG_TAG, "Release BTDM BSS [0x%08x] - [0x%08x], len %d", mem_start, mem_end, mem_end - mem_start); + ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + } + } + /* if the addresses of btdm .data and bt .data are consecutive, + they are registered in the system heap as a piece of memory + */ + if(_bt_data_end == _btdm_data_start) { + mem_start = (intptr_t)&_bt_data_start; + mem_end = (intptr_t)&_btdm_data_end; + if (mem_start != mem_end) { + ESP_LOGD(BTDM_LOG_TAG, "Release data [0x%08x] - [0x%08x], len %d", mem_start, mem_end, mem_end - mem_start); + ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + } + } else { + mem_start = (intptr_t)&_bt_data_start; + mem_end = (intptr_t)&_bt_data_end; + if (mem_start != mem_end) { + ESP_LOGD(BTDM_LOG_TAG, "Release BT Data [0x%08x] - [0x%08x], len %d", mem_start, mem_end, mem_end - mem_start); + ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + } + + mem_start = (intptr_t)&_btdm_data_start; + mem_end = (intptr_t)&_btdm_data_end; + if (mem_start != mem_end) { + ESP_LOGD(BTDM_LOG_TAG, "Release BTDM Data [0x%08x] - [0x%08x], len %d", mem_start, mem_end, mem_end - mem_start); + ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + } + } + + mem_start = (intptr_t)&_nimble_bss_start; + mem_end = (intptr_t)&_nimble_bss_end; + if (mem_start != mem_end) { + ESP_LOGD(BTDM_LOG_TAG, "Release NimBLE BSS [0x%08x] - [0x%08x], len %d", mem_start, mem_end, mem_end - mem_start); + ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + } + mem_start = (intptr_t)&_nimble_data_start; + mem_end = (intptr_t)&_nimble_data_end; + if (mem_start != mem_end) { + ESP_LOGD(BTDM_LOG_TAG, "Release NimBLE Data [0x%08x] - [0x%08x], len %d", mem_start, mem_end, mem_end - mem_start); + ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + } + } return ESP_OK; } diff --git a/components/bt/controller/lib_esp32c3_family b/components/bt/controller/lib_esp32c3_family index 25bc757fd0..5031203040 160000 --- a/components/bt/controller/lib_esp32c3_family +++ b/components/bt/controller/lib_esp32c3_family @@ -1 +1 @@ -Subproject commit 25bc757fd0933e2d5dd448ca29db3d3136debe78 +Subproject commit 5031203040da20073bb5c9f32f3444333f30dce7 diff --git a/components/bt/host/bluedroid/bta/dm/bta_dm_api.c b/components/bt/host/bluedroid/bta/dm/bta_dm_api.c index 5507839f6f..b22bf0af5d 100644 --- a/components/bt/host/bluedroid/bta/dm/bta_dm_api.c +++ b/components/bt/host/bluedroid/bta/dm/bta_dm_api.c @@ -666,6 +666,7 @@ void BTA_DmOobReply(BD_ADDR bd_addr, UINT8 len, UINT8 *p_value) if ((p_msg = (tBTA_DM_API_OOB_REPLY *) osi_malloc(sizeof(tBTA_DM_API_OOB_REPLY))) != NULL) { p_msg->hdr.event = BTA_DM_API_OOB_REPLY_EVT; if(p_value == NULL || len > BT_OCTET16_LEN) { + osi_free(p_msg); return; } memcpy(p_msg->bd_addr, bd_addr, BD_ADDR_LEN); diff --git a/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c b/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c index acac42e4d5..071a388f5d 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c @@ -1103,12 +1103,6 @@ void bta_gattc_read(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) /* read fail */ if (status != BTA_GATT_OK) { - /* Dequeue the data, if it was enqueued */ - if (p_clcb->p_q_cmd == p_data) { - p_clcb->p_q_cmd = NULL; - bta_gattc_pop_command_to_send(p_clcb); - } - bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status, NULL); } } @@ -1138,12 +1132,6 @@ void bta_gattc_read_by_type(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) /* read fail */ if (status != BTA_GATT_OK) { - /* Dequeue the data, if it was enqueued */ - if (p_clcb->p_q_cmd == p_data) { - p_clcb->p_q_cmd = NULL; - bta_gattc_pop_command_to_send(p_clcb); - } - bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status, NULL); } } @@ -1174,12 +1162,6 @@ void bta_gattc_read_multi(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) /* read fail */ if (status != BTA_GATT_OK) { - /* Dequeue the data, if it was enqueued */ - if (p_clcb->p_q_cmd == p_data) { - p_clcb->p_q_cmd = NULL; - bta_gattc_pop_command_to_send(p_clcb); - } - bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status, NULL); } } @@ -1216,13 +1198,12 @@ void bta_gattc_write(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) /* write fail */ if (status != BTA_GATT_OK) { - /* Dequeue the data, if it was enqueued */ - if (p_clcb->p_q_cmd == p_data) { - p_clcb->p_q_cmd = NULL; - bta_gattc_pop_command_to_send(p_clcb); - } - bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_WRITE, status, NULL); + tGATT_CL_COMPLETE cl_data = {0}; + cl_data.handle = p_data->api_write.handle; + memcpy(&cl_data.att_value, &attr, sizeof(tGATT_VALUE)); + + bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_WRITE, status, &cl_data); } } /******************************************************************************* diff --git a/components/bt/host/bluedroid/bta/gatt/bta_gattc_cache.c b/components/bt/host/bluedroid/bta/gatt/bta_gattc_cache.c index d8af854525..fb48cb9cfa 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gattc_cache.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gattc_cache.c @@ -285,6 +285,12 @@ static tBTA_GATT_STATUS bta_gattc_add_srvc_to_cache(tBTA_GATTC_SERV *p_srvc_cb, p_srvc_cb->p_srvc_cache = list_new(service_free); } + if(!p_srvc_cb->p_srvc_cache) { + APPL_TRACE_WARNING("%s(), no resource.", __func__); + osi_free(p_new_srvc); + return BTA_GATT_NO_RESOURCES; + } + if(is_primary) { list_append(p_srvc_cb->p_srvc_cache, p_new_srvc); } else { @@ -549,7 +555,7 @@ void bta_gattc_update_include_service(const list_t *services) { } for (list_node_t *sn = list_begin(services); sn != list_end(services); sn = list_next(sn)) { tBTA_GATTC_SERVICE *service = list_node(sn); - if(!service && list_is_empty(service->included_svc)) break; + if(!service || !service->included_svc || list_is_empty(service->included_svc)) break; for (list_node_t *sn = list_begin(service->included_svc); sn != list_end(service->included_svc); sn = list_next(sn)) { tBTA_GATTC_INCLUDED_SVC *include_service = list_node(sn); if(include_service && !include_service->included_service) { diff --git a/components/bt/host/bluedroid/bta/gatt/bta_gattc_co.c b/components/bt/host/bluedroid/bta/gatt/bta_gattc_co.c index dfac21b568..c5f17d79eb 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gattc_co.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gattc_co.c @@ -583,6 +583,9 @@ BOOLEAN bta_gattc_co_cache_append_assoc_addr(BD_ADDR src_addr, BD_ADDR assoc_add UINT8 addr_index = 0; cache_addr_info_t *addr_info; UINT8 *p_assoc_buf = osi_malloc(sizeof(BD_ADDR)); + if(!p_assoc_buf) { + return FALSE; + } memcpy(p_assoc_buf, assoc_addr, sizeof(BD_ADDR)); if ((addr_index = bta_gattc_co_find_addr_in_cache(src_addr)) != INVALID_ADDR_NUM) { addr_info = &cache_env->cache_addr[addr_index]; @@ -590,6 +593,8 @@ BOOLEAN bta_gattc_co_cache_append_assoc_addr(BD_ADDR src_addr, BD_ADDR assoc_add addr_info->assoc_addr =list_new(NULL); } return list_append(addr_info->assoc_addr, p_assoc_buf); + } else { + osi_free(p_assoc_buf); } return FALSE; diff --git a/components/bt/host/bluedroid/stack/btm/btm_sec.c b/components/bt/host/bluedroid/stack/btm/btm_sec.c index 18ed1facdc..7e189aa4f8 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_sec.c +++ b/components/bt/host/bluedroid/stack/btm/btm_sec.c @@ -3167,6 +3167,10 @@ void btm_sec_rmt_name_request_complete (UINT8 *p_bd_addr, UINT8 *p_bd_name, UINT } } + if(!p_dev_rec) { + return; + } + /* If this is a bonding procedure can disconnect the link now */ if ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) && (p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED)) { @@ -3950,6 +3954,10 @@ void btm_sec_auth_complete (UINT16 handle, UINT8 status) } } + if(!p_dev_rec) { + return; + } + p_dev_rec->sec_state = BTM_SEC_STATE_IDLE; #if (CLASSIC_BT_INCLUDED == TRUE) @@ -4793,6 +4801,10 @@ void btm_sec_link_key_notification (UINT8 *p_bda, UINT8 *p_link_key, UINT8 key_t } } + if(!p_dev_rec) { + return; + } + /* We will save link key only if the user authorized it - BTE report link key in all cases */ #ifdef BRCM_NONE_BTE if (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_AUTHED) diff --git a/components/bt/host/bluedroid/stack/gatt/gatt_db.c b/components/bt/host/bluedroid/stack/gatt/gatt_db.c index 0c2f0d8f0b..2f78f26d32 100644 --- a/components/bt/host/bluedroid/stack/gatt/gatt_db.c +++ b/components/bt/host/bluedroid/stack/gatt/gatt_db.c @@ -976,7 +976,7 @@ tGATT_STATUS gatts_write_attr_value_by_handle(tGATT_SVC_DB *p_db, memcpy(p_attr->p_value->attr_val.attr_val + offset, p_value, len); p_attr->p_value->attr_val.attr_len = len + offset; return GATT_SUCCESS; - } else if (p_attr->p_value->attr_val.attr_max_len < offset + len){ + } else if (p_attr->p_value && p_attr->p_value->attr_val.attr_max_len < offset + len){ GATT_TRACE_DEBUG("Remote device try to write with a length larger then attribute's max length\n"); return GATT_INVALID_ATTR_LEN; } else if ((p_attr->p_value == NULL) || (p_attr->p_value->attr_val.attr_val == NULL)){ diff --git a/components/bt/include/esp32c3/include/esp_bt.h b/components/bt/include/esp32c3/include/esp_bt.h index ccd3c146a4..0b9cae835b 100644 --- a/components/bt/include/esp32c3/include/esp_bt.h +++ b/components/bt/include/esp32c3/include/esp_bt.h @@ -18,7 +18,7 @@ extern "C" { #endif #define ESP_BT_CTRL_CONFIG_MAGIC_VAL 0x5A5AA5A5 -#define ESP_BT_CTRL_CONFIG_VERSION 0x02112280 +#define ESP_BT_CTRL_CONFIG_VERSION 0x02209230 #define ESP_BT_HCI_TL_MAGIC_VALUE 0xfadebead #define ESP_BT_HCI_TL_VERSION 0x00010000 @@ -143,6 +143,11 @@ typedef void (* esp_bt_hci_tl_callback_t) (void *arg, uint8_t status); #define AGC_RECORRECT_EN ((BT_CTRL_AGC_RECORRECT_EN << 0) | (BT_CTRL_CODED_AGC_RECORRECT <<1)) +#ifdef CONFIG_BT_CTRL_SCAN_BACKOFF_UPPERLIMITMAX +#define BT_CTRL_SCAN_BACKOFF_UPPERLIMITMAX CONFIG_BT_CTRL_SCAN_BACKOFF_UPPERLIMITMAX +#else +#define BT_CTRL_SCAN_BACKOFF_UPPERLIMITMAX 0 +#endif #define CFG_MASK_BIT_SCAN_DUPLICATE_OPTION (1<<0) @@ -181,6 +186,7 @@ typedef void (* esp_bt_hci_tl_callback_t) (void *arg, uint8_t status); .slave_ce_len_min = SLAVE_CE_LEN_MIN_DEFAULT, \ .hw_recorrect_en = AGC_RECORRECT_EN, \ .cca_thresh = CONFIG_BT_CTRL_HW_CCA_VAL, \ + .scan_backoff_upperlimitmax = BT_CTRL_SCAN_BACKOFF_UPPERLIMITMAX, \ } #else @@ -248,6 +254,7 @@ typedef struct { uint8_t slave_ce_len_min; uint8_t hw_recorrect_en; uint8_t cca_thresh; /*!< cca threshold*/ + uint16_t scan_backoff_upperlimitmax; /*!< scan backoff upperlimitmax value */ } esp_bt_controller_config_t; /** diff --git a/examples/bluetooth/bluedroid/ble/ble_compatibility_test/main/ble_compatibility_test.c b/examples/bluetooth/bluedroid/ble/ble_compatibility_test/main/ble_compatibility_test.c index 55b5def4ab..dc4dc278fe 100644 --- a/examples/bluetooth/bluedroid/ble/ble_compatibility_test/main/ble_compatibility_test.c +++ b/examples/bluetooth/bluedroid/ble/ble_compatibility_test/main/ble_compatibility_test.c @@ -627,8 +627,6 @@ void app_main(void) } ESP_ERROR_CHECK( ret ); - ESP_ERROR_CHECK(nvs_flash_erase()); - ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT)); esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();