Procházet zdrojové kódy

Component/bt: add new api to get connection parameters

zhiweijian před 6 roky
rodič
revize
3eacc32c8c

+ 11 - 0
components/bt/bluedroid/api/esp_gap_ble_api.c

@@ -730,6 +730,17 @@ esp_err_t esp_gap_ble_set_authorization(esp_bd_addr_t bd_addr, bool authorize)
     }
     return ESP_FAIL;
 }
+esp_err_t esp_ble_get_current_conn_params(esp_bd_addr_t bd_addr, esp_gap_conn_params_t *conn_params)
+{
+    if(!bd_addr || !conn_params) {
+        return ESP_ERR_INVALID_ARG;
+    }
+    if(BTM_GetCurrentConnParams(bd_addr, &conn_params->interval, &conn_params->latency, &conn_params->timeout)) {
+        return ESP_OK;
+    }
+    return ESP_ERR_NOT_FOUND;
+}
+
 
 
 

+ 23 - 0
components/bt/bluedroid/api/include/api/esp_gap_ble_api.h

@@ -390,6 +390,15 @@ typedef struct {
                                                         advertising reports for each packet received */
 } esp_ble_scan_params_t;
 
+/// connection parameters information 
+typedef struct {
+    uint16_t             interval;                  /*!< connection interval */
+    uint16_t             latency;                   /*!< Slave latency for the connection in number of connection events. Range: 0x0000 to 0x01F3 */
+    uint16_t             timeout;                   /*!< Supervision timeout for the LE Link. Range: 0x000A to 0x0C80.
+                                                      Mandatory Range: 0x000A to 0x0C80 Time = N * 10 msec
+                                                      Time Range: 100 msec to 32 seconds */
+} esp_gap_conn_params_t;
+
 /// Connection update parameters
 typedef struct {
     esp_bd_addr_t bda;                              /*!< Bluetooth device address */
@@ -1257,6 +1266,20 @@ esp_err_t esp_ble_gap_disconnect(esp_bd_addr_t remote_device);
 *
 */
 esp_err_t esp_gap_ble_set_authorization(esp_bd_addr_t bd_addr, bool authorize);
+
+/**
+* @brief           This function is called to read the connection
+*                  parameters information of the device
+*
+* @param[in]       bd_addr: BD address of the peer device.
+* @param[out]      conn_params: the connection parameters information
+*
+* @return          - ESP_OK : success
+*                  - other  : failed
+*
+*/
+esp_err_t esp_ble_get_current_conn_params(esp_bd_addr_t bd_addr, esp_gap_conn_params_t *conn_params);
+
 #ifdef __cplusplus
 }
 #endif

+ 11 - 0
components/bt/bluedroid/api/include/api/esp_gatt_defs.h

@@ -395,6 +395,17 @@ typedef enum {
     ESP_GATT_WRITE_TYPE_RSP,                                /*!< Gatt write attribute need remote response */
 } esp_gatt_write_type_t;
 
+/**
+  * @brief Connection parameters information
+  */
+typedef struct {
+    uint16_t             interval;                         /*!< connection interval */
+    uint16_t             latency;                          /*!< Slave latency for the connection in number of connection events. Range: 0x0000 to 0x01F3 */
+    uint16_t             timeout;                          /*!< Supervision timeout for the LE Link. Range: 0x000A to 0x0C80.
+                                                                Mandatory Range: 0x000A to 0x0C80 Time = N * 10 msec
+                                                                Time Range: 100 msec to 32 seconds */
+} esp_gatt_conn_params_t;
+
 #define ESP_GATT_IF_NONE    0xff                            /*!< If callback report gattc_if/gatts_if as this macro, means this event is not correspond to any app */
 
 typedef uint8_t    esp_gatt_if_t;                           /*!< Gatt interface type, different application on GATT client use different gatt_if */

+ 1 - 0
components/bt/bluedroid/api/include/api/esp_gattc_api.h

