Skip to content

Commit

Permalink
component/bt: support BLE Read Attribute value by UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
xiewenxiang committed Dec 3, 2020
1 parent dfefe7b commit a15ce32
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 5 deletions.
33 changes: 33 additions & 0 deletions components/bt/bluedroid/api/esp_gattc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions components/bt/bluedroid/api/include/api/esp_gap_ble_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions components/bt/bluedroid/api/include/api/esp_gattc_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 36 additions & 1 deletion components/bt/bluedroid/bta/gatt/bta_gattc_act.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions components/bt/bluedroid/bta/gatt/bta_gattc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*******************************************************************************
**
Expand Down
10 changes: 9 additions & 1 deletion components/bt/bluedroid/bta/gatt/bta_gattc_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down Expand Up @@ -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
};


Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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";
}
Expand Down
6 changes: 6 additions & 0 deletions components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions components/bt/bluedroid/bta/include/bta/bta_gatt_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions components/bt/bluedroid/btc/profile/std/include/btc_gattc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion components/bt/bluedroid/stack/btm/btm_sec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit a15ce32

Please sign in to comment.