components/bt: Add api to query the number of available buffers for the current connection

This commit is contained in:
wangcheng 2020-07-24 14:30:47 +08:00 committed by baohongde
parent debda4a63f
commit 12d476427f
16 changed files with 212 additions and 7 deletions

View file

@ -50,9 +50,33 @@ esp_err_t esp_ble_gatt_set_local_mtu (uint16_t mtu)
#if (BLE_INCLUDED == TRUE) #if (BLE_INCLUDED == TRUE)
extern uint16_t L2CA_GetFreePktBufferNum_LE(void); extern uint16_t L2CA_GetFreePktBufferNum_LE(void);
/**
* @brief This function is called to get currently sendable packets number on controller,
* the function is called only in BLE running core and single connection now.
*
* @return
* sendable packets number on controller
*
*/
uint16_t esp_ble_get_sendable_packets_num () uint16_t esp_ble_get_sendable_packets_num (void)
{ {
return L2CA_GetFreePktBufferNum_LE(); return L2CA_GetFreePktBufferNum_LE();
} }
/**
* @brief This function is used to query the number of available buffers for the current connection.
* When you need to query the current available buffer number, it is recommended to use this API.
* @param[in] conn_id: current connection id.
*
* @return
* Number of available buffers for the current connection
*
*/
extern UINT16 L2CA_GetCurFreePktBufferNum_LE(UINT16 conn_id);
uint16_t esp_ble_get_cur_sendable_packets_num (uint16_t connid)
{
return L2CA_GetCurFreePktBufferNum_LE(connid);
}
#endif #endif

View file

@ -431,6 +431,9 @@ esp_err_t esp_ble_gattc_write_char(esp_gatt_if_t gattc_if,
arg.write_char.value = value; arg.write_char.value = value;
arg.write_char.write_type = write_type; arg.write_char.write_type = write_type;
arg.write_char.auth_req = auth_req; arg.write_char.auth_req = auth_req;
if(write_type == ESP_GATT_WRITE_TYPE_NO_RSP){
l2ble_update_att_acl_pkt_num(L2CA_ADD_BTC_NUM, NULL);
}
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
} }
@ -462,6 +465,10 @@ esp_err_t esp_ble_gattc_write_char_descr (esp_gatt_if_t gattc_if,
arg.write_descr.write_type = write_type; arg.write_descr.write_type = write_type;
arg.write_descr.auth_req = auth_req; arg.write_descr.auth_req = auth_req;
if(write_type == ESP_GATT_WRITE_TYPE_NO_RSP){
l2ble_update_att_acl_pkt_num(L2CA_ADD_BTC_NUM, NULL);
}
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), btc_gattc_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
} }

View file

@ -274,6 +274,9 @@ esp_err_t esp_ble_gatts_send_indicate(esp_gatt_if_t gatts_if, uint16_t conn_id,
arg.send_ind.value_len = value_len; arg.send_ind.value_len = value_len;
arg.send_ind.value = value; arg.send_ind.value = value;
if(need_confirm == false){
l2ble_update_att_acl_pkt_num(L2CA_ADD_BTC_NUM, NULL);
}
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t), return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatts_args_t),
btc_gatts_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); btc_gatts_arg_deep_copy) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
} }

View file

@ -46,6 +46,7 @@ extern esp_err_t esp_ble_gatt_set_local_mtu (uint16_t mtu);
#if (BLE_INCLUDED == TRUE) #if (BLE_INCLUDED == TRUE)
extern uint16_t esp_ble_get_sendable_packets_num (void); extern uint16_t esp_ble_get_sendable_packets_num (void);
extern uint16_t esp_ble_get_cur_sendable_packets_num (uint16_t connid);
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -31,7 +31,7 @@
#include "bta/bta_sys.h" #include "bta/bta_sys.h"
#include "bta/bta_gatt_api.h" #include "bta/bta_gatt_api.h"
#include "bta_gattc_int.h" #include "bta_gattc_int.h"
#include "stack/l2c_api.h"
/***************************************************************************** /*****************************************************************************
** Constants ** Constants
*****************************************************************************/ *****************************************************************************/
@ -603,6 +603,10 @@ void BTA_GATTC_WriteCharValue ( UINT16 conn_id,
memcpy(p_buf->p_value, p_value, len); memcpy(p_buf->p_value, p_value, len);
} }
if(write_type == BTA_GATTC_TYPE_WRITE_NO_RSP){
l2ble_update_att_acl_pkt_num(L2CA_DECREASE_BTC_NUM, NULL);
l2ble_update_att_acl_pkt_num(L2CA_ADD_BTU_NUM, NULL);
}
bta_sys_sendmsg(p_buf); bta_sys_sendmsg(p_buf);
} }
return; return;
@ -650,6 +654,10 @@ void BTA_GATTC_WriteCharDescr (UINT16 conn_id,
memcpy(p_buf->p_value, p_data->p_value, p_data->len); memcpy(p_buf->p_value, p_data->p_value, p_data->len);
} }
if(write_type == BTA_GATTC_TYPE_WRITE_NO_RSP){
l2ble_update_att_acl_pkt_num(L2CA_DECREASE_BTC_NUM, NULL);
l2ble_update_att_acl_pkt_num(L2CA_ADD_BTU_NUM, NULL);
}
bta_sys_sendmsg(p_buf); bta_sys_sendmsg(p_buf);
} }
return; return;