@@ -209,6 +209,7 @@ typedef union {
     struct gattc_connect_evt_param {
         uint16_t conn_id;               /*!< Connection id */
         esp_bd_addr_t remote_bda;       /*!< Remote bluetooth device address */
+        esp_gatt_conn_params_t conn_params; /*!< current connection parameters */
     } connect;                          /*!< Gatt client callback param of ESP_GATTC_CONNECT_EVT */
 
     /**

+ 1 - 0
components/bt/bluedroid/api/include/api/esp_gatts_api.h

@@ -197,6 +197,7 @@ typedef union {
     struct gatts_connect_evt_param {
         uint16_t conn_id;               /*!< Connection id */
         esp_bd_addr_t remote_bda;       /*!< Remote bluetooth device address */
+        esp_gatt_conn_params_t conn_params; /*!< current Connection parameters */
     } connect;                          /*!< Gatt server callback param of ESP_GATTS_CONNECT_EVT */
 
     /**

+ 11 - 1
components/bt/bluedroid/bta/gatt/bta_gattc_act.c

@@ -729,7 +729,7 @@ void bta_gattc_conncback(tBTA_GATTC_RCB *p_rcb, tBTA_GATTC_DATA *p_data)
     if (p_rcb) {
         bta_gattc_send_connect_cback(p_rcb,
                                      p_data->int_conn.remote_bda,
-                                     p_data->int_conn.hdr.layer_specific);
+                                     p_data->int_conn.hdr.layer_specific, p_data->int_conn.conn_params);
 
     }
 }
@@ -1686,6 +1686,16 @@ static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
 
         p_buf->int_conn.hdr.event            = connected ? BTA_GATTC_INT_CONN_EVT :
                                                BTA_GATTC_INT_DISCONN_EVT;
+        if(p_buf->int_conn.hdr.event == BTA_GATTC_INT_CONN_EVT) {
+            tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(bda, BT_TRANSPORT_LE);
+            if(p_lcb != NULL) {
+                p_buf->int_conn.conn_params.interval = p_lcb->current_used_conn_interval;
+                p_buf->int_conn.conn_params.latency = p_lcb->current_used_conn_latency;
+                p_buf->int_conn.conn_params.timeout = p_lcb->current_used_conn_timeout;
+            } else {
+                APPL_TRACE_WARNING("%s not found connection parameters of the device ", __func__);
+            }
+        } 
         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);

+ 4 - 1
components/bt/bluedroid/bta/gatt/bta_gattc_utils.c

@@ -765,7 +765,7 @@ void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status
 ** Returns
 **
 *******************************************************************************/
-void bta_gattc_send_connect_cback( tBTA_GATTC_RCB *p_clreg, BD_ADDR remote_bda, UINT16 conn_id)
+void bta_gattc_send_connect_cback( tBTA_GATTC_RCB *p_clreg, BD_ADDR remote_bda, UINT16 conn_id, tBTA_GATT_CONN_PARAMS conn_params)
 {
     tBTA_GATTC      cb_data;
 
@@ -774,6 +774,9 @@ void bta_gattc_send_connect_cback( tBTA_GATTC_RCB *p_clreg, BD_ADDR remote_bda,
 
         cb_data.connect.client_if = p_clreg->client_if;
         cb_data.connect.conn_id = conn_id;
+        cb_data.connect.conn_params.interval = conn_params.interval;
+        cb_data.connect.conn_params.latency = conn_params.latency;
+        cb_data.connect.conn_params.timeout = conn_params.timeout;
         bdcpy(cb_data.connect.remote_bda, remote_bda);
 
         (*p_clreg->p_cback)(BTA_GATTC_CONNECT_EVT, &cb_data);

+ 12 - 2
components/bt/bluedroid/bta/gatt/bta_gatts_act.c

@@ -36,6 +36,7 @@
 #include <string.h>
 #include "osi/allocator.h"
 #include "stack/l2c_api.h"
+#include "l2c_int.h"
 
 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,
@@ -967,7 +968,7 @@ static void bta_gatts_conn_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
                                   BOOLEAN connected, tGATT_DISCONN_REASON reason,
                                   tGATT_TRANSPORT transport)
 {
-    tBTA_GATTS      cb_data;
+    tBTA_GATTS      cb_data = {0};
     UINT8           evt = connected ? BTA_GATTS_CONNECT_EVT : BTA_GATTS_DISCONNECT_EVT;
     tBTA_GATTS_RCB  *p_reg;
 
@@ -995,7 +996,16 @@ 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;
+            }else {
+                APPL_TRACE_WARNING("%s not found connection parameters of the device ", __func__);
+            }
+        }
         cb_data.conn.conn_id = conn_id;
         cb_data.conn.server_if = gatt_if;
         cb_data.conn.reason = reason;

