forked from espressif/esp-idf
fix(bt/bluedroid): Update ble gattc gatts coex example
This commit is contained in:
@@ -25,7 +25,6 @@
|
|||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "sdkconfig.h"
|
#include "sdkconfig.h"
|
||||||
|
|
||||||
|
|
||||||
#define GATTS_SERVICE_UUID_TEST_A 0x00FF
|
#define GATTS_SERVICE_UUID_TEST_A 0x00FF
|
||||||
#define GATTS_CHAR_UUID_TEST_A 0xFF01
|
#define GATTS_CHAR_UUID_TEST_A 0xFF01
|
||||||
#define GATTS_NUM_HANDLE_TEST_A 4
|
#define GATTS_NUM_HANDLE_TEST_A 4
|
||||||
@@ -57,6 +56,9 @@
|
|||||||
|
|
||||||
static const char remote_device_name[] = "ESP_GATTS_DEMO";
|
static const char remote_device_name[] = "ESP_GATTS_DEMO";
|
||||||
|
|
||||||
|
// record peer gatt server address
|
||||||
|
esp_bd_addr_t peer_gatts_addr = {0};
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t *prepare_buf;
|
uint8_t *prepare_buf;
|
||||||
int prepare_len;
|
int prepare_len;
|
||||||
@@ -284,7 +286,17 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param
|
|||||||
connect = true;
|
connect = true;
|
||||||
ESP_LOGI(COEX_TAG, "connect to the remote device %s", remote_device_name);
|
ESP_LOGI(COEX_TAG, "connect to the remote device %s", remote_device_name);
|
||||||
esp_ble_gap_stop_scanning();
|
esp_ble_gap_stop_scanning();
|
||||||
esp_ble_gattc_open(gattc_profile_tab[GATTC_PROFILE_C_APP_ID].gattc_if, scan_result->scan_rst.bda, scan_result->scan_rst.ble_addr_type, true);
|
|
||||||
|
// Initiate GATT connection with the remote device,
|
||||||
|
// If ble physical connection is set up, ESP_GATTS_CONNECT_EVT and ESP_GATTC_CONNECT_EVT event will come
|
||||||
|
esp_ble_gattc_open(gattc_profile_tab[GATTC_PROFILE_C_APP_ID].gattc_if,
|
||||||
|
scan_result->scan_rst.bda,
|
||||||
|
scan_result->scan_rst.ble_addr_type,
|
||||||
|
true);
|
||||||
|
|
||||||
|
// Update peer gatt server address
|
||||||
|
memcpy(peer_gatts_addr, scan_result->scan_rst.bda, sizeof(esp_bd_addr_t));
|
||||||
|
esp_log_buffer_hex("the remote device addr", peer_gatts_addr, sizeof(esp_bd_addr_t));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -316,6 +328,8 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
|
|||||||
break;
|
break;
|
||||||
case ESP_GATTC_CONNECT_EVT: {
|
case ESP_GATTC_CONNECT_EVT: {
|
||||||
ESP_LOGI(COEX_TAG, "ESP_GATTC_CONNECT_EVT conn_id %d, if %d", p_data->connect.conn_id, gattc_if);
|
ESP_LOGI(COEX_TAG, "ESP_GATTC_CONNECT_EVT conn_id %d, if %d", p_data->connect.conn_id, gattc_if);
|
||||||
|
if (!memcmp(peer_gatts_addr, param->connect.remote_bda, sizeof(esp_bd_addr_t))) {
|
||||||
|
// Update gattc_profile_tab if peer device is a gatt server
|
||||||
gattc_profile_tab[GATTC_PROFILE_C_APP_ID].conn_id = p_data->connect.conn_id;
|
gattc_profile_tab[GATTC_PROFILE_C_APP_ID].conn_id = p_data->connect.conn_id;
|
||||||
memcpy(gattc_profile_tab[GATTC_PROFILE_C_APP_ID].remote_bda, p_data->connect.remote_bda, sizeof(esp_bd_addr_t));
|
memcpy(gattc_profile_tab[GATTC_PROFILE_C_APP_ID].remote_bda, p_data->connect.remote_bda, sizeof(esp_bd_addr_t));
|
||||||
ESP_LOGI(COEX_TAG, "REMOTE BDA:");
|
ESP_LOGI(COEX_TAG, "REMOTE BDA:");
|
||||||
@@ -324,6 +338,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
|
|||||||
if (mtu_ret) {
|
if (mtu_ret) {
|
||||||
ESP_LOGE(COEX_TAG, "config MTU error, error code = %x", mtu_ret);
|
ESP_LOGE(COEX_TAG, "config MTU error, error code = %x", mtu_ret);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ESP_GATTC_OPEN_EVT:
|
case ESP_GATTC_OPEN_EVT:
|
||||||
@@ -521,8 +536,11 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
|
|||||||
ESP_LOGI(COEX_TAG, "write char success ");
|
ESP_LOGI(COEX_TAG, "write char success ");
|
||||||
break;
|
break;
|
||||||
case ESP_GATTC_DISCONNECT_EVT: {
|
case ESP_GATTC_DISCONNECT_EVT: {
|
||||||
|
if (!memcmp(peer_gatts_addr, p_data->disconnect.remote_bda, sizeof(esp_bd_addr_t))) {
|
||||||
|
// Update connect flag and get_server flag if peer device is a gatt server
|
||||||
connect = false;
|
connect = false;
|
||||||
get_server = false;
|
get_server = false;
|
||||||
|
}
|
||||||
ESP_LOGI(COEX_TAG, "ESP_GATTC_DISCONNECT_EVT, reason = %d", p_data->disconnect.reason);
|
ESP_LOGI(COEX_TAG, "ESP_GATTC_DISCONNECT_EVT, reason = %d", p_data->disconnect.reason);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -531,7 +549,8 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param) {
|
static void example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param)
|
||||||
|
{
|
||||||
esp_gatt_status_t status = ESP_GATT_OK;
|
esp_gatt_status_t status = ESP_GATT_OK;
|
||||||
if (param->write.need_rsp) {
|
if (param->write.need_rsp) {
|
||||||
if (param->write.is_prep) {
|
if (param->write.is_prep) {
|
||||||
@@ -579,7 +598,8 @@ static void example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param) {
|
static void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param)
|
||||||
|
{
|
||||||
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
|
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
|
||||||
esp_log_buffer_hex(COEX_TAG, prepare_write_env->prepare_buf, prepare_write_env->prepare_len);
|
esp_log_buffer_hex(COEX_TAG, prepare_write_env->prepare_buf, prepare_write_env->prepare_len);
|
||||||
} else {
|
} else {
|
||||||
@@ -592,7 +612,8 @@ static void example_exec_write_event_env(prepare_type_env_t *prepare_write_env,
|
|||||||
prepare_write_env->prepare_len = 0;
|
prepare_write_env->prepare_len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
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) {
|
switch (event) {
|
||||||
case ESP_GATTS_REG_EVT:
|
case ESP_GATTS_REG_EVT:
|
||||||
ESP_LOGI(COEX_TAG, "REGISTER_APP_EVT, status %d, app_id %d", param->reg.status, param->reg.app_id);
|
ESP_LOGI(COEX_TAG, "REGISTER_APP_EVT, status %d, app_id %d", param->reg.status, param->reg.app_id);
|
||||||
@@ -736,17 +757,23 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|||||||
param->start.status, param->start.service_handle);
|
param->start.status, param->start.service_handle);
|
||||||
break;
|
break;
|
||||||
case ESP_GATTS_CONNECT_EVT: {
|
case ESP_GATTS_CONNECT_EVT: {
|
||||||
|
if (memcmp(peer_gatts_addr, param->connect.remote_bda, sizeof(esp_bd_addr_t))) {
|
||||||
|
// If a BLE physical connection is established, both ESP_GATTS_CONNECT_EVT and ESP_GATTC_CONNECT_EVT events will occur.
|
||||||
|
// Update the connection ID in the gatts_profile_tab only if the peer device is a GATT client.
|
||||||
|
gatts_profile_tab[GATTS_PROFILE_A_APP_ID].conn_id = param->connect.conn_id;
|
||||||
|
}
|
||||||
ESP_LOGI(COEX_TAG, "ESP_GATTS_CONNECT_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x",
|
ESP_LOGI(COEX_TAG, "ESP_GATTS_CONNECT_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x",
|
||||||
param->connect.conn_id,
|
param->connect.conn_id,
|
||||||
param->connect.remote_bda[0], param->connect.remote_bda[1], param->connect.remote_bda[2],
|
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.remote_bda[3], param->connect.remote_bda[4], param->connect.remote_bda[5]);
|
||||||
gatts_profile_tab[GATTS_PROFILE_A_APP_ID].conn_id = param->connect.conn_id;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ESP_GATTS_DISCONNECT_EVT:
|
case ESP_GATTS_DISCONNECT_EVT:
|
||||||
ESP_LOGI(COEX_TAG, "ESP_GATTS_DISCONNECT_EVT, disconnect reason 0x%x", param->disconnect.reason);
|
ESP_LOGI(COEX_TAG, "ESP_GATTS_DISCONNECT_EVT, disconnect reason 0x%x", param->disconnect.reason);
|
||||||
|
if (memcmp(peer_gatts_addr, param->disconnect.remote_bda, sizeof(esp_bd_addr_t))) {
|
||||||
|
// If the peer device is a GATT client, restart advertising
|
||||||
esp_ble_gap_start_advertising(&adv_params);
|
esp_ble_gap_start_advertising(&adv_params);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case ESP_GATTS_CONF_EVT:
|
case ESP_GATTS_CONF_EVT:
|
||||||
ESP_LOGI(COEX_TAG, "ESP_GATTS_CONF_EVT, status %d attr_handle %d", param->conf.status, param->conf.handle);
|
ESP_LOGI(COEX_TAG, "ESP_GATTS_CONF_EVT, status %d attr_handle %d", param->conf.status, param->conf.handle);
|
||||||
@@ -760,7 +787,8 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {
|
static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
|
||||||
|
{
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case ESP_GATTS_REG_EVT:
|
case ESP_GATTS_REG_EVT:
|
||||||
ESP_LOGI(COEX_TAG, "REGISTER_APP_EVT, status %d, app_id %d", param->reg.status, param->reg.app_id);
|
ESP_LOGI(COEX_TAG, "REGISTER_APP_EVT, status %d, app_id %d", param->reg.status, param->reg.app_id);
|
||||||
@@ -882,7 +910,11 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|||||||
param->connect.conn_id,
|
param->connect.conn_id,
|
||||||
param->connect.remote_bda[0], param->connect.remote_bda[1], param->connect.remote_bda[2],
|
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.remote_bda[3], param->connect.remote_bda[4], param->connect.remote_bda[5]);
|
||||||
|
if (memcmp(peer_gatts_addr, param->connect.remote_bda, sizeof(esp_bd_addr_t))) {
|
||||||
|
// If a BLE physical connection is established, both ESP_GATTS_CONNECT_EVT and ESP_GATTC_CONNECT_EVT events will occur.
|
||||||
|
// Update the connection ID in the gatts_profile_tab only if the peer device is a GATT client.
|
||||||
gatts_profile_tab[GATTS_PROFILE_B_APP_ID].conn_id = param->connect.conn_id;
|
gatts_profile_tab[GATTS_PROFILE_B_APP_ID].conn_id = param->connect.conn_id;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case ESP_GATTS_CONF_EVT:
|
case ESP_GATTS_CONF_EVT:
|
||||||
ESP_LOGI(COEX_TAG, "ESP_GATTS_CONF_EVT status %d attr_handle %d", param->conf.status, param->conf.handle);
|
ESP_LOGI(COEX_TAG, "ESP_GATTS_CONF_EVT status %d attr_handle %d", param->conf.status, param->conf.handle);
|
||||||
|
Reference in New Issue
Block a user