From 945606b6582500baee51669b8afc84495ca94ea6 Mon Sep 17 00:00:00 2001 From: xiewenxiang Date: Mon, 21 Sep 2020 10:35:14 +0800 Subject: [PATCH] component/bt: support BLE Read Attribute value by UUID --- .../bt/host/bluedroid/api/esp_gattc_api.c | 33 +++++++++++++++++ .../api/include/api/esp_gap_ble_api.h | 3 +- .../bluedroid/api/include/api/esp_gattc_api.h | 23 ++++++++++++ .../host/bluedroid/bta/gatt/bta_gattc_act.c | 37 ++++++++++++++++++- .../host/bluedroid/bta/gatt/bta_gattc_api.c | 33 +++++++++++++++++ .../host/bluedroid/bta/gatt/bta_gattc_main.c | 10 ++++- .../bta/gatt/include/bta_gattc_int.h | 6 +++ .../bluedroid/bta/include/bta/bta_gatt_api.h | 16 ++++++++ .../btc/profile/std/gatt/btc_gattc.c | 10 +++++ .../btc/profile/std/include/btc_gattc.h | 9 +++++ .../bt/host/bluedroid/stack/btm/btm_sec.c | 4 +- 11 files changed, 178 insertions(+), 6 deletions(-) diff --git a/components/bt/host/bluedroid/api/esp_gattc_api.c b/components/bt/host/bluedroid/api/esp_gattc_api.c index c76803dfdf..e4ceb184f3 100644 --- a/components/bt/host/bluedroid/api/esp_gattc_api.c +++ b/components/bt/host/bluedroid/api/esp_gattc_api.c @@ -350,6 +350,39 @@ esp_err_t esp_ble_gattc_read_char (esp_gatt_if_t gattc_if, return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } +esp_err_t esp_ble_gattc_read_by_type (esp_gatt_if_t gattc_if, + uint16_t conn_id, + uint16_t start_handle, + uint16_t end_handle, + esp_bt_uuid_t *uuid, + esp_gatt_auth_req_t auth_req) +{ + btc_msg_t msg; + btc_ble_gattc_args_t arg; + + ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); + + if (uuid == NULL) { + return ESP_GATT_ILLEGAL_PARAMETER; + } + + if (L2CA_CheckIsCongest(L2CAP_ATT_CID, conn_id)) { + LOG_DEBUG("%s, the l2cap chanel is congest.", __func__); + return ESP_FAIL; + } + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_GATTC; + msg.act = BTC_GATTC_ACT_READ_BY_TYPE; + arg.read_by_type.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id); + arg.read_by_type.s_handle = start_handle; + arg.read_by_type.e_handle = end_handle; + arg.read_by_type.auth_req = auth_req; + memcpy(&(arg.read_by_type.uuid), uuid, sizeof(esp_bt_uuid_t)); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + esp_err_t esp_ble_gattc_read_multiple(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_gattc_multi_t *read_multi, esp_gatt_auth_req_t auth_req) diff --git a/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h b/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h index 633267ed1f..4bee1667b7 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h @@ -1310,13 +1310,12 @@ esp_err_t esp_gap_ble_set_channels(esp_gap_ble_channels channels); * * @param[in] bd_addr: BD address of the peer device. * @param[out] authorize: Authorized the link or not. -* +* * @return - ESP_OK : success * - other : failed * */ esp_err_t esp_gap_ble_set_authorization(esp_bd_addr_t bd_addr, bool authorize); - #ifdef __cplusplus } #endif 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 24655298b9..b8d4bdcf73 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 @@ -613,6 +613,29 @@ esp_err_t esp_ble_gattc_read_char (esp_gatt_if_t gattc_if, uint16_t handle, esp_gatt_auth_req_t auth_req); +/** + * @brief This function is called to read a service's characteristics of + * the given characteristic UUID + * + * @param[in] gattc_if: Gatt client access interface. + * @param[in] conn_id : connection ID. + * @param[in] start_handle : the attribute start handle. + * @param[in] end_handle : the attribute end handle + * @param[in] uuid : The UUID of attribute which will be read. + * @param[in] auth_req : authenticate request type + * + * @return + * - ESP_OK: success + * - other: failed + * + */ +esp_err_t esp_ble_gattc_read_by_type (esp_gatt_if_t gattc_if, + uint16_t conn_id, + uint16_t start_handle, + uint16_t end_handle, + esp_bt_uuid_t *uuid, + esp_gatt_auth_req_t auth_req); + /** * @brief This function is called to read multiple characteristic or * characteristic descriptors. 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 e6367c2efd..5cd07f6631 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c @@ -1104,6 +1104,41 @@ void bta_gattc_read(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) } /******************************************************************************* ** +** Function bta_gattc_read_by_type +** +** Description Read an attribute +** +** Returns None. +** +*******************************************************************************/ +void bta_gattc_read_by_type(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) +{ + if (!bta_gattc_enqueue(p_clcb, p_data)) { + return; + } + + tGATT_READ_PARAM read_param; + memset (&read_param, 0 ,sizeof(tGATT_READ_PARAM)); + read_param.service.auth_req = p_data->api_read.auth_req; + read_param.service.s_handle = p_data->api_read.s_handle; + read_param.service.e_handle = p_data->api_read.e_handle; + memcpy(&(read_param.service.uuid), &(p_data->api_read.uuid), sizeof(tBT_UUID)); + + tBTA_GATT_STATUS status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_TYPE, &read_param); + + /* read fail */ + if (status != BTA_GATT_OK) { + /* Dequeue the data, if it was enqueued */ + if (p_clcb->p_q_cmd == p_data) { + p_clcb->p_q_cmd = NULL; + bta_gattc_pop_command_to_send(p_clcb); + } + + bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status, NULL); + } +} +/******************************************************************************* +** ** Function bta_gattc_read_multi ** ** Description read multiple @@ -1397,7 +1432,7 @@ void bta_gattc_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) return; } if (p_clcb->p_q_cmd->hdr.event != bta_gattc_opcode_to_int_evt[op - GATTC_OPTYPE_READ]) { - if (p_clcb->p_q_cmd->hdr.event != BTA_GATTC_API_READ_MULTI_EVT) { + if ((p_clcb->p_q_cmd->hdr.event != BTA_GATTC_API_READ_MULTI_EVT)&&(p_clcb->p_q_cmd->hdr.event != BTA_GATTC_API_READ_BY_TYPE_EVT)) { mapped_op = p_clcb->p_q_cmd->hdr.event - BTA_GATTC_API_READ_EVT + GATTC_OPTYPE_READ; if ( mapped_op > GATTC_OPTYPE_INDICATION) { mapped_op = 0; diff --git a/components/bt/host/bluedroid/bta/gatt/bta_gattc_api.c b/components/bt/host/bluedroid/bta/gatt/bta_gattc_api.c index 767342d64f..875a7f3be3 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gattc_api.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gattc_api.c @@ -562,6 +562,39 @@ void BTA_GATTC_ReadMultiple(UINT16 conn_id, tBTA_GATTC_MULTI *p_read_multi, return; } +/******************************************************************************* +** +** Function BTA_GATTC_Read_by_type +** +** Description This function is called to read a attribute value by uuid +** +** Parameters conn_id - connection ID. +** s_handle - start handle. +** e_handle - end hanle +** uuid - The attribute UUID. +** +** Returns None +** +*******************************************************************************/ +void BTA_GATTC_Read_by_type(UINT16 conn_id, UINT16 s_handle,UINT16 e_handle, tBT_UUID *uuid, tBTA_GATT_AUTH_REQ auth_req) +{ + tBTA_GATTC_API_READ *p_buf; + + if ((p_buf = (tBTA_GATTC_API_READ *) osi_malloc(sizeof(tBTA_GATTC_API_READ))) != NULL) { + memset(p_buf, 0, sizeof(tBTA_GATTC_API_READ)); + + p_buf->hdr.event = BTA_GATTC_API_READ_BY_TYPE_EVT; + p_buf->hdr.layer_specific = conn_id; + p_buf->auth_req = auth_req; + p_buf->s_handle = s_handle; + p_buf->e_handle = e_handle; + memcpy(&(p_buf->uuid), uuid, sizeof(tBT_UUID)); + p_buf->cmpl_evt = BTA_GATTC_READ_CHAR_EVT; + + bta_sys_sendmsg(p_buf); + } + return; +} /******************************************************************************* ** diff --git a/components/bt/host/bluedroid/bta/gatt/bta_gattc_main.c b/components/bt/host/bluedroid/bta/gatt/bta_gattc_main.c index afcc692fb5..bf5a9a9270 100644 --- a/components/bt/host/bluedroid/bta/gatt/bta_gattc_main.c +++ b/components/bt/host/bluedroid/bta/gatt/bta_gattc_main.c @@ -65,6 +65,7 @@ enum { BTA_GATTC_DISC_CLOSE, BTA_GATTC_RESTART_DISCOVER, BTA_GATTC_CFG_MTU, + BTA_GATTC_READ_BY_TYPE, BTA_GATTC_IGNORE }; @@ -98,7 +99,8 @@ const tBTA_GATTC_ACTION bta_gattc_action[] = { bta_gattc_ignore_op_cmpl, bta_gattc_disc_close, bta_gattc_restart_discover, - bta_gattc_cfg_mtu + bta_gattc_cfg_mtu, + bta_gattc_read_by_type }; @@ -134,6 +136,7 @@ static const UINT8 bta_gattc_st_idle[][BTA_GATTC_NUM_COLS] = { /* BTA_GATTC_OP_CMPL_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST}, /* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST}, + /* BTA_GATTC_API_READ_BY_TYPE_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_IDLE_ST}, }; /* state table for wait for open state */ @@ -163,6 +166,7 @@ static const UINT8 bta_gattc_st_w4_conn[][BTA_GATTC_NUM_COLS] = { /* BTA_GATTC_OP_CMPL_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST}, /* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_OPEN_FAIL, BTA_GATTC_IDLE_ST}, + /* BTA_GATTC_API_READ_BY_TYPE_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_W4_CONN_ST}, }; /* state table for open state */ @@ -193,6 +197,7 @@ static const UINT8 bta_gattc_st_connected[][BTA_GATTC_NUM_COLS] = { /* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_CLOSE, BTA_GATTC_IDLE_ST}, + /* BTA_GATTC_API_READ_BY_TYPE_EVT */ {BTA_GATTC_READ_BY_TYPE, BTA_GATTC_CONN_ST}, }; /* state table for discover state */ @@ -222,6 +227,7 @@ static const UINT8 bta_gattc_st_discover[][BTA_GATTC_NUM_COLS] = { /* BTA_GATTC_OP_CMPL_EVT */ {BTA_GATTC_IGNORE_OP_CMPL, BTA_GATTC_DISCOVER_ST}, /* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_CLOSE, BTA_GATTC_IDLE_ST}, + /* BTA_GATTC_API_READ_BY_TYPE_EVT */ {BTA_GATTC_Q_CMD, BTA_GATTC_DISCOVER_ST}, }; /* type for state table */ @@ -479,6 +485,8 @@ static char *gattc_evt_code(tBTA_GATTC_INT_EVT evt_code) return "BTA_GATTC_API_DISABLE_EVT"; case BTA_GATTC_API_CFG_MTU_EVT: return "BTA_GATTC_API_CFG_MTU_EVT"; + case BTA_GATTC_API_READ_BY_TYPE_EVT: + return "BTA_GATTC_API_READ_BY_TYPE_EVT"; default: return "unknown GATTC event code"; } 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 f7539af9e2..3f4fa95504 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 @@ -60,6 +60,8 @@ enum { BTA_GATTC_OP_CMPL_EVT, BTA_GATTC_INT_DISCONN_EVT, + BTA_GATTC_API_READ_BY_TYPE_EVT, + BTA_GATTC_INT_START_IF_EVT, BTA_GATTC_API_REG_EVT, BTA_GATTC_API_DEREG_EVT, @@ -137,6 +139,9 @@ typedef struct { BT_HDR hdr; tBTA_GATT_AUTH_REQ auth_req; UINT16 handle; + UINT16 s_handle; + UINT16 e_handle; + tBT_UUID uuid; tBTA_GATTC_EVT cmpl_evt; } tBTA_GATTC_API_READ; @@ -451,6 +456,7 @@ extern void bta_gattc_disc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_dat extern void bta_gattc_start_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_read(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); +extern void bta_gattc_read_by_type(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_write(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); extern void bta_gattc_q_cmd(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data); 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 ff635605ea..77d51bb81a 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 @@ -940,6 +940,22 @@ extern void BTA_GATTC_GetGattDb(UINT16 conn_id, UINT16 start_handle, UINT16 end_ *******************************************************************************/ void BTA_GATTC_ReadCharacteristic(UINT16 conn_id, UINT16 handle, tBTA_GATT_AUTH_REQ auth_req); +/******************************************************************************* +** +** Function BTA_GATTC_Read_by_type +** +** Description This function is called to read a attribute value by uuid +** +** Parameters conn_id - connection ID. +** s_handle - start handle. +** e_handle - end hanle +** uuid - The attribute UUID. +** +** Returns None +** +*******************************************************************************/ +void BTA_GATTC_Read_by_type(UINT16 conn_id, UINT16 s_handle,UINT16 e_handle, tBT_UUID *uuid, tBTA_GATT_AUTH_REQ auth_req); + /******************************************************************************* ** ** Function BTA_GATTC_ReadCharDescr 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 cf8a02d584..a44fc2c876 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 @@ -613,6 +613,13 @@ static void btc_gattc_read_char_descr(btc_ble_gattc_args_t *arg) BTA_GATTC_ReadCharDescr(arg->read_descr.conn_id, arg->read_descr.handle, arg->read_descr.auth_req); } +static void btc_gattc_read_by_type(btc_ble_gattc_args_t *arg) +{ + tBT_UUID uuid; + btc_to_bta_uuid(&uuid, &(arg->read_by_type.uuid)); + BTA_GATTC_Read_by_type(arg->read_by_type.conn_id, arg->read_by_type.s_handle, arg->read_by_type.e_handle, &uuid, arg->read_by_type.auth_req); +} + static void btc_gattc_write_char(btc_ble_gattc_args_t *arg) { BTA_GATTC_WriteCharValue(arg->write_char.conn_id, @@ -724,6 +731,9 @@ void btc_gattc_call_handler(btc_msg_t *msg) case BTC_GATTC_ACT_READ_CHAR_DESCR: btc_gattc_read_char_descr(arg); break; + case BTC_GATTC_ACT_READ_BY_TYPE: + btc_gattc_read_by_type(arg); + break; case BTC_GATTC_ACT_WRITE_CHAR: btc_gattc_write_char(arg); break; diff --git a/components/bt/host/bluedroid/btc/profile/std/include/btc_gattc.h b/components/bt/host/bluedroid/btc/profile/std/include/btc_gattc.h index e54d9e9467..4ed9013bcc 100644 --- a/components/bt/host/bluedroid/btc/profile/std/include/btc_gattc.h +++ b/components/bt/host/bluedroid/btc/profile/std/include/btc_gattc.h @@ -30,6 +30,7 @@ typedef enum { BTC_GATTC_ACT_READ_CHAR, BTC_GATTC_ACT_READ_MULTIPLE_CHAR, BTC_GATTC_ACT_READ_CHAR_DESCR, + BTC_GATTC_ACT_READ_BY_TYPE, BTC_GATTC_ACT_WRITE_CHAR, BTC_GATTC_ACT_WRITE_CHAR_DESCR, BTC_GATTC_ACT_PREPARE_WRITE, @@ -113,6 +114,14 @@ typedef union { uint16_t handle; esp_gatt_auth_req_t auth_req; } read_descr; + // BTC_GATTC_ACT_READ_BY_TYPE + struct read_by_type_arg { + uint16_t conn_id; + uint16_t s_handle; + uint16_t e_handle; + esp_bt_uuid_t uuid; + esp_gatt_auth_req_t auth_req; + } read_by_type; //BTC_GATTC_ACT_WRITE_CHAR, struct write_char_arg { uint16_t conn_id; diff --git a/components/bt/host/bluedroid/stack/btm/btm_sec.c b/components/bt/host/bluedroid/stack/btm/btm_sec.c index 2865106dfc..e8ff1c9555 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_sec.c +++ b/components/bt/host/bluedroid/stack/btm/btm_sec.c @@ -6295,7 +6295,7 @@ BOOLEAN btm_sec_dev_authorization(BD_ADDR bd_addr, BOOLEAN authorized) if (p_dev_rec) { sec_flag = (UINT8)(p_dev_rec->sec_flags >> 8); if (!(sec_flag & BTM_SEC_LINK_KEY_AUTHED)) { - BTM_TRACE_ERROR("Authorized should after successful Authentication(MITM protection)\n"); + BTM_TRACE_ERROR("Authorized should after successful Authentication(MITM protection)\n"); return FALSE; } @@ -6312,4 +6312,4 @@ BOOLEAN btm_sec_dev_authorization(BD_ADDR bd_addr, BOOLEAN authorized) #endif ///SMP_INCLUDED == TRUE return FALSE; } -#endif /// BLE_INCLUDE == TRUE \ No newline at end of file +#endif /// BLE_INCLUDE == TRUE