+ 2 - 1
components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h

@@ -220,6 +220,7 @@ typedef struct {
     tBT_TRANSPORT           transport;
     tGATT_DISCONN_REASON    reason;
     BOOLEAN                 already_connect;
+    tBTA_GATT_CONN_PARAMS   conn_params;
 } tBTA_GATTC_INT_CONN;
 
 typedef struct {
@@ -472,7 +473,7 @@ extern void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN *p_data, tBTA_GATTC_RCB *
 extern void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN *p_data);
 extern void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS 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);
+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);
 extern void bta_gattc_send_disconnect_cback( tBTA_GATTC_RCB *p_clreg, tGATT_DISCONN_REASON reason,
                                 BD_ADDR remote_bda, UINT16 conn_id);
 extern void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg);

+ 8 - 0
components/bt/bluedroid/bta/include/bta/bta_gatt_api.h

@@ -200,6 +200,12 @@ typedef struct {
     UINT8               name_spc;   /* The name space of the description */
 } tBTA_GATT_CHAR_PRES;
 
+typedef struct {
+    UINT16             interval;
+    UINT16             latency;
+    UINT16             timeout;
+} tBTA_GATT_CONN_PARAMS;
+
 #define BTA_GATT_CLT_CONFIG_NONE               GATT_CLT_CONFIG_NONE         /* 0x0000    */
 #define BTA_GATT_CLT_CONFIG_NOTIFICATION       GATT_CLT_CONFIG_NOTIFICATION /* 0x0001 */
 #define BTA_GATT_CLT_CONFIG_INDICATION         GATT_CLT_CONFIG_INDICATION   /* 0x0002 */
@@ -397,6 +403,7 @@ typedef struct {
     UINT16              conn_id;
     tBTA_GATTC_IF       client_if;
     BD_ADDR             remote_bda;
+    tBTA_GATT_CONN_PARAMS conn_params;
 } tBTA_GATTC_CONNECT;
 
 typedef struct {
@@ -606,6 +613,7 @@ typedef struct {
     UINT16              conn_id;
     tBTA_GATT_REASON    reason; /* report disconnect reason */
     tBTA_GATT_TRANSPORT transport;
+    tBTA_GATT_CONN_PARAMS conn_params;
 } tBTA_GATTS_CONN;
 
 typedef struct {

+ 3 - 0
components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c

@@ -909,6 +909,9 @@ void btc_gattc_cb_handler(btc_msg_t *msg)
         gattc_if = connect->client_if;
         param.connect.conn_id = BTC_GATT_GET_CONN_ID(connect->conn_id);
         memcpy(param.connect.remote_bda, connect->remote_bda, sizeof(esp_bd_addr_t));
+        param.connect.conn_params.interval = connect->conn_params.interval;
+        param.connect.conn_params.latency = connect->conn_params.latency;
+        param.connect.conn_params.timeout = connect->conn_params.timeout;
         btc_gattc_cb_to_app(ESP_GATTC_CONNECT_EVT, gattc_if, &param);
         break;
     }

+ 3 - 1
components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c

@@ -907,7 +907,9 @@ void btc_gatts_cb_handler(btc_msg_t *msg)
         gatts_if = p_data->conn.server_if;
         param.connect.conn_id = BTC_GATT_GET_CONN_ID(p_data->conn.conn_id);
         memcpy(param.connect.remote_bda, p_data->conn.remote_bda, ESP_BD_ADDR_LEN);
-
+        param.connect.conn_params.interval = p_data->conn.conn_params.interval;
+        param.connect.conn_params.latency = p_data->conn.conn_params.latency;
+        param.connect.conn_params.timeout = p_data->conn.conn_params.timeout;
         btc_gatts_cb_to_app(ESP_GATTS_CONNECT_EVT, gatts_if, &param);
         break;
     case BTA_GATTS_DISCONNECT_EVT:

+ 31 - 0
components/bt/bluedroid/stack/btm/btm_ble.c

@@ -2728,4 +2728,35 @@ void btm_ble_set_keep_rfu_in_auth_req(BOOLEAN keep_rfu)
 
 #endif /* BTM_BLE_CONFORMANCE_TESTING */
 
+/*******************************************************************************
+**
+** Function         btm_get_current_conn_params
+**
+** Description      This function is called to get current connection parameters
+**                  information of the device           
+**
+** Returns          TRUE if the information is geted, else FALSE
+**
+*******************************************************************************/
+
+BOOLEAN btm_get_current_conn_params(BD_ADDR bda, UINT16 *interval, UINT16 *latency, UINT16 *timeout)
+{
+    if( (interval == NULL) || (latency == NULL) || (timeout == NULL) ) {
+        BTM_TRACE_ERROR("%s invalid parameters ", __func__);
+        return FALSE;
+    }
+
+    tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(bda, BT_TRANSPORT_LE);
+    if(p_lcb != NULL) {
+         (*interval) = p_lcb->current_used_conn_interval;
+         (*latency) = p_lcb->current_used_conn_latency;
+         (*timeout) = p_lcb->current_used_conn_timeout;
+        return TRUE;
+    }
+    BTM_TRACE_WARNING("%s Device is not connected", __func__);
+    
+    return FALSE;
+}
+
+
 #endif /* BLE_INCLUDED */

+ 25 - 0
components/bt/bluedroid/stack/btm/btm_ble_gap.c

@@ -2042,6 +2042,31 @@ void BTM_Recovery_Pre_State(void)
     return;
 }
 