View file

@ -35,6 +35,7 @@
#include "stack/btm_ble_api.h" #include "stack/btm_ble_api.h"
#include <string.h> #include <string.h>
#include "osi/allocator.h" #include "osi/allocator.h"
#include "stack/l2c_api.h"
static void bta_gatts_nv_save_cback(BOOLEAN is_saved, tGATTS_HNDL_RANGE *p_hndl_range); static void bta_gatts_nv_save_cback(BOOLEAN is_saved, tGATTS_HNDL_RANGE *p_hndl_range);
static BOOLEAN bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD cmd, tGATTS_SRV_CHG_REQ *p_req, static BOOLEAN bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD cmd, tGATTS_SRV_CHG_REQ *p_req,
@ -672,6 +673,7 @@ void bta_gatts_indicate_handle (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
p_msg->api_indicate.len, p_msg->api_indicate.len,
p_msg->api_indicate.value); p_msg->api_indicate.value);
} else { } else {
l2ble_update_att_acl_pkt_num(L2CA_DECREASE_BTU_NUM, NULL);
status = GATTS_HandleValueNotification (p_msg->api_indicate.hdr.layer_specific, status = GATTS_HandleValueNotification (p_msg->api_indicate.hdr.layer_specific,
p_msg->api_indicate.attr_id, p_msg->api_indicate.attr_id,
p_msg->api_indicate.len, p_msg->api_indicate.len,

View file

@ -31,7 +31,7 @@
#include "bta/bta_gatt_api.h" #include "bta/bta_gatt_api.h"
#include "bta_gatts_int.h" #include "bta_gatts_int.h"
#include "osi/allocator.h" #include "osi/allocator.h"
#include "stack/l2c_api.h"
/***************************************************************************** /*****************************************************************************
** Constants ** Constants
*****************************************************************************/ *****************************************************************************/
@ -426,6 +426,11 @@ void BTA_GATTS_HandleValueIndication (UINT16 conn_id, UINT16 attr_id, UINT16 dat
memcpy(p_buf->value, p_data, data_len); memcpy(p_buf->value, p_data, data_len);
} }
if(need_confirm == false){
l2ble_update_att_acl_pkt_num(L2CA_DECREASE_BTC_NUM, NULL);
l2ble_update_att_acl_pkt_num(L2CA_ADD_BTU_NUM, NULL);
}
bta_sys_sendmsg(p_buf); bta_sys_sendmsg(p_buf);
} }
return; return;

View file

