You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1173 lines
44 KiB
1173 lines
44 KiB
/*
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
*/
|
|
|
|
/****************************************************************************
|
|
*
|
|
* This demo showcases BLE GATT server. It can send adv data, be connected by client.
|
|
* 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. The two devices will then exchange
|
|
* data.
|
|
*
|
|
****************************************************************************/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "freertos/event_groups.h"
|
|
#include "esp_system.h"
|
|
#include "esp_log.h"
|
|
#include "nvs_flash.h"
|
|
#include "esp_bt.h"
|
|
#include "driver/timer.h"
|
|
#include "cJSON.h"
|
|
#include "cJSON_Utils.h"
|
|
|
|
#include "esp_gap_ble_api.h"
|
|
#include "esp_gatts_api.h"
|
|
#include "esp_bt_defs.h"
|
|
#include "esp_bt_main.h"
|
|
#include "esp_gatt_common_api.h"
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "motor_drive.h"
|
|
|
|
#define GATTS_TAG "GATTS_DEMO"
|
|
|
|
/// 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);
|
|
|
|
#define GATTS_SERVICE_UUID_TEST_A 0x00FF
|
|
#define GATTS_CHAR_UUID_TEST_A 0xFF01
|
|
#define GATTS_DESCR_UUID_TEST_A 0x3333
|
|
#define GATTS_NUM_HANDLE_TEST_A 4
|
|
|
|
#define GATTS_SERVICE_UUID_TEST_B 0x00EE
|
|
#define GATTS_CHAR_UUID_TEST_B 0xEE01
|
|
#define GATTS_DESCR_UUID_TEST_B 0x2222
|
|
#define GATTS_NUM_HANDLE_TEST_B 4
|
|
|
|
#define TEST_DEVICE_NAME "ESP_GATTS_DEMO"
|
|
#define TEST_MANUFACTURER_DATA_LEN 17
|
|
|
|
#define GATTS_DEMO_CHAR_VAL_LEN_MAX 0x40
|
|
|
|
#define PREPARE_BUF_MAX_SIZE 1024
|
|
|
|
static uint8_t char1_str[] = {0x11, 0x22, 0x33};
|
|
static esp_gatt_char_prop_t a_property = 0;
|
|
static esp_gatt_char_prop_t b_property = 0;
|
|
|
|
static 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,
|
|
0x02, 0x0a, 0xeb, 0x03, 0x03, 0xab, 0xcd};
|
|
static uint8_t raw_scan_rsp_data[] = {
|
|
0x0f, 0x09, 0x45, 0x53, 0x50, 0x5f, 0x47, 0x41, 0x54, 0x54, 0x53, 0x5f, 0x44,
|
|
0x45, 0x4d, 0x4f};
|
|
#else
|
|
|
|
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, 0xEE, 0x00, 0x00, 0x33, //
|
|
// second uuid, 32bit, [12], [13], [14], [15] is the value
|
|
0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, //
|
|
|
|
};
|
|
|
|
static uint8_t adv_service_uuid128_test[16] = {
|
|
/* LSB <--------------------------------------------------------------------------------> MSB */
|
|
0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x01, 0x00, 0x40, 0x6E, //
|
|
};
|
|
|
|
// static uint16_t set_service_uuid128[32] = {
|
|
// 0X11,0X22,0X33,0X44,0X55,0X66,0X77,0X88,0X99,0XAA,0XBB,0XCC,0XDD,0XEE,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};
|
|
// adv data
|
|
static esp_ble_adv_data_t adv_data = {
|
|
.set_scan_rsp = false,
|
|
.include_name = true,
|
|
.include_txpower = false,
|
|
.min_interval = 0x0006, // slave connection min interval, Time = min_interval * 1.25 msec
|
|
.max_interval = 0x0010, // slave connection max interval, Time = max_interval * 1.25 msec
|
|
.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 = sizeof(adv_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 = 0x0006,
|
|
//.max_interval = 0x0010,
|
|
.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 = sizeof(adv_service_uuid128),
|
|
.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 adv_params = {
|
|
.adv_int_min = 0x20,
|
|
.adv_int_max = 0x40,
|
|
.adv_type = ADV_TYPE_IND,
|
|
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
|
|
//.peer_addr =
|
|
//.peer_addr_type =
|
|
.channel_map = ADV_CHNL_ALL,
|
|
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
|
|
};
|
|
|
|
#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;
|
|
esp_gatt_srvc_id_t service_id;
|
|
uint16_t char_handle;
|
|
esp_bt_uuid_t char_uuid;
|
|
esp_gatt_perm_t perm;
|
|
esp_gatt_char_prop_t property;
|
|
uint16_t descr_handle;
|
|
esp_bt_uuid_t descr_uuid;
|
|
};
|
|
|
|
/* 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 = gatts_profile_b_event_handler, /* 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 */
|
|
},
|
|
};
|
|
|
|
typedef struct
|
|
{
|
|
uint8_t *prepare_buf;
|
|
int prepare_len;
|
|
} prepare_type_env_t;
|
|
|
|
static prepare_type_env_t a_prepare_write_env;
|
|
static prepare_type_env_t b_prepare_write_env;
|
|
|
|
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);
|
|
void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param);
|
|
|
|
/***********************************************************************************************************************
|
|
* ****************************************************user_define**************************************************** *
|
|
***********************************************************************************************************************/
|
|
typedef struct bluetooth_processer
|
|
{
|
|
char *bluetooth_processer_rx_buf;
|
|
uint8_t bluetooth_processer_rx_buf_size; //
|
|
|
|
int bluetooth_baundrate_one_packet_delay_ms;
|
|
void (*port_delay_ms)(uint64_t us);
|
|
|
|
char *order; //指令名称
|
|
int index; //
|
|
int speed_level; //
|
|
double position; //角度
|
|
int direction; //旋转方向
|
|
int code; //错误码
|
|
char *info; //错误码信息
|
|
char *deviceState; //设备状态
|
|
int deviceException; //设备异常编号
|
|
char *deviceExceptionInfo; //设备异常信息
|
|
|
|
bool cmd_flag;
|
|
bool actively_report_flag;
|
|
|
|
} bluetooth_processer_t;
|
|
|
|
#define profile_b_buffer_size 128
|
|
char bluetooth_rx_buffer[profile_b_buffer_size] = {0};
|
|
uint8_t bluetooth_rx_buffer_len = 0;
|
|
bool bluetooth_rx_buffer_start_receving = false;
|
|
bool bluetooth_rx_buffer_processing = false;
|
|
|
|
bool flag = true;
|
|
uint32_t total = 0;
|
|
#define timer_group0_interval_num 10000
|
|
// #define timer_group1_interval_num 5
|
|
#define timer_interval_s (uint32_t)1000000
|
|
#define timer_interval_ms (uint32_t)1000
|
|
#define timer_interval_us (uint32_t)1
|
|
#define kbluetooth_baundrate_one_packet_delay_ms 200
|
|
|
|
#define cmd_length_set_position 5
|
|
#define cmd_length_get_status 2
|
|
// #define cmd_length_device_status_report 6
|
|
|
|
#define set_position "setPosition"
|
|
#define get_status "getStatus"
|
|
|
|
esp_gatts_cb_event_t constructor_event;
|
|
esp_gatt_if_t constructor_gatts_if;
|
|
esp_ble_gatts_cb_param_t *constructor_param;
|
|
|
|
/***********************************************************************************************************************
|
|
* **********************************************user_function_statement********************************************** *
|
|
***********************************************************************************************************************/
|
|
void buffer_all_init();
|
|
void bluetooth_gatts_try_process_data();
|
|
void timer_set_pause_and_counter_zero(int group, int timer);
|
|
void port_timer_delay_ms(uint64_t delay);
|
|
void start_receive_data_to_buffer(uint16_t length, uint8_t *value);
|
|
bool parse_rxbuffer_and_validation_data(cJSON **json_tmp);
|
|
bool parse_json_to_struct(cJSON *ch);
|
|
void receipt_json_set_position();
|
|
void receipt_json_get_status();
|
|
void gatts_profile_a_constructor(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
|
|
|
|
bluetooth_processer_t s_bluetooth_processer = {
|
|
.bluetooth_processer_rx_buf = bluetooth_rx_buffer,
|
|
.bluetooth_processer_rx_buf_size = sizeof(bluetooth_rx_buffer),
|
|
.bluetooth_baundrate_one_packet_delay_ms = kbluetooth_baundrate_one_packet_delay_ms,
|
|
.port_delay_ms = port_timer_delay_ms,
|
|
|
|
.order = "order",
|
|
.index = 0,
|
|
.speed_level = 0,
|
|
.position = 0.0,
|
|
.direction = 0,
|
|
.code = 0,
|
|
.info = "noerror",
|
|
.deviceState = "init",
|
|
.deviceException = 0,
|
|
.deviceExceptionInfo = "noexception",
|
|
|
|
.cmd_flag = false,
|
|
.actively_report_flag = false,
|
|
};
|
|
|
|
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
|
|
{
|
|
switch (event)
|
|
{
|
|
#ifdef CONFIG_SET_RAW_ADV_DATA
|
|
case ESP_GAP_BLE_ADV_DATA_RAW_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_RAW_SET_COMPLETE_EVT:
|
|
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)
|
|
{
|
|
ESP_LOGE(GATTS_TAG, "Advertising start failed\n");
|
|
}
|
|
break;
|
|
case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:
|
|
if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS)
|
|
{
|
|
ESP_LOGE(GATTS_TAG, "Advertising stop failed\n");
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGI(GATTS_TAG, "Stop adv successfully\n");
|
|
}
|
|
break;
|
|
case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT:
|
|
ESP_LOGI(GATTS_TAG, "update connection params status = %d, min_int = %d, max_int = %d,conn_int = %d,latency = %d, timeout = %d",
|
|
param->update_conn_params.status,
|
|
param->update_conn_params.min_int,
|
|
param->update_conn_params.max_int,
|
|
param->update_conn_params.conn_int,
|
|
param->update_conn_params.latency,
|
|
param->update_conn_params.timeout);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
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;
|
|
if (param->write.need_rsp)
|
|
{
|
|
if (param->write.is_prep)
|
|
{
|
|
if (prepare_write_env->prepare_buf == NULL)
|
|
{
|
|
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)
|
|
{
|
|
ESP_LOGE(GATTS_TAG, "Gatt_server prep no mem\n");
|
|
status = ESP_GATT_NO_RESOURCES;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (param->write.offset > PREPARE_BUF_MAX_SIZE)
|
|
{
|
|
status = ESP_GATT_INVALID_OFFSET;
|
|
}
|
|
else if ((param->write.offset + param->write.len) > PREPARE_BUF_MAX_SIZE)
|
|
{
|
|
status = ESP_GATT_INVALID_ATTR_LEN;
|
|
}
|
|
}
|
|
|
|
esp_gatt_rsp_t *gatt_rsp = (esp_gatt_rsp_t *)malloc(sizeof(esp_gatt_rsp_t));
|
|
gatt_rsp->attr_value.len = param->write.len;
|
|
gatt_rsp->attr_value.handle = param->write.handle;
|
|
gatt_rsp->attr_value.offset = param->write.offset;
|
|
gatt_rsp->attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
|
|
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)
|
|
{
|
|
ESP_LOGE(GATTS_TAG, "Send response error\n");
|
|
}
|
|
free(gatt_rsp);
|
|
if (status != ESP_GATT_OK)
|
|
{
|
|
return;
|
|
}
|
|
memcpy(prepare_write_env->prepare_buf + param->write.offset,
|
|
param->write.value,
|
|
param->write.len);
|
|
prepare_write_env->prepare_len += param->write.len;
|
|
}
|
|
else
|
|
{
|
|
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, status, NULL);
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
esp_log_buffer_hex(GATTS_TAG, prepare_write_env->prepare_buf, prepare_write_env->prepare_len);
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGI(GATTS_TAG, "ESP_GATT_PREP_WRITE_CANCEL");
|
|
}
|
|
if (prepare_write_env->prepare_buf)
|
|
{
|
|
free(prepare_write_env->prepare_buf);
|
|
prepare_write_env->prepare_buf = NULL;
|
|
}
|
|
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)
|
|
{
|
|
gatts_profile_a_constructor(event, gatts_if, param);
|
|
switch (event)
|
|
{
|
|
case ESP_GATTS_REG_EVT:
|
|
ESP_LOGI(GATTS_TAG, "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;
|
|
// set unknown service uuid
|
|
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.len = ESP_UUID_LEN_128;
|
|
memcpy(gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.uuid.uuid128, adv_service_uuid128_test, 16);
|
|
// gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.uuid.uuid16 = GATTS_SERVICE_UUID_TEST_A;
|
|
|
|
esp_err_t set_dev_name_ret = esp_ble_gap_set_device_name(TEST_DEVICE_NAME);
|
|
if (set_dev_name_ret)
|
|
{
|
|
ESP_LOGE(GATTS_TAG, "set device name failed, error code = %x", set_dev_name_ret);
|
|
}
|
|
#ifdef CONFIG_SET_RAW_ADV_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
|
|
// 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;
|
|
case ESP_GATTS_READ_EVT:
|
|
{
|
|
ESP_LOGI(GATTS_TAG, "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 = param->read.handle;
|
|
rsp.attr_value.len = 4;
|
|
rsp.attr_value.value[0] = 0x11;
|
|
rsp.attr_value.value[1] = 0x22;
|
|
rsp.attr_value.value[2] = 0x33;
|
|
rsp.attr_value.value[3] = 0x44;
|
|
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:
|
|
{
|
|
// example_write_event_env(gatts_if, &a_prepare_write_env, param);
|
|
break;
|
|
}
|
|
case ESP_GATTS_EXEC_WRITE_EVT:
|
|
// ESP_LOGI(GATTS_TAG,"ESP_GATTS_EXEC_WRITE_EVT");
|
|
// esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
|
|
// 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_UNREG_EVT:
|
|
break;
|
|
case ESP_GATTS_CREATE_EVT:
|
|
ESP_LOGI(GATTS_TAG, "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;
|
|
// Unknown Characteristic uuid
|
|
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.len = ESP_UUID_LEN_128;
|
|
// gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid128 = adv_service_uuid128;
|
|
memcpy(gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid128, adv_service_uuid128_test, 16);
|
|
|
|
esp_ble_gatts_start_service(gl_profile_tab[PROFILE_A_APP_ID].service_handle);
|
|
// a_property = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
|
|
a_property = ESP_GATT_CHAR_PROP_BIT_READ;
|
|
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;
|
|
case ESP_GATTS_ADD_CHAR_EVT:
|
|
{
|
|
uint16_t length = 0;
|
|
const uint8_t *prf_char;
|
|
|
|
ESP_LOGI(GATTS_TAG, "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_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_128;
|
|
memcpy(gl_profile_tab[PROFILE_A_APP_ID].descr_uuid.uuid.uuid128, adv_service_uuid128_test, 16);
|
|
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_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_descr.attr_handle;
|
|
ESP_LOGI(GATTS_TAG, "ADD_DESCR_EVT, status %d, attr_handle %d, service_handle %d\n",
|
|
param->add_char_descr.status, param->add_char_descr.attr_handle, param->add_char_descr.service_handle);
|
|
break;
|
|
case ESP_GATTS_DELETE_EVT:
|
|
break;
|
|
case ESP_GATTS_START_EVT:
|
|
ESP_LOGI(GATTS_TAG, "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:
|
|
{
|
|
esp_ble_conn_update_params_t conn_params = {0};
|
|
memcpy(conn_params.bda, param->connect.remote_bda, sizeof(esp_bd_addr_t));
|
|
/* For the IOS system, please reference the apple official documents about the ble connection parameters restrictions. */
|
|
conn_params.latency = 0;
|
|
conn_params.max_int = 0x20; // max_int = 0x20*1.25ms = 40ms
|
|
conn_params.min_int = 0x10; // min_int = 0x10*1.25ms = 20ms
|
|
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:",
|
|
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]);
|
|
gl_profile_tab[PROFILE_A_APP_ID].conn_id = param->connect.conn_id;
|
|
// start sent the update connection parameters to the peer device.
|
|
esp_ble_gap_update_conn_params(&conn_params);
|
|
break;
|
|
}
|
|
case ESP_GATTS_DISCONNECT_EVT:
|
|
ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT, disconnect reason 0x%x", param->disconnect.reason);
|
|
esp_ble_gap_start_advertising(&adv_params);
|
|
break;
|
|
case ESP_GATTS_CONF_EVT:
|
|
// ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONF_EVT, status %d attr_handle %d", param->conf.status, param->conf.handle);
|
|
// if (param->conf.status != ESP_GATT_OK){
|
|
// esp_log_buffer_hex(GATTS_TAG, param->conf.value, param->conf.len);
|
|
// }
|
|
break;
|
|
case ESP_GATTS_OPEN_EVT:
|
|
case ESP_GATTS_CANCEL_OPEN_EVT:
|
|
case ESP_GATTS_CLOSE_EVT:
|
|
case ESP_GATTS_LISTEN_EVT:
|
|
case ESP_GATTS_CONGEST_EVT:
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
case ESP_GATTS_REG_EVT:
|
|
ESP_LOGI(GATTS_TAG, "REGISTER_APP_EVT, status %d, app_id %d\n", param->reg.status, param->reg.app_id);
|
|
gl_profile_tab[PROFILE_B_APP_ID].service_id.is_primary = true;
|
|
gl_profile_tab[PROFILE_B_APP_ID].service_id.id.inst_id = 0x00;
|
|
// gl_profile_tab[PROFILE_B_APP_ID].service_id.id.uuid.len = ESP_UUID_LEN_16;
|
|
// gl_profile_tab[PROFILE_B_APP_ID].service_id.id.uuid.uuid.uuid16 = GATTS_SERVICE_UUID_TEST_B;
|
|
gl_profile_tab[PROFILE_B_APP_ID].char_uuid.len = ESP_UUID_LEN_128;
|
|
memcpy(gl_profile_tab[PROFILE_B_APP_ID].char_uuid.uuid.uuid128, adv_service_uuid128_test, 16);
|
|
|
|
esp_ble_gatts_create_service(gatts_if, &gl_profile_tab[PROFILE_B_APP_ID].service_id, GATTS_NUM_HANDLE_TEST_B);
|
|
break;
|
|
case ESP_GATTS_READ_EVT:
|
|
{
|
|
// ESP_LOGI(GATTS_TAG, "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 = 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(gatts_if, param->read.conn_id, param->read.trans_id,
|
|
// ESP_GATT_OK, &rsp);
|
|
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);
|
|
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);
|
|
start_receive_data_to_buffer(param->write.len, param->write.value);
|
|
// for (int i = 0; i < param->write.len; i++)
|
|
// {
|
|
// bluetooth_rx_buffer[bluetooth_rx_buffer_len++] = param->write.value[i];
|
|
// }
|
|
}
|
|
example_write_event_env(gatts_if, &b_prepare_write_env, param);
|
|
break;
|
|
}
|
|
case ESP_GATTS_EXEC_WRITE_EVT:
|
|
// ESP_LOGI(GATTS_TAG,"ESP_GATTS_EXEC_WRITE_EVT");
|
|
// esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
|
|
// 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_UNREG_EVT:
|
|
break;
|
|
case ESP_GATTS_CREATE_EVT:
|
|
ESP_LOGI(GATTS_TAG, "CREATE_SERVICE_EVT, status %d, service_handle %d\n", param->create.status, param->create.service_handle);
|
|
gl_profile_tab[PROFILE_B_APP_ID].service_handle = param->create.service_handle;
|
|
// set profile b characteristic
|
|
gl_profile_tab[PROFILE_B_APP_ID].char_uuid.len = ESP_UUID_LEN_128;
|
|
// gl_profile_tab[PROFILE_B_APP_ID].char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST_B;
|
|
memcpy(gl_profile_tab[PROFILE_B_APP_ID].char_uuid.uuid.uuid128, adv_service_uuid128_test, 16);
|
|
|
|
esp_ble_gatts_start_service(gl_profile_tab[PROFILE_B_APP_ID].service_handle);
|
|
// b_property = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
|
|
b_property = ESP_GATT_CHAR_PROP_BIT_WRITE;
|
|
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;
|
|
case ESP_GATTS_ADD_CHAR_EVT:
|
|
ESP_LOGI(GATTS_TAG, "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_profile_tab[PROFILE_B_APP_ID].char_handle = param->add_char.attr_handle;
|
|
gl_profile_tab[PROFILE_B_APP_ID].descr_uuid.len = ESP_UUID_LEN_128;
|
|
memcpy(gl_profile_tab[PROFILE_B_APP_ID].descr_uuid.uuid.uuid128, adv_service_uuid128_test, 16);
|
|
esp_ble_gatts_add_char_descr(gl_profile_tab[PROFILE_B_APP_ID].service_handle, &gl_profile_tab[PROFILE_B_APP_ID].descr_uuid,
|
|
ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
|
|
NULL, NULL);
|
|
break;
|
|
case ESP_GATTS_ADD_CHAR_DESCR_EVT:
|
|
gl_profile_tab[PROFILE_B_APP_ID].descr_handle = param->add_char_descr.attr_handle;
|
|
ESP_LOGI(GATTS_TAG, "ADD_DESCR_EVT, status %d, attr_handle %d, service_handle %d\n",
|
|
param->add_char_descr.status, param->add_char_descr.attr_handle, param->add_char_descr.service_handle);
|
|
break;
|
|
case ESP_GATTS_DELETE_EVT:
|
|
break;
|
|
case ESP_GATTS_START_EVT:
|
|
ESP_LOGI(GATTS_TAG, "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:
|
|
ESP_LOGI(GATTS_TAG, "CONNECT_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:",
|
|
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]);
|
|
gl_profile_tab[PROFILE_B_APP_ID].conn_id = param->connect.conn_id;
|
|
break;
|
|
case ESP_GATTS_CONF_EVT:
|
|
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONF_EVT status %d attr_handle %d", param->conf.status, param->conf.handle);
|
|
if (param->conf.status != ESP_GATT_OK)
|
|
{
|
|
esp_log_buffer_hex(GATTS_TAG, param->conf.value, param->conf.len);
|
|
}
|
|
break;
|
|
case ESP_GATTS_DISCONNECT_EVT:
|
|
ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT, disconnect reason 0x%x", param->disconnect.reason);
|
|
esp_ble_gap_start_advertising(&adv_params);
|
|
break;
|
|
case ESP_GATTS_OPEN_EVT:
|
|
case ESP_GATTS_CANCEL_OPEN_EVT:
|
|
case ESP_GATTS_CLOSE_EVT:
|
|
case ESP_GATTS_LISTEN_EVT:
|
|
case ESP_GATTS_CONGEST_EVT:
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
|
|
{
|
|
/* 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
|
|
{
|
|
ESP_LOGI(GATTS_TAG, "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);
|
|
}
|
|
|
|
/***********************************************************************************************************************
|
|
* *******************************************************timer******************************************************* *
|
|
***********************************************************************************************************************/
|
|
// static bool IRAM_ATTR timer_group_isr_callback(void *args)
|
|
// {
|
|
// bluetooth_rx_buffer_start_receving = true;
|
|
// return pdFALSE;
|
|
// }
|
|
|
|
static void timer_group_init(int group, int timer, bool auto_reload, int timer_interval, uint32_t timer_unit_time)
|
|
{
|
|
/* Select and initialize basic parameters of the timer */
|
|
timer_config_t config = {
|
|
.divider = 80,
|
|
.counter_dir = TIMER_COUNT_UP,
|
|
.counter_en = TIMER_PAUSE,
|
|
.alarm_en = TIMER_ALARM_EN,
|
|
.auto_reload = auto_reload,
|
|
}; // default clock source is APB
|
|
timer_init(group, timer, &config);
|
|
|
|
/* Timer's counter will initially start from value below.
|
|
Also, if auto_reload is set, this value will be automatically reload on alarm */
|
|
timer_set_counter_value(group, timer, 0); //指定定时器首个计数值为0
|
|
|
|
//检查定时器的当前值,调用函数 timer_get_counter_value() 或 timer_get_counter_time_sec()。
|
|
//可通过调用函数 timer_pause() 随时暂停定时器。要再次启动它,调用函数 timer_start()。
|
|
|
|
/* Configure the alarm value and the interrupt on alarm. */
|
|
timer_set_alarm_value(group, timer, timer_interval * ((80 * timer_unit_time) / 80));
|
|
|
|
timer_enable_intr(group, timer);
|
|
|
|
// timer_isr_callback_add(group, timer, timer_group_isr_callback, NULL, 0);
|
|
|
|
// timer_start(group, timer);
|
|
timer_pause(group, timer);
|
|
}
|
|
|
|
/***********************************************************************************************************************
|
|
* *******************************************************main******************************************************** *
|
|
***********************************************************************************************************************/
|
|
void app_main(void)
|
|
{
|
|
esp_err_t ret;
|
|
|
|
// motor_init();
|
|
// motor_encoder_init();
|
|
|
|
// timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_interval_num, timer_interval_ms);
|
|
timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms);
|
|
|
|
// Initialize NVS.
|
|
ret = nvs_flash_init();
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
|
{
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
ret = nvs_flash_init();
|
|
}
|
|
ESP_ERROR_CHECK(ret);
|
|
|
|
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
|
|
|
|
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
|
|
|
ret = esp_bt_controller_init(&bt_cfg);
|
|
if (ret)
|
|
{
|
|
ESP_LOGE(GATTS_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
|
|
return;
|
|
}
|
|
|
|
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
|
|
if (ret)
|
|
{
|
|
ESP_LOGE(GATTS_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
|
|
return;
|
|
}
|
|
|
|
ret = esp_bluedroid_init();
|
|
if (ret)
|
|
{
|
|
ESP_LOGE(GATTS_TAG, "%s init bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
|
|
return;
|
|
}
|
|
|
|
ret = esp_bluedroid_enable();
|
|
if (ret)
|
|
{
|
|
ESP_LOGE(GATTS_TAG, "%s enable bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
|
|
return;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
|
|
if (local_mtu_ret)
|
|
{
|
|
ESP_LOGE(GATTS_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
|
|
}
|
|
|
|
while (true)
|
|
{
|
|
bluetooth_gatts_try_process_data();
|
|
if (s_bluetooth_processer.actively_report_flag)
|
|
{
|
|
receipt_json_get_status();
|
|
s_bluetooth_processer.actively_report_flag = false;
|
|
}
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
/***********************************************************************************************************************
|
|
* *************************************************user_funtion_def************************************************** *
|
|
***********************************************************************************************************************/
|
|
void buffer_all_init()
|
|
{
|
|
bluetooth_rx_buffer_len = 0;
|
|
memset(bluetooth_rx_buffer, 0, profile_b_buffer_size);
|
|
}
|
|
|
|
void timer_set_pause_and_counter_zero(int group, int timer)
|
|
{
|
|
timer_pause(group, timer);
|
|
timer_set_counter_value(group, timer, 0);
|
|
}
|
|
|
|
bool validation_param(cJSON *object, char *param)
|
|
{
|
|
cJSON *current_element = object->child;
|
|
|
|
while (current_element->string != NULL)
|
|
{
|
|
if (current_element->string == param)
|
|
{
|
|
return true;
|
|
}
|
|
current_element = current_element->next;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void bluetooth_gatts_try_process_data()
|
|
{
|
|
cJSON *json_tmp;
|
|
// cJSON *ch;
|
|
//开始接收
|
|
if (bluetooth_rx_buffer_start_receving)
|
|
{
|
|
//开启定时器
|
|
s_bluetooth_processer.port_delay_ms(s_bluetooth_processer.bluetooth_baundrate_one_packet_delay_ms);
|
|
// port_timer_delay_ms(kbluetooth_baundrate_one_packet_delay_ms);
|
|
bluetooth_rx_buffer_processing = true;
|
|
|
|
//打印输出
|
|
// ESP_LOGI(GATTS_TAG, "%s", s_bluetooth_processer.bluetooth_processer_rx_buf);
|
|
|
|
//验证解析数据是否正确
|
|
if (parse_rxbuffer_and_validation_data(&json_tmp))
|
|
{
|
|
// JSON解析到结构体,如果order更改表示有指令传输进来,并且更改指令标志位(cmd_flag)为true
|
|
if (parse_json_to_struct(json_tmp->child))
|
|
{
|
|
ESP_LOGI(GATTS_TAG, "order:%s ,index:%d speedLevel:%d position:%f direction:%d",
|
|
s_bluetooth_processer.order, s_bluetooth_processer.index, s_bluetooth_processer.speed_level,
|
|
s_bluetooth_processer.position, s_bluetooth_processer.direction);
|
|
if (strcmp(s_bluetooth_processer.order, set_position) == 0)
|
|
{
|
|
ESP_LOGI(GATTS_TAG, set_position);
|
|
// motor_cmd_set_position(s_bluetooth_processer.speed_level, s_bluetooth_processer.position, s_bluetooth_processer.direction);
|
|
receipt_json_set_position();
|
|
}
|
|
if (strcmp(s_bluetooth_processer.order, get_status) == 0)
|
|
{
|
|
ESP_LOGI(GATTS_TAG, get_status);
|
|
receipt_json_get_status();
|
|
}
|
|
// if (strcmp(s_bluetooth_processer.order, "deviceStatusReport") == 0)
|
|
// {
|
|
// ESP_LOGI(GATTS_TAG, "deviceStatusReport");
|
|
// }
|
|
s_bluetooth_processer.actively_report_flag = true;
|
|
}
|
|
}
|
|
|
|
//释放空间
|
|
cJSON_Delete(json_tmp);
|
|
// buffer置0
|
|
buffer_all_init();
|
|
//未在处理数据
|
|
s_bluetooth_processer.cmd_flag = false;
|
|
bluetooth_rx_buffer_start_receving = false;
|
|
bluetooth_rx_buffer_processing = false;
|
|
}
|
|
}
|
|
|
|
void port_timer_delay_ms(uint64_t delay)
|
|
{
|
|
uint64_t timer_count = 0;
|
|
timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
|
|
timer_start(TIMER_GROUP_0, TIMER_0);
|
|
|
|
while (timer_count < (delay * 1000))
|
|
{
|
|
timer_get_counter_value(TIMER_GROUP_0, TIMER_0, &timer_count);
|
|
}
|
|
|
|
timer_pause(TIMER_GROUP_0, TIMER_0);
|
|
}
|
|
|
|
void start_receive_data_to_buffer(uint16_t length, uint8_t *value)
|
|
{
|
|
bluetooth_rx_buffer_start_receving = true;
|
|
timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
|
|
//判断是否buffer越界
|
|
if ((length + bluetooth_rx_buffer_len) > profile_b_buffer_size)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!bluetooth_rx_buffer_processing)
|
|
{
|
|
//写入到buffer
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
bluetooth_rx_buffer[bluetooth_rx_buffer_len++] = value[i];
|
|
}
|
|
}
|
|
}
|
|
|
|
bool parse_rxbuffer_and_validation_data(cJSON **json_tmp)
|
|
{
|
|
*json_tmp = cJSON_Parse(&bluetooth_rx_buffer[0]);
|
|
if (*json_tmp == NULL)
|
|
{
|
|
ESP_LOGE(GATTS_TAG, "parse rxbuffer nil or redundant symbol ',' ");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool parse_json_to_struct(cJSON *ch)
|
|
{
|
|
uint8_t cmd_length = 0;
|
|
while (ch != NULL)
|
|
{
|
|
// ESP_LOGI(GATTS_TAG, "%s", ch->string);
|
|
if (strcmp(ch->string, "order") == 0)
|
|
{
|
|
s_bluetooth_processer.order = ch->valuestring;
|
|
|
|
if (strcmp(ch->valuestring, set_position) == 0)
|
|
{
|
|
cmd_length = cmd_length_set_position;
|
|
}
|
|
if (strcmp(ch->valuestring, get_status) == 0)
|
|
{
|
|
cmd_length = cmd_length_get_status;
|
|
}
|
|
// if (strcmp(ch->valuestring, "deviceStatusReport") == 0)
|
|
// {
|
|
// cmd_length = cmd_length_device_status_report;
|
|
// }
|
|
cmd_length--;
|
|
}
|
|
if (strcmp(ch->string, "index") == 0)
|
|
{
|
|
s_bluetooth_processer.index = ch->valueint;
|
|
cmd_length--;
|
|
}
|
|
if (strcmp(ch->string, "speedLevel") == 0)
|
|
{
|
|
s_bluetooth_processer.speed_level = ch->valueint;
|
|
cmd_length--;
|
|
}
|
|
if (strcmp(ch->string, "position") == 0)
|
|
{
|
|
s_bluetooth_processer.position = ch->valuedouble;
|
|
cmd_length--;
|
|
}
|
|
if (strcmp(ch->string, "direction") == 0)
|
|
{
|
|
s_bluetooth_processer.direction = ch->valueint;
|
|
cmd_length--;
|
|
}
|
|
ch = ch->next;
|
|
}
|
|
|
|
if (cmd_length == 0)
|
|
{
|
|
s_bluetooth_processer.cmd_flag = true;
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGE(GATTS_TAG, "JSON directive missing or exceeded");
|
|
}
|
|
|
|
return s_bluetooth_processer.cmd_flag;
|
|
}
|
|
|
|
void receipt_json_set_position()
|
|
{
|
|
cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
|
|
if (!pRoot)
|
|
{
|
|
return;
|
|
}
|
|
|
|
cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
|
|
cJSON_AddNumberToObject(pRoot, "code", s_bluetooth_processer.code);
|
|
cJSON_AddStringToObject(pRoot, "info", "success");
|
|
cJSON_AddNumberToObject(pRoot, "index", s_bluetooth_processer.index);
|
|
char *szJson = cJSON_Print(pRoot);
|
|
|
|
if (szJson != NULL)
|
|
{
|
|
ESP_LOGI(GATTS_TAG, "%s", szJson);
|
|
|
|
// esp_gatt_rsp_t rsp;
|
|
// memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
|
|
// rsp.attr_value.handle = constructor_param->read.handle;
|
|
// rsp.attr_value.len = 4;
|
|
// rsp.attr_value.value[0] = 0x11;
|
|
// rsp.attr_value.value[1] = 0x22;
|
|
// rsp.attr_value.value[2] = 0x33;
|
|
// rsp.attr_value.value[3] = 0x44;
|
|
|
|
// esp_ble_gatts_send_response(constructor_gatts_if, constructor_param->read.conn_id,
|
|
// constructor_param->read.trans_id, ESP_GATT_OK, &rsp);
|
|
|
|
free(szJson);
|
|
}
|
|
|
|
cJSON_Delete(pRoot);
|
|
}
|
|
|
|
void receipt_json_get_status()
|
|
{
|
|
cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
|
|
if (!pRoot)
|
|
{
|
|
return;
|
|
}
|
|
|
|
cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
|
|
cJSON_AddNumberToObject(pRoot, "index", s_bluetooth_processer.index);
|
|
cJSON_AddStringToObject(pRoot, "deviceState", s_bluetooth_processer.deviceState);
|
|
cJSON_AddNumberToObject(pRoot, "deviceException", s_bluetooth_processer.deviceException);
|
|
cJSON_AddStringToObject(pRoot, "deviceExceptionInfo", s_bluetooth_processer.deviceExceptionInfo);
|
|
cJSON_AddNumberToObject(pRoot, "position", s_bluetooth_processer.position);
|
|
|
|
char *szJson = cJSON_Print(pRoot);
|
|
|
|
if (szJson != NULL)
|
|
{
|
|
ESP_LOGI(GATTS_TAG, "%s", szJson);
|
|
free(szJson);
|
|
}
|
|
|
|
cJSON_Delete(pRoot);
|
|
}
|
|
|
|
void gatts_profile_a_constructor(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
|
|
{
|
|
constructor_event = event;
|
|
constructor_gatts_if = gatts_if;
|
|
constructor_param = param;
|
|
}
|