+/*******************************************************************************
+**
+** Function         BTM_GetCurrentConnParams
+**
+** Description      This function is called to read the current connection parameters
+**                  of the device
+**
+** Returns          TRUE or FALSE
+**
+*******************************************************************************/
+
+BOOLEAN BTM_GetCurrentConnParams(BD_ADDR bda, uint16_t *interval, uint16_t *latency, uint16_t *timeout)
+{
+    if( (interval == NULL) || (latency == NULL) || (timeout == NULL) ) {
+        BTM_TRACE_ERROR("%s error ", __func__);
+        return FALSE;
+    }
+
+    if(btm_get_current_conn_params(bda, interval, latency, timeout)) {
+        return TRUE;
+    }
+
+    return FALSE;
+}
+
 /*******************************************************************************
 **
 ** Function         btm_ble_build_adv_data

+ 2 - 0
components/bt/bluedroid/stack/btm/include/btm_ble_int.h

@@ -505,6 +505,8 @@ void btm_set_random_address(BD_ADDR random_bda);
 void btm_ble_set_keep_rfu_in_auth_req(BOOLEAN keep_rfu);
 #endif
 
+BOOLEAN btm_get_current_conn_params(BD_ADDR bda, UINT16 *interval, UINT16 *latency, UINT16 *timeout);
+
 /*
 #ifdef __cplusplus
 }

+ 14 - 0
components/bt/bluedroid/stack/include/stack/btm_ble_api.h

@@ -2110,6 +2110,20 @@ tBTM_STATUS BTM_UpdateBleDuplicateExceptionalList(uint8_t subcode, uint32_t type
 **
 *******************************************************************************/
 BOOLEAN BTM_Ble_Authorization(BD_ADDR bd_addr, BOOLEAN authorize);
+
+/*******************************************************************************
+**
+** Function         BTM_GetCurrentConnParams
+**
+** Description      This function is called to read the current connection parameters 
+**                  of the device 
+**
+** Returns          TRUE or FALSE
+**
+*******************************************************************************/
+
+BOOLEAN BTM_GetCurrentConnParams(BD_ADDR bda, uint16_t *interval, uint16_t *latency, uint16_t *timeout);
+
 /*
 #ifdef __cplusplus
 }