mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
component/bt: add open and close event for gatts
1.added open event and close event for gatts. 2.used esp_log_buffer_char/hex for gattc scan result.
This commit is contained in:
parent
6873c19131
commit
f5a59f4f78
@ -208,12 +208,25 @@ typedef union {
|
||||
/**
|
||||
* @brief ESP_GATTS_OPEN_EVT
|
||||
*/
|
||||
struct gatts_open_evt_param {
|
||||
esp_gatt_status_t status; /*!< Operation status */
|
||||
} open; /*!< Gatt server callback param of ESP_GATTS_OPEN_EVT */
|
||||
|
||||
/**
|
||||
* @brief ESP_GATTS_CANCEL_OPEN_EVT
|
||||
*/
|
||||
struct gatts_cancel_open_evt_param {
|
||||
esp_gatt_status_t status; /*!< Operation status */
|
||||
} cancel_open; /*!< Gatt server callback param of ESP_GATTS_CANCEL_OPEN_EVT */
|
||||
|
||||
/**
|
||||
* @brief ESP_GATTS_CLOSE_EVT
|
||||
*/
|
||||
struct gatts_close_evt_param {
|
||||
esp_gatt_status_t status; /*!< Operation status */
|
||||
uint16_t conn_id; /*!< Connection id */
|
||||
} close; /*!< Gatt server callback param of ESP_GATTS_CLOSE_EVT */
|
||||
|
||||
/**
|
||||
* @brief ESP_GATTS_LISTEN_EVT
|
||||
*/
|
||||
|
@ -710,6 +710,7 @@ void bta_gatts_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
|
||||
tBTA_GATTS_RCB *p_rcb = NULL;
|
||||
tBTA_GATT_STATUS status = BTA_GATT_ERROR;
|
||||
UINT16 conn_id;
|
||||
tBTA_GATTS_OPEN open;
|
||||
UNUSED(p_cb);
|
||||
|
||||
if ((p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_open.server_if)) != NULL) {
|
||||
@ -728,7 +729,9 @@ void bta_gatts_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
|
||||
}
|
||||
|
||||
if (p_rcb && p_rcb->p_cback) {
|
||||
(*p_rcb->p_cback)(BTA_GATTS_OPEN_EVT, (tBTA_GATTS *)&status);
|
||||
open.status = status;
|
||||
open.server_if = p_msg->api_open.server_if;
|
||||
(*p_rcb->p_cback)(BTA_GATTS_OPEN_EVT, (tBTA_GATTS *)&open);
|
||||
}
|
||||
|
||||
}
|
||||
@ -745,6 +748,7 @@ void bta_gatts_cancel_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
|
||||
{
|
||||
tBTA_GATTS_RCB *p_rcb;
|
||||
tBTA_GATT_STATUS status = BTA_GATT_ERROR;
|
||||
tBTA_GATTS_CANCEL_OPEN cancel_open;
|
||||
UNUSED(p_cb);
|
||||
|
||||
if ((p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_cancel_open.server_if)) != NULL) {
|
||||
@ -759,7 +763,10 @@ void bta_gatts_cancel_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
|
||||
}
|
||||
|
||||
if (p_rcb && p_rcb->p_cback) {
|
||||
(*p_rcb->p_cback)(BTA_GATTS_CANCEL_OPEN_EVT, (tBTA_GATTS *)&status);
|
||||
cancel_open.status = status;
|
||||
cancel_open.server_if = p_msg->api_cancel_open.server_if;
|
||||
(*p_rcb->p_cback)(BTA_GATTS_CANCEL_OPEN_EVT, (tBTA_GATTS *)&cancel_open);
|
||||
|
||||
}
|
||||
}
|
||||
/*******************************************************************************
|
||||
@ -778,7 +785,7 @@ void bta_gatts_close (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
|
||||
tGATT_IF gatt_if;
|
||||
BD_ADDR remote_bda;
|
||||
tBTA_GATT_TRANSPORT transport;
|
||||
|
||||
tBTA_GATTS_CLOSE close;
|
||||
UNUSED(p_cb);
|
||||
|
||||
if (GATT_GetConnectionInfor(p_msg->hdr.layer_specific, &gatt_if, remote_bda, &transport)) {
|
||||
@ -795,7 +802,9 @@ void bta_gatts_close (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
|
||||
bta_sys_conn_close( BTA_ID_GATTS , BTA_ALL_APP_ID, remote_bda);
|
||||
}
|
||||
|
||||
(*p_rcb->p_cback)(BTA_GATTS_CLOSE_EVT, (tBTA_GATTS *)&status);
|
||||
close.status = status;
|
||||
close.conn_id = p_msg->hdr.layer_specific;
|
||||
(*p_rcb->p_cback)(BTA_GATTS_CLOSE_EVT, (tBTA_GATTS *)&close);
|
||||
}
|
||||
} else {
|
||||
APPL_TRACE_ERROR("Unknown connection ID: %d", p_msg->hdr.layer_specific);
|
||||
|
@ -582,6 +582,21 @@ typedef struct {
|
||||
tBTA_GATT_STATUS status; /* notification/indication status */
|
||||
} tBTA_GATTS_CONF;
|
||||
|
||||
typedef struct {
|
||||
tBTA_GATT_STATUS status;
|
||||
UINT16 conn_id; /* connection ID */
|
||||
} tBTA_GATTS_CLOSE;
|
||||
|
||||
typedef struct {
|
||||
tBTA_GATT_STATUS status;
|
||||
tBTA_GATTS_IF server_if;
|
||||
} tBTA_GATTS_OPEN;
|
||||
|
||||
typedef struct {
|
||||
tBTA_GATT_STATUS status;
|
||||
tBTA_GATTS_IF server_if;
|
||||
} tBTA_GATTS_CANCEL_OPEN;
|
||||
|
||||
/* GATTS callback data */
|
||||
typedef union {
|
||||
tBTA_GATTS_REG_OPER reg_oper;
|
||||
@ -596,6 +611,10 @@ typedef union {
|
||||
tBTA_GATTS_CONN conn; /* BTA_GATTS_CONN_EVT */
|
||||
tBTA_GATTS_CONGEST congest; /* BTA_GATTS_CONGEST_EVT callback data */
|
||||
tBTA_GATTS_CONF confirm; /* BTA_GATTS_CONF_EVT callback data */
|
||||
tBTA_GATTS_CLOSE close; /* BTA_GATTS_CLOSE_EVT callback data */
|
||||
tBTA_GATTS_OPEN open; /* BTA_GATTS_OPEN_EVT callback data */
|
||||
tBTA_GATTS_CANCEL_OPEN cancel_open; /* tBTA_GATTS_CANCEL_OPEN callback data */
|
||||
|
||||
} tBTA_GATTS;
|
||||
|
||||
/* GATTS enable callback function */
|
||||
|
@ -765,11 +765,26 @@ void btc_gatts_cb_handler(btc_msg_t *msg)
|
||||
btc_gatts_cb_to_app(ESP_GATTS_DISCONNECT_EVT, gatts_if, ¶m);
|
||||
break;
|
||||
case BTA_GATTS_OPEN_EVT:
|
||||
// do nothing
|
||||
gatts_if = p_data->open.server_if;
|
||||
param.open.status = p_data->open.status;
|
||||
|
||||
btc_gatts_cb_to_app(BTA_GATTS_OPEN_EVT, gatts_if, ¶m);
|
||||
break;
|
||||
case BTA_GATTS_CANCEL_OPEN_EVT:
|
||||
// do nothing
|
||||
gatts_if = p_data->cancel_open.server_if;
|
||||
param.cancel_open.status = p_data->cancel_open.status;
|
||||
|
||||
btc_gatts_cb_to_app(BTA_GATTS_CANCEL_OPEN_EVT, gatts_if, ¶m);
|
||||
break;
|
||||
|
||||
case BTA_GATTS_CLOSE_EVT:
|
||||
// do nothing
|
||||
gatts_if = BTC_GATT_GET_GATT_IF(p_data->close.conn_id);
|
||||
param.close.status = p_data->close.status;
|
||||
param.close.conn_id = BTC_GATT_GET_CONN_ID(p_data->close.conn_id);
|
||||
|
||||
btc_gatts_cb_to_app(BTA_GATTS_CLOSE_EVT, gatts_if, ¶m);
|
||||
break;
|
||||
|
||||
case BTA_GATTS_LISTEN_EVT:
|
||||
// do nothing
|
||||
break;
|
||||
|
@ -335,10 +335,10 @@ void esp_log_buffer_hex(const char *tag, const char *buffer, uint16_t buff_len)
|
||||
|
||||
void esp_log_buffer_char(const char *tag, const char *buffer, uint16_t buff_len)
|
||||
{
|
||||
char temp_buffer[2*BYTES_PER_LINE + 1] = {0};
|
||||
char temp_buffer[BYTES_PER_LINE + 1] = {0};
|
||||
int line_len = 0;
|
||||
for (int i = 0; i < buff_len; i++) {
|
||||
line_len += sprintf(temp_buffer+line_len, "%c ", buffer[i]);
|
||||
line_len += sprintf(temp_buffer+line_len, "%c", buffer[i]);
|
||||
if (((i + 1) % BYTES_PER_LINE == 0) || (i == buff_len - 1)) {
|
||||
ESP_LOGI(tag, "%s", temp_buffer);
|
||||
line_len = 0;
|
||||
|
@ -119,11 +119,8 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
|
||||
memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->open.remote_bda, sizeof(esp_bd_addr_t));
|
||||
ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d", conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
|
||||
|
||||
ESP_LOGI(GATTC_TAG, "REMOTE BDA %02x:%02x:%02x:%02x:%02x:%02x",
|
||||
gl_profile_tab[PROFILE_A_APP_ID].remote_bda[0], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[1],
|
||||
gl_profile_tab[PROFILE_A_APP_ID].remote_bda[2], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[3],
|
||||
gl_profile_tab[PROFILE_A_APP_ID].remote_bda[4], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[5]
|
||||
);
|
||||
ESP_LOGI(GATTC_TAG, "REMOTE BDA:");
|
||||
esp_log_buffer_hex(GATTC_TAG, (char *)gl_profile_tab[PROFILE_A_APP_ID].remote_bda, sizeof(esp_bd_addr_t));
|
||||
|
||||
esp_ble_gattc_search_service(gattc_if, conn_id, NULL);
|
||||
break;
|
||||
@ -136,12 +133,8 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
|
||||
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_32) {
|
||||
ESP_LOGI(GATTC_TAG, "UUID32: %x", srvc_id->id.uuid.uuid.uuid32);
|
||||
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_128) {
|
||||
ESP_LOGI(GATTC_TAG, "UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x", srvc_id->id.uuid.uuid.uuid128[0],
|
||||
srvc_id->id.uuid.uuid.uuid128[1], srvc_id->id.uuid.uuid.uuid128[2], srvc_id->id.uuid.uuid.uuid128[3],
|
||||
srvc_id->id.uuid.uuid.uuid128[4], srvc_id->id.uuid.uuid.uuid128[5], srvc_id->id.uuid.uuid.uuid128[6],
|
||||
srvc_id->id.uuid.uuid.uuid128[7], srvc_id->id.uuid.uuid.uuid128[8], srvc_id->id.uuid.uuid.uuid128[9],
|
||||
srvc_id->id.uuid.uuid.uuid128[10], srvc_id->id.uuid.uuid.uuid128[11], srvc_id->id.uuid.uuid.uuid128[12],
|
||||
srvc_id->id.uuid.uuid.uuid128[13], srvc_id->id.uuid.uuid.uuid128[14], srvc_id->id.uuid.uuid.uuid128[15]);
|
||||
ESP_LOGI(GATTC_TAG, "UUID128:");
|
||||
esp_log_buffer_hex(GATTC_TAG, (char *)srvc_id->id.uuid.uuid.uuid128, ESP_UUID_LEN_128);
|
||||
} else {
|
||||
ESP_LOGE(GATTC_TAG, "UNKNOWN LEN %d", srvc_id->id.uuid.len);
|
||||
}
|
||||
@ -216,12 +209,8 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
|
||||
memcpy(gl_profile_tab[PROFILE_B_APP_ID].remote_bda, p_data->open.remote_bda, sizeof(esp_bd_addr_t));
|
||||
ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d", conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
|
||||
|
||||
ESP_LOGI(GATTC_TAG, "REMOTE BDA %02x:%02x:%02x:%02x:%02x:%02x",
|
||||
gl_profile_tab[PROFILE_B_APP_ID].remote_bda[0], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[1],
|
||||
gl_profile_tab[PROFILE_B_APP_ID].remote_bda[2], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[3],
|
||||
gl_profile_tab[PROFILE_B_APP_ID].remote_bda[4], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[5]
|
||||
);
|
||||
|
||||
ESP_LOGI(GATTC_TAG, "REMOTE BDA:");
|
||||
esp_log_buffer_hex(GATTC_TAG, (char *)gl_profile_tab[PROFILE_B_APP_ID].remote_bda, sizeof(esp_bd_addr_t));
|
||||
esp_ble_gattc_search_service(gattc_if, conn_id, NULL);
|
||||
break;
|
||||
case ESP_GATTC_SEARCH_RES_EVT: {
|
||||
@ -233,12 +222,8 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
|
||||
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_32) {
|
||||
ESP_LOGI(GATTC_TAG, "UUID32: %x", srvc_id->id.uuid.uuid.uuid32);
|
||||
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_128) {
|
||||
ESP_LOGI(GATTC_TAG, "UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x", srvc_id->id.uuid.uuid.uuid128[0],
|
||||
srvc_id->id.uuid.uuid.uuid128[1], srvc_id->id.uuid.uuid.uuid128[2], srvc_id->id.uuid.uuid.uuid128[3],
|
||||
srvc_id->id.uuid.uuid.uuid128[4], srvc_id->id.uuid.uuid.uuid128[5], srvc_id->id.uuid.uuid.uuid128[6],
|
||||
srvc_id->id.uuid.uuid.uuid128[7], srvc_id->id.uuid.uuid.uuid128[8], srvc_id->id.uuid.uuid.uuid128[9],
|
||||
srvc_id->id.uuid.uuid.uuid128[10], srvc_id->id.uuid.uuid.uuid128[11], srvc_id->id.uuid.uuid.uuid128[12],
|
||||
srvc_id->id.uuid.uuid.uuid128[13], srvc_id->id.uuid.uuid.uuid128[14], srvc_id->id.uuid.uuid.uuid128[15]);
|
||||
ESP_LOGI(GATTC_TAG, "UUID128:");
|
||||
esp_log_buffer_hex(GATTC_TAG, (char *)srvc_id->id.uuid.uuid.uuid128, ESP_UUID_LEN_128);
|
||||
} else {
|
||||
ESP_LOGE(GATTC_TAG, "UNKNOWN LEN %d", srvc_id->id.uuid.len);
|
||||
}
|
||||
@ -312,17 +297,13 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par
|
||||
esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *)param;
|
||||
switch (scan_result->scan_rst.search_evt) {
|
||||
case ESP_GAP_SEARCH_INQ_RES_EVT:
|
||||
for (int i = 0; i < 6; i++) {
|
||||
ESP_LOGI(GATTC_TAG, "%x:", scan_result->scan_rst.bda[i]);
|
||||
}
|
||||
ESP_LOGI(GATTC_TAG, "Searched Adv Data Len %d, Scan Response Len %d\n", scan_result->scan_rst.adv_data_len, scan_result->scan_rst.scan_rsp_len);
|
||||
ESP_LOGI(GATTC_TAG, "\n");
|
||||
esp_log_buffer_hex(GATTC_TAG, (char *)scan_result->scan_rst.bda, 6);
|
||||
ESP_LOGI(GATTC_TAG, "Searched Adv Data Len %d, Scan Response Len %d", scan_result->scan_rst.adv_data_len, scan_result->scan_rst.scan_rsp_len);
|
||||
adv_name = esp_ble_resolve_adv_data(scan_result->scan_rst.ble_adv,
|
||||
ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len);
|
||||
ESP_LOGI(GATTC_TAG, "Searched Device Name Len %d", adv_name_len);
|
||||
for (int j = 0; j < adv_name_len; j++) {
|
||||
ESP_LOGI(GATTC_TAG, "%c", adv_name[j]);
|
||||
}
|
||||
esp_log_buffer_char(GATTC_TAG, (char *)adv_name, adv_name_len);
|
||||
ESP_LOGI(GATTC_TAG, "\n");
|
||||
if (adv_name != NULL) {
|
||||
if (strlen(device_name) == adv_name_len && strncmp((char *)adv_name, device_name, adv_name_len) == 0) {
|
||||
ESP_LOGI(GATTC_TAG, "Searched device %s\n", device_name);
|
||||
|
Loading…
x
Reference in New Issue
Block a user