From b564f391e12dd6a0ed7856c4f110f9fe870b7e19 Mon Sep 17 00:00:00 2001 From: zwsd Date: Thu, 21 Jul 2022 17:10:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=BA=86=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E6=96=87=E4=BB=B6log=20tag=EF=BC=8C=E5=B0=86=E4=B8=BB=E5=8A=A8?= =?UTF-8?q?=E4=B8=8A=E6=8A=A5=E5=8F=82=E6=95=B0=E5=8A=A0=E5=85=A5=E8=93=9D?= =?UTF-8?q?=E7=89=99=E7=BB=93=E6=9E=84=E4=BD=93=E4=B8=AD=EF=BC=8C=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E4=B8=80=E4=BA=9B=E6=B3=A8=E9=87=8A=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/ble_parse_data.c | 18 +++++++------- main/ble_parse_data.h | 6 +++++ main/ble_spp_server_demo.c | 26 +++++++------------- main/ble_spp_server_demo.h | 3 +-- main/main.c | 60 ++++++++++++++-------------------------------- 5 files changed, 43 insertions(+), 70 deletions(-) diff --git a/main/ble_parse_data.c b/main/ble_parse_data.c index e7cd666..0e45ef1 100644 --- a/main/ble_parse_data.c +++ b/main/ble_parse_data.c @@ -1,6 +1,6 @@ #include "ble_parse_data.h" -#define GATTS_TABLE_TAG "GATTS_SPP_DEMO" +#define BLE_PARSE_DATA_TAG "BLE_PARSE_DATA" #define cmd_length_set_position 5 #define cmd_length_get_status 2 @@ -23,27 +23,27 @@ void bluetooth_gatts_try_process_data() { parse_bluetooth_processer->bluetooth_rx_buffer_processing = true; //打印输出 - // ESP_LOGI(GATTS_TABLE_TAG, "%s", parse_bluetooth_processer->bluetooth_processer_rx_buf); + // 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(GATTS_TABLE_TAG, "order:%s ,index:%d speedLevel:%d position:%f direction:%d", parse_bluetooth_processer->order, parse_bluetooth_processer->index, + 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) { - ESP_LOGI(GATTS_TABLE_TAG, set_position); + ESP_LOGI(BLE_PARSE_DATA_TAG, set_position); // 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(GATTS_TABLE_TAG, get_status); + ESP_LOGI(BLE_PARSE_DATA_TAG, get_status); test_flag = true; // receipt_json_get_status(); } // if (strcmp(parse_bluetooth_processer->order, "deviceStatusReport") == 0) // { - // ESP_LOGI(GATTS_TABLE_TAG, "deviceStatusReport"); + // ESP_LOGI(BLE_PARSE_DATA_TAG, "deviceStatusReport"); // } parse_bluetooth_processer->actively_report_flag = true; } @@ -84,7 +84,7 @@ void buffer_all_init() { 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(GATTS_TABLE_TAG, "parse rxbuffer null or redundant symbol ',','{' "); + ESP_LOGE(BLE_PARSE_DATA_TAG, "parse rxbuffer null or redundant symbol ',','{' "); return false; } return true; @@ -92,7 +92,7 @@ bool parse_rxbuffer_and_validation_data(cJSON **json_tmp) { bool parse_json_to_struct(cJSON *ch) { uint8_t cmd_length = 0; while (ch != NULL) { - // ESP_LOGI(GATTS_TABLE_TAG, "%s", ch->string); + // ESP_LOGI(BLE_PARSE_DATA_TAG, "%s", ch->string); if (strcmp(ch->string, "order") == 0) { parse_bluetooth_processer->order = ch->valuestring; @@ -130,7 +130,7 @@ bool parse_json_to_struct(cJSON *ch) { if (cmd_length == 0) { parse_bluetooth_processer->cmd_flag = true; } else { - ESP_LOGE(GATTS_TABLE_TAG, "JSON directive missing or exceeded"); + ESP_LOGE(BLE_PARSE_DATA_TAG, "JSON directive missing or exceeded"); } return parse_bluetooth_processer->cmd_flag; diff --git a/main/ble_parse_data.h b/main/ble_parse_data.h index fcf10ef..585687c 100644 --- a/main/ble_parse_data.h +++ b/main/ble_parse_data.h @@ -5,7 +5,9 @@ #include "cJSON.h" #include "cJSON_Utils.h" #include "driver/timer.h" +#include "esp_gatts_api.h" #include "esp_log.h" +#include "esp_system.h" #define profile_b_buffer_size 128 @@ -33,6 +35,10 @@ typedef struct bluetooth_processer { int deviceException; //设备异常编号 char *deviceExceptionInfo; //设备异常信息 + uint16_t table_conn_id_m; + esp_gatt_if_t table_gatts_if_m; + uint16_t table_handle_m; + bool cmd_flag; bool actively_report_flag; diff --git a/main/ble_spp_server_demo.c b/main/ble_spp_server_demo.c index e723a1e..84191cd 100644 --- a/main/ble_spp_server_demo.c +++ b/main/ble_spp_server_demo.c @@ -8,11 +8,7 @@ #include "ble_spp_server_demo.h" -#define GATTS_TABLE_TAG "GATTS_SPP_DEMO" - -static uint16_t *hid_conn_id; -static esp_gatt_if_t *hid_gatts_if; -static short unsigned int *hid_handle; +#define GATTS_TABLE_TAG "GATTS_SPP_SERVER" static bluetooth_processer_t *g_bluetooth_processer; @@ -565,9 +561,9 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_ case ESP_GATTS_STOP_EVT: break; case ESP_GATTS_CONNECT_EVT: - *hid_conn_id = param->connect.conn_id; - *hid_handle = spp_handle_table[SPP_IDX_SPP_DATA_NTY_VAL]; - *hid_gatts_if = gatts_if; + g_bluetooth_processer->table_conn_id_m = param->connect.conn_id; + g_bluetooth_processer->table_gatts_if_m = gatts_if; + g_bluetooth_processer->table_handle_m = spp_handle_table[SPP_IDX_SPP_DATA_NTY_VAL]; spp_conn_id = p_data->connect.conn_id; spp_gatts_if = gatts_if; is_connected = true; @@ -578,9 +574,9 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_ #endif break; case ESP_GATTS_DISCONNECT_EVT: - *hid_gatts_if = 0; - *hid_conn_id = 0; - *hid_handle = 0; + g_bluetooth_processer->table_conn_id_m = 0; + g_bluetooth_processer->table_gatts_if_m = 0; + g_bluetooth_processer->table_handle_m = 0; is_connected = false; enable_data_ntf = false; #ifdef SUPPORT_HEARTBEAT @@ -642,14 +638,10 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_ } while (0); } -void ble_spp_server_demo_app_main(uint16_t *conn_id_ble, esp_gatt_if_t *gatts_if_ble, uint16_t *handle_ble, bluetooth_processer_t *bluetooth_processer) { +void ble_spp_server_demo_app_main(bluetooth_processer_t *bluetooth_processer) { esp_err_t ret; esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); - hid_conn_id = conn_id_ble; - hid_gatts_if = gatts_if_ble; - hid_handle = handle_ble; - g_bluetooth_processer = bluetooth_processer; // Initialize NVS @@ -690,7 +682,7 @@ void ble_spp_server_demo_app_main(uint16_t *conn_id_ble, esp_gatt_if_t *gatts_if esp_ble_gap_register_callback(gap_event_handler); esp_ble_gatts_app_register(ESP_SPP_APP_ID); -// spp_task_init(); + // spp_task_init(); return; } diff --git a/main/ble_spp_server_demo.h b/main/ble_spp_server_demo.h index db5e2b1..7eeea64 100644 --- a/main/ble_spp_server_demo.h +++ b/main/ble_spp_server_demo.h @@ -62,5 +62,4 @@ enum { SPP_IDX_NB, }; - -void ble_spp_server_demo_app_main(uint16_t *conn_id_ble, esp_gatt_if_t *gatts_if_ble, uint16_t *handle_ble, bluetooth_processer_t *bluetooth_processer); \ No newline at end of file +void ble_spp_server_demo_app_main(bluetooth_processer_t *bluetooth_processer); \ No newline at end of file diff --git a/main/main.c b/main/main.c index 4e444ff..a57ad76 100644 --- a/main/main.c +++ b/main/main.c @@ -6,27 +6,16 @@ #include "freertos/task.h" #include "nvs_flash.h" #include "string.h" - // #include "ble_spp_server_demo.h" #include "timer_u.h" -#define GATTS_TABLE_TAG "GATTS_SPP_DEMO" - -uint16_t table_conn_id_m; -esp_gatt_if_t table_gatts_if_m; -uint16_t table_handle_m; +#define MAIN_TAG "MAIN" static char bluetooth_tx_buffer[profile_b_buffer_size] = {0}; char bluetooth_rx_buffer[profile_b_buffer_size] = {0}; -// ble_gatts_str_t ble_gatts_a_str = { -// .attr_handle = 0, -// .send_conn_id = 0, -// .send_gatts_if = 0, -// }; - void receipt_json_set_position(); void receipt_json_get_status(); void bluetooth_rx_buffer_format_receipt(); @@ -53,27 +42,24 @@ bluetooth_processer_t s_bluetooth_processer = { .deviceException = 0, .deviceExceptionInfo = "noexception", + .table_conn_id_m = 0, + .table_gatts_if_m = 0, + .table_handle_m = 0, + .cmd_flag = false, .actively_report_flag = false, }; void app_main(void) { constructor_bluetooth_processer(&s_bluetooth_processer); - // ble_spp_server_demo_app_main(&table_conn_id_m, &table_gatts_if_m, &table_handle_m); - // table_conn_id_m = - // char temp_buffer[5] = {'1', '2', '3', '4', '5'}; - - // while (true) { - // if (table_handle_m != 0) { - // ets_delay_us(1000000); - // esp_ble_gatts_send_indicate(table_gatts_if_m, table_conn_id_m, table_handle_m, 5, (uint8_t *)temp_buffer, false); - // } - // } bluetooth_rx_buffer_format_receipt(); - ble_spp_server_demo_app_main(&table_conn_id_m, &table_gatts_if_m, &table_handle_m, &s_bluetooth_processer); + ble_spp_server_demo_app_main(&s_bluetooth_processer); timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms); while (true) { + if (s_bluetooth_processer.table_handle_m != 0) { + bluetooth_rx_buffer_send_indicate(); + } bluetooth_gatts_try_process_data(); if (s_bluetooth_processer.actively_report_flag) { receipt_json_get_status(); @@ -109,19 +95,7 @@ void receipt_json_set_position() { char *szJson = cJSON_Print(pRoot); if (szJson != NULL) { - ESP_LOGI(GATTS_TABLE_TAG, "%s", szJson); - - // esp_gatt_rsp_t rsp; - // memset(&rsp, 0, sizeof(esp_gatt_rsp_t)); - // rsp.attr_value.handle = constructor_param->read.handle; - // rsp.attr_value.len = 4; - // rsp.attr_value.value[0] = 0x11; - // rsp.attr_value.value[1] = 0x22; - // rsp.attr_value.value[2] = 0x33; - // rsp.attr_value.value[3] = 0x44; - - // esp_ble_gatts_send_response(constructor_gatts_if, constructor_param->read.conn_id, - // constructor_param->read.trans_id, ESP_GATT_OK, &rsp); + ESP_LOGI(MAIN_TAG, "%s", szJson); free(szJson); } @@ -145,7 +119,7 @@ void receipt_json_get_status() { char *szJson = cJSON_Print(pRoot); if (szJson != NULL) { - ESP_LOGI(GATTS_TABLE_TAG, "%s", szJson); + ESP_LOGI(MAIN_TAG, "%s", szJson); free(szJson); } @@ -153,8 +127,8 @@ void receipt_json_get_status() { } void bluetooth_rx_buffer_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); + 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_rx_buffer_send_indicate() { @@ -175,11 +149,13 @@ void bluetooth_rx_buffer_send_indicate() { for (i = 0; i < temp_count_total; i++) { string_copy_by_num(temp_buffer, bluetooth_tx_buffer, i, 15); - esp_ble_gatts_send_indicate(table_gatts_if_m, table_conn_id_m, table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false); + 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(table_gatts_if_m, table_conn_id_m, table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false); + 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); } } @@ -188,7 +164,7 @@ void string_copy_by_num(char *dest, const char *src, uint8_t count, uint8_t num) memset(dest, '\0', 15); if (dest == NULL || src == NULL || num == 0) { /* code */ - ESP_LOGW(GATTS_TABLE_TAG, "string_copy_by_num function parameter is empty"); + ESP_LOGW(MAIN_TAG, "string_copy_by_num function parameter is empty"); } for (i = 0; i < num; i++) {