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.

263 lines
9.4 KiB

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