app_ota.c 6.1 KB

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