app_ota.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "app_ota.h"
  2. #include "nrf_gpio.h"
  3. #include "usr_config.h"
  4. #include "bsp_time.h"
  5. #include "system.h"
  6. #include "hal_mt.h"
  7. #include "app_host.h"
  8. #include "app_charge.h"
  9. #include "hal_ble_client.h"
  10. #include "hal_ble_host.h"
  11. #include "nrf_delay.h"
  12. #include "hal_flash.h"
  13. #include "ble_comm.h"
  14. #include "hal_soft_time.h"
  15. static ble_gap_addr_t mAddr;
  16. static uint32_t isOTA=0;
  17. static uint8_t isHostOTA=0;
  18. static volatile uint8_t m_ready_for_reset = 0;
  19. static bool app_shutdown_handler(nrf_pwr_mgmt_evt_t event)
  20. {
  21. switch (event){
  22. case NRF_PWR_MGMT_EVT_PREPARE_DFU:
  23. switch(m_ready_for_reset){
  24. case 0:
  25. m_ready_for_reset =1;
  26. return false;
  27. case 1:
  28. return false;
  29. default:
  30. break;
  31. }
  32. break;
  33. default:
  34. return true;
  35. }
  36. SEGGER_RTT_printf(0,"Into bootloader\n");
  37. return true;
  38. }
  39. NRF_PWR_MGMT_HANDLER_REGISTER(app_shutdown_handler, 0);
  40. static void app_ota_clear_flash(void)
  41. {
  42. uint32_t temp =0;
  43. if(1 == m_ready_for_reset){
  44. memset((uint8_t*)(&mFlash),0xFF,sizeof(Flash_t));
  45. temp = Flash_SaveInfomation();
  46. SEGGER_RTT_printf(0,"------>clear all flash...%d,\n",temp);
  47. m_ready_for_reset =2;
  48. }
  49. else if(2 == m_ready_for_reset){
  50. nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_CONTINUE);
  51. }
  52. }
  53. void app_ota_Process(void)
  54. {
  55. static uint8_t state = 0;
  56. char buf[16];
  57. uint8_t responhost =0;
  58. switch(state){
  59. case 0:{
  60. if(mFlash.isHost > 0){//左鞋
  61. if(1 == isHostOTA){
  62. SEGGER_RTT_printf(0,"------>isHostOTA\n");
  63. state = 1;
  64. host_disconnect();
  65. nrf_ble_scan_stop();
  66. Process_UpdatePeroid(app_ota_Process,60000);
  67. BLE_Client_Tx_Send(0,BLE_Client_T_DFU,&responhost,1);
  68. }
  69. }
  70. else{
  71. if(isOTA==1){ isOTA = 0;
  72. if(slave_isconnect()) slave_disconnect();
  73. else advertising_stop();
  74. memset(buf,0,16);
  75. sprintf(buf,"SH_%02X%02X%02X%02X%02X%02X",mAddr.addr[5],mAddr.addr[4],mAddr.addr[3],mAddr.addr[2],mAddr.addr[1],mAddr.addr[0]);
  76. SEGGER_RTT_printf(0,"OTA name(%d):%s\n",strlen(buf),buf);
  77. slave_set_adv_name(buf,strlen(buf));
  78. slave_adv_init();
  79. state = 1;
  80. Process_UpdatePeroid(app_ota_Process,60000);
  81. }
  82. }
  83. break;}
  84. case 1:{
  85. if(mFlash.isHost > 0){//左鞋
  86. isHostOTA =0;
  87. state =0;
  88. m_ready_for_reset =0;
  89. SEGGER_RTT_printf(0,"------>left out to dfu\n");
  90. }
  91. else{
  92. if(slave_isconnect()==0){
  93. advertising_start();
  94. Process_UpdatePeroid(app_ota_Process,60000);
  95. state = 2;
  96. }
  97. }
  98. break;}
  99. case 2:{
  100. if(slave_isconnect()) state = 0;
  101. Process_UpdatePeroid(app_ota_Process,0);
  102. advertising_stop();
  103. memset(buf,0,16);
  104. sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  105. SEGGER_RTT_printf(0,"ADV name(%d):%s\n",strlen(buf),buf);
  106. slave_set_adv_name(buf,strlen(buf));
  107. slave_adv_init();
  108. advertising_start();
  109. state = 0;
  110. break;}
  111. default:state = 0;Process_UpdatePeroid(app_ota_Process,0);break;
  112. }
  113. }
  114. /*********************** 接收从机命令 ************************/
  115. void cb_BLE_Host_R_DFU(void* handle)
  116. {
  117. BLE_Host_Rx_t* target = handle;
  118. BLE_Client_Tx_Send(0,BLE_Client_T_DFU,target->pDat,1); //从机应答
  119. }
  120. /*********************** 接收手机命令 ************************/
  121. void cb_BLE_Client_R_DFU(void* handle)
  122. {
  123. // SEGGER_RTT_printf(0,">>>>>>>>>>BLE_Client_R_DFU\n");
  124. // BLE_Client_Rx_t* target = handle;
  125. // BLE_Host_Tx_Send(0,BLE_Host_T_DFU,target->pDat,1); //发给从机
  126. // if(mFlash.isHost>0 && *target->pDat == 0)isHostOTA = 1;
  127. //
  128. // if(mFlash.isHost>0) return; //左鞋不需要理会
  129. // isOTA = 1;
  130. // SEGGER_RTT_printf(0,">>>>>>>>>>DFU start....\n");
  131. SEGGER_RTT_printf(0,">>>>>>>>>>BLE_Client_R_DFU\n");
  132. BLE_Client_Rx_t* target = handle;
  133. if(mFlash.isHost>0){
  134. if(*target->pDat == 0){isHostOTA = 1;
  135. return; //左鞋不需要理会
  136. }
  137. else if(*target->pDat == 1){
  138. BLE_Host_Tx_Send(0,BLE_Host_T_DFU,target->pDat,1); //发给从机
  139. }
  140. }
  141. else {
  142. uint8_t responhost =1;
  143. BLE_Client_Tx_Send(0,BLE_Client_T_DFU,&responhost,1);
  144. isOTA = 1;
  145. SEGGER_RTT_printf(0,">>>>>>>>>>DFU start....\n");
  146. }
  147. }
  148. uint8_t app_ota_host_state(void)
  149. {
  150. return isHostOTA;
  151. }
  152. void app_ota_Init(void)
  153. {
  154. uint32_t err_code = sd_ble_gap_addr_get(&mAddr); APP_ERROR_CHECK(err_code);
  155. Process_Start(0,"app_ota",app_ota_Process);
  156. Process_Start(500,"app_ota_clear_flash",app_ota_clear_flash);
  157. BLE_Client_Rx_Regist(BLE_Client_R_DFU,cb_BLE_Client_R_DFU);
  158. BLE_Host_Rx_Regist(BLE_Host_R_DFU,cb_BLE_Host_R_DFU);
  159. }