Browse Source

解析数据封装

devtest
zwsd 3 years ago
parent
commit
a662b6c3e7
  1. 3
      .vscode/settings.json
  2. 1
      main/CMakeLists.txt
  3. 137
      main/ble_parse_data.c
  4. 46
      main/ble_parse_data.h
  5. 163
      main/ble_spp_server_demo.c
  6. 50
      main/ble_spp_server_demo.h
  7. 135
      main/main.c

3
.vscode/settings.json

@ -20,6 +20,7 @@
"esp_bt_main.h": "c",
"esp_gatts_api.h": "c",
"freertos.h": "c",
"regex": "c"
"regex": "c",
"ble_parse_data.h": "c"
},
}

1
main/CMakeLists.txt

@ -3,6 +3,7 @@ idf_component_register(SRCS #
"ble_spp_server_demo.c"
"main.c"
"timer_u.c"
"ble_parse_data.c"
INCLUDE_DIRS #
"../dep/"
".")

137
main/ble_parse_data.c

@ -0,0 +1,137 @@
#include "ble_parse_data.h"
#define GATTS_TABLE_TAG "GATTS_SPP_DEMO"
#define cmd_length_set_position 5
#define cmd_length_get_status 2
static bluetooth_processer_t *parse_bluetooth_processer;
uint8_t bluetooth_rx_buffer_len = 0;
bool test_flag;
void constructor_bluetooth_processer(bluetooth_processer_t *bluetooth_processer) { parse_bluetooth_processer = bluetooth_processer; }
void bluetooth_gatts_try_process_data() {
cJSON *json_tmp;
// cJSON *ch;
//
if (parse_bluetooth_processer->bluetooth_rx_buffer_start_receving) {
//
parse_bluetooth_processer->port_delay_ms(parse_bluetooth_processer->bluetooth_baundrate_one_packet_delay_ms);
// port_timer_delay_ms(kbluetooth_baundrate_one_packet_delay_ms);
parse_bluetooth_processer->bluetooth_rx_buffer_processing = true;
//
// ESP_LOGI(GATTS_TABLE_TAG, "%s", parse_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_TABLE_TAG, "order:%s ,index:%d speedLevel:%d position:%f direction:%d", parse_bluetooth_processer->order, parse_bluetooth_processer->index,
parse_bluetooth_processer->speedLevel, parse_bluetooth_processer->position, parse_bluetooth_processer->direction);
if (strcmp(parse_bluetooth_processer->order, set_position) == 0) {
ESP_LOGI(GATTS_TABLE_TAG, set_position);
// motor_cmd_set_position(parse_bluetooth_processer->speedLevel, parse_bluetooth_processer->position, parse_bluetooth_processer->direction);
// receipt_json_set_position();
}
if (strcmp(parse_bluetooth_processer->order, get_status) == 0) {
ESP_LOGI(GATTS_TABLE_TAG, get_status);
test_flag = true;
// receipt_json_get_status();
}
// if (strcmp(parse_bluetooth_processer->order, "deviceStatusReport") == 0)
// {
// ESP_LOGI(GATTS_TABLE_TAG, "deviceStatusReport");
// }
parse_bluetooth_processer->actively_report_flag = true;
}
}
//
cJSON_Delete(json_tmp);
// buffer置0
buffer_all_init();
//
parse_bluetooth_processer->cmd_flag = false;
parse_bluetooth_processer->bluetooth_rx_buffer_start_receving = false;
parse_bluetooth_processer->bluetooth_rx_buffer_processing = false;
}
}
void start_receive_data_to_buffer(uint16_t length, uint8_t *value) {
parse_bluetooth_processer->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 (!parse_bluetooth_processer->bluetooth_rx_buffer_processing) {
//buffer
for (int i = 0; i < length; i++) {
parse_bluetooth_processer->bluetooth_processer_rx_buf[bluetooth_rx_buffer_len++] = value[i];
}
}
}
void buffer_all_init() {
bluetooth_rx_buffer_len = 0;
memset(parse_bluetooth_processer->bluetooth_processer_rx_buf, 0, profile_b_buffer_size);
}
bool parse_rxbuffer_and_validation_data(cJSON **json_tmp) {
*json_tmp = cJSON_Parse(parse_bluetooth_processer->bluetooth_processer_rx_buf);
if (*json_tmp == NULL) {
ESP_LOGE(GATTS_TABLE_TAG, "parse rxbuffer null 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_TABLE_TAG, "%s", ch->string);
if (strcmp(ch->string, "order") == 0) {
parse_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) {
parse_bluetooth_processer->index = ch->valueint;
cmd_length--;
}
if (strcmp(ch->string, "speedLevel") == 0) {
parse_bluetooth_processer->speedLevel = ch->valueint;
cmd_length--;
}
if (strcmp(ch->string, "position") == 0) {
parse_bluetooth_processer->position = ch->valuedouble;
cmd_length--;
}
if (strcmp(ch->string, "direction") == 0) {
parse_bluetooth_processer->direction = ch->valueint;
cmd_length--;
}
ch = ch->next;
}
if (cmd_length == 0) {
parse_bluetooth_processer->cmd_flag = true;
} else {
ESP_LOGE(GATTS_TABLE_TAG, "JSON directive missing or exceeded");
}
return parse_bluetooth_processer->cmd_flag;
}

46
main/ble_parse_data.h

@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
#include "cJSON_Utils.h"
#include "driver/timer.h"
#include "esp_log.h"
#define profile_b_buffer_size 128
#define set_position "setPosition"
#define get_status "getStatus"
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);
bool bluetooth_rx_buffer_start_receving;
bool bluetooth_rx_buffer_processing;
char *order; //
int index; //
int speedLevel; //
double position; //
int direction; //
int code; //
char *info; //
char *deviceState; //
int deviceException; //
char *deviceExceptionInfo; //
bool cmd_flag;
bool actively_report_flag;
} bluetooth_processer_t;
void constructor_bluetooth_processer(bluetooth_processer_t *bluetooth_processer);
void bluetooth_gatts_try_process_data();
void start_receive_data_to_buffer(uint16_t length, uint8_t *value);
void buffer_all_init();
bool parse_rxbuffer_and_validation_data(cJSON **json_tmp);
bool parse_json_to_struct(cJSON *ch);

