Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_ble_pktlen_change_v4.4' into 'release/v4.4'
Browse files Browse the repository at this point in the history
Bugfix/fix ble pktlen change (v4.4)

See merge request espressif/esp-idf!31253
  • Loading branch information
jack0c committed Jun 13, 2024
2 parents 2146569 + 64b1fe9 commit d1c8a1d
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2363,6 +2363,7 @@ void btc_gap_ble_call_handler(btc_msg_t *msg)
//register connection parameter update callback
void btc_gap_callback_init(void)
{
BTM_BleRegiseterPktLengthChangeCallback(btc_set_pkt_length_callback);
BTM_BleRegiseterConnParamCallback(btc_update_conn_param_callback);
#if (BLE_50_FEATURE_SUPPORT == TRUE)
BTM_BleGapRegisterCallback(btc_ble_5_gap_callback);
Expand Down
18 changes: 16 additions & 2 deletions components/bt/host/bluedroid/stack/btm/btm_ble_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static tBTM_BLE_VSC_CB *cmn_ble_gap_vsc_cb_ptr;
static tBTM_BLE_CTRL_FEATURES_CBACK *p_ctrl_le_feature_rd_cmpl_cback = NULL;
#endif

tBTM_CallbackFunc conn_param_update_cb;
tBTM_CallbackFunc conn_callback_func;
/*******************************************************************************
** Local functions
*******************************************************************************/
Expand Down Expand Up @@ -309,7 +309,21 @@ void btm_ble_sem_free(void)
*******************************************************************************/
void BTM_BleRegiseterConnParamCallback(tBTM_UPDATE_CONN_PARAM_CBACK *update_conn_param_cb)
{
conn_param_update_cb.update_conn_param_cb = update_conn_param_cb;
conn_callback_func.update_conn_param_cb = update_conn_param_cb;
}

/*******************************************************************************
**
** Function BTM_BleRegiseterPktLengthChangeCallback
**
** Description Registers a callback function for packet length changes.
**
** Returns void
**
*******************************************************************************/
void BTM_BleRegiseterPktLengthChangeCallback(tBTM_SET_PKT_DATA_LENGTH_CBACK *ptk_len_chane_cb)
{
conn_callback_func.set_pkt_data_length_cb = ptk_len_chane_cb;
}

/*******************************************************************************
Expand Down
4 changes: 3 additions & 1 deletion components/bt/host/bluedroid/stack/btm/include/btm_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -959,9 +959,11 @@ typedef struct {
typedef struct{
//connection parameters update callback
tBTM_UPDATE_CONN_PARAM_CBACK *update_conn_param_cb;
// setting packet data length callback
tBTM_SET_PKT_DATA_LENGTH_CBACK *set_pkt_data_length_cb;
}tBTM_CallbackFunc;

extern tBTM_CallbackFunc conn_param_update_cb;
extern tBTM_CallbackFunc conn_callback_func;
/* security action for L2CAP COC channels */
#define BTM_SEC_OK 1
#define BTM_SEC_ENCRYPT 2 /* encrypt the link with current key */
Expand Down
1 change: 0 additions & 1 deletion components/bt/host/bluedroid/stack/btu/btu_hcif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,6 @@ static void btu_hcif_command_status_evt(uint8_t status, BT_HDR *command, void *c
hack->context = context;

event->event = BTU_POST_TO_TASK_NO_GOOD_HORRIBLE_HACK;

if (btu_task_post(SIG_BTU_HCI_MSG, event, OSI_THREAD_MAX_TIMEOUT) == false) {
osi_free(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,7 @@ extern "C" {
**
*******************************************************************************/
void BTM_BleRegiseterConnParamCallback(tBTM_UPDATE_CONN_PARAM_CBACK *update_conn_param_cb);
void BTM_BleRegiseterPktLengthChangeCallback(tBTM_SET_PKT_DATA_LENGTH_CBACK *ptk_len_chane_cb);

/*******************************************************************************
**
Expand Down
31 changes: 17 additions & 14 deletions components/bt/host/bluedroid/stack/l2cap/l2c_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ BOOLEAN L2CA_UpdateBleConnParams (BD_ADDR rem_bda, UINT16 min_int, UINT16 max_in
L2CAP_TRACE_ERROR("There are two connection parameter requests that are being updated, please try later ");
}

if ((need_cb == TRUE) && (conn_param_update_cb.update_conn_param_cb != NULL)) {
if ((need_cb == TRUE) && (conn_callback_func.update_conn_param_cb != NULL)) {
tBTM_LE_UPDATE_CONN_PRAMS update_param;
update_param.max_conn_int = max_int;
update_param.min_conn_int = min_int;
update_param.conn_int = p_lcb->current_used_conn_interval;
update_param.slave_latency = p_lcb->current_used_conn_latency;
update_param.supervision_tout = p_lcb->current_used_conn_timeout;
(conn_param_update_cb.update_conn_param_cb)(status, p_lcb->remote_bd_addr, &update_param);
(conn_callback_func.update_conn_param_cb)(status, p_lcb->remote_bd_addr, &update_param);
return (status == HCI_SUCCESS);
}

Expand Down Expand Up @@ -287,7 +287,7 @@ UINT8 L2CA_GetBleConnRole (BD_ADDR bd_addr)
**
** Function l2cble_notify_le_connection
**
** Description This function notifiy the l2cap connection to the app layer
** Description This function notify the l2cap connection to the app layer
**
** Returns none
**
Expand Down Expand Up @@ -647,7 +647,7 @@ void l2cble_process_conn_update_evt (UINT16 handle, UINT8 status, UINT16 conn_in
p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PARAM_FULL;
btu_stop_timer(&p_lcb->upda_con_timer);

if (conn_param_update_cb.update_conn_param_cb != NULL) {
if (conn_callback_func.update_conn_param_cb != NULL) {
l2c_send_update_conn_params_cb(p_lcb, status);
}

Expand Down Expand Up @@ -686,7 +686,7 @@ void l2cble_get_conn_param_format_err_from_contoller (UINT8 status, UINT16 handl

btu_stop_timer (&p_lcb->upda_con_timer);

if (conn_param_update_cb.update_conn_param_cb != NULL) {
if (conn_callback_func.update_conn_param_cb != NULL) {
l2c_send_update_conn_params_cb(p_lcb, status);
}
if ((p_lcb->conn_update_mask & L2C_BLE_UPDATE_PARAM_FULL) != 0){
Expand Down Expand Up @@ -868,7 +868,7 @@ void l2cble_process_sig_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
**
** Function l2cble_init_direct_conn
**
** Description This function is to initate a direct connection
** Description This function is to initiate a direct connection
**
** Returns TRUE connection initiated, FALSE otherwise.
**
Expand All @@ -894,7 +894,7 @@ BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb)

/* There can be only one BLE connection request outstanding at a time */
if (p_dev_rec == NULL) {
L2CAP_TRACE_WARNING ("unknown device, can not initate connection");
L2CAP_TRACE_WARNING ("unknown device, can not initiate connection");
return (FALSE);
}

Expand Down Expand Up @@ -947,7 +947,7 @@ BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb)

if (!btm_ble_topology_check(BTM_BLE_STATE_INIT)) {
l2cu_release_lcb (p_lcb);
L2CAP_TRACE_ERROR("initate direct connection fail, topology limitation");
L2CAP_TRACE_ERROR("initiate direct connection fail, topology limitation");
return FALSE;
}
uint32_t link_timeout = L2CAP_BLE_LINK_CONNECT_TOUT;
Expand Down Expand Up @@ -981,7 +981,7 @@ BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb)
BLE_CE_LEN_MIN, /* UINT16 min_len */
BLE_CE_LEN_MIN)) { /* UINT16 max_len */
l2cu_release_lcb (p_lcb);
L2CAP_TRACE_ERROR("initate direct connection fail, no resources");
L2CAP_TRACE_ERROR("initiate direct connection fail, no resources");
return (FALSE);
} else {
p_lcb->link_state = LST_CONNECTING;
Expand Down Expand Up @@ -1033,7 +1033,7 @@ BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb)
btm_ble_set_conn_st (BLE_DIR_CONN);
if(!btsnd_hcic_ble_create_ext_conn(&aux_conn)) {
l2cu_release_lcb (p_lcb);
L2CAP_TRACE_ERROR("initate Aux connection failed, no resources");
L2CAP_TRACE_ERROR("initiate Aux connection failed, no resources");
}
#else
L2CAP_TRACE_ERROR("BLE 5.0 not support!\n");
Expand Down Expand Up @@ -1324,15 +1324,18 @@ void l2cble_process_data_length_change_event(UINT16 handle, UINT16 tx_data_len,
if(p_acl) {
p_acl->data_length_params = data_length_params;
if (p_acl->p_set_pkt_data_cback) {
// Only when the corresponding API is called will the callback be registered
(*p_acl->p_set_pkt_data_cback)(BTM_SUCCESS, &data_length_params);
} else {
// If the callback is not registered,using global callback
(*conn_callback_func.set_pkt_data_length_cb)(BTM_SUCCESS, &data_length_params);
}

p_acl->data_len_updating = false;
if(p_acl->data_len_waiting) {
p_acl->data_len_waiting = false;
p_acl->p_set_pkt_data_cback = p_acl->p_set_data_len_cback_waiting;
p_acl->p_set_data_len_cback_waiting = NULL;
// if value is same, triger callback directly
// if value is same, trigger callback directly
if(p_acl->tx_len_waiting == p_acl->data_length_params.tx_len) {
if(p_acl->p_set_pkt_data_cback) {
(*p_acl->p_set_pkt_data_cback)(BTM_SUCCESS, &p_acl->data_length_params);
Expand Down Expand Up @@ -1396,7 +1399,7 @@ void l2cble_set_fixed_channel_tx_data_length(BD_ADDR remote_bda, UINT16 fix_cid,
*******************************************************************************/
void l2c_send_update_conn_params_cb(tL2C_LCB *p_lcb, UINT8 status)
{
if(conn_param_update_cb.update_conn_param_cb != NULL){
if(conn_callback_func.update_conn_param_cb != NULL){
tBTM_LE_UPDATE_CONN_PRAMS update_param;
//if myself update the connection parameters
if (p_lcb->updating_param_flag){
Expand All @@ -1412,7 +1415,7 @@ void l2c_send_update_conn_params_cb(tL2C_LCB *p_lcb, UINT8 status)
update_param.slave_latency = p_lcb->current_used_conn_latency;
update_param.supervision_tout = p_lcb->current_used_conn_timeout;

(conn_param_update_cb.update_conn_param_cb)(status, p_lcb->remote_bd_addr, &update_param);
(conn_callback_func.update_conn_param_cb)(status, p_lcb->remote_bd_addr, &update_param);
}
}

Expand Down
16 changes: 10 additions & 6 deletions examples/bluetooth/bluedroid/ble/gatt_client/main/gattc_demo.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/*
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/



Expand Down Expand Up @@ -402,6 +400,12 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par
param->update_conn_params.latency,
param->update_conn_params.timeout);
break;
case ESP_GAP_BLE_SET_PKT_LENGTH_COMPLETE_EVT:
ESP_LOGI(GATTC_TAG, "packet length updated: rx = %d, tx = %d, status = %d",
param->pkt_data_lenth_cmpl.params.rx_len,
param->pkt_data_lenth_cmpl.params.tx_len,
param->pkt_data_lenth_cmpl.status);
break;
default:
break;
}
Expand Down
28 changes: 17 additions & 11 deletions examples/bluetooth/bluedroid/ble/gatt_server/main/gatts_demo.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
Expand Down Expand Up @@ -233,6 +233,12 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param
param->update_conn_params.latency,
param->update_conn_params.timeout);
break;
case ESP_GAP_BLE_SET_PKT_LENGTH_COMPLETE_EVT:
ESP_LOGI(GATTS_TAG, "packet length updated: rx = %d, tx = %d, status = %d",
param->pkt_data_lenth_cmpl.params.rx_len,
param->pkt_data_lenth_cmpl.params.tx_len,
param->pkt_data_lenth_cmpl.status);
break;
default:
break;
}
Expand Down Expand Up @@ -283,7 +289,7 @@ void example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare

void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param){
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC){
esp_log_buffer_hex(GATTS_TAG, prepare_write_env->prepare_buf, prepare_write_env->prepare_len);
ESP_LOG_BUFFER_HEX(GATTS_TAG, prepare_write_env->prepare_buf, prepare_write_env->prepare_len);
}else{
ESP_LOGI(GATTS_TAG,"ESP_GATT_PREP_WRITE_CANCEL");
}
Expand Down Expand Up @@ -336,7 +342,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
esp_ble_gatts_create_service(gatts_if, &gl_profile_tab[PROFILE_A_APP_ID].service_id, GATTS_NUM_HANDLE_TEST_A);
break;
case ESP_GATTS_READ_EVT: {
ESP_LOGI(GATTS_TAG, "GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
ESP_LOGI(GATTS_TAG, "GATT_READ_EVT, conn_id %d, trans_id %u, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
esp_gatt_rsp_t rsp;
memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
rsp.attr_value.handle = param->read.handle;
Expand All @@ -350,10 +356,10 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
break;
}
case ESP_GATTS_WRITE_EVT: {
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d", param->write.conn_id, param->write.trans_id, param->write.handle);
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %u, handle %d", param->write.conn_id, param->write.trans_id, param->write.handle);
if (!param->write.is_prep){
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value :", param->write.len);
esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len);
ESP_LOG_BUFFER_HEX(GATTS_TAG, param->write.value, param->write.len);
if (gl_profile_tab[PROFILE_A_APP_ID].descr_handle == param->write.handle && param->write.len == 2){
uint16_t descr_value = param->write.value[1]<<8 | param->write.value[0];
if (descr_value == 0x0001){
Expand Down Expand Up @@ -385,7 +391,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
ESP_LOGI(GATTS_TAG, "notify/indicate disable ");
}else{
ESP_LOGE(GATTS_TAG, "unknown descr value");
esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len);
ESP_LOG_BUFFER_HEX(GATTS_TAG, param->write.value, param->write.len);
}

}
Expand Down Expand Up @@ -483,7 +489,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
case ESP_GATTS_CONF_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONF_EVT, status %d attr_handle %d", param->conf.status, param->conf.handle);
if (param->conf.status != ESP_GATT_OK){
esp_log_buffer_hex(GATTS_TAG, param->conf.value, param->conf.len);
ESP_LOG_BUFFER_HEX(GATTS_TAG, param->conf.value, param->conf.len);
}
break;
case ESP_GATTS_OPEN_EVT:
Expand All @@ -508,7 +514,7 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
esp_ble_gatts_create_service(gatts_if, &gl_profile_tab[PROFILE_B_APP_ID].service_id, GATTS_NUM_HANDLE_TEST_B);
break;
case ESP_GATTS_READ_EVT: {
ESP_LOGI(GATTS_TAG, "GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
ESP_LOGI(GATTS_TAG, "GATT_READ_EVT, conn_id %d, trans_id %u, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
esp_gatt_rsp_t rsp;
memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
rsp.attr_value.handle = param->read.handle;
Expand All @@ -522,10 +528,10 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
break;
}
case ESP_GATTS_WRITE_EVT: {
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle);
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %u, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle);
if (!param->write.is_prep){
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value :", param->write.len);
esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len);
ESP_LOG_BUFFER_HEX(GATTS_TAG, param->write.value, param->write.len);
if (gl_profile_tab[PROFILE_B_APP_ID].descr_handle == param->write.handle && param->write.len == 2){
uint16_t descr_value= param->write.value[1]<<8 | param->write.value[0];
if (descr_value == 0x0001){
Expand Down Expand Up @@ -626,7 +632,7 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
case ESP_GATTS_CONF_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONF_EVT status %d attr_handle %d", param->conf.status, param->conf.handle);
if (param->conf.status != ESP_GATT_OK){
esp_log_buffer_hex(GATTS_TAG, param->conf.value, param->conf.len);
ESP_LOG_BUFFER_HEX(GATTS_TAG, param->conf.value, param->conf.len);
}
break;
case ESP_GATTS_DISCONNECT_EVT:
Expand Down

0 comments on commit d1c8a1d

Please sign in to comment.