Browse Source

添加了json解析到结构体函数,优化了验证解析数据函数,添加了全局变量结构体并初始化

master
zwsd 3 years ago
parent
commit
09cd6034a5
  1. 84
      main/gatts_demo.c

84
main/gatts_demo.c

@ -208,14 +208,14 @@ typedef struct bluetooth_processer
void (*port_delay_ms)(uint64_t us); void (*port_delay_ms)(uint64_t us);
char *order; // char *order; //
uint8_t index; //
uint8_t speed_level; //
int index; //
int speed_level; //
double position; // double position; //
int direction; // int direction; //
uint8_t code; //
int code; //
char *info; // char *info; //
char *deviceState; // char *deviceState; //
uint8_t deviceException; //
int deviceException; //
char *deviceExceptionInfo; // char *deviceExceptionInfo; //
bool cmd_flag; bool cmd_flag;
@ -223,7 +223,7 @@ typedef struct bluetooth_processer
} bluetooth_processer_t; } bluetooth_processer_t;
#define profile_b_buffer_size 100
#define profile_b_buffer_size 128
char bluetooth_rx_buffer[profile_b_buffer_size] = {0}; char bluetooth_rx_buffer[profile_b_buffer_size] = {0};
uint8_t bluetooth_rx_buffer_len = 0; uint8_t bluetooth_rx_buffer_len = 0;
bool bluetooth_rx_buffer_start_receving = false; 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 port_timer_delay_ms(uint64_t delay);
void start_receive_data_to_buffer(uint16_t length, uint8_t *value); void start_receive_data_to_buffer(uint16_t length, uint8_t *value);
bool parse_rxbuffer_and_validation_data(cJSON **json_tmp); bool parse_rxbuffer_and_validation_data(cJSON **json_tmp);
bool parse_json_to_struct(cJSON *ch);
bluetooth_processer_t s_bluetooth_processer = { bluetooth_processer_t s_bluetooth_processer = {
.bluetooth_processer_rx_buf = bluetooth_rx_buffer, .bluetooth_processer_rx_buf = bluetooth_rx_buffer,
@ -920,7 +921,7 @@ bool validation_param(cJSON *object, char *param)
void bluetooth_gatts_try_process_data() void bluetooth_gatts_try_process_data()
{ {
cJSON *json_tmp; cJSON *json_tmp;
cJSON *ch;
// cJSON *ch;
// //
if (bluetooth_rx_buffer_start_receving) 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); // 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;
}
if (strcmp(ch->string, "position") == 0)
{
s_bluetooth_processer.position = ch->valuedouble;
}
if (strcmp(ch->string, "direction") == 0)
{
s_bluetooth_processer.direction = 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);
} }
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); cJSON_Delete(json_tmp);
// buffer置0 // buffer置0
buffer_all_init(); buffer_all_init();
// //
s_bluetooth_processer.cmd_flag = false;
bluetooth_rx_buffer_start_receving = false; bluetooth_rx_buffer_start_receving = false;
bluetooth_rx_buffer_processing = false; bluetooth_rx_buffer_processing = false;
} }
@ -1018,11 +994,39 @@ bool parse_rxbuffer_and_validation_data(cJSON **json_tmp)
if (*json_tmp == NULL) if (*json_tmp == NULL)
{ {
ESP_LOGE("ERROR", "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 false;
} }
return true; 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;
}
Loading…
Cancel
Save