app_Authen.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 =0;
  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. }
  56. if('C' == mFlash.mClient.isConfig && LR_FLAG_VALUE != mFlash.LR_FLAG && 1 == Phoneflag){
  57. app_detect_LR_process_stop();
  58. mFlash.LR_FLAG = LR_FLAG_VALUE;
  59. Flash_SaveInfomation();
  60. mBackup.isHost= mFlash.isHost;
  61. mBackup.LR_FLAG = mFlash.LR_FLAG;
  62. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_FLASH,"app_detect_LR save backup fail");
  63. }
  64. }
  65. break;
  66. case 1:
  67. if(0 == NowClient_state){state =3;}
  68. else if(1 == Phoneflag){
  69. state =2;
  70. if('C' == mFlash.mClient.isConfig && LR_FLAG_VALUE != mFlash.LR_FLAG){
  71. app_detect_LR_process_stop();
  72. mFlash.LR_FLAG = LR_FLAG_VALUE;
  73. Flash_SaveInfomation();
  74. mBackup.isHost= mFlash.isHost;
  75. mBackup.LR_FLAG = mFlash.LR_FLAG;
  76. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_FLASH,"app_detect_LR save backup fail");
  77. }
  78. }
  79. else if(TIME_GetTicks() - tim >= 15000){
  80. if(NowClient_state)slave_disconnect();
  81. else state =3;
  82. DEBUG_LOG("AUTHEN over time\r\n");
  83. }
  84. break;
  85. case 2:
  86. if(0 == NowClient_state){state =3;}
  87. break;
  88. case 3:
  89. default:state=0;Phoneflag =0;break;
  90. }
  91. LastClient_state = NowClient_state;
  92. }
  93. void app_Authen_Init(void)
  94. {
  95. Process_Start(1000,"app_Authon",app_Authen_Process);
  96. BLE_Client_Rx_Regist(BLE_AUTHEN,cb_BLE_Client_BLE_AUTHEN);
  97. DEBUG_LOG("BLE_Client_Rx_Regist %02x\r\n",BLE_AUTHEN);
  98. BLE_Host_Rx_Regist(BLE_AUTHEN,cb_BLE_Host_BLE_AUTHEN);
  99. }