163
main/ble_spp_server_demo.c

@ -6,28 +6,14 @@
CONDITIONS OF ANY KIND, either express or implied.
*/
#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/uart.h"
#include "string.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_bt_defs.h"
#include "esp_bt_main.h"
#include "ble_spp_server_demo.h"
#define GATTS_TABLE_TAG "GATTS_SPP_DEMO"
static uint16_t *hid_conn_id;
static esp_gatt_if_t *hid_gatts_if;
static short unsigned int *hid_handle;
uint8_t bluetooth_rx_buffer_len = 0;
static bluetooth_processer_t *g_bluetooth_processer;
#define SPP_PROFILE_NUM 1
@ -54,8 +40,7 @@ static const uint8_t spp_adv_data[23] = {
/* Complete List of 16-bit Service Class UUIDs */
0x03, 0x03, 0xF0, 0xAB,
/* Complete Local Name in advertising */
0x0A,0x09, 'y', 'i', 'm', 'e', 'i', '_', 'b', 'l', 'e'
};
0x0A, 0x09, 'y', 'i', 'm', 'e', 'i', '_', 'b', 'l', 'e'};
static uint16_t spp_mtu_size = 23;
static uint16_t spp_conn_id = 0xffff;
@ -72,7 +57,9 @@ static uint8_t heartbeat_count_num = 0;
static bool enable_data_ntf = false;
static bool is_connected = false;
static esp_bd_addr_t spp_remote_bda = {0x0,};
static esp_bd_addr_t spp_remote_bda = {
0x0,
};
static uint16_t spp_handle_table[SPP_IDX_NB];
@ -115,19 +102,15 @@ typedef struct spp_receive_data_buff{
spp_receive_data_node_t *first_node;
} spp_receive_data_buff_t;
static spp_receive_data_buff_t SppRecvDataBuff = {
.node_num = 0,
.buff_size = 0,
.first_node = NULL
};
static spp_receive_data_buff_t SppRecvDataBuff = {.node_num = 0, .buff_size = 0, .first_node = NULL};
static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *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 spp_profile_tab[SPP_PROFILE_NUM] = {
[SPP_PROFILE_APP_IDX] = {
.gatts_cb = gatts_profile_event_handler,
.gatts_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
[SPP_PROFILE_APP_IDX] =
{
.gatts_cb = gatts_profile_event_handler, .gatts_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
},
};
@ -189,37 +172,31 @@ static const uint8_t spp_char_uuid128_tx[16] = {
};
/// 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_IDX_SVC] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ,
sizeof(spp_service_uuid128), sizeof(spp_service_uuid128), (uint8_t *)&spp_service_uuid128}},
[SPP_IDX_SVC] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ, sizeof(spp_service_uuid128), sizeof(spp_service_uuid128), (uint8_t *)&spp_service_uuid128}},
// SPP - data notify characteristic Declaration
[SPP_IDX_SPP_DATA_NOTIFY_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_IDX_SPP_DATA_NOTIFY_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 - data notify characteristic Value
[SPP_IDX_SPP_DATA_NTY_VAL] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_128, (uint8_t *)&spp_char_uuid128_rx, ESP_GATT_PERM_READ,
SPP_DATA_MAX_LEN, sizeof(spp_data_notify_val), (uint8_t *)spp_data_notify_val}},
[SPP_IDX_SPP_DATA_NTY_VAL] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_128, (uint8_t *)&spp_char_uuid128_rx, ESP_GATT_PERM_READ, SPP_DATA_MAX_LEN, sizeof(spp_data_notify_val), (uint8_t *)spp_data_notify_val}},
// SPP - data notify characteristic - Client Characteristic Configuration Descriptor
[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,
sizeof(uint16_t),sizeof(spp_data_notify_ccc), (uint8_t *)spp_data_notify_ccc}},
[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, sizeof(uint16_t), sizeof(spp_data_notify_ccc),
(uint8_t *)spp_data_notify_ccc}},
// SPP - data receive characteristic Declaration
[SPP_IDX_SPP_DATA_RECV_CHAR] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_WRITE,
CHAR_DECLARATION_SIZE,CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write}},
[SPP_IDX_SPP_DATA_RECV_CHAR] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_WRITE, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write}},
// SPP - data receive characteristic Value
[SPP_IDX_SPP_DATA_RECV_VAL] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_128, (uint8_t *)&spp_char_uuid128_tx, ESP_GATT_PERM_WRITE,
SPP_DATA_MAX_LEN,sizeof(spp_data_receive_val), (uint8_t *)spp_data_receive_val}},
[SPP_IDX_SPP_DATA_RECV_VAL] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_128, (uint8_t *)&spp_char_uuid128_tx, ESP_GATT_PERM_WRITE, SPP_DATA_MAX_LEN, sizeof(spp_data_receive_val), (uint8_t *)spp_data_receive_val}},
// //SPP - command characteristic Declaration
// [SPP_IDX_SPP_COMMAND_CHAR] =
@ -248,24 +225,23 @@ static const esp_gatts_attr_db_t spp_gatt_db[SPP_IDX_NB] =
#ifdef SUPPORT_HEARTBEAT
// SPP - Heart beat characteristic Declaration
[SPP_IDX_SPP_HEARTBEAT_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_notify}},
[SPP_IDX_SPP_HEARTBEAT_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_notify}},
// SPP - Heart beat characteristic Value
[SPP_IDX_SPP_HEARTBEAT_VAL] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&spp_heart_beat_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE,
sizeof(spp_heart_beat_val), sizeof(spp_heart_beat_val), (uint8_t *)spp_heart_beat_val}},
[SPP_IDX_SPP_HEARTBEAT_VAL] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&spp_heart_beat_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, sizeof(spp_heart_beat_val), sizeof(spp_heart_beat_val),
(uint8_t *)spp_heart_beat_val}},
// SPP - Heart beat characteristic - Client Characteristic Configuration Descriptor
[SPP_IDX_SPP_HEARTBEAT_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_data_notify_ccc), (uint8_t *)spp_heart_beat_ccc}},
[SPP_IDX_SPP_HEARTBEAT_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_data_notify_ccc),
(uint8_t *)spp_heart_beat_ccc}},
#endif
};
static uint8_t find_char_and_desr_index(uint16_t handle)
{
static uint8_t find_char_and_desr_index(uint16_t handle) {
uint8_t error = 0xff;
for (int i = 0; i < SPP_IDX_NB; i++) {
@ -277,8 +253,7 @@ static uint8_t find_char_and_desr_index(uint16_t handle)
return error;
}
static bool store_wr_buffer(esp_ble_gatts_cb_param_t *p_data)
{
static bool store_wr_buffer(esp_ble_gatts_cb_param_t *p_data) {
temp_spp_recv_data_node_p1 = (spp_receive_data_node_t *)malloc(sizeof(spp_receive_data_node_t));
if (temp_spp_recv_data_node_p1 == NULL) {
@ -304,8 +279,7 @@ static bool store_wr_buffer(esp_ble_gatts_cb_param_t *p_data)
return true;
}
static void free_write_buffer(void)
{
static void free_write_buffer(void) {
temp_spp_recv_data_node_p1 = SppRecvDataBuff.first_node;
while (temp_spp_recv_data_node_p1 != NULL) {
@ -320,8 +294,7 @@ static void free_write_buffer(void)
SppRecvDataBuff.first_node = NULL;
}
static void print_write_buffer(void)
{
static void print_write_buffer(void) {
temp_spp_recv_data_node_p1 = SppRecvDataBuff.first_node;
while (temp_spp_recv_data_node_p1 != NULL) {
@ -330,8 +303,7 @@ static void print_write_buffer(void)
}
}
void uart_task(void *pvParameters)
{
void uart_task(void *pvParameters) {
uart_event_t event;
uint8_t total_num = 0;
uint8_t current_num = 0;
@ -409,8 +381,7 @@ void uart_task(void *pvParameters)
vTaskDelete(NULL);
}
static void spp_uart_init(void)
{
static void spp_uart_init(void) {
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
@ -431,8 +402,7 @@ static void spp_uart_init(void)
}
#ifdef SUPPORT_HEARTBEAT
void spp_heartbeat_task(void * arg)
{
void spp_heartbeat_task(void *arg) {
uint16_t cmd_id;
for (;;) {
@ -456,8 +426,7 @@ void spp_heartbeat_task(void * arg)
}
#endif
void spp_cmd_task(void * arg)
{
void spp_cmd_task(void *arg) {
uint8_t *cmd_id;
for (;;) {
@ -470,8 +439,7 @@ void spp_cmd_task(void * arg)
vTaskDelete(NULL);
}
static void spp_task_init(void)
{
static void spp_task_init(void) {
spp_uart_init();
#ifdef SUPPORT_HEARTBEAT
@ -483,8 +451,7 @@ static void spp_task_init(void)
xTaskCreate(spp_cmd_task, "spp_cmd_task", 2048, NULL, 10, NULL);
}
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
esp_err_t err;
ESP_LOGE(GATTS_TABLE_TAG, "GAP_EVT, event %d\n", event);
@ -503,8 +470,7 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param
}
}
static void gatts_profile_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_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {
esp_ble_gatts_cb_param_t *p_data = (esp_ble_gatts_cb_param_t *)param;
uint8_t res = 0xff;
@ -637,11 +603,9 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
ESP_LOGI(GATTS_TABLE_TAG, "The number handle =%x\n", param->add_attr_tab.num_handle);
if (param->add_attr_tab.status != ESP_GATT_OK) {
ESP_LOGE(GATTS_TABLE_TAG, "Create attribute table failed, error code=0x%x", param->add_attr_tab.status);
}
else if (param->add_attr_tab.num_handle != SPP_IDX_NB){
} else if (param->add_attr_tab.num_handle != SPP_IDX_NB) {
ESP_LOGE(GATTS_TABLE_TAG, "Create attribute table abnormally, num_handle (%d) doesn't equal to HRS_IDX_NB(%d)", param->add_attr_tab.num_handle, SPP_IDX_NB);
}
else {
} else {
memcpy(spp_handle_table, param->add_attr_tab.handles, sizeof(spp_handle_table));
esp_ble_gatts_start_service(spp_handle_table[SPP_IDX_SVC]);
}
@ -652,9 +616,7 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
}
}
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *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) {
ESP_LOGI(GATTS_TABLE_TAG, "EVT %d, gatts if %d\n", event, gatts_if);
/* If event is register event, store the gatts_if for each profile */
@ -680,8 +642,7 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
} while (0);
}
void ble_spp_server_demo_app_main(uint16_t *conn_id_ble, esp_gatt_if_t *gatts_if_ble, uint16_t *handle_ble, bluetooth_processer_t *bluetooth_processer )
{
void ble_spp_server_demo_app_main(uint16_t *conn_id_ble, esp_gatt_if_t *gatts_if_ble, uint16_t *handle_ble, bluetooth_processer_t *bluetooth_processer) {
esp_err_t ret;
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
@ -729,33 +690,7 @@ void ble_spp_server_demo_app_main(uint16_t *conn_id_ble, esp_gatt_if_t *gatts_if
esp_ble_gap_register_callback(gap_event_handler);
esp_ble_gatts_app_register(ESP_SPP_APP_ID);
// spp_task_init();
spp_task_init();
return;
}
void start_receive_data_to_buffer(uint16_t length, uint8_t *value)
{
g_bluetooth_processer->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 (!g_bluetooth_processer->bluetooth_rx_buffer_processing)
{
//buffer
for (int i = 0; i < length; i++)
{
g_bluetooth_processer->bluetooth_processer_rx_buf[bluetooth_rx_buffer_len++] = value[i];
}
}
}
void buffer_all_init()
{
bluetooth_rx_buffer_len = 0;
memset(g_bluetooth_processer->bluetooth_processer_rx_buf, 0, profile_b_buffer_size);
}

50
main/ble_spp_server_demo.h

@ -10,11 +10,19 @@
#include <stdlib.h>
#include <string.h>
#include "driver/timer.h"
#include "cJSON.h"
#include "cJSON_Utils.h"
#define GATTS_TABLE_TAG "GATTS_SPP_DEMO"
#include "ble_parse_data.h"
#include "driver/uart.h"
#include "esp_bt.h"
#include "esp_bt_defs.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_log.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
#include "nvs_flash.h"
/*
* DEFINES
@ -55,36 +63,4 @@ enum {
SPP_IDX_NB,
};
#define profile_b_buffer_size 128
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);
bool bluetooth_rx_buffer_start_receving;
bool bluetooth_rx_buffer_processing;
char *order; //
int index; //
int speedLevel; //
double position; //
int direction; //
int code; //
char *info; //
char *deviceState; //
int deviceException; //
char *deviceExceptionInfo; //
bool cmd_flag;
bool actively_report_flag;
} bluetooth_processer_t;
void ble_spp_server_demo_app_main(uint16_t *conn_id_ble, esp_gatt_if_t *gatts_if_ble, uint16_t *handle_ble, bluetooth_processer_t *bluetooth_processer);
void start_receive_data_to_buffer(uint16_t length, uint8_t *value);
void buffer_all_init();

135
main/main.c

@ -11,24 +11,14 @@
#include "ble_spp_server_demo.h"
#include "timer_u.h"
#define set_position "setPosition"
#define get_status "getStatus"
#define GATTS_TABLE_TAG "GATTS_SPP_DEMO"
uint16_t table_conn_id_m;
esp_gatt_if_t table_gatts_if_m;
uint16_t table_handle_m;
#define cmd_length_set_position 5
#define cmd_length_get_status 2
bool test_flag;
static char bluetooth_tx_buffer[profile_b_buffer_size] = {0};
/***********************************************************************************************************************
* ****************************************************user_define**************************************************** *
***********************************************************************************************************************/
char bluetooth_rx_buffer[profile_b_buffer_size] = {0};
// ble_gatts_str_t ble_gatts_a_str = {
@ -37,13 +27,6 @@ char bluetooth_rx_buffer[profile_b_buffer_size] = {0};
// .send_gatts_if = 0,
// };
/***********************************************************************************************************************
* **********************************************user_function_statement********************************************** *
***********************************************************************************************************************/
void bluetooth_gatts_try_process_data();
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 bluetooth_rx_buffer_format_receipt();
@ -75,6 +58,7 @@ bluetooth_processer_t s_bluetooth_processer = {
};
void app_main(void) {
constructor_bluetooth_processer(&s_bluetooth_processer);
// ble_spp_server_demo_app_main(&table_conn_id_m, &table_gatts_if_m, &table_handle_m);
// table_conn_id_m =
// char temp_buffer[5] = {'1', '2', '3', '4', '5'};
@ -90,14 +74,6 @@ void app_main(void) {
ble_spp_server_demo_app_main(&table_conn_id_m, &table_gatts_if_m, &table_handle_m, &s_bluetooth_processer);
timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms);
while (true) {
// ESP_LOGI("test", "%d,%d,%d", strlen(bluetooth_tx_buffer), 89 / 15, 99 / 15);
// ESP_LOGI("test", "%d,%d,%d", table_conn_id_m, table_gatts_if_m, table_handle_m);
// if (table_handle_m != 0 && test_flag == true) {
// ets_delay_us(6000000);
// bluetooth_rx_buffer_send_indicate();
// test_flag = false;
// }
bluetooth_gatts_try_process_data();
if (s_bluetooth_processer.actively_report_flag) {
receipt_json_get_status();
@ -108,9 +84,6 @@ void app_main(void) {
return;
}
/***********************************************************************************************************************
* *************************************************user_funtion_def************************************************** *
***********************************************************************************************************************/
bool validation_param(cJSON *object, char *param) {
cJSON *current_element = object->child;
@ -123,110 +96,6 @@ bool validation_param(cJSON *object, char *param) {
return false;
}
void bluetooth_gatts_try_process_data() {
cJSON *json_tmp;
// cJSON *ch;
//
if (s_bluetooth_processer.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);
s_bluetooth_processer.bluetooth_rx_buffer_processing = true;
//
// ESP_LOGI(GATTS_TABLE_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_TABLE_TAG, "order:%s ,index:%d speedLevel:%d position:%f direction:%d", s_bluetooth_processer.order, s_bluetooth_processer.index, s_bluetooth_processer.speedLevel,
s_bluetooth_processer.position, s_bluetooth_processer.direction);
if (strcmp(s_bluetooth_processer.order, set_position) == 0) {
ESP_LOGI(GATTS_TABLE_TAG, set_position);
// motor_cmd_set_position(s_bluetooth_processer.speedLevel, s_bluetooth_processer.position, s_bluetooth_processer.direction);
// receipt_json_set_position();
}
if (strcmp(s_bluetooth_processer.order, get_status) == 0) {
ESP_LOGI(GATTS_TABLE_TAG, get_status);
test_flag = true;
// receipt_json_get_status();
}
// if (strcmp(s_bluetooth_processer.order, "deviceStatusReport") == 0)
// {
// ESP_LOGI(GATTS_TABLE_TAG, "deviceStatusReport");
// }
s_bluetooth_processer.actively_report_flag = true;
}
}
//
cJSON_Delete(json_tmp);
// buffer置0
buffer_all_init();
//
s_bluetooth_processer.cmd_flag = false;
s_bluetooth_processer.bluetooth_rx_buffer_start_receving = false;
s_bluetooth_processer.bluetooth_rx_buffer_processing = false;
}
}
bool parse_rxbuffer_and_validation_data(cJSON **json_tmp) {
*json_tmp = cJSON_Parse(&bluetooth_rx_buffer[0]);
if (*json_tmp == NULL) {
ESP_LOGE(GATTS_TABLE_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_TABLE_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.speedLevel = 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_TABLE_TAG, "JSON directive missing or exceeded");
}
return s_bluetooth_processer.cmd_flag;
}
void receipt_json_set_position() {
cJSON *pRoot = cJSON_CreateObject(); //
if (!pRoot) {

Loading…
Cancel
Save