From 539718735b3a476888aed9275b39a47718f174e1 Mon Sep 17 00:00:00 2001 From: liqigan Date: Wed, 3 Feb 2021 17:23:45 +0800 Subject: [PATCH] fix spp connect failed or discovery failed after the first connection release --- .../bt/bluedroid/stack/rfcomm/port_utils.c | 12 ++++-- .../main/example_spp_initiator_demo.c | 37 +++++++++++++------ 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/components/bt/bluedroid/stack/rfcomm/port_utils.c b/components/bt/bluedroid/stack/rfcomm/port_utils.c index 5be5b75634..6bc42efcc8 100644 --- a/components/bt/bluedroid/stack/rfcomm/port_utils.c +++ b/components/bt/bluedroid/stack/rfcomm/port_utils.c @@ -211,15 +211,19 @@ void port_release_port (tPORT *p_port) osi_mutex_global_lock(); RFCOMM_TRACE_DEBUG("port_release_port, p_port:%p", p_port); if (p_port->rx.queue != NULL) { - while ((p_buf = (BT_HDR *)fixed_queue_dequeue(p_port->rx.queue)) != NULL) { - osi_free(p_buf); + while (fixed_queue_length(p_port->rx.queue) > 0) { + if((p_buf = (BT_HDR *)fixed_queue_try_dequeue(p_port->rx.queue)) != NULL){ + osi_free(p_buf); + } } } p_port->rx.queue_size = 0; if (p_port->tx.queue != NULL) { - while ((p_buf = (BT_HDR *)fixed_queue_dequeue(p_port->tx.queue)) != NULL) { - osi_free(p_buf); + while (fixed_queue_length(p_port->tx.queue) > 0) { + if((p_buf = (BT_HDR *)fixed_queue_try_dequeue(p_port->tx.queue)) != NULL){ + osi_free(p_buf); + } } } p_port->tx.queue_size = 0; diff --git a/examples/bluetooth/bt_spp_initiator/main/example_spp_initiator_demo.c b/examples/bluetooth/bt_spp_initiator/main/example_spp_initiator_demo.c index de8dd0edcf..aa97086376 100644 --- a/examples/bluetooth/bt_spp_initiator/main/example_spp_initiator_demo.c +++ b/examples/bluetooth/bt_spp_initiator/main/example_spp_initiator_demo.c @@ -115,12 +115,21 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) } break; case ESP_SPP_OPEN_EVT: - ESP_LOGI(SPP_TAG, "ESP_SPP_OPEN_EVT"); - esp_spp_write(param->open.handle, SPP_DATA_LEN, spp_data); - gettimeofday(&time_old, NULL); + if (param->open.status == ESP_SPP_SUCCESS) { + ESP_LOGI(SPP_TAG, "ESP_SPP_OPEN_EVT OK hdl:0x%x", param->open.handle); + esp_spp_write(param->open.handle, SPP_DATA_LEN, spp_data); + gettimeofday(&time_old, NULL); + } else { + ESP_LOGI(SPP_TAG, "ESP_SPP_OPEN_EVT Failed!, status:%d", param->open.status); + } break; case ESP_SPP_CLOSE_EVT: - ESP_LOGI(SPP_TAG, "ESP_SPP_CLOSE_EVT"); + if ((param->close.async == false && param->close.status == ESP_SPP_SUCCESS) || param->close.async) { + ESP_LOGI(SPP_TAG, "ESP_SPP_CLOSE_EVT OK, async:%d", param->close.async); + esp_spp_start_discovery(peer_bd_addr); + } else { + ESP_LOGI(SPP_TAG, "ESP_SPP_CLOSE_EVT failed!"); + } break; case ESP_SPP_START_EVT: ESP_LOGI(SPP_TAG, "ESP_SPP_START_EVT"); @@ -140,17 +149,21 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) } break; case ESP_SPP_WRITE_EVT: + if (param->write.status == ESP_SPP_SUCCESS) { #if (SPP_SHOW_MODE == SPP_SHOW_DATA) - ESP_LOGI(SPP_TAG, "ESP_SPP_WRITE_EVT len=%d cong=%d", param->write.len , param->write.cong); - esp_log_buffer_hex("",spp_data,SPP_DATA_LEN); + ESP_LOGI(SPP_TAG, "ESP_SPP_WRITE_EVT len=%d cong=%d", param->write.len, param->write.cong); + esp_log_buffer_hex("", spp_data, SPP_DATA_LEN); #else - gettimeofday(&time_new, NULL); - data_num += param->write.len; - if (time_new.tv_sec - time_old.tv_sec >= 3) { - print_speed(); - } + gettimeofday(&time_new, NULL); + data_num += param->write.len; + if (time_new.tv_sec - time_old.tv_sec >= 3) { + print_speed(); + } #endif - if (param->write.cong == 0) { + } else { + ESP_LOGE(SPP_TAG, "ESP_SPP_WRITE_EVT failed(%d)!", param->write.status); + } + if (param->write.cong == 0 && param->write.handle > 0) { esp_spp_write(param->write.handle, SPP_DATA_LEN, spp_data); } break;