From 76f7c727d59e2be9b59b081b2b6c62da0be300bd Mon Sep 17 00:00:00 2001 From: Yuan Hong Hui Date: Mon, 20 Jun 2022 21:31:10 +0800 Subject: [PATCH] ble_mesh:example:fix memory leak --- .../ble_mesh_console/main/ble_mesh_adapter.c | 21 +++++++++--- .../ble_mesh_console/main/ble_mesh_adapter.h | 3 +- .../main/ble_mesh_console_main.c | 1 + .../main/ble_mesh_register_cmd.c | 32 +++++++++---------- 4 files changed, 35 insertions(+), 22 deletions(-) diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_adapter.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_adapter.c index 064abfd860..345c9d6193 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_adapter.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_adapter.c @@ -69,7 +69,7 @@ esp_ble_mesh_comp_t *ble_mesh_get_component(uint16_t model_id) return comp; } -void ble_mesh_node_init(void) +int ble_mesh_init_node_prestore_params(void) { uint16_t i; @@ -77,11 +77,14 @@ void ble_mesh_node_init(void) ble_mesh_node_prestore_params[i].net_idx = ESP_BLE_MESH_KEY_UNUSED; ble_mesh_node_prestore_params[i].unicast_addr = ESP_BLE_MESH_ADDR_UNASSIGNED; } - - ble_mesh_node_sema = xSemaphoreCreateMutex(); - if (!ble_mesh_node_sema) { - ESP_LOGE(TAG, "%s init fail, mesh node semaphore create fail", __func__); + if(ble_mesh_node_sema == NULL) { + ble_mesh_node_sema = xSemaphoreCreateMutex(); + if (!ble_mesh_node_sema) { + ESP_LOGE(TAG, "%s init fail, mesh node semaphore create fail", __func__); + return ESP_ERR_NO_MEM; + } } + return 0; } void ble_mesh_set_node_prestore_params(uint16_t netkey_index, uint16_t unicast_addr) @@ -97,6 +100,14 @@ void ble_mesh_set_node_prestore_params(uint16_t netkey_index, uint16_t unicast_a xSemaphoreGive(ble_mesh_node_sema); } +void ble_mesh_deinit_node_prestore_params(void) +{ + if (ble_mesh_node_sema != NULL) { + vSemaphoreDelete(ble_mesh_node_sema); + ble_mesh_node_sema = NULL; + } +} + void ble_mesh_node_statistics_get(void) { xSemaphoreTake(ble_mesh_node_sema, portMAX_DELAY); diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_adapter.h b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_adapter.h index 5746d04470..55fad39633 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_adapter.h +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_adapter.h @@ -107,7 +107,8 @@ extern SemaphoreHandle_t ble_mesh_node_sema; } \ }while(0) \ -void ble_mesh_node_init(void); +int ble_mesh_init_node_prestore_params(void); +void ble_mesh_deinit_node_prestore_params(void); void ble_mesh_set_node_prestore_params(uint16_t netkey_index, uint16_t unicast_addr); esp_ble_mesh_model_t *ble_mesh_get_model(uint16_t model_id); esp_ble_mesh_comp_t *ble_mesh_get_component(uint16_t model_id); diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_console_main.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_console_main.c index 6a5ed970d1..bd99b2f4e9 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_console_main.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_console_main.c @@ -69,6 +69,7 @@ void app_main(void) #endif // init console REPL environment + repl_config.max_history_len = 1; ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &repl)); /* Register commands */ diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_register_cmd.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_register_cmd.c index 6c73b2c490..c5182f0889 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_register_cmd.c +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/main/ble_mesh_register_cmd.c @@ -84,7 +84,7 @@ ble_mesh_node_status node_status = { .previous = 0x0, .current = 0x0, }; -SemaphoreHandle_t ble_mesh_node_sema; +SemaphoreHandle_t ble_mesh_node_sema = NULL; typedef struct { struct arg_str *add_del; @@ -161,7 +161,6 @@ void ble_mesh_register_mesh_node(void) int ble_mesh_register_cb(int argc, char** argv) { ESP_LOGD(TAG, "enter %s\n", __func__); - ble_mesh_node_init(); esp_ble_mesh_register_prov_callback(ble_mesh_prov_cb); esp_ble_mesh_register_custom_model_callback(ble_mesh_model_cb); esp_ble_mesh_register_generic_server_callback(ble_mesh_generic_server_model_cb); @@ -243,6 +242,9 @@ void ble_mesh_prov_cb(esp_ble_mesh_prov_cb_event_t event, esp_ble_mesh_prov_cb_p case ESP_BLE_MESH_PROV_REGISTER_COMP_EVT: ble_mesh_callback_check_err_code(param->prov_register_comp.err_code, "Bm:Init"); break; + case ESP_BLE_MESH_DEINIT_MESH_COMP_EVT: + ble_mesh_callback_check_err_code(param->deinit_mesh_comp.err_code, "Bm:DeInit"); + break; case ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT: ble_mesh_callback_check_err_code(param->node_prov_enable_comp.err_code, "Node:EnBearer"); break; @@ -399,7 +401,7 @@ void ble_mesh_model_cb(esp_ble_mesh_model_cb_event_t event, esp_ble_mesh_model_c outcome = esp_ble_mesh_server_model_send_msg(param->model_operation.model, param->model_operation.ctx, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS, sizeof(status), &status); if (outcome != ESP_OK) { - ESP_LOGE(TAG, "Node:SendMsg,Fal"); + ESP_LOGE(TAG, "Node:SendMsg,Fail"); } } else if (param->model_operation.opcode == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { ble_mesh_node_set_state(param->model_operation.msg[0]); @@ -407,7 +409,7 @@ void ble_mesh_model_cb(esp_ble_mesh_model_cb_event_t event, esp_ble_mesh_model_c outcome = esp_ble_mesh_server_model_send_msg(param->model_operation.model, param->model_operation.ctx, ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS, sizeof(status), param->model_operation.msg); if (outcome != ESP_OK) { - ESP_LOGE(TAG, "Node:SendMsg,Fal"); + ESP_LOGE(TAG, "Node:SendMsg,Fail"); } } else if (param->model_operation.opcode == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK) { ble_mesh_node_set_state(param->model_operation.msg[0]); @@ -571,6 +573,12 @@ int ble_mesh_init(int argc, char **argv) return 1; } + err = ble_mesh_init_node_prestore_params(); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Bm:NodeInitPreParam,Fail\n"); + return err; + } + ESP_LOGD(TAG, "enter %s, module %x\n", __func__, component.model_type->ival[0]); local_component = ble_mesh_get_component(component.model_type->ival[0]); @@ -593,10 +601,6 @@ int ble_mesh_init(int argc, char **argv) } err = esp_ble_mesh_init(&prov, local_component); - if (err) { - ESP_LOGI(TAG, "Bm:Init,OK\n"); - return err; - } free(device_uuid); ESP_LOGD(TAG, "exit %s\n", __func__); @@ -683,17 +687,13 @@ int ble_mesh_deinit(int argc, char **argv) arg_print_errors(stderr, deinit.end, argv[0]); return 1; } + + ble_mesh_deinit_node_prestore_params(); + if (deinit.action->count != 0) { param.erase_flash = deinit.action->ival[0]; err = esp_ble_mesh_deinit(¶m); - if (err == ESP_OK) { - ESP_LOGI(TAG, "Bm:DeInit,OK,%d,\n", deinit.action->ival[0]); - } - else{ - ESP_LOGI(TAG, "Bm:DeInit,Fail\n"); - } - } - else { + } else { return 1; } ESP_LOGD(TAG, "exit %s\n", __func__);