|
|
@@ -1,4 +1,4 @@
|
|
|
-// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
+// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
|
|
|
//
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
@@ -12,6 +12,16 @@
|
|
|
// See the License for the specific language governing permissions and
|
|
|
// limitations under the License.
|
|
|
|
|
|
+/****************************************************************************
|
|
|
+*
|
|
|
+* This file is for gatt server. It can send adv data, be connected by clent.
|
|
|
+* Run the gatt_client demo, the client demo will automatically connect to the gatt_server demo.
|
|
|
+* Client demo will enable gatt_server's notify after connection. Then two devices will exchange
|
|
|
+* data.
|
|
|
+*
|
|
|
+****************************************************************************/
|
|
|
+
|
|
|
+
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
|
@@ -22,7 +32,6 @@
|
|
|
#include "esp_log.h"
|
|
|
#include "nvs_flash.h"
|
|
|
#include "bt.h"
|
|
|
-#include "bta_api.h"
|
|
|
|
|
|
#include "esp_gap_ble_api.h"
|
|
|
#include "esp_gatts_api.h"
|
|
|
@@ -34,7 +43,7 @@
|
|
|
|
|
|
#define GATTS_TAG "GATTS_DEMO"
|
|
|
|
|
|
-///Declare the static function
|
|
|
+///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 *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);
|
|
|
|
|
|
@@ -56,13 +65,20 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|
|
#define PREPARE_BUF_MAX_SIZE 1024
|
|
|
|
|
|
uint8_t char1_str[] = {0x11,0x22,0x33};
|
|
|
-esp_attr_value_t gatts_demo_char1_val =
|
|
|
+esp_gatt_char_prop_t a_property = 0;
|
|
|
+esp_gatt_char_prop_t b_property = 0;
|
|
|
+
|
|
|
+esp_attr_value_t gatts_demo_char1_val =
|
|
|
{
|
|
|
.attr_max_len = GATTS_DEMO_CHAR_VAL_LEN_MAX,
|
|
|
.attr_len = sizeof(char1_str),
|
|
|
.attr_value = char1_str,
|
|
|
};
|
|
|
|
|
|
+static uint8_t adv_config_done = 0;
|
|
|
+#define adv_config_flag (1 << 0)
|
|
|
+#define scan_rsp_config_flag (1 << 1)
|
|
|
+
|
|
|
#ifdef CONFIG_SET_RAW_ADV_DATA
|
|
|
static uint8_t raw_adv_data[] = {
|
|
|
0x02, 0x01, 0x06,
|
|
|
@@ -73,16 +89,19 @@ static uint8_t raw_scan_rsp_data[] = {
|
|
|
0x45, 0x4d, 0x4f
|
|
|
};
|
|
|
#else
|
|
|
-static uint8_t test_service_uuid128[32] = {
|
|
|
+
|
|
|
+static uint8_t adv_service_uuid128[32] = {
|
|
|
/* LSB <--------------------------------------------------------------------------------> MSB */
|
|
|
//first uuid, 16bit, [12],[13] is the value
|
|
|
- 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xAB, 0xCD, 0x00, 0x00,
|
|
|
+ 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xEE, 0x00, 0x00, 0x00,
|
|
|
//second uuid, 32bit, [12], [13], [14], [15] is the value
|
|
|
- 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xAB, 0xCD, 0xAB, 0xCD,
|
|
|
+ 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
|
|
|
};
|
|
|
|
|
|
+// The length of adv data must be less than 31 bytes
|
|
|
//static uint8_t test_manufacturer[TEST_MANUFACTURER_DATA_LEN] = {0x12, 0x23, 0x45, 0x56};
|
|
|
-static esp_ble_adv_data_t test_adv_data = {
|
|
|
+//adv data
|
|
|
+static esp_ble_adv_data_t adv_data = {
|
|
|
.set_scan_rsp = false,
|
|
|
.include_name = true,
|
|
|
.include_txpower = true,
|
|
|
@@ -94,12 +113,29 @@ static esp_ble_adv_data_t test_adv_data = {
|
|
|
.service_data_len = 0,
|
|
|
.p_service_data = NULL,
|
|
|
.service_uuid_len = 32,
|
|
|
- .p_service_uuid = test_service_uuid128,
|
|
|
+ .p_service_uuid = adv_service_uuid128,
|
|
|
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
|
|
|
};
|
|
|
+// scan response data
|
|
|
+static esp_ble_adv_data_t scan_rsp_data = {
|
|
|
+ .set_scan_rsp = true,
|
|
|
+ .include_name = true,
|
|
|
+ .include_txpower = true,
|
|
|
+ .min_interval = 0x20,
|
|
|
+ .max_interval = 0x40,
|
|
|
+ .appearance = 0x00,
|
|
|
+ .manufacturer_len = 0, //TEST_MANUFACTURER_DATA_LEN,
|
|
|
+ .p_manufacturer_data = NULL, //&test_manufacturer[0],
|
|
|
+ .service_data_len = 0,
|
|
|
+ .p_service_data = NULL,
|
|
|
+ .service_uuid_len = 32,
|
|
|
+ .p_service_uuid = adv_service_uuid128,
|
|
|
+ .flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
|
|
|
+};
|
|
|
+
|
|
|
#endif /* CONFIG_SET_RAW_ADV_DATA */
|
|
|
|
|
|
-static esp_ble_adv_params_t test_adv_params = {
|
|
|
+static esp_ble_adv_params_t adv_params = {
|
|
|
.adv_int_min = 0x20,
|
|
|
.adv_int_max = 0x40,
|
|
|
.adv_type = ADV_TYPE_IND,
|
|
|
@@ -155,15 +191,33 @@ void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble
|
|
|
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
|
|
|
{
|
|
|
switch (event) {
|
|
|
- case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
|
|
|
- esp_ble_gap_start_advertising(&test_adv_params);
|
|
|
- break;
|
|
|
+#ifdef CONFIG_SET_RAW_ADV_DATA
|
|
|
case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:
|
|
|
- esp_ble_gap_start_advertising(&test_adv_params);
|
|
|
+ adv_config_done &= (~adv_config_flag);
|
|
|
+ if (adv_config_done==0){
|
|
|
+ esp_ble_gap_start_advertising(&adv_params);
|
|
|
+ }
|
|
|
break;
|
|
|
case ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT:
|
|
|
- esp_ble_gap_start_advertising(&test_adv_params);
|
|
|
+ adv_config_done &= (~scan_rsp_config_flag);
|
|
|
+ if (adv_config_done==0){
|
|
|
+ esp_ble_gap_start_advertising(&adv_params);
|
|
|
+ }
|
|
|
break;
|
|
|
+#else
|
|
|
+ case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
|
|
|
+ adv_config_done &= (~adv_config_flag);
|
|
|
+ if (adv_config_done == 0){
|
|
|
+ esp_ble_gap_start_advertising(&adv_params);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT:
|
|
|
+ adv_config_done &= (~scan_rsp_config_flag);
|
|
|
+ if (adv_config_done == 0){
|
|
|
+ esp_ble_gap_start_advertising(&adv_params);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+#endif
|
|
|
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
|
|
|
//advertising start complete event to indicate advertising start successfully or failed
|
|
|
if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) {
|
|
|
@@ -200,7 +254,7 @@ void example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare
|
|
|
prepare_write_env->prepare_buf = (uint8_t *)malloc(PREPARE_BUF_MAX_SIZE*sizeof(uint8_t));
|
|
|
prepare_write_env->prepare_len = 0;
|
|
|
if (prepare_write_env->prepare_buf == NULL) {
|
|
|
- LOG_ERROR("Gatt_server prep no mem\n");
|
|
|
+ ESP_LOGE(GATTS_TAG, "Gatt_server prep no mem\n");
|
|
|
status = ESP_GATT_NO_RESOURCES;
|
|
|
}
|
|
|
} else {
|
|
|
@@ -219,7 +273,7 @@ void example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare
|
|
|
memcpy(gatt_rsp->attr_value.value, param->write.value, param->write.len);
|
|
|
esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, status, gatt_rsp);
|
|
|
if (response_err != ESP_OK){
|
|
|
- LOG_ERROR("Send response error\n");
|
|
|
+ ESP_LOGE(GATTS_TAG, "Send response error\n");
|
|
|
}
|
|
|
free(gatt_rsp);
|
|
|
if (status != ESP_GATT_OK){
|
|
|
@@ -260,10 +314,30 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|
|
|
|
|
esp_ble_gap_set_device_name(TEST_DEVICE_NAME);
|
|
|
#ifdef CONFIG_SET_RAW_ADV_DATA
|
|
|
- esp_ble_gap_config_adv_data_raw(raw_adv_data, sizeof(raw_adv_data));
|
|
|
- esp_ble_gap_config_scan_rsp_data_raw(raw_scan_rsp_data, sizeof(raw_scan_rsp_data));
|
|
|
+ esp_err_t raw_adv_ret = esp_ble_gap_config_adv_data_raw(raw_adv_data, sizeof(raw_adv_data));
|
|
|
+ if (raw_adv_ret){
|
|
|
+ ESP_LOGE(GATTS_TAG, "config raw adv data failed, error code = %x ", raw_adv_ret);
|
|
|
+ }
|
|
|
+ adv_config_done |= adv_config_flag;
|
|
|
+ esp_err_t raw_scan_ret = esp_ble_gap_config_scan_rsp_data_raw(raw_scan_rsp_data, sizeof(raw_scan_rsp_data));
|
|
|
+ if (raw_scan_ret){
|
|
|
+ ESP_LOGE(GATTS_TAG, "config raw scan rsp data failed, error code = %x", raw_scan_ret);
|
|
|
+ }
|
|
|
+ adv_config_done |= scan_rsp_config_flag;
|
|
|
#else
|
|
|
- esp_ble_gap_config_adv_data(&test_adv_data);
|
|
|
+ //config adv data
|
|
|
+ esp_err_t ret = esp_ble_gap_config_adv_data(&adv_data);
|
|
|
+ if (ret){
|
|
|
+ ESP_LOGE(GATTS_TAG, "config adv data failed, error code = %x", ret);
|
|
|
+ }
|
|
|
+ adv_config_done |= adv_config_flag;
|
|
|
+ //config scan response data
|
|
|
+ ret = esp_ble_gap_config_adv_data(&scan_rsp_data);
|
|
|
+ if (ret){
|
|
|
+ ESP_LOGE(GATTS_TAG, "config scan response data failed, error code = %x", ret);
|
|
|
+ }
|
|
|
+ adv_config_done |= scan_rsp_config_flag;
|
|
|
+
|
|
|
#endif
|
|
|
esp_ble_gatts_create_service(gatts_if, &gl_profile_tab[PROFILE_A_APP_ID].service_id, GATTS_NUM_HANDLE_TEST_A);
|
|
|
break;
|
|
|
@@ -282,8 +356,46 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|
|
break;
|
|
|
}
|
|
|
case ESP_GATTS_WRITE_EVT: {
|
|
|
- ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle);
|
|
|
- ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value %08x\n", param->write.len, *(uint32_t *)param->write.value);
|
|
|
+ ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d", param->write.conn_id, param->write.trans_id, param->write.handle);
|
|
|
+ if (!param->write.is_prep){
|
|
|
+ ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value :", param->write.len);
|
|
|
+ esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len);
|
|
|
+ if (gl_profile_tab[PROFILE_A_APP_ID].descr_handle == param->write.handle && param->write.len == 2){
|
|
|
+ uint16_t descr_value = param->write.value[1]<<8 | param->write.value[0];
|
|
|
+ if (descr_value == 0x0001){
|
|
|
+ if (a_property & ESP_GATT_CHAR_PROP_BIT_NOTIFY){
|
|
|
+ ESP_LOGI(GATTS_TAG, "notify enable");
|
|
|
+ uint8_t notify_data[15];
|
|
|
+ for (int i = 0; i < sizeof(notify_data); ++i)
|
|
|
+ {
|
|
|
+ notify_data[i] = i%0xff;
|
|
|
+ }
|
|
|
+ //the size of notify_data[] need less than MTU size
|
|
|
+ esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, gl_profile_tab[PROFILE_A_APP_ID].char_handle,
|
|
|
+ sizeof(notify_data), notify_data, false);
|
|
|
+ }
|
|
|
+ }else if (descr_value == 0x0002){
|
|
|
+ if (a_property & ESP_GATT_CHAR_PROP_BIT_INDICATE){
|
|
|
+ ESP_LOGI(GATTS_TAG, "indicate enable");
|
|
|
+ uint8_t indicate_data[15];
|
|
|
+ for (int i = 0; i < sizeof(indicate_data); ++i)
|
|
|
+ {
|
|
|
+ indicate_data[i] = i%0xff;
|
|
|
+ }
|
|
|
+ //the size of indicate_data[] need less than MTU size
|
|
|
+ esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, gl_profile_tab[PROFILE_A_APP_ID].char_handle,
|
|
|
+ sizeof(indicate_data), indicate_data, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (descr_value == 0x0000){
|
|
|
+ ESP_LOGI(GATTS_TAG, "notify/indicate disable ");
|
|
|
+ }else{
|
|
|
+ ESP_LOGE(GATTS_TAG, "unknown descr value");
|
|
|
+ esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
example_write_event_env(gatts_if, &a_prepare_write_env, param);
|
|
|
break;
|
|
|
}
|
|
|
@@ -293,6 +405,8 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|
|
example_exec_write_event_env(&a_prepare_write_env, param);
|
|
|
break;
|
|
|
case ESP_GATTS_MTU_EVT:
|
|
|
+ ESP_LOGI(GATTS_TAG, "ESP_GATTS_MTU_EVT, MTU %d", param->mtu.mtu);
|
|
|
+ break;
|
|
|
case ESP_GATTS_CONF_EVT:
|
|
|
case ESP_GATTS_UNREG_EVT:
|
|
|
break;
|
|
|
@@ -303,11 +417,14 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|
|
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST_A;
|
|
|
|
|
|
esp_ble_gatts_start_service(gl_profile_tab[PROFILE_A_APP_ID].service_handle);
|
|
|
-
|
|
|
- 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,
|
|
|
- &gatts_demo_char1_val, NULL);
|
|
|
+ a_property = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
|
|
|
+ esp_err_t add_char_ret = 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,
|
|
|
+ a_property,
|
|
|
+ &gatts_demo_char1_val, NULL);
|
|
|
+ if (add_char_ret){
|
|
|
+ ESP_LOGE(GATTS_TAG, "add char failed, error code =%x",add_char_ret);
|
|
|
+ }
|
|
|
break;
|
|
|
case ESP_GATTS_ADD_INCL_SRVC_EVT:
|
|
|
break;
|
|
|
@@ -320,17 +437,24 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|
|
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_get_attr_value(param->add_char.attr_handle, &length, &prf_char);
|
|
|
+ esp_err_t get_attr_ret = esp_ble_gatts_get_attr_value(param->add_char.attr_handle, &length, &prf_char);
|
|
|
+ if (get_attr_ret == ESP_FAIL){
|
|
|
+ ESP_LOGE(GATTS_TAG, "ILLEGAL HANDLE");
|
|
|
+ }
|
|
|
|
|
|
ESP_LOGI(GATTS_TAG, "the gatts demo char length = %x\n", length);
|
|
|
for(int i = 0; i < length; i++){
|
|
|
ESP_LOGI(GATTS_TAG, "prf_char[%x] =%x\n",i,prf_char[i]);
|
|
|
}
|
|
|
- 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, NULL, NULL);
|
|
|
+ esp_err_t add_descr_ret = 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, NULL, NULL);
|
|
|
+ if (add_descr_ret){
|
|
|
+ ESP_LOGE(GATTS_TAG, "add char descr failed, error code =%x", add_descr_ret);
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
case ESP_GATTS_ADD_CHAR_DESCR_EVT:
|
|
|
+ gl_profile_tab[PROFILE_A_APP_ID].descr_handle = param->add_char.attr_handle;
|
|
|
ESP_LOGI(GATTS_TAG, "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;
|
|
|
@@ -350,7 +474,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|
|
conn_params.max_int = 0x50; // max_int = 0x50*1.25ms = 100ms
|
|
|
conn_params.min_int = 0x30; // min_int = 0x30*1.25ms = 60ms
|
|
|
conn_params.timeout = 400; // timeout = 400*10ms = 4000ms
|
|
|
- ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONNECT_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
|
|
|
+ ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONNECT_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d",
|
|
|
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],
|
|
|
@@ -361,7 +485,8 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|
|
break;
|
|
|
}
|
|
|
case ESP_GATTS_DISCONNECT_EVT:
|
|
|
- esp_ble_gap_start_advertising(&test_adv_params);
|
|
|
+ ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT");
|
|
|
+ esp_ble_gap_start_advertising(&adv_params);
|
|
|
break;
|
|
|
case ESP_GATTS_OPEN_EVT:
|
|
|
case ESP_GATTS_CANCEL_OPEN_EVT:
|
|
|
@@ -400,7 +525,44 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|
|
}
|
|
|
case ESP_GATTS_WRITE_EVT: {
|
|
|
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle);
|
|
|
- ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value %08x\n", param->write.len, *(uint32_t *)param->write.value);
|
|
|
+ if (!param->write.is_prep){
|
|
|
+ ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value :", param->write.len);
|
|
|
+ esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len);
|
|
|
+ if (gl_profile_tab[PROFILE_B_APP_ID].descr_handle == param->write.handle && param->write.len == 2){
|
|
|
+ uint16_t descr_value= param->write.value[1]<<8 | param->write.value[0];
|
|
|
+ if (descr_value == 0x0001){
|
|
|
+ if (b_property & ESP_GATT_CHAR_PROP_BIT_NOTIFY){
|
|
|
+ ESP_LOGI(GATTS_TAG, "notify enable");
|
|
|
+ uint8_t notify_data[15];
|
|
|
+ for (int i = 0; i < sizeof(notify_data); ++i)
|
|
|
+ {
|
|
|
+ notify_data[i] = i%0xff;
|
|
|
+ }
|
|
|
+ //the size of notify_data[] need less than MTU size
|
|
|
+ esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, gl_profile_tab[PROFILE_B_APP_ID].char_handle,
|
|
|
+ sizeof(notify_data), notify_data, false);
|
|
|
+ }
|
|
|
+ }else if (descr_value == 0x0002){
|
|
|
+ if (b_property & ESP_GATT_CHAR_PROP_BIT_INDICATE){
|
|
|
+ ESP_LOGI(GATTS_TAG, "indicate enable");
|
|
|
+ uint8_t indicate_data[15];
|
|
|
+ for (int i = 0; i < sizeof(indicate_data); ++i)
|
|
|
+ {
|
|
|
+ indicate_data[i] = i%0xff;
|
|
|
+ }
|
|
|
+ //the size of indicate_data[] need less than MTU size
|
|
|
+ esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, gl_profile_tab[PROFILE_B_APP_ID].char_handle,
|
|
|
+ sizeof(indicate_data), indicate_data, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (descr_value == 0x0000){
|
|
|
+ ESP_LOGI(GATTS_TAG, "notify/indicate disable ");
|
|
|
+ }else{
|
|
|
+ ESP_LOGE(GATTS_TAG, "unknown value");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
example_write_event_env(gatts_if, &b_prepare_write_env, param);
|
|
|
break;
|
|
|
}
|
|
|
@@ -410,6 +572,8 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|
|
example_exec_write_event_env(&b_prepare_write_env, param);
|
|
|
break;
|
|
|
case ESP_GATTS_MTU_EVT:
|
|
|
+ ESP_LOGI(GATTS_TAG, "ESP_GATTS_MTU_EVT, MTU %d", param->mtu.mtu);
|
|
|
+ break;
|
|
|
case ESP_GATTS_CONF_EVT:
|
|
|
case ESP_GATTS_UNREG_EVT:
|
|
|
break;
|
|
|
@@ -420,11 +584,14 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|
|
gl_profile_tab[PROFILE_B_APP_ID].char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST_B;
|
|
|
|
|
|
esp_ble_gatts_start_service(gl_profile_tab[PROFILE_B_APP_ID].service_handle);
|
|
|
-
|
|
|
- esp_ble_gatts_add_char(gl_profile_tab[PROFILE_B_APP_ID].service_handle, &gl_profile_tab[PROFILE_B_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,
|
|
|
- NULL, NULL);
|
|
|
+ b_property = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
|
|
|
+ esp_err_t add_char_ret =esp_ble_gatts_add_char( gl_profile_tab[PROFILE_B_APP_ID].service_handle, &gl_profile_tab[PROFILE_B_APP_ID].char_uuid,
|
|
|
+ ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
|
|
|
+ b_property,
|
|
|
+ NULL, NULL);
|
|
|
+ if (add_char_ret){
|
|
|
+ ESP_LOGE(GATTS_TAG, "add char failed, error code =%x",add_char_ret);
|
|
|
+ }
|
|
|
break;
|
|
|
case ESP_GATTS_ADD_INCL_SRVC_EVT:
|
|
|
break;
|
|
|
@@ -440,6 +607,7 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|
|
NULL, NULL);
|
|
|
break;
|
|
|
case ESP_GATTS_ADD_CHAR_DESCR_EVT:
|
|
|
+ gl_profile_tab[PROFILE_B_APP_ID].descr_handle = param->add_char.attr_handle;
|
|
|
ESP_LOGI(GATTS_TAG, "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;
|
|
|
@@ -452,7 +620,7 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
|
|
case ESP_GATTS_STOP_EVT:
|
|
|
break;
|
|
|
case ESP_GATTS_CONNECT_EVT:
|
|
|
- ESP_LOGI(GATTS_TAG, "CONNECT_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
|
|
|
+ ESP_LOGI(GATTS_TAG, "CONNECT_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d",
|
|
|
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],
|
|
|
@@ -478,7 +646,7 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
|
|
|
gl_profile_tab[param->reg.app_id].gatts_if = gatts_if;
|
|
|
} else {
|
|
|
ESP_LOGI(GATTS_TAG, "Reg app failed, app_id %04x, status %d\n",
|
|
|
- param->reg.app_id,
|
|
|
+ param->reg.app_id,
|
|
|
param->reg.status);
|
|
|
return;
|
|
|
}
|
|
|
@@ -534,10 +702,26 @@ void app_main()
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- esp_ble_gatts_register_callback(gatts_event_handler);
|
|
|
- esp_ble_gap_register_callback(gap_event_handler);
|
|
|
- esp_ble_gatts_app_register(PROFILE_A_APP_ID);
|
|
|
- esp_ble_gatts_app_register(PROFILE_B_APP_ID);
|
|
|
+ ret = esp_ble_gatts_register_callback(gatts_event_handler);
|
|
|
+ if (ret){
|
|
|
+ ESP_LOGE(GATTS_TAG, "gatts register error, error code = %x", ret);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ret = esp_ble_gap_register_callback(gap_event_handler);
|
|
|
+ if (ret){
|
|
|
+ ESP_LOGE(GATTS_TAG, "gap register error, error code = %x", ret);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ret = esp_ble_gatts_app_register(PROFILE_A_APP_ID);
|
|
|
+ if (ret){
|
|
|
+ ESP_LOGE(GATTS_TAG, "gatts app register error, error code = %x", ret);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ret = esp_ble_gatts_app_register(PROFILE_B_APP_ID);
|
|
|
+ if (ret){
|
|
|
+ ESP_LOGE(GATTS_TAG, "gatts app register error, error code = %x", ret);
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
return;
|
|
|
}
|