Browse Source

封装解析数据

master
zwsd 3 years ago
parent
commit
79450725e6
  1. 103
      main/gatts_demo.c
  2. 49
      main/t_bluetooth.c
  3. 51
      main/t_bluetooth.h

103
main/gatts_demo.c

@ -20,51 +20,8 @@
/*********************************************************************************************************************** /***********************************************************************************************************************
* ****************************************************user_define**************************************************** * * ****************************************************user_define**************************************************** *
***********************************************************************************************************************/ ***********************************************************************************************************************/
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);
char *order; //
int index; //
int speed_level; //
double position; //
int direction; //
int code; //
char *info; //
char *deviceState; //
int deviceException; //
char *deviceExceptionInfo; //
bool cmd_flag;
bool actively_report_flag;
} bluetooth_processer_t;
#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;
bool bluetooth_rx_buffer_start_receving = false;
bool bluetooth_rx_buffer_processing = false;
bool flag = true;
uint32_t total = 0;
#define timer_group0_interval_num 10000
// #define timer_group1_interval_num 5
#define timer_interval_s (uint32_t)1000000
#define timer_interval_ms (uint32_t)1000
#define timer_interval_us (uint32_t)1
#define kbluetooth_baundrate_one_packet_delay_ms 200
#define cmd_length_set_position 5
#define cmd_length_get_status 2
// #define cmd_length_device_status_report 6
#define set_position "setPosition"
#define get_status "getStatus"
ble_gatts_str_t ble_gatts_a_str = { ble_gatts_str_t ble_gatts_a_str = {
.attr_handle = 0, .attr_handle = 0,
@ -79,7 +36,6 @@ void buffer_all_init();
void bluetooth_gatts_try_process_data(); void bluetooth_gatts_try_process_data();
void timer_set_pause_and_counter_zero(int group, int timer); 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);
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); bool parse_json_to_struct(cJSON *ch);
void receipt_json_set_position(); void receipt_json_set_position();
@ -92,6 +48,9 @@ bluetooth_processer_t s_bluetooth_processer = {
.bluetooth_baundrate_one_packet_delay_ms = kbluetooth_baundrate_one_packet_delay_ms, .bluetooth_baundrate_one_packet_delay_ms = kbluetooth_baundrate_one_packet_delay_ms,
.port_delay_ms = port_timer_delay_ms, .port_delay_ms = port_timer_delay_ms,
.bluetooth_rx_buffer_start_receving = false,
.bluetooth_rx_buffer_processing = false,
.order = "order", .order = "order",
.index = 0, .index = 0,
.speed_level = 0, .speed_level = 0,
@ -151,10 +110,10 @@ void timer_group_init(int group, int timer, bool auto_reload, int timer_interval
***********************************************************************************************************************/ ***********************************************************************************************************************/
void app_main(void) void app_main(void)
{ {
esp_ble_gatts_init(&ble_gatts_a_str);
esp_ble_gatts_init(&ble_gatts_a_str, &s_bluetooth_processer);
// motor_init(); // motor_init();
// motor_encoder_init(); // motor_encoder_init();
// timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms);
timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms);
// uint8_t temp[5] = "hello"; // uint8_t temp[5] = "hello";
// esp_gatt_rsp_t rsp; // esp_gatt_rsp_t rsp;
@ -173,18 +132,18 @@ void app_main(void)
// ESP_LOGI(GATTS_TAG, "%d,%d,%d", ble_gatts_a_str.attr_handle, ble_gatts_a_str.send_conn_id, ble_gatts_a_str.send_gatts_if); // ESP_LOGI(GATTS_TAG, "%d,%d,%d", ble_gatts_a_str.attr_handle, ble_gatts_a_str.send_conn_id, ble_gatts_a_str.send_gatts_if);
// esp_ble_gatts_send_indicate(ble_gatts_a_str.send_gatts_if, ble_gatts_a_str.send_conn_id, // esp_ble_gatts_send_indicate(ble_gatts_a_str.send_gatts_if, ble_gatts_a_str.send_conn_id,
// ble_gatts_a_str.attr_handle, 5, temp, true); // ble_gatts_a_str.attr_handle, 5, temp, true);
// esp_ble_gatts_send_response(ble_gatts_a_str.send_gatts_if, ble_gatts_a_str.send_conn_id, 1,
// ESP_GATT_OK, &rsp);
// ets_delay_us(1000000);
// esp_ble_gatts_send_response(ble_gatts_a_str.send_gatts_if, ble_gatts_a_str.send_conn_id, 1,
// ESP_GATT_OK, &rsp);
// ets_delay_us(1000000);
// } // }
// esp_ble_gatts_set_attr_value(ble_gatts_a_str.constructor_param->read.handle, 5, temp); // esp_ble_gatts_set_attr_value(ble_gatts_a_str.constructor_param->read.handle, 5, temp);
// bluetooth_gatts_try_process_data();
// if (s_bluetooth_processer.actively_report_flag)
// {
// receipt_json_get_status();
// s_bluetooth_processer.actively_report_flag = false;
// }
bluetooth_gatts_try_process_data();
if (s_bluetooth_processer.actively_report_flag)
{
receipt_json_get_status();
s_bluetooth_processer.actively_report_flag = false;
}
} }
return; return;
@ -193,11 +152,7 @@ void app_main(void)
/*********************************************************************************************************************** /***********************************************************************************************************************
* *************************************************user_funtion_def************************************************** * * *************************************************user_funtion_def************************************************** *
***********************************************************************************************************************/ ***********************************************************************************************************************/
void buffer_all_init()
{
bluetooth_rx_buffer_len = 0;
memset(bluetooth_rx_buffer, 0, profile_b_buffer_size);
}
void timer_set_pause_and_counter_zero(int group, int timer) void timer_set_pause_and_counter_zero(int group, int timer)
{ {
@ -225,12 +180,12 @@ void bluetooth_gatts_try_process_data()
cJSON *json_tmp; cJSON *json_tmp;
// cJSON *ch; // cJSON *ch;
// //
if (bluetooth_rx_buffer_start_receving)
if (s_bluetooth_processer.bluetooth_rx_buffer_start_receving)
{ {
// //
s_bluetooth_processer.port_delay_ms(s_bluetooth_processer.bluetooth_baundrate_one_packet_delay_ms); 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); // port_timer_delay_ms(kbluetooth_baundrate_one_packet_delay_ms);
bluetooth_rx_buffer_processing = true;
s_bluetooth_processer.bluetooth_rx_buffer_processing = true;
// //
// ESP_LOGI(GATTS_TAG, "%s", s_bluetooth_processer.bluetooth_processer_rx_buf); // ESP_LOGI(GATTS_TAG, "%s", s_bluetooth_processer.bluetooth_processer_rx_buf);
@ -269,8 +224,8 @@ void bluetooth_gatts_try_process_data()
buffer_all_init(); buffer_all_init();
// //
s_bluetooth_processer.cmd_flag = false; s_bluetooth_processer.cmd_flag = false;
bluetooth_rx_buffer_start_receving = false;
bluetooth_rx_buffer_processing = false;
s_bluetooth_processer.bluetooth_rx_buffer_start_receving = false;
s_bluetooth_processer.bluetooth_rx_buffer_processing = false;
} }
} }
@ -288,26 +243,6 @@ void port_timer_delay_ms(uint64_t delay)
timer_pause(TIMER_GROUP_0, TIMER_0); timer_pause(TIMER_GROUP_0, TIMER_0);
} }
void start_receive_data_to_buffer(uint16_t length, uint8_t *value)
{
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 (!bluetooth_rx_buffer_processing)
{
//buffer
for (int i = 0; i < length; i++)
{
bluetooth_rx_buffer[bluetooth_rx_buffer_len++] = value[i];
}
}
}
bool parse_rxbuffer_and_validation_data(cJSON **json_tmp) bool parse_rxbuffer_and_validation_data(cJSON **json_tmp)
{ {
*json_tmp = cJSON_Parse(&bluetooth_rx_buffer[0]); *json_tmp = cJSON_Parse(&bluetooth_rx_buffer[0]);

49
main/t_bluetooth.c

@ -4,7 +4,10 @@
* *******************************************************user******************************************************** * * *******************************************************user******************************************************** *
***********************************************************************************************************************/ ***********************************************************************************************************************/
static ble_gatts_str_t *ble_gatts_a_structer;
uint8_t bluetooth_rx_buffer_len = 0;
static ble_gatts_str_t *g_ble_gatts_a_structer;
static bluetooth_processer_t *g_bluetooth_processer;
/*********************************************************************************************************************** /***********************************************************************************************************************
* ******************************************************************************************************************* * * ******************************************************************************************************************* *
@ -477,12 +480,7 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
{ {
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value :", param->write.len); ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value :", param->write.len);
esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len); esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len);
ESP_LOGI(GATTS_TAG, "ye!!!!!!!!!");
// start_receive_data_to_buffer(param->write.len, param->write.value);
// for (int i = 0; i < param->write.len; i++)
// {
// bluetooth_rx_buffer[bluetooth_rx_buffer_len++] = param->write.value[i];
// }
start_receive_data_to_buffer(param->write.len, param->write.value);
} }
example_write_event_env(gatts_if, &b_prepare_write_env, param); example_write_event_env(gatts_if, &b_prepare_write_env, param);
break; break;
@ -608,11 +606,12 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
} while (0); } while (0);
} }
void esp_ble_gatts_init(ble_gatts_str_t *ble_gatts_a_str)
void esp_ble_gatts_init(ble_gatts_str_t *ble_gatts_a_str, bluetooth_processer_t *bluetooth_processer)
{ {
esp_err_t ret; esp_err_t ret;
ble_gatts_a_structer = ble_gatts_a_str;
g_ble_gatts_a_structer = ble_gatts_a_str;
g_bluetooth_processer = bluetooth_processer;
// Initialize NVS. // Initialize NVS.
ret = nvs_flash_init(); ret = nvs_flash_init();
@ -692,7 +691,33 @@ void esp_ble_gatts_init(ble_gatts_str_t *ble_gatts_a_str)
void gatts_profile_a_constructor(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) void gatts_profile_a_constructor(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
{ {
ble_gatts_a_structer->attr_handle = param->read.handle;
ble_gatts_a_structer->send_conn_id = param->read.conn_id;
ble_gatts_a_structer->send_gatts_if = gatts_if;
g_ble_gatts_a_structer->attr_handle = param->read.handle;
g_ble_gatts_a_structer->send_conn_id = param->read.conn_id;
g_ble_gatts_a_structer->send_gatts_if = gatts_if;
}
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);
} }

51
main/t_bluetooth.h

@ -46,6 +46,25 @@
#define PROFILE_A_APP_ID 0 #define PROFILE_A_APP_ID 0
#define PROFILE_B_APP_ID 1 #define PROFILE_B_APP_ID 1
/***********************************************************************************************************************
* ****************************************************user_define**************************************************** *
***********************************************************************************************************************/
#define profile_b_buffer_size 128
#define timer_group0_interval_num 10000
// #define timer_group1_interval_num 5
#define timer_interval_s (uint32_t)1000000
#define timer_interval_ms (uint32_t)1000
#define timer_interval_us (uint32_t)1
#define kbluetooth_baundrate_one_packet_delay_ms 200
#define cmd_length_set_position 5
#define cmd_length_get_status 2
// #define cmd_length_device_status_report 6
#define set_position "setPosition"
#define get_status "getStatus"
struct gatts_profile_inst struct gatts_profile_inst
{ {
esp_gatts_cb_t gatts_cb; esp_gatts_cb_t gatts_cb;
@ -75,8 +94,38 @@ typedef struct ble_gatts_str
uint16_t attr_handle; uint16_t attr_handle;
} ble_gatts_str_t; } ble_gatts_str_t;
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 speed_level; //
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 example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param); void example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param);
void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param); void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param);
void esp_ble_gatts_init(ble_gatts_str_t* ble_gatts_a_str);
void esp_ble_gatts_init(ble_gatts_str_t *ble_gatts_a_str, bluetooth_processer_t *bluetooth_processer);
void gatts_profile_a_constructor(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param); void gatts_profile_a_constructor(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
void start_receive_data_to_buffer(uint16_t length, uint8_t *value);
void buffer_all_init();
Loading…
Cancel
Save