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.

265 lines
9.5 KiB

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