bugfix:Fixed throughput wasting memory and packet loss issues.

This commit is contained in:
gengyuchao 2019-12-19 21:38:09 +08:00
parent 7dc5c2af59
commit 74a89774fd
4 changed files with 72 additions and 29 deletions

View file

@ -44,6 +44,10 @@ extern "C" {
*/
extern esp_err_t esp_ble_gatt_set_local_mtu (uint16_t mtu);
#if (BLE_INCLUDED == TRUE)
extern uint16_t L2CA_GetFreePkgBufferNum(void);
#endif
#ifdef __cplusplus
}
#endif

View file

@ -1871,6 +1871,14 @@ BOOLEAN L2CA_CheckIsCongest(UINT16 fixed_cid, UINT16 handle)
return TRUE;
}
#if (BLE_INCLUDED == TRUE)
UINT16 L2CA_GetFreePkgBufferNum(void)
{
return l2cb.controller_le_xmit_window;
}
#endif
/*******************************************************************************
**
** Function L2CA_RemoveFixedChnl

View file

@ -30,6 +30,15 @@
#include "freertos/semphr.h"
#include "freertos/task.h"
/**********************************************************
* Thread/Task reference
**********************************************************/
#ifdef CONFIG_BLUEDROID_PINNED_TO_CORE
#define BLUETOOTH_TASK_PINNED_TO_CORE (CONFIG_BLUEDROID_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BLUEDROID_PINNED_TO_CORE : tskNO_AFFINITY)
#else
#define BLUETOOTH_TASK_PINNED_TO_CORE (0)
#endif
#define GATTC_TAG "GATTC_DEMO"
#define REMOTE_SERVICE_UUID 0x00FF
#define REMOTE_NOTIFY_CHAR_UUID 0xFF01
@ -58,7 +67,7 @@ static SemaphoreHandle_t gattc_semaphore;
uint8_t write_data[GATTC_WRITE_LEN] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0e, 0x0f};
#endif /* #if (CONFIG_GATTC_WRITE_THROUGHPUT) */
static bool is_connecet = false;
static bool is_connect = false;
/* Declare static functions */
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
@ -141,7 +150,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
}
break;
case ESP_GATTC_CONNECT_EVT: {
is_connecet = true;
is_connect = true;
ESP_LOGI(GATTC_TAG, "ESP_GATTC_CONNECT_EVT conn_id %d, if %d", p_data->connect.conn_id, gattc_if);
gl_profile_tab[PROFILE_A_APP_ID].conn_id = p_data->connect.conn_id;
memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->connect.remote_bda, sizeof(esp_bd_addr_t));
@ -331,7 +340,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
ESP_LOGI(GATTC_TAG, "write char success ");
break;
case ESP_GATTC_DISCONNECT_EVT:
is_connecet = false;
is_connect = false;
get_server = false;
#if (CONFIG_GATTS_NOTIFY_THROUGHPUT)
start = false;
@ -477,15 +486,15 @@ static void throughput_client_task(void *param)
while(1) {
#if (CONFIG_GATTS_NOTIFY_THROUGHPUT)
vTaskDelay(2000 / portTICK_PERIOD_MS);
if(is_connecet){
if(is_connect){
uint32_t bit_rate = 0;
if (start_time) {
current_time = esp_timer_get_time();
bit_rate = notify_len * SECOND_TO_USECOND / (current_time - start_time);
ESP_LOGI(GATTC_TAG, "Notify Bit rate = %d Btye/s, = %d bit/s, time = %ds",
ESP_LOGI(GATTC_TAG, "Notify Bit rate = %d Byte/s, = %d bit/s, time = %ds",
bit_rate, bit_rate<<3, (int)((current_time - start_time) / SECOND_TO_USECOND));
} else {
ESP_LOGI(GATTC_TAG, "Notify Bit rate = 0 Btye/s, = 0 bit/s");
ESP_LOGI(GATTC_TAG, "Notify Bit rate = 0 Byte/s, = 0 bit/s");
}
}
#endif /* #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) */
@ -494,15 +503,22 @@ static void throughput_client_task(void *param)
int res = xSemaphoreTake(gattc_semaphore, portMAX_DELAY);
assert(res == pdTRUE);
} else {
if (is_connecet) {
// the app data set to 490 just for divided into two packages to send in the low layer
// when the packet length set to 251.
esp_ble_gattc_write_char(gl_profile_tab[PROFILE_A_APP_ID].gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
gl_profile_tab[PROFILE_A_APP_ID].char_handle,
sizeof(write_data), write_data,
ESP_GATT_WRITE_TYPE_NO_RSP,
ESP_GATT_AUTH_REQ_NONE);
if (is_connect) {
int free_buff_num = L2CA_GetFreePkgBufferNum();
if(free_buff_num > 0) {
for( ; free_buff_num > 0; free_buff_num--) {
// the app data set to 490 just for divided into two packages to send in the low layer
// when the packet length set to 251.
esp_ble_gattc_write_char(gl_profile_tab[PROFILE_A_APP_ID].gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
gl_profile_tab[PROFILE_A_APP_ID].char_handle,
sizeof(write_data), write_data,
ESP_GATT_WRITE_TYPE_NO_RSP,
ESP_GATT_AUTH_REQ_NONE);
}
} else { //you can delete the else for the fast speed
vTaskDelay( 10 / portTICK_PERIOD_MS );
}
}
}
#endif /* #if (CONFIG_GATTC_WRITE_THROUGHPUT) */
@ -518,7 +534,7 @@ void app_main()
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
ESP_ERROR_CHECK(ret);
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
@ -571,7 +587,8 @@ void app_main()
ESP_LOGE(GATTC_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
}
xTaskCreate(&throughput_client_task, "throughput_client_task", 4096, NULL, 10, NULL);
xTaskCreatePinnedToCore(&throughput_client_task, "throughput_client_task", 4096, NULL, 10, NULL, BLUETOOTH_TASK_PINNED_TO_CORE);
#if (CONFIG_GATTC_WRITE_THROUGHPUT)
gattc_semaphore = xSemaphoreCreateBinary();
if (!gattc_semaphore) {

View file

@ -24,16 +24,23 @@
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_bt_defs.h"
#include "esp_bt_main.h"
#include "esp_bt_main.h"
#include "esp_gatt_common_api.h"
#include "sdkconfig.h"
/**********************************************************
* Thread/Task reference
**********************************************************/
#ifdef CONFIG_BLUEDROID_PINNED_TO_CORE
#define BLUETOOTH_TASK_PINNED_TO_CORE (CONFIG_BLUEDROID_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BLUEDROID_PINNED_TO_CORE : tskNO_AFFINITY)
#else
#define BLUETOOTH_TASK_PINNED_TO_CORE (0)
#endif
#define SECOND_TO_USECOND 1000000
#define GATTS_TAG "GATTS_DEMO"
@ -53,7 +60,7 @@ static uint64_t start_time = 0;
static uint64_t current_time = 0;
#endif /* #if (CONFIG_EXAMPLE_GATTC_WRITE_THROUGHPUT) */
static bool is_connecet = false;
static bool is_connect = false;
///Declare the static function
static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
@ -522,7 +529,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
case ESP_GATTS_STOP_EVT:
break;
case ESP_GATTS_CONNECT_EVT: {
is_connecet = true;
is_connect = true;
esp_ble_conn_update_params_t conn_params = {0};
memcpy(conn_params.bda, param->connect.remote_bda, sizeof(esp_bd_addr_t));
/* For the IOS system, please reference the apple official documents about the ble connection parameters restrictions. */
@ -540,7 +547,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
break;
}
case ESP_GATTS_DISCONNECT_EVT:
is_connecet = false;
is_connect = false;
ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT");
esp_ble_gap_start_advertising(&adv_params);
break;
@ -616,10 +623,17 @@ void throughput_server_task(void *param)
int res = xSemaphoreTake(gatts_semaphore, portMAX_DELAY);
assert(res == pdTRUE);
} else {
if (is_connecet) {
esp_ble_gatts_send_indicate(gl_profile_tab[PROFILE_A_APP_ID].gatts_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id,
gl_profile_tab[PROFILE_A_APP_ID].char_handle,
sizeof(indicate_data), indicate_data, false);
if (is_connect) {
int free_buff_num = L2CA_GetFreePkgBufferNum();
if(free_buff_num > 0) {
for( ; free_buff_num > 0; free_buff_num--) {
esp_ble_gatts_send_indicate(gl_profile_tab[PROFILE_A_APP_ID].gatts_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id,
gl_profile_tab[PROFILE_A_APP_ID].char_handle,
sizeof(indicate_data), indicate_data, false);
}
} else { //you can delete the else for the fast speed
vTaskDelay( 10 / portTICK_PERIOD_MS );
}
}
}
#endif /* #if (CONFIG_EXAMPLE_GATTS_NOTIFY_THROUGHPUT) */
@ -630,10 +644,10 @@ void throughput_server_task(void *param)
if (start_time) {
current_time = esp_timer_get_time();
bit_rate = write_len * SECOND_TO_USECOND / (current_time - start_time);
ESP_LOGI(GATTS_TAG, "GATTC write Bit rate = %d Btye/s, = %d bit/s, time = %ds",
ESP_LOGI(GATTS_TAG, "GATTC write Bit rate = %d Byte/s, = %d bit/s, time = %ds",
bit_rate, bit_rate<<3, (int)((current_time - start_time) / SECOND_TO_USECOND));
} else {
ESP_LOGI(GATTS_TAG, "GATTC write Bit rate = 0 Btye/s, = 0 bit/s");
ESP_LOGI(GATTS_TAG, "GATTC write Bit rate = 0 Byte/s, = 0 bit/s");
}
#endif /* #if (CONFIG_EXAMPLE_GATTC_WRITE_THROUGHPUT) */
@ -698,7 +712,7 @@ void app_main()
ESP_LOGE(GATTS_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
}
xTaskCreate(&throughput_server_task, "throughput_server_task", 4048, NULL, 15, NULL);
xTaskCreatePinnedToCore(&throughput_server_task, "throughput_server_task", 4096, NULL, 15, NULL, BLUETOOTH_TASK_PINNED_TO_CORE);
#if (CONFIG_EXAMPLE_GATTS_NOTIFY_THROUGHPUT)
gatts_semaphore = xSemaphoreCreateBinary();
if (!gatts_semaphore) {