#include "app_ota.h" #include "nrf_gpio.h" #include "usr_config.h" #include "bsp_time.h" #include "system.h" #include "hal_mt.h" #include "app_host.h" #include "app_charge.h" #include "hal_ble_client.h" #include "hal_ble_host.h" #include "nrf_delay.h" #include "hal_flash.h" #include "ble_comm.h" #include "hal_soft_time.h" static ble_gap_addr_t mAddr; static uint32_t isOTA=0; static uint8_t isHostOTA=0; static volatile uint8_t m_ready_for_reset = 0; static bool app_shutdown_handler(nrf_pwr_mgmt_evt_t event) { switch (event){ case NRF_PWR_MGMT_EVT_PREPARE_DFU: switch(m_ready_for_reset){ case 0: m_ready_for_reset =1; return false; case 1: return false; default: break; } break; default: return true; } SEGGER_RTT_printf(0,"Into bootloader\n"); return true; } NRF_PWR_MGMT_HANDLER_REGISTER(app_shutdown_handler, 0); static void app_ota_clear_flash(void) { uint32_t temp =0; if(1 == m_ready_for_reset){ memset((uint8_t*)(&mFlash),0xFF,sizeof(Flash_t)); temp = Flash_SaveInfomation(); SEGGER_RTT_printf(0,"------>clear all flash...%d,\n",temp); m_ready_for_reset =2; } else if(2 == m_ready_for_reset){ nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_CONTINUE); } } void app_ota_Process(void) { static uint8_t state = 0; char buf[16]; uint8_t responhost =0; switch(state){ case 0:{ if(mFlash.isHost > 0){//左鞋 if(1 == isHostOTA){ SEGGER_RTT_printf(0,"------>isHostOTA\n"); state = 1; host_disconnect(); nrf_ble_scan_stop(); Process_UpdatePeroid(app_ota_Process,60000); BLE_Client_Tx_Send(0,BLE_Client_T_DFU,&responhost,1); } } else{ if(isOTA==1){ isOTA = 0; if(slave_isconnect()) slave_disconnect(); else advertising_stop(); memset(buf,0,16); 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]); SEGGER_RTT_printf(0,"OTA name(%d):%s\n",strlen(buf),buf); slave_set_adv_name(buf,strlen(buf)); slave_adv_init(); state = 1; Process_UpdatePeroid(app_ota_Process,60000); } } break;} case 1:{ if(mFlash.isHost > 0){//左鞋 isHostOTA =0; state =0; m_ready_for_reset =0; SEGGER_RTT_printf(0,"------>left out to dfu\n"); } else{ if(slave_isconnect()==0){ advertising_start(); Process_UpdatePeroid(app_ota_Process,60000); state = 2; } } break;} case 2:{ if(slave_isconnect()) state = 0; Process_UpdatePeroid(app_ota_Process,0); advertising_stop(); memset(buf,0,16); 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]); SEGGER_RTT_printf(0,"ADV name(%d):%s\n",strlen(buf),buf); slave_set_adv_name(buf,strlen(buf)); slave_adv_init(); advertising_start(); state = 0; break;} default:state = 0;Process_UpdatePeroid(app_ota_Process,0);break; } } /*********************** 接收从机命令 ************************/ void cb_BLE_Host_R_DFU(void* handle) { BLE_Host_Rx_t* target = handle; BLE_Client_Tx_Send(0,BLE_Client_T_DFU,target->pDat,1); //从机应答 } /*********************** 接收手机命令 ************************/ void cb_BLE_Client_R_DFU(void* handle) { // SEGGER_RTT_printf(0,">>>>>>>>>>BLE_Client_R_DFU\n"); // BLE_Client_Rx_t* target = handle; // BLE_Host_Tx_Send(0,BLE_Host_T_DFU,target->pDat,1); //发给从机 // if(mFlash.isHost>0 && *target->pDat == 0)isHostOTA = 1; // // if(mFlash.isHost>0) return; //左鞋不需要理会 // isOTA = 1; // SEGGER_RTT_printf(0,">>>>>>>>>>DFU start....\n"); SEGGER_RTT_printf(0,">>>>>>>>>>BLE_Client_R_DFU\n"); BLE_Client_Rx_t* target = handle; if(mFlash.isHost>0){ if(*target->pDat == 0){isHostOTA = 1; return; //左鞋不需要理会 } else if(*target->pDat == 1){ BLE_Host_Tx_Send(0,BLE_Host_T_DFU,target->pDat,1); //发给从机 } } else { uint8_t responhost =1; BLE_Client_Tx_Send(0,BLE_Client_T_DFU,&responhost,1); isOTA = 1; SEGGER_RTT_printf(0,">>>>>>>>>>DFU start....\n"); } } uint8_t app_ota_host_state(void) { return isHostOTA; } void app_ota_Init(void) { uint32_t err_code = sd_ble_gap_addr_get(&mAddr); APP_ERROR_CHECK(err_code); Process_Start(0,"app_ota",app_ota_Process); Process_Start(500,"app_ota_clear_flash",app_ota_clear_flash); BLE_Client_Rx_Regist(BLE_Client_R_DFU,cb_BLE_Client_R_DFU); BLE_Host_Rx_Regist(BLE_Host_R_DFU,cb_BLE_Host_R_DFU); }