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.

171 lines
5.3 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. bluetooth_auto_report_format_receipt();
  46. ble_spp_server_demo_app_main(&s_bluetooth_processer);
  47. timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms);
  48. while (true) {
  49. if (s_bluetooth_processer.table_handle_m != 0) {
  50. bluetooth_tx_buffer_send_indicate();
  51. }
  52. bluetooth_gatts_try_process_data();
  53. if (s_bluetooth_processer.auto_report_flag) {
  54. receipt_json_get_status();
  55. s_bluetooth_processer.auto_report_flag = false;
  56. }
  57. }
  58. return;
  59. }
  60. bool validation_param(cJSON *object, char *param) {
  61. cJSON *current_element = object->child;
  62. while (current_element->string != NULL) {
  63. if (current_element->string == param) {
  64. return true;
  65. }
  66. current_element = current_element->next;
  67. }
  68. return false;
  69. }
  70. void receipt_json_set_position() {
  71. cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
  72. if (!pRoot) {
  73. return;
  74. }
  75. cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
  76. cJSON_AddNumberToObject(pRoot, "code", s_bluetooth_processer.code);
  77. cJSON_AddStringToObject(pRoot, "info", "success");
  78. cJSON_AddNumberToObject(pRoot, "index", s_bluetooth_processer.index);
  79. char *szJson = cJSON_Print(pRoot);
  80. if (szJson != NULL) {
  81. ESP_LOGI(MAIN_TAG, "%s", szJson);
  82. free(szJson);
  83. }
  84. cJSON_Delete(pRoot);
  85. }
  86. void receipt_json_get_status() {
  87. cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
  88. if (!pRoot) {
  89. return;
  90. }
  91. cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
  92. cJSON_AddNumberToObject(pRoot, "index", s_bluetooth_processer.index);
  93. cJSON_AddStringToObject(pRoot, "deviceState", s_bluetooth_processer.deviceState);
  94. cJSON_AddNumberToObject(pRoot, "deviceException", s_bluetooth_processer.deviceException);
  95. cJSON_AddStringToObject(pRoot, "deviceExceptionInfo", s_bluetooth_processer.deviceExceptionInfo);
  96. cJSON_AddNumberToObject(pRoot, "position", s_bluetooth_processer.position);
  97. char *szJson = cJSON_Print(pRoot);
  98. if (szJson != NULL) {
  99. ESP_LOGI(MAIN_TAG, "%s", szJson);
  100. free(szJson);
  101. }
  102. cJSON_Delete(pRoot);
  103. }
  104. void bluetooth_auto_report_format_receipt() {
  105. sprintf(bluetooth_tx_buffer, "{ \"order\": \"receipt\", \"index\": %d, \"speedLevel\": %d, \"position\": %.2lf, \"direction\": %d }", //
  106. s_bluetooth_processer.index, s_bluetooth_processer.speedLevel, s_bluetooth_processer.position, s_bluetooth_processer.direction);
  107. }
  108. void bluetooth_tx_buffer_send_indicate() {
  109. char temp_buffer[20] = {0};
  110. uint8_t temp_lenght = 0;
  111. uint8_t temp_count_total = 0;
  112. uint8_t temp_last_count_not_15 = false;
  113. uint8_t temp_count_remainder = 0;
  114. uint8_t i = 0;
  115. temp_lenght = strlen(bluetooth_tx_buffer);
  116. temp_count_total = temp_lenght / 15;
  117. if ((temp_lenght % 15) != 0) {
  118. temp_count_remainder = temp_lenght % 15;
  119. temp_last_count_not_15 = true;
  120. }
  121. for (i = 0; i < temp_count_total; i++) {
  122. string_copy_by_num(temp_buffer, bluetooth_tx_buffer, i, 15);
  123. esp_ble_gatts_send_indicate(s_bluetooth_processer.table_gatts_if_m, s_bluetooth_processer.table_conn_id_m, //
  124. s_bluetooth_processer.table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false);
  125. }
  126. if (temp_last_count_not_15 == true) {
  127. string_copy_by_num(temp_buffer, bluetooth_tx_buffer, temp_count_total, temp_count_remainder);
  128. esp_ble_gatts_send_indicate(s_bluetooth_processer.table_gatts_if_m, s_bluetooth_processer.table_conn_id_m, //
  129. s_bluetooth_processer.table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false);
  130. }
  131. }
  132. void string_copy_by_num(char *dest, const char *src, uint8_t count, uint8_t num) {
  133. int i = 0;
  134. memset(dest, '\0', 15);
  135. if (dest == NULL || src == NULL || num == 0) {
  136. /* code */
  137. ESP_LOGW(MAIN_TAG, "string_copy_by_num function parameter is empty");
  138. }
  139. for (i = 0; i < num; i++) {
  140. dest[i] = src[(count * 15) + i];
  141. }
  142. }