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.

241 lines
8.5 KiB

3 years ago
3 years ago
3 years ago
  1. #include "ble_parse_data.h"
  2. #include "motor_drive.h"
  3. #define BLE_PARSE_DATA_TAG "BLE_PARSE_DATA"
  4. #define cmd_length_set_position 5
  5. #define cmd_length_get_status 2
  6. static bluetooth_processer_t *parse_bluetooth_processer;
  7. uint8_t bluetooth_rx_buffer_len = 0;
  8. void constructor_bluetooth_processer(bluetooth_processer_t *bluetooth_processer) { parse_bluetooth_processer = bluetooth_processer; }
  9. void bluetooth_gatts_try_process_data() {
  10. cJSON *json_tmp;
  11. // cJSON *ch;
  12. //开始接收
  13. if (parse_bluetooth_processer->bluetooth_rx_buffer_start_receving) {
  14. //开启定时器
  15. parse_bluetooth_processer->port_delay_ms(parse_bluetooth_processer->bluetooth_baundrate_one_packet_delay_ms);
  16. // port_timer_delay_ms(kbluetooth_baundrate_one_packet_delay_ms);
  17. parse_bluetooth_processer->bluetooth_rx_buffer_processing = true;
  18. //打印输出
  19. // ESP_LOGI(BLE_PARSE_DATA_TAG, "%s", parse_bluetooth_processer->bluetooth_processer_rx_buf);
  20. //验证解析数据是否正确
  21. if (parse_rxbuffer_and_validation_data(&json_tmp)) {
  22. // JSON解析到结构体,如果order更改表示有指令传输进来,并且更改指令标志位(cmd_flag)为true
  23. if (parse_json_to_struct(json_tmp->child)) {
  24. ESP_LOGI(BLE_PARSE_DATA_TAG, "order:%s ,index:%d speedLevel:%d position:%f direction:%d", parse_bluetooth_processer->order, parse_bluetooth_processer->index,
  25. parse_bluetooth_processer->speedLevel, parse_bluetooth_processer->position, parse_bluetooth_processer->direction);
  26. if (strcmp(parse_bluetooth_processer->order, set_position) == 0) {
  27. parse_bluetooth_processer->auto_report_flag = true;
  28. ESP_LOGI(BLE_PARSE_DATA_TAG, set_position);
  29. // motor_cmd_set_position(parse_bluetooth_processer->speedLevel, parse_bluetooth_processer->position, parse_bluetooth_processer->direction);
  30. receipt_json_set_position();
  31. }
  32. if (strcmp(parse_bluetooth_processer->order, get_status) == 0) {
  33. ESP_LOGI(BLE_PARSE_DATA_TAG, get_status);
  34. receipt_json_get_status();
  35. }
  36. // if (strcmp(parse_bluetooth_processer->order, "deviceStatusReport") == 0)
  37. // {
  38. // ESP_LOGI(BLE_PARSE_DATA_TAG, "deviceStatusReport");
  39. // }
  40. }
  41. }
  42. //释放空间
  43. cJSON_Delete(json_tmp);
  44. // buffer置0
  45. buffer_all_init();
  46. //未在处理数据
  47. parse_bluetooth_processer->cmd_flag = false;
  48. parse_bluetooth_processer->bluetooth_rx_buffer_start_receving = false;
  49. parse_bluetooth_processer->bluetooth_rx_buffer_processing = false;
  50. }
  51. }
  52. void start_receive_data_to_buffer(uint16_t length, uint8_t *value) {
  53. parse_bluetooth_processer->bluetooth_rx_buffer_start_receving = true;
  54. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
  55. //判断是否buffer越界
  56. if ((length + bluetooth_rx_buffer_len) > profile_b_buffer_size) {
  57. return;
  58. }
  59. if (!parse_bluetooth_processer->bluetooth_rx_buffer_processing) {
  60. //写入到buffer
  61. for (int i = 0; i < length; i++) {
  62. parse_bluetooth_processer->bluetooth_processer_rx_buf[bluetooth_rx_buffer_len++] = value[i];
  63. }
  64. }
  65. }
  66. void buffer_all_init() {
  67. bluetooth_rx_buffer_len = 0;
  68. memset(parse_bluetooth_processer->bluetooth_processer_rx_buf, 0, profile_b_buffer_size);
  69. }
  70. bool parse_rxbuffer_and_validation_data(cJSON **json_tmp) {
  71. *json_tmp = cJSON_Parse(parse_bluetooth_processer->bluetooth_processer_rx_buf);
  72. if (*json_tmp == NULL) {
  73. ESP_LOGE(BLE_PARSE_DATA_TAG, "parse rxbuffer null or redundant symbol ',','{' ");
  74. return false;
  75. }
  76. return true;
  77. }
  78. bool parse_json_to_struct(cJSON *ch) {
  79. uint8_t cmd_length = 0;
  80. while (ch != NULL) {
  81. // ESP_LOGI(BLE_PARSE_DATA_TAG, "%s", ch->string);
  82. if (strcmp(ch->string, "order") == 0) {
  83. parse_bluetooth_processer->order = ch->valuestring;
  84. if (strcmp(ch->valuestring, set_position) == 0) {
  85. cmd_length = cmd_length_set_position;
  86. }
  87. if (strcmp(ch->valuestring, get_status) == 0) {
  88. cmd_length = cmd_length_get_status;
  89. }
  90. // if (strcmp(ch->valuestring, "deviceStatusReport") == 0)
  91. // {
  92. // cmd_length = cmd_length_device_status_report;
  93. // }
  94. cmd_length--;
  95. }
  96. if (strcmp(ch->string, "index") == 0) {
  97. parse_bluetooth_processer->index = ch->valueint;
  98. cmd_length--;
  99. }
  100. if (strcmp(ch->string, "speedLevel") == 0) {
  101. parse_bluetooth_processer->speedLevel = ch->valueint;
  102. cmd_length--;
  103. }
  104. if (strcmp(ch->string, "position") == 0) {
  105. parse_bluetooth_processer->position = ch->valuedouble;
  106. cmd_length--;
  107. }
  108. if (strcmp(ch->string, "direction") == 0) {
  109. parse_bluetooth_processer->direction = ch->valueint;
  110. cmd_length--;
  111. }
  112. ch = ch->next;
  113. }
  114. if (cmd_length == 0) {
  115. parse_bluetooth_processer->cmd_flag = true;
  116. } else {
  117. ESP_LOGE(BLE_PARSE_DATA_TAG, "JSON directive missing or exceeded");
  118. }
  119. return parse_bluetooth_processer->cmd_flag;
  120. }
  121. bool validation_param(cJSON *object, char *param) {
  122. cJSON *current_element = object->child;
  123. while (current_element->string != NULL) {
  124. if (current_element->string == param) {
  125. return true;
  126. }
  127. current_element = current_element->next;
  128. }
  129. return false;
  130. }
  131. void receipt_json_set_position() {
  132. cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
  133. if (!pRoot) {
  134. return;
  135. }
  136. cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
  137. cJSON_AddNumberToObject(pRoot, "code", parse_bluetooth_processer->code);
  138. cJSON_AddStringToObject(pRoot, "info", "success");
  139. cJSON_AddNumberToObject(pRoot, "index", parse_bluetooth_processer->index);
  140. char *szJson = cJSON_Print(pRoot);
  141. if (szJson != NULL) {
  142. ESP_LOGI(BLE_PARSE_DATA_TAG, "%s", szJson);
  143. free(szJson);
  144. }
  145. cJSON_Delete(pRoot);
  146. }
  147. void receipt_json_get_status() {
  148. cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
  149. if (!pRoot) {
  150. return;
  151. }
  152. parse_bluetooth_processer->motor_drive_turn_flag = true;
  153. cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
  154. cJSON_AddNumberToObject(pRoot, "index", parse_bluetooth_processer->index);
  155. cJSON_AddStringToObject(pRoot, "deviceState", parse_bluetooth_processer->deviceState);
  156. cJSON_AddNumberToObject(pRoot, "deviceException", parse_bluetooth_processer->deviceException);
  157. cJSON_AddStringToObject(pRoot, "deviceExceptionInfo", parse_bluetooth_processer->deviceExceptionInfo);
  158. cJSON_AddNumberToObject(pRoot, "position", parse_bluetooth_processer->position);
  159. char *szJson = cJSON_Print(pRoot);
  160. if (szJson != NULL) {
  161. ESP_LOGI(BLE_PARSE_DATA_TAG, "%s", szJson);
  162. free(szJson);
  163. }
  164. cJSON_Delete(pRoot);
  165. }
  166. void bluetooth_auto_report_format_receipt() {
  167. sprintf(parse_bluetooth_processer->bluetooth_processer_tx_buf, "{ \"order\": \"receipt\", \"index\": %d, \"speedLevel\": %d, \"position\": %.2lf, \"direction\": %d }", //
  168. parse_bluetooth_processer->index, parse_bluetooth_processer->speedLevel, parse_bluetooth_processer->position, parse_bluetooth_processer->direction);
  169. }
  170. void bluetooth_tx_buffer_send_indicate(cb_t format) {
  171. char temp_buffer[20] = {0};
  172. uint8_t temp_lenght = 0;
  173. uint8_t temp_count_total = 0;
  174. uint8_t temp_last_count_not_15 = false;
  175. uint8_t temp_count_remainder = 0;
  176. uint8_t i = 0;
  177. format();
  178. temp_lenght = strlen(parse_bluetooth_processer->bluetooth_processer_tx_buf);
  179. temp_count_total = temp_lenght / 15;
  180. if ((temp_lenght % 15) != 0) {
  181. temp_count_remainder = temp_lenght % 15;
  182. temp_last_count_not_15 = true;
  183. }
  184. for (i = 0; i < temp_count_total; i++) {
  185. string_copy_by_num(temp_buffer, parse_bluetooth_processer->bluetooth_processer_tx_buf, i, 15);
  186. esp_ble_gatts_send_indicate(parse_bluetooth_processer->table_gatts_if_m, parse_bluetooth_processer->table_conn_id_m, //
  187. parse_bluetooth_processer->table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false);
  188. }
  189. if (temp_last_count_not_15 == true) {
  190. string_copy_by_num(temp_buffer, parse_bluetooth_processer->bluetooth_processer_tx_buf, temp_count_total, temp_count_remainder);
  191. esp_ble_gatts_send_indicate(parse_bluetooth_processer->table_gatts_if_m, parse_bluetooth_processer->table_conn_id_m, //
  192. parse_bluetooth_processer->table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false);
  193. }
  194. }
  195. void string_copy_by_num(char *dest, const char *src, uint8_t count, uint8_t num) {
  196. int i = 0;
  197. memset(dest, '\0', 15);
  198. if (dest == NULL || src == NULL || num == 0) {
  199. /* code */
  200. ESP_LOGW(BLE_PARSE_DATA_TAG, "string_copy_by_num function parameter is empty");
  201. }
  202. for (i = 0; i < num; i++) {
  203. dest[i] = src[(count * 15) + i];
  204. }
  205. }