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.

169 lines
5.2 KiB

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 MAIN_TAG "MAIN"
  13. static char bluetooth_tx_buffer[profile_b_buffer_size] = {0};
  14. static char bluetooth_rx_buffer[profile_b_buffer_size] = {0};
  15. void receipt_json_set_position();
  16. void receipt_json_get_status();
  17. void bluetooth_auto_report_format_receipt();
  18. void bluetooth_tx_buffer_send_indicate();
  19. void string_copy_by_num(char *dest, const char *src, uint8_t count, uint8_t num);
  20. bluetooth_processer_t s_bluetooth_processer = {
  21. .bluetooth_processer_rx_buf = bluetooth_rx_buffer,
  22. .bluetooth_processer_rx_buf_size = sizeof(bluetooth_rx_buffer),
  23. .bluetooth_baundrate_one_packet_delay_ms = kbluetooth_baundrate_one_packet_delay_ms,
  24. .port_delay_ms = port_timer_delay_ms,
  25. .bluetooth_rx_buffer_start_receving = false,
  26. .bluetooth_rx_buffer_processing = false,
  27. .order = "order",
  28. .index = 0,
  29. .speedLevel = 0,
  30. .position = 0.0,
  31. .direction = 0,
  32. .code = 0,
  33. .info = "noerror",
  34. .deviceState = "init",
  35. .deviceException = 0,
  36. .deviceExceptionInfo = "noexception",
  37. .table_conn_id_m = 0,
  38. .table_gatts_if_m = 0,
  39. .table_handle_m = 0,
  40. .cmd_flag = false,
  41. .auto_report_flag = false,
  42. };
  43. void app_main(void) {
  44. constructor_bluetooth_processer(&s_bluetooth_processer);
  45. ble_spp_server_demo_app_main(&s_bluetooth_processer);
  46. timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms);
  47. while (true) {
  48. bluetooth_gatts_try_process_data();
  49. if (s_bluetooth_processer.auto_report_flag) {
  50. receipt_json_get_status();
  51. s_bluetooth_processer.auto_report_flag = false;
  52. }
  53. }
  54. return;
  55. }
  56. bool validation_param(cJSON *object, char *param) {
  57. cJSON *current_element = object->child;
  58. while (current_element->string != NULL) {
  59. if (current_element->string == param) {
  60. return true;
  61. }
  62. current_element = current_element->next;
  63. }
  64. return false;
  65. }
  66. void receipt_json_set_position() {
  67. cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
  68. if (!pRoot) {
  69. return;
  70. }
  71. cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
  72. cJSON_AddNumberToObject(pRoot, "code", s_bluetooth_processer.code);
  73. cJSON_AddStringToObject(pRoot, "info", "success");
  74. cJSON_AddNumberToObject(pRoot, "index", s_bluetooth_processer.index);
  75. char *szJson = cJSON_Print(pRoot);
  76. if (szJson != NULL) {
  77. ESP_LOGI(MAIN_TAG, "%s", szJson);
  78. free(szJson);
  79. }
  80. cJSON_Delete(pRoot);
  81. }
  82. void receipt_json_get_status() {
  83. cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
  84. if (!pRoot) {
  85. return;
  86. }
  87. cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
  88. cJSON_AddNumberToObject(pRoot, "index", s_bluetooth_processer.index);
  89. cJSON_AddStringToObject(pRoot, "deviceState", s_bluetooth_processer.deviceState);
  90. cJSON_AddNumberToObject(pRoot, "deviceException", s_bluetooth_processer.deviceException);
  91. cJSON_AddStringToObject(pRoot, "deviceExceptionInfo", s_bluetooth_processer.deviceExceptionInfo);
  92. cJSON_AddNumberToObject(pRoot, "position", s_bluetooth_processer.position);
  93. char *szJson = cJSON_Print(pRoot);
  94. if (szJson != NULL) {
  95. ESP_LOGI(MAIN_TAG, "%s", szJson);
  96. free(szJson);
  97. }
  98. cJSON_Delete(pRoot);
  99. }
  100. void bluetooth_auto_report_format_receipt() {
  101. sprintf(bluetooth_tx_buffer, "{ \"order\": \"receipt\", \"index\": %d, \"speedLevel\": %d, \"position\": %.2lf, \"direction\": %d }", //
  102. s_bluetooth_processer.index, s_bluetooth_processer.speedLevel, s_bluetooth_processer.position, s_bluetooth_processer.direction);
  103. }
  104. void bluetooth_tx_buffer_send_indicate() {
  105. char temp_buffer[20] = {0};
  106. uint8_t temp_lenght = 0;
  107. uint8_t temp_count_total = 0;
  108. uint8_t temp_last_count_not_15 = false;
  109. uint8_t temp_count_remainder = 0;
  110. uint8_t i = 0;
  111. bluetooth_auto_report_format_receipt();
  112. temp_lenght = strlen(bluetooth_tx_buffer);
  113. temp_count_total = temp_lenght / 15;
  114. if ((temp_lenght % 15) != 0) {
  115. temp_count_remainder = temp_lenght % 15;
  116. temp_last_count_not_15 = true;
  117. }
  118. for (i = 0; i < temp_count_total; i++) {
  119. string_copy_by_num(temp_buffer, bluetooth_tx_buffer, i, 15);
  120. esp_ble_gatts_send_indicate(s_bluetooth_processer.table_gatts_if_m, s_bluetooth_processer.table_conn_id_m, //
  121. s_bluetooth_processer.table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false);
  122. }
  123. if (temp_last_count_not_15 == true) {
  124. string_copy_by_num(temp_buffer, bluetooth_tx_buffer, temp_count_total, temp_count_remainder);
  125. esp_ble_gatts_send_indicate(s_bluetooth_processer.table_gatts_if_m, s_bluetooth_processer.table_conn_id_m, //
  126. s_bluetooth_processer.table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false);
  127. }
  128. }
  129. void string_copy_by_num(char *dest, const char *src, uint8_t count, uint8_t num) {
  130. int i = 0;
  131. memset(dest, '\0', 15);
  132. if (dest == NULL || src == NULL || num == 0) {
  133. /* code */
  134. ESP_LOGW(MAIN_TAG, "string_copy_by_num function parameter is empty");
  135. }
  136. for (i = 0; i < num; i++) {
  137. dest[i] = src[(count * 15) + i];
  138. }
  139. }