app_connect_manage.c 6.8 KB

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