From a518a99129ad69dfdc77476ce28237933b21fba0 Mon Sep 17 00:00:00 2001 From: zwsd Date: Thu, 21 Jul 2022 17:49:37 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E5=B0=81=E8=A3=85=E4=B8=BB?= =?UTF-8?q?=E5=8A=A8=E4=B8=8A=E6=8A=A5=EF=BC=8C=E5=B0=86tx=5Fbuffer?= =?UTF-8?q?=E5=8A=A0=E5=85=A5=E5=88=B0=E7=BB=93=E6=9E=84=E4=BD=93=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/ble_parse_data.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++ main/ble_parse_data.h | 9 +++- main/main.c | 112 +------------------------------------------------- 3 files changed, 114 insertions(+), 111 deletions(-) diff --git a/main/ble_parse_data.c b/main/ble_parse_data.c index 65165d2..c1c99d8 100644 --- a/main/ble_parse_data.c +++ b/main/ble_parse_data.c @@ -134,4 +134,108 @@ bool parse_json_to_struct(cJSON *ch) { } 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; + } + + 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() { + 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; + + bluetooth_auto_report_format_receipt(); + + 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]; + } } \ No newline at end of file diff --git a/main/ble_parse_data.h b/main/ble_parse_data.h index d453c99..46ba18f 100644 --- a/main/ble_parse_data.h +++ b/main/ble_parse_data.h @@ -17,6 +17,8 @@ typedef struct bluetooth_processer { char *bluetooth_processer_rx_buf; uint8_t bluetooth_processer_rx_buf_size; // + char *bluetooth_processer_tx_buf; + uint8_t bluetooth_processer_tx_buf_size; // int bluetooth_baundrate_one_packet_delay_ms; void (*port_delay_ms)(uint64_t us); @@ -49,4 +51,9 @@ void bluetooth_gatts_try_process_data(); void start_receive_data_to_buffer(uint16_t length, uint8_t *value); void buffer_all_init(); bool parse_rxbuffer_and_validation_data(cJSON **json_tmp); -bool parse_json_to_struct(cJSON *ch); \ No newline at end of file +bool parse_json_to_struct(cJSON *ch); +void receipt_json_set_position(); +void receipt_json_get_status(); +void bluetooth_auto_report_format_receipt(); +void bluetooth_tx_buffer_send_indicate(); +void string_copy_by_num(char *dest, const char *src, uint8_t count, uint8_t num); \ No newline at end of file diff --git a/main/main.c b/main/main.c index 7d20fa8..7f26bcc 100644 --- a/main/main.c +++ b/main/main.c @@ -15,15 +15,11 @@ static char bluetooth_tx_buffer[profile_b_buffer_size] = {0}; static char bluetooth_rx_buffer[profile_b_buffer_size] = {0}; -void receipt_json_set_position(); -void receipt_json_get_status(); -void bluetooth_auto_report_format_receipt(); -void bluetooth_tx_buffer_send_indicate(); -void string_copy_by_num(char *dest, const char *src, uint8_t count, uint8_t num); - bluetooth_processer_t s_bluetooth_processer = { .bluetooth_processer_rx_buf = bluetooth_rx_buffer, .bluetooth_processer_rx_buf_size = sizeof(bluetooth_rx_buffer), + .bluetooth_processer_tx_buf = bluetooth_tx_buffer, + .bluetooth_processer_tx_buf_size = sizeof(bluetooth_tx_buffer), .bluetooth_baundrate_one_packet_delay_ms = kbluetooth_baundrate_one_packet_delay_ms, .port_delay_ms = port_timer_delay_ms, @@ -64,107 +60,3 @@ void app_main(void) { return; } - -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", s_bluetooth_processer.code); - cJSON_AddStringToObject(pRoot, "info", "success"); - cJSON_AddNumberToObject(pRoot, "index", s_bluetooth_processer.index); - char *szJson = cJSON_Print(pRoot); - - if (szJson != NULL) { - ESP_LOGI(MAIN_TAG, "%s", szJson); - - free(szJson); - } - - cJSON_Delete(pRoot); -} - -void receipt_json_get_status() { - cJSON *pRoot = cJSON_CreateObject(); //创建一个对象 - if (!pRoot) { - return; - } - - cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点 - cJSON_AddNumberToObject(pRoot, "index", s_bluetooth_processer.index); - cJSON_AddStringToObject(pRoot, "deviceState", s_bluetooth_processer.deviceState); - cJSON_AddNumberToObject(pRoot, "deviceException", s_bluetooth_processer.deviceException); - cJSON_AddStringToObject(pRoot, "deviceExceptionInfo", s_bluetooth_processer.deviceExceptionInfo); - cJSON_AddNumberToObject(pRoot, "position", s_bluetooth_processer.position); - - char *szJson = cJSON_Print(pRoot); - - if (szJson != NULL) { - ESP_LOGI(MAIN_TAG, "%s", szJson); - free(szJson); - } - - cJSON_Delete(pRoot); -} - -void bluetooth_auto_report_format_receipt() { - sprintf(bluetooth_tx_buffer, "{ \"order\": \"receipt\", \"index\": %d, \"speedLevel\": %d, \"position\": %.2lf, \"direction\": %d }", // - s_bluetooth_processer.index, s_bluetooth_processer.speedLevel, s_bluetooth_processer.position, s_bluetooth_processer.direction); -} - -void bluetooth_tx_buffer_send_indicate() { - 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; - - bluetooth_auto_report_format_receipt(); - - temp_lenght = strlen(bluetooth_tx_buffer); - 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, bluetooth_tx_buffer, i, 15); - esp_ble_gatts_send_indicate(s_bluetooth_processer.table_gatts_if_m, s_bluetooth_processer.table_conn_id_m, // - s_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, bluetooth_tx_buffer, temp_count_total, temp_count_remainder); - esp_ble_gatts_send_indicate(s_bluetooth_processer.table_gatts_if_m, s_bluetooth_processer.table_conn_id_m, // - s_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(MAIN_TAG, "string_copy_by_num function parameter is empty"); - } - - for (i = 0; i < num; i++) { - dest[i] = src[(count * 15) + i]; - } -} \ No newline at end of file