|
@ -23,16 +23,20 @@ |
|
|
#include "esp_bt_main.h" |
|
|
#include "esp_bt_main.h" |
|
|
#include "ble_spp_server_demo.h" |
|
|
#include "ble_spp_server_demo.h" |
|
|
|
|
|
|
|
|
|
|
|
static uint16_t *hid_conn_id; |
|
|
|
|
|
static esp_gatt_if_t *hid_gatts_if; |
|
|
|
|
|
static short unsigned int *hid_handle; |
|
|
|
|
|
|
|
|
#define GATTS_TABLE_TAG "GATTS_SPP_DEMO" |
|
|
#define GATTS_TABLE_TAG "GATTS_SPP_DEMO" |
|
|
|
|
|
|
|
|
#define SPP_PROFILE_NUM 1 |
|
|
#define SPP_PROFILE_NUM 1 |
|
|
#define SPP_PROFILE_APP_IDX 0 |
|
|
#define SPP_PROFILE_APP_IDX 0 |
|
|
#define ESP_SPP_APP_ID 0x56 |
|
|
#define ESP_SPP_APP_ID 0x56 |
|
|
#define SAMPLE_DEVICE_NAME "ESP_SPP_SERVER" //The Device Name Characteristics in GAP |
|
|
|
|
|
|
|
|
#define SAMPLE_DEVICE_NAME "yimei_ble" //The Device Name Characteristics in GAP |
|
|
#define SPP_SVC_INST_ID 0 |
|
|
#define SPP_SVC_INST_ID 0 |
|
|
|
|
|
|
|
|
/// SPP Service |
|
|
/// SPP Service |
|
|
static const uint16_t spp_service_uuid = 0xABF0; |
|
|
|
|
|
|
|
|
// static const uint16_t spp_service_uuid = 0xABF0; |
|
|
/// Characteristic UUID |
|
|
/// Characteristic UUID |
|
|
#define ESP_GATT_UUID_SPP_DATA_RECEIVE 0xABF1 |
|
|
#define ESP_GATT_UUID_SPP_DATA_RECEIVE 0xABF1 |
|
|
#define ESP_GATT_UUID_SPP_DATA_NOTIFY 0xABF2 |
|
|
#define ESP_GATT_UUID_SPP_DATA_NOTIFY 0xABF2 |
|
@ -49,7 +53,7 @@ static const uint8_t spp_adv_data[23] = { |
|
|
/* Complete List of 16-bit Service Class UUIDs */ |
|
|
/* Complete List of 16-bit Service Class UUIDs */ |
|
|
0x03,0x03,0xF0,0xAB, |
|
|
0x03,0x03,0xF0,0xAB, |
|
|
/* Complete Local Name in advertising */ |
|
|
/* Complete Local Name in advertising */ |
|
|
0x0F,0x09, 'E', 'S', 'P', '_', 'S', 'P', 'P', '_', 'S', 'E', 'R','V', 'E', 'R' |
|
|
|
|
|
|
|
|
0x0A,0x09, 'y', 'i', 'm', 'e', 'i', '_', 'b', 'l', 'e' |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
static uint16_t spp_mtu_size = 23; |
|
|
static uint16_t spp_mtu_size = 23; |
|
@ -144,22 +148,22 @@ static const uint8_t char_prop_read_write_notify = ESP_GATT_CHAR_PROP_BIT_READ|E |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
|
|
|
///SPP Service - data receive characteristic, read&write without response |
|
|
///SPP Service - data receive characteristic, read&write without response |
|
|
static const uint16_t spp_data_receive_uuid = ESP_GATT_UUID_SPP_DATA_RECEIVE; |
|
|
|
|
|
|
|
|
// static const uint16_t spp_data_receive_uuid = ESP_GATT_UUID_SPP_DATA_RECEIVE; |
|
|
static const uint8_t spp_data_receive_val[20] = {0x00}; |
|
|
static const uint8_t spp_data_receive_val[20] = {0x00}; |
|
|
|
|
|
|
|
|
///SPP Service - data notify characteristic, notify&read |
|
|
///SPP Service - data notify characteristic, notify&read |
|
|
static const uint16_t spp_data_notify_uuid = ESP_GATT_UUID_SPP_DATA_NOTIFY; |
|
|
|
|
|
|
|
|
// static const uint16_t spp_data_notify_uuid = ESP_GATT_UUID_SPP_DATA_NOTIFY; |
|
|
static const uint8_t spp_data_notify_val[20] = {0x00}; |
|
|
static const uint8_t spp_data_notify_val[20] = {0x00}; |
|
|
static const uint8_t spp_data_notify_ccc[2] = {0x00, 0x00}; |
|
|
static const uint8_t spp_data_notify_ccc[2] = {0x00, 0x00}; |
|
|
|
|
|
|
|
|
///SPP Service - command characteristic, read&write without response |
|
|
///SPP Service - command characteristic, read&write without response |
|
|
static const uint16_t spp_command_uuid = ESP_GATT_UUID_SPP_COMMAND_RECEIVE; |
|
|
|
|
|
static const uint8_t spp_command_val[10] = {0x00}; |
|
|
|
|
|
|
|
|
// static const uint16_t spp_command_uuid = ESP_GATT_UUID_SPP_COMMAND_RECEIVE; |
|
|
|
|
|
// static const uint8_t spp_command_val[10] = {0x00}; |
|
|
|
|
|
|
|
|
///SPP Service - status characteristic, notify&read |
|
|
///SPP Service - status characteristic, notify&read |
|
|
static const uint16_t spp_status_uuid = ESP_GATT_UUID_SPP_COMMAND_NOTIFY; |
|
|
|
|
|
static const uint8_t spp_status_val[10] = {0x00}; |
|
|
|
|
|
static const uint8_t spp_status_ccc[2] = {0x00, 0x00}; |
|
|
|
|
|
|
|
|
// static const uint16_t spp_status_uuid = ESP_GATT_UUID_SPP_COMMAND_NOTIFY; |
|
|
|
|
|
// static const uint8_t spp_status_val[10] = {0x00}; |
|
|
|
|
|
// static const uint8_t spp_status_ccc[2] = {0x00, 0x00}; |
|
|
|
|
|
|
|
|
#ifdef SUPPORT_HEARTBEAT |
|
|
#ifdef SUPPORT_HEARTBEAT |
|
|
///SPP Server - Heart beat characteristic, notify&write&read |
|
|
///SPP Server - Heart beat characteristic, notify&write&read |
|
@ -168,13 +172,28 @@ static const uint8_t spp_heart_beat_val[2] = {0x00, 0x00}; |
|
|
static const uint8_t spp_heart_beat_ccc[2] = {0x00, 0x00}; |
|
|
static const uint8_t spp_heart_beat_ccc[2] = {0x00, 0x00}; |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
static const uint8_t spp_service_uuid128[16] = { |
|
|
|
|
|
/* LSB <--------------------------------------------------------------------------------> MSB */ |
|
|
|
|
|
0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x01, 0x00, 0x40, 0x6E, // |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static const uint8_t spp_char_uuid128_rx[16] = { |
|
|
|
|
|
/* LSB <--------------------------------------------------------------------------------> MSB */ |
|
|
|
|
|
0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x02, 0x00, 0x40, 0x6E, // |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static const uint8_t spp_char_uuid128_tx[16] = { |
|
|
|
|
|
/* LSB <--------------------------------------------------------------------------------> MSB */ |
|
|
|
|
|
0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x03, 0x00, 0x40, 0x6E, // |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
///Full HRS Database Description - Used to add attributes into the database |
|
|
///Full HRS Database Description - Used to add attributes into the database |
|
|
static const esp_gatts_attr_db_t spp_gatt_db[SPP_IDX_NB] = |
|
|
static const esp_gatts_attr_db_t spp_gatt_db[SPP_IDX_NB] = |
|
|
{ |
|
|
{ |
|
|
//SPP - Service Declaration |
|
|
//SPP - Service Declaration |
|
|
[SPP_IDX_SVC] = |
|
|
[SPP_IDX_SVC] = |
|
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ, |
|
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ, |
|
|
sizeof(spp_service_uuid), sizeof(spp_service_uuid), (uint8_t *)&spp_service_uuid}}, |
|
|
|
|
|
|
|
|
sizeof(spp_service_uuid128), sizeof(spp_service_uuid128), (uint8_t *)&spp_service_uuid128}}, |
|
|
|
|
|
|
|
|
//SPP - data receive characteristic Declaration |
|
|
//SPP - data receive characteristic Declaration |
|
|
[SPP_IDX_SPP_DATA_RECV_CHAR] = |
|
|
[SPP_IDX_SPP_DATA_RECV_CHAR] = |
|
@ -183,7 +202,7 @@ static const esp_gatts_attr_db_t spp_gatt_db[SPP_IDX_NB] = |
|
|
|
|
|
|
|
|
//SPP - data receive characteristic Value |
|
|
//SPP - data receive characteristic Value |
|
|
[SPP_IDX_SPP_DATA_RECV_VAL] = |
|
|
[SPP_IDX_SPP_DATA_RECV_VAL] = |
|
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&spp_data_receive_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE, |
|
|
|
|
|
|
|
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_128, (uint8_t *)&spp_char_uuid128_rx, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE, |
|
|
SPP_DATA_MAX_LEN,sizeof(spp_data_receive_val), (uint8_t *)spp_data_receive_val}}, |
|
|
SPP_DATA_MAX_LEN,sizeof(spp_data_receive_val), (uint8_t *)spp_data_receive_val}}, |
|
|
|
|
|
|
|
|
//SPP - data notify characteristic Declaration |
|
|
//SPP - data notify characteristic Declaration |
|
@ -193,38 +212,38 @@ static const esp_gatts_attr_db_t spp_gatt_db[SPP_IDX_NB] = |
|
|
|
|
|
|
|
|
//SPP - data notify characteristic Value |
|
|
//SPP - data notify characteristic Value |
|
|
[SPP_IDX_SPP_DATA_NTY_VAL] = |
|
|
[SPP_IDX_SPP_DATA_NTY_VAL] = |
|
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&spp_data_notify_uuid, ESP_GATT_PERM_READ, |
|
|
|
|
|
|
|
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_128, (uint8_t *)&spp_char_uuid128_tx, ESP_GATT_PERM_READ, |
|
|
SPP_DATA_MAX_LEN, sizeof(spp_data_notify_val), (uint8_t *)spp_data_notify_val}}, |
|
|
SPP_DATA_MAX_LEN, sizeof(spp_data_notify_val), (uint8_t *)spp_data_notify_val}}, |
|
|
|
|
|
|
|
|
//SPP - data notify characteristic - Client Characteristic Configuration Descriptor |
|
|
|
|
|
|
|
|
// SPP - data notify characteristic - Client Characteristic Configuration Descriptor |
|
|
[SPP_IDX_SPP_DATA_NTF_CFG] = |
|
|
[SPP_IDX_SPP_DATA_NTF_CFG] = |
|
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE, |
|
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE, |
|
|
sizeof(uint16_t),sizeof(spp_data_notify_ccc), (uint8_t *)spp_data_notify_ccc}}, |
|
|
sizeof(uint16_t),sizeof(spp_data_notify_ccc), (uint8_t *)spp_data_notify_ccc}}, |
|
|
|
|
|
|
|
|
//SPP - command characteristic Declaration |
|
|
|
|
|
[SPP_IDX_SPP_COMMAND_CHAR] = |
|
|
|
|
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, |
|
|
|
|
|
CHAR_DECLARATION_SIZE,CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write}}, |
|
|
|
|
|
|
|
|
// //SPP - command characteristic Declaration |
|
|
|
|
|
// [SPP_IDX_SPP_COMMAND_CHAR] = |
|
|
|
|
|
// {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, |
|
|
|
|
|
// CHAR_DECLARATION_SIZE,CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write}}, |
|
|
|
|
|
|
|
|
//SPP - command characteristic Value |
|
|
|
|
|
[SPP_IDX_SPP_COMMAND_VAL] = |
|
|
|
|
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&spp_command_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE, |
|
|
|
|
|
SPP_CMD_MAX_LEN,sizeof(spp_command_val), (uint8_t *)spp_command_val}}, |
|
|
|
|
|
|
|
|
// //SPP - command characteristic Value |
|
|
|
|
|
// [SPP_IDX_SPP_COMMAND_VAL] = |
|
|
|
|
|
// {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&spp_command_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE, |
|
|
|
|
|
// SPP_CMD_MAX_LEN,sizeof(spp_command_val), (uint8_t *)spp_command_val}}, |
|
|
|
|
|
|
|
|
//SPP - status characteristic Declaration |
|
|
|
|
|
[SPP_IDX_SPP_STATUS_CHAR] = |
|
|
|
|
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, |
|
|
|
|
|
CHAR_DECLARATION_SIZE,CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_notify}}, |
|
|
|
|
|
|
|
|
// //SPP - status characteristic Declaration |
|
|
|
|
|
// [SPP_IDX_SPP_STATUS_CHAR] = |
|
|
|
|
|
// {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, |
|
|
|
|
|
// CHAR_DECLARATION_SIZE,CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_notify}}, |
|
|
|
|
|
|
|
|
//SPP - status characteristic Value |
|
|
|
|
|
[SPP_IDX_SPP_STATUS_VAL] = |
|
|
|
|
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&spp_status_uuid, ESP_GATT_PERM_READ, |
|
|
|
|
|
SPP_STATUS_MAX_LEN,sizeof(spp_status_val), (uint8_t *)spp_status_val}}, |
|
|
|
|
|
|
|
|
// //SPP - status characteristic Value |
|
|
|
|
|
// [SPP_IDX_SPP_STATUS_VAL] = |
|
|
|
|
|
// {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&spp_status_uuid, ESP_GATT_PERM_READ, |
|
|
|
|
|
// SPP_STATUS_MAX_LEN,sizeof(spp_status_val), (uint8_t *)spp_status_val}}, |
|
|
|
|
|
|
|
|
//SPP - status characteristic - Client Characteristic Configuration Descriptor |
|
|
|
|
|
[SPP_IDX_SPP_STATUS_CFG] = |
|
|
|
|
|
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE, |
|
|
|
|
|
sizeof(uint16_t),sizeof(spp_status_ccc), (uint8_t *)spp_status_ccc}}, |
|
|
|
|
|
|
|
|
// //SPP - status characteristic - Client Characteristic Configuration Descriptor |
|
|
|
|
|
// [SPP_IDX_SPP_STATUS_CFG] = |
|
|
|
|
|
// {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE, |
|
|
|
|
|
// sizeof(uint16_t),sizeof(spp_status_ccc), (uint8_t *)spp_status_ccc}}, |
|
|
|
|
|
|
|
|
#ifdef SUPPORT_HEARTBEAT |
|
|
#ifdef SUPPORT_HEARTBEAT |
|
|
//SPP - Heart beat characteristic Declaration |
|
|
//SPP - Heart beat characteristic Declaration |
|
@ -577,6 +596,9 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_ |
|
|
case ESP_GATTS_STOP_EVT: |
|
|
case ESP_GATTS_STOP_EVT: |
|
|
break; |
|
|
break; |
|
|
case ESP_GATTS_CONNECT_EVT: |
|
|
case ESP_GATTS_CONNECT_EVT: |
|
|
|
|
|
*hid_conn_id = param->connect.conn_id; |
|
|
|
|
|
*hid_handle = spp_handle_table[SPP_IDX_SPP_DATA_NTY_VAL]; |
|
|
|
|
|
*hid_gatts_if = gatts_if; |
|
|
spp_conn_id = p_data->connect.conn_id; |
|
|
spp_conn_id = p_data->connect.conn_id; |
|
|
spp_gatts_if = gatts_if; |
|
|
spp_gatts_if = gatts_if; |
|
|
is_connected = true; |
|
|
is_connected = true; |
|
@ -587,6 +609,9 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_ |
|
|
#endif |
|
|
#endif |
|
|
break; |
|
|
break; |
|
|
case ESP_GATTS_DISCONNECT_EVT: |
|
|
case ESP_GATTS_DISCONNECT_EVT: |
|
|
|
|
|
*hid_gatts_if = 0; |
|
|
|
|
|
*hid_conn_id = 0; |
|
|
|
|
|
*hid_handle = 0; |
|
|
is_connected = false; |
|
|
is_connected = false; |
|
|
enable_data_ntf = false; |
|
|
enable_data_ntf = false; |
|
|
#ifdef SUPPORT_HEARTBEAT |
|
|
#ifdef SUPPORT_HEARTBEAT |
|
@ -652,11 +677,15 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_ |
|
|
} while (0); |
|
|
} while (0); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void ble_spp_server_demo_app_main(void) |
|
|
|
|
|
|
|
|
void ble_spp_server_demo_app_main(uint16_t *conn_id_ble, esp_gatt_if_t *gatts_if_ble, uint16_t *handle_ble ) |
|
|
{ |
|
|
{ |
|
|
esp_err_t ret; |
|
|
esp_err_t ret; |
|
|
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); |
|
|
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); |
|
|
|
|
|
|
|
|
|
|
|
hid_conn_id = conn_id_ble; |
|
|
|
|
|
hid_gatts_if = gatts_if_ble; |
|
|
|
|
|
hid_handle = handle_ble; |
|
|
|
|
|
|
|
|
// Initialize NVS |
|
|
// Initialize NVS |
|
|
ret = nvs_flash_init(); |
|
|
ret = nvs_flash_init(); |
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
|
|