component/bt : change api for V2.0
1. change all gatt cb function to 3 args and function type. add gattc_if/gatts_if as second argument 2. delete gatt_if of "gatt cb param" 3. separate conn_id and gatt_if from conn_id 4. change the demo code as the gatt changed
This commit is contained in:
@@ -69,7 +69,7 @@ static void blufi_data_recv(uint8_t *data, int len)
|
||||
|
||||
}
|
||||
|
||||
static void blufi_callback(uint32_t event, void *param)
|
||||
static void blufi_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param)
|
||||
{
|
||||
/* actually, should post to blufi_task handle the procedure,
|
||||
* now, as a demo, we do simplely */
|
||||
@@ -90,11 +90,6 @@ static void blufi_callback(uint32_t event, void *param)
|
||||
|
||||
static esp_err_t blufi_startup_in_blufi_task(void *arg)
|
||||
{
|
||||
/*set connectable,discoverable, pairable and paired only modes of local device*/
|
||||
tBTA_DM_DISC disc_mode = BTA_DM_BLE_GENERAL_DISCOVERABLE;
|
||||
tBTA_DM_CONN conn_mode = BTA_DM_BLE_CONNECTABLE;
|
||||
BTA_DmSetVisibility(disc_mode, conn_mode, (uint8_t)BTA_DM_NON_PAIRABLE, (uint8_t)BTA_DM_CONN_ALL);
|
||||
|
||||
esp_blufi_register_callback(blufi_callback);
|
||||
esp_blufi_profile_init();
|
||||
|
||||
@@ -111,8 +106,6 @@ esp_err_t blufi_enable(void *arg)
|
||||
{
|
||||
esp_err_t err;
|
||||
|
||||
BTM_SetTraceLevel(BT_TRACE_LEVEL_ERROR);
|
||||
|
||||
err = esp_enable_bluetooth();
|
||||
if (err) {
|
||||
LOG_ERROR("%s failed\n", __func__);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// You may obtain A copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
@@ -30,6 +30,9 @@
|
||||
#include "esp_bt_main.h"
|
||||
#include "esp_bt_main.h"
|
||||
|
||||
///Declare the static function
|
||||
static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *parama);
|
||||
|
||||
#define GATTS_SERVICE_UUID_TEST 0x00FF
|
||||
#define GATTS_CHAR_UUID_TEST 0xFF01
|
||||
#define GATTS_DESCR_UUID_TEST 0x3333
|
||||
@@ -74,8 +77,13 @@ static esp_ble_adv_params_t test_adv_params = {
|
||||
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
|
||||
};
|
||||
|
||||
struct gatts_test_inst {
|
||||
uint16_t gatt_if;
|
||||
#define PROFILE_NUM 2
|
||||
#define PROFILE_A_APP_ID 0
|
||||
#define PROFILE_B_APP_ID 1
|
||||
|
||||
struct gatts_profile_inst {
|
||||
esp_gatts_cb_t gatts_cb;
|
||||
uint16_t gatts_if;
|
||||
uint16_t app_id;
|
||||
uint16_t conn_id;
|
||||
uint16_t service_handle;
|
||||
@@ -87,9 +95,20 @@ struct gatts_test_inst {
|
||||
uint16_t descr_handle;
|
||||
esp_bt_uuid_t descr_uuid;
|
||||
};
|
||||
static struct gatts_test_inst gl_test;
|
||||
|
||||
static void gap_event_handler(uint32_t event, void *param)
|
||||
/* One gatt-based profile one app_id and one gatts_if, this array will store the gatts_if returned by ESP_GATTS_REG_EVT */
|
||||
static struct gatts_profile_inst gl_profile_tab[PROFILE_NUM] = {
|
||||
[PROFILE_A_APP_ID] = {
|
||||
.gatts_cb = gatts_profile_a_event_handler,
|
||||
.gatts_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
|
||||
},
|
||||
[PROFILE_B_APP_ID] = {
|
||||
.gatts_cb = NULL, /* This demo does not implement, similar as profile A */
|
||||
.gatts_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
|
||||
},
|
||||
};
|
||||
|
||||
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
|
||||
{
|
||||
LOG_ERROR("GAP_EVT, event %d\n", event);
|
||||
|
||||
@@ -102,42 +121,42 @@ static void gap_event_handler(uint32_t event, void *param)
|
||||
}
|
||||
}
|
||||
|
||||
static void gatts_event_handler(uint32_t event, void *param)
|
||||
{
|
||||
esp_ble_gatts_cb_param_t *p = (esp_ble_gatts_cb_param_t *)param;
|
||||
|
||||
static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {
|
||||
switch (event) {
|
||||
case ESP_GATTS_REG_EVT:
|
||||
LOG_INFO("REGISTER_APP_EVT, status %d, gatt_if %d, app_id %d\n", p->reg.status, p->reg.gatt_if, p->reg.app_id);
|
||||
gl_test.gatt_if = p->reg.gatt_if;
|
||||
gl_test.service_id.is_primary = true;
|
||||
gl_test.service_id.id.inst_id = 0x00;
|
||||
gl_test.service_id.id.uuid.len = ESP_UUID_LEN_16;
|
||||
gl_test.service_id.id.uuid.uuid.uuid16 = GATTS_SERVICE_UUID_TEST;
|
||||
LOG_INFO("REGISTER_APP_EVT, status %d, app_id %d\n", param->reg.status, param->reg.app_id);
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.is_primary = true;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.inst_id = 0x00;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.len = ESP_UUID_LEN_16;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.uuid.uuid16 = GATTS_SERVICE_UUID_TEST;
|
||||
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
esp_ble_gap_set_device_name(TEST_DEVICE_NAME);
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
esp_ble_gap_config_adv_data(&test_adv_data);
|
||||
|
||||
esp_ble_gatts_create_service(gl_test.gatt_if, &gl_test.service_id, GATTS_NUM_HANDLE_TEST);
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
esp_ble_gatts_create_service(gatts_if, &gl_profile_tab[PROFILE_A_APP_ID].service_id, GATTS_NUM_HANDLE_TEST);
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
break;
|
||||
case ESP_GATTS_READ_EVT: {
|
||||
LOG_INFO("GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", p->read.conn_id, p->read.trans_id, p->read.handle);
|
||||
LOG_INFO("GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
|
||||
esp_gatt_rsp_t rsp;
|
||||
memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
|
||||
rsp.attr_value.handle = p->read.handle;
|
||||
rsp.attr_value.handle = param->read.handle;
|
||||
rsp.attr_value.len = 4;
|
||||
rsp.attr_value.value[0] = 0xde;
|
||||
rsp.attr_value.value[1] = 0xed;
|
||||
rsp.attr_value.value[2] = 0xbe;
|
||||
rsp.attr_value.value[3] = 0xef;
|
||||
esp_ble_gatts_send_response(p->read.conn_id, p->read.trans_id,
|
||||
esp_ble_gatts_send_response(gatts_if, param->read.conn_id, param->read.trans_id,
|
||||
ESP_GATT_OK, &rsp);
|
||||
break;
|
||||
}
|
||||
case ESP_GATTS_WRITE_EVT: {
|
||||
LOG_INFO("GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", p->write.conn_id, p->write.trans_id, p->write.handle);
|
||||
LOG_INFO("GATT_WRITE_EVT, value len %d, value %08x\n", p->write.len, *(uint32_t *)p->write.value);
|
||||
esp_ble_gatts_send_response(p->write.conn_id, p->write.trans_id, ESP_GATT_OK, NULL);
|
||||
LOG_INFO("GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle);
|
||||
LOG_INFO("GATT_WRITE_EVT, value len %d, value %08x\n", param->write.len, *(uint32_t *)param->write.value);
|
||||
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
|
||||
break;
|
||||
}
|
||||
case ESP_GATTS_EXEC_WRITE_EVT:
|
||||
@@ -146,48 +165,48 @@ static void gatts_event_handler(uint32_t event, void *param)
|
||||
case ESP_GATTS_UNREG_EVT:
|
||||
break;
|
||||
case ESP_GATTS_CREATE_EVT:
|
||||
LOG_INFO("CREATE_SERVICE_EVT, status %d, gatt_if %d, service_handle %d\n", p->create.status, p->create.gatt_if, p->create.service_handle);
|
||||
gl_test.service_handle = p->create.service_handle;
|
||||
gl_test.char_uuid.len = ESP_UUID_LEN_16;
|
||||
gl_test.char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST;
|
||||
LOG_INFO("CREATE_SERVICE_EVT, status %d, service_handle %d\n", param->create.status, param->create.service_handle);
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_handle = param->create.service_handle;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.len = ESP_UUID_LEN_16;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST;
|
||||
|
||||
esp_ble_gatts_start_service(gl_test.service_handle);
|
||||
esp_ble_gatts_start_service(gl_profile_tab[PROFILE_A_APP_ID].service_handle);
|
||||
|
||||
esp_ble_gatts_add_char(gl_test.service_handle, &gl_test.char_uuid,
|
||||
esp_ble_gatts_add_char(gl_profile_tab[PROFILE_A_APP_ID].service_handle, &gl_profile_tab[PROFILE_A_APP_ID].char_uuid,
|
||||
ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
|
||||
ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY);
|
||||
break;
|
||||
case ESP_GATTS_ADD_INCL_SRVC_EVT:
|
||||
break;
|
||||
case ESP_GATTS_ADD_CHAR_EVT:
|
||||
LOG_INFO("ADD_CHAR_EVT, status %d, gatt_if %d, attr_handle %d, service_handle %d\n",
|
||||
p->add_char.status, p->add_char.gatt_if, p->add_char.attr_handle, p->add_char.service_handle);
|
||||
LOG_INFO("ADD_CHAR_EVT, status %d, attr_handle %d, service_handle %d\n",
|
||||
param->add_char.status, param->add_char.attr_handle, param->add_char.service_handle);
|
||||
|
||||
gl_test.char_handle = p->add_char.attr_handle;
|
||||
gl_test.descr_uuid.len = ESP_UUID_LEN_16;
|
||||
gl_test.descr_uuid.uuid.uuid16 = ESP_GATT_UUID_CHAR_CLIENT_CONFIG;
|
||||
esp_ble_gatts_add_char_descr(gl_test.service_handle, &gl_test.descr_uuid,
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_handle = param->add_char.attr_handle;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].descr_uuid.len = ESP_UUID_LEN_16;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].descr_uuid.uuid.uuid16 = ESP_GATT_UUID_CHAR_CLIENT_CONFIG;
|
||||
esp_ble_gatts_add_char_descr(gl_profile_tab[PROFILE_A_APP_ID].service_handle, &gl_profile_tab[PROFILE_A_APP_ID].descr_uuid,
|
||||
ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE);
|
||||
break;
|
||||
case ESP_GATTS_ADD_CHAR_DESCR_EVT:
|
||||
LOG_INFO("ADD_DESCR_EVT, status %d, gatt_if %d, attr_handle %d, service_handle %d\n",
|
||||
p->add_char.status, p->add_char.gatt_if, p->add_char.attr_handle, p->add_char.service_handle);
|
||||
LOG_INFO("ADD_DESCR_EVT, status %d, attr_handle %d, service_handle %d\n",
|
||||
param->add_char.status, param->add_char.attr_handle, param->add_char.service_handle);
|
||||
break;
|
||||
case ESP_GATTS_DELETE_EVT:
|
||||
break;
|
||||
case ESP_GATTS_START_EVT:
|
||||
LOG_INFO("SERVICE_START_EVT, status %d, gatt_if %d, service_handle %d\n",
|
||||
p->start.status, p->start.gatt_if, p->start.service_handle);
|
||||
LOG_INFO("SERVICE_START_EVT, status %d, service_handle %d\n",
|
||||
param->start.status, param->start.service_handle);
|
||||
break;
|
||||
case ESP_GATTS_STOP_EVT:
|
||||
break;
|
||||
case ESP_GATTS_CONNECT_EVT:
|
||||
LOG_INFO("SERVICE_START_EVT, conn_id %d, gatt_if %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
|
||||
p->connect.conn_id, p->connect.gatt_if,
|
||||
p->connect.remote_bda[0], p->connect.remote_bda[1], p->connect.remote_bda[2],
|
||||
p->connect.remote_bda[3], p->connect.remote_bda[4], p->connect.remote_bda[5],
|
||||
p->connect.is_connected);
|
||||
gl_test.conn_id = p->connect.conn_id;
|
||||
LOG_INFO("SERVICE_START_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
|
||||
param->connect.conn_id,
|
||||
param->connect.remote_bda[0], param->connect.remote_bda[1], param->connect.remote_bda[2],
|
||||
param->connect.remote_bda[3], param->connect.remote_bda[4], param->connect.remote_bda[5],
|
||||
param->connect.is_connected);
|
||||
gl_profile_tab[PROFILE_A_APP_ID].conn_id = param->connect.conn_id;
|
||||
break;
|
||||
case ESP_GATTS_DISCONNECT_EVT:
|
||||
case ESP_GATTS_OPEN_EVT:
|
||||
@@ -200,6 +219,37 @@ static void gatts_event_handler(uint32_t event, void *param)
|
||||
}
|
||||
}
|
||||
|
||||
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
|
||||
{
|
||||
LOG_INFO("EVT %d, gatts if %d\n", event, gatts_if);
|
||||
|
||||
/* If event is register event, store the gatts_if for each profile */
|
||||
if (event == ESP_GATTS_REG_EVT) {
|
||||
if (param->reg.status == ESP_GATT_OK) {
|
||||
gl_profile_tab[param->reg.app_id].gatts_if = gatts_if;
|
||||
} else {
|
||||
LOG_INFO("Reg app failed, app_id %04x, status %d\n",
|
||||
param->reg.app_id,
|
||||
param->reg.status);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* If the gatts_if equal to profile A, call profile A cb handler,
|
||||
* so here call each profile's callback */
|
||||
do {
|
||||
int idx;
|
||||
for (idx = 0; idx < PROFILE_NUM; idx++) {
|
||||
if (gatts_if == ESP_GATT_IF_NONE || /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */
|
||||
gatts_if == gl_profile_tab[idx].gatts_if) {
|
||||
if (gl_profile_tab[idx].gatts_cb) {
|
||||
gl_profile_tab[idx].gatts_cb(event, gatts_if, param);
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (0);
|
||||
}
|
||||
|
||||
void app_main()
|
||||
{
|
||||
esp_err_t ret;
|
||||
@@ -219,7 +269,7 @@ void app_main()
|
||||
|
||||
esp_ble_gatts_register_callback(gatts_event_handler);
|
||||
esp_ble_gap_register_callback(gap_event_handler);
|
||||
esp_ble_gatts_app_register(GATTS_SERVICE_UUID_TEST);
|
||||
esp_ble_gatts_app_register(PROFILE_A_APP_ID);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -37,16 +37,17 @@
|
||||
#include "esp_gatt_defs.h"
|
||||
#include "esp_bt_main.h"
|
||||
|
||||
///Declare static functions
|
||||
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
|
||||
static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
|
||||
static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
|
||||
|
||||
|
||||
#define BT_BD_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x"
|
||||
#define BT_BD_ADDR_HEX(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
|
||||
|
||||
esp_gatt_if_t client_if;
|
||||
esp_gatt_status_t status = ESP_GATT_ERROR;
|
||||
bool connet = false;
|
||||
uint16_t simpleClient_id = 0xEE;
|
||||
|
||||
const char device_name[] = "Heart Rate";
|
||||
static bool connect = false;
|
||||
static const char device_name[] = "Heart Rate";
|
||||
|
||||
static esp_ble_scan_params_t ble_scan_params = {
|
||||
.scan_type = BLE_SCAN_TYPE_ACTIVE,
|
||||
@@ -57,11 +58,74 @@ static esp_ble_scan_params_t ble_scan_params = {
|
||||
};
|
||||
|
||||
|
||||
static void esp_gap_cb(uint32_t event, void *param);
|
||||
#define PROFILE_NUM 2
|
||||
#define PROFILE_A_APP_ID 0
|
||||
#define PROFILE_B_APP_ID 1
|
||||
|
||||
static void esp_gattc_cb(uint32_t event, void *param);
|
||||
struct gattc_profile_inst {
|
||||
esp_gattc_cb_t gattc_cb;
|
||||
uint16_t gattc_if;
|
||||
uint16_t app_id;
|
||||
uint16_t conn_id;
|
||||
};
|
||||
|
||||
static void esp_gap_cb(uint32_t event, void *param)
|
||||
/* One gatt-based profile one app_id and one gattc_if, this array will store the gattc_if returned by ESP_GATTS_REG_EVT */
|
||||
static struct gattc_profile_inst gl_profile_tab[PROFILE_NUM] = {
|
||||
[PROFILE_A_APP_ID] = {
|
||||
.gattc_cb = gattc_profile_a_event_handler,
|
||||
.gattc_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
|
||||
},
|
||||
[PROFILE_B_APP_ID] = {
|
||||
.gattc_cb = NULL, /* This demo does not implement, similar as profile A */
|
||||
.gattc_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
|
||||
},
|
||||
};
|
||||
|
||||
static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
|
||||
{
|
||||
uint16_t conn_id = 0;
|
||||
esp_ble_gattc_cb_param_t *p_data = (esp_ble_gattc_cb_param_t *)param;
|
||||
|
||||
switch (event) {
|
||||
case ESP_GATTC_REG_EVT:
|
||||
LOG_INFO("REG_EVT\n");
|
||||
esp_ble_gap_set_scan_params(&ble_scan_params);
|
||||
break;
|
||||
case ESP_GATTC_OPEN_EVT:
|
||||
conn_id = p_data->open.conn_id;
|
||||
LOG_INFO("ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d\n", conn_id, gattc_if, p_data->open.status);
|
||||
esp_ble_gattc_search_service(gattc_if, conn_id, NULL);
|
||||
break;
|
||||
case ESP_GATTC_SEARCH_RES_EVT: {
|
||||
esp_gatt_srvc_id_t *srvc_id = &p_data->search_res.srvc_id;
|
||||
conn_id = p_data->open.conn_id;
|
||||
LOG_INFO("SEARCH RES: conn_id = %x\n", conn_id);
|
||||
if (srvc_id->id.uuid.len == ESP_UUID_LEN_16) {
|
||||
LOG_INFO("UUID16: %x\n", srvc_id->id.uuid.uuid.uuid16);
|
||||
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_32) {
|
||||
LOG_INFO("UUID32: %x\n", srvc_id->id.uuid.uuid.uuid32);
|
||||
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_128) {
|
||||
LOG_INFO("UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x\n", 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]);
|
||||
} else {
|
||||
LOG_ERROR("UNKNOWN LEN %d\n", srvc_id->id.uuid.len);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ESP_GATTC_SEARCH_CMPL_EVT:
|
||||
conn_id = p_data->search_cmpl.conn_id;
|
||||
LOG_INFO("SEARCH_CMPL: conn_id = %x, status %d\n", conn_id, p_data->search_cmpl.status);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
|
||||
{
|
||||
uint8_t *adv_name = NULL;
|
||||
uint8_t adv_name_len = 0;
|
||||
@@ -94,12 +158,12 @@ static void esp_gap_cb(uint32_t event, void *param)
|
||||
|
||||
if (adv_name != NULL) {
|
||||
if (strcmp((char *)adv_name, device_name) == 0) {
|
||||
LOG_INFO("the name eque to Heart Rate.\n");
|
||||
if (status == ESP_GATT_OK && connet == false) {
|
||||
connet = true;
|
||||
LOG_INFO("Connet to the remote device.\n");
|
||||
LOG_INFO("the name equal to Heart Rate\n");
|
||||
if (connect == false) {
|
||||
connect = true;
|
||||
LOG_INFO("Connect to the remote device.\n");
|
||||
esp_ble_gap_stop_scanning();
|
||||
esp_ble_gattc_open(client_if, scan_result->scan_rst.bda, true);
|
||||
esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -116,55 +180,41 @@ static void esp_gap_cb(uint32_t event, void *param)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void esp_gattc_cb(uint32_t event, void *param)
|
||||
static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
|
||||
{
|
||||
uint16_t conn_id = 0;
|
||||
esp_ble_gattc_cb_param_t *p_data = (esp_ble_gattc_cb_param_t *)param;
|
||||
LOG_INFO("EVT %d, gattc if %d\n", event, gattc_if);
|
||||
|
||||
LOG_INFO("esp_gattc_cb, event = %x\n", event);
|
||||
switch (event) {
|
||||
case ESP_GATTC_REG_EVT:
|
||||
status = p_data->reg.status;
|
||||
client_if = p_data->reg.gatt_if;
|
||||
LOG_INFO("status = %x, client_if = %x\n", status, client_if);
|
||||
break;
|
||||
case ESP_GATTC_OPEN_EVT:
|
||||
conn_id = p_data->open.conn_id;
|
||||
LOG_INFO("ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d\n", conn_id, p_data->open.gatt_if, p_data->open.status);
|
||||
esp_ble_gattc_search_service(conn_id, NULL);
|
||||
break;
|
||||
case ESP_GATTC_SEARCH_RES_EVT: {
|
||||
esp_gatt_srvc_id_t *srvc_id = &p_data->search_res.srvc_id;
|
||||
conn_id = p_data->open.conn_id;
|
||||
LOG_INFO("SEARCH RES: conn_id = %x\n", conn_id);
|
||||
if (srvc_id->id.uuid.len == ESP_UUID_LEN_16) {
|
||||
LOG_INFO("UUID16: %x\n", srvc_id->id.uuid.uuid.uuid16);
|
||||
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_32) {
|
||||
LOG_INFO("UUID32: %x\n", srvc_id->id.uuid.uuid.uuid32);
|
||||
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_128) {
|
||||
LOG_INFO("UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x\n", 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]);
|
||||
/* If event is register event, store the gattc_if for each profile */
|
||||
if (event == ESP_GATTC_REG_EVT) {
|
||||
if (param->reg.status == ESP_GATT_OK) {
|
||||
gl_profile_tab[param->reg.app_id].gattc_if = gattc_if;
|
||||
} else {
|
||||
LOG_ERROR("UNKNOWN LEN %d\n", srvc_id->id.uuid.len);
|
||||
LOG_INFO("Reg app failed, app_id %04x, status %d\n",
|
||||
param->reg.app_id,
|
||||
param->reg.status);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ESP_GATTC_SEARCH_CMPL_EVT:
|
||||
conn_id = p_data->search_cmpl.conn_id;
|
||||
LOG_INFO("SEARCH_CMPL: conn_id = %x, status %d\n", conn_id, p_data->search_cmpl.status);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
/* If the gattc_if equal to profile A, call profile A cb handler,
|
||||
* so here call each profile's callback */
|
||||
do {
|
||||
int idx;
|
||||
for (idx = 0; idx < PROFILE_NUM; idx++) {
|
||||
if (gattc_if == ESP_GATT_IF_NONE || /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */
|
||||
gattc_if == gl_profile_tab[idx].gattc_if) {
|
||||
if (gl_profile_tab[idx].gattc_cb) {
|
||||
gl_profile_tab[idx].gattc_cb(event, gattc_if, param);
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (0);
|
||||
}
|
||||
|
||||
void ble_client_appRegister(void)
|
||||
{
|
||||
esp_err_t status;
|
||||
|
||||
LOG_INFO("register callback\n");
|
||||
|
||||
//register the scan callback function to the gap moudule
|
||||
@@ -178,8 +228,7 @@ void ble_client_appRegister(void)
|
||||
LOG_ERROR("gattc register error, error code = %x\n", status);
|
||||
return;
|
||||
}
|
||||
esp_ble_gattc_app_register(simpleClient_id);
|
||||
esp_ble_gap_set_scan_params(&ble_scan_params);
|
||||
esp_ble_gattc_app_register(PROFILE_A_APP_ID);
|
||||
}
|
||||
|
||||
void gattc_client_test(void)
|
||||
|
||||
Reference in New Issue
Block a user