app_ota.c 5.5 KB

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