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.

242 lines
8.6 KiB

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