@ -219,6 +219,7 @@ void gatt_act_write (tGATT_CLCB *p_clcb, UINT8 sec_act)
if (p_attr) { if (p_attr) {
switch (p_clcb->op_subtype) { switch (p_clcb->op_subtype) {
case GATT_WRITE_NO_RSP: case GATT_WRITE_NO_RSP:
l2ble_update_att_acl_pkt_num(L2CA_DECREASE_BTU_NUM, NULL);
p_clcb->s_handle = p_attr->handle; p_clcb->s_handle = p_attr->handle;
op_code = (sec_act == GATT_SEC_SIGN_DATA) ? GATT_SIGN_CMD_WRITE : GATT_CMD_WRITE; op_code = (sec_act == GATT_SEC_SIGN_DATA) ? GATT_SIGN_CMD_WRITE : GATT_CMD_WRITE;
rt = gatt_send_write_msg(p_tcb, rt = gatt_send_write_msg(p_tcb,

View file

@ -29,6 +29,7 @@
#include "common/bt_target.h" #include "common/bt_target.h"
#include "stack/l2cdefs.h" #include "stack/l2cdefs.h"
#include "stack/hcidefs.h" #include "stack/hcidefs.h"
#include "osi/fixed_queue.h"
/***************************************************************************** /*****************************************************************************
** Constants ** Constants
@ -1227,6 +1228,21 @@ extern UINT16 L2CA_GetDisconnectReason (BD_ADDR remote_bda, tBT_TRANSPORT transp
extern BOOLEAN L2CA_CheckIsCongest(UINT16 fixed_cid, UINT16 handle); extern BOOLEAN L2CA_CheckIsCongest(UINT16 fixed_cid, UINT16 handle);
#define L2CA_GET_ATT_NUM 0
#define L2CA_ADD_BTC_NUM 1
#define L2CA_DECREASE_BTC_NUM 2
#define L2CA_ADD_BTU_NUM 3
#define L2CA_DECREASE_BTU_NUM 4
#define L2CA_BUFF_INI 5
#define L2CA_BUFF_DEINIT 6
typedef struct {
UINT16 conn_id;
UINT16 * get_num;
} tl2c_buff_param_t;
extern void l2ble_update_att_acl_pkt_num(UINT8 type, tl2c_buff_param_t *param);
#endif /* (BLE_INCLUDED == TRUE) */ #endif /* (BLE_INCLUDED == TRUE) */

View file

@ -73,7 +73,7 @@
#define L2CAP_DEFAULT_RETRANS_TOUT 2000 /* 2000 milliseconds */ #define L2CAP_DEFAULT_RETRANS_TOUT 2000 /* 2000 milliseconds */
#define L2CAP_DEFAULT_MONITOR_TOUT 12000 /* 12000 milliseconds */ #define L2CAP_DEFAULT_MONITOR_TOUT 12000 /* 12000 milliseconds */
#define L2CAP_FCR_ACK_TOUT 200 /* 200 milliseconds */ #define L2CAP_FCR_ACK_TOUT 200 /* 200 milliseconds */
#define L2CAP_CACHE_ATT_ACL_NUM 10
/* Define the possible L2CAP channel states. The names of /* Define the possible L2CAP channel states. The names of
** the states may seem a bit strange, but they are taken from ** the states may seem a bit strange, but they are taken from
** the Bluetooth specification. ** the Bluetooth specification.
@ -165,6 +165,10 @@ typedef enum {
#define L2CAP_MAX_FCR_CFG_TRIES 2 /* Config attempts before disconnecting */ #define L2CAP_MAX_FCR_CFG_TRIES 2 /* Config attempts before disconnecting */
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
typedef uint8_t tL2C_BLE_FIXED_CHNLS_MASK; typedef uint8_t tL2C_BLE_FIXED_CHNLS_MASK;
typedef struct { typedef struct {

View file

@ -36,6 +36,8 @@
#include "stack/btu.h" #include "stack/btu.h"
#include "stack/btm_api.h" #include "stack/btm_api.h"
#include "osi/allocator.h" #include "osi/allocator.h"
#include "gatt_int.h"
#include "freertos/semphr.h"
#if (CLASSIC_BT_INCLUDED == TRUE) #if (CLASSIC_BT_INCLUDED == TRUE)
/******************************************************************************* /*******************************************************************************
@ -1877,6 +1879,16 @@ UINT16 L2CA_GetFreePktBufferNum_LE(void)
{ {
return l2cb.controller_le_xmit_window; return l2cb.controller_le_xmit_window;
} }
UINT16 L2CA_GetCurFreePktBufferNum_LE(UINT16 conn_id)
{
uint16_t num = 0;
tl2c_buff_param_t param;
param.conn_id = conn_id;
param.get_num = &num;
l2ble_update_att_acl_pkt_num(L2CA_GET_ATT_NUM, &param);
return num;
}
#endif #endif
/******************************************************************************* /*******************************************************************************
@ -2265,3 +2277,112 @@ UINT16 L2CA_FlushChannel (UINT16 lcid, UINT16 num_to_flush)
return (num_left); return (num_left);
} }
/******************************************************************************
**
** Function update_acl_pkt_num
**
** Description Update the number of att acl packets to be sent in xmit_hold_q.
**
** Returns None
**
*******************************************************************************/
#if BLE_INCLUDED == TRUE
void l2ble_update_att_acl_pkt_num(UINT8 type, tl2c_buff_param_t *param)
{
static SemaphoreHandle_t buff_semaphore = NULL ;
static INT16 btc_buf;
static INT16 btu_buf;
if(buff_semaphore == NULL && type != L2CA_BUFF_INI){
L2CAP_TRACE_ERROR("%s buff_semaphore not init", __func__);
return;
}
switch (type)
{
case L2CA_ADD_BTC_NUM:{
xSemaphoreTake(buff_semaphore, portMAX_DELAY);
btc_buf ++;
xSemaphoreGive(buff_semaphore);
break;
}
case L2CA_DECREASE_BTC_NUM:{
xSemaphoreTake(buff_semaphore, portMAX_DELAY);
btc_buf --;
xSemaphoreGive(buff_semaphore);
break;
}
case L2CA_ADD_BTU_NUM:{
xSemaphoreTake(buff_semaphore, portMAX_DELAY);
btu_buf ++;
xSemaphoreGive(buff_semaphore);
break;
}
case L2CA_DECREASE_BTU_NUM:{
xSemaphoreTake(buff_semaphore, portMAX_DELAY);
btu_buf --;
xSemaphoreGive(buff_semaphore);
break;
}
case L2CA_GET_ATT_NUM:{
xSemaphoreTake(buff_semaphore, portMAX_DELAY);
INT16 att_acl_pkt_num = 0;
INT16 att_max_num = 0;
*(param->get_num) = 0;
UINT8 tcb_idx = param->conn_id;
tGATT_TCB * p_tcb = gatt_get_tcb_by_idx(tcb_idx);
if (p_tcb == NULL){
L2CAP_TRACE_ERROR("%s not found p_tcb", __func__);
xSemaphoreGive(buff_semaphore);
break;
}
tL2C_LCB * p_lcb = l2cu_find_lcb_by_bd_addr (p_tcb->peer_bda, BT_TRANSPORT_LE);
if (p_lcb == NULL){
L2CAP_TRACE_ERROR("%s not found p_lcb", __func__);
xSemaphoreGive(buff_semaphore);
break;
}
fixed_queue_t * queue = p_lcb->p_fixed_ccbs[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL]->xmit_hold_q;
att_max_num = MIN(p_lcb->link_xmit_quota, L2CAP_CACHE_ATT_ACL_NUM);
if (queue == NULL){
L2CAP_TRACE_ERROR("%s not found queue", __func__);
xSemaphoreGive(buff_semaphore);
break;
}
att_acl_pkt_num = fixed_queue_length(queue);
if(att_acl_pkt_num < att_max_num){
if(btc_buf + btu_buf < att_max_num - att_acl_pkt_num){
*(param->get_num) = att_max_num - att_acl_pkt_num - (btc_buf + btu_buf);
}
}
xSemaphoreGive(buff_semaphore);
break;
}
case L2CA_BUFF_INI:{
btc_buf = 0;
btu_buf = 0;
buff_semaphore = xSemaphoreCreateBinary();
if (buff_semaphore == NULL) {
L2CAP_TRACE_ERROR("%s NO MEMORY", __func__);
break;
}
xSemaphoreGive(buff_semaphore);
break;
}
case L2CA_BUFF_DEINIT:{
xSemaphoreTake(buff_semaphore, portMAX_DELAY);
btc_buf = 0;
btu_buf = 0;
xSemaphoreGive(buff_semaphore);
vSemaphoreDelete(buff_semaphore);
buff_semaphore = NULL;
break;
}
default:
break;
}
xSemaphoreGive(buff_semaphore);
}
#endif

View file

@ -1127,6 +1127,12 @@ void l2c_link_check_send_pkts (tL2C_LCB *p_lcb, tL2C_CCB *p_ccb, BT_HDR *p_buf)
while ((l2cb.controller_xmit_window != 0) && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota)) while ((l2cb.controller_xmit_window != 0) && (p_lcb->sent_not_acked < p_lcb->link_xmit_quota))
#endif #endif
{ {
//need check flag: partial_segment_being_sent.
if ( (p_lcb->partial_segment_being_sent)
|| (p_lcb->link_state != LST_CONNECTED)
|| (L2C_LINK_CHECK_POWER_MODE (p_lcb)) ) {
break;
}
//L2CAP_TRACE_DEBUG("l2cu_get_next_buffer_to_send = %p",l2cu_get_next_buffer_to_send(p_lcb)); //L2CAP_TRACE_DEBUG("l2cu_get_next_buffer_to_send = %p",l2cu_get_next_buffer_to_send(p_lcb));
if ((p_buf = l2cu_get_next_buffer_to_send (p_lcb)) == NULL) { if ((p_buf = l2cu_get_next_buffer_to_send (p_lcb)) == NULL) {
break; break;

View file

@ -880,6 +880,10 @@ void l2c_init (void)
if (l2cb.rcv_pending_q == NULL) { if (l2cb.rcv_pending_q == NULL) {
L2CAP_TRACE_ERROR("%s unable to allocate memory for link layer control block", __func__); L2CAP_TRACE_ERROR("%s unable to allocate memory for link layer control block", __func__);
} }
#if BLE_INCLUDED == TRUE
l2ble_update_att_acl_pkt_num(L2CA_BUFF_INI, NULL);
#endif
} }
void l2c_free(void) void l2c_free(void)
@ -889,6 +893,9 @@ void l2c_free(void)
#if L2C_DYNAMIC_MEMORY #if L2C_DYNAMIC_MEMORY
FREE_AND_RESET(l2c_cb_ptr); FREE_AND_RESET(l2c_cb_ptr);
#endif #endif
#if BLE_INCLUDED == TRUE
l2ble_update_att_acl_pkt_num(L2CA_BUFF_DEINIT, NULL);
#endif
} }
/******************************************************************************* /*******************************************************************************

View file

@ -3657,7 +3657,7 @@ void l2cu_check_channel_congestion (tL2C_CCB *p_ccb)
} else { } else {
tL2C_LCB *p_lcb = p_ccb->p_lcb; tL2C_LCB *p_lcb = p_ccb->p_lcb;
/* If this channel was not congested but it is congested now, tell the app */ /* If this channel was not congested but it is congested now, tell the app */
if ((q_count > p_ccb->buff_quota) || (p_lcb && (p_ccb->local_cid == L2CAP_ATT_CID) && (p_lcb->link_xmit_quota > 0) && (p_lcb->link_xmit_quota <= p_lcb->sent_not_acked))) { if (q_count > p_ccb->buff_quota || (p_lcb && (p_lcb->link_xmit_data_q) && (list_length(p_lcb->link_xmit_data_q) + q_count) > p_ccb->buff_quota)) {
p_ccb->cong_sent = TRUE; p_ccb->cong_sent = TRUE;
if (p_ccb->p_rcb && p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb) { if (p_ccb->p_rcb && p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb) {
L2CAP_TRACE_DEBUG ("L2CAP - Calling CongestionStatus_Cb (TRUE),CID:0x%04x,XmitQ:%u,Quota:%u", L2CAP_TRACE_DEBUG ("L2CAP - Calling CongestionStatus_Cb (TRUE),CID:0x%04x,XmitQ:%u,Quota:%u",

View file

@ -504,7 +504,7 @@ static void throughput_client_task(void *param)
assert(res == pdTRUE); assert(res == pdTRUE);
} else { } else {
if (is_connect) { if (is_connect) {
int free_buff_num = esp_ble_get_sendable_packets_num(); int free_buff_num = esp_ble_get_cur_sendable_packets_num(gl_profile_tab[PROFILE_A_APP_ID].conn_id);
if(free_buff_num > 0) { if(free_buff_num > 0) {
for( ; free_buff_num > 0; free_buff_num--) { 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 // the app data set to 490 just for divided into two packages to send in the low layer

View file

@ -624,7 +624,7 @@ void throughput_server_task(void *param)
assert(res == pdTRUE); assert(res == pdTRUE);
} else { } else {
if (is_connect) { if (is_connect) {
int free_buff_num = esp_ble_get_sendable_packets_num(); int free_buff_num = esp_ble_get_cur_sendable_packets_num(gl_profile_tab[PROFILE_A_APP_ID].conn_id);
if(free_buff_num > 0) { if(free_buff_num > 0) {
for( ; free_buff_num > 0; free_buff_num--) { 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, esp_ble_gatts_send_indicate(gl_profile_tab[PROFILE_A_APP_ID].gatts_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id,