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.

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