component/bt: support BLE Read Attribute value by UUID

This commit is contained in:
xiewenxiang 2020-09-21 10:35:14 +08:00
parent 7e5e0ba7de
commit 945606b658
11 changed files with 178 additions and 6 deletions

View File

@ -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)

View File

@ -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

View File

@ -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.

View File

@ -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;

View File

@ -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;
}
/*******************************************************************************
**

View File

@ -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";
}

View File

@ -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);

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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
#endif /// BLE_INCLUDE == TRUE