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.

376 lines
14 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. /*
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. /****************************************************************************
  8. *
  9. * This demo showcases BLE GATT server. It can send adv data, be connected by client.
  10. * Run the gatt_client demo, the client demo will automatically connect to the gatt_server demo.
  11. * Client demo will enable gatt_server's notify after connection. The two devices will then exchange
  12. * data.
  13. *
  14. ****************************************************************************/
  15. #include "motor_drive.h"
  16. #include "t_bluetooth.h"
  17. /***********************************************************************************************************************
  18. * ****************************************************user_define**************************************************** *
  19. ***********************************************************************************************************************/
  20. char bluetooth_rx_buffer[profile_b_buffer_size] = {0};
  21. ble_gatts_str_t ble_gatts_a_str = {
  22. .attr_handle = 0,
  23. .send_conn_id = 0,
  24. .send_gatts_if = 0,
  25. };
  26. /***********************************************************************************************************************
  27. * **********************************************user_function_statement********************************************** *
  28. ***********************************************************************************************************************/
  29. void buffer_all_init();
  30. void bluetooth_gatts_try_process_data();
  31. void timer_set_pause_and_counter_zero(int group, int timer);
  32. void port_timer_delay_ms(uint64_t delay);
  33. bool parse_rxbuffer_and_validation_data(cJSON **json_tmp);
  34. bool parse_json_to_struct(cJSON *ch);
  35. void receipt_json_set_position();
  36. void receipt_json_get_status();
  37. void gatts_profile_a_constructor(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
  38. bluetooth_processer_t s_bluetooth_processer = {
  39. .bluetooth_processer_rx_buf = bluetooth_rx_buffer,
  40. .bluetooth_processer_rx_buf_size = sizeof(bluetooth_rx_buffer),
  41. .bluetooth_baundrate_one_packet_delay_ms = kbluetooth_baundrate_one_packet_delay_ms,
  42. .port_delay_ms = port_timer_delay_ms,
  43. .bluetooth_rx_buffer_start_receving = false,
  44. .bluetooth_rx_buffer_processing = false,
  45. .order = "order",
  46. .index = 0,
  47. .speed_level = 0,
  48. .position = 0.0,
  49. .direction = 0,
  50. .code = 0,
  51. .info = "noerror",
  52. .deviceState = "init",
  53. .deviceException = 0,
  54. .deviceExceptionInfo = "noexception",
  55. .cmd_flag = false,
  56. .actively_report_flag = false,
  57. };
  58. /***********************************************************************************************************************
  59. * *******************************************************timer******************************************************* *
  60. ***********************************************************************************************************************/
  61. // static bool IRAM_ATTR timer_group_isr_callback(void *args)
  62. // {
  63. // bluetooth_rx_buffer_start_receving = true;
  64. // return pdFALSE;
  65. // }
  66. void timer_group_init(int group, int timer, bool auto_reload, int timer_interval, uint32_t timer_unit_time)
  67. {
  68. /* Select and initialize basic parameters of the timer */
  69. timer_config_t config = {
  70. .divider = 80,
  71. .counter_dir = TIMER_COUNT_UP,
  72. .counter_en = TIMER_PAUSE,
  73. .alarm_en = TIMER_ALARM_EN,
  74. .auto_reload = auto_reload,
  75. }; // default clock source is APB
  76. timer_init(group, timer, &config);
  77. /* Timer's counter will initially start from value below.
  78. Also, if auto_reload is set, this value will be automatically reload on alarm */
  79. timer_set_counter_value(group, timer, 0); //指定定时器首个计数值为0
  80. //检查定时器的当前值,调用函数 timer_get_counter_value() 或 timer_get_counter_time_sec()。
  81. //可通过调用函数 timer_pause() 随时暂停定时器。要再次启动它,调用函数 timer_start()。
  82. /* Configure the alarm value and the interrupt on alarm. */
  83. timer_set_alarm_value(group, timer, timer_interval * ((80 * timer_unit_time) / 80));
  84. timer_enable_intr(group, timer);
  85. // timer_isr_callback_add(group, timer, timer_group_isr_callback, NULL, 0);
  86. // timer_start(group, timer);
  87. timer_pause(group, timer);
  88. }
  89. /***********************************************************************************************************************
  90. * *******************************************************main******************************************************** *
  91. ***********************************************************************************************************************/
  92. void app_main(void)
  93. {
  94. esp_ble_gatts_init(&ble_gatts_a_str, &s_bluetooth_processer);
  95. // motor_init();
  96. // motor_encoder_init();
  97. timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms);
  98. // uint8_t temp[5] = "hello";
  99. // esp_gatt_rsp_t rsp;
  100. // memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
  101. // rsp.attr_value.len = 4;
  102. // rsp.attr_value.value[0] = 0x11;
  103. // rsp.attr_value.value[1] = 0x22;
  104. // rsp.attr_value.value[2] = 0x33;
  105. // rsp.attr_value.value[3] = 0x44;
  106. while (true)
  107. {
  108. // if (ble_gatts_a_str.attr_handle != 0)
  109. // {
  110. // rsp.attr_value.handle = ble_gatts_a_str.attr_handle;
  111. // ESP_LOGI(GATTS_TAG, "%d,%d,%d", ble_gatts_a_str.attr_handle, ble_gatts_a_str.send_conn_id, ble_gatts_a_str.send_gatts_if);
  112. // esp_ble_gatts_send_indicate(ble_gatts_a_str.send_gatts_if, ble_gatts_a_str.send_conn_id,
  113. // ble_gatts_a_str.attr_handle, 5, temp, true);
  114. // esp_ble_gatts_send_response(ble_gatts_a_str.send_gatts_if, ble_gatts_a_str.send_conn_id, 1,
  115. // ESP_GATT_OK, &rsp);
  116. // ets_delay_us(1000000);
  117. // }
  118. // esp_ble_gatts_set_attr_value(ble_gatts_a_str.constructor_param->read.handle, 5, temp);
  119. bluetooth_gatts_try_process_data();
  120. if (s_bluetooth_processer.actively_report_flag)
  121. {
  122. receipt_json_get_status();
  123. s_bluetooth_processer.actively_report_flag = false;
  124. }
  125. }
  126. return;
  127. }
  128. /***********************************************************************************************************************
  129. * *************************************************user_funtion_def************************************************** *
  130. ***********************************************************************************************************************/
  131. void timer_set_pause_and_counter_zero(int group, int timer)
  132. {
  133. timer_pause(group, timer);
  134. timer_set_counter_value(group, timer, 0);
  135. }
  136. bool validation_param(cJSON *object, char *param)
  137. {
  138. cJSON *current_element = object->child;
  139. while (current_element->string != NULL)
  140. {
  141. if (current_element->string == param)
  142. {
  143. return true;
  144. }
  145. current_element = current_element->next;
  146. }
  147. return false;
  148. }
  149. void bluetooth_gatts_try_process_data()
  150. {
  151. cJSON *json_tmp;
  152. // cJSON *ch;
  153. //开始接收
  154. if (s_bluetooth_processer.bluetooth_rx_buffer_start_receving)
  155. {
  156. //开启定时器
  157. s_bluetooth_processer.port_delay_ms(s_bluetooth_processer.bluetooth_baundrate_one_packet_delay_ms);
  158. // port_timer_delay_ms(kbluetooth_baundrate_one_packet_delay_ms);
  159. s_bluetooth_processer.bluetooth_rx_buffer_processing = true;
  160. //打印输出
  161. // ESP_LOGI(GATTS_TAG, "%s", s_bluetooth_processer.bluetooth_processer_rx_buf);
  162. //验证解析数据是否正确
  163. if (parse_rxbuffer_and_validation_data(&json_tmp))
  164. {
  165. // JSON解析到结构体,如果order更改表示有指令传输进来,并且更改指令标志位(cmd_flag)为true
  166. if (parse_json_to_struct(json_tmp->child))
  167. {
  168. ESP_LOGI(GATTS_TAG, "order:%s ,index:%d speedLevel:%d position:%f direction:%d",
  169. s_bluetooth_processer.order, s_bluetooth_processer.index, s_bluetooth_processer.speed_level,
  170. s_bluetooth_processer.position, s_bluetooth_processer.direction);
  171. if (strcmp(s_bluetooth_processer.order, set_position) == 0)
  172. {
  173. ESP_LOGI(GATTS_TAG, set_position);
  174. motor_cmd_set_position(s_bluetooth_processer.speed_level, s_bluetooth_processer.position, s_bluetooth_processer.direction);
  175. receipt_json_set_position();
  176. }
  177. if (strcmp(s_bluetooth_processer.order, get_status) == 0)
  178. {
  179. ESP_LOGI(GATTS_TAG, get_status);
  180. receipt_json_get_status();
  181. }
  182. // if (strcmp(s_bluetooth_processer.order, "deviceStatusReport") == 0)
  183. // {
  184. // ESP_LOGI(GATTS_TAG, "deviceStatusReport");
  185. // }
  186. s_bluetooth_processer.actively_report_flag = true;
  187. }
  188. }
  189. //释放空间
  190. cJSON_Delete(json_tmp);
  191. // buffer置0
  192. buffer_all_init();
  193. //未在处理数据
  194. s_bluetooth_processer.cmd_flag = false;
  195. s_bluetooth_processer.bluetooth_rx_buffer_start_receving = false;
  196. s_bluetooth_processer.bluetooth_rx_buffer_processing = false;
  197. }
  198. }
  199. void port_timer_delay_ms(uint64_t delay)
  200. {
  201. uint64_t timer_count = 0;
  202. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
  203. timer_start(TIMER_GROUP_0, TIMER_0);
  204. while (timer_count < (delay * 1000))
  205. {
  206. timer_get_counter_value(TIMER_GROUP_0, TIMER_0, &timer_count);
  207. }
  208. timer_pause(TIMER_GROUP_0, TIMER_0);
  209. }
  210. bool parse_rxbuffer_and_validation_data(cJSON **json_tmp)
  211. {
  212. *json_tmp = cJSON_Parse(&bluetooth_rx_buffer[0]);
  213. if (*json_tmp == NULL)
  214. {
  215. ESP_LOGE(GATTS_TAG, "parse rxbuffer nil or redundant symbol ',' ");
  216. return false;
  217. }
  218. return true;
  219. }
  220. bool parse_json_to_struct(cJSON *ch)
  221. {
  222. uint8_t cmd_length = 0;
  223. while (ch != NULL)
  224. {
  225. // ESP_LOGI(GATTS_TAG, "%s", ch->string);
  226. if (strcmp(ch->string, "order") == 0)
  227. {
  228. s_bluetooth_processer.order = ch->valuestring;
  229. if (strcmp(ch->valuestring, set_position) == 0)
  230. {
  231. cmd_length = cmd_length_set_position;
  232. }
  233. if (strcmp(ch->valuestring, get_status) == 0)
  234. {
  235. cmd_length = cmd_length_get_status;
  236. }
  237. // if (strcmp(ch->valuestring, "deviceStatusReport") == 0)
  238. // {
  239. // cmd_length = cmd_length_device_status_report;
  240. // }
  241. cmd_length--;
  242. }
  243. if (strcmp(ch->string, "index") == 0)
  244. {
  245. s_bluetooth_processer.index = ch->valueint;
  246. cmd_length--;
  247. }
  248. if (strcmp(ch->string, "speedLevel") == 0)
  249. {
  250. s_bluetooth_processer.speed_level = ch->valueint;
  251. cmd_length--;
  252. }
  253. if (strcmp(ch->string, "position") == 0)
  254. {
  255. s_bluetooth_processer.position = ch->valuedouble;
  256. cmd_length--;
  257. }
  258. if (strcmp(ch->string, "direction") == 0)
  259. {
  260. s_bluetooth_processer.direction = ch->valueint;
  261. cmd_length--;
  262. }
  263. ch = ch->next;
  264. }
  265. if (cmd_length == 0)
  266. {
  267. s_bluetooth_processer.cmd_flag = true;
  268. }
  269. else
  270. {
  271. ESP_LOGE(GATTS_TAG, "JSON directive missing or exceeded");
  272. }
  273. return s_bluetooth_processer.cmd_flag;
  274. }
  275. void receipt_json_set_position()
  276. {
  277. cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
  278. if (!pRoot)
  279. {
  280. return;
  281. }
  282. cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
  283. cJSON_AddNumberToObject(pRoot, "code", s_bluetooth_processer.code);
  284. cJSON_AddStringToObject(pRoot, "info", "success");
  285. cJSON_AddNumberToObject(pRoot, "index", s_bluetooth_processer.index);
  286. char *szJson = cJSON_Print(pRoot);
  287. if (szJson != NULL)
  288. {
  289. ESP_LOGI(GATTS_TAG, "%s", szJson);
  290. // esp_gatt_rsp_t rsp;
  291. // memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
  292. // rsp.attr_value.handle = constructor_param->read.handle;
  293. // rsp.attr_value.len = 4;
  294. // rsp.attr_value.value[0] = 0x11;
  295. // rsp.attr_value.value[1] = 0x22;
  296. // rsp.attr_value.value[2] = 0x33;
  297. // rsp.attr_value.value[3] = 0x44;
  298. // esp_ble_gatts_send_response(constructor_gatts_if, constructor_param->read.conn_id,
  299. // constructor_param->read.trans_id, ESP_GATT_OK, &rsp);
  300. free(szJson);
  301. }
  302. cJSON_Delete(pRoot);
  303. }
  304. void receipt_json_get_status()
  305. {
  306. cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
  307. if (!pRoot)
  308. {
  309. return;
  310. }
  311. cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
  312. cJSON_AddNumberToObject(pRoot, "index", s_bluetooth_processer.index);
  313. cJSON_AddStringToObject(pRoot, "deviceState", s_bluetooth_processer.deviceState);
  314. cJSON_AddNumberToObject(pRoot, "deviceException", s_bluetooth_processer.deviceException);
  315. cJSON_AddStringToObject(pRoot, "deviceExceptionInfo", s_bluetooth_processer.deviceExceptionInfo);
  316. cJSON_AddNumberToObject(pRoot, "position", s_bluetooth_processer.position);
  317. char *szJson = cJSON_Print(pRoot);
  318. if (szJson != NULL)
  319. {
  320. ESP_LOGI(GATTS_TAG, "%s", szJson);
  321. free(szJson);
  322. }
  323. cJSON_Delete(pRoot);
  324. }