app_ota.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "app_flash.h"
  13. #include "ble_comm.h"
  14. static ble_gap_addr_t mAddr;
  15. static uint32_t isOTA=0;
  16. static uint8_t isHostOTA=0;
  17. static bool app_shutdown_handler(nrf_pwr_mgmt_evt_t event)
  18. {
  19. switch (event){
  20. case NRF_PWR_MGMT_EVT_PREPARE_DFU:
  21. break;
  22. default:
  23. return true;
  24. }
  25. SEGGER_RTT_printf(0,"Into bootloader\n");
  26. return true;
  27. }
  28. NRF_PWR_MGMT_HANDLER_REGISTER(app_shutdown_handler, 0);
  29. static void app_ota_Process(void)
  30. {
  31. static uint8_t state = 0;
  32. char buf[16];
  33. uint8_t responhost =0;
  34. switch(state){
  35. case 0:{
  36. if(mFlash.isHost > 0){//左鞋
  37. if(1 == isHostOTA){
  38. SEGGER_RTT_printf(0,"------>isHostOTA\n");
  39. state = 1;
  40. host_disconnect();
  41. nrf_ble_scan_stop();
  42. Process_UpdatePeroid(app_ota_Process,60000);
  43. BLE_Client_Tx_Send(0,BLE_DFU,&responhost,1);
  44. }
  45. }
  46. else{
  47. if(isOTA==1){ isOTA = 0;
  48. if(slave_isconnect()) slave_disconnect();
  49. else advertising_stop();
  50. memset(buf,0,16);
  51. 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]);
  52. SEGGER_RTT_printf(0,"OTA name(%d):%s\n",strlen(buf),buf);
  53. slave_set_adv_name(buf,strlen(buf));
  54. slave_adv_init();
  55. state = 1;
  56. Process_UpdatePeroid(app_ota_Process,2000);
  57. }
  58. }
  59. break;}
  60. case 1:{
  61. if(mFlash.isHost > 0){//左鞋
  62. isHostOTA =0;
  63. state =0;
  64. SEGGER_RTT_printf(0,"------>left out to dfu\n");
  65. }
  66. else{
  67. if(slave_isconnect()==0){
  68. advertising_start();
  69. Process_UpdatePeroid(app_ota_Process,60000);
  70. state = 2;
  71. }
  72. else slave_disconnect();
  73. }
  74. break;}
  75. case 2:{
  76. if(slave_isconnect()) state = 0;
  77. Process_UpdatePeroid(app_ota_Process,0);
  78. advertising_stop();
  79. memset(buf,0,16);
  80. #if BleNameHoldOn_ENANBLE
  81. slave_set_adv_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  82. SEGGER_RTT_printf(0,"AdvName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  83. #else
  84. 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]);
  85. SEGGER_RTT_printf(0,"ADV name(%d):%s\n",strlen(buf),buf);
  86. slave_set_adv_name(buf,strlen(buf));
  87. #endif
  88. slave_adv_init();
  89. advertising_start();
  90. state = 0;
  91. break;}
  92. default:state = 0;Process_UpdatePeroid(app_ota_Process,0);break;
  93. }
  94. }
  95. static uint8_t number =0;
  96. static uint8_t left_dfu_state =0;
  97. static void app_ota_sendleftstate(void)
  98. {
  99. if(number>0){
  100. BLE_Client_Tx_Send(0,BLE_DFU,&left_dfu_state,1);
  101. number--;
  102. }
  103. else Process_Stop(app_ota_sendleftstate);
  104. }
  105. /*********************** 接收从机命令 ************************/
  106. void cb_BLE_Host_R_DFU(void* handle)
  107. {
  108. BLE_Host_Rx_t* target = handle;
  109. Process_Start(500,"app_ota",app_ota_sendleftstate);
  110. number =10;
  111. left_dfu_state = *target->pDat;
  112. }
  113. /*********************** 接收手机命令 ************************/
  114. void cb_BLE_Client_R_DFU(void* handle)
  115. {
  116. SEGGER_RTT_printf(0,">>>>>>>>>>BLE_Client_R_DFU\n");
  117. BLE_Client_Rx_t* target = handle;
  118. if(mFlash.isHost>0){
  119. if(*target->pDat == 0){isHostOTA = 1;
  120. return; //左鞋不需要理会
  121. }
  122. else if(*target->pDat == 1){
  123. BLE_Host_Tx_Send(0,BLE_DFU,target->pDat,1); //发给从机
  124. }
  125. }
  126. else {
  127. uint8_t responhost =1;
  128. BLE_Client_Tx_Send(0,BLE_DFU,&responhost,1);
  129. isOTA = 1;
  130. SEGGER_RTT_printf(0,">>>>>>>>>>DFU start....\n");
  131. }
  132. }
  133. uint8_t app_ota_host_state(void)
  134. {
  135. return isHostOTA;
  136. }
  137. #if BleNameHoldOn_ENANBLE
  138. extern uint8_t SaveFlashFlag_holdOn;
  139. static void app_ota_SaveFlash(void){
  140. if(1 ==SaveFlashFlag_holdOn ){
  141. SaveFlashFlag_holdOn =0;
  142. Flash_SaveBackup();
  143. Flash_SaveInfomation();
  144. uint8_t i =0;
  145. SEGGER_RTT_printf(0,"mac host addr:");for(i=0;i<6;i++){SEGGER_RTT_printf(0,"%02X ",mFlash.macHost[i]);}SEGGER_RTT_printf(0,"\n");
  146. SEGGER_RTT_printf(0,"mClient addr:");for(i=0;i<6;i++){SEGGER_RTT_printf(0,"%02X ",mFlash.mClient.macAddr[i]);}SEGGER_RTT_printf(0,"\n");
  147. SEGGER_RTT_printf(0,"macAddr_L:");for(i=0;i<6;i++){SEGGER_RTT_printf(0,"%02X ",mBackup.macAddr_L[i]);}SEGGER_RTT_printf(0,"\n");
  148. SEGGER_RTT_printf(0,"mBackup.macAddr_R:");for(i=0;i<6;i++){SEGGER_RTT_printf(0,"%02X ",mBackup.macAddr_R[i]);}SEGGER_RTT_printf(0,"\n");
  149. }
  150. }
  151. #endif
  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. BLE_Client_Rx_Regist(BLE_DFU,cb_BLE_Client_R_DFU);
  157. BLE_Host_Rx_Regist(BLE_DFU,cb_BLE_Host_R_DFU);
  158. #if BleNameHoldOn_ENANBLE
  159. SEGGER_RTT_printf(0,"mac addr:");for(int i=0;i<6;i++){SEGGER_RTT_printf(0,"%02X ",mAddr.addr[i]);}SEGGER_RTT_printf(0,"\n");
  160. uint8_t i=0;
  161. for(i=0;i<6;i++)if(mFlash.macHost[i] !=0xff)break;
  162. if(6 ==i){
  163. for(uint8_t a=0;a<6;a++){
  164. mFlash.macHost[a] = mAddr.addr[5-a];
  165. mBackup.macAddr_L[a] = mAddr.addr[5-a];
  166. }
  167. SaveFlashFlag_holdOn =1;
  168. }
  169. Process_Start(1000,"app_ota_SaveFlash",app_ota_SaveFlash);
  170. #endif
  171. }