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.

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