app_connect_manage.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "app_connect_manage.h"
  2. #include "bsp_time.h"
  3. #include "system.h"
  4. #include "ble_comm.h"
  5. #include "app_flash.h"
  6. #include "hal_charge.h"
  7. #include "app_ota.h"
  8. #include "app_game.h"
  9. #include "exception.h"
  10. #include "hal_ble_client.h"
  11. #include "hal_ble_host.h"
  12. #include "hal_led.h"
  13. #include "exception.h"
  14. #include "hal_mt.h"
  15. #include "hal_battery.h"
  16. #include "app_wireless_pair.h"
  17. /********************** 变量区 *************************/
  18. #define Ble_update_connted_Error 60//一次最多更新蓝牙连接间隔 次数
  19. /********************** 函数声明区 *************************/
  20. extern uint8_t app_client_step_GetIsScan(void);
  21. BLE_Host_Tx_t mBLE_Host_T_CONNET_R = {
  22. .n = 10,
  23. .t = 500,
  24. .cb = 0,
  25. };
  26. static uint16_t LedPlaytime =0;
  27. static uint32_t LedBlinktim =0;
  28. uint8_t app_conneted_getRight(void){
  29. uint8_t state =BLE_RIGHT_DISCONNTED;
  30. if('C' != mFlash.mClient.isConfig)
  31. state =BLE_RIGHT_NOCONFIG;
  32. else if(host_isconnect())
  33. state =BLE_RIGHT_CONNTED;
  34. #if BleNameHoldOn_ENANBLE
  35. if(host_isconnect())
  36. state =BLE_RIGHT_CONNTED;
  37. #endif
  38. return state;
  39. }
  40. void app_connect_LED_Process(void)
  41. {
  42. static uint8_t ResportCs_Flag = 0;
  43. static uint8_t state =0;
  44. static uint8_t host_state=0;
  45. switch(state){
  46. case 0:
  47. if(slave_isconnect()){
  48. state =1;
  49. ResportCs_Flag = 0;
  50. }
  51. break;
  52. case 1:
  53. if(host_isconnect()){
  54. if(0 == ResportCs_Flag){ResportCs_Flag = 1;
  55. host_state = app_conneted_getRight();
  56. BLE_Host_Tx_Send(&mBLE_Host_T_CONNET_R,BLE_CONNET_R,&host_state,1);
  57. DEBUG_LOG("====================>BLE_Host_T_CONNET_LED:%d\n",TIME_GetTicks());
  58. }
  59. }
  60. else if(ResportCs_Flag > 0) {ResportCs_Flag = 0;
  61. BLE_Host_Tx_Clear(&mBLE_Host_T_CONNET_R);
  62. }
  63. if(0 != LedPlaytime){
  64. if(TIME_GetTicks() - LedBlinktim >=LedPlaytime){LedPlaytime =0;
  65. LED_Stop(LED_OVERTURN);
  66. Process_SetHoldOn(app_connect_LED_Process,0);
  67. }
  68. }
  69. if(!slave_isconnect()){
  70. state =0;
  71. LedPlaytime =0;
  72. Process_SetHoldOn(app_connect_LED_Process,0);
  73. LED_Stop(LED_OVERTURN);
  74. BLE_Host_Tx_Clear(&mBLE_Host_T_CONNET_R);
  75. }
  76. break;
  77. default:state =0;LedPlaytime =0;LED_Stop(LED_OVERTURN);break;
  78. }
  79. }
  80. static void cb_BLE_BLINK_LED(void* handle)
  81. {
  82. BLE_Client_Rx_t* target = handle;
  83. uint8_t buf[2]={0};
  84. buf[0] = target->pDat[0];
  85. buf[1] = target->pDat[1];
  86. LedPlaytime = ((target->pDat[0]<<8) + target->pDat[1]);
  87. DEBUG_LOG("=======>cb_BLE_BLINK_LED,displaytime:%d\n",LedPlaytime);
  88. BLE_Host_Tx_Send(0,BLE_BLINK_LED,buf,2);
  89. Process_SetHoldOn(app_connect_LED_Process,1);
  90. LedBlinktim = TIME_GetTicks();
  91. LED_Start(LED_OVERTURN,COLOR_GREEN);
  92. }
  93. void cb_Slave_Connect(void)
  94. {
  95. DEBUG_LOG("=======>cb_Slave_Connect\n");
  96. if(mFlash.isHost) slave_update_conn_interval_request(30,30);
  97. }
  98. void cb_Host_Connect(void)
  99. {
  100. DEBUG_LOG("=======>cb_Host_Connect\n");
  101. }
  102. int App_Wireless_Pair_Set_Scan_Callback(int on_off)
  103. {
  104. return wireless_pair_scan_start(on_off);
  105. }
  106. int App_Wireless_Pair_Set_Adv_Callback(int on_off)
  107. {
  108. return wireless_pair_advertising_start(on_off);
  109. }
  110. void app_connect_Process(void)
  111. {
  112. static uint8_t state = 0;
  113. static uint32_t cnt = 0;
  114. static uint16_t update_temp = 0;
  115. static uint8_t ble_con_int = 0;//左右鞋蓝牙之间的连接间隔
  116. static uint8_t hostclientswitch =0;
  117. if(!mFlash.isHost){
  118. if(1 == hostclientswitch){hostclientswitch =0;
  119. state = 0;
  120. cnt = 0;
  121. update_temp = 0;
  122. ble_con_int = 0;
  123. Process_SetHoldOn(app_connect_Process,0);
  124. }
  125. if(hal_charge_Getstate()!=BLE_CHARGE_PULLOUT)
  126. {
  127. app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_CHARGING_INSERTING);//右鞋配对扫描
  128. }
  129. else if(hal_charge_Getstate()==BLE_CHARGE_PULLOUT)
  130. {
  131. app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_CHARGING_UNPLUG);//右鞋取消配对扫描
  132. }
  133. return;
  134. }
  135. if( 1 != hostclientswitch)hostclientswitch =1;
  136. switch(state){
  137. case 0:{
  138. if((app_client_step_GetIsScan()||slave_isconnect()||(hal_charge_Getstate()!=BLE_CHARGE_PULLOUT) ) && 0 == app_ota_host_state()){
  139. DEBUG_LOG("=======>scan_start:%d,%d\n",app_client_step_GetIsScan(),slave_isconnect());
  140. Process_SetHoldOn(app_connect_Process,1);
  141. scan_start();
  142. cnt = 0;
  143. state = 1;
  144. }
  145. if(mFlash.isHost && 0 == slave_isconnect()){
  146. if(host_isconnect())host_disconnect();
  147. }
  148. break;}
  149. case 1:{
  150. if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(hal_charge_Getstate()==BLE_CHARGE_PULLOUT)){
  151. Except_TxError(EXCEPT_DFU,"adv disconnted over time");
  152. DEBUG_LOG("=======>nrf_ble_scan_stop\n");
  153. nrf_ble_scan_stop();
  154. Process_SetHoldOn(app_connect_Process,0);
  155. state = 0;
  156. }else if(host_isconnect()){
  157. DEBUG_LOG("=======>host_isconnect\n");
  158. Process_SetHoldOn(app_connect_Process,0);
  159. if(mFlash.isHost){
  160. update_temp =0;
  161. ble_con_int = 0;
  162. }
  163. state =2;
  164. }
  165. if(++cnt>=100){ if(0 == app_client_step_GetIsScan() && 'C' == mFlash.mClient.isConfig)Except_TxError(EXCEPT_CONNET,"scan right shoes over time");
  166. DEBUG_LOG("=======>app_err_Set nrf_ble_scan_stop\n");
  167. nrf_ble_scan_stop();
  168. Process_SetHoldOn(app_connect_Process,0);
  169. state = 0;
  170. }
  171. break;}
  172. case 2:{
  173. if(app_game_GetGameMode()){//游戏模式下更新连接间隔
  174. if(ble_con_int !=2 && update_temp <= Ble_update_connted_Error){
  175. if(APP_SUCCESS == Ble_update_conn_interval(7.5,7.5)){
  176. ble_con_int =2;
  177. update_temp =0;
  178. }
  179. else {
  180. if(Ble_update_connted_Error == update_temp)Except_TxError(EXCEPT_CONNET,"update Ble conn interval 7.5ms fail");
  181. ++update_temp;
  182. }
  183. }
  184. }else{
  185. if(ble_con_int !=1 && update_temp <= Ble_update_connted_Error){
  186. if(APP_SUCCESS == Ble_update_conn_interval(100,100)){
  187. ble_con_int =1;
  188. update_temp =0;
  189. }
  190. else {
  191. if(Ble_update_connted_Error == update_temp)Except_TxError(EXCEPT_CONNET,"update Ble conn interval 100ms fail");
  192. update_temp++;
  193. }
  194. }
  195. }
  196. if(app_client_step_GetIsScan()==0 && slave_isconnect()==0 && (hal_charge_Getstate()==BLE_CHARGE_PULLOUT)){
  197. if(host_isconnect()){
  198. // DEBUG_LOG("=======>host_isconnect 112233\n");
  199. host_disconnect();
  200. }
  201. }
  202. if(!host_isconnect()){
  203. DEBUG_LOG("=======>host_disconnect\n");
  204. Process_SetHoldOn(app_connect_Process,0);
  205. state = 0;
  206. }
  207. break;}
  208. default:state=0;break;
  209. }
  210. if(hal_charge_Getstate()!=BLE_CHARGE_PULLOUT)
  211. {
  212. app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_CHARGING_INSERTING);//左鞋配对广播
  213. }
  214. else if(hal_charge_Getstate()==BLE_CHARGE_PULLOUT)
  215. {
  216. app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_CHARGING_UNPLUG);//左鞋取消配对广播
  217. }
  218. }
  219. static void cb_Client_BLE_CONNET_R(void* handle)
  220. {
  221. uint8_t host_state=BLE_RIGHT_DISCONNTED;
  222. host_state =app_conneted_getRight();
  223. if(mFlash.isHost){
  224. if(BLE_RIGHT_CONNTED == host_state){
  225. BLE_Host_Tx_Send(0,BLE_CONNET_R,&host_state,1);
  226. }
  227. else BLE_Client_Tx_Send(0,BLE_CONNET_R,&host_state,1);
  228. }
  229. else {
  230. host_state=BLE_RIGHT_CONNTED;
  231. BLE_Client_Tx_Send(0,BLE_CONNET_R,&host_state,1);
  232. }
  233. }
  234. static void cb_Host_BLE_CONNET_R(void* handle)
  235. {
  236. uint8_t host_state=BLE_RIGHT_CONNTED;
  237. BLE_Client_Tx_Send(0,BLE_CONNET_R,&host_state,1);
  238. }
  239. void app_connect_manage_Init(void)
  240. {
  241. Process_Start(100,"connect",app_connect_Process);
  242. Ble_Slave_Connectd_Evt_Regist(cb_Slave_Connect);
  243. Ble_Host_Connectd_Evt_Regist(cb_Host_Connect);
  244. Process_Start(100,"connect_led",app_connect_LED_Process);
  245. BLE_Client_Rx_Regist(BLE_CONNET_R,cb_Client_BLE_CONNET_R);
  246. BLE_Host_Rx_Regist(BLE_CONNET_R,cb_Host_BLE_CONNET_R);
  247. BLE_Client_Rx_Regist(BLE_BLINK_LED,cb_BLE_BLINK_LED);
  248. app_wireless_pair_Regist_SetScan(App_Wireless_Pair_Set_Scan_Callback);
  249. app_wireless_pair_Regist_SetAdv(App_Wireless_Pair_Set_Adv_Callback);
  250. }