Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_ble_examples_malloc' into 'master'
Browse files Browse the repository at this point in the history
BLE examples malloc related code optimization

See merge request espressif/esp-idf!23638
  • Loading branch information
Weijian-Espressif committed Aug 28, 2023
2 parents 948e319 + 334a8f2 commit 09f5f66
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 48 deletions.
16 changes: 16 additions & 0 deletions components/bt/common/btc/profile/esp/blufi/blufi_prf.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ static void btc_blufi_wifi_conn_report(uint8_t opmode, uint8_t sta_conn_state, u
data_len = info_len + 3;
p = data = osi_malloc(data_len);
if (data == NULL) {
BTC_TRACE_ERROR("%s no mem\n", __func__);
return;
}

Expand Down Expand Up @@ -413,6 +414,7 @@ static void btc_blufi_send_error_info(uint8_t state)
data_len = 1;
p = data = osi_malloc(data_len);
if (data == NULL) {
BTC_TRACE_ERROR("%s no mem\n", __func__);
return;
}

Expand Down Expand Up @@ -688,6 +690,7 @@ void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)

dst->wifi_conn_report.extra_info = osi_calloc(sizeof(esp_blufi_extra_info_t));
if (dst->wifi_conn_report.extra_info == NULL) {
BTC_TRACE_ERROR("%s no mem line %d\n", __func__, __LINE__);
return;
}

Expand All @@ -702,6 +705,9 @@ void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
memcpy(dst->wifi_conn_report.extra_info->sta_ssid, src_info->sta_ssid, src_info->sta_ssid_len);
dst->wifi_conn_report.extra_info->sta_ssid_len = src_info->sta_ssid_len;
dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->sta_ssid_len + 2);
} else {
BTC_TRACE_ERROR("%s no mem line %d\n", __func__, __LINE__);
return;
}
}
if (src_info->sta_passwd) {
Expand All @@ -710,6 +716,9 @@ void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
memcpy(dst->wifi_conn_report.extra_info->sta_passwd, src_info->sta_passwd, src_info->sta_passwd_len);
dst->wifi_conn_report.extra_info->sta_passwd_len = src_info->sta_passwd_len;
dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->sta_passwd_len + 2);
} else {
BTC_TRACE_ERROR("%s no mem line %d\n", __func__, __LINE__);
return;
}
}
if (src_info->softap_ssid) {
Expand All @@ -718,6 +727,9 @@ void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
memcpy(dst->wifi_conn_report.extra_info->softap_ssid, src_info->softap_ssid, src_info->softap_ssid_len);
dst->wifi_conn_report.extra_info->softap_ssid_len = src_info->softap_ssid_len;
dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->softap_ssid_len + 2);
} else {
BTC_TRACE_ERROR("%s no mem line %d\n", __func__, __LINE__);
return;
}
}
if (src_info->softap_passwd) {
Expand All @@ -726,6 +738,9 @@ void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
memcpy(dst->wifi_conn_report.extra_info->softap_passwd, src_info->softap_passwd, src_info->softap_passwd_len);
dst->wifi_conn_report.extra_info->softap_passwd_len = src_info->softap_passwd_len;
dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->softap_passwd_len + 2);
} else {
BTC_TRACE_ERROR("%s no mem line %d\n", __func__, __LINE__);
return;
}
}
if (src_info->softap_authmode_set) {
Expand Down Expand Up @@ -768,6 +783,7 @@ void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
}
dst->wifi_list.list = (esp_blufi_ap_record_t *)osi_malloc(sizeof(esp_blufi_ap_record_t) * src->wifi_list.apCount);
if (dst->wifi_list.list == NULL) {
BTC_TRACE_ERROR("%s no mem line %d\n", __func__, __LINE__);
break;
}
memcpy(dst->wifi_list.list, list, sizeof(esp_blufi_ap_record_t) * src->wifi_list.apCount);
Expand Down
14 changes: 13 additions & 1 deletion examples/bluetooth/bluedroid/ble/ble_ancs/main/ble_ancs_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,15 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
&count);
if (ret_status != ESP_GATT_OK) {
ESP_LOGE(BLE_ANCS_TAG, "esp_ble_gattc_get_attr_count error, %d", __LINE__);
break;
}
if (count > 0) {
char_elem_result = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count);
memset(char_elem_result, 0xff, sizeof(esp_gattc_char_elem_t) * count);
if (!char_elem_result) {
ESP_LOGE(BLE_ANCS_TAG, "gattc no mem");
break;
} else {
memset(char_elem_result, 0xff, sizeof(esp_gattc_char_elem_t) * count);
ret_status = esp_ble_gattc_get_all_char(gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
gl_profile_tab[PROFILE_A_APP_ID].service_start_handle,
Expand All @@ -400,6 +402,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
offset);
if (ret_status != ESP_GATT_OK) {
ESP_LOGE(BLE_ANCS_TAG, "esp_ble_gattc_get_all_char error, %d", __LINE__);
free(char_elem_result);
char_elem_result = NULL;
break;
}
if (count > 0) {

Expand Down Expand Up @@ -432,6 +437,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
}
}
free(char_elem_result);
char_elem_result = NULL;
}
} else {
ESP_LOGE(BLE_ANCS_TAG, "No Apple Notification Service found");
Expand All @@ -456,11 +462,13 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
&count);
if (ret_status != ESP_GATT_OK) {
ESP_LOGE(BLE_ANCS_TAG, "esp_ble_gattc_get_attr_count error, %d", __LINE__);
break;
}
if (count > 0) {
descr_elem_result = malloc(sizeof(esp_gattc_descr_elem_t) * count);
if (!descr_elem_result) {
ESP_LOGE(BLE_ANCS_TAG, "malloc error, gattc no mem");
break;
} else {
ret_status = esp_ble_gattc_get_all_descr(gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
Expand All @@ -470,6 +478,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
offset);
if (ret_status != ESP_GATT_OK) {
ESP_LOGE(BLE_ANCS_TAG, "esp_ble_gattc_get_all_descr error, %d", __LINE__);
free(descr_elem_result);
descr_elem_result = NULL;
break;
}

for (int i = 0; i < count; ++ i) {
Expand All @@ -487,6 +498,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
}
}
free(descr_elem_result);
descr_elem_result = NULL;
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ static void show_bonded_devices(void)
int dev_num = esp_ble_get_bond_device_num();

esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num);
if (!dev_list) {
ESP_LOGE(EXAMPLE_TAG, "malloc failed, return\n");
return;
}
esp_ble_get_bond_device_list(&dev_num, dev_list);
EXAMPLE_DEBUG(EXAMPLE_TAG, "Bonded devices number : %d\n", dev_num);

