#include "ble_parse_data.h" #define BLE_PARSE_DATA_TAG "BLE_PARSE_DATA" #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(BLE_PARSE_DATA_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(BLE_PARSE_DATA_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(BLE_PARSE_DATA_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(BLE_PARSE_DATA_TAG, get_status); test_flag = true; // receipt_json_get_status(); } // if (strcmp(parse_bluetooth_processer->order, "deviceStatusReport") == 0) // { // ESP_LOGI(BLE_PARSE_DATA_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(BLE_PARSE_DATA_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(BLE_PARSE_DATA_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(BLE_PARSE_DATA_TAG, "JSON directive missing or exceeded"); } return parse_bluetooth_processer->cmd_flag; }