app_connect_manage.c 6.7 KB

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