Expand All @@ -266,6 +270,10 @@ static void __attribute__((unused)) remove_all_bonded_devices(void)
int dev_num = esp_ble_get_bond_device_num();

esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num);
if (!dev_list) {
ESP_LOGE(EXAMPLE_TAG, "malloc failed, return\n");
return;
}
esp_ble_get_bond_device_list(&dev_num, dev_list);
for (int i = 0; i < dev_num; i++) {
esp_ble_remove_bond_device(dev_list[i].bd_addr);
Expand Down Expand Up @@ -414,7 +422,8 @@ void example_prepare_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t
}
free(gatt_rsp);
}else{
ESP_LOGE(EXAMPLE_TAG, "%s, malloc failed", __func__);
ESP_LOGE(EXAMPLE_TAG, "%s, malloc failed, and no resource to send response", __func__);
status = ESP_GATT_NO_RESOURCES;
}
}
if (status != ESP_GATT_OK){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,13 @@ static bool store_wr_buffer(esp_ble_gatts_cb_param_t *p_data)
temp_spp_recv_data_node_p1->next_node = NULL;
temp_spp_recv_data_node_p1->node_buff = (uint8_t *)malloc(p_data->write.len);
temp_spp_recv_data_node_p2 = temp_spp_recv_data_node_p1;
memcpy(temp_spp_recv_data_node_p1->node_buff,p_data->write.value,p_data->write.len);
if (temp_spp_recv_data_node_p1->node_buff == NULL) {
ESP_LOGI(GATTS_TABLE_TAG, "malloc error %s %d\n", __func__, __LINE__);
temp_spp_recv_data_node_p1->len = 0;
} else {
memcpy(temp_spp_recv_data_node_p1->node_buff,p_data->write.value,p_data->write.len);
}

if(SppRecvDataBuff.node_num == 0){
SppRecvDataBuff.first_node = temp_spp_recv_data_node_p1;
SppRecvDataBuff.node_num++;
Expand All @@ -288,7 +294,9 @@ static void free_write_buffer(void)

while(temp_spp_recv_data_node_p1 != NULL){
temp_spp_recv_data_node_p2 = temp_spp_recv_data_node_p1->next_node;
free(temp_spp_recv_data_node_p1->node_buff);
if (temp_spp_recv_data_node_p1->node_buff) {
free(temp_spp_recv_data_node_p1->node_buff);
}
free(temp_spp_recv_data_node_p1);
temp_spp_recv_data_node_p1 = temp_spp_recv_data_node_p2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,14 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
&count);
if (status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error");
break;
}

if (count > 0){
char_elem_result = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count);
if (!char_elem_result){
ESP_LOGE(GATTC_TAG, "gattc no mem");
break;
}else{
status = esp_ble_gattc_get_char_by_uuid( gattc_if,
p_data->search_cmpl.conn_id,
Expand All @@ -219,8 +221,11 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
remote_filter_char_uuid,
char_elem_result,
&count);
if (status != ESP_GATT_OK){
if (status != ESP_GATT_OK) {
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_char_by_uuid error");
free(char_elem_result);
char_elem_result = NULL;
break;
}

/* Every service has only one char in our 'throughput_server' demo, so we use first 'char_elem_result' */
Expand All @@ -231,6 +236,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
}
/* free char_elem_result */
free(char_elem_result);
char_elem_result = NULL;
}else{
ESP_LOGE(GATTC_TAG, "no char found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,23 @@ void example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare
}

esp_gatt_rsp_t *gatt_rsp = (esp_gatt_rsp_t *)malloc(sizeof(esp_gatt_rsp_t));
gatt_rsp->attr_value.len = param->write.len;
gatt_rsp->attr_value.handle = param->write.handle;
gatt_rsp->attr_value.offset = param->write.offset;
gatt_rsp->attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
memcpy(gatt_rsp->attr_value.value, param->write.value, param->write.len);
esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, status, gatt_rsp);

if (response_err != ESP_OK) {
ESP_LOGE(GATTS_TAG, "Send response error");
if (gatt_rsp) {
gatt_rsp->attr_value.len = param->write.len;
gatt_rsp->attr_value.handle = param->write.handle;
gatt_rsp->attr_value.offset = param->write.offset;
gatt_rsp->attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
memcpy(gatt_rsp->attr_value.value, param->write.value, param->write.len);
esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, status, gatt_rsp);

if (response_err != ESP_OK) {
ESP_LOGE(GATTS_TAG, "Send response error\n");
}
free(gatt_rsp);
} else {
ESP_LOGE(GATTS_TAG, "malloc failed, no resource to send response error\n");
status = ESP_GATT_NO_RESOURCES;
}
free(gatt_rsp);

if (status != ESP_GATT_OK) {
return;
}
Expand Down
10 changes: 10 additions & 0 deletions examples/bluetooth/bluedroid/ble/gatt_client/main/gattc_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,14 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
&count);
if (status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error");
break;
}

if (count > 0){
char_elem_result = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count);
if (!char_elem_result){
ESP_LOGE(GATTC_TAG, "gattc no mem");
break;
}else{
status = esp_ble_gattc_get_char_by_uuid( gattc_if,
p_data->search_cmpl.conn_id,
Expand All @@ -190,6 +192,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
&count);
if (status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_char_by_uuid error");
free(char_elem_result);
char_elem_result = NULL;
break;
}

/* Every service have only one char in our 'ESP_GATTS_DEMO' demo, so we used first 'char_elem_result' */
Expand Down Expand Up @@ -221,11 +226,13 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
&count);
if (ret_status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error");
break;
}
if (count > 0){
descr_elem_result = malloc(sizeof(esp_gattc_descr_elem_t) * count);
if (!descr_elem_result){
ESP_LOGE(GATTC_TAG, "malloc error, gattc no mem");
break;
}else{
ret_status = esp_ble_gattc_get_descr_by_char_handle( gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
Expand All @@ -235,6 +242,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
&count);
if (ret_status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_descr_by_char_handle error");
free(descr_elem_result);
descr_elem_result = NULL;
break;
}
/* Every char has only one descriptor in our 'ESP_GATTS_DEMO' demo, so we used first 'descr_elem_result' */
if (count > 0 && descr_elem_result[0].uuid.len == ESP_UUID_LEN_16 && descr_elem_result[0].uuid.uuid.uuid16 == ESP_GATT_UUID_CHAR_CLIENT_CONFIG){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,13 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
&count);
if (ret_status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error, %d", __LINE__);
break;
}
if (count > 0){
char_elem_result = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count);
if (!char_elem_result){
ESP_LOGE(GATTC_TAG, "gattc no mem");
break;
}else{
ret_status = esp_ble_gattc_get_all_char(gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
Expand All @@ -242,6 +244,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
offset);
if (ret_status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_all_char error, %d", __LINE__);
free(char_elem_result);
char_elem_result = NULL;
break;
}
if (count > 0){

Expand All @@ -259,6 +264,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
}
}
free(char_elem_result);
char_elem_result = NULL;
}
}

Expand All @@ -281,11 +287,13 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
&count);
if (ret_status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error, %d", __LINE__);
break;
}
if (count > 0){
descr_elem_result = malloc(sizeof(esp_gattc_descr_elem_t) * count);
if (!descr_elem_result){
ESP_LOGE(GATTC_TAG, "malloc error, gattc no mem");
break;
}else{
ret_status = esp_ble_gattc_get_all_descr(gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
Expand All @@ -295,6 +303,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
offset);
if (ret_status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_all_descr error, %d", __LINE__);
free(descr_elem_result);
descr_elem_result = NULL;
break;
}

for (int i = 0; i < count; ++i)
Expand All @@ -314,6 +325,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
}
}
free(descr_elem_result);
descr_elem_result = NULL;
}

break;
Expand Down
Loading

0 comments on commit 09f5f66

Please sign in to comment.