From 3176c707daeee7dbd152a597ee77100edec71d79 Mon Sep 17 00:00:00 2001 From: xiewenxiang Date: Sat, 13 Nov 2021 19:21:05 +0800 Subject: [PATCH] component/bt: add link role param for gatt disconnection event(backport v4.4) --- .../bluedroid/api/include/api/esp_gattc_api.h | 1 + .../bluedroid/api/include/api/esp_gatts_api.h | 1 + .../host/bluedroid/bta/gatt/bta_gattc_act.c | 5 +-- .../host/bluedroid/bta/gatt/bta_gattc_utils.c | 3 +- .../host/bluedroid/bta/gatt/bta_gatts_act.c | 3 +- .../bta/gatt/include/bta_gattc_int.h | 2 +- .../bluedroid/bta/include/bta/bta_gatt_api.h | 1 + .../btc/profile/std/gatt/btc_gattc.c | 1 + .../btc/profile/std/gatt/btc_gatts.c | 1 + .../bluedroid/stack/l2cap/include/l2c_int.h | 1 + .../bt/host/bluedroid/stack/l2cap/l2c_utils.c | 31 +++++++++++++++++++ 11 files changed, 45 insertions(+), 5 deletions(-) diff --git a/components/bt/host/bluedroid/api/include/api/esp_gattc_api.h b/components/bt/host/bluedroid/api/include/api/esp_gattc_api.h index 4071909634..6c32868156 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_gattc_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_gattc_api.h @@ -220,6 +220,7 @@ typedef union { struct gattc_disconnect_evt_param { esp_gatt_conn_reason_t reason; /*!< disconnection reason */ uint16_t conn_id; /*!< Connection id */ + uint8_t link_role; /*!< Link role : master role = 0 ; slave role = 1*/ esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */ } disconnect; /*!< Gatt client callback param of ESP_GATTC_DISCONNECT_EVT */ /** diff --git a/components/bt/host/bluedroid/api/include/api/esp_gatts_api.h b/components/bt/host/bluedroid/api/include/api/esp_gatts_api.h index 475ae08c55..558d5d6296 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_gatts_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_gatts_api.h @@ -206,6 +206,7 @@ typedef union { */ struct gatts_disconnect_evt_param { uint16_t conn_id; /*!< Connection id */ + uint8_t link_role; /*!< Link role : master role = 0 ; slave role = 1*/ esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */ esp_gatt_conn_reason_t reason; /*!< Indicate the reason of disconnection */ } disconnect; /*!< Gatt server callback param of ESP_GATTS_DISCONNECT_EVT */ diff --git a/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c b/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c index b4fb4966a0..3baa259993 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c @@ -763,7 +763,8 @@ void bta_gattc_disconncback(tBTA_GATTC_RCB *p_rcb, tBTA_GATTC_DATA *p_data) bta_gattc_send_disconnect_cback(p_rcb, p_data->int_conn.reason, p_data->int_conn.remote_bda, - p_data->int_conn.hdr.layer_specific); + p_data->int_conn.hdr.layer_specific, + p_data->int_conn.role); } } @@ -1716,7 +1717,7 @@ static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id, } p_buf->int_conn.hdr.layer_specific = conn_id; p_buf->int_conn.client_if = gattc_if; - p_buf->int_conn.role = L2CA_GetBleConnRole(bda); + p_buf->int_conn.role = l2cu_find_link_role_by_bd_addr(bda, BT_TRANSPORT_LE); p_buf->int_conn.reason = reason; p_buf->int_conn.transport = transport; bdcpy(p_buf->int_conn.remote_bda, bda); diff --git a/components/bt/host/bluedroid/bta/gatt/bta_gattc_utils.c b/components/bt/host/bluedroid/bta/gatt/bta_gattc_utils.c index bdb712cb32..2116d09944 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gattc_utils.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gattc_utils.c @@ -794,7 +794,7 @@ void bta_gattc_send_connect_cback( tBTA_GATTC_RCB *p_clreg, BD_ADDR remote_bda, ** *******************************************************************************/ void bta_gattc_send_disconnect_cback( tBTA_GATTC_RCB *p_clreg, tGATT_DISCONN_REASON reason, - BD_ADDR remote_bda, UINT16 conn_id) + BD_ADDR remote_bda, UINT16 conn_id, UINT8 link_role) { tBTA_GATTC cb_data; @@ -804,6 +804,7 @@ void bta_gattc_send_disconnect_cback( tBTA_GATTC_RCB *p_clreg, tGATT_DISCONN_REA cb_data.disconnect.reason = reason; cb_data.disconnect.client_if = p_clreg->client_if; cb_data.disconnect.conn_id = conn_id; + cb_data.disconnect.link_role = link_role; bdcpy(cb_data.disconnect.remote_bda, remote_bda); (*p_clreg->p_cback)(BTA_GATTC_DISCONNECT_EVT, &cb_data); diff --git a/components/bt/host/bluedroid/bta/gatt/bta_gatts_act.c b/components/bt/host/bluedroid/bta/gatt/bta_gatts_act.c index adcb2830db..527883be6d 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gatts_act.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gatts_act.c @@ -996,17 +996,18 @@ static void bta_gatts_conn_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id, bta_sys_conn_close( BTA_ID_GATTS , BTA_ALL_APP_ID, bda); } } + if(evt == BTA_GATTS_CONNECT_EVT) { tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(bda, BT_TRANSPORT_LE); if(p_lcb != NULL) { cb_data.conn.conn_params.interval = p_lcb->current_used_conn_interval; cb_data.conn.conn_params.latency = p_lcb->current_used_conn_latency; cb_data.conn.conn_params.timeout = p_lcb->current_used_conn_timeout; - cb_data.conn.link_role = p_lcb->link_role; }else { APPL_TRACE_WARNING("%s not found connection parameters of the device ", __func__); } } + cb_data.conn.link_role = l2cu_find_link_role_by_bd_addr(bda, BT_TRANSPORT_LE); cb_data.conn.conn_id = conn_id; cb_data.conn.server_if = gatt_if; cb_data.conn.reason = reason; diff --git a/components/bt/host/bluedroid/bta/gatt/include/bta_gattc_int.h b/components/bt/host/bluedroid/bta/gatt/include/bta_gattc_int.h index 06a8f5f187..b42507414b 100644 --- a/components/bt/host/bluedroid/bta/gatt/include/bta_gattc_int.h +++ b/components/bt/host/bluedroid/bta/gatt/include/bta_gattc_int.h @@ -477,7 +477,7 @@ extern void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS BD_ADDR remote_bda, UINT16 conn_id, tBTA_TRANSPORT transport, UINT16 mtu); extern void bta_gattc_send_connect_cback( tBTA_GATTC_RCB *p_clreg, BD_ADDR remote_bda, UINT16 conn_id, tBTA_GATT_CONN_PARAMS conn_params, UINT8 link_role); extern void bta_gattc_send_disconnect_cback( tBTA_GATTC_RCB *p_clreg, tGATT_DISCONN_REASON reason, - BD_ADDR remote_bda, UINT16 conn_id); + BD_ADDR remote_bda, UINT16 conn_id, UINT8 link_role); extern void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg); extern void bta_gattc_process_api_cache_clean(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg); extern void bta_gattc_process_api_cache_assoc(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg); diff --git a/components/bt/host/bluedroid/bta/include/bta/bta_gatt_api.h b/components/bt/host/bluedroid/bta/include/bta/bta_gatt_api.h index 03e22ca20f..3b0949832b 100644 --- a/components/bt/host/bluedroid/bta/include/bta/bta_gatt_api.h +++ b/components/bt/host/bluedroid/bta/include/bta/bta_gatt_api.h @@ -415,6 +415,7 @@ typedef struct { typedef struct { tGATT_DISCONN_REASON reason; UINT16 conn_id; + UINT8 link_role; tBTA_GATTC_IF client_if; BD_ADDR remote_bda; } tBTA_GATTC_DISCONNECT; diff --git a/components/bt/host/bluedroid/btc/profile/std/gatt/btc_gattc.c b/components/bt/host/bluedroid/btc/profile/std/gatt/btc_gattc.c index cc79e09eb9..5e13f70704 100644 --- a/components/bt/host/bluedroid/btc/profile/std/gatt/btc_gattc.c +++ b/components/bt/host/bluedroid/btc/profile/std/gatt/btc_gattc.c @@ -932,6 +932,7 @@ void btc_gattc_cb_handler(btc_msg_t *msg) gattc_if = disconnect->client_if; param.disconnect.reason = disconnect->reason; + param.disconnect.link_role = disconnect->link_role; param.disconnect.conn_id = BTC_GATT_GET_CONN_ID(disconnect->conn_id); memcpy(param.disconnect.remote_bda, disconnect->remote_bda, sizeof(esp_bd_addr_t)); btc_gattc_cb_to_app(ESP_GATTC_DISCONNECT_EVT, gattc_if, ¶m); diff --git a/components/bt/host/bluedroid/btc/profile/std/gatt/btc_gatts.c b/components/bt/host/bluedroid/btc/profile/std/gatt/btc_gatts.c index 4f49c8872e..e2bcb63f2a 100644 --- a/components/bt/host/bluedroid/btc/profile/std/gatt/btc_gatts.c +++ b/components/bt/host/bluedroid/btc/profile/std/gatt/btc_gatts.c @@ -903,6 +903,7 @@ void btc_gatts_cb_handler(btc_msg_t *msg) gatts_if = p_data->conn.server_if; param.disconnect.conn_id = BTC_GATT_GET_CONN_ID(p_data->conn.conn_id); param.disconnect.reason = p_data->conn.reason; + param.disconnect.link_role = p_data->conn.link_role; memcpy(param.disconnect.remote_bda, p_data->conn.remote_bda, ESP_BD_ADDR_LEN); btc_gatts_cb_to_app(ESP_GATTS_DISCONNECT_EVT, gatts_if, ¶m); diff --git a/components/bt/host/bluedroid/stack/l2cap/include/l2c_int.h b/components/bt/host/bluedroid/stack/l2cap/include/l2c_int.h index 49014e582a..7fac44c1b0 100644 --- a/components/bt/host/bluedroid/stack/l2cap/include/l2c_int.h +++ b/components/bt/host/bluedroid/stack/l2cap/include/l2c_int.h @@ -597,6 +597,7 @@ extern void l2c_process_held_packets (BOOLEAN timed_out); extern tL2C_LCB *l2cu_allocate_lcb (BD_ADDR p_bd_addr, BOOLEAN is_bonding, tBT_TRANSPORT transport); extern BOOLEAN l2cu_start_post_bond_timer (UINT16 handle); extern void l2cu_release_lcb (tL2C_LCB *p_lcb); +extern UINT8 l2cu_find_link_role_by_bd_addr (BD_ADDR p_bd_addr, tBT_TRANSPORT transport); extern tL2C_LCB *l2cu_find_lcb_by_bd_addr (BD_ADDR p_bd_addr, tBT_TRANSPORT transport); extern tL2C_LCB *l2cu_find_lcb_by_handle (UINT16 handle); extern uint8_t l2cu_plcb_active_count(void); diff --git a/components/bt/host/bluedroid/stack/l2cap/l2c_utils.c b/components/bt/host/bluedroid/stack/l2cap/l2c_utils.c index f77ab9ee71..9cd12d53d2 100644 --- a/components/bt/host/bluedroid/stack/l2cap/l2c_utils.c +++ b/components/bt/host/bluedroid/stack/l2cap/l2c_utils.c @@ -288,6 +288,37 @@ void l2cu_release_lcb (tL2C_LCB *p_lcb) #endif ///BLE_INCLUDED == TRUE } +/******************************************************************************* +** +** Function l2cu_find_link_role_by_bd_addr +** +** Description Look through all active Link Role for a match based on the +** remote BD address. +** +** Returns Link Role, or HCI_ROLE_UNKNOWN if no match +** +*******************************************************************************/ +UINT8 l2cu_find_link_role_by_bd_addr (BD_ADDR p_bd_addr, tBT_TRANSPORT transport) +{ + list_node_t *p_node = NULL; + tL2C_LCB *p_lcb = NULL; + UINT8 link_role = HCI_ROLE_UNKNOWN; + + for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) { + p_lcb = list_node(p_node); + if ((p_lcb) && +#if BLE_INCLUDED == TRUE + p_lcb->transport == transport && +#endif + (!memcmp (p_lcb->remote_bd_addr, p_bd_addr, BD_ADDR_LEN))) { + link_role = p_lcb->link_role; + } + } + + /* If here, no match found */ + return link_role; +} + /******************************************************************************* **