ble_mesh:example:fix memory leak

This commit is contained in:
Yuan Hong Hui 2022-06-20 21:31:10 +08:00 committed by BOT
parent b992d3b4f8
commit cc27c90b8c
4 changed files with 35 additions and 22 deletions

View File

@ -66,7 +66,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;
@ -74,11 +74,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)
@ -94,6 +97,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);

View File

@ -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);

View File

@ -75,6 +75,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 */

View File

@ -83,7 +83,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;
@ -160,7 +160,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);
@ -242,6 +241,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;
@ -398,7 +400,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]);
@ -406,7 +408,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]);
@ -570,6 +572,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]);
@ -592,10 +600,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__);
@ -682,17 +686,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(&param);
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__);