#include "ble_parse_data.h" #include "motor_drive.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; 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) { parse_bluetooth_processer->auto_report_flag = true; ESP_LOGI(BLE_PARSE_DATA_TAG, set_position); motor_drive_set_packages_ctr(352.68); // 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); receipt_json_get_status(); } // if (strcmp(parse_bluetooth_processer->order, "deviceStatusReport") == 0) // { // ESP_LOGI(BLE_PARSE_DATA_TAG, "deviceStatusReport"); // } } } //释放空间 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; } bool validation_param(cJSON *object, char *param) { cJSON *current_element = object->child; while (current_element->string != NULL) { if (current_element->string == param) { return true; } current_element = current_element->next; } return false; } void receipt_json_set_position() { cJSON *pRoot = cJSON_CreateObject(); //创建一个对象 if (!pRoot) { return; } cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点 cJSON_AddNumberToObject(pRoot, "code", parse_bluetooth_processer->code); cJSON_AddStringToObject(pRoot, "info", "success"); cJSON_AddNumberToObject(pRoot, "index", parse_bluetooth_processer->index); char *szJson = cJSON_Print(pRoot); if (szJson != NULL) { ESP_LOGI(BLE_PARSE_DATA_TAG, "%s", szJson); free(szJson); } cJSON_Delete(pRoot); } void receipt_json_get_status() { cJSON *pRoot = cJSON_CreateObject(); //创建一个对象 if (!pRoot) { return; } parse_bluetooth_processer->motor_drive_turn_flag = true; cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点 cJSON_AddNumberToObject(pRoot, "index", parse_bluetooth_processer->index); cJSON_AddStringToObject(pRoot, "deviceState", parse_bluetooth_processer->deviceState); cJSON_AddNumberToObject(pRoot, "deviceException", parse_bluetooth_processer->deviceException); cJSON_AddStringToObject(pRoot, "deviceExceptionInfo", parse_bluetooth_processer->deviceExceptionInfo); cJSON_AddNumberToObject(pRoot, "position", parse_bluetooth_processer->position); char *szJson = cJSON_Print(pRoot); if (szJson != NULL) { ESP_LOGI(BLE_PARSE_DATA_TAG, "%s", szJson); free(szJson); } cJSON_Delete(pRoot); } void bluetooth_auto_report_format_receipt() { sprintf(parse_bluetooth_processer->bluetooth_processer_tx_buf, "{ \"order\": \"receipt\", \"index\": %d, \"speedLevel\": %d, \"position\": %.2lf, \"direction\": %d }", // parse_bluetooth_processer->index, parse_bluetooth_processer->speedLevel, parse_bluetooth_processer->position, parse_bluetooth_processer->direction); } void bluetooth_tx_buffer_send_indicate(cb_t format) { char temp_buffer[20] = {0}; uint8_t temp_lenght = 0; uint8_t temp_count_total = 0; uint8_t temp_last_count_not_15 = false; uint8_t temp_count_remainder = 0; uint8_t i = 0; format(); temp_lenght = strlen(parse_bluetooth_processer->bluetooth_processer_tx_buf); temp_count_total = temp_lenght / 15; if ((temp_lenght % 15) != 0) { temp_count_remainder = temp_lenght % 15; temp_last_count_not_15 = true; } for (i = 0; i < temp_count_total; i++) { string_copy_by_num(temp_buffer, parse_bluetooth_processer->bluetooth_processer_tx_buf, i, 15); esp_ble_gatts_send_indicate(parse_bluetooth_processer->table_gatts_if_m, parse_bluetooth_processer->table_conn_id_m, // parse_bluetooth_processer->table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false); } if (temp_last_count_not_15 == true) { string_copy_by_num(temp_buffer, parse_bluetooth_processer->bluetooth_processer_tx_buf, temp_count_total, temp_count_remainder); esp_ble_gatts_send_indicate(parse_bluetooth_processer->table_gatts_if_m, parse_bluetooth_processer->table_conn_id_m, // parse_bluetooth_processer->table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false); } } void string_copy_by_num(char *dest, const char *src, uint8_t count, uint8_t num) { int i = 0; memset(dest, '\0', 15); if (dest == NULL || src == NULL || num == 0) { /* code */ ESP_LOGW(BLE_PARSE_DATA_TAG, "string_copy_by_num function parameter is empty"); } for (i = 0; i < num; i++) { dest[i] = src[(count * 15) + i]; } } void bluetooth_active_notify(uint8_t *buffer, uint8_t buffer_size) { if (parse_bluetooth_processer->table_handle_m != 0) { esp_ble_gatts_send_indicate(parse_bluetooth_processer->table_gatts_if_m, parse_bluetooth_processer->table_conn_id_m, // parse_bluetooth_processer->table_handle_m, buffer_size, buffer, false); } }