ble.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /************************************************************************************************
  2. * Include *
  3. ************************************************************************************************/
  4. #include "ble.h"
  5. #include "ble_cmd.h"
  6. #include "ble_sn2model.h"
  7. #include "stm32f4xx_gpio.h"
  8. #include "systick.h"
  9. #include "uart.h"
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <time.h>
  14. /************************************************************************************************
  15. * Config *
  16. ************************************************************************************************/
  17. uint8_t zero_sn[];
  18. extern BLE_CMD_ASYN g_ble_cmd_asyn_table[];
  19. static uint8_t BUSY_IND = 0; // 0、空闲;1、繁忙
  20. static uint8_t AUTH_IND = 0; // 0、未认证;1、认证
  21. static uint8_t BLE_CONN_FLG = 0;
  22. uint8_t g_ble_mac[MAC_LEN] = {0};
  23. uint8_t g_ble_name[BLE_NAME_LEN + 1] = {0};
  24. uint8_t g_ble_adv_data[28] = {0};
  25. uint8_t g_ble_rst = 0; // 0、运行;1、复位
  26. BLE_CMD_CACHE g_ble_cmd_cache; // 命令缓存
  27. /************************************************************************************************
  28. * Data structs *
  29. ************************************************************************************************/
  30. void ble_reset(void)
  31. {
  32. GPIO_ResetBits(BT_RST_PORT, BT_RST_PIN);
  33. ms_delay(1);
  34. GPIO_SetBits(BT_RST_PORT, BT_RST_PIN);
  35. }
  36. void ble_init(void)
  37. {
  38. usart_config_init(&usart2_context, 9600);
  39. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  40. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  41. GPIO_InitTypeDef GPIO_StructInit;
  42. GPIO_StructInit.GPIO_Mode = GPIO_Mode_OUT;
  43. GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
  44. GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
  45. GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP;
  46. GPIO_StructInit.GPIO_Pin = BT_RST_PIN;
  47. GPIO_Init(BT_RST_PORT, &GPIO_StructInit);
  48. ble_reset();
  49. GPIO_StructInit.GPIO_Mode = GPIO_Mode_IN;
  50. GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
  51. GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
  52. GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_NOPULL;
  53. GPIO_StructInit.GPIO_Pin = BT_BUSY_IND_PIN;
  54. GPIO_Init(BT_BUSY_IND_PORT, &GPIO_StructInit);
  55. GPIO_StructInit.GPIO_Mode = GPIO_Mode_IN;
  56. GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
  57. GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
  58. GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP;
  59. GPIO_StructInit.GPIO_Pin = BT_CONN_IND_PIN;
  60. GPIO_Init(BT_CONN_IND_PORT, &GPIO_StructInit);
  61. }
  62. // 蓝牙名称
  63. int32_t ble_cmd08_action_asyn(uint8_t cmd, uint8_t status, uint8_t *data, uint8_t len)
  64. {
  65. RSP_CMD08 *ptr;
  66. if (status != 0)
  67. {
  68. return status;
  69. }
  70. ptr = (RSP_CMD08 *)data;
  71. strcpy(g_ble_name, ptr->name);
  72. return 0;
  73. }
  74. // MAC地址
  75. int32_t ble_cmd18_action_asyn(uint8_t cmd, uint8_t status, uint8_t *data, uint8_t len)
  76. {
  77. RSP_CMD18 *ptr;
  78. if (status != 0)
  79. {
  80. return status;
  81. }
  82. ptr = (RSP_CMD18 *)data;
  83. if (ptr->item0.item == 3)
  84. {
  85. memcpy(g_ble_mac, ptr->item3.mac, sizeof(g_ble_mac));
  86. // OPP_SWAP_N(g_ble_mac, mac_len);
  87. }
  88. return 0;
  89. }
  90. // 广播数据
  91. int32_t ble_cmd0e_action_asyn(uint8_t cmd, uint8_t status, uint8_t *data, uint8_t len)
  92. {
  93. RSP_CMD0E *ptr;
  94. if (status != 0)
  95. {
  96. return status;
  97. }
  98. ptr = (RSP_CMD0E *)data;
  99. memcpy(g_ble_adv_data, ptr->adv_data, sizeof(g_ble_adv_data));
  100. return 0;
  101. }
  102. // 序列初始化
  103. int32_t ble_cfg_init_asyn(EN_BLE_INIT_CMD cmd)
  104. {
  105. int32_t ret;
  106. REQ_CMD07 cmd07;
  107. REQ_CMD08 cmd08;
  108. uint8_t SN[SN_LEN] = {0};
  109. uint8_t UPP[SN_LEN] = {0};
  110. uint8_t model[MODEL_LEN] = {0};
  111. uint8_t upp_model[MODEL_LEN] = {0};
  112. uint8_t buff[64] = {0};
  113. if (g_ble_cmd_cache.is_used == 1)
  114. {
  115. return 1;
  116. }
  117. if (cmd == BLE_INIT_CMD_NAME_QRY)
  118. {
  119. g_ble_cmd_cache.is_used = 1;
  120. g_ble_cmd_cache.cmd = 0x08;
  121. g_ble_cmd_cache.len = sizeof(REQ_CMD08);
  122. memcpy(g_ble_cmd_cache.data, &cmd08, sizeof(REQ_CMD08));
  123. g_ble_cmd_cache.p_func = ble_cmd08_action_asyn;
  124. g_ble_cmd_cache.tick = 0;
  125. }
  126. if (cmd == BLE_INIT_CMD_NAME_CFG)
  127. {
  128. // int32_t len = alientek_sn_get(SN);
  129. int32_t len = 12;
  130. ret = sn_2_model(SN, 1, model);
  131. if (ret == 0)
  132. {
  133. len = sprintf(buff, "%s", model);
  134. // upp_sn_get(UPP);
  135. if (memcpy(UPP, zero_sn, SN_LEN) != 0)
  136. {
  137. ret = sn_2_model((uint8_t *)UPP, 1, upp_model);
  138. if (ret == 0)
  139. {
  140. sprintf(buff + len, " %s", upp_model);
  141. }
  142. }
  143. if (strcpy(g_ble_name, buff) != 0)
  144. {
  145. strcpy(cmd07.name, buff);
  146. g_ble_cmd_cache.cmd = 0x07;
  147. g_ble_cmd_cache.len = strlen(buff);
  148. strcpy(&g_ble_cmd_cache.data, buff);
  149. g_ble_cmd_cache.p_func = NULL;
  150. g_ble_cmd_cache.tick = 0;
  151. }
  152. }
  153. }
  154. if (cmd == BLE_INIT_CMD_TXWPR_CFG)
  155. {
  156. REQ_CMD0B req0b;
  157. req0b.tx_power = 8;
  158. ret = ble_exec_cmd0b(&req0b);
  159. if (ret == 0)
  160. {
  161. }
  162. }
  163. return 0;
  164. }
  165. int32_t ble_cfg_init_do_asyn(void)
  166. {
  167. static EN_BLE_INIT_CMD cmd = BLE_INIT_CMD_NAME_QRY;
  168. int32_t ret;
  169. ret = ble_cfg_init_asyn(cmd);
  170. if (ret == 0)
  171. {
  172. cmd++;
  173. }
  174. if (cmd == BLE_INIT_CMD_MAX)
  175. {
  176. cmd = BLE_INIT_CMD_NAME_QRY;
  177. return 0;
  178. }
  179. return 1;
  180. }
  181. int32_t ble_cfg_init(void)
  182. {
  183. int32_t ret;
  184. REQ_CMD07 cmd07;
  185. REQ_CMD08 cmd08;
  186. RSP_CMD08 rsp08;
  187. uint8_t SN[SN_LEN] = {0};
  188. uint8_t model[MODEL_LEN] = {0};
  189. memset(&rsp08, 0, sizeof(RSP_CMD08));
  190. ret = ble_exec_cmd08(&cmd08, &rsp08);
  191. if (ret == 0)
  192. {
  193. }
  194. int32_t len = 12;
  195. // int32_t len = alientek_sn_get(SN);
  196. ret = sn_2_model(SN, 1, model);
  197. if (ret == 0)
  198. {
  199. if (strcmp(rsp08.name, model) != 0)
  200. {
  201. strcpy(cmd07.name, model);
  202. ret = ble_exec_cmd07(&cmd07, CMD_T0);
  203. if (ret != 0)
  204. {
  205. }
  206. }
  207. }
  208. else
  209. {
  210. }
  211. return 0;
  212. }
  213. int32_t ble_cfg_query_asyn(EN_BLE_INIT_CMD cmd)
  214. {
  215. REQ_CMD18 cmd18;
  216. REQ_CMD0E cmd0e;
  217. uint8_t buff[64] = {0};
  218. int32_t len = 0;
  219. if (g_ble_cmd_cache.is_used == 1)
  220. {
  221. return 1;
  222. }
  223. if (cmd == BLE_QRY_CMD_ADV_QRY)
  224. {
  225. g_ble_cmd_cache.is_used = 1;
  226. g_ble_cmd_cache.cmd = 0x0e;
  227. g_ble_cmd_cache.len = sizeof(REQ_CMD0E);
  228. memcpy(g_ble_cmd_cache.data, &cmd0e, sizeof(REQ_CMD0E));
  229. g_ble_cmd_cache.p_func = ble_cmd0e_action_asyn;
  230. g_ble_cmd_cache.tick = 0;
  231. }
  232. if (cmd == BLE_QRY_CMD_MAC_QRY)
  233. {
  234. cmd18.item = 3;
  235. g_ble_cmd_cache.is_used = 1;
  236. g_ble_cmd_cache.cmd = 0x18;
  237. g_ble_cmd_cache.len = sizeof(REQ_CMD18);
  238. memcpy(g_ble_cmd_cache.data, &cmd0e, sizeof(REQ_CMD18));
  239. g_ble_cmd_cache.p_func = ble_cmd18_action_asyn;
  240. g_ble_cmd_cache.tick = 0;
  241. }
  242. if (cmd == BLE_QRY_CMD_ADV_CFG)
  243. {
  244. if (memcpy(g_ble_mac, g_ble_adv_data, MAC_LEN) != 0)
  245. {
  246. buff[len++] = 1;
  247. buff[len++] = 1 + MAC_LEN;
  248. buff[len++] = 0xFF;
  249. memcpy(&buff[len], g_ble_mac, MAC_LEN);
  250. len += MAC_LEN;
  251. g_ble_cmd_cache.is_used = 1;
  252. g_ble_cmd_cache.cmd = 0x0d;
  253. g_ble_cmd_cache.len = len;
  254. memcpy(g_ble_cmd_cache.data, buff, len);
  255. g_ble_cmd_cache.p_func = NULL;
  256. g_ble_cmd_cache.tick = 0;
  257. }
  258. }
  259. return 0;
  260. }
  261. int32_t ble_cfg_query_do_asyn(void)
  262. {
  263. static EN_BLE_QRY_CMD cmd = BLE_QRY_CMD_ADV_QRY;
  264. int32_t ret;
  265. ret = ble_cfg_query_asyn(cmd);
  266. if (ret == 0)
  267. {
  268. cmd++;
  269. }
  270. if (cmd == BLE_QRY_CMD_MAX)
  271. {
  272. cmd = BLE_QRY_CMD_ADV_QRY;
  273. return 0;
  274. }
  275. return 1;
  276. }
  277. int32_t ble_cfg_query(void)
  278. {
  279. int32_t ret;
  280. REQ_CMD18 cmd18;
  281. RSP_CMD18 rsp18;
  282. REQ_CMD0A cmd0a;
  283. RSP_CMD0A rsp0a;
  284. REQ_CMD0C cmd0c;
  285. RSP_CMD0C rsp0c;
  286. REQ_CMD0E cmd0e;
  287. RSP_CMD0E rsp0e;
  288. uint8_t buff[64] = {0};
  289. int32_t len = 0;
  290. int32_t adv_data_len = 0;
  291. uint8_t *adv_data_ptr = NULL;
  292. uint8_t adv_data_uuid = 0;
  293. ret = ble_exec_cmd0e(&cmd0e, &rsp0e);
  294. if (ret == 0)
  295. {
  296. adv_data_len = rsp0e.adv_data[0];
  297. adv_data_uuid = rsp0e.adv_data[1];
  298. adv_data_ptr = &rsp0e.adv_data[2];
  299. // len += sprintf(buff+len)
  300. }
  301. cmd18.item = 3;
  302. ret = ble_exec_cmd18(&cmd18, &rsp18);
  303. if (ret == 0)
  304. {
  305. memcpy(g_ble_mac, rsp18.item3.mac, len);
  306. // OPP_SWAP_N(g_ble_mac, mac_len);
  307. if (adv_data_len == 0 || adv_data_ptr == NULL || memcmp(rsp18.item3.mac, adv_data_ptr, MAC_LEN) != 0)
  308. {
  309. ret = ble_exec_cmd0d(1, rsp18.item3.mac, MAC_LEN);
  310. if (ret != 0)
  311. {
  312. }
  313. }
  314. }
  315. else
  316. {
  317. }
  318. cmd18.item = 5;
  319. ret = ble_exec_cmd18(&cmd18, &rsp18);
  320. if (ret == 0)
  321. {
  322. }
  323. ret = ble_exec_cmd0a(&cmd0a, &rsp0a);
  324. if (ret == 0)
  325. {
  326. }
  327. ret = ble_exec_cmd0c(&cmd0c, &rsp0c);
  328. if (ret == 0)
  329. {
  330. }
  331. }
  332. int32_t BUSY_IND_LOOP(void)
  333. {
  334. static uint32_t tick = 0;
  335. static uint32_t cnt = 0;
  336. uint8_t busy;
  337. if (tick == 0)
  338. {
  339. tick = get_systick_ms();
  340. }
  341. if (get_systick_ms() - tick > 50)
  342. {
  343. if (cnt++ < 2)
  344. {
  345. return 0;
  346. }
  347. else
  348. {
  349. cnt = 0;
  350. }
  351. if (GPIO_ReadInputDataBit(BT_BUSY_IND_PORT, BT_BUSY_IND_PIN) == 0)
  352. {
  353. busy = 1;
  354. }
  355. else
  356. {
  357. busy = 0;
  358. }
  359. if (BUSY_IND != busy)
  360. {
  361. BUSY_IND = busy;
  362. }
  363. }
  364. return 0;
  365. }
  366. int32_t CONN_IND_LOOP(void)
  367. {
  368. static uint32_t tick = 0;
  369. static uint32_t cnt = 0;
  370. uint8_t conn;
  371. if (tick == 0)
  372. {
  373. tick = get_systick_ms();
  374. }
  375. if (get_systick_ms() - tick > 50)
  376. {
  377. if (cnt++ < 2)
  378. {
  379. return 0;
  380. }
  381. else
  382. {
  383. cnt = 0;
  384. }
  385. if (GPIO_ReadInputDataBit(BT_CONN_IND_PORT, BT_CONN_IND_PIN) == 0)
  386. {
  387. conn = 1;
  388. }
  389. else
  390. {
  391. conn = 0;
  392. }
  393. if (BLE_CONN_FLG != conn)
  394. {
  395. BLE_CONN_FLG = conn;
  396. if (BLE_CONN_FLG == 0)
  397. {
  398. AUTH_IND = 0;
  399. }
  400. }
  401. }
  402. return 0;
  403. }
  404. int32_t auth_ind_set(uint8_t auth)
  405. {
  406. AUTH_IND = auth;
  407. return 0;
  408. }
  409. int32_t ble_is_conn(void)
  410. {
  411. return BLE_CONN_FLG;
  412. }
  413. int32_t ble_get_busy_port_sts(void)
  414. {
  415. return BUSY_IND;
  416. }
  417. int32_t ble_is_busy(void)
  418. {
  419. if (BLE_CONN_FLG == 0 || BUSY_IND == 1 || g_ble_cmd_cache.is_used == 1)
  420. {
  421. return 1;
  422. }
  423. return 0;
  424. }
  425. int32_t ble_is_auth(void)
  426. {
  427. return AUTH_IND;
  428. }
  429. int32_t ble_data_recv(uint8_t data[], uint8_t length, int32_t ms)
  430. {
  431. static uint32_t tick = 0;
  432. uint8_t rdata[BLE_MTU];
  433. int32_t rlen;
  434. if (tick == 0)
  435. {
  436. tick = get_systick_ms();
  437. }
  438. do
  439. {
  440. rlen = usart_read_ble_format(&usart2_context);
  441. if (rlen > 0)
  442. {
  443. usart_read_update(&usart2_context, rlen, (uint8_t *)rdata);
  444. if (rdata[2] == g_ble_cmd_cache.cmd)
  445. {
  446. if (g_ble_cmd_cache.p_func != NULL)
  447. {
  448. g_ble_cmd_cache.p_func(rdata[2], rdata[4], rdata, rlen);
  449. }
  450. memset(&g_ble_cmd_cache, 0, sizeof(BLE_CMD_CACHE));
  451. }
  452. if (rdata[2] == 0x42)
  453. {
  454. // ble_2_app((uint8_t *)&rdata[4], rlen - 4);
  455. tick = 0;
  456. return 0;
  457. }
  458. else
  459. {
  460. memcpy(data, rdata, rlen);
  461. tick = 0;
  462. return rlen;
  463. }
  464. }
  465. else
  466. {
  467. }
  468. } while (get_systick_ms() - tick < ms);
  469. tick = 0;
  470. return -1;
  471. }
  472. int32_t ble_send_data(uint8_t data[], int32_t length)
  473. {
  474. if (length > BLE_MTU)
  475. {
  476. return 1;
  477. }
  478. g_ble_cmd_cache.is_used = 1;
  479. g_ble_cmd_cache.cmd = 0x41;
  480. g_ble_cmd_cache.len = length;
  481. memcpy(g_ble_cmd_cache.data, data, length);
  482. g_ble_cmd_cache.tick = 0;
  483. g_ble_cmd_cache.p_func = NULL;
  484. return 0;
  485. }
  486. int32_t ble_rcv_loop(void)
  487. {
  488. uint8_t rdata[BLE_MTU];
  489. ble_data_recv(rdata, sizeof(rdata), 0);
  490. return 0;
  491. }
  492. int32_t ble_name_modify(void)
  493. {
  494. static uint8_t last_sn[SN_LEN] = {0};
  495. if (ble_is_conn())
  496. {
  497. return 0;
  498. }
  499. }
  500. int32_t ble_tx_loop(void)
  501. {
  502. static uint8_t count = 0;
  503. if (g_ble_cmd_cache.is_used == 1)
  504. {
  505. if (g_ble_cmd_cache.tick == 0)
  506. {
  507. g_ble_cmd_cache.tick = get_systick_ms();
  508. count = 0;
  509. }
  510. if (count == 0 || get_systick_ms() - g_ble_cmd_cache.tick > 3000)
  511. {
  512. for (int32_t i = 0; i < countof_asyn_cmd_table(); i++)
  513. {
  514. if (g_ble_cmd_asyn_table[i].cmd == g_ble_cmd_cache.cmd && g_ble_cmd_asyn_table[i].p_func != NULL)
  515. {
  516. g_ble_cmd_asyn_table[i].p_func(g_ble_cmd_cache.cmd, g_ble_cmd_cache.data, g_ble_cmd_cache.len);
  517. }
  518. }
  519. g_ble_cmd_cache.tick = get_systick_ms();
  520. count++;
  521. }
  522. if (count > 10)
  523. {
  524. ble_reset();
  525. g_ble_rst = 1;
  526. memset(&g_ble_cmd_cache, 0, sizeof(BLE_CMD_CACHE));
  527. count = 0;
  528. }
  529. }
  530. return 0;
  531. }
  532. int32_t ble_loop(void)
  533. {
  534. static uint8_t init = 0;
  535. static uint32_t tick = 0;
  536. static uint8_t init1 = 0;
  537. static uint32_t tick1 = 0;
  538. int32_t ret;
  539. if (g_ble_rst == 1)
  540. {
  541. init = 0;
  542. init1 = 0;
  543. g_ble_rst = 0;
  544. }
  545. if (!init)
  546. {
  547. if (tick == 0)
  548. tick = get_systick_ms();
  549. if (get_systick_ms() - tick > BLE_CFG_DELAY_MS)
  550. {
  551. ret = ble_cfg_init_do_asyn();
  552. if (ret == 0)
  553. {
  554. init = 1;
  555. }
  556. }
  557. }
  558. if (init && !init1)
  559. {
  560. if (tick1 == 0)
  561. tick1 = get_systick_ms();
  562. if (get_systick_ms() - tick1 > BLE_QRY_DELAY_MS)
  563. {
  564. ret = ble_cfg_query_do_asyn();
  565. if (ret == 0)
  566. {
  567. init1 = 1;
  568. }
  569. }
  570. }
  571. ble_rcv_loop();
  572. if (init && init1)
  573. {
  574. BUSY_IND_LOOP();
  575. CONN_IND_LOOP();
  576. ble_name_modify();
  577. }
  578. ble_tx_loop();
  579. return 0;
  580. }
  581. /************************************************************************************************
  582. * implements *
  583. ************************************************************************************************/