Fixed memory leak when RAM free size is insufficient or setting ext scan parameters failed on ESP32-C2

This commit is contained in:
zwl
2022-11-29 16:29:26 +08:00
parent 2f23825f60
commit 1732a6d044
8 changed files with 97 additions and 88 deletions
+27 -8
View File
@@ -60,7 +60,7 @@
#define OSI_COEX_VERSION 0x00010006
#define OSI_COEX_MAGIC_VALUE 0xFADEBEAD
#define EXT_FUNC_VERSION 0x20220125
#define EXT_FUNC_VERSION 0x20221122
#define EXT_FUNC_MAGIC_VALUE 0xA5A5A5A5
#define BT_ASSERT_PRINT ets_printf
@@ -102,6 +102,7 @@ struct ext_funcs_t {
int (* _ecc_gen_key_pair)(uint8_t *public, uint8_t *priv);
int (* _ecc_gen_dh_key)(const uint8_t *remote_pub_key_x, const uint8_t *remote_pub_key_y, const uint8_t *local_priv_key, uint8_t *dhkey);
void (* _esp_reset_rpa_moudle)(void);
void (* _esp_bt_track_pll_cap)(void);
uint32_t magic;
};
@@ -641,6 +642,8 @@ void ble_rtc_clk_init(void)
esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
{
esp_err_t ret = ESP_OK;
if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "invalid controller state");
return ESP_FAIL;
@@ -659,7 +662,6 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
/* Initialize the function pointers for OS porting */
npl_freertos_funcs_init();
struct npl_funcs_t *p_npl_funcs = npl_freertos_funcs_get();
if (!p_npl_funcs) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "npl functions get failed");
@@ -668,18 +670,21 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
if (esp_register_npl_funcs(p_npl_funcs) != 0) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "npl functions register failed");
return ESP_ERR_INVALID_ARG;
ret = ESP_ERR_INVALID_ARG;
goto free_mem;
}
if (npl_freertos_mempool_init() != 0) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "npl mempool init failed");
return ESP_ERR_INVALID_ARG;
ret = ESP_ERR_INVALID_ARG;
goto free_mem;
}
/* Initialize the global memory pool */
if (os_msys_buf_alloc() != 0) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "os msys alloc failed");
return ESP_ERR_INVALID_ARG;
ret = ESP_ERR_INVALID_ARG;
goto free_mem;
}
os_msys_init();
@@ -699,7 +704,8 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
if (ble_osi_coex_funcs_register((struct osi_coex_funcs_t *)&s_osi_coex_funcs_ro) != 0) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "osi coex funcs reg failed");
return ESP_ERR_INVALID_ARG;
ret = ESP_ERR_INVALID_ARG;
goto free_phy;
}
#if CONFIG_SW_COEXIST_ENABLE
@@ -708,7 +714,8 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
int rc = ble_controller_init(cfg);
if (rc != 0) {
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "ble_controller_init failed %d", rc);
return ESP_ERR_NO_MEM;
ret = ESP_ERR_NO_MEM;
goto free_phy;
}
controller_sleep_init();
@@ -724,8 +731,20 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
ble_hci_trans_cfg_hs((ble_hci_trans_rx_cmd_fn *)ble_hci_unregistered_hook,NULL,
(ble_hci_trans_rx_acl_fn *)ble_hci_unregistered_hook,NULL);
return ESP_OK;
free_phy:
esp_phy_disable();
esp_phy_pd_mem_deinit();
#if CONFIG_BT_NIMBLE_ENABLED
ble_npl_eventq_deinit(nimble_port_get_dflt_eventq());
#endif // CONFIG_BT_NIMBLE_ENABLED
free_mem:
os_msys_buf_free();
npl_freertos_mempool_deinit();
esp_unregister_npl_funcs();
npl_freertos_funcs_deinit();
esp_unregister_ext_funcs();
return ret;
}
esp_err_t esp_bt_controller_deinit(void)
@@ -121,7 +121,7 @@ uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem);
* Callouts
*/
void ble_npl_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
int ble_npl_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
ble_npl_event_fn *ev_cb, void *ev_arg);
ble_npl_error_t ble_npl_callout_reset(struct ble_npl_callout *co,
@@ -148,6 +148,10 @@ os_msys_buf_alloc(void)
#if OS_MSYS_2_BLOCK_COUNT > 0
os_msys_init_2_data = (os_membuf_t *)bt_osi_mem_calloc(1, (sizeof(os_membuf_t) * SYSINIT_MSYS_2_MEMPOOL_SIZE));
if (!os_msys_init_2_data) {
#if OS_MSYS_1_BLOCK_COUNT > 0
bt_osi_mem_free(os_msys_init_1_data);
os_msys_init_1_data = NULL;
#endif
return ESP_FAIL;
}
#endif
@@ -102,7 +102,7 @@ struct npl_funcs_t {
ble_npl_error_t (*p_ble_npl_sem_pend)(struct ble_npl_sem *, ble_npl_time_t);
ble_npl_error_t (*p_ble_npl_sem_release)(struct ble_npl_sem *);
uint16_t (*p_ble_npl_sem_get_count)(struct ble_npl_sem *);
void (*p_ble_npl_callout_init)(struct ble_npl_callout *, struct ble_npl_eventq *, ble_npl_event_fn *, void *);
int (*p_ble_npl_callout_init)(struct ble_npl_callout *, struct ble_npl_eventq *, ble_npl_event_fn *, void *);
ble_npl_error_t (*p_ble_npl_callout_reset)(struct ble_npl_callout *, ble_npl_time_t);
void (*p_ble_npl_callout_stop)(struct ble_npl_callout *);
void (*p_ble_npl_callout_deinit)(struct ble_npl_callout *);
@@ -259,12 +259,13 @@ IRAM_ATTR ble_npl_sem_get_count(struct ble_npl_sem *sem)
return npl_funcs->p_ble_npl_sem_get_count(sem);
}
static inline void
static inline int
IRAM_ATTR ble_npl_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
ble_npl_event_fn *ev_cb, void *ev_arg)
{
return npl_funcs->p_ble_npl_callout_init(co, evq, ev_cb, ev_arg);
}
static inline void
IRAM_ATTR ble_npl_callout_deinit(struct ble_npl_callout *co)
{
@@ -72,7 +72,7 @@ ble_npl_error_t npl_freertos_sem_pend(struct ble_npl_sem *sem,
ble_npl_error_t npl_freertos_sem_release(struct ble_npl_sem *sem);
void npl_freertos_callout_init(struct ble_npl_callout *co,
int npl_freertos_callout_init(struct ble_npl_callout *co,
struct ble_npl_eventq *evq,
ble_npl_event_fn *ev_cb, void *ev_arg);
@@ -676,7 +676,7 @@ IRAM_ATTR os_callout_timer_cb(TimerHandle_t timer)
}
#endif
void
int
npl_freertos_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
ble_npl_event_fn *ev_cb, void *ev_arg)
{
@@ -692,61 +692,82 @@ npl_freertos_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
#if CONFIG_BT_NIMBLE_USE_ESP_TIMER
callout->evq = evq;
callout->evq = evq;
esp_timer_create_args_t create_args = {
.callback = ble_npl_event_fn_wrapper,
.arg = callout,
.name = "nimble_timer"
};
ESP_ERROR_CHECK(esp_timer_create(&create_args, &callout->handle));
esp_timer_create_args_t create_args = {
.callback = ble_npl_event_fn_wrapper,
.arg = callout,
.name = "nimble_timer"
};
if (esp_timer_create(&create_args, &callout->handle) != ESP_OK) {
ble_npl_event_deinit(&callout->ev);
os_memblock_put(&ble_freertos_co_pool,callout);
co->co = NULL;
return -1;
}
#else
callout->handle = xTimerCreate("co", 1, pdFALSE, callout, os_callout_timer_cb);
#endif
callout->handle = xTimerCreate("co", 1, pdFALSE, callout, os_callout_timer_cb);
BLE_LL_ASSERT(callout->handle);
if (!callout->handle) {
ble_npl_event_deinit(&callout->ev);
os_memblock_put(&ble_freertos_co_pool,callout);
co->co = NULL;
return -1;
}
#endif // CONFIG_BT_NIMBLE_USE_ESP_TIMER
} else {
callout = (struct ble_npl_callout_freertos *)co->co;
BLE_LL_ASSERT(callout);
callout->evq = evq;
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
callout = (struct ble_npl_callout_freertos *)co->co;
BLE_LL_ASSERT(callout);
callout->evq = evq;
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
}
#else
if(!co->co) {
co->co = malloc(sizeof(struct ble_npl_callout_freertos));
callout = (struct ble_npl_callout_freertos *)co->co;
BLE_LL_ASSERT(callout);
if (!callout) {
return -1;
}
memset(callout, 0, sizeof(*callout));
memset(callout, 0, sizeof(*callout));
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
#if CONFIG_BT_NIMBLE_USE_ESP_TIMER
callout->evq = evq;
callout->evq = evq;
esp_timer_create_args_t create_args = {
.callback = ble_npl_event_fn_wrapper,
.arg = callout,
.name = "nimble_timer"
};
esp_timer_create_args_t create_args = {
.callback = ble_npl_event_fn_wrapper,
.arg = callout,
.name = "nimble_timer"
};
ESP_ERROR_CHECK(esp_timer_create(&create_args, &callout->handle));
if (esp_timer_create(&create_args, &callout->handle) != ESP_OK) {
ble_npl_event_deinit(&callout->ev);
free((void *)callout);
co->co = NULL;
return -1;
}
#else
callout->handle = xTimerCreate("co", 1, pdFALSE, callout, os_callout_timer_cb);
#endif
callout->handle = xTimerCreate("co", 1, pdFALSE, callout, os_callout_timer_cb);
BLE_LL_ASSERT(callout->handle);
if (!callout->handle) {
ble_npl_event_deinit(&callout->ev);
free((void *)callout);
co->co = NULL;
return -1;
}
#endif // CONFIG_BT_NIMBLE_USE_ESP_TIMER
}
else {
callout = (struct ble_npl_callout_freertos *)co->co;
BLE_LL_ASSERT(callout);
callout->evq = evq;
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
callout->evq = evq;
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
}
#endif
#endif // OS_MEM_ALLOC
return 0;
}
void
@@ -756,11 +777,14 @@ npl_freertos_callout_deinit(struct ble_npl_callout *co)
/* Since we dynamically deinit timers, function can be called for NULL timers. Return for such scenarios */
if (!callout) {
return;
return;
}
BLE_LL_ASSERT(callout->handle);
if (!callout->handle) {
return;
}
ble_npl_event_deinit(&callout->ev);
#if CONFIG_BT_NIMBLE_USE_ESP_TIMER
esp_err_t err = esp_timer_stop(callout->handle);
if(err != ESP_OK) {
@@ -773,17 +797,13 @@ npl_freertos_callout_deinit(struct ble_npl_callout *co)
ESP_LOGW(TAG, "Timer not deleted");
}
#else
xTimerDelete(callout->handle, portMAX_DELAY);
ble_npl_event_deinit(&callout->ev);
#if OS_MEM_ALLOC
os_memblock_put(&ble_freertos_co_pool,callout);
#else
free((void *)callout);
#endif
#endif
#endif // OS_MEM_ALLOC
#endif // CONFIG_BT_NIMBLE_USE_ESP_TIMER
co->co = NULL;
memset(co, 0, sizeof(struct ble_npl_callout));
}