From ccc3dad9fa1fd6ccfa0a654a8c96bdcdb994fd72 Mon Sep 17 00:00:00 2001 From: xiongweichao Date: Wed, 24 Dec 2025 19:07:49 +0800 Subject: [PATCH 1/2] fix(bt): fix l2cap malloc fail in throughput test --- .../bluedroid/btc/profile/std/l2cap/btc_l2cap.c | 13 +++++++------ .../bluedroid/common/include/common/bt_target.h | 2 +- components/bt/host/bluedroid/stack/gap/gap_conn.c | 7 ++++--- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/components/bt/host/bluedroid/btc/profile/std/l2cap/btc_l2cap.c b/components/bt/host/bluedroid/btc/profile/std/l2cap/btc_l2cap.c index 84641f31c8..5bd0208e7c 100644 --- a/components/bt/host/bluedroid/btc/profile/std/l2cap/btc_l2cap.c +++ b/components/bt/host/bluedroid/btc/profile/std/l2cap/btc_l2cap.c @@ -42,6 +42,7 @@ #define VFS_CLOSE_TIMEOUT (20 * 1000) #define BTC_L2CAP_ROLE_MASTER 0 #define BTC_L2CAP_ROLE_SLAVE 1 +#define BTC_L2CAP_RX_MTU (990) typedef struct { bool peer_fc; /* true if flow control is set based on peer's request */ @@ -101,10 +102,10 @@ static const tL2CAP_ERTM_INFO obex_l2c_etm_opt = { L2CAP_FCR_ERTM_MODE, /* Mandatory for OBEX over l2cap */ L2CAP_FCR_CHAN_OPT_ERTM, /* Mandatory for OBEX over l2cap */ - OBX_USER_RX_POOL_ID, - OBX_USER_TX_POOL_ID, - OBX_FCR_RX_POOL_ID, - OBX_FCR_TX_POOL_ID + L2CAP_USER_RX_BUF_SIZE, + L2CAP_USER_TX_BUF_SIZE, + L2CAP_FCR_RX_BUF_SIZE, + L2CAP_FCR_TX_BUF_SIZE }; #if L2CAP_DYNAMIC_MEMORY == FALSE @@ -554,7 +555,7 @@ static void btc_l2cap_start_srv(btc_l2cap_args_t *arg) cfg.fcr_present = TRUE; cfg.fcr = obex_l2c_fcr_opts_def; BTA_JvL2capStartServer(slot->security, slot->role, &obex_l2c_etm_opt, slot->psm, - L2CAP_MAX_SDU_LENGTH, &cfg, (tBTA_JV_L2CAP_CBACK *)btc_l2cap_inter_cb, (void *)slot->id); + BTC_L2CAP_RX_MTU, &cfg, (tBTA_JV_L2CAP_CBACK *)btc_l2cap_inter_cb, (void *)slot->id); osi_mutex_unlock(&l2cap_local_param.l2cap_slot_mutex); } while(0); @@ -695,7 +696,7 @@ static void btc_l2cap_connect(btc_l2cap_args_t *arg) cfg.fcr = obex_l2c_fcr_opts_def; BTA_JvL2capConnect(slot->security, slot->role, &obex_l2c_etm_opt, slot->psm, - L2CAP_MAX_SDU_LENGTH, &cfg, slot->addr, (tBTA_JV_L2CAP_CBACK *)btc_l2cap_inter_cb, (void *)slot->id); + BTC_L2CAP_RX_MTU, &cfg, slot->addr, (tBTA_JV_L2CAP_CBACK *)btc_l2cap_inter_cb, (void *)slot->id); osi_mutex_unlock(&l2cap_local_param.l2cap_slot_mutex); } while (0); diff --git a/components/bt/host/bluedroid/common/include/common/bt_target.h b/components/bt/host/bluedroid/common/include/common/bt_target.h index 8b66f05528..844b8b2850 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -1992,7 +1992,7 @@ * in basic and streaming modes. Range: 1 - 63 */ #ifndef OBX_FCR_OPT_TX_WINDOW_SIZE_BR_EDR -#define OBX_FCR_OPT_TX_WINDOW_SIZE_BR_EDR 20 +#define OBX_FCR_OPT_TX_WINDOW_SIZE_BR_EDR 10 #endif /* diff --git a/components/bt/host/bluedroid/stack/gap/gap_conn.c b/components/bt/host/bluedroid/stack/gap/gap_conn.c index cdd6a2d3a9..2fa54fa923 100644 --- a/components/bt/host/bluedroid/stack/gap/gap_conn.c +++ b/components/bt/host/bluedroid/stack/gap/gap_conn.c @@ -510,18 +510,19 @@ UINT16 GAP_ConnWriteData (UINT16 gap_handle, UINT8 *p_data, UINT16 max_len, UINT } while (max_len) { + UINT16 length = (p_ccb->rem_mtu_size < max_len) ? p_ccb->rem_mtu_size : max_len; if (p_ccb->cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) { - if ((p_buf = (BT_HDR *)osi_malloc(L2CAP_FCR_ERTM_BUF_SIZE)) == NULL) { + if ((p_buf = (BT_HDR *)osi_malloc(BT_HDR_SIZE + length + L2CAP_MIN_OFFSET + L2CAP_FCS_LEN)) == NULL) { return (GAP_ERR_CONGESTED); } } else { - if ((p_buf = (BT_HDR *)osi_malloc(GAP_DATA_BUF_SIZE)) == NULL) { + if ((p_buf = (BT_HDR *)osi_malloc(BT_HDR_SIZE + length + L2CAP_MIN_OFFSET)) == NULL) { return (GAP_ERR_CONGESTED); } } p_buf->offset = L2CAP_MIN_OFFSET; - p_buf->len = (p_ccb->rem_mtu_size < max_len) ? p_ccb->rem_mtu_size : max_len; + p_buf->len = length; p_buf->event = BT_EVT_TO_BTU_SP_DATA; memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, p_buf->len); From 419d324e361f662578f61a9a6e6415a902c95e65 Mon Sep 17 00:00:00 2001 From: xiongweichao Date: Wed, 24 Dec 2025 19:21:54 +0800 Subject: [PATCH 2/2] fix(bt): retry when L2CAP write fails --- .../host/bluedroid/btc/profile/std/l2cap/btc_l2cap.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/components/bt/host/bluedroid/btc/profile/std/l2cap/btc_l2cap.c b/components/bt/host/bluedroid/btc/profile/std/l2cap/btc_l2cap.c index 5bd0208e7c..5f4bb5f110 100644 --- a/components/bt/host/bluedroid/btc/profile/std/l2cap/btc_l2cap.c +++ b/components/bt/host/bluedroid/btc/profile/std/l2cap/btc_l2cap.c @@ -1026,6 +1026,15 @@ void btc_l2cap_cb_handler(btc_msg_t *msg) p_buf->event++; BTA_JvL2capWrite(p_data->l2c_write.handle, slot->id, p_buf->data + p_buf->offset, p_buf->len, (void *)slot->id); } + } else { + if (!p_data->l2c_write.cong && slot->connected) { + // retry + BTA_JvL2capWrite(p_data->l2c_write.handle, slot->id, p_buf->data + p_buf->offset, p_buf->len, (void *)slot->id); + } else { + p_buf->event--; + // Reset layer-specific flag to 0, marking packet as ready for transmission + p_buf->layer_specific = 0; + } } } osi_mutex_unlock(&l2cap_local_param.l2cap_slot_mutex); @@ -1086,7 +1095,7 @@ static ssize_t l2cap_vfs_write(int fd, const void * data, size_t size) } p_buf->offset = 0; p_buf->len = write_size; - p_buf->event = 0; // indicate the p_buf be sent count + p_buf->event = 0; // Indicate the count of successful sends of p_buf p_buf->layer_specific = 0; // indicate the p_buf whether to be sent, 0 - ready to send; 1 - have sent memcpy((UINT8 *)(p_buf + 1), data + sent, write_size); }