app_wireless_pair.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 "app_flash.h"
  9. #include "app_wireless_pair.h"
  10. /*Private macro ------------------------------------------------------------------------------------------------------------------------------------*/
  11. #define APP_WIRELESS_PAIR_FSMTABLE_LEN 8 //状态表长度
  12. #define APP_WIRELESS_PAIR_RSSI_THRESHOLD -18 //rssi阈值
  13. /*Struction ------------------------------------------------------------------------------------------------------------------------------------*/
  14. //定义状态数据的枚举类型
  15. typedef enum _app_wireless_pair_state
  16. {
  17. APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY, //配对待机状态
  18. APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION, //配对准备中状态
  19. APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS, //配对进行中状态
  20. APP_WIRELESS_PAIR_STATE_END_OF_PAIRING, //配对结束状态
  21. }App_Wireless_Pair_State_e;
  22. //定义状态表的数据类型
  23. typedef struct _app_wireless_pair_fsmtable_s
  24. {
  25. App_Wireless_Pair_Event_e event; //事件
  26. App_Wireless_Pair_State_e CurState; //当前状态
  27. int (*eventActFun)(); //函数指针
  28. App_Wireless_Pair_State_e NextState; //下一个状态
  29. }App_Wireless_Pair_fsmtable_s;
  30. //定义状态机类型
  31. typedef struct _app_wireless_pair_fsm_s
  32. {
  33. App_Wireless_Pair_State_e curState; //当前状态
  34. App_Wireless_Pair_fsmtable_s FsmTable[APP_WIRELESS_PAIR_FSMTABLE_LEN]; //状态表
  35. App_Wireless_Pair_Set_Scan_Cb set_scan_cb; //设置扫描回调
  36. App_Wireless_Pair_Set_Adv_Cb set_adv_cb; //设置广播回调
  37. uint8_t peer_addr[6]; //对端MAC地址
  38. bool is_busy; //是否忙碌
  39. uint8_t led_display_count; //亮灯次数
  40. uint8_t led_switch; //灯的开关
  41. char already_scan_or_adv_mac_buf[16]; //已经在扫描或广播的mac地址
  42. }App_Wireless_Pair_Fsm_s;
  43. /*Local Variable ------------------------------------------------------------------------------------------------------------------------------------*/
  44. static App_Wireless_Pair_Fsm_s ob_app_wireless_pair_fsm;
  45. /*Local Functions ------------------------------------------------------------------------------------------------------------------------------------*/
  46. static int app_wireless_pair_set_scan_adv_on(void)
  47. {
  48. int ret = 0;
  49. //若是右鞋则进入主动扫描状态
  50. //若是左鞋则设置扫描回应为MAC地址状态
  51. if(mFlash.isHost)
  52. {
  53. advertising_stop();
  54. nrf_ble_scan_stop();
  55. host_disconnect();
  56. if(host_isconnect() == 0)
  57. {
  58. if(ob_app_wireless_pair_fsm.set_adv_cb != NULL)ret = ob_app_wireless_pair_fsm.set_adv_cb(0x01);
  59. else ret = -1;
  60. if(ob_app_wireless_pair_fsm.set_scan_cb != NULL)ret = ob_app_wireless_pair_fsm.set_scan_cb(0x00);
  61. else ret = -1;
  62. }
  63. else
  64. {
  65. ret = -1;
  66. }
  67. }
  68. else
  69. {
  70. advertising_stop();
  71. nrf_ble_scan_stop();
  72. slave_disconnect();
  73. if(slave_isconnect() == 0)
  74. {
  75. if(ob_app_wireless_pair_fsm.set_adv_cb != NULL)ret = ob_app_wireless_pair_fsm.set_adv_cb(0x00);
  76. else ret = -1;
  77. if(ob_app_wireless_pair_fsm.set_scan_cb != NULL)ret = ob_app_wireless_pair_fsm.set_scan_cb(0x01);
  78. else ret = -1;
  79. }
  80. else
  81. {
  82. ret = -1;
  83. }
  84. }
  85. return ret;
  86. }
  87. static int app_wireless_pair_set_scan_adv_off(void)
  88. {
  89. int ret = 0;
  90. char buf[16];
  91. //若是右鞋则进入被动扫描状态
  92. //若是左鞋则设置扫描回应为空数据状态
  93. if(mFlash.isHost)
  94. {
  95. advertising_stop();
  96. nrf_ble_scan_stop();
  97. host_disconnect();
  98. if(host_isconnect() == 0)
  99. {
  100. if(ob_app_wireless_pair_fsm.set_adv_cb != NULL)ret = ob_app_wireless_pair_fsm.set_adv_cb(0x00);
  101. else ret = -1;
  102. if(ob_app_wireless_pair_fsm.set_scan_cb != NULL)
  103. {
  104. 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]);
  105. host_set_scan_name(buf,strlen(buf));
  106. ret = ob_app_wireless_pair_fsm.set_scan_cb(0x00);
  107. }
  108. else ret = -1;
  109. }
  110. else
  111. {
  112. ret = -1;
  113. }
  114. }
  115. else
  116. {
  117. advertising_stop();
  118. nrf_ble_scan_stop();
  119. slave_disconnect();
  120. if(slave_isconnect() == 0)
  121. {
  122. if(ob_app_wireless_pair_fsm.set_adv_cb != NULL)
  123. {
  124. 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]);
  125. slave_set_adv_name(buf,strlen(buf));
  126. slave_adv_init();
  127. ret = ob_app_wireless_pair_fsm.set_adv_cb(0x00);
  128. }
  129. else ret = -1;
  130. }
  131. else
  132. {
  133. ret = -1;
  134. }
  135. }
  136. return ret;
  137. }
  138. static int app_wireless_pair_unconditional_entry(void)
  139. {
  140. return 0; //不用做任何操作,直接进入下一个状态
  141. }
  142. static void Bubble_sort(int arr[], int size)
  143. {
  144. int j,i,tem;
  145. for (i = 0; i < size-1;i ++)//size-1是因为不用与自己比较,所以比的数就少一个
  146. {
  147. int count = 0;
  148. for (j = 0; j < size-1 - i; j++) //size-1-i是因为每一趟就会少一个数比较
  149. {
  150. if (arr[j] > arr[j+1])//这是升序排法,前一个数和后一个数比较,如果前数大则与后一个数换位置
  151. {
  152. tem = arr[j];
  153. arr[j] = arr[j+1];
  154. arr[j+1] = tem;
  155. count = 1;
  156. }
  157. }
  158. if (count == 0) //如果某一趟没有交换位置,则说明已经排好序,直接退出循环
  159. break;
  160. //值得注意的是函数中的定义的count是为了检验是否进行了元素的交换,是对代码的一种优化
  161. }
  162. }
  163. static int app_wireless_pair_enter_in_process(int rssi, uint8_t *addr)
  164. {
  165. #define N 15
  166. static int rssi_buf[N], rssi_filter_buf[N];
  167. static uint8_t index = 0;
  168. int ret = 0;
  169. uint8_t null_addr[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
  170. if(ob_app_wireless_pair_fsm.is_busy == false)
  171. {
  172. rssi_buf[index++] = rssi;
  173. if(index == N)index = 0;
  174. memcpy(rssi_filter_buf,rssi_buf,sizeof(rssi_buf));
  175. Bubble_sort(rssi_filter_buf, N);
  176. // SEGGER_RTT_printf(0,"rssi:%d,st:%d,curState:%d,active:%d\r\n",rssi_filter_buf[N-1],NRF_RTC0->COUNTER, \
  177. // ob_app_wireless_pair_fsm.curState,wireless_pair_scan_get_active());
  178. if(rssi_filter_buf[N-1] >= APP_WIRELESS_PAIR_RSSI_THRESHOLD)
  179. {
  180. if(memcmp(addr,null_addr,6) != 0)
  181. {
  182. ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
  183. for(int i=0; i<6; i++)ob_app_wireless_pair_fsm.peer_addr[i] = addr[5-i];
  184. }
  185. else
  186. {
  187. ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS);
  188. }
  189. }
  190. else
  191. {
  192. ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS);
  193. }
  194. }
  195. return ret;
  196. }
  197. static void display_led_process(void)
  198. {
  199. ob_app_wireless_pair_fsm.led_display_count++;
  200. if (ob_app_wireless_pair_fsm.led_display_count % 2 == 0)
  201. {
  202. LED_Start(LED_PAIR, COLOR_BLACK);
  203. }
  204. else
  205. {
  206. LED_Start(LED_PAIR, COLOR_BLUE);
  207. }
  208. if (ob_app_wireless_pair_fsm.led_display_count > 20)
  209. {
  210. ob_app_wireless_pair_fsm.led_display_count = 0;
  211. LED_Stop(LED_PAIR);
  212. Process_SetHoldOn(display_led_process, 0);
  213. Process_Stop(display_led_process);
  214. }
  215. }
  216. static void app_wireless_pair_start_display_led(void)
  217. {
  218. ob_app_wireless_pair_fsm.led_display_count = 0;
  219. Process_Start(500, "display_led_process", display_led_process);
  220. Process_SetHoldOn(display_led_process, 1);
  221. }
  222. static void app_wireless_pair_stop_display_led(void)
  223. {
  224. ob_app_wireless_pair_fsm.led_display_count = 0;
  225. LED_Stop(LED_PAIR);
  226. Process_SetHoldOn(display_led_process, 0);
  227. Process_Stop(display_led_process);
  228. }
  229. static void app_wireless_pair_in_process(void)
  230. {
  231. int ret = 0;
  232. uint8_t *connect_addr;
  233. uint8_t peer_addr[6]; //对端mac地址
  234. ble_gap_addr_t m_addr; //自身mac地址
  235. char buf[16];
  236. uint32_t err_code;
  237. if(ob_app_wireless_pair_fsm.curState == APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS)
  238. {
  239. ob_app_wireless_pair_fsm.is_busy = true;
  240. //获取已连接的对端mac地址
  241. connect_addr = wireless_pair_get_connect_macaddr();
  242. //获取自身mac地址
  243. err_code = sd_ble_gap_addr_get(&m_addr);
  244. APP_ERROR_CHECK(err_code);
  245. //获取对端的mac地址
  246. for(int i=0;i<6;i++)peer_addr[i]=ob_app_wireless_pair_fsm.peer_addr[i];
  247. if(mFlash.isHost) //左鞋处理逻辑
  248. {
  249. //判断是否处于连接且连接的mac地址为配对地址
  250. if(host_isconnect() == 1 && memcmp(peer_addr,connect_addr,6) == 0)
  251. {
  252. //判断是否配对过,若已配对过,直接进入配对结束阶段。若无配对过,则记录MAC地址,清除步数,进入配对结束。
  253. if(memcmp(peer_addr,mFlash.mClient.macAddr,6) != 0)
  254. {
  255. for(int i = 0; i < 6; i++)
  256. {
  257. mFlash.mClient.macAddr[i] = peer_addr[i];
  258. mFlash.macHost[i] = m_addr.addr[5-i];
  259. mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
  260. mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i]; //从机地址
  261. }
  262. mFlash.mClient.isConfig ='C';
  263. mBackup.hardVersion = mFlash.mClient.hardVersion;
  264. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  265. mBackup.isConfig = mFlash.mClient.isConfig;
  266. if(Flash_SaveBackup() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "pair save backup fail");}
  267. if(Flash_DeleteAllStep() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "pair clear step fail");}
  268. memset(&mFlash.mStep, 0, sizeof(FlashStep_t));
  269. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "Flash_SaveInfomation fail");}
  270. }
  271. app_wireless_pair_start_led();
  272. app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_PAIR_DONE);
  273. }
  274. else
  275. {
  276. //若不是,断开连接,设置扫描为新的配对地址
  277. 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]);
  278. if(memcmp(ob_app_wireless_pair_fsm.already_scan_or_adv_mac_buf,buf,strlen(buf)) != 0)
  279. {
  280. advertising_stop();
  281. nrf_ble_scan_stop();
  282. host_disconnect();
  283. if(host_isconnect() == 0)
  284. {
  285. ret += ob_app_wireless_pair_fsm.set_adv_cb(0x01);
  286. host_set_scan_name(buf,strlen(buf));
  287. ret += ob_app_wireless_pair_fsm.set_scan_cb(0x02);
  288. if(ret == 0)memcpy(ob_app_wireless_pair_fsm.already_scan_or_adv_mac_buf,buf,strlen(buf));
  289. }
  290. }
  291. }
  292. }
  293. else //右鞋处理逻辑
  294. {
  295. //判断是否处于连接且连接的mac地址为配对地址
  296. if(slave_isconnect() == 1 && memcmp(peer_addr,connect_addr,6) == 0)
  297. {
  298. //判断是否配对过,若已配对过,直接进入配对结束阶段。若无配对过,则记录MAC地址,清除步数,进入配对结束。
  299. if(memcmp(peer_addr,mFlash.mClient.macAddr,6) != 0)
  300. {
  301. for(int i = 0; i < 6; i++)
  302. {
  303. mFlash.mClient.macAddr[i] = peer_addr[i];
  304. mFlash.macHost[i] = m_addr.addr[5-i];
  305. mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
  306. mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i]; //从机地址
  307. }
  308. mFlash.mClient.isConfig ='C';
  309. mBackup.hardVersion = mFlash.mClient.hardVersion;
  310. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  311. mBackup.isConfig = mFlash.mClient.isConfig;
  312. if(Flash_SaveBackup() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "pair save backup fail");}
  313. if(Flash_DeleteAllStep() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "pair clear step fail");}
  314. memset(&mFlash.mStep, 0, sizeof(FlashStep_t));
  315. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "Flash_SaveInfomation fail");}
  316. }
  317. app_wireless_pair_start_led();
  318. app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_PAIR_DONE);
  319. }
  320. else
  321. {
  322. //若不是,断开连接,设置广播为新的配对地址
  323. 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]);
  324. if(memcmp(ob_app_wireless_pair_fsm.already_scan_or_adv_mac_buf,buf,strlen(buf)) != 0)
  325. {
  326. advertising_stop();
  327. nrf_ble_scan_stop();
  328. slave_disconnect();
  329. if(slave_isconnect() == 0)
  330. {
  331. ret += ob_app_wireless_pair_fsm.set_scan_cb(0x01);
  332. slave_set_adv_name(buf,strlen(buf));
  333. slave_adv_init();
  334. ret += ob_app_wireless_pair_fsm.set_adv_cb(0x02);
  335. if(ret == 0)memcpy(ob_app_wireless_pair_fsm.already_scan_or_adv_mac_buf,buf,strlen(buf));
  336. }
  337. }
  338. }
  339. }
  340. ob_app_wireless_pair_fsm.is_busy = false;
  341. }
  342. }
  343. /*API ------------------------------------------------------------------------------------------------------------------------------------*/
  344. /**
  345. @brief 初始化无线配对应用
  346. @param 无
  347. @return 错误代码 - [out] -1失败,0成功
  348. */
  349. int app_wireless_pair_Init(void)
  350. {
  351. //初始化结构体
  352. // memset(&ob_app_wireless_pair_fsm,0,sizeof(App_Wireless_Pair_Fsm_s)); //不能meset初始化,因为回调函数会被清除。
  353. ob_app_wireless_pair_fsm.curState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY;
  354. ob_app_wireless_pair_fsm.is_busy = false;
  355. ob_app_wireless_pair_fsm.led_switch = 0x01;
  356. //{到来的事件,当前的状态,将要要执行的函数,下一个状态}
  357. ob_app_wireless_pair_fsm.FsmTable[0].event = APP_WIRELESS_PAIR_EVENT_CHARGING_INSERTING; //充电插入
  358. ob_app_wireless_pair_fsm.FsmTable[0].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY; //配对待机状态
  359. ob_app_wireless_pair_fsm.FsmTable[0].eventActFun = app_wireless_pair_set_scan_adv_on; //设置扫描和广播开
  360. ob_app_wireless_pair_fsm.FsmTable[0].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
  361. ob_app_wireless_pair_fsm.FsmTable[1].event = APP_WIRELESS_PAIR_EVENT_CHARGING_UNPLUG; //充电拔出
  362. ob_app_wireless_pair_fsm.FsmTable[1].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
  363. ob_app_wireless_pair_fsm.FsmTable[1].eventActFun = app_wireless_pair_set_scan_adv_off; //设置扫描和广播关
  364. ob_app_wireless_pair_fsm.FsmTable[1].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY; //配对待机状态
  365. ob_app_wireless_pair_fsm.FsmTable[2].event = APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS; //rssi符合要求
  366. ob_app_wireless_pair_fsm.FsmTable[2].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
  367. ob_app_wireless_pair_fsm.FsmTable[2].eventActFun = app_wireless_pair_unconditional_entry; //不操作,直接进入下一个状态
  368. ob_app_wireless_pair_fsm.FsmTable[2].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS; //配对进行中状态
  369. ob_app_wireless_pair_fsm.FsmTable[3].event = APP_WIRELESS_PAIR_EVENT_CHARGING_UNPLUG; //充电拔出
  370. ob_app_wireless_pair_fsm.FsmTable[3].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS; //配对进行中状态
  371. ob_app_wireless_pair_fsm.FsmTable[3].eventActFun = app_wireless_pair_set_scan_adv_off; //设置扫描和广播关
  372. ob_app_wireless_pair_fsm.FsmTable[3].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY; //配对待机状态
  373. ob_app_wireless_pair_fsm.FsmTable[4].event = APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS; //rssi不符合要求
  374. ob_app_wireless_pair_fsm.FsmTable[4].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS; //配对进行中状态
  375. ob_app_wireless_pair_fsm.FsmTable[4].eventActFun = app_wireless_pair_unconditional_entry; //不操作,直接进入下一个状态
  376. ob_app_wireless_pair_fsm.FsmTable[4].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
  377. ob_app_wireless_pair_fsm.FsmTable[5].event = APP_WIRELESS_PAIR_EVENT_PAIR_DONE; //配对完成
  378. ob_app_wireless_pair_fsm.FsmTable[5].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS; //配对进行中状态
  379. ob_app_wireless_pair_fsm.FsmTable[5].eventActFun = app_wireless_pair_unconditional_entry; //不操作,直接进入下一个状态
  380. ob_app_wireless_pair_fsm.FsmTable[5].NextState = APP_WIRELESS_PAIR_STATE_END_OF_PAIRING; //配对结束状态
  381. ob_app_wireless_pair_fsm.FsmTable[6].event = APP_WIRELESS_PAIR_EVENT_CHARGING_UNPLUG; //充电拔出
  382. ob_app_wireless_pair_fsm.FsmTable[6].CurState = APP_WIRELESS_PAIR_STATE_END_OF_PAIRING; //配对结束状态
  383. ob_app_wireless_pair_fsm.FsmTable[6].eventActFun = app_wireless_pair_set_scan_adv_off; //设置扫描和广播关
  384. ob_app_wireless_pair_fsm.FsmTable[6].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY; //配对待机状态
  385. ob_app_wireless_pair_fsm.FsmTable[7].event = APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS; //rssi不符合要求
  386. ob_app_wireless_pair_fsm.FsmTable[7].CurState = APP_WIRELESS_PAIR_STATE_END_OF_PAIRING; //配对结束状态
  387. ob_app_wireless_pair_fsm.FsmTable[7].eventActFun = app_wireless_pair_unconditional_entry; //不操作,直接进入下一个状态
  388. ob_app_wireless_pair_fsm.FsmTable[7].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
  389. Process_Start(0,"app_wireless_pair_in_process",app_wireless_pair_in_process);
  390. return 0;
  391. }
  392. /**
  393. @brief 无线配对注册扫描设置
  394. @param cb - [in] 注册的回调
  395. @return 错误代码 - [out] -1失败,0成功
  396. */
  397. int app_wireless_pair_Regist_SetScan(App_Wireless_Pair_Set_Scan_Cb cb)
  398. {
  399. if(cb != NULL)
  400. {
  401. ob_app_wireless_pair_fsm.set_scan_cb = cb;
  402. return 0;
  403. }
  404. return -1;
  405. }
  406. /**
  407. @brief 无线配对注册广播设置
  408. @param cb - [in] 注册的回调
  409. @return 错误代码 - [out] -1失败,0成功
  410. */
  411. int app_wireless_pair_Regist_SetAdv(App_Wireless_Pair_Set_Adv_Cb cb)
  412. {
  413. if(cb != NULL)
  414. {
  415. ob_app_wireless_pair_fsm.set_adv_cb = cb;
  416. return 0;
  417. }
  418. return -1;
  419. }
  420. /**
  421. @brief 无线配对扫描请求
  422. @param rssi - [in] 天线信号强度
  423. @param addr - [in] MAC地址缓冲区指针
  424. @return 错误代码 - [out] -1失败,0成功
  425. */
  426. int app_wireless_pair_Scan_Req(int rssi, uint8_t *addr)
  427. {
  428. int ret = 0;
  429. if(mFlash.isHost)
  430. {
  431. app_wireless_pair_enter_in_process(rssi,addr);
  432. }
  433. else
  434. {
  435. ret = -1; //只有左鞋收到右鞋的扫描请求才对
  436. }
  437. return ret;
  438. }
  439. /**
  440. @brief 无线配对扫描回复
  441. @param rssi - [in] 天线信号强度
  442. @param addr - [in] MAC地址缓冲区指针
  443. @return 错误代码 - [out] -1失败,0成功
  444. */
  445. int app_wireless_pair_Scan_Rsp(int rssi, uint8_t *addr)
  446. {
  447. int ret = 0;
  448. if(!mFlash.isHost)
  449. {
  450. app_wireless_pair_enter_in_process(rssi,addr);
  451. }
  452. else
  453. {
  454. ret = -1; //只有右鞋收到左鞋的扫描回应才对
  455. }
  456. return ret;
  457. }
  458. /**
  459. @brief 无线配对亮灯
  460. @param 无
  461. @return 错误代码 - [out] -1失败,0成功
  462. */
  463. int app_wireless_pair_start_led(void)
  464. {
  465. // SEGGER_RTT_printf(0,"start: curState:%d\r\n",ob_app_wireless_pair_fsm.curState);
  466. if(ob_app_wireless_pair_fsm.led_switch != 0 && (ob_app_wireless_pair_fsm.curState == APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS))
  467. {
  468. ob_app_wireless_pair_fsm.led_switch--;
  469. app_wireless_pair_start_display_led();
  470. // SEGGER_RTT_printf(0,"app_wireless_pair_start_display_led\r\n");
  471. }
  472. return 0;
  473. }
  474. /**
  475. @brief 无线配对灭灯
  476. @param 无
  477. @return 错误代码 - [out] -1失败,0成功
  478. */
  479. int app_wireless_pair_stop_led(void)
  480. {
  481. // SEGGER_RTT_printf(0,"stop: curState:%d\r\n",ob_app_wireless_pair_fsm.curState);
  482. app_wireless_pair_stop_display_led();
  483. ob_app_wireless_pair_fsm.led_switch = 1;
  484. app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS);
  485. // SEGGER_RTT_printf(0,"app_wireless_pair_stop_display_led\r\n");
  486. return 0;
  487. }
  488. /**
  489. @brief 无线配对事件处理
  490. @param event - [in] 事件
  491. @return 错误代码 - [out] -1失败,0成功
  492. */
  493. int app_wireless_pair_EventHandle(App_Wireless_Pair_Event_e event)
  494. {
  495. int (*eventActFun)() = NULL; //函数指针初始化为空
  496. App_Wireless_Pair_State_e NextState;
  497. int flag = 0; //标识是否满足条件
  498. int i;
  499. int ret = 0;
  500. /*获取当前动作函数*/
  501. for (i = 0; i<APP_WIRELESS_PAIR_FSMTABLE_LEN; i++)
  502. {
  503. //当且仅当当前状态下来个指定的事件,我才执行它
  504. if (event == ob_app_wireless_pair_fsm.FsmTable[i].event && ob_app_wireless_pair_fsm.curState == ob_app_wireless_pair_fsm.FsmTable[i].CurState)
  505. {
  506. flag = 1;
  507. eventActFun = ob_app_wireless_pair_fsm.FsmTable[i].eventActFun;
  508. NextState = ob_app_wireless_pair_fsm.FsmTable[i].NextState;
  509. break;
  510. }
  511. }
  512. if (flag) //如果满足条件了
  513. {
  514. /*动作执行*/
  515. if(eventActFun)
  516. {
  517. ret = eventActFun();
  518. }
  519. //跳转到下一个状态
  520. if(ret == 0)
  521. {
  522. ob_app_wireless_pair_fsm.curState = NextState;
  523. }
  524. }
  525. else
  526. {
  527. //there is no match
  528. ret = -1;
  529. }
  530. return ret;
  531. }