app_Authen.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "usr_config.h"
  2. #include "app_Authen.h"
  3. #include "system.h"
  4. #include "hal_ble_client.h"
  5. #include "hal_ble_host.h"
  6. #include "bsp_time.h"
  7. #include "ble_comm.h"
  8. #include "bll_imu.h"
  9. #include "app_flash.h"
  10. #include "app_connect_manage.h"
  11. #include "app_detectIsHost.h"
  12. /************************ º¯ÊýÉùÃ÷ ***********************************/
  13. static BLE_Host_Tx_t m_Host_BLE_AUTHEN = {
  14. .n = 0,
  15. .t = 300,
  16. };
  17. static uint8_t Phoneflag =0;
  18. static uint8_t HostAutodat =1;
  19. static void cb_BLE_Client_BLE_AUTHEN(void* handle){
  20. DEBUG_LOG("Get cb_BLE_Client_BLE_AUTHEN\r\n");
  21. if(m_Host_BLE_AUTHEN.n <= 1){
  22. m_Host_BLE_AUTHEN.n =10;
  23. BLE_Host_Tx_Send(&m_Host_BLE_AUTHEN,BLE_AUTHEN,&HostAutodat,1);
  24. }
  25. else BLE_Host_Tx_Send(0,BLE_AUTHEN,&HostAutodat,1);
  26. Phoneflag =1;
  27. }
  28. static void cb_BLE_Host_BLE_AUTHEN(void* handle){
  29. BLE_Host_Rx_t* target = handle;
  30. BLE_Client_Tx_Send(0,BLE_AUTHEN,target->pDat,target->datLen);
  31. }
  32. static void app_Authen_Process(void){
  33. static uint8_t state =0;
  34. uint8_t NowClient_state =0;
  35. static uint8_t LastClient_state =1;
  36. static uint32_t tim =0;
  37. if(slave_isconnect())NowClient_state = 1;
  38. else NowClient_state = 0;
  39. if(mFlash.isHost){
  40. if(0 == NowClient_state){
  41. if(BLE_RIGHT_CONNTED == app_conneted_getRight()){
  42. if(1 == LastClient_state){
  43. DEBUG_LOG("app_conneted_getRight>>>>>>>>\r\n");
  44. BLE_Host_Tx_Send(&m_Host_BLE_AUTHEN,BLE_AUTHEN,&HostAutodat,1);
  45. }
  46. }
  47. }
  48. }
  49. switch(state){
  50. case 0:
  51. if(NowClient_state){
  52. if(0 == LastClient_state){
  53. state =1;
  54. tim = TIME_GetTicks();
  55. DEBUG_LOG("AUTHEN start:%d\r\n",tim);
  56. }
  57. if('C' == mFlash.mClient.isConfig && LR_FLAG_VALUE != mFlash.LR_FLAG && 1 == Phoneflag){
  58. app_detect_LR_process_stop();
  59. mFlash.LR_FLAG = LR_FLAG_VALUE;
  60. Flash_SaveInfomation();
  61. mBackup.isHost= mFlash.isHost;
  62. mBackup.LR_FLAG = mFlash.LR_FLAG;
  63. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_FLASH,"app_detect_LR save backup fail");
  64. }
  65. }
  66. break;
  67. case 1:
  68. if(0 == NowClient_state){state =3;}
  69. else if(1 == Phoneflag){
  70. state =2;
  71. if('C' == mFlash.mClient.isConfig && LR_FLAG_VALUE != mFlash.LR_FLAG){
  72. app_detect_LR_process_stop();
  73. mFlash.LR_FLAG = LR_FLAG_VALUE;
  74. Flash_SaveInfomation();
  75. mBackup.isHost= mFlash.isHost;
  76. mBackup.LR_FLAG = mFlash.LR_FLAG;
  77. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_FLASH,"app_detect_LR save backup fail");
  78. }
  79. }
  80. else if(TIME_GetTicks() - tim >= 15000){
  81. if(NowClient_state)slave_disconnect();
  82. state =3;
  83. DEBUG_LOG("AUTHEN over time:%d\r\n",TIME_GetTicks());
  84. }
  85. break;
  86. case 2:
  87. if(0 == NowClient_state){state =3;}
  88. break;
  89. case 3:
  90. default:state=0;Phoneflag =0;break;
  91. }
  92. LastClient_state = NowClient_state;
  93. }
  94. void app_Authen_Init(void)
  95. {
  96. Process_Start(100,"app_Authon",app_Authen_Process);
  97. BLE_Client_Rx_Regist(BLE_AUTHEN,cb_BLE_Client_BLE_AUTHEN);
  98. DEBUG_LOG("BLE_Client_Rx_Regist %02x\r\n",BLE_AUTHEN);
  99. BLE_Host_Rx_Regist(BLE_AUTHEN,cb_BLE_Host_BLE_AUTHEN);
  100. }