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.

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