app_ota - 副本.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 volatile uint8_t m_ready_for_reset = 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. switch(m_ready_for_reset){
  23. case 0:
  24. m_ready_for_reset =1;
  25. return false;
  26. case 1:
  27. return false;
  28. default:
  29. break;
  30. }
  31. break;
  32. default:
  33. return true;
  34. }
  35. SEGGER_RTT_printf(0,"Into bootloader\n");
  36. return true;
  37. }
  38. NRF_PWR_MGMT_HANDLER_REGISTER(app_shutdown_handler, 0);
  39. static void app_ota_clear_flash(void)
  40. {
  41. uint32_t temp =0;
  42. if(1 == m_ready_for_reset){
  43. memset((uint8_t*)(&mFlash),0xFF,sizeof(Flash_t));
  44. temp = Flash_SaveInfomation();
  45. SEGGER_RTT_printf(0,"------>clear all flash...%d,\n",temp);
  46. m_ready_for_reset =2;
  47. }
  48. else if(2 == m_ready_for_reset){
  49. nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_CONTINUE);
  50. }
  51. }
  52. static void app_ota_Process(void)
  53. {
  54. static uint8_t state = 0;
  55. char buf[16];
  56. uint8_t responhost =0;
  57. switch(state){
  58. case 0:{
  59. if(mFlash.isHost > 0){//左鞋
  60. if(1 == isHostOTA){
  61. SEGGER_RTT_printf(0,"------>isHostOTA\n");
  62. state = 1;
  63. host_disconnect();
  64. nrf_ble_scan_stop();
  65. Process_UpdatePeroid(app_ota_Process,60000);
  66. BLE_Client_Tx_Send(0,BLE_DFU,&responhost,1);
  67. }
  68. }
  69. else{
  70. if(isOTA==1){ isOTA = 0;
  71. if(slave_isconnect()) slave_disconnect();
  72. else advertising_stop();
  73. memset(buf,0,16);
  74. 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]);
  75. SEGGER_RTT_printf(0,"OTA name(%d):%s\n",strlen(buf),buf);
  76. slave_set_adv_name(buf,strlen(buf));
  77. slave_adv_init();
  78. state = 1;
  79. Process_UpdatePeroid(app_ota_Process,2000);
  80. }
  81. }
  82. break;}
  83. case 1:{
  84. if(mFlash.isHost > 0){//左鞋
  85. isHostOTA =0;
  86. state =0;
  87. m_ready_for_reset =0;
  88. SEGGER_RTT_printf(0,"------>left out to dfu\n");
  89. }
  90. else{
  91. if(slave_isconnect()==0){
  92. advertising_start();
  93. Process_UpdatePeroid(app_ota_Process,60000);
  94. state = 2;
  95. }
  96. else slave_disconnect();
  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. #if BleNameHoldOn_ENANBLE
  105. slave_set_adv_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  106. SEGGER_RTT_printf(0,"AdvName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  107. #else
  108. 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]);
  109. SEGGER_RTT_printf(0,"ADV name(%d):%s\n",strlen(buf),buf);
  110. slave_set_adv_name(buf,strlen(buf));
  111. #endif
  112. slave_adv_init();
  113. advertising_start();
  114. state = 0;
  115. break;}
  116. default:state = 0;Process_UpdatePeroid(app_ota_Process,0);break;
  117. }
  118. }
  119. static uint8_t number =0;
  120. static uint8_t left_dfu_state =0;
  121. static void app_ota_sendleftstate(void)
  122. {
  123. if(number>0){
  124. BLE_Client_Tx_Send(0,BLE_DFU,&left_dfu_state,1);
  125. number--;
  126. }
  127. else Process_Stop(app_ota_sendleftstate);
  128. }
  129. /*********************** 接收从机命令 ************************/
  130. void cb_BLE_Host_R_DFU(void* handle)
  131. {
  132. BLE_Host_Rx_t* target = handle;
  133. Process_Start(500,"app_ota",app_ota_sendleftstate);
  134. number =10;
  135. left_dfu_state = *target->pDat;
  136. // BLE_Client_Tx_Send(0,BLE_DFU,target->pDat,1); //发送到手机
  137. }
  138. /*********************** 接收手机命令 ************************/
  139. void cb_BLE_Client_R_DFU(void* handle)
  140. {
  141. SEGGER_RTT_printf(0,">>>>>>>>>>BLE_Client_R_DFU\n");
  142. BLE_Client_Rx_t* target = handle;
  143. if(mFlash.isHost>0){
  144. if(*target->pDat == 0){isHostOTA = 1;
  145. return; //左鞋不需要理会
  146. }
  147. else if(*target->pDat == 1){
  148. BLE_Host_Tx_Send(0,BLE_DFU,target->pDat,1); //发给从机
  149. }
  150. }
  151. else {
  152. uint8_t responhost =1;
  153. BLE_Client_Tx_Send(0,BLE_DFU,&responhost,1);
  154. isOTA = 1;
  155. SEGGER_RTT_printf(0,">>>>>>>>>>DFU start....\n");
  156. }
  157. }
  158. uint8_t app_ota_host_state(void)
  159. {
  160. return isHostOTA;
  161. }
  162. #if BleNameHoldOn_ENANBLE
  163. extern uint8_t SaveFlashFlag_holdOn;
  164. static void app_ota_SaveFlash(void){
  165. if(1 ==SaveFlashFlag_holdOn ){
  166. SaveFlashFlag_holdOn =0;
  167. Flash_SaveBackup();
  168. Flash_SaveInfomation();
  169. uint8_t i =0;
  170. 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");
  171. 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");
  172. 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");
  173. 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");
  174. }
  175. }
  176. #endif
  177. void app_ota_Init(void)
  178. {
  179. uint32_t err_code = sd_ble_gap_addr_get(&mAddr); APP_ERROR_CHECK(err_code);
  180. Process_Start(0,"app_ota",app_ota_Process);
  181. Process_Start(500,"app_ota_clear_flash",app_ota_clear_flash);
  182. BLE_Client_Rx_Regist(BLE_DFU,cb_BLE_Client_R_DFU);
  183. BLE_Host_Rx_Regist(BLE_DFU,cb_BLE_Host_R_DFU);
  184. #if BleNameHoldOn_ENANBLE
  185. 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");
  186. uint8_t i=0;
  187. for(i=0;i<6;i++)if(mFlash.macHost[i] !=0xff)break;
  188. if(6 ==i){
  189. for(uint8_t a=0;a<6;a++){
  190. mFlash.macHost[a] = mAddr.addr[5-a];
  191. mBackup.macAddr_L[a] = mAddr.addr[5-a];
  192. }
  193. SaveFlashFlag_holdOn =1;
  194. }
  195. Process_Start(1000,"app_ota_SaveFlash",app_ota_SaveFlash);
  196. #endif
  197. }