app_ota.c 6.0 KB

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