app_wireless_pair.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*Includes ----------------------------------------------*/
  2. #include "tool.h"
  3. #include "nrf_delay.h"
  4. #include "system.h"
  5. #include "ble_comm.h"
  6. #include "bsp_time.h"
  7. #include "hal_led.h"
  8. #include "hal_charge.h"
  9. #include "hal_ble_common.h"
  10. #include "hal_ble_client.h"
  11. #include "hal_ble_host.h"
  12. #include "app_flash.h"
  13. #include "app_wireless_pair.h"
  14. #include "app_detectIsHost.h"
  15. /*Private macro ------------------------------------------------------------------------------------------------------------------------------------*/
  16. #define APP_WIRELESS_PAIR_FSMTABLE_LEN 8 //状态表长度
  17. #define APP_WIRELESS_PAIR_RSSI_THRESHOLD -18 //rssi阈值
  18. #define APP_WIRELESS_PAIR_RSSI_BUFF_LEN 15 //rssi缓冲区长度
  19. /*Struction ------------------------------------------------------------------------------------------------------------------------------------*/
  20. //定义状态表的数据类型
  21. typedef struct _app_wireless_pair_fsmtable_s
  22. {
  23. App_Wireless_Pair_Event_e event; //事件
  24. App_Wireless_Pair_State_e CurState; //当前状态
  25. int (*eventActFun)(); //函数指针
  26. App_Wireless_Pair_State_e NextState; //下一个状态
  27. }App_Wireless_Pair_fsmtable_s;
  28. //定义状态机类型
  29. typedef struct _app_wireless_pair_fsm_s
  30. {
  31. App_Wireless_Pair_State_e curState; //当前状态
  32. App_Wireless_Pair_fsmtable_s FsmTable[APP_WIRELESS_PAIR_FSMTABLE_LEN]; //状态表
  33. App_Wireless_Pair_Set_Scan_Cb set_scan_cb; //设置扫描回调
  34. App_Wireless_Pair_Set_Adv_Cb set_adv_cb; //设置广播回调
  35. uint8_t peer_addr[6]; //对端MAC地址
  36. bool is_busy; //是否忙碌
  37. uint8_t led_display_count; //亮灯次数
  38. uint8_t led_switch; //灯的开关
  39. char already_scan_or_adv_mac_buf[16]; //已经在扫描或广播的mac地址
  40. int rssi_buf[APP_WIRELESS_PAIR_RSSI_BUFF_LEN]; //rssi缓冲区
  41. }App_Wireless_Pair_Fsm_s;
  42. /*Local Variable ------------------------------------------------------------------------------------------------------------------------------------*/
  43. static App_Wireless_Pair_Fsm_s ob_app_wireless_pair_fsm;
  44. /*Local Functions ------------------------------------------------------------------------------------------------------------------------------------*/
  45. static void app_wireless_pair_slow_clear_rssi_buff_process(void);
  46. static int app_wireless_pair_set_scan_adv_on(void)
  47. {
  48. int ret = 0;
  49. int adv_stop = -1;
  50. //若是右鞋则进入主动扫描状态
  51. //若是左鞋则设置扫描回应为MAC地址状态
  52. if(mFlash.isHost)
  53. {
  54. if(slave_isconnect() == 0)
  55. {
  56. //已连接状态下,不能调用停止广播
  57. adv_stop = wireless_pair_advertising_stop();
  58. if(ob_app_wireless_pair_fsm.set_adv_cb != NULL && adv_stop == 0 && slave_isconnect() == 0)ret += ob_app_wireless_pair_fsm.set_adv_cb(0x01);
  59. else ret = -1;
  60. }
  61. nrf_ble_scan_stop();
  62. host_disconnect();
  63. //已连接状态下,不能调用停止扫描
  64. if(host_isconnect() == 0)
  65. {
  66. if(ob_app_wireless_pair_fsm.set_scan_cb != NULL)ret += ob_app_wireless_pair_fsm.set_scan_cb(0x00);
  67. else ret = -1;
  68. }
  69. else
  70. {
  71. ret = -1;
  72. }
  73. }
  74. else
  75. {
  76. if(host_isconnect() == 0)
  77. {
  78. //已连接状态下,不能调用停止扫描
  79. nrf_ble_scan_stop();
  80. if(ob_app_wireless_pair_fsm.set_scan_cb != NULL)ret += ob_app_wireless_pair_fsm.set_scan_cb(0x01);
  81. else ret = -1;
  82. }
  83. slave_disconnect();
  84. //已连接状态下,不能调用停止广播
  85. if(slave_isconnect() == 0)adv_stop = wireless_pair_advertising_stop();
  86. if(adv_stop == 0 && slave_isconnect() == 0)
  87. {
  88. if(ob_app_wireless_pair_fsm.set_adv_cb != NULL)ret += ob_app_wireless_pair_fsm.set_adv_cb(0x00);
  89. else ret = -1;
  90. }
  91. else
  92. {
  93. ret = -1;
  94. }
  95. }
  96. if(ret == 0)Process_Start(100,"app_wireless_pair_slow_clear_rssi_buff_process",app_wireless_pair_slow_clear_rssi_buff_process);
  97. return ret;
  98. }
  99. static int app_wireless_pair_set_scan_adv_off(void)
  100. {
  101. int ret = 0;
  102. char buf[16];
  103. int adv_stop = -1;
  104. //若是右鞋则进入被动扫描状态
  105. //若是左鞋则设置扫描回应为空数据状态
  106. if(mFlash.isHost)
  107. {
  108. if(slave_isconnect() == 0)
  109. {
  110. //已连接状态下,不能调用停止广播
  111. adv_stop = wireless_pair_advertising_stop();
  112. if(ob_app_wireless_pair_fsm.set_adv_cb != NULL && adv_stop == 0 && slave_isconnect() == 0)ret += ob_app_wireless_pair_fsm.set_adv_cb(0x00);
  113. else ret = -1;
  114. }
  115. nrf_ble_scan_stop();
  116. host_disconnect();
  117. //已连接状态下,不能调用停止扫描
  118. if(host_isconnect() == 0)
  119. {
  120. if(ob_app_wireless_pair_fsm.set_scan_cb != NULL)
  121. {
  122. sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  123. host_set_scan_name(buf,strlen(buf));
  124. ret += ob_app_wireless_pair_fsm.set_scan_cb(0x00);
  125. }
  126. else ret = -1;
  127. }
  128. else
  129. {
  130. ret = -1;
  131. }
  132. }
  133. else
  134. {
  135. if(host_isconnect() == 0)
  136. {
  137. //已连接状态下,不能调用停止扫描
  138. //调用停止扫描,因为右鞋不需要,反而会干扰传输速度
  139. nrf_ble_scan_stop();
  140. }
  141. slave_disconnect();
  142. //已连接状态下,不能调用停止广播
  143. if(slave_isconnect() == 0)adv_stop = wireless_pair_advertising_stop();
  144. if(adv_stop == 0 && slave_isconnect() == 0)
  145. {
  146. if(ob_app_wireless_pair_fsm.set_adv_cb != NULL)
  147. {
  148. if(mFlash.mClient.isConfig == 'C')
  149. {
  150. sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.mClient.macAddr[0],mFlash.mClient.macAddr[1],mFlash.mClient.macAddr[2],mFlash.macHost[3],mFlash.macHost[4],mFlash.macHost[5]);
  151. }
  152. else
  153. {
  154. ble_gap_addr_t m_addr;
  155. uint32_t err_code;
  156. //获取自身mac地址
  157. err_code = sd_ble_gap_addr_get(&m_addr);
  158. APP_ERROR_CHECK(err_code);
  159. for(int i=0;i<6;i++)mFlash.macHost[i] = m_addr.addr[5-i];
  160. sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.macHost[3],mFlash.macHost[4],mFlash.macHost[5]);
  161. }
  162. slave_set_adv_name(buf,strlen(buf));
  163. slave_adv_init();
  164. ret += ob_app_wireless_pair_fsm.set_adv_cb(0x00);
  165. }
  166. else ret = -1;
  167. }
  168. else
  169. {
  170. ret = -1;
  171. }
  172. }
  173. if(ret == 0)
  174. {
  175. ob_app_wireless_pair_fsm.led_switch = 1;
  176. memset(&ob_app_wireless_pair_fsm.peer_addr,0xFF,sizeof(ob_app_wireless_pair_fsm.peer_addr));
  177. for(int i=0;i<APP_WIRELESS_PAIR_RSSI_BUFF_LEN;i++)ob_app_wireless_pair_fsm.rssi_buf[i] = -500;
  178. Process_Stop(app_wireless_pair_slow_clear_rssi_buff_process);
  179. }
  180. return ret;
  181. }
  182. static int app_wireless_pair_unconditional_entry(void)
  183. {
  184. return 0; //不用做任何操作,直接进入下一个状态
  185. }
  186. static int app_wireless_pair_enter_in_process(int rssi, uint8_t *addr)
  187. {
  188. static uint8_t index = 0;
  189. int ret = 0;
  190. int rssi_max = -500;
  191. uint8_t null_addr[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
  192. if(ob_app_wireless_pair_fsm.is_busy == false)
  193. {
  194. ob_app_wireless_pair_fsm.rssi_buf[index++] = rssi;
  195. if(index == APP_WIRELESS_PAIR_RSSI_BUFF_LEN)index = 0;
  196. for(int i=0;i<APP_WIRELESS_PAIR_RSSI_BUFF_LEN;i++)rssi_max = (rssi_max > ob_app_wireless_pair_fsm.rssi_buf[i])?rssi_max:ob_app_wireless_pair_fsm.rssi_buf[i];
  197. // SEGGER_RTT_printf(0,"rssi:%d,%d,st:%d,curState:%d,active:%d,host:%d,slave:%d,mFlash.isHost:%d,led:%d,mac: ",rssi_max,rssi,NRF_RTC0->COUNTER, \
  198. // ob_app_wireless_pair_fsm.curState,wireless_pair_scan_get_active(),\
  199. // host_isconnect(),slave_isconnect(),mFlash.isHost,ob_app_wireless_pair_fsm.led_switch);
  200. if(mFlash.isHost)
  201. {
  202. extern char Target_scan[TARFET_LEN_MAX];
  203. // for(int i=0; i<6; i++)SEGGER_RTT_printf(0,"0x%x ",Target_scan[5-i]);
  204. }
  205. else
  206. {
  207. int len;
  208. char name[16];
  209. slave_get_advname_len(&len);
  210. slave_get_advname(name,len);
  211. // for(int i=0; i<6; i++)SEGGER_RTT_printf(0,"0x%x ",name[5-i]);
  212. }
  213. // if(rssi >= APP_WIRELESS_PAIR_RSSI_THRESHOLD)
  214. // {
  215. // SEGGER_RTT_printf(0," rssi_mac : ");for(int i=0; i<6; i++)SEGGER_RTT_printf(0,"0x%x ",addr[5-i]);SEGGER_RTT_printf(0,"\r\n");
  216. // }
  217. // else
  218. // SEGGER_RTT_printf(0,"\r\n");
  219. if(memcmp(addr,null_addr,6) != 0 && rssi >=APP_WIRELESS_PAIR_RSSI_THRESHOLD)
  220. {
  221. for(int i=0; i<6; i++)ob_app_wireless_pair_fsm.peer_addr[i] = addr[5-i];//这里是确保该rssi对应的mac
  222. ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
  223. }
  224. else if(rssi_max < APP_WIRELESS_PAIR_RSSI_THRESHOLD)
  225. {
  226. if(mFlash.isHost && host_isconnect() == 1)
  227. {
  228. ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
  229. }
  230. else if(!mFlash.isHost && slave_isconnect() == 1)
  231. {
  232. ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
  233. }
  234. else ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS);
  235. }
  236. else
  237. {
  238. if(memcmp(addr,null_addr,6) != 0)
  239. {
  240. ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
  241. }
  242. else
  243. {
  244. if(mFlash.isHost && host_isconnect() == 1)
  245. {
  246. ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
  247. }
  248. else if(!mFlash.isHost && slave_isconnect() == 1)
  249. {
  250. ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
  251. }
  252. else ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS);
  253. }
  254. }
  255. // if(rssi_max >= APP_WIRELESS_PAIR_RSSI_THRESHOLD)
  256. // {
  257. // if(memcmp(addr,null_addr,6) != 0)
  258. // {
  259. // ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
  260. // }
  261. // else
  262. // {
  263. // if(!mFlash.isHost && slave_isconnect() == 1)
  264. // {
  265. // ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
  266. // }
  267. // else ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS);
  268. // }
  269. // }
  270. // else
  271. // {
  272. // if(mFlash.isHost && host_isconnect() == 1)
  273. // {
  274. // ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
  275. // }
  276. // else if(!mFlash.isHost && slave_isconnect() == 1)
  277. // {
  278. // ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
  279. // }
  280. // else ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS);
  281. // }
  282. }
  283. return ret;
  284. }
  285. static void app_wireless_pair_slow_clear_rssi_buff_process(void)
  286. {
  287. uint8_t addr[6];
  288. memset(addr,0x00,6);
  289. app_wireless_pair_enter_in_process(APP_WIRELESS_PAIR_RSSI_THRESHOLD*2,addr);
  290. }
  291. static void display_led_process(void)
  292. {
  293. ob_app_wireless_pair_fsm.led_display_count++;
  294. if (ob_app_wireless_pair_fsm.led_display_count % 2 == 0)
  295. {
  296. LED_Start(LED_PAIR, COLOR_BLACK);
  297. }
  298. else
  299. {
  300. LED_Start(LED_PAIR, COLOR_BLUE);
  301. }
  302. if (ob_app_wireless_pair_fsm.led_display_count > 20)
  303. {
  304. ob_app_wireless_pair_fsm.led_display_count = 0;
  305. LED_Stop(LED_PAIR);
  306. Process_SetHoldOn(display_led_process, 0);
  307. Process_Stop(display_led_process);
  308. }
  309. }
  310. static void app_wireless_pair_start_display_led(void)
  311. {
  312. ob_app_wireless_pair_fsm.led_display_count = 0;
  313. Process_Start(500, "display_led_process", display_led_process);
  314. Process_SetHoldOn(display_led_process, 1);
  315. }
  316. static void app_wireless_pair_stop_display_led(void)
  317. {
  318. ob_app_wireless_pair_fsm.led_display_count = 0;
  319. LED_Stop(LED_PAIR);
  320. Process_SetHoldOn(display_led_process, 0);
  321. Process_Stop(display_led_process);
  322. }
  323. static void app_wireless_pair_in_process(void)
  324. {
  325. int ret = 0;
  326. uint8_t *connect_addr;
  327. uint8_t peer_addr[6]; //对端mac地址
  328. ble_gap_addr_t m_addr; //自身mac地址
  329. char buf[16];
  330. uint32_t err_code;
  331. int adv_stop = -1;
  332. uint8_t null_addr[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
  333. if(!get_LR_readly())
  334. {
  335. return;
  336. }
  337. if(ob_app_wireless_pair_fsm.curState == APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS)
  338. {
  339. ob_app_wireless_pair_fsm.is_busy = true;
  340. //获取已连接的对端mac地址
  341. connect_addr = wireless_pair_get_connect_macaddr();
  342. //获取自身mac地址
  343. err_code = sd_ble_gap_addr_get(&m_addr);
  344. APP_ERROR_CHECK(err_code);
  345. //获取对端的mac地址
  346. for(int i=0;i<6;i++)peer_addr[i]=ob_app_wireless_pair_fsm.peer_addr[i];
  347. if(mFlash.isHost) //左鞋处理逻辑
  348. {
  349. //判断是否处于连接且连接的mac地址为配对地址
  350. if(host_isconnect() == 1 && memcmp(peer_addr,connect_addr,6) == 0)
  351. {
  352. //判断是否配对过,若已配对过,直接进入配对结束阶段。若无配对过,则记录MAC地址,清除步数,进入配对结束。
  353. if(memcmp(peer_addr,mFlash.mClient.macAddr,6) != 0)
  354. {
  355. //配置左鞋广播名字,已连接状态下,不能调用停止广播
  356. if(slave_isconnect() == 0)adv_stop = wireless_pair_advertising_stop();
  357. if(adv_stop == 0 && slave_isconnect() == 0)
  358. {
  359. sprintf(buf,"SH_%02X%02X",m_addr.addr[4], m_addr.addr[5]);
  360. slave_set_adv_name(buf,strlen(buf));
  361. slave_adv_init();
  362. ret = ob_app_wireless_pair_fsm.set_adv_cb(0x01);
  363. if(ret == 0)
  364. {
  365. for(int i = 0; i < 6; i++)
  366. {
  367. mFlash.mClient.macAddr[i] = peer_addr[i];
  368. mFlash.macHost[i] = m_addr.addr[5-i];
  369. mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
  370. mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i]; //从机地址
  371. }
  372. mFlash.mClient.isConfig ='C';
  373. mBackup.hardVersion = mFlash.mClient.hardVersion;
  374. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  375. mBackup.isConfig = mFlash.mClient.isConfig;
  376. if(Flash_SaveBackup() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "pair save backup fail");}
  377. if(Flash_DeleteAllStep() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "pair clear step fail");}
  378. memset(&mFlash.mStep, 0, sizeof(FlashStep_t));
  379. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "Flash_SaveInfomation fail");}
  380. app_wireless_pair_start_led();//如果连接上没靠近,要在这里加才会开线程。
  381. app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_PAIR_DONE);
  382. }
  383. }
  384. }
  385. else
  386. {
  387. app_wireless_pair_start_led();//如果连接上没靠近,要在这里加才会开线程。
  388. app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_PAIR_DONE);
  389. }
  390. if(ob_app_wireless_pair_fsm.curState == APP_WIRELESS_PAIR_STATE_END_OF_PAIRING && host_get_rssi() >= APP_WIRELESS_PAIR_RSSI_THRESHOLD)
  391. {
  392. if(slave_isconnect() == 0)adv_stop = wireless_pair_advertising_stop();
  393. if(adv_stop == 0 && slave_isconnect() == 0)
  394. {
  395. ob_app_wireless_pair_fsm.set_adv_cb(0x00);
  396. }
  397. }
  398. }
  399. else
  400. {
  401. //若不是,断开连接,设置扫描为新的配对地址
  402. sprintf(buf,"%02X%02X%02X%02X%02X%02X",m_addr.addr[5],m_addr.addr[4],m_addr.addr[3],peer_addr[3],peer_addr[4],peer_addr[5]);
  403. if(memcmp(ob_app_wireless_pair_fsm.already_scan_or_adv_mac_buf,buf,strlen(buf)) != 0 && memcmp(peer_addr,null_addr,6) != 0)
  404. {
  405. // SEGGER_RTT_printf(0,"scan_or_adv_mac_buf: %s\r\n",ob_app_wireless_pair_fsm.already_scan_or_adv_mac_buf);
  406. // SEGGER_RTT_printf(0,"buf: %s\r\n",buf);
  407. if(slave_isconnect() == 0)
  408. {
  409. //已连接状态下,不能调用停止广播
  410. adv_stop = wireless_pair_advertising_stop();
  411. if(adv_stop == 0 && slave_isconnect() == 0)ret += ob_app_wireless_pair_fsm.set_adv_cb(0x01);
  412. }
  413. nrf_ble_scan_stop();
  414. host_disconnect();
  415. //已连接状态下,不能调用停止扫描
  416. if(host_isconnect() == 0)
  417. {
  418. host_set_scan_name(buf,strlen(buf));
  419. ret += ob_app_wireless_pair_fsm.set_scan_cb(0x02);
  420. if(ret == 0){memcpy(ob_app_wireless_pair_fsm.already_scan_or_adv_mac_buf,buf,strlen(buf));}
  421. }
  422. }
  423. }
  424. }
  425. else //右鞋处理逻辑
  426. {
  427. //判断是否处于连接且连接的mac地址为配对地址
  428. if(slave_isconnect() == 1 && memcmp(peer_addr,connect_addr,6) == 0)
  429. {
  430. //判断是否配对过,若已配对过,直接进入配对结束阶段。若无配对过,则记录MAC地址,清除步数,进入配对结束。
  431. if(memcmp(peer_addr,mFlash.mClient.macAddr,6) != 0)
  432. {
  433. for(int i = 0; i < 6; i++)
  434. {
  435. mFlash.mClient.macAddr[i] = peer_addr[i];
  436. mFlash.macHost[i] = m_addr.addr[5-i];
  437. mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
  438. mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i]; //从机地址
  439. }
  440. mFlash.mClient.isConfig ='C';
  441. mBackup.hardVersion = mFlash.mClient.hardVersion;
  442. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  443. mBackup.isConfig = mFlash.mClient.isConfig;
  444. if(Flash_SaveBackup() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "pair save backup fail");}
  445. if(Flash_DeleteAllStep() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "pair clear step fail");}
  446. memset(&mFlash.mStep, 0, sizeof(FlashStep_t));
  447. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "Flash_SaveInfomation fail");}
  448. }
  449. app_wireless_pair_start_led();//如果连接上没靠近,要在这里加才会开线程。
  450. app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_PAIR_DONE);
  451. if(ob_app_wireless_pair_fsm.curState == APP_WIRELESS_PAIR_STATE_END_OF_PAIRING && slave_get_rssi() >= APP_WIRELESS_PAIR_RSSI_THRESHOLD)
  452. {
  453. if(host_isconnect() == 0)nrf_ble_scan_stop();
  454. }
  455. }
  456. else
  457. {
  458. //若不是,断开连接,设置广播为新的配对地址
  459. sprintf(buf,"%02X%02X%02X%02X%02X%02X",peer_addr[0],peer_addr[1],peer_addr[2],m_addr.addr[2],m_addr.addr[1],m_addr.addr[0]);
  460. if(memcmp(ob_app_wireless_pair_fsm.already_scan_or_adv_mac_buf,buf,strlen(buf)) != 0 && memcmp(peer_addr,null_addr,6) != 0)
  461. {
  462. if(host_isconnect() == 0)
  463. {
  464. //已连接状态下,不能调用停止扫描
  465. nrf_ble_scan_stop();
  466. ret += ob_app_wireless_pair_fsm.set_scan_cb(0x01);
  467. }
  468. slave_disconnect();
  469. //已连接状态下,不能调用停止广播
  470. if(slave_isconnect() == 0)adv_stop = wireless_pair_advertising_stop();
  471. if(adv_stop == 0 && slave_isconnect() == 0)
  472. {
  473. DEBUG_LOG("pair : advName(%d):%s\n",strlen(buf),buf);
  474. slave_set_adv_name(buf,strlen(buf));
  475. slave_adv_init();
  476. ret += ob_app_wireless_pair_fsm.set_adv_cb(0x02);
  477. if(ret == 0){memcpy(ob_app_wireless_pair_fsm.already_scan_or_adv_mac_buf,buf,strlen(buf));}
  478. }
  479. }
  480. }
  481. }
  482. ob_app_wireless_pair_fsm.is_busy = false;
  483. }
  484. }
  485. /*API ------------------------------------------------------------------------------------------------------------------------------------*/
  486. /**
  487. @brief 初始化无线配对应用
  488. @param 无
  489. @return 错误代码 - [out] -1失败,0成功
  490. */
  491. int app_wireless_pair_Init(void)
  492. {
  493. //初始化结构体
  494. // memset(&ob_app_wireless_pair_fsm,0,sizeof(App_Wireless_Pair_Fsm_s)); //不能meset初始化,因为回调函数会被清除。
  495. ob_app_wireless_pair_fsm.curState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY;
  496. ob_app_wireless_pair_fsm.is_busy = false;
  497. ob_app_wireless_pair_fsm.led_switch = 1;
  498. memset(&ob_app_wireless_pair_fsm.peer_addr,0xFF,sizeof(ob_app_wireless_pair_fsm.peer_addr));
  499. for(int i=0;i<APP_WIRELESS_PAIR_RSSI_BUFF_LEN;i++)ob_app_wireless_pair_fsm.rssi_buf[i] = -500;
  500. //{到来的事件,当前的状态,将要要执行的函数,下一个状态}
  501. ob_app_wireless_pair_fsm.FsmTable[0].event = APP_WIRELESS_PAIR_EVENT_CHARGING_INSERTING; //充电插入
  502. ob_app_wireless_pair_fsm.FsmTable[0].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY; //配对待机状态
  503. ob_app_wireless_pair_fsm.FsmTable[0].eventActFun = app_wireless_pair_set_scan_adv_on; //设置扫描和广播开
  504. ob_app_wireless_pair_fsm.FsmTable[0].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
  505. ob_app_wireless_pair_fsm.FsmTable[1].event = APP_WIRELESS_PAIR_EVENT_CHARGING_UNPLUG; //充电拔出
  506. ob_app_wireless_pair_fsm.FsmTable[1].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
  507. ob_app_wireless_pair_fsm.FsmTable[1].eventActFun = app_wireless_pair_set_scan_adv_off; //设置扫描和广播关
  508. ob_app_wireless_pair_fsm.FsmTable[1].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY; //配对待机状态
  509. ob_app_wireless_pair_fsm.FsmTable[2].event = APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS; //rssi符合要求
  510. ob_app_wireless_pair_fsm.FsmTable[2].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
  511. ob_app_wireless_pair_fsm.FsmTable[2].eventActFun = app_wireless_pair_unconditional_entry; //不操作,直接进入下一个状态
  512. ob_app_wireless_pair_fsm.FsmTable[2].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS; //配对进行中状态
  513. ob_app_wireless_pair_fsm.FsmTable[3].event = APP_WIRELESS_PAIR_EVENT_CHARGING_UNPLUG; //充电拔出
  514. ob_app_wireless_pair_fsm.FsmTable[3].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS; //配对进行中状态
  515. ob_app_wireless_pair_fsm.FsmTable[3].eventActFun = app_wireless_pair_set_scan_adv_off; //设置扫描和广播关
  516. ob_app_wireless_pair_fsm.FsmTable[3].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY; //配对待机状态
  517. ob_app_wireless_pair_fsm.FsmTable[4].event = APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS; //rssi不符合要求
  518. ob_app_wireless_pair_fsm.FsmTable[4].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS; //配对进行中状态
  519. ob_app_wireless_pair_fsm.FsmTable[4].eventActFun = app_wireless_pair_unconditional_entry; //不操作,直接进入下一个状态
  520. ob_app_wireless_pair_fsm.FsmTable[4].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
  521. ob_app_wireless_pair_fsm.FsmTable[5].event = APP_WIRELESS_PAIR_EVENT_PAIR_DONE; //配对完成
  522. ob_app_wireless_pair_fsm.FsmTable[5].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS; //配对进行中状态
  523. ob_app_wireless_pair_fsm.FsmTable[5].eventActFun = app_wireless_pair_unconditional_entry; //不操作,直接进入下一个状态
  524. ob_app_wireless_pair_fsm.FsmTable[5].NextState = APP_WIRELESS_PAIR_STATE_END_OF_PAIRING; //配对结束状态
  525. ob_app_wireless_pair_fsm.FsmTable[6].event = APP_WIRELESS_PAIR_EVENT_CHARGING_UNPLUG; //充电拔出
  526. ob_app_wireless_pair_fsm.FsmTable[6].CurState = APP_WIRELESS_PAIR_STATE_END_OF_PAIRING; //配对结束状态
  527. ob_app_wireless_pair_fsm.FsmTable[6].eventActFun = app_wireless_pair_set_scan_adv_off; //设置扫描和广播关
  528. ob_app_wireless_pair_fsm.FsmTable[6].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY; //配对待机状态
  529. ob_app_wireless_pair_fsm.FsmTable[7].event = APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS; //rssi不符合要求
  530. ob_app_wireless_pair_fsm.FsmTable[7].CurState = APP_WIRELESS_PAIR_STATE_END_OF_PAIRING; //配对结束状态
  531. ob_app_wireless_pair_fsm.FsmTable[7].eventActFun = app_wireless_pair_unconditional_entry; //不操作,直接进入下一个状态
  532. ob_app_wireless_pair_fsm.FsmTable[7].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
  533. Process_Start(0,"app_wireless_pair_in_process",app_wireless_pair_in_process);
  534. return 0;
  535. }
  536. /**
  537. @brief 无线配对注册扫描设置
  538. @param cb - [in] 注册的回调
  539. @return 错误代码 - [out] -1失败,0成功
  540. */
  541. int app_wireless_pair_Regist_SetScan(App_Wireless_Pair_Set_Scan_Cb cb)
  542. {
  543. if(cb != NULL)
  544. {
  545. ob_app_wireless_pair_fsm.set_scan_cb = cb;
  546. return 0;
  547. }
  548. return -1;
  549. }
  550. /**
  551. @brief 无线配对注册广播设置
  552. @param cb - [in] 注册的回调
  553. @return 错误代码 - [out] -1失败,0成功
  554. */
  555. int app_wireless_pair_Regist_SetAdv(App_Wireless_Pair_Set_Adv_Cb cb)
  556. {
  557. if(cb != NULL)
  558. {
  559. ob_app_wireless_pair_fsm.set_adv_cb = cb;
  560. return 0;
  561. }
  562. return -1;
  563. }
  564. /**
  565. @brief 无线配对扫描请求
  566. @param rssi - [in] 天线信号强度
  567. @param addr - [in] MAC地址缓冲区指针
  568. @return 错误代码 - [out] -1失败,0成功
  569. */
  570. int app_wireless_pair_Scan_Req(int rssi, uint8_t *addr)
  571. {
  572. int ret = 0;
  573. if(mFlash.isHost)
  574. {
  575. app_wireless_pair_enter_in_process(rssi,addr);
  576. }
  577. else
  578. {
  579. ret = -1; //只有左鞋收到右鞋的扫描请求才对
  580. }
  581. return ret;
  582. }
  583. /**
  584. @brief 无线配对扫描回复
  585. @param rssi - [in] 天线信号强度
  586. @param addr - [in] MAC地址缓冲区指针
  587. @return 错误代码 - [out] -1失败,0成功
  588. */
  589. int app_wireless_pair_Scan_Rsp(int rssi, uint8_t *addr)
  590. {
  591. int ret = 0;
  592. if(!mFlash.isHost)
  593. {
  594. app_wireless_pair_enter_in_process(rssi,addr);
  595. }
  596. else
  597. {
  598. ret = -1; //只有右鞋收到左鞋的扫描回应才对
  599. }
  600. return ret;
  601. }
  602. /**
  603. @brief 无线配对亮灯
  604. @param 无
  605. @return 错误代码 - [out] -1失败,0成功
  606. */
  607. int app_wireless_pair_start_led(void)
  608. {
  609. if(ob_app_wireless_pair_fsm.led_switch != 0 && (ob_app_wireless_pair_fsm.curState == APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS))
  610. {
  611. ob_app_wireless_pair_fsm.led_switch--;
  612. app_wireless_pair_start_display_led();
  613. // SEGGER_RTT_printf(0,"app_wireless_pair_start_display_led\r\n");
  614. }
  615. return 0;
  616. }
  617. /**
  618. @brief 无线配对灭灯
  619. @param 无
  620. @return 错误代码 - [out] -1失败,0成功
  621. */
  622. int app_wireless_pair_stop_led(void)
  623. {
  624. app_wireless_pair_stop_display_led();
  625. app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS);
  626. // SEGGER_RTT_printf(0,"app_wireless_pair_stop_led\r\n");
  627. return 0;
  628. }
  629. /**
  630. @brief 无线配对事件处理
  631. @param event - [in] 事件
  632. @return 错误代码 - [out] -1失败,0成功
  633. */
  634. int app_wireless_pair_EventHandle(App_Wireless_Pair_Event_e event)
  635. {
  636. int (*eventActFun)() = NULL; //函数指针初始化为空
  637. App_Wireless_Pair_State_e NextState;
  638. int flag = 0; //标识是否满足条件
  639. int i;
  640. int ret = 0;
  641. /*获取当前动作函数*/
  642. for (i = 0; i<APP_WIRELESS_PAIR_FSMTABLE_LEN; i++)
  643. {
  644. //当且仅当当前状态下来个指定的事件,我才执行它
  645. if (event == ob_app_wireless_pair_fsm.FsmTable[i].event && ob_app_wireless_pair_fsm.curState == ob_app_wireless_pair_fsm.FsmTable[i].CurState)
  646. {
  647. flag = 1;
  648. eventActFun = ob_app_wireless_pair_fsm.FsmTable[i].eventActFun;
  649. NextState = ob_app_wireless_pair_fsm.FsmTable[i].NextState;
  650. break;
  651. }
  652. }
  653. if (flag) //如果满足条件了
  654. {
  655. /*动作执行*/
  656. if(eventActFun)
  657. {
  658. ret = eventActFun();
  659. }
  660. //跳转到下一个状态
  661. if(ret == 0)
  662. {
  663. ob_app_wireless_pair_fsm.curState = NextState;
  664. // SEGGER_RTT_printf(0,"success state:%d, event:%d\r\n",ob_app_wireless_pair_fsm.curState,event);
  665. }
  666. else
  667. {
  668. // SEGGER_RTT_printf(0,"*****************************************fail state:%d, event:%d\r\n",ob_app_wireless_pair_fsm.curState,event);
  669. }
  670. }
  671. else
  672. {
  673. //there is no match
  674. ret = -1;
  675. }
  676. return ret;
  677. }
  678. App_Wireless_Pair_State_e app_wireless_pair_get_curstate(void)
  679. {
  680. return ob_app_wireless_pair_fsm.curState;
  681. }