bluetooth.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. #include "head.h"
  2. #include "bluetooth.h"
  3. // #define BL_DEBUG // 调试打印
  4. #define FREAM_SPLIT_LEN (240) // 分帧长度
  5. #define FREAM_INTERVAL_TIME_MS (65) // 帧间隔ms,手册建议帧间隔60ms以上
  6. #define BL_DISC_REBOOT_TIME_MS (100) // 连接断开复位蓝牙延时ms
  7. #define BL_IDLE_REBOOT_TIME_MS (120000) // 蓝牙空闲状态自动复位延时ms
  8. #define BL_ENABLE_RESEND_MODE (1) // 重发机制
  9. #define BL_RESEND_TIMES (3) // 重发次数
  10. #define BL_RECV_TIMEOUT_MS (500) // 接收超时时间
  11. #define BL_TIMEOUT_RESEND_MS (500) // 超时重发时间
  12. #define BLUETOOTH_DEFAULT_NAME "OLE-DTUv5" // 蓝牙默认名称,获取ID失败时设置此蓝牙名称
  13. /* 各指令详细描述请参考蓝牙手册,路径:Z:\研发中心\研发二部\共享\88_DTU5.0\02蓝牙模块 */
  14. /* AT指令匹配列表 */
  15. const struct at_cmd_index_match at_cmd_index_match[AT_CMD_MAX] =
  16. {
  17. //指令字符串 追加字符串 指令结束字符串 数据段结束字符串 指令结果结束字符串 指令匹配的枚举类型
  18. {"SCAN" , "=" , "{\r\n" , "}\r\n" , NULL , AT_CMD_SACN },
  19. {"CONN" , "=" , "=" , NULL , NULL , AT_CMD_CONN },
  20. {"CHINFO" , "" , "{\r\n" , "}\r\n" , "OK\r\n" , AT_CMD_CHINFO },
  21. {"LESEND" , "=" , "\r\n" , NULL , "OK\r\n" , AT_CMD_LESEND },
  22. {"LEDISC" , "=" , "=" , "\r\n" , NULL , AT_CMD_LEDISC },
  23. {"NAME" , "=" , "=" , "\r\n" , "OK\r\n" , AT_CMD_NAME },
  24. {"MAC" , "" , "=" , "\r\n" , "OK\r\n" , AT_CMD_MAC },
  25. {"REBOOT" , "" , "\r\n" , NULL , "OK\r\n" , AT_CMD_REBOOT },
  26. {"DATA" , "" , "=" , "\r\n" , NULL , AT_CMD_DATA },
  27. {"GATTSTAT" , "" , "=" , "\r\n" , NULL , AT_CMD_STAT },
  28. };
  29. /**
  30. * @brief 获取一个buf
  31. * @details 从缓存队列中返回一个buf
  32. * @param _pt 结构体指针
  33. * @return buf索引
  34. * @author EWen
  35. * @date 2025-10-11
  36. * @remarks 增加发送队列缓存是考虑到多帧分帧放进缓存后自动发送,但目前报文组帧处理了可以一帧发完,暂时用不上
  37. */
  38. static u8 _get_one_buf(bl_tbuf_t *pt)
  39. {
  40. u8 head;
  41. head = pt->tb_head;
  42. pt->tb_head++;
  43. if (pt->tb_head >= BL_TBUF_NUM)
  44. {
  45. pt->tb_head = 0;
  46. }
  47. // 系统设计时,保证缓冲不会满,以下情况不应出现
  48. if (pt->tb_head == pt->tb_tail_ack || pt->tb_head == pt->tb_tail_send)
  49. {
  50. rt_printf_time("bluetooth: _get_one_buf error:head=%d,send=%d,ack=%d.\r\n", pt->tb_head, pt->tb_tail_send, pt->tb_tail_ack);
  51. }
  52. return head;
  53. }
  54. /**
  55. * @brief 确认一条发送的报文
  56. * @details none
  57. * @param _pt 结构体指针
  58. * @param _cmd_type 接收到的报文类型
  59. * @return none
  60. * @author EWen
  61. * @date 2025-10-11
  62. * @remarks Tnone
  63. */
  64. static void _ack_one_buf(bl_tbuf_t *pt, s8 _cmd_type)
  65. {
  66. if (_cmd_type == AT_CMD_DATA)
  67. {
  68. // 蓝牙模块主动发送的数据不需进行发送确认
  69. return;
  70. }
  71. if (_cmd_type == AT_CMD_STAT)
  72. {
  73. u8 send_cmd_index;
  74. send_cmd_index = (pt->tb_tail_ack + 1) >= BL_TBUF_NUM ? 0 : (pt->tb_tail_ack + 1);
  75. // 不是因终端主动发起的命令而收到AT_CMD_STAT类型数据不需确认
  76. if (pt->tb_cmd[send_cmd_index] != AT_CMD_CONN || pt->tb_cmd[send_cmd_index] != AT_CMD_LEDISC)
  77. return;
  78. }
  79. pt->tb_tail_ack++;
  80. if (pt->tb_tail_ack >= BL_TBUF_NUM)
  81. pt->tb_tail_ack = 0;
  82. #ifdef BL_DEBUG
  83. rt_printf("ack tb_head=%d,tb_tail_send=%d,tb_tail_ack=%d\r\n", pt->tb_head, pt->tb_tail_send, pt->tb_tail_ack);
  84. #endif
  85. }
  86. /**
  87. * @brief 组帧
  88. * @details none
  89. * @param _pt 结构体指针
  90. * @param _pdat 数据指针
  91. * @param _cmd 组帧类型
  92. * @param _len 数据长度
  93. * @return 0: 成功 其它: 失败
  94. * @author EWen
  95. * @date 2025-10-11
  96. * @remarks 目前格式字符填充部分最长长度为数据发送格式填充18字节
  97. */
  98. static int _frame_packing(BLUETOOTH_DEF *_pt, const u8 *_pdat, AT_CMD _cmd, u8 _len)
  99. {
  100. u8 i = 0;
  101. u8 head = 0;
  102. u8 *pd = NULL;
  103. u8 ext_len = 0;
  104. u8 str_len = 0;
  105. // 目前格式字符填充部分最长长度为数据发送格式填充18字节
  106. // 组帧buf首字节为发送长度因此再 -1
  107. if (_len >= BL_TBUF_LEN - 18 - 1)
  108. {
  109. rt_printf("蓝牙->%s 组帧数据长度越限,in len=%d,max=%d\r\n", __func__, _len, BL_TBUF_LEN - 19);
  110. return -1;
  111. }
  112. // 匹配at命令
  113. for (i = 0; i < AT_CMD_MAX; i++)
  114. {
  115. if (at_cmd_index_match[i].cmd == _cmd)
  116. break;
  117. }
  118. if (i == AT_CMD_MAX)
  119. {
  120. rt_printf("bluetooth: %s error! can't find cmd %d\r\n", __func__, _cmd);
  121. return -2;
  122. }
  123. head = _get_one_buf(&_pt->tx_buf[0]);
  124. _pt->tx_buf[0].tb_cmd[head] = _cmd;
  125. pd = &_pt->tx_buf[0].tb_data[head][1];
  126. // 固定头
  127. strcpy(pd, "AT+");
  128. pd += 3;
  129. ext_len += 3;
  130. // 根据_cmd 填充命令
  131. str_len = strlen(at_cmd_index_match[i].cmd_str);
  132. strncpy(pd, at_cmd_index_match[i].cmd_str, str_len);
  133. pd += str_len;
  134. ext_len += str_len;
  135. // 根据可选字符填充
  136. if (at_cmd_index_match[i].send_op_str)
  137. {
  138. str_len = strlen(at_cmd_index_match[i].send_op_str);
  139. strncpy(pd, at_cmd_index_match[i].send_op_str, str_len);
  140. pd += str_len;
  141. ext_len += str_len;
  142. }
  143. // 填充长度数据
  144. if (_cmd == AT_CMD_LESEND)
  145. {
  146. u8 tmp_len[6] = {0};
  147. *pd++ = '0';
  148. *pd++ = ',';
  149. ext_len += 2;
  150. sprintf(tmp_len, "%d", _len);
  151. str_len = strlen(tmp_len);
  152. strncpy(pd, tmp_len, str_len);
  153. pd += str_len;
  154. ext_len += str_len;
  155. *pd++ = ',';
  156. ext_len += 1;
  157. }
  158. // 填充发送数据
  159. if (_pdat)
  160. {
  161. memcpy(pd, _pdat, _len);
  162. pd += _len;
  163. }
  164. // 固定尾
  165. strcpy(pd, "\r\n");
  166. ext_len += 2;
  167. // 首字节为需发送报文长度
  168. _pt->tx_buf[0].tb_data[head][0] = _len + ext_len;
  169. #ifdef BL_DEBUG
  170. rt_printf_time("%s data:%d,ext:%d\r\n", __func__, _len, ext_len);
  171. for (i = 0; i < _len + ext_len; i++)
  172. {
  173. if (i != 0 && i % 16 == 0)
  174. rt_printf("\r\n");
  175. rt_printf("%02x ", _pt->tx_buf[0].tb_data[head][i + 1]);
  176. }
  177. rt_printf("\r\n");
  178. #endif
  179. return 0;
  180. }
  181. /**
  182. * @brief 设置蓝牙名称
  183. * @details none
  184. * @param _pt 结构体指针
  185. * @param _pdat 名称字符串指针
  186. * @param _len 名称长度
  187. * @return 0: 成功 其它: 失败
  188. * @author EWen
  189. * @date 2025-10-11
  190. * @remarks none
  191. */
  192. static int _name_set(BLUETOOTH_DEF *pt, const u8 *_pdat, u8 _len)
  193. {
  194. return _frame_packing(pt, _pdat, AT_CMD_NAME, _len);
  195. }
  196. /**
  197. * @brief 蓝牙模块软重启
  198. * @details none
  199. * @param _pt 结构体指针
  200. * @return 0: 成功 其它: 失败
  201. * @author EWen
  202. * @date 2025-10-11
  203. * @remarks none
  204. */
  205. static int _reboot(BLUETOOTH_DEF *pt)
  206. {
  207. return _frame_packing(pt, NULL, AT_CMD_REBOOT, 0);
  208. }
  209. /**
  210. * @brief 断开蓝牙连接
  211. * @details none
  212. * @param _pt 结构体指针
  213. * @return 0: 成功 其它: 失败
  214. * @author EWen
  215. * @date 2025-10-11
  216. * @remarks 已连接状态才起作用,否则蓝牙模块不回复
  217. */
  218. static int _ledisc(BLUETOOTH_DEF *pt)
  219. {
  220. const u8 para = '0'; // 固定为 "0"
  221. if (!pt->lnk_sta)
  222. {
  223. // 非连接状态下发送断开连接命令,蓝牙模块不会回复报文
  224. rt_printf("非连接状态下不能进行断开连接操作!\r\n");
  225. return -1;
  226. }
  227. return _frame_packing(pt, &para, AT_CMD_LEDISC, 1);
  228. }
  229. /**
  230. * @brief 接收数据复位
  231. * @details none
  232. * @param _pt 结构体指针
  233. * @return 0: 成功 其它: 失败
  234. * @author EWen
  235. * @date 2025-10-11
  236. * @remarks none
  237. */
  238. static void _recv_rst(BLUETOOTH_DEF *pt, u8 num)
  239. {
  240. if (num == 10)
  241. {
  242. rt_printf_time("接收帧错误,超时未能接收到帧结束符号!!!\r\n");
  243. }
  244. if (pt->cTypeCounter)
  245. {
  246. rt_printf_time("bluetooth_recv_rst:state=%d,counter=%d,num=%d\r\n", pt->cTypeCounter, pt->cRecvCnt, num);
  247. }
  248. pt->cTypeCounter = 0;
  249. pt->cRecvLenth = 0;
  250. pt->cRecvCnt = 0;
  251. pt->us0_recv = 0;
  252. }
  253. /**
  254. * @brief 蓝牙接收数据解析
  255. * @details 判断是否接收到完整一帧
  256. * @param _pt 结构体指针
  257. * @return 0: 成功 其它: 失败
  258. * @author EWen
  259. * @date 2025-10-11
  260. * @remarks none
  261. */
  262. static int _recv_parse(BLUETOOTH_DEF *pt, u8 c)
  263. {
  264. u8 i = 0;
  265. u8 match_count = 0;
  266. switch (pt->cTypeCounter)
  267. {
  268. case 0:
  269. if (c == '+') // 固定起始符号
  270. {
  271. pt->cmd_type = -1;
  272. pt->cTypeCounter++;
  273. pt->us0_recv = bsp_ustimer_get_origin();
  274. memset(pt->cmd_recvbuf, '\0', sizeof(pt->cmd_recvbuf));
  275. }
  276. else
  277. {
  278. _recv_rst(pt, 1);
  279. }
  280. break;
  281. case 1:
  282. pt->cmd_recvbuf[pt->cRecvCnt++] = c;
  283. if (pt->cRecvCnt >= CMD_RECVBUF_LEN)
  284. {
  285. _recv_rst(pt, 2);
  286. rt_printf_time("接收指令错误,指令长度溢出!!!\r\n");
  287. break;
  288. }
  289. // 最短命令匹配字符MAC
  290. if (pt->cRecvCnt < 3)
  291. {
  292. break;
  293. }
  294. // 命令未匹配
  295. if (pt->cmd_type == -1)
  296. {
  297. for (i = 0; i < AT_CMD_MAX; i++)
  298. {
  299. // 匹配命令
  300. if (strstr(at_cmd_index_match[i].cmd_str, pt->cmd_recvbuf) != NULL)
  301. {
  302. match_count++;
  303. pt->cmd_type = at_cmd_index_match[i].cmd;
  304. break;
  305. }
  306. }
  307. // 3个字符都未能匹配一条,不支持命令,重新接收
  308. if (match_count == 0)
  309. {
  310. rt_printf("蓝牙接收->不支持的指令:%s\r\n", pt->cmd_recvbuf);
  311. pt->cmd_type = -1;
  312. _recv_rst(pt, 3);
  313. break;
  314. }
  315. else if (match_count > 1)
  316. {
  317. // 多条匹配继续接收匹配
  318. pt->cmd_type = -1;
  319. }
  320. }
  321. else
  322. {
  323. if (strstr(pt->cmd_recvbuf, at_cmd_index_match[pt->cmd_type].cmd_end_str) != NULL)
  324. {
  325. #ifdef BL_DEBUG
  326. pt->cmd_recvbuf[pt->cRecvCnt - 1] = '\0';
  327. rt_printf("recv cmd:%s\r\n", pt->cmd_recvbuf);
  328. #endif
  329. // 命令结束,转入下一流程
  330. pt->cRecvCnt = 0;
  331. pt->cTypeCounter++;
  332. memset(pt->recvbuf, 0, sizeof(pt->recvbuf));
  333. _ack_one_buf(&pt->tx_buf[0], pt->cmd_type);
  334. }
  335. }
  336. break;
  337. case 2:
  338. if (at_cmd_index_match[pt->cmd_type].data_end_str)
  339. {
  340. // 此帧包含数据
  341. pt->recvbuf[pt->cRecvCnt++] = c;
  342. if (pt->cRecvCnt >= BLUETOOTH_BUF_LEN)
  343. {
  344. _recv_rst(pt, 4);
  345. rt_printf("蓝牙接收->数据长度溢出!!!\r\n");
  346. break;
  347. }
  348. if (c == '\n')
  349. {
  350. if (strstr(&pt->recvbuf[pt->cRecvCnt - strlen(at_cmd_index_match[pt->cmd_type].data_end_str)], at_cmd_index_match[pt->cmd_type].data_end_str) != NULL)
  351. {
  352. #ifdef BL_DEBUG
  353. int i = 0;
  354. rt_printf("recv data:\r\n");
  355. for (i = 0; i <= pt->cRecvCnt - 1; i++)
  356. {
  357. if (i % 16 == 0 && i != 0)
  358. rt_printf("\r\n");
  359. rt_printf("%02x ", pt->recvbuf[i]);
  360. }
  361. rt_printf("\r\n");
  362. #endif
  363. if (at_cmd_index_match[pt->cmd_type].sta_end_str)
  364. {
  365. pt->cRecvCnt = 0;
  366. pt->cTypeCounter++;
  367. }
  368. else
  369. {
  370. pt->bData = true;
  371. pt->cTypeCounter = 0;
  372. _recv_rst(pt, 5);
  373. }
  374. }
  375. }
  376. }
  377. else if (at_cmd_index_match[pt->cmd_type].sta_end_str)
  378. {
  379. // 结果判断
  380. pt->cTypeCounter++;
  381. pt->cmd_recvbuf[pt->cRecvCnt++] = c;
  382. }
  383. else
  384. {
  385. _recv_rst(pt, 6);
  386. rt_printf("%s %d unknowd operation!\r\n", __func__, __LINE__);
  387. }
  388. break;
  389. case 3:
  390. // 结果
  391. pt->cmd_recvbuf[pt->cRecvCnt++] = c;
  392. if (c == '\n')
  393. {
  394. #ifdef BL_DEBUG
  395. rt_printf("recv statue:%s", pt->cmd_recvbuf);
  396. #endif
  397. if (strstr(pt->cmd_recvbuf, "OK\r\n") != NULL)
  398. {
  399. pt->cmd_sta = true;
  400. if (at_cmd_index_match[pt->cmd_type].data_end_str)
  401. {
  402. pt->bData = true;
  403. }
  404. }
  405. else
  406. {
  407. rt_printf("蓝牙->执行%s->%s失败!!\r\n", at_cmd_index_match[pt->cmd_type].cmd_str, pt->cmd_recvbuf);
  408. }
  409. pt->cTypeCounter = 0;
  410. _recv_rst(pt, 7);
  411. }
  412. break;
  413. default:
  414. _recv_rst(pt, 8);
  415. break;
  416. }
  417. return 0;
  418. }
  419. /**
  420. * @brief 接收应用
  421. * @details 从fifo提取数据去解析
  422. * @param _pt 结构体指针
  423. * @return none
  424. * @author EWen
  425. * @date 2025-10-11
  426. * @remarks none
  427. */
  428. static void _recv_app(BLUETOOTH_DEF *pt)
  429. {
  430. u8 c;
  431. // 接收数据
  432. while (pt->bData == false)
  433. {
  434. if (rt_fifo_get(&pt->recv_fifo, &c, 1) == 0)
  435. {
  436. if (pt->b_recv_reset)
  437. {
  438. _recv_rst(pt, 10);
  439. pt->b_recv_reset = 0;
  440. }
  441. break;
  442. }
  443. _recv_parse(pt, c);
  444. }
  445. }
  446. /**
  447. * @brief 蓝牙接收数据解析
  448. * @details 数据接收判断时已将"+DATA="去除
  449. * @param _pt 结构体指针
  450. * @return 0: 成功 其它: 失败
  451. * @author EWen
  452. * @date 2025-10-11
  453. * @remarks 数据内容格式:+DATA=para1,para2,para3; para1:固定值0,para2:接收的数据长度,para3:接收的数据内容
  454. */
  455. static int _data_parse(BLUETOOTH_DEF *pt)
  456. {
  457. u16 i = 0;
  458. u8 *pd = NULL;
  459. char *endptr;
  460. u16 data_len = 0;
  461. u8 len_str[5] = {0};
  462. // 固定开头为 "0,"
  463. if (pt->recvbuf[0] != '0' || pt->recvbuf[1] != ',')
  464. {
  465. rt_printf("%s data format error!\r\n", __func__);
  466. return -1;
  467. }
  468. // 解析长度字符串
  469. pd = &pt->recvbuf[2];
  470. for (i = 0; i < 5; i++)
  471. {
  472. len_str[i] = *pd++;
  473. // 长度字符串以","结束,跳过","
  474. if (*pd == ',')
  475. {
  476. pd++;
  477. break;
  478. }
  479. }
  480. if (i == 5)
  481. {
  482. rt_printf("%s lenth error!\r\n", __func__);
  483. return -2;
  484. }
  485. else
  486. {
  487. data_len = strtol(len_str, &endptr, 10);
  488. if (*endptr != '\0')
  489. {
  490. rt_printf("%s lenth strtol failed!\r\n", __func__);
  491. return -3;
  492. }
  493. #ifdef BL_DEBUG
  494. rt_printf("%s data_len:%d\r\n", __func__, data_len);
  495. #endif
  496. }
  497. for (i = 0; i < data_len; i++)
  498. {
  499. u8 j;
  500. int ret;
  501. ret = sec_recv(&pt->rx_buf, *pd++, pt->arrSendBuf, false);
  502. if (ret == SEC_FRAME_OK_APP) // 带规约的报文
  503. {
  504. rt_printf("未实现的蓝牙通讯帧处理->SEC_FRAME_OK_APP\r\n");
  505. }
  506. else if (ret == SEC_FRAME_OK_EXT) // 纯安全扩展报文 发送
  507. {
  508. u16 remaining_length = 0;
  509. // 前两个字节为加密返回报文的长度
  510. remaining_length = ((pt->arrSendBuf[0] << 8) + (pt->arrSendBuf[1]));
  511. #ifdef BL_DEBUG
  512. rt_printf("esam data len->%d\r\n", remaining_length);
  513. #endif
  514. // 分帧
  515. // TODO 直接将报文分帧有较大几率证书管理工具会报错,此处已在加密报文处理中将每帧证书长度修改为220处理
  516. // 可以在一帧报文中将加密安全扩展报文发送,如确实需分帧的话要按照加密报文分帧处理再分帧,此处没有处理。。。
  517. if (remaining_length > FREAM_SPLIT_LEN)
  518. {
  519. for (j = 0;; j++)
  520. {
  521. if (remaining_length <= FREAM_SPLIT_LEN)
  522. {
  523. _frame_packing(pt, pt->arrSendBuf + 2 + j * FREAM_SPLIT_LEN, AT_CMD_LESEND, (u8)remaining_length);
  524. break;
  525. }
  526. _frame_packing(pt, pt->arrSendBuf + 2 + j * FREAM_SPLIT_LEN, AT_CMD_LESEND, FREAM_SPLIT_LEN);
  527. remaining_length -= FREAM_SPLIT_LEN;
  528. }
  529. }
  530. else
  531. {
  532. _frame_packing(pt, pt->arrSendBuf + 2, AT_CMD_LESEND, (u8)remaining_length);
  533. }
  534. }
  535. }
  536. return 0;
  537. }
  538. /**
  539. * @brief 蓝牙连接状态解析
  540. * @details none
  541. * @param _pt 结构体指针
  542. * @return 0:成功 其它:失败
  543. * @author EWen
  544. * @date 2025-10-11
  545. * @remarks 数据格式0,0 1 2
  546. */
  547. static int _status_parse(BLUETOOTH_DEF *pt)
  548. {
  549. // 固定开头为 "0,"
  550. if (pt->recvbuf[0] != '0' || pt->recvbuf[1] != ',')
  551. {
  552. rt_printf("%s data format error!\r\n", __func__);
  553. return -1;
  554. }
  555. if (pt->recvbuf[2] == '1')
  556. {
  557. // 连接断开
  558. pt->lnk_sta = false;
  559. rt_printf("蓝牙连接断开!\r\n");
  560. pt->us0_disc_reboot = bsp_ustimer_get_origin();
  561. }
  562. else if (pt->recvbuf[2] == '2')
  563. {
  564. // 正在连接
  565. rt_printf("蓝牙连接中...\r\n");
  566. }
  567. else if (pt->recvbuf[2] == '3')
  568. {
  569. // 已连接
  570. pt->lnk_sta = true;
  571. rt_printf("蓝牙已连接!\r\n");
  572. }
  573. else
  574. {
  575. // 未知的连接状态
  576. rt_printf_time("蓝牙连接状态解析错误,未知的状态%d%d%d!\r\n", pt->recvbuf[0], pt->recvbuf[1], pt->recvbuf[2]);
  577. return -2;
  578. }
  579. return 0;
  580. }
  581. /**
  582. * @brief 蓝牙名称设置解析
  583. * @details 解析名称设置结果
  584. * @param _pt 结构体指针
  585. * @return none
  586. * @author EWen
  587. * @date 2025-10-11
  588. * @remarks none
  589. */
  590. static void _name_set_parse(BLUETOOTH_DEF *pt)
  591. {
  592. rt_printf("设置蓝牙名称:%s%s\r\n", pt->recvbuf, pt->cmd_sta ? "成功!" : "失败!");
  593. }
  594. /**
  595. * @brief 蓝牙数据初始化
  596. * @details none
  597. * @param _pt 结构体指针
  598. * @param chnl 通道
  599. * @return 0: 成功 其它: 失败
  600. * @author EWen
  601. * @date 2025-10-11
  602. * @remarks none
  603. */
  604. int bluetooth_init(BLUETOOTH_DEF *pt, u8 chnl)
  605. {
  606. u8 name_buf[30] = {0};
  607. RMT_FIXED_TABLE tfixedset;
  608. memset(pt, 0, sizeof(BLUETOOTH_DEF));
  609. memset(&tfixedset, 0, sizeof(tfixedset));
  610. tfixedset.num = RMT_FIXED_SET_NUM;
  611. tfixedset.set[0].type = FIXED_SET_ID;
  612. iec_get_fixedset_csv(&tfixedset);
  613. // 初始化接收buf结构
  614. pt->chnl = chnl;
  615. pt->rx_buf.chn = pt->chnl;
  616. pt->rx_buf.buf = pt->esambuf;
  617. rt_fifo_init(&pt->recv_fifo, pt->recv_fifo_buf, BLUETOOTH_BUF_LEN);
  618. if (tfixedset.set[0].len == 24)
  619. {
  620. /* ID 标准定义24位 */
  621. rt_printf("设置蓝牙名称:%s\r\n", tfixedset.set[0].str);
  622. memcpy(name_buf, tfixedset.set[0].str, tfixedset.set[0].len);
  623. }
  624. else
  625. {
  626. strcpy(name_buf, BLUETOOTH_DEFAULT_NAME);
  627. rt_printf("蓝牙: 未能从fixset.csv中获取到正确ID,使用默认名称:%s.\r\n", BLUETOOTH_DEFAULT_NAME);
  628. }
  629. _name_set(pt, name_buf, strlen(name_buf));
  630. _reboot(pt);
  631. return 0;
  632. }
  633. /**
  634. * @brief 蓝牙数据接收
  635. * @details 将数据存入fifo中
  636. * @param _pt 结构体指针
  637. * @param _byte 数据
  638. * @return none
  639. * @author EWen
  640. * @date 2025-10-11
  641. * @remarks none
  642. */
  643. void bluetooth_recv(BLUETOOTH_DEF *pt, u8 _byte)
  644. {
  645. rt_fifo_put(&pt->recv_fifo, &_byte, 1);
  646. }
  647. /**
  648. * @brief 发送检查
  649. * @details none
  650. * @param _pt 结构体指针
  651. * @return none
  652. * @author EWen
  653. * @date 2025-10-11
  654. * @remarks 帧间隔延时精度受扫描间隔影响,实际测试到1000ms间隔发送也没什么问题
  655. */
  656. static void _send_polling(BLUETOOTH_DEF *_pt)
  657. {
  658. u8 *buf;
  659. u16 i = 0;
  660. u16 len = 0;
  661. u8 *pd, tb_send;
  662. bl_tbuf_t *pbuf;
  663. pbuf = &_pt->tx_buf[0];
  664. #if BL_ENABLE_RESEND_MODE
  665. // 上一帧未收到回复
  666. if (pbuf->tb_tail_send != pbuf->tb_tail_ack)
  667. {
  668. // 未超时不发下一帧
  669. if (bsp_ustimer_get_duration(_pt->us0_cfmtimeout) < USTIMER_MS * BL_TIMEOUT_RESEND_MS)
  670. {
  671. // tb_tail_send != tb_tail_ack,说明已进行过报文发送即更新过_pt->us0_cfmtimeout
  672. // 所以此处直接判断延时是否超时也没有问题
  673. return;
  674. }
  675. else
  676. {
  677. // 重发次数到达不再重发
  678. if (_pt->resend_cnt >= BL_RESEND_TIMES)
  679. {
  680. _pt->resend_cnt = 0;
  681. memset(_pt->tx_buf, 0, sizeof(_pt->tx_buf));
  682. return;
  683. }
  684. // 超时重发
  685. pbuf->tb_tail_send--; // 发送尾索引回上一帧的位置进行重发
  686. _pt->resend_cnt++;
  687. rt_printf_time("超时重发上一帧,重发次数=%d,tb_head=%d,tb_tail_send=%d,len=%d\r\n", _pt->resend_cnt, pbuf->tb_head, pbuf->tb_tail_send, pbuf->tb_data[pbuf->tb_tail_send][0]);
  688. for (i = 0; i < pbuf->tb_data[pbuf->tb_tail_send][0]; i++)
  689. {
  690. if (i != 0 && i % 16 == 0)
  691. rt_printf("\r\n");
  692. rt_printf("%02x ", pbuf->tb_data[pbuf->tb_tail_send][i + 1]);
  693. }
  694. rt_printf("\r\n");
  695. }
  696. }
  697. #endif
  698. // 如果串口忙或者帧间隔时间未到,不能发送数据
  699. if (g_tRsComm[_pt->chnl].bextsend || bsp_ustimer_get_duration(_pt->us0_sendpiece) < USTIMER_MS * FREAM_INTERVAL_TIME_MS)
  700. {
  701. return;
  702. }
  703. if (pbuf->tb_head == pbuf->tb_tail_send)
  704. {
  705. return;
  706. }
  707. // 更新发送尾指针,保留老指针备用。
  708. tb_send = pbuf->tb_tail_send;
  709. pbuf->tb_tail_send++;
  710. if (pbuf->tb_tail_send >= BL_TBUF_NUM)
  711. {
  712. pbuf->tb_tail_send = 0;
  713. }
  714. #ifdef BL_DEBUG
  715. rt_printf("send tb_head=%dtb_tail_send=%dtb_tail_ack=%d\r\n", pbuf->tb_head, pbuf->tb_tail_send, pbuf->tb_tail_ack);
  716. #endif
  717. // 将数据复制到真正发送的buf
  718. len = pbuf->tb_data[tb_send][0];
  719. pd = g_tRsComm[_pt->chnl].extsendbuf;
  720. memcpy(pd, &pbuf->tb_data[tb_send][1], len);
  721. g_tRsComm[_pt->chnl].extsendcnt = 0;
  722. g_tRsComm[_pt->chnl].extsendlen = len;
  723. g_tRsComm[_pt->chnl].bextsend = true;
  724. #ifdef BL_DEBUG
  725. {
  726. rt_printf_time("bluetooth send data:%d\r\n", len);
  727. for (i = 0; i < len; i++)
  728. {
  729. if (i != 0 && i % 16 == 0)
  730. rt_printf("\r\n");
  731. rt_printf("%02x ", g_tRsComm[_pt->chnl].extsendbuf[i]);
  732. }
  733. rt_printf("\r\n");
  734. }
  735. #endif
  736. _pt->us0_cfmtimeout = _pt->us0_sendpiece = bsp_ustimer_get_origin();
  737. }
  738. /**
  739. * @brief 蓝牙应用巡检
  740. * @details none
  741. * @param _pt 结构体指针
  742. * @return 0:成功 其它:失败
  743. * @author EWen
  744. * @date 2025-10-11
  745. * @remarks none
  746. */
  747. int bluetooth_app(BLUETOOTH_DEF *pt)
  748. {
  749. _recv_app(pt);
  750. if (pt->bData)
  751. {
  752. switch (pt->cmd_type)
  753. {
  754. case AT_CMD_DATA:
  755. _data_parse(pt);
  756. break;
  757. case AT_CMD_STAT:
  758. _status_parse(pt);
  759. break;
  760. case AT_CMD_NAME:
  761. _name_set_parse(pt);
  762. break;
  763. default:
  764. rt_printf("%s 未支持的指令类型->%d\r\n", __func__, pt->cmd_type);
  765. break;
  766. }
  767. pt->bData = false;
  768. }
  769. _send_polling(pt);
  770. return 0;
  771. }
  772. /**
  773. * @brief 蓝牙定时器
  774. * @details 处理一些定时任务
  775. * @param _pt 结构体指针
  776. * @return none
  777. * @author EWen
  778. * @date 2025-10-11
  779. * @remarks 延时精度受扫描间隔影响,但实际在此处理的操作实时性要求不高,因此没有关系
  780. */
  781. void bluetooth_timer(BLUETOOTH_DEF *pt)
  782. {
  783. if (pt->us0_disc_reboot)
  784. {
  785. if (bsp_ustimer_get_duration(pt->us0_disc_reboot) >= BL_DISC_REBOOT_TIME_MS * USTIMER_MS)
  786. {
  787. _reboot(pt);
  788. pt->us0_disc_reboot = 0;
  789. }
  790. }
  791. if (pt->lnk_sta)
  792. {
  793. pt->us0_idle_reboot = 0;
  794. }
  795. else
  796. {
  797. if (pt->us0_idle_reboot == 0)
  798. {
  799. pt->us0_idle_reboot = bsp_ustimer_get_origin();
  800. }
  801. else
  802. {
  803. if (bsp_ustimer_get_duration(pt->us0_idle_reboot) >= BL_IDLE_REBOOT_TIME_MS * USTIMER_MS)
  804. {
  805. _reboot(pt);
  806. pt->us0_idle_reboot = 0;
  807. }
  808. }
  809. }
  810. if (pt->us0_recv)
  811. {
  812. if (bsp_ustimer_get_duration(pt->us0_recv) >= BL_RECV_TIMEOUT_MS * USTIMER_MS)
  813. {
  814. pt->us0_recv = 0;
  815. pt->b_recv_reset = true;
  816. }
  817. }
  818. }