From a15ce322b5e001efe7bef58d6631ada05a70c8ee Mon Sep 17 00:00:00 2001 From: xiewenxiang Date: Thu, 3 Dec 2020 21:04:21 +0800 Subject: [PATCH] component/bt: support BLE Read Attribute value by UUID --- components/bt/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 ++++++++++++ .../bt/bluedroid/bta/gatt/bta_gattc_act.c | 37 ++++++++++++++++++- .../bt/bluedroid/bta/gatt/bta_gattc_api.c | 33 +++++++++++++++++ .../bt/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 | 11 ++++++ .../btc/profile/std/include/btc_gattc.h | 9 +++++ components/bt/bluedroid/stack/btm/btm_sec.c | 2 +- 11 files changed, 178 insertions(+), 5 deletions(-) diff --git a/components/bt/bluedroid/api/esp_gattc_api.c b/components/bt/bluedroid/api/esp_gattc_api.c index e1e55ef1a11..655116ae84a 100644 --- a/components/bt/bluedroid/api/esp_gattc_api.c +++ b/components/bt/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/bluedroid/api/include/api/esp_gap_ble_api.h b/components/bt/bluedroid/api/include/api/esp_gap_ble_api.h index 108bef703eb..d13de3f0cab 100644 --- a/components/bt/bluedroid/api/include/api/esp_gap_ble_api.h +++ b/components/bt/bluedroid/api/include/api/esp_gap_ble_api.h @@ -1251,13 +1251,12 @@ esp_err_t esp_ble_gap_disconnect(esp_bd_addr_t remote_device); * * @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/bluedroid/api/include/api/esp_gattc_api.h b/components/bt/bluedroid/api/include/api/esp_gattc_api.h index b494bcfd398..c7465253ad6 100644 --- a/components/bt/bluedroid/api/include/api/esp_gattc_api.h +++ b/components/bt/bluedroid/api/include/api/esp_gattc_api.h @@ -603,6 +603,29 @@ esp_err_t esp_ble_gattc_read_char (esp_gatt_if_t gattc_if, uint16_t conn_id, 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 diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_act.c b/components/bt/bluedroid/bta/gatt/bta_gattc_act.c index 4ae5eba31eb..5d30185b53f 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_act.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_act.c @@ -1099,6 +1099,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 @@ -1392,7 +1427,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/bluedroid/bta/gatt/bta_gattc_api.c b/components/bt/bluedroid/bta/gatt/bta_gattc_api.c index bd5335a5745..d75c5a96a5a 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_api.c +++ b/components/bt/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/bluedroid/bta/gatt/bta_gattc_main.c b/components/bt/bluedroid/bta/gatt/bta_gattc_main.c index e20efe499e5..e1bdce2d39d 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_main.c +++ b/components/bt/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 }; @@ -133,6 +135,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 */ @@ -161,6 +164,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 */ @@ -190,6 +194,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 */ @@ -218,6 +223,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 */ @@ -470,6 +476,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/bluedroid/bta/gatt/include/bta_gattc_int.h b/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h index 5c2b1a85a53..c1c909ab2a5 100644 --- a/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h +++ b/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h @@ -59,6 +59,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, @@ -136,6 +138,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; @@ -449,6 +454,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/bluedroid/bta/include/bta/bta_gatt_api.h b/components/bt/bluedroid/bta/include/bta/bta_gatt_api.h index aa81b176a2d..59dc1a1bb96 100644 --- a/components/bt/bluedroid/bta/include/bta/bta_gatt_api.h +++ b/components/bt/bluedroid/bta/include/bta/bta_gatt_api.h @@ -939,6 +939,22 @@ void BTA_GATTC_ReadCharacteristic(UINT16 conn_id, UINT16 handle, tBTA_GATT_AUTH_ *******************************************************************************/ void BTA_GATTC_ReadCharDescr (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_WriteCharValue diff --git a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c index 300d802f914..5fd852ccb3a 100644 --- a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c +++ b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c @@ -613,6 +613,14 @@ 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 +732,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/bluedroid/btc/profile/std/include/btc_gattc.h b/components/bt/bluedroid/btc/profile/std/include/btc_gattc.h index afc3e4ba910..e9c484576e1 100644 --- a/components/bt/bluedroid/btc/profile/std/include/btc_gattc.h +++ b/components/bt/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, @@ -112,6 +113,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/bluedroid/stack/btm/btm_sec.c b/components/bt/bluedroid/stack/btm/btm_sec.c index 9638601a77f..fad74e1e10d 100644 --- a/components/bt/bluedroid/stack/btm/btm_sec.c +++ b/components/bt/bluedroid/stack/btm/btm_sec.c @@ -6276,7 +6276,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; }