7 changed files with 715 additions and 750 deletions
-
3.vscode/settings.json
-
1main/CMakeLists.txt
-
137main/ble_parse_data.c
-
46main/ble_parse_data.h
-
1091main/ble_spp_server_demo.c
-
52main/ble_spp_server_demo.h
-
135main/main.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; |
||||
|
} |
@ -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); |
1091
main/ble_spp_server_demo.c
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
Write
Preview
Loading…
Cancel
Save
Reference in new issue