mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
example/ble_throughput:
Fixed the throughput wasting memory and packet loss issues. Fixed the throughput crash bug because of using the Mutex.
This commit is contained in:
parent
da19c94976
commit
4643ca6ef8
@ -46,4 +46,13 @@ esp_err_t esp_ble_gatt_set_local_mtu (uint16_t mtu)
|
||||
arg.set_mtu.mtu = mtu;
|
||||
|
||||
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatt_com_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
|
||||
}
|
||||
}
|
||||
|
||||
#if (BLE_INCLUDED == TRUE)
|
||||
extern uint16_t L2CA_GetFreePktBufferNum_LE(void);
|
||||
|
||||
uint16_t esp_ble_get_sendable_packets_num ()
|
||||
{
|
||||
return L2CA_GetFreePktBufferNum_LE();
|
||||
}
|
||||
#endif
|
||||
|
@ -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 esp_ble_get_sendable_packets_num (void);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -1871,6 +1871,14 @@ BOOLEAN L2CA_CheckIsCongest(UINT16 fixed_cid, UINT16 handle)
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
#if (BLE_INCLUDED == TRUE)
|
||||
UINT16 L2CA_GetFreePktBufferNum_LE(void)
|
||||
{
|
||||
return l2cb.controller_le_xmit_window;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
** Function L2CA_RemoveFixedChnl
|
||||
|
@ -35,6 +35,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
|
||||
@ -63,7 +72,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;
|
||||
|
||||
/* eclare static functions */
|
||||
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
|
||||
@ -146,7 +155,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));
|
||||
@ -336,7 +345,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;
|
||||
@ -482,15 +491,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) */
|
||||
@ -499,15 +508,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 = esp_ble_get_sendable_packets_num();
|
||||
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 { //Add the vTaskDelay to prevent this task from consuming the CPU all the time, causing low-priority tasks to not be executed at all.
|
||||
vTaskDelay( 10 / portTICK_PERIOD_MS );
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* #if (CONFIG_GATTC_WRITE_THROUGHPUT) */
|
||||
@ -523,7 +539,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));
|
||||
|
||||
@ -575,10 +591,12 @@ void app_main()
|
||||
if (local_mtu_ret){
|
||||
ESP_LOGE(GATTC_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
|
||||
}
|
||||
// The task is only created on the CPU core that Bluetooth is working on,
|
||||
// preventing the sending task from using the un-updated Bluetooth state on another CPU.
|
||||
xTaskCreatePinnedToCore(&throughput_client_task, "throughput_client_task", 4096, NULL, 10, NULL, BLUETOOTH_TASK_PINNED_TO_CORE);
|
||||
|
||||
xTaskCreate(&throughput_client_task, "throughput_client_task", 4096, NULL, 10, NULL);
|
||||
#if (CONFIG_GATTC_WRITE_THROUGHPUT)
|
||||
gattc_semaphore = xSemaphoreCreateMutex();
|
||||
gattc_semaphore = xSemaphoreCreateBinary();
|
||||
if (!gattc_semaphore) {
|
||||
ESP_LOGE(GATTC_TAG, "%s, init fail, the gattc semaphore create fail.", __func__);
|
||||
return;
|
||||
|
@ -18,16 +18,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"
|
||||
@ -47,7 +54,7 @@ static uint64_t start_time = 0;
|
||||
static uint64_t current_time = 0;
|
||||
#endif /* #if (CONFIG_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);
|
||||
|
||||
@ -517,7 +524,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. */
|
||||
@ -535,7 +542,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;
|
||||
@ -606,15 +613,22 @@ void throughput_server_task(void *param)
|
||||
#endif /* #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) */
|
||||
|
||||
while(1) {
|
||||
#if (CONFIG_GATTS_NOTIFY_THROUGHPUT)
|
||||
#if (CONFIG_GATTS_NOTIFY_THROUGHPUT)
|
||||
if (!can_send_notify) {
|
||||
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 = esp_ble_get_sendable_packets_num();
|
||||
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 { //Add the vTaskDelay to prevent this task from consuming the CPU all the time, causing low-priority tasks to not be executed at all.
|
||||
vTaskDelay( 10 / portTICK_PERIOD_MS );
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) */
|
||||
@ -625,10 +639,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_GATTC_WRITE_THROUGHPUT) */
|
||||
|
||||
@ -690,12 +704,13 @@ void app_main()
|
||||
|
||||
esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(517);
|
||||
if (local_mtu_ret){
|
||||
ESP_LOGE(GATTS_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
|
||||
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);
|
||||
// The task is only created on the CPU core that Bluetooth is working on,
|
||||
// preventing the sending task from using the un-updated Bluetooth state on another CPU.
|
||||
xTaskCreatePinnedToCore(&throughput_server_task, "throughput_server_task", 4096, NULL, 15, NULL, BLUETOOTH_TASK_PINNED_TO_CORE);
|
||||
#if (CONFIG_GATTS_NOTIFY_THROUGHPUT)
|
||||
gatts_semaphore = xSemaphoreCreateMutex();
|
||||
gatts_semaphore = xSemaphoreCreateBinary();
|
||||
if (!gatts_semaphore) {
|
||||
ESP_LOGE(GATTS_TAG, "%s, init fail, the gatts semaphore create fail.", __func__);
|
||||
return;
|
||||
|
Loading…
Reference in New Issue
Block a user