app_ota.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. mFlash.RestartCnt =0;
  46. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_DFU,"save information 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. memset(buf,0,16);
  77. 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]);
  78. DEBUG_LOG("OTA name(%d):%s\n",strlen(buf),buf);
  79. slave_set_adv_name(buf,strlen(buf));
  80. slave_adv_init();
  81. state = 1;
  82. Process_UpdatePeroid(app_ota_Process,2000);
  83. }
  84. }
  85. break;}
  86. case 1:{
  87. if(mFlash.isHost > 0){//左鞋
  88. isHostOTA =0;
  89. state =0;
  90. DEBUG_LOG("------>left out to dfu\n");
  91. }
  92. else{
  93. if(slave_isconnect()==0){
  94. DEBUG_LOG("app_ota_Process advertising_start\r\n");
  95. advertising_start();
  96. Process_UpdatePeroid(app_ota_Process,10000);
  97. state = 2;
  98. }
  99. else slave_disconnect();
  100. }
  101. break;}
  102. case 2:{
  103. if(slave_isconnect()) state = 0;
  104. Process_UpdatePeroid(app_ota_Process,0);
  105. advertising_stop();
  106. memset(buf,0,16);
  107. #if BleNameHoldOn_ENANBLE
  108. slave_set_adv_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  109. DEBUG_LOG("AdvName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  110. #else
  111. 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]);
  112. DEBUG_LOG("ADV name(%d):%s\n",strlen(buf),buf);
  113. slave_set_adv_name(buf,strlen(buf));
  114. #endif
  115. Except_TxError(EXCEPT_DFU,"adv disconnted over time");
  116. slave_adv_init();
  117. advertising_start();
  118. state = 0;
  119. break;}
  120. default:state = 0;Process_UpdatePeroid(app_ota_Process,0);break;
  121. }
  122. }
  123. /*********************** 接收从机命令 ************************/
  124. void cb_BLE_Host_R_DFU(void* handle)
  125. {
  126. uint8_t left_dfu_state =0;
  127. BLE_Host_Rx_t* target = handle;
  128. left_dfu_state = target->pDat[0];
  129. BLE_Client_Tx_Send(0,BLE_DFU,&left_dfu_state,1);
  130. }
  131. static void cb_Client_BLE_DFU(void* handle)
  132. {
  133. Process_SetHoldOn(app_ota_Process,0);
  134. ResRightFlag =1;
  135. }
  136. static BLE_Client_Tx_t m_Client_BLE_DFU = {
  137. .n = 15,
  138. .t = 200,
  139. .cb = cb_Client_BLE_DFU,
  140. };
  141. /*********************** 接收手机命令 ************************/
  142. void cb_BLE_Client_R_DFU(void* handle)
  143. {
  144. DEBUG_LOG(">>>>>>>>>>BLE_Client_R_DFU\n");
  145. BLE_Client_Rx_t* target = handle;
  146. if(mFlash.isHost>0){
  147. if(target->pDat[0] == 0){isHostOTA = 1;
  148. return; //左鞋不需要理会
  149. }
  150. else if(target->pDat[0] == 1){
  151. BLE_Host_Tx_Send(0,BLE_DFU,target->pDat,1); //发给从机
  152. }
  153. }
  154. else {
  155. static uint8_t responhost =1;
  156. BLE_Client_Tx_Send(&m_Client_BLE_DFU,BLE_DFU,&responhost,1);
  157. isOTA = 1;
  158. Process_SetHoldOn(app_ota_Process,1);
  159. DEBUG_LOG(">>>>>>>>>>DFU start....\n");
  160. }
  161. }
  162. uint8_t app_ota_host_state(void)
  163. {
  164. return isHostOTA;
  165. }
  166. #if BleNameHoldOn_ENANBLE
  167. extern uint8_t SaveFlashFlag_holdOn;
  168. static void app_ota_SaveFlash(void){
  169. if(1 ==SaveFlashFlag_holdOn ){
  170. SaveFlashFlag_holdOn =0;
  171. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_DFU,"save information fail");
  172. uint8_t i =0;
  173. DEBUG_LOG("mac host addr:");for(i=0;i<6;i++){DEBUG_LOG("%02X ",mFlash.macHost[i]);}DEBUG_LOG("\n");
  174. DEBUG_LOG("mClient addr:");for(i=0;i<6;i++){DEBUG_LOG("%02X ",mFlash.mClient.macAddr[i]);}DEBUG_LOG("\n");
  175. }
  176. }
  177. #endif
  178. void app_ota_Init(void)
  179. {
  180. uint32_t err_code = sd_ble_gap_addr_get(&mAddr); APP_ERROR_CHECK(err_code);
  181. Process_Start(0,"app_ota_Process",app_ota_Process);
  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. Process_Start(500,"app_ota_clear_flash",app_ota_clear_flash);
  185. #if BleNameHoldOn_ENANBLE
  186. DEBUG_LOG("mac addr:");for(int i=0;i<6;i++){DEBUG_LOG("%02X ",mAddr.addr[i]);}DEBUG_LOG("\n");
  187. uint8_t i=0;
  188. for(i=0;i<6;i++)if(mFlash.macHost[i] !=0xff)break;
  189. if(6 ==i){
  190. for(uint8_t a=0;a<6;a++){
  191. mFlash.macHost[a] = mAddr.addr[5-a];
  192. }
  193. SaveFlashFlag_holdOn =1;
  194. }
  195. Process_Start(1000,"app_ota_SaveFlash",app_ota_SaveFlash);
  196. #endif
  197. }