| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539 |
- /*
- * @Description:
- * @Version: 2.0
- * @Author: Seven
- * @Date: 2022-08-22 17:13:45
- * @LastEditors: Seven
- * @LastEditTime: 2022-12-19 14:00:16
- */
- #include "ota.h"
- #include "includes.h"
- #include "gw_ctrl.h"
- #include "lora.h"
- #include "radio.h"
- uint8_t updata_state = 0;
- ota_state_t ota_state;
- ota_pkg_info_t ota_slave;
- ota_pkg_info_t ota_master;
- master_rcv_info_t master_rcv_info;
- slave_upload_info_t slave_upload_info;
- //比较接收设备的类型和sn
- int device_type_and_id_cmp(u32 device_type, u32 device_sn)
- {
- int ret = 0;
- if(device_type == 0x0201/*device_info.device_type*/)
- {
- if(device_sn == device_info.device_sn || device_sn == 0xFFFFFFFF)
- {
- ret = 1;
- }
- }
- else if(device_type == 0xffff && device_sn == 0xffffffff)
- {
- ret = 1;
- }
- return ret;
- }
- //存储升级状态
- void updata_state_save(uint8_t state)
- {
- uint16_t updata_state_temp;
- updata_state = state;
- updata_state_temp = set_updata_state(updata_state);
- BKP_WriteBackupRegister(BKP_DR3, updata_state_temp);
- }
- //向备份寄存器存储转义数据
- void BKP_WriteBackupRegister_code(uint16_t BKP_DR, uint8_t Data)
- {
- uint16_t temp;
- temp = set_updata_state(Data);
- BKP_WriteBackupRegister(BKP_DR, temp);
- }
- //采集器主动发送
- void coll_msg_send(collect_gateway_msg_format_t *p_msg, uint8_t msg_type, uint16_t len)
- {
- static uint32_t msg_no = 0;
- char *msg = NULL;
- uint16_t *p_crc;
- p_msg->frame_header = 0xfefe;
- p_msg->proto_Ver = PROTOCOL_VERSION;
- p_msg->msg_id = msg_no++;
- p_msg->first_type = msg_type;
- p_msg->second_type = 0x00;
- p_msg->msg_len = len+2;//后面加两字节的CRC
-
- p_crc = (uint16_t*)&p_msg->info[len];
-
- *p_crc = CRC16_get((uint8_t *)p_msg,p_msg->msg_len+FRAME_HEADER_LEN-2);
- msg = (char *)lora_queue_mem_calloc_must(SYS_LORA_TX);
- if(msg) {
- memcpy(msg, (char *)p_msg, FRAME_HEADER_LEN + len+2);
- lora_queue_insert(SYS_LORA_TX,(char *)msg, FRAME_HEADER_LEN + len+2);
- }
- else
- {
- lora_queue_mem_free(SYS_LORA_TX,msg);
- }
- }
- //升级重传函数
- void ota_retransmit_req(uint16_t repeat_sub_pkg_id)
- {
- collect_gateway_msg_format_t msg;
- ota_retransmit_req_t *p_ota = (ota_retransmit_req_t *)msg.info;
- p_ota->repeat_devict_type = ota_slave.rcv_device_type;
- p_ota->repeat_device_sn = device_info.device_sn;
- p_ota->repeat_sub_pkg_id = repeat_sub_pkg_id;
-
- coll_msg_send(&msg, net_msg_type_update_retransmit_req, OTA_RETRNSMIT_INFO_LEN);
- }
- //存储设备升级信息
- int lora_node_ota_info_Init(ota_pkg_info_t *ota_info,ota_start_info_t *p_ota)
- {
- // memset(ota_info, 0, sizeof(ota_pkg_info_t));
- ota_info->rcv_device_type = p_ota->rcv_device_type;
- ota_info->rcv_device_sn = p_ota->rcv_device_sn;
- ota_info->des_device_type = p_ota->des_device_type;
- ota_info->des_device_sn = p_ota->des_device_sn;
- ota_info->update_version = p_ota->update_version;
- ota_info->total_bytes = p_ota->total_bytes;
- ota_info->total_pkgs = p_ota->total_bytes/OTA_UPDATE_PAYLAOA_LEN;
- ota_info->lossPkgIndex = 0;
- ota_info->lastRightPkgNum = 1;
- if(p_ota->total_bytes%OTA_UPDATE_PAYLAOA_LEN) {
- ota_info->total_pkgs += 1;
- }
- ota_info->lossPkgCounter = 0;
- ota_info->sub_pkg_id = 1;
-
- ota_info->Send_interval = p_ota->Send_interval;
- return 0;
- }
- //从设备设置旁观者定时器
- void ota_timer_onlooker_slave_set(uint8_t state, uint32_t timeout)
- {
- if(state == OPEN)
- {
- //升级指示灯
- ota_start_led_init();
- Led_Single_State_Set(UPDATA_ONLOOKER_LED,LED_ON);
- Data_last4bit_led(0xff,1);
-
- ota_state.update_mode = OTA_WORK_STATE_ONLOOKER;
- timeout_setValue(&ota_slave.tt_ota_onlooker,timeout,1);
- timeout_stop(&ota_slave.tt_ota_continuous);//关闭连续定时
- timeout_stop(&ota_slave.tt_ota_retransmit);//关闭重传定时
- }
- else if(state == CLOSE)
- {
- timeout_stop(&ota_slave.tt_ota_onlooker);
- }
- }
- //从设备旁观者超时处理
- void ota_timeout_onlooker_slave(void)
- {
- if(timeout_isOut(&ota_slave.tt_ota_onlooker))
- {
- memset(&ota_state,0,sizeof(ota_state));
- ota_timer_continuous_slave_set(CLOSE,0);//关闭连续发包定时器
- ota_timer_retransmit_slave_set(CLOSE,0);//关闭重传阶段定时器
- ota_timer_total_slave_set(CLOSE,0);//关闭总定时器
- if(updata_state)//上报升级状态
- {
- device_fixed_info.slave_uploade_start = 1;
- }
- else
- {
- if(device_info.alrd == DEVICE_ALRD)//已初始化
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
- }
- else
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
- }
- //升级指示灯
- led_run_set(LED_ALL,LED_OFF);
- }
- printf("onlooker timeout \n");
- }
- }
- //从设备设置重传定时器
- void ota_timer_retransmit_slave_set(uint8_t state, uint32_t timeout)
- {
- if(state == OPEN)
- {
- ota_state.update_state_slave = OTA_WORK_STATE_RETRANSMIT;
- timeout_setValue(&ota_slave.tt_ota_retransmit, timeout, 1);
- }
- else if(state == CLOSE)
- {
- timeout_stop(&ota_slave.tt_ota_retransmit);
- }
- }
- //从设备重传超时
- void ota_timeout_retransmit_slave(void)
- {
- if(timeout_isOut(&ota_slave.tt_ota_retransmit))
- {
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- updata_state_save(RETRANSMIT_TIMEOUT);//存储升级状态
- printf("retransmit timeout \n");
- }
- }
- //从设备设置连续接收定时器
- void ota_timer_continuous_slave_set(uint8_t state, uint32_t timeout)
- {
- if(state == OPEN)
- {
- ota_state.update_state_slave = OTA_WORK_STATE_CONTINUOUS;
- timeout_setValue(&ota_slave.tt_ota_continuous,timeout,1);
- }
- else if(state == CLOSE)
- {
- timeout_stop(&ota_slave.tt_ota_continuous);
- }
- }
- //从设备连续接收超时
- void ota_timeout_continuous_slave(void)
- {
- uint16_t i;
- if(timeout_isOut(&ota_slave.tt_ota_continuous))
- {
- //更新收包相关状态
- for(i = ota_slave.lastRightPkgNum;i < ota_slave.total_pkgs+1;i++) {
- ota_slave.lossPkgCounter++;
- ota_slave.lossPkgId[ota_slave.lossPkgIndex++] = i;//+1因为子包编号从0开始,
- //丢包太多了,超过了MAX_LOSS_PKG_COUNT,结束本次升级。
- if(ota_slave.lossPkgCounter == MAX_LOSS_PKG_COUNT) {//降为旁观者, 放弃本次升级
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- updata_state_save(PACKET_LOSS_MORE);//存储升级状态
- return;
- }
- }
- if(ota_slave.lossPkgCounter == 0) //没有丢包
- {
- ota_state.update_state_slave = OTA_WORK_STATE_RCV_END;
- printf("check begin\n");
- ota_continuous_end_handle();
- }
- else //有丢包
- {
- ota_timer_retransmit_require_slave_set(OPEN,RETRANSMIT_DELAY_TIMEOUT_MIN);//打开重传延时定时器
- ota_timer_retransmit_slave_set(OPEN,RETRANSMIT_TIMEOUT_TIME); //打开重传定时器
- printf("retransmit begin\n");
- }
- }
- }
- //从设备总定时器设置
- void ota_timer_total_slave_set(uint8_t state, uint32_t timeout)
- {
- if(state == OPEN)
- {
- timeout_setValue(&ota_slave.tt_ota_total,timeout,1);
- }
- else if(state == CLOSE)
- {
- timeout_stop(&ota_slave.tt_ota_total);
- }
- }
- //从设备总定时超时
- void ota_timeout_total_slave(void)
- {
- if(timeout_isOut(&ota_slave.tt_ota_total))
- {
- if(updata_state)//上报升级状态
- {
- device_fixed_info.slave_uploade_start = 1;
- }
- else
- {
- if(device_info.alrd == DEVICE_ALRD)//已初始化
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
- }
- else
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
- }
- }
- ota_timer_onlooker_slave_set(CLOSE,0);//关闭旁观者定时器
- ota_timer_continuous_slave_set(CLOSE,0);//关闭连续发包定时器
- ota_timer_retransmit_slave_set(CLOSE,0);//关闭重传阶段定时器
- memset(&ota_state,0,sizeof(ota_state));
- updata_state_save(UPDATA_TOTAL_TIMEOUT);//存储升级状态
- printf("total timeout \n");
- }
- }
- //从设备重传请求间隔定时设置
- void ota_timer_retransmit_require_slave_set(uint8_t state, uint32_t timeout)
- {
- if(state == OPEN)
- {
- timeout_setValue(&ota_slave.tt_ota_retransmit_delay,timeout,1);
- }
- else if(state == CLOSE)
- {
- timeout_stop(&ota_slave.tt_ota_retransmit_delay);
- }
- }
- //从设备重传请求超时处理
- void ota_timeout_retransmit_require_slave(void)
- {
- if(timeout_isOut(&ota_slave.tt_ota_retransmit_delay))
- {
- ota_continuous_end_handle();
- }
- }
- //从设备主动上报升级状态总定时设置
- void ota_timer_upload_total_slave_set(uint8_t state, uint32_t timeout)
- {
- if(state == OPEN)
- {
- ota_state.update_mode = OTA_WORK_STATE_UPLOAD;
- timeout_setValue(&slave_upload_info.upload_total_time,timeout,1);
- }
- else if(state == CLOSE)
- {
- timeout_stop(&slave_upload_info.upload_total_time);
- }
- }
- //从设备主动上报升级状态总定时超时处理
- void ota_timeout_upload_total_slave(void)
- {
- if(timeout_isOut(&slave_upload_info.upload_total_time))
- {
- slave_upload_end();
- }
- }
- //从设备主动上报升级状态发送间隔定时设置
- void ota_timer_upload_cad_slave_set(uint8_t state, uint32_t timeout)
- {
- if(state == OPEN)
- {
- timeout_setValue(&slave_upload_info.upload_cad_time,timeout,1);
- }
- else if(state == CLOSE)
- {
- timeout_stop(&slave_upload_info.upload_cad_time);
- }
- }
- //从设备主动上报升级状态发送间隔超时处理
- void ota_timeout_upload_cad_slave(void)
- {
- if(timeout_isOut(&slave_upload_info.upload_cad_time))
- {
- lora_slave_ota_updatestate_resp(updata_state);
- }
- }
- uint32_t OTA_Continuous_Timeout = 0;
- //网关下发的升级命令分析
- void gateway_lora_ota_proc(collect_gateway_msg_format_t *p_msg)
- {
- uint8_t flash_flag=0;
- uint32_t i;
- uint32_t remain_pkg = 0;
- ota_start_info_t *p_start = NULL;
- ota_continues_info_t *p_cont = NULL;
- gateway_coll_offline_upgrade_t *p_off = NULL;
- if(device_fixed_info.Work_State != DEV_WORK_STATE_UPDATING)
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_UPDATING;
- updata_state = 0;
- srand(device_info.device_sn);//随机数种子
- memset(&ota_state,0,sizeof(ota_state));
- }
- if(ota_state.update_mode == OTA_WORK_STATE_ONLOOKER)//旁观者
- {
- printf("p_msg->type:%x\n",p_msg->first_type);
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);
- }
- else//非旁观者
- {
- switch(p_msg->first_type)
- {
- case net_msg_type_update_start://开始升级指令
- p_start = (ota_start_info_t *)p_msg->info;
- printf("p_msg->type:%x\n",p_msg->first_type);
- /* 校验当前设备是否为接收对象 */
- if(device_type_and_id_cmp(p_start->rcv_device_type, p_start->rcv_device_sn) && p_start->des_device_type == ((p_start->update_version&0xffff0000)>>16))
- {
- ota_state.update_mode = OTA_WORK_STATE_SLAVE;
- if(p_start->total_bytes > OTA_UPDATE_APP3_FLASH_SIZE)//升级包总长度大于存储区域大小
- {
- updata_state_save(UPDATA_PACKAGE_LENGTH_ERROR);//存储升级状态
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- printf("总长度太大\n");
- return;
- }
- // if((p_start->rcv_device_type == 0xffff) && ((p_start->update_version&0xf0000000) != 0x10000000))//显示屏升级
- // {
- // //软件版本错误
- // updata_state_save(DEVICE_TYPE_ERROR);//存储升级状态
- // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- // printf("软件版本错误\n");
- // return;
- // }
- // if((p_start->update_version&0xfffff000) != (SOFTWARE_VERSION_APP&0xfffff000))//软件版本号错误
- // {
- // updata_state_save(DEVICE_TYPE_ERROR);//存储升级状态
- // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- // printf("软件版本错误\n");
- // return;
- // }
-
- if(!ota_state.flash_erase)//第一次接收到指令擦除flash
- {
- ota_state.flash_erase = 1;
- device_irq_set(DISABLE);
- flash_flag = Flash_RangeErase(OTA_UPDATE_APP3_FLASH_START_ADDR, OTA_UPDATE_APP3_FLASH_SIZE); //擦除app3
- printf("flash flag=%d\n",flash_flag);
- memset(&ota_slave, 0, sizeof(ota_pkg_info_t));
- //打开升级总定时器
- ota_timer_total_slave_set(OPEN,OTA_TOTAL_TIMEOUT_TIME_TICK);
- //存储升级状态
- updata_state_save(START_UPDATA);
- //清除显示屏升级标志
- BKP_WriteBackupRegister_code(BKP_DR4,0);
- //升级指示灯
- ota_start_led_init();
- Led_Single_State_Set(UPDATA_SLAVE_LED,LED_ON);
- }
- ota_state.update_state_slave = OTA_WORK_STATE_START;
- //存储升级信息
- lora_node_ota_info_Init(&ota_slave,p_start);
- OTA_Continuous_Timeout = (ota_slave.total_pkgs * ota_slave.Send_interval) + CONTINUOUS_TIMEOUT_DELAY_TIME;//连续接收定时时间
- if(OTA_Continuous_Timeout > CONTINUOUS_TIMEOUT_TIME_MUX)//发送时间太长
- {
- printf("continuous time to long\n");
- printf("ota continuous time:%u\n",OTA_Continuous_Timeout/1000);
-
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- return;
- }
- ota_timer_continuous_slave_set(OPEN,OTA_Continuous_Timeout);//打开连续接收定时器
- printf("ota_totle_byte:%d, ota_totle_pkg:%d\n",ota_slave.total_bytes,ota_slave.total_pkgs);
- printf("send interval:%d, continuous time:%d\n",ota_slave.Send_interval,OTA_Continuous_Timeout/1000);
- }
- else
- {
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- }
- break;
- case net_msg_type_update_continues://连续发包阶段
- p_cont = (ota_continues_info_t *)p_msg->info;
- if(device_type_and_id_cmp(p_cont->rcv_device_type, p_cont->rcv_device_sn)) //是当前设备
- {
- if(ota_state.update_state_slave == OTA_WORK_STATE_CONTINUOUS)//收到了升级开始指令
- {
- ota_state.update_mode = OTA_WORK_STATE_SLAVE;
- if(p_cont->sub_pkg_len > OTA_UPDATE_PAYLAOA_LEN)//收到的包长度大于128,收到的数据错误
- return;
- if(p_cont->sub_pkg_id < ota_slave.lastRightPkgNum)
- return;
- if(p_cont->total_pkgs == ota_slave.total_pkgs)
- {
- printf("update continues: type = 0x%x, id = 0x%x, {%d/%d/%d}\r\n",
- p_cont->rcv_device_type, p_cont->rcv_device_sn,
- p_cont->total_pkgs, p_cont->sub_pkg_id,ota_slave.lossPkgCounter);
- //接收本包并写入flash的对应位置
- Flash_BufferWrite(OTA_UPDATE_APP3_FLASH_START_ADDR + (p_cont->sub_pkg_id - 1)*OTA_UPDATE_PAYLAOA_LEN,
- (uint32_t *)(p_cont->pkg_content), p_cont->sub_pkg_len);
- //更新收包相关状态
- for(i = ota_slave.lastRightPkgNum;i < p_cont->sub_pkg_id;i++) {
- ota_slave.lossPkgCounter++;
- ota_slave.lossPkgId[ota_slave.lossPkgIndex++] = i;//+1因为子包编号从0开始,
- //丢包太多了,超过了MAX_LOSS_PKG_COUNT,结束本次升级。
- if(ota_slave.lossPkgCounter == MAX_LOSS_PKG_COUNT) {//降为旁观者, 放弃本次升级
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- updata_state_save(PACKET_LOSS_MORE);//存储升级状态
- return;
- }
- }
- ota_slave.lastRightPkgNum = p_cont->sub_pkg_id + 1;
- if(p_cont->sub_pkg_id == ota_slave.total_pkgs)//当前是最后一包
- {
- if(ota_slave.lossPkgCounter == 0) //没有丢包
- {
- ota_state.update_state_slave = OTA_WORK_STATE_RCV_END;
- printf("check begin\n");
- ota_continuous_end_handle();
- }
- else //有丢包
- {
- ota_timer_retransmit_require_slave_set(OPEN,RETRANSMIT_DELAY_TIMEOUT_MIN);//打开重传延时定时器
- ota_timer_retransmit_slave_set(OPEN,RETRANSMIT_TIMEOUT_TIME); //打开重传定时器
- ota_timer_continuous_slave_set(CLOSE,0);//关闭连续定时器
- printf("retransmit begin\n");
- }
- }
- else
- {
- //连续接收定时时间
- OTA_Continuous_Timeout = ((ota_slave.total_pkgs-p_cont->sub_pkg_id) * ota_slave.Send_interval)+ CONTINUOUS_TIMEOUT_DELAY_TIME;
- ota_timer_continuous_slave_set(OPEN,OTA_Continuous_Timeout);//打开连续接收定时器
- printf("send interval:%d, continuous time:%d\n",ota_slave.Send_interval,OTA_Continuous_Timeout/1000);
- }
- //指示灯显示
- remain_pkg = ota_slave.total_pkgs-p_cont->sub_pkg_id;
- Data_last4bit_led(remain_pkg,1);
- }
- }
- else//未收到升级开始指令
- {
- updata_state_save(START_UPDATA_NONE);//存储升级状态
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- ota_timer_continuous_slave_set(CLOSE,OTA_Continuous_Timeout);//关闭连续接收定时器
- }
- }
- else
- {
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- ota_timer_continuous_slave_set(CLOSE,OTA_Continuous_Timeout);//关闭连续接收定时器
- }
- break;
- case net_msg_type_update_retransmit_rsp://重传发包指令
- p_cont = (ota_continues_info_t *)p_msg->info;
- if(device_type_and_id_cmp(p_cont->rcv_device_type, p_cont->rcv_device_sn))
- {
- if(ota_state.update_state_slave == OTA_WORK_STATE_RETRANSMIT)//收到升级开始指令
- {
- ota_state.update_mode = OTA_WORK_STATE_SLAVE;
- if(p_cont->sub_pkg_len > OTA_UPDATE_PAYLAOA_LEN)//收到的包长度大于128,收到的数据错误
- return;
- if(p_cont->total_pkgs == ota_slave.total_pkgs)
- {
- //判定该包是否为本设备丢的那些包之中的子包
- for(i = 0;i < ota_slave.lossPkgIndex;i++) {
- if(ota_slave.lossPkgId[i] == p_cont->sub_pkg_id) {
- ota_slave.lossPkgCounter--;
- ota_slave.lossPkgId[i] = 0;
-
- //接受本包并写入flash的对应位置
- Flash_BufferWrite(OTA_UPDATE_APP3_FLASH_START_ADDR + (p_cont->sub_pkg_id - 1)*OTA_UPDATE_PAYLAOA_LEN,
- (uint32_t *)(p_cont->pkg_content), p_cont->sub_pkg_len);
-
- printf("rcv retransmit pkgid:%d\n",p_cont->sub_pkg_id);
- break;
- }
- }
- printf("ota_info.lossPkgCounter = %d, lossPkgId:%d\r\n", ota_slave.lossPkgCounter,p_cont->sub_pkg_id);
- if(ota_slave.lossPkgCounter == 0) //丢包已收完
- {
- ota_state.update_state_slave = OTA_WORK_STATE_RCV_END;
- ota_continuous_end_handle();
- return;
- }
-
- //指示灯显示
- remain_pkg = ota_slave.lossPkgCounter;
- Data_last4bit_led(remain_pkg,1);
- }
- ota_timer_retransmit_slave_set(OPEN,RETRANSMIT_TIMEOUT_TIME);//打开重传定时器
- }
- else//未收到升级开始指令
- {
- // updata_state_save(START_UPDATA_NONE);//存储升级状态
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- }
- }
- else
- {
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- }
- break;
- case net_msg_type_update_offline://网关下发开始离线升级
- if(ota_state.update_mode == 0)
- {
- p_off = (gateway_coll_offline_upgrade_t *)p_msg->info;
- if(p_off->master_device_type==0x0201 && p_off->master_device_sn==device_info.device_sn)
- {
- ota_state.update_mode = OTA_WORK_STATE_MASTER;
- lora_master_ota_gateway_info_init(p_off);
- device_fixed_info.master_updata_start = 1;
- ota_state.offline_mode = GATEWAY_TRIGGER;
- printf("网关下发开始离线升级\n");
- printf("slave sn:%010u, des sn:%010u\n",p_off->slave_device_sn,p_off->des_device_sn);
- }
- else
- {
- if(device_info.alrd == DEVICE_ALRD)
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
- }
- else
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
- }
- }
- }
- break;
- default:
- if(ota_state.update_mode == 0)
- {
- if(device_info.alrd == DEVICE_ALRD)
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
- }
- else
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
- }
- }
- break;
- }
- }
- }
- //升级包校验判断
- uint8_t ota_update_pkg_check(uint16_t type, uint8_t state)
- {
- // uint8_t flashbuf[64];
- uint8_t ret = 0;
- uint32_t crcVl,fixChar,fileVer,filelen/*,fileaddr*/;
- uint32_t getcrc;
- // uint32_t xspl_addr,xspl_len,xspa_len;
- //判断升级设备类型
- crcVl = *(uint32_t*)(OTA_UPDATE_APP3_FLASH_START_ADDR+ota_slave.total_bytes-4); //CRC校验
- fixChar = *(uint32_t*)(OTA_UPDATE_APP3_FLASH_START_ADDR+ota_slave.total_bytes-8); // 固定字符 WBJW
- fileVer = *(uint32_t*)(OTA_UPDATE_APP3_FLASH_START_ADDR+ota_slave.total_bytes-12); // 固件版本信息
- // fileaddr = *(uint32_t*)(OTA_UPDATE_APP3_FLASH_START_ADDR+ota_slave.total_bytes-16); // 存储地址
- filelen = *(uint32_t*)(OTA_UPDATE_APP3_FLASH_START_ADDR+ota_slave.total_bytes-20); // 文件总长度
- if(type == 0x0201)
- {
- printf("read_fixchar:%08x, fixchar:%08x\n",fixChar,0x57424A57);
- if(fixChar != 0x57424A57) //固定字符错误
- {
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- updata_state_save(FIXED_CHARACTER_ERROR);//存储升级状态
- printf("fixChar error\r\n");// 升级文件固定字符校验错误
- return ret;
- }
- printf("read_filever:%08x, update_ver:%08x\n",fileVer,ota_slave.update_version);
- if((fileVer) != ota_slave.update_version) //固件信息错误
- {
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- updata_state_save(DEVICE_TYPE_ERROR);//存储升级状态
- printf("fileVer error\r\n");// 升级文件固件信息校验错误
- return ret;
- }
- printf("read_filelen:%08x, total_bytes:%08x\n",filelen,(ota_slave.total_bytes-20));
- if(ota_slave.total_bytes != (filelen+20))//长度错误
- {
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- updata_state_save(UPDATA_PACKAGE_LENGTH_ERROR);//存储升级状态
- printf("filelen error\r\n");// 升级文件长度校验错误
- return ret;
- }
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE);
- CRC_ResetDR();
- CRC_CalcBlockCRC((uint32_t*)OTA_UPDATE_APP3_FLASH_START_ADDR, (ota_slave.total_bytes -4)/4);
- getcrc = CRC_GetCRC();
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE);
- printf("read_crc:%x, get_crc:%x\n",crcVl,getcrc);
- if(getcrc!=crcVl) { //校验错误
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- updata_state_save(UPDATA_CRC_ERROR);//存储升级状态
- printf("update file crc error\r\n");// 升级文件crc校验错误
- return ret;
- }
- printf("收包正确\n");
- updata_state_save(UPDATA_DATA_SUCCESS);//存储升级状态
- ret = 1;
- // return ret;
- }
- else if(type == XSP_0904)
- {
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE);
- CRC_ResetDR();
- CRC_CalcBlockCRC((uint32_t*)OTA_UPDATE_APP3_FLASH_START_ADDR, (ota_slave.total_bytes -4)/4);
- getcrc = CRC_GetCRC();
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE);
- printf("read_crc:%x, get_crc:%x\n",crcVl,getcrc);
- if(getcrc!=crcVl) { //校验错误
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- updata_state_save(UPDATA_CRC_ERROR);//存储升级状态
- printf("update file crc error\r\n");// 升级文件crc校验错误
- return ret;
- }
- printf("收包正确\n");
- updata_state_save(UPDATA_DATA_SUCCESS);//存储升级状态
- ret = 1;
- // return ret;
-
-
- // if(fixChar != XSPL_CHAR) //固定字符错误
- // {
- // if(state)
- // {
- // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- // updata_state_save(FIXED_CHARACTER_ERROR);//存储升级状态
- // }
- // printf("fixChar error\r\n");// 升级文件固定字符校验错误
- // return ret;
- // }
- //// printf("read_filever:%08x, update_ver:%08x\n",fileVer,ota_slave.update_version);
- //// if((fileVer) != ota_slave.update_version) //固件版本信息错误
- //// {
- //// ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- //// updata_state_save(DEVICE_TYPE_ERROR);//存储升级状态
- //// printf("fileVer error\r\n");// 升级文件固件信息校验错误
- //// return ret;
- //// }
- // XSPL_len = filelen;
- // XSPL_addr = fileaddr;
- // if(XSPL_addr != OTA_UPDATE_APP3_FLASH_START_ADDR+ota_slave.total_bytes-filelen-20)
- // {
- // //路由表的起始地址和长度错误
- // if(state)
- // {
- // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- // updata_state_save(UPDATA_PACKAGE_LENGTH_ERROR);//存储升级状态
- // }
- // printf("xspl startaddr and length err\n");
- // return ret;
- // }
- //
- // printf("xspl_addr:%08x,len:%d\n",XSPL_addr,(XSPL_len+16));
- //// if((XSPL_len+16) <= 64)
- //// {
- //// Flash_BufferRead(XSPL_addr,(uint8_t *)flashbuf,XSPL_len+16);
- //// data_dump("flash",flashbuf,XSPL_len+16);
- //// }
- // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE);
- // CRC_ResetDR();
- // CRC_CalcBlockCRC((uint32_t*)XSPL_addr, (XSPL_len+16)/4);
- // getcrc = CRC_GetCRC();
- // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE);
- // printf("read_crc:%x, get_crc:%x\n",crcVl,getcrc);
- // if(getcrc!=crcVl) { //校验错误
- // if(state)
- // {
- // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- // updata_state_save(UPDATA_CRC_ERROR);//存储升级状态
- // }
- // printf("update file crc error\r\n");// 升级文件crc校验错误
- // return ret;
- // }
- // //显示屏路由表校验正确,校验显示屏升级包
- // crcVl = *(uint32_t*)(XSPL_addr-4); //CRC校验
- // fixChar = *(uint32_t*)(XSPL_addr-8); // 固定字符 XSPA
- // fileVer = *(uint32_t*)(XSPL_addr-12); // 固件信息
- // filelen = *(uint32_t*)(XSPL_addr-20); // 文件总长度
- // XSPA_len = XSPL_addr-OTA_UPDATE_APP3_FLASH_START_ADDR-20;
- //
- // XSP_OTA_VER = fileVer;//显示屏版本号
- //
- // if(fixChar != XSPA_CHAR) //固定字符错误
- // {
- // if(state)
- // {
- // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- // updata_state_save(FIXED_CHARACTER_ERROR);//存储升级状态
- // }
- // printf("fixChar error\r\n");// 升级文件固定字符校验错误
- // return ret;
- // }
- // if(state)
- // {
- // printf("read_filever:%08x, update_ver:%08x\n",fileVer,ota_slave.update_version);
- // if((fileVer) != ota_slave.update_version) //固件信息错误
- // {
- // XSP_OTA_VER = 0;
- // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- // updata_state_save(DEVICE_TYPE_ERROR);//存储升级状态
- // printf("fileVer error\r\n");// 升级文件固件信息校验错误
- // return ret;
- // }
- // }
- // printf("read_filelen:%08x, total_bytes:%08x\n",filelen,(XSPA_len));
- // if(XSPA_len != (filelen))//长度错误
- // {
- // if(state)
- // {
- // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- // updata_state_save(UPDATA_PACKAGE_LENGTH_ERROR);//存储升级状态
- // }
- // printf("filelen error\r\n");// 升级文件长度校验错误
- // return ret;
- // }
- // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE);
- // CRC_ResetDR();
- // CRC_CalcBlockCRC((uint32_t*)OTA_UPDATE_APP3_FLASH_START_ADDR, (XSPA_len+16)/4);
- // getcrc = CRC_GetCRC();
- // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE);
- // printf("read_crc:%x, get_crc:%x\n",crcVl,getcrc);
- // if(getcrc!=crcVl) { //校验错误
- // if(state)
- // {
- // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
- // updata_state_save(UPDATA_CRC_ERROR);//存储升级状态
- // }
- // printf("update file crc error\r\n");// 升级文件crc校验错误
- // return ret;
- // }
- // printf("收包正确\n");
- // updata_state_save(UPDATA_DATA_SUCCESS);//存储升级状态
- //
- // //计算升级包信息
- // XSPH_APP_Len = *(uint16_t*)(XSPL_addr-20-2);
- // XSPH_APP_Addr = *(uint16_t*)(XSPL_addr-20-4);
- // XSPL_APP_Len = *(uint16_t*)(XSPL_addr-20-6);
- // XSPL_APP_Addr = *(uint16_t*)(XSPL_addr-20-8);
- // XSPL_APP_Crc = xsp_update_crc(OTA_UPDATE_APP3_FLASH_START_ADDR+XSPL_APP_Addr,XSPL_APP_Len);
- // XSPH_APP_Crc = xsp_update_crc(OTA_UPDATE_APP3_FLASH_START_ADDR+XSPH_APP_Addr,XSPH_APP_Len);
- // ota_xsp_pack_info.update_ver = fileVer;
- // printf("xspl addr=0x%04x, xspl len=%d, xspl crc=0x%04x\n",XSPL_APP_Addr,XSPL_APP_Len,XSPL_APP_Crc);
- // printf("xsph addr=0x%04x, xsph len=%d, xsph crc=0x%04x\n",XSPH_APP_Addr,XSPH_APP_Len,XSPH_APP_Crc);
- //
- // ret = 1;
- // return ret;
- }
- return ret;
- }
- //连续接收完成任务处理函数
- void ota_continuous_end_handle(void)
- {
- // uint32_t crcVl,fixChar,fileVer,filelen;
- // uint32_t getcrc;
- uint16_t i;
- uint8_t ret;
- switch(ota_state.update_state_slave)
- {
- case OTA_WORK_STATE_RCV_END:
- ota_timer_continuous_slave_set(CLOSE,0);//关闭连续定时器
- ota_timer_retransmit_slave_set(CLOSE,0);//关闭重传定时器
- ota_timer_retransmit_require_slave_set(CLOSE,0);//关闭重传请求定时器
- if(ota_slave.des_device_type == 0x0201)//采集器升级
- {
- ret = ota_update_pkg_check(0x0201,0);
- if(ret)//校验正确
- {
- if(ota_slave.des_device_type == (SOFTWARE_VERSION_APP&0xffff0000)>>16)//自身升级
- {
- //置升级标志
- updata_state_info.updata_flag = set_updata_state(UPDATA_FLAG);
- BKP_WriteBackupRegister(BKP_DR2, updata_state_info.updata_flag);
- //存储APP3版本
- device_version_info.APP3_ver = ota_slave.update_version;
- eeprom_write_data(EECFG_APP3_VER0,(uint8_t *)&device_version_info.APP3_ver,4);
-
- NVIC_SystemReset();
- }
- }
- }
- else if(ota_slave.des_device_type == XSP_0904)//显示屏升级
- {
- ret = ota_update_pkg_check(XSP_0904,0);
- if(ret)//校验正确
- {
- //存储APP3版本
- device_version_info.APP3_ver = ota_slave.update_version;
- eeprom_write_data(EECFG_APP3_VER0,(uint8_t *)&device_version_info.APP3_ver,4);
- ota_timer_onlooker_slave_set(CLOSE,0);//关闭旁观者定时器
- ota_timer_continuous_slave_set(CLOSE,0);//关闭连续发包定时器
- ota_timer_retransmit_slave_set(CLOSE,0);//关闭重传阶段定时器
- ota_timer_total_slave_set(CLOSE,0);//关闭总定时器
- memset(&ota_state,0,sizeof(ota_state));
- if(device_info.alrd == DEVICE_ALRD)//已初始化
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
- }
- else
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
- }
- device_fixed_info.xsp_update_start = 1;//升级显示屏标志
- printf("收包完成,开始升级显示屏 \n");
- }
- }
- // else if((ota_slave.des_device_type&0xf000) == 0x1000)//显示屏升级
- // {
- // ret = ota_update_pkg_check(DISPLAY,1);
- // if(ret)//校验正确
- // {
- // get_xsp_ota_info(XSPL_addr,XSPL_len);
- // if(check_route_xsp_ota())
- // {
- // ota_timer_onlooker_slave_set(CLOSE,0);//关闭旁观者定时器
- //
- // //显示屏升级标志存储
- // BKP_WriteBackupRegister_code(BKP_DR4,XSP_UPDATE);
- // //显示屏升级次数存储
- // BKP_WriteBackupRegister_code(BKP_DR5,0);
- //
- // xsp_ota_start();
- //// memset(&ota_xsp_info,0,sizeof(ota_xsp_info_t));
- // ota_xsp_init();
- // }
- //
- // }
- //
- //// device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
- //// ota_timer_onlooker_slave_set(CLOSE,0);
- //// ota_timer_total_slave_set(CLOSE,0);//关闭总定时器
- // }
- break;
- case OTA_WORK_STATE_RETRANSMIT://重传
- if(sys_lora_tx.lora_q.MsgQ.NbrEntries==0)//当前队列中的消息数。
- {
- for(i = 0;i < ota_slave.lossPkgIndex;i++) {
- if(ota_slave.lossPkgId[i])
- {
- ota_retransmit_req(ota_slave.lossPkgId[i]);
- printf("send retransmit pkgid:%d\n",ota_slave.lossPkgId[i]);
- break;
- }
- }
- }
- // ota_timer_retransmit_require_slave_set(OPEN,RETRANSMIT_DELAY_TIMEOUT_MIN);//打开重传延时定时器
- break;
- }
- }
- //从设备升级状态返回
- void lora_slave_ota_updatestate_resp(uint8_t updatestate)
- {
- collect_gateway_msg_format_t msg;
- ota_state_upload_t *p_ota = (ota_state_upload_t *)msg.info;
- p_ota->rcv_device_type = device_info.device_type;
- p_ota->rcv_device_sn = device_info.device_sn;
- p_ota->des_device_type = device_info.device_type;
- p_ota->des_device_sn = device_info.device_sn;
- p_ota->des_bt_ver = device_fixed_info.Soft_ver_boot;
- p_ota->des_app_ver = device_fixed_info.Soft_ver_app;
- p_ota->update_state = updatestate;
- p_ota->reserve = 0;
- coll_msg_send(&msg, net_msg_type_update_status_upload, OTA_STATE_UPLOAD_LEN);
-
- // slave_upload_info.num++;//上报次数
- // printf("slave send %d ",slave_upload_info.num);
- // data_dump("times",(uint8_t *)&msg,FRAME_HEADER_LEN + OTA_STATE_UPLOAD_LEN+2);
- }
- //从设备主动上报结束
- void slave_upload_end(void)
- {
- ota_timer_upload_cad_slave_set(CLOSE,0);//关闭上报间隔定时
- ota_timer_upload_total_slave_set(CLOSE,0);//关闭上报总定时
- if(device_info.alrd == DEVICE_ALRD)//已初始化
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
- }
- else
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
- }
- memset(&slave_upload_info,0,sizeof(slave_upload_info_t));
- memset(&ota_state,0,sizeof(ota_state_t));
- close_all_led();
- }
- //从设备升级定时器处理
- void ota_slave_time_handle(void)
- {
- ota_timeout_onlooker_slave(); //旁观者定时器超时处理
- ota_timeout_continuous_slave(); //连续阶段定时器超时处理
- ota_timeout_retransmit_slave(); //重传阶段定时器超时处理
- ota_timeout_total_slave(); //总定时器超时处理
- ota_timeout_retransmit_require_slave(); //重传请求超时处理
- ota_timeout_upload_cad_slave(); //主动上报升级状态间隔超时处理
- ota_timeout_upload_total_slave(); //主动上报升级状态总定时超时处理
- }
- ////////////////////////////////////////////////////////////////////////////////////
- ////离线升级
- //主设备总定时器设置
- void ota_timer_total_master_set(uint8_t state, uint32_t timeout)
- {
- if(state == OPEN)
- {
- timeout_setValue(&ota_master.tt_ota_total,timeout,1);
- }
- else if(state == CLOSE)
- {
- timeout_stop(&ota_master.tt_ota_total);
- }
- }
- //主设备总定时超时
- void ota_timeout_total_master(void)
- {
- if(timeout_isOut(&ota_master.tt_ota_total))
- {
- if(ota_state.update_mode != OTA_WORK_STATE_XSP)
- master_rcv_total_proc();//离线升级时打印
-
-
- if(device_info.alrd == DEVICE_ALRD)//已初始化
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
- }
- else
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
- }
- memset(&ota_state,0,sizeof(ota_state_t));
- ota_timer_retransmit_master_set(CLOSE,0);//关闭重传定时器
- close_all_led();
- printf("master total timeout \n");
- }
- }
- //主设备重传定时器设置
- void ota_timer_retransmit_master_set(uint8_t state, uint32_t timeout)
- {
- if(state == OPEN)
- {
- timeout_setValue(&ota_master.tt_ota_retransmit,timeout,1);
- }
- else if(state == CLOSE)
- {
- timeout_stop(&ota_master.tt_ota_retransmit);
- }
- }
- //主设备重传定时超时
- void ota_timeout_retransmit_master(void)
- {
- if(timeout_isOut(&ota_master.tt_ota_retransmit))
- {
- if(ota_state.update_mode != OTA_WORK_STATE_XSP)
- master_rcv_total_proc();//离线升级时打印
-
- memset(&ota_state,0,sizeof(ota_state_t));
- ota_timer_total_master_set(CLOSE,0);//关闭总定时器
- close_all_led();
- if(device_info.alrd == DEVICE_ALRD)//已初始化
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
- }
- else
- {
- device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
- }
- printf("master update over\n");
- }
- }
- //读取app程序包的大小并校验
- uint32_t Read_APP_datalen_check(uint32_t addr, uint32_t len)
- {
- uint32_t datalen = 0;
- uint32_t crcVl,fixChar,fileVer,filelen,getcrc,t_addr;
- uint32_t bin_data[6] = {0};
- uint8_t flag = 0;
- uint8_t check_flag = 0;
- printf("addr=%08x\n",addr);
- datalen = Flash_DatalenRead(addr,len);
- filelen = *(uint32_t*)(addr+datalen-20); // 文件总长度
- printf("datalen=%08x,%u; filelen=%08x,%u\n",datalen,datalen,filelen,filelen);
- if(datalen > (128*1024))
- {
- printf("升级包长度超过128K: %d\n",datalen);
- datalen = 0;
- }
- else
- {
- printf("addr=%08x\n",addr);
- crcVl = *(uint32_t*)(addr + datalen-4); //CRC校验
- fixChar = *(uint32_t*)(addr + datalen-8); // 固定字符 WBJW
- fileVer = *(uint32_t*)(addr + datalen-12); // 固件信息
- filelen = *(uint32_t*)(addr + datalen-20); // 文件总长度
- t_addr = *(uint32_t*)(addr + datalen-16); // 文件写入的地址
-
- printf("crcVl=%08x,fixChar=%08x,fileVer=%08x,filelen=%08x,t_addr=%08x\n",crcVl,fixChar,fileVer,filelen,t_addr);
-
- //判断后20字节是否正确
- if((fixChar == WBJW_CHAR) && (t_addr == 0x08020000))//升级包中包含了后20字节
- {
- printf("升级包包含了后20字节\n");
- if(datalen != (filelen+20))
- {
- printf("升级包长度校验错误\n");
- check_flag = 1;
- }
- else if(fileVer != device_fixed_info.Soft_ver_app)
- {
- printf("固件信息错误\n");
- printf("filever=%08x,app=%08x\n",fileVer,device_fixed_info.Soft_ver_app);
- check_flag = 1;
- }
- else
- {
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE);
- CRC_ResetDR();
- CRC_CalcBlockCRC((uint32_t*)addr, (datalen -4)/4);
- getcrc = CRC_GetCRC();
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE);
- printf("read_crc:%08x, get_crc:%08x\n",crcVl,getcrc);
-
- if(crcVl != getcrc)
- {
- printf("crc 错误\n");
- check_flag = 1;
- }
- }
- }
- else
- {
- check_flag = 1;
- }
- if(check_flag)//需要计算后20个字节
- {
- bin_data[0] = datalen; //原始bin文件字节长度
- bin_data[1] = 0x08020000; //存储地址
- bin_data[2] = device_fixed_info.Soft_ver_app; //固件版本
- bin_data[3] = WBJW_CHAR; //固定标记,"WBJW"
- flag = Flash_BufferWrite(addr+datalen,bin_data,16);
- printf("first flag = %d\n",flag);
-
- datalen += 20;
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE);
- CRC_ResetDR();
- CRC_CalcBlockCRC((uint32_t*)addr, (datalen -4)/4);
- getcrc = CRC_GetCRC();
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE);
- bin_data[0] = getcrc;
- printf("crc=%08x\n",bin_data[0]);
- flag = Flash_BufferWrite(addr+datalen-4,bin_data,4); //crc校验
- printf("second flag = %d\n",flag);
- }
- }
-
- return datalen;
- }
- //升级开始指令
- void lora_master_ota_start(void)
- {
- uint32_t timer = 0;
- collect_gateway_msg_format_t msg;
- ota_start_info_t *p_ota = (ota_start_info_t *)msg.info;
- p_ota->des_device_type = ota_master.des_device_type; // device_info.device_type;
- p_ota->des_device_sn = ota_master.des_device_sn; //0xffffffff;
- p_ota->rcv_device_type = ota_master.rcv_device_type; //device_info.device_type;
- p_ota->rcv_device_sn = ota_master.rcv_device_sn; //0xffffffff;
- p_ota->update_version = ota_master.update_version; //device_fixed_info.Soft_ver_app;
- p_ota->total_bytes = ota_master.total_bytes;
- timer = RadioTimeOnAir2(MODEM_LORA,140);
- timer = (timer>UPDATE_EACH_TIME) ? timer : UPDATE_EACH_TIME;
- p_ota->Send_interval = timer;
- printf("total_bytes = %d, send interval = %d\n",p_ota->total_bytes, timer);
- coll_msg_send(&msg, net_msg_type_update_start, OTA_START_INFO_LEN);
- }
- //网关下发离线升级时初始化
- int lora_master_ota_gateway_info_init(gateway_coll_offline_upgrade_t *p_off)
- {
- memset(&ota_master, 0, sizeof(ota_pkg_info_t));
- ota_master.des_device_type = p_off->des_device_type;
- ota_master.des_device_sn = p_off->des_device_sn;
- ota_master.rcv_device_type = p_off->slave_device_type;
- ota_master.rcv_device_sn = p_off->slave_device_sn;
- ota_master.update_version = device_fixed_info.Soft_ver_app;
- ota_master.total_bytes = 0;
- ota_master.total_crc = 0;
- ota_master.total_pkgs = 0;
- ota_master.lastRightPkgNum = 1;
- ota_master.sub_pkg_id = 1;
- return 0;
- }
- //按键触发离线升级时初始化
- int lora_master_ota_key_info_Init(uint32_t total_len, uint32_t total_crc)
- {
- memset(&ota_master, 0, sizeof(ota_pkg_info_t));
- ota_master.des_device_type = device_info.device_type;
- ota_master.des_device_sn = 0xffffffff;
- ota_master.rcv_device_sn = 0xffffffff;
- ota_master.rcv_device_type = device_info.device_type;
- ota_master.update_version = device_fixed_info.Soft_ver_app;
- ota_master.total_bytes = total_len;
- ota_master.total_crc = total_crc;
- ota_master.total_pkgs = total_len/OTA_UPDATE_PAYLAOA_LEN;
- ota_master.lastRightPkgNum = 1;
- if(total_len%OTA_UPDATE_PAYLAOA_LEN) {
- ota_master.total_pkgs += 1;
- }
- ota_master.sub_pkg_id = 1;
- return 0;
- }
- //连续发送阶段
- void lora_master_ota_continues(void)
- {
- collect_gateway_msg_format_t msg;
- ota_continues_info_t *p_ota = (ota_continues_info_t *)msg.info;
- OS_ERR err;
- uint32_t timer = 0;
- char buff[128] = {0};
- // uint16_t i=0;
- printf("lora_gw_ota_continues\r\n");
- p_ota->total_pkgs = ota_master.total_pkgs;
- p_ota->sub_pkg_len = OTA_UPDATE_PAYLAOA_LEN;
- p_ota->des_device_type = ota_master.des_device_type;
- p_ota->des_device_sn = ota_master.des_device_sn;
- p_ota->rcv_device_sn = ota_master.rcv_device_sn;
- p_ota->rcv_device_type = ota_master.rcv_device_type;
- timer = RadioTimeOnAir2(MODEM_LORA,140);
- timer = (timer>UPDATE_EACH_TIME) ? timer : UPDATE_EACH_TIME;
- printf("timer = %d\r\n", timer);
- printf("lora_update total_pkgs = %d\r\n",ota_master.total_pkgs);
- while(ota_master.sub_pkg_id <= ota_master.total_pkgs) {
- printf("lora_gw_ota_continues {%d/%d}\r\n", ota_master.total_pkgs,ota_master.sub_pkg_id);
- p_ota->sub_pkg_id = ota_master.sub_pkg_id++;
- Flash_BufferRead(OTA_UPDATE_APP1_FLASH_START_ADDR + (p_ota->sub_pkg_id - 1)*OTA_UPDATE_PAYLAOA_LEN,
- (uint8_t *)buff, OTA_UPDATE_PAYLAOA_LEN);
- memcpy(p_ota->pkg_content,buff,128);
- coll_msg_send(&msg, net_msg_type_update_continues, OTA_CONTINUES_INFO_LEN + OTA_UPDATE_PAYLAOA_LEN);
- //指示灯显示(显示剩余包数后四位)
- Data_last4bit_led(ota_master.total_pkgs-ota_master.sub_pkg_id,1);
-
- OSTimeDlyHMSM(0, 0, 0, timer, OS_OPT_TIME_DLY, &err);
- }
- }
- //发送重传包
- void lora_master_ota_retransmit_resp(ota_retransmit_req_t *p_req)
- {
- collect_gateway_msg_format_t msg;
- ota_continues_info_t *p_ota = (ota_continues_info_t *)msg.info;
- char buff[128] = {0};
- p_ota->total_pkgs = ota_master.total_pkgs;
- p_ota->sub_pkg_id = p_req->repeat_sub_pkg_id;
- p_ota->sub_pkg_len = OTA_UPDATE_PAYLAOA_LEN;
- p_ota->des_device_type = ota_master.des_device_type;
- p_ota->des_device_sn = ota_master.des_device_sn;
- p_ota->rcv_device_sn = ota_master.rcv_device_sn;
- p_ota->rcv_device_type = ota_master.rcv_device_type;
- Flash_BufferRead(OTA_UPDATE_APP1_FLASH_START_ADDR + (p_req->repeat_sub_pkg_id - 1)*OTA_UPDATE_PAYLAOA_LEN,
- (uint8_t *)buff, OTA_UPDATE_PAYLAOA_LEN);
- memcpy(p_ota->pkg_content,buff,128);
- coll_msg_send(&msg, net_msg_type_update_retransmit_rsp, OTA_CONTINUES_INFO_LEN + OTA_UPDATE_PAYLAOA_LEN);
- }
- //主设备收到状态上报指令处理
- void master_upload_proc(collect_gateway_msg_format_t *p_msg)
- {
- int i;
- ota_state_upload_t *p_ota = (ota_state_upload_t *)p_msg->info;
- master_rcv_info.rcv_print_num++;
- printf("第%03d条:des_type:%04x,des_sn:%08x,des_bt:%08x,des_app:%08x,update_state:%02x\n",master_rcv_info.rcv_print_num, p_ota->des_device_type,\
- p_ota->des_device_sn,p_ota->des_bt_ver,p_ota->des_app_ver,p_ota->update_state);
- if(master_rcv_info.rcv_total_num < 64)
- {
- if(master_rcv_info.rcv_total_num)
- {
- for(i = 0; i < master_rcv_info.rcv_total_num; i++)
- {
- if(master_rcv_info.master_rcv_update[i].des_sn == p_ota->des_device_sn)
- {
- master_rcv_info.master_rcv_update[i].des_type = p_ota->des_device_type;
- master_rcv_info.master_rcv_update[i].des_sn = p_ota->des_device_sn;
- master_rcv_info.master_rcv_update[i].des_bt = p_ota->des_bt_ver;
- master_rcv_info.master_rcv_update[i].des_app = p_ota->des_app_ver;
- master_rcv_info.master_rcv_update[i].state = p_ota->update_state;
- return;
- }
- }
- }
- master_rcv_info.master_rcv_update[master_rcv_info.rcv_total_num].des_type = p_ota->des_device_type;
- master_rcv_info.master_rcv_update[master_rcv_info.rcv_total_num].des_sn = p_ota->des_device_sn;
- master_rcv_info.master_rcv_update[master_rcv_info.rcv_total_num].des_bt = p_ota->des_bt_ver;
- master_rcv_info.master_rcv_update[master_rcv_info.rcv_total_num].des_app = p_ota->des_app_ver;
- master_rcv_info.master_rcv_update[master_rcv_info.rcv_total_num].state = p_ota->update_state;
- master_rcv_info.rcv_total_num++;
-
- }
- }
- int cmp(const void *a, const void *b)
- {
- // return (*(uint32_t *)a - *(uint32_t *)b);
- return ((*(uint32_t *)a>*(uint32_t *)b)?1:-1); //从小到大排序
- }
- //主设备统计状态
- void master_rcv_total_proc(void)
- {
- uint32_t des_sn[64] = {0};
- int i;
- for(i = 0; i < master_rcv_info.rcv_total_num; i++)
- {
- if(master_rcv_info.master_rcv_update[i].state == UPDATA_SUCCESS)
- {
- des_sn[master_rcv_info.rcv_success_num] = master_rcv_info.master_rcv_update[i].des_sn;
- master_rcv_info.rcv_success_num++;
- }
- }
- printf("升级成功%d个:\n",master_rcv_info.rcv_success_num);
-
- //快排(从小到大排序)
- qsort(des_sn,master_rcv_info.rcv_success_num,sizeof(des_sn[0]),cmp);
-
- for(i = 0; i < master_rcv_info.rcv_success_num; i++)
- {
- if((i%4 == 0)&&i) {
- if((i%8 == 0)&&i) {
- printf("\r\n");
- } else {
- printf(" ");
- }
- }
- printf("%010u ",des_sn[i]);
- }
- printf("\n");
- }
- //收到重传请求后下发命令
- void lora_master_ota_proc(collect_gateway_msg_format_t *p_msg)
- {
- ota_retransmit_req_t *p_req = NULL;
-
- if(ota_state.update_mode == OTA_WORK_STATE_ONLOOKER)
- {
- ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);
- return;
- }
- switch(p_msg->first_type) {
- case net_msg_type_update_retransmit_req://从设备的重传请求
- p_req = (ota_retransmit_req_t *)p_msg->info;
- lora_master_ota_retransmit_resp(p_req);
- ota_timer_retransmit_master_set(OPEN,OTA_MASTER_WAIT_TIME_TICK);//主设备等待定时器
- break;
- case net_msg_type_update_status:
- break;
- case net_msg_type_update_status_upload://从设备的状态返回
- ota_timer_retransmit_master_set(OPEN,OTA_MASTER_WAIT_TIME_TICK);//主设备等待定时器
- master_upload_proc(p_msg);
- break;
- default:
- break;
- }
- }
- //离线升级时的主设备
- void Offline_upgrade_master(void)
- {
- OS_ERR err;
- uint32_t total_bytes = 0;
- uint32_t i = 0;
-
- //清空接收信息
- memset(&master_rcv_info,0,sizeof(master_rcv_info_t));
-
- //打开总定时器
- ota_timer_total_master_set(OPEN,OTA_TOTAL_TIMEOUT_TIME_TICK);
-
- //升级指示灯
- ota_start_led_init();
- Led_Single_State_Set(UPDATA_MASTER_LED,LED_ON);
-
- total_bytes = Read_APP_datalen_check(OTA_UPDATE_APP1_FLASH_START_ADDR,OTA_UPDATE_APP1_FLASH_SIZE);
- if(total_bytes==0) //升级包长度错误,升级结束,总定时设置为1ms
- {
- ota_timer_total_master_set(OPEN,1);
- return;
- }
-
- printf("send total_bytes=%d\n",total_bytes);
- if(ota_state.offline_mode == KEY_TRIGGER)
- {
- lora_master_ota_key_info_Init(total_bytes,0);
- printf("按键触发\n");
- }
- else if(ota_state.offline_mode == GATEWAY_TRIGGER)
- {
- printf("命令触发\n");
- ota_master.total_bytes = total_bytes;
- ota_master.total_pkgs = total_bytes/OTA_UPDATE_PAYLAOA_LEN;
- if(total_bytes%OTA_UPDATE_PAYLAOA_LEN)
- {
- ota_master.total_pkgs++;
- }
- }
-
- for(i = 0; i < 5; i++)
- {
- lora_master_ota_start();
- OSTimeDlyHMSM(0, 0, 2, 0, OS_OPT_TIME_DLY, &err);
- }
- lora_master_ota_continues();
-
- //连续发送完成,打开等待定时器,如果一定时间内没收到重传请求或状态上报则认为升级结束
- ota_timer_retransmit_master_set(OPEN,OTA_MASTER_WAIT_TIME_TICK);
- }
- //离线升级主设备定时处理
- void ota_master_time_handle(void)
- {
- ota_timeout_retransmit_master();//重传定时超时
- ota_timeout_total_master();//总定时超时
- }
- //显示屏升级重启后升级
- uint32_t xsp_update_reboot(void)
- {
- uint8_t ret;
- uint32_t datalen = 0;
- uint32_t filelen,fixChar;//fileVer,crcVl,getcrc,t_addr;
- datalen = Flash_DatalenRead(OTA_UPDATE_APP3_FLASH_START_ADDR,OTA_UPDATE_APP3_FLASH_SIZE);
- filelen = *(uint32_t*)(OTA_UPDATE_APP3_FLASH_START_ADDR+datalen-20); // 文件总长度
- fixChar = *(uint32_t*)(OTA_UPDATE_APP3_FLASH_START_ADDR+datalen-8); // 固定字符 WBJW
- printf("datalen=%08x,%u; filelen=%08x,%u\n",datalen,datalen,filelen,filelen);
- if(datalen > (128*1024) || datalen == 0 || filelen > (datalen) || filelen == 0)
- {
- printf("升级包长度错误: %d\n",datalen);
- //清除显示屏升级标志
- BKP_WriteBackupRegister_code(BKP_DR4,0);
- //显示屏升级次数存储
- BKP_WriteBackupRegister_code(BKP_DR5,0);
- return datalen;
- }
- memset(&ota_slave,0,sizeof(ota_pkg_info_t));
- ota_slave.total_bytes = datalen;
- XSPL_addr = OTA_UPDATE_APP3_FLASH_START_ADDR+datalen-filelen-20;
- XSPL_len = filelen;
- printf("addr=%08x\n",XSPL_addr);
- // crcVl = *(uint32_t*)(XSPL_addr + datalen-4); //CRC校验
- // fixChar = *(uint32_t*)(XSPL_addr + datalen-8); // 固定字符 WBJW
- // fileVer = *(uint32_t*)(XSPL_addr + datalen-12); // 固件信息
- // filelen = *(uint32_t*)(XSPL_addr + datalen-20); // 文件总长度
- // t_addr = *(uint32_t*)(XSPL_addr + datalen-16); // 文件写入的地址
-
- if(fixChar != XSPL_CHAR)//显示屏
- {
- //清除显示屏升级标志
- BKP_WriteBackupRegister_code(BKP_DR4,0);
- //显示屏升级次数存储
- BKP_WriteBackupRegister_code(BKP_DR5,0);
- return datalen;
- }
- ret = ota_update_pkg_check(0x0401,0);
- if(ret)//校验正确
- {
- get_xsp_ota_info(XSPL_addr,XSPL_len);
- if(check_route_xsp_ota())
- {
- printf("xsp ota\n");
- // device_fixed_info.Work_State = DEV_WORK_STATE_XSP_UPDATING;
- // ota_xsp_info.xsp_ota_begin = 1;
- xsp_ota_start();
- // memset(&ota_xsp_info,0,sizeof(ota_xsp_info_t));
- ota_xsp_init();
- }
- }
- else
- {
- //清除显示屏升级标志
- BKP_WriteBackupRegister_code(BKP_DR4,0);
- //显示屏升级次数存储
- BKP_WriteBackupRegister_code(BKP_DR5,0);
- }
- return datalen;
- }
- //测试。下发开始离线升级命令
- void lora_offline_update(uint32_t master_sn, uint32_t slave_sn, uint32_t des_sn)
- {
- collect_gateway_msg_format_t msg;
- gateway_coll_offline_upgrade_t *p_off = (gateway_coll_offline_upgrade_t *)msg.info;
- p_off->master_device_type = 0x0201;
- p_off->master_device_sn = master_sn;
- p_off->slave_device_type = 0x0201;
- p_off->slave_device_sn = slave_sn;
- p_off->des_device_type = 0x0201;
- p_off->des_device_sn = des_sn;
- p_off->reserve = 0;
- coll_msg_send(&msg, net_msg_type_update_offline, OTA_OFFLINE_LEN);
- // data_dump("offline",(uint8_t *)&msg,FRAME_HEADER_LEN+OTA_OFFLINE_LEN+2);
- }
|