You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
6.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include "esp_gatts_api.h"
  2. #include "esp_log.h"
  3. #include "esp_system.h"
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/event_groups.h"
  6. #include "freertos/task.h"
  7. #include "nvs_flash.h"
  8. #include "string.h"
  9. //
  10. #include "ble_spp_server_demo.h"
  11. #include "timer_u.h"
  12. #define GATTS_TABLE_TAG "GATTS_SPP_DEMO"
  13. uint16_t table_conn_id_m;
  14. esp_gatt_if_t table_gatts_if_m;
  15. uint16_t table_handle_m;
  16. static char bluetooth_tx_buffer[profile_b_buffer_size] = {0};
  17. char bluetooth_rx_buffer[profile_b_buffer_size] = {0};
  18. // ble_gatts_str_t ble_gatts_a_str = {
  19. // .attr_handle = 0,
  20. // .send_conn_id = 0,
  21. // .send_gatts_if = 0,
  22. // };
  23. void receipt_json_set_position();
  24. void receipt_json_get_status();
  25. void bluetooth_rx_buffer_format_receipt();
  26. void bluetooth_rx_buffer_send_indicate();
  27. void string_copy_by_num(char *dest, const char *src, uint8_t count, uint8_t num);
  28. bluetooth_processer_t s_bluetooth_processer = {
  29. .bluetooth_processer_rx_buf = bluetooth_rx_buffer,
  30. .bluetooth_processer_rx_buf_size = sizeof(bluetooth_rx_buffer),
  31. .bluetooth_baundrate_one_packet_delay_ms = kbluetooth_baundrate_one_packet_delay_ms,
  32. .port_delay_ms = port_timer_delay_ms,
  33. .bluetooth_rx_buffer_start_receving = false,
  34. .bluetooth_rx_buffer_processing = false,
  35. .order = "order",
  36. .index = 0,
  37. .speedLevel = 0,
  38. .position = 0.0,
  39. .direction = 0,
  40. .code = 0,
  41. .info = "noerror",
  42. .deviceState = "init",
  43. .deviceException = 0,
  44. .deviceExceptionInfo = "noexception",
  45. .cmd_flag = false,
  46. .actively_report_flag = false,
  47. };
  48. void app_main(void) {
  49. constructor_bluetooth_processer(&s_bluetooth_processer);
  50. // ble_spp_server_demo_app_main(&table_conn_id_m, &table_gatts_if_m, &table_handle_m);
  51. // table_conn_id_m =
  52. // char temp_buffer[5] = {'1', '2', '3', '4', '5'};
  53. // while (true) {
  54. // if (table_handle_m != 0) {
  55. // ets_delay_us(1000000);
  56. // esp_ble_gatts_send_indicate(table_gatts_if_m, table_conn_id_m, table_handle_m, 5, (uint8_t *)temp_buffer, false);
  57. // }
  58. // }
  59. bluetooth_rx_buffer_format_receipt();
  60. ble_spp_server_demo_app_main(&table_conn_id_m, &table_gatts_if_m, &table_handle_m, &s_bluetooth_processer);
  61. timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms);
  62. while (true) {
  63. bluetooth_gatts_try_process_data();
  64. if (s_bluetooth_processer.actively_report_flag) {
  65. receipt_json_get_status();
  66. s_bluetooth_processer.actively_report_flag = false;
  67. }
  68. }
  69. return;
  70. }
  71. bool validation_param(cJSON *object, char *param) {
  72. cJSON *current_element = object->child;
  73. while (current_element->string != NULL) {
  74. if (current_element->string == param) {
  75. return true;
  76. }
  77. current_element = current_element->next;
  78. }
  79. return false;
  80. }
  81. void receipt_json_set_position() {
  82. cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
  83. if (!pRoot) {
  84. return;
  85. }
  86. cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
  87. cJSON_AddNumberToObject(pRoot, "code", s_bluetooth_processer.code);
  88. cJSON_AddStringToObject(pRoot, "info", "success");
  89. cJSON_AddNumberToObject(pRoot, "index", s_bluetooth_processer.index);
  90. char *szJson = cJSON_Print(pRoot);
  91. if (szJson != NULL) {
  92. ESP_LOGI(GATTS_TABLE_TAG, "%s", szJson);
  93. // esp_gatt_rsp_t rsp;
  94. // memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
  95. // rsp.attr_value.handle = constructor_param->read.handle;
  96. // rsp.attr_value.len = 4;
  97. // rsp.attr_value.value[0] = 0x11;
  98. // rsp.attr_value.value[1] = 0x22;
  99. // rsp.attr_value.value[2] = 0x33;
  100. // rsp.attr_value.value[3] = 0x44;
  101. // esp_ble_gatts_send_response(constructor_gatts_if, constructor_param->read.conn_id,
  102. // constructor_param->read.trans_id, ESP_GATT_OK, &rsp);
  103. free(szJson);
  104. }
  105. cJSON_Delete(pRoot);
  106. }
  107. void receipt_json_get_status() {
  108. cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
  109. if (!pRoot) {
  110. return;
  111. }
  112. cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
  113. cJSON_AddNumberToObject(pRoot, "index", s_bluetooth_processer.index);
  114. cJSON_AddStringToObject(pRoot, "deviceState", s_bluetooth_processer.deviceState);
  115. cJSON_AddNumberToObject(pRoot, "deviceException", s_bluetooth_processer.deviceException);
  116. cJSON_AddStringToObject(pRoot, "deviceExceptionInfo", s_bluetooth_processer.deviceExceptionInfo);
  117. cJSON_AddNumberToObject(pRoot, "position", s_bluetooth_processer.position);
  118. char *szJson = cJSON_Print(pRoot);
  119. if (szJson != NULL) {
  120. ESP_LOGI(GATTS_TABLE_TAG, "%s", szJson);
  121. free(szJson);
  122. }
  123. cJSON_Delete(pRoot);
  124. }
  125. void bluetooth_rx_buffer_format_receipt() {
  126. sprintf(bluetooth_tx_buffer, "{ \"order\": \"receipt\", \"index\": %d, \"speedLevel\": %d, \"position\": %.2lf, \"direction\": %d }", s_bluetooth_processer.index, s_bluetooth_processer.speedLevel,
  127. s_bluetooth_processer.position, s_bluetooth_processer.direction);
  128. }
  129. void bluetooth_rx_buffer_send_indicate() {
  130. char temp_buffer[20] = {0};
  131. uint8_t temp_lenght = 0;
  132. uint8_t temp_count_total = 0;
  133. uint8_t temp_last_count_not_15 = false;
  134. uint8_t temp_count_remainder = 0;
  135. uint8_t i = 0;
  136. temp_lenght = strlen(bluetooth_tx_buffer);
  137. temp_count_total = temp_lenght / 15;
  138. if ((temp_lenght % 15) != 0) {
  139. temp_count_remainder = temp_lenght % 15;
  140. temp_last_count_not_15 = true;
  141. }
  142. for (i = 0; i < temp_count_total; i++) {
  143. string_copy_by_num(temp_buffer, bluetooth_tx_buffer, i, 15);
  144. esp_ble_gatts_send_indicate(table_gatts_if_m, table_conn_id_m, table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false);
  145. }
  146. if (temp_last_count_not_15 == true) {
  147. string_copy_by_num(temp_buffer, bluetooth_tx_buffer, temp_count_total, temp_count_remainder);
  148. esp_ble_gatts_send_indicate(table_gatts_if_m, table_conn_id_m, table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false);
  149. }
  150. }
  151. void string_copy_by_num(char *dest, const char *src, uint8_t count, uint8_t num) {
  152. int i = 0;
  153. memset(dest, '\0', 15);
  154. if (dest == NULL || src == NULL || num == 0) {
  155. /* code */
  156. ESP_LOGW(GATTS_TABLE_TAG, "string_copy_by_num function parameter is empty");
  157. }
  158. for (i = 0; i < num; i++) {
  159. dest[i] = src[(count * 15) + i];
  160. }
  161. }