From 09cd6034a51001a453656382959c4dd1d9e589ec Mon Sep 17 00:00:00 2001 From: zwsd Date: Mon, 11 Jul 2022 19:52:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86json=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=88=B0=E7=BB=93=E6=9E=84=E4=BD=93=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E4=BA=86=E9=AA=8C=E8=AF=81=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E6=95=B0=E6=8D=AE=E5=87=BD=E6=95=B0=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BA=86=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E4=BD=93=E5=B9=B6=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/gatts_demo.c | 84 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/main/gatts_demo.c b/main/gatts_demo.c index 506a852..10c9809 100644 --- a/main/gatts_demo.c +++ b/main/gatts_demo.c @@ -208,14 +208,14 @@ typedef struct bluetooth_processer void (*port_delay_ms)(uint64_t us); char *order; //指令名称 - uint8_t index; // - uint8_t speed_level; // + int index; // + int speed_level; // double position; //角度 int direction; //旋转方向 - uint8_t code; //错误码 + int code; //错误码 char *info; //错误码信息 char *deviceState; //设备状态 - uint8_t deviceException; //设备异常编号 + int deviceException; //设备异常编号 char *deviceExceptionInfo; //设备异常信息 bool cmd_flag; @@ -223,7 +223,7 @@ typedef struct bluetooth_processer } bluetooth_processer_t; -#define profile_b_buffer_size 100 +#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; @@ -246,6 +246,7 @@ 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); bluetooth_processer_t s_bluetooth_processer = { .bluetooth_processer_rx_buf = bluetooth_rx_buffer, @@ -920,7 +921,7 @@ bool validation_param(cJSON *object, char *param) void bluetooth_gatts_try_process_data() { cJSON *json_tmp; - cJSON *ch; + // cJSON *ch; //开始接收 if (bluetooth_rx_buffer_start_receving) { @@ -933,46 +934,21 @@ void bluetooth_gatts_try_process_data() // ESP_LOGI(GATTS_TAG, "%s", s_bluetooth_processer.bluetooth_processer_rx_buf); //验证解析数据是否正确 - if (!parse_rxbuffer_and_validation_data(&json_tmp)) + if (parse_rxbuffer_and_validation_data(&json_tmp)) { - return; - } - - ch = json_tmp->child; - - 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->string, "index") == 0) - { - s_bluetooth_processer.index = ch->valueint; - } - if (strcmp(ch->string, "speedLevel") == 0) + // JSON解析到结构体,如果order更改表示有指令传输进来,并且更改指令标志位(cmd_flag)为true + if (parse_json_to_struct(json_tmp->child)) { - s_bluetooth_processer.speed_level = ch->valueint; + 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(ch->string, "position") == 0) - { - s_bluetooth_processer.position = ch->valuedouble; - } - if (strcmp(ch->string, "direction") == 0) - { - s_bluetooth_processer.direction = ch->valueint; - } - ch = ch->next; } - 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); - //释放空间 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; } @@ -1018,11 +994,39 @@ bool parse_rxbuffer_and_validation_data(cJSON **json_tmp) if (*json_tmp == NULL) { ESP_LOGE("ERROR", "json_tmp null"); - cJSON_Delete(*json_tmp); - buffer_all_init(); - bluetooth_rx_buffer_start_receving = false; - bluetooth_rx_buffer_processing = false; return false; } return true; +} + +bool parse_json_to_struct(cJSON *ch) +{ + while (ch != NULL) + { + // ESP_LOGI(GATTS_TAG, "%s", ch->string); + if (strcmp(ch->string, "order") == 0) + { + s_bluetooth_processer.order = ch->valuestring; + s_bluetooth_processer.cmd_flag = true; + } + if (strcmp(ch->string, "index") == 0) + { + s_bluetooth_processer.index = ch->valueint; + } + if (strcmp(ch->string, "speedLevel") == 0) + { + s_bluetooth_processer.speed_level = ch->valueint; + } + if (strcmp(ch->string, "position") == 0) + { + s_bluetooth_processer.position = ch->valuedouble; + } + if (strcmp(ch->string, "direction") == 0) + { + s_bluetooth_processer.direction = ch->valueint; + } + ch = ch->next; + } + + return s_bluetooth_processer.cmd_flag; } \ No newline at end of file