ota.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. /*
  2. * @Description:
  3. * @Version: 2.0
  4. * @Author: Seven
  5. * @Date: 2022-08-22 17:13:45
  6. * @LastEditors: Seven
  7. * @LastEditTime: 2022-12-19 14:00:16
  8. */
  9. #include "ota.h"
  10. #include "includes.h"
  11. #include "gw_ctrl.h"
  12. #include "lora.h"
  13. #include "radio.h"
  14. uint8_t updata_state = 0;
  15. ota_state_t ota_state;
  16. ota_pkg_info_t ota_slave;
  17. ota_pkg_info_t ota_master;
  18. master_rcv_info_t master_rcv_info;
  19. slave_upload_info_t slave_upload_info;
  20. //比较接收设备的类型和sn
  21. int device_type_and_id_cmp(u32 device_type, u32 device_sn)
  22. {
  23. int ret = 0;
  24. if(device_type == 0x0201/*device_info.device_type*/)
  25. {
  26. if(device_sn == device_info.device_sn || device_sn == 0xFFFFFFFF)
  27. {
  28. ret = 1;
  29. }
  30. }
  31. else if(device_type == 0xffff && device_sn == 0xffffffff)
  32. {
  33. ret = 1;
  34. }
  35. return ret;
  36. }
  37. //存储升级状态
  38. void updata_state_save(uint8_t state)
  39. {
  40. uint16_t updata_state_temp;
  41. updata_state = state;
  42. updata_state_temp = set_updata_state(updata_state);
  43. BKP_WriteBackupRegister(BKP_DR3, updata_state_temp);
  44. }
  45. //向备份寄存器存储转义数据
  46. void BKP_WriteBackupRegister_code(uint16_t BKP_DR, uint8_t Data)
  47. {
  48. uint16_t temp;
  49. temp = set_updata_state(Data);
  50. BKP_WriteBackupRegister(BKP_DR, temp);
  51. }
  52. //采集器主动发送
  53. void coll_msg_send(collect_gateway_msg_format_t *p_msg, uint8_t msg_type, uint16_t len)
  54. {
  55. static uint32_t msg_no = 0;
  56. char *msg = NULL;
  57. uint16_t *p_crc;
  58. p_msg->frame_header = 0xfefe;
  59. p_msg->proto_Ver = PROTOCOL_VERSION;
  60. p_msg->msg_id = msg_no++;
  61. p_msg->first_type = msg_type;
  62. p_msg->second_type = 0x00;
  63. p_msg->msg_len = len+2;//后面加两字节的CRC
  64. p_crc = (uint16_t*)&p_msg->info[len];
  65. *p_crc = CRC16_get((uint8_t *)p_msg,p_msg->msg_len+FRAME_HEADER_LEN-2);
  66. msg = (char *)lora_queue_mem_calloc_must(SYS_LORA_TX);
  67. if(msg) {
  68. memcpy(msg, (char *)p_msg, FRAME_HEADER_LEN + len+2);
  69. lora_queue_insert(SYS_LORA_TX,(char *)msg, FRAME_HEADER_LEN + len+2);
  70. }
  71. else
  72. {
  73. lora_queue_mem_free(SYS_LORA_TX,msg);
  74. }
  75. }
  76. //升级重传函数
  77. void ota_retransmit_req(uint16_t repeat_sub_pkg_id)
  78. {
  79. collect_gateway_msg_format_t msg;
  80. ota_retransmit_req_t *p_ota = (ota_retransmit_req_t *)msg.info;
  81. p_ota->repeat_devict_type = ota_slave.rcv_device_type;
  82. p_ota->repeat_device_sn = device_info.device_sn;
  83. p_ota->repeat_sub_pkg_id = repeat_sub_pkg_id;
  84. coll_msg_send(&msg, net_msg_type_update_retransmit_req, OTA_RETRNSMIT_INFO_LEN);
  85. }
  86. //存储设备升级信息
  87. int lora_node_ota_info_Init(ota_pkg_info_t *ota_info,ota_start_info_t *p_ota)
  88. {
  89. // memset(ota_info, 0, sizeof(ota_pkg_info_t));
  90. ota_info->rcv_device_type = p_ota->rcv_device_type;
  91. ota_info->rcv_device_sn = p_ota->rcv_device_sn;
  92. ota_info->des_device_type = p_ota->des_device_type;
  93. ota_info->des_device_sn = p_ota->des_device_sn;
  94. ota_info->update_version = p_ota->update_version;
  95. ota_info->total_bytes = p_ota->total_bytes;
  96. ota_info->total_pkgs = p_ota->total_bytes/OTA_UPDATE_PAYLAOA_LEN;
  97. ota_info->lossPkgIndex = 0;
  98. ota_info->lastRightPkgNum = 1;
  99. if(p_ota->total_bytes%OTA_UPDATE_PAYLAOA_LEN) {
  100. ota_info->total_pkgs += 1;
  101. }
  102. ota_info->lossPkgCounter = 0;
  103. ota_info->sub_pkg_id = 1;
  104. ota_info->Send_interval = p_ota->Send_interval;
  105. return 0;
  106. }
  107. //从设备设置旁观者定时器
  108. void ota_timer_onlooker_slave_set(uint8_t state, uint32_t timeout)
  109. {
  110. if(state == OPEN)
  111. {
  112. //升级指示灯
  113. ota_start_led_init();
  114. Led_Single_State_Set(UPDATA_ONLOOKER_LED,LED_ON);
  115. Data_last4bit_led(0xff,1);
  116. ota_state.update_mode = OTA_WORK_STATE_ONLOOKER;
  117. timeout_setValue(&ota_slave.tt_ota_onlooker,timeout,1);
  118. timeout_stop(&ota_slave.tt_ota_continuous);//关闭连续定时
  119. timeout_stop(&ota_slave.tt_ota_retransmit);//关闭重传定时
  120. }
  121. else if(state == CLOSE)
  122. {
  123. timeout_stop(&ota_slave.tt_ota_onlooker);
  124. }
  125. }
  126. //从设备旁观者超时处理
  127. void ota_timeout_onlooker_slave(void)
  128. {
  129. if(timeout_isOut(&ota_slave.tt_ota_onlooker))
  130. {
  131. memset(&ota_state,0,sizeof(ota_state));
  132. ota_timer_continuous_slave_set(CLOSE,0);//关闭连续发包定时器
  133. ota_timer_retransmit_slave_set(CLOSE,0);//关闭重传阶段定时器
  134. ota_timer_total_slave_set(CLOSE,0);//关闭总定时器
  135. if(updata_state)//上报升级状态
  136. {
  137. device_fixed_info.slave_uploade_start = 1;
  138. }
  139. else
  140. {
  141. if(device_info.alrd == DEVICE_ALRD)//已初始化
  142. {
  143. device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
  144. }
  145. else
  146. {
  147. device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
  148. }
  149. //升级指示灯
  150. led_run_set(LED_ALL,LED_OFF);
  151. }
  152. printf("onlooker timeout \n");
  153. }
  154. }
  155. //从设备设置重传定时器
  156. void ota_timer_retransmit_slave_set(uint8_t state, uint32_t timeout)
  157. {
  158. if(state == OPEN)
  159. {
  160. ota_state.update_state_slave = OTA_WORK_STATE_RETRANSMIT;
  161. timeout_setValue(&ota_slave.tt_ota_retransmit, timeout, 1);
  162. }
  163. else if(state == CLOSE)
  164. {
  165. timeout_stop(&ota_slave.tt_ota_retransmit);
  166. }
  167. }
  168. //从设备重传超时
  169. void ota_timeout_retransmit_slave(void)
  170. {
  171. if(timeout_isOut(&ota_slave.tt_ota_retransmit))
  172. {
  173. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  174. updata_state_save(RETRANSMIT_TIMEOUT);//存储升级状态
  175. printf("retransmit timeout \n");
  176. }
  177. }
  178. //从设备设置连续接收定时器
  179. void ota_timer_continuous_slave_set(uint8_t state, uint32_t timeout)
  180. {
  181. if(state == OPEN)
  182. {
  183. ota_state.update_state_slave = OTA_WORK_STATE_CONTINUOUS;
  184. timeout_setValue(&ota_slave.tt_ota_continuous,timeout,1);
  185. }
  186. else if(state == CLOSE)
  187. {
  188. timeout_stop(&ota_slave.tt_ota_continuous);
  189. }
  190. }
  191. //从设备连续接收超时
  192. void ota_timeout_continuous_slave(void)
  193. {
  194. uint16_t i;
  195. if(timeout_isOut(&ota_slave.tt_ota_continuous))
  196. {
  197. //更新收包相关状态
  198. for(i = ota_slave.lastRightPkgNum;i < ota_slave.total_pkgs+1;i++) {
  199. ota_slave.lossPkgCounter++;
  200. ota_slave.lossPkgId[ota_slave.lossPkgIndex++] = i;//+1因为子包编号从0开始,
  201. //丢包太多了,超过了MAX_LOSS_PKG_COUNT,结束本次升级。
  202. if(ota_slave.lossPkgCounter == MAX_LOSS_PKG_COUNT) {//降为旁观者, 放弃本次升级
  203. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  204. updata_state_save(PACKET_LOSS_MORE);//存储升级状态
  205. return;
  206. }
  207. }
  208. if(ota_slave.lossPkgCounter == 0) //没有丢包
  209. {
  210. ota_state.update_state_slave = OTA_WORK_STATE_RCV_END;
  211. printf("check begin\n");
  212. ota_continuous_end_handle();
  213. }
  214. else //有丢包
  215. {
  216. ota_timer_retransmit_require_slave_set(OPEN,RETRANSMIT_DELAY_TIMEOUT_MIN);//打开重传延时定时器
  217. ota_timer_retransmit_slave_set(OPEN,RETRANSMIT_TIMEOUT_TIME); //打开重传定时器
  218. printf("retransmit begin\n");
  219. }
  220. }
  221. }
  222. //从设备总定时器设置
  223. void ota_timer_total_slave_set(uint8_t state, uint32_t timeout)
  224. {
  225. if(state == OPEN)
  226. {
  227. timeout_setValue(&ota_slave.tt_ota_total,timeout,1);
  228. }
  229. else if(state == CLOSE)
  230. {
  231. timeout_stop(&ota_slave.tt_ota_total);
  232. }
  233. }
  234. //从设备总定时超时
  235. void ota_timeout_total_slave(void)
  236. {
  237. if(timeout_isOut(&ota_slave.tt_ota_total))
  238. {
  239. if(updata_state)//上报升级状态
  240. {
  241. device_fixed_info.slave_uploade_start = 1;
  242. }
  243. else
  244. {
  245. if(device_info.alrd == DEVICE_ALRD)//已初始化
  246. {
  247. device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
  248. }
  249. else
  250. {
  251. device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
  252. }
  253. }
  254. ota_timer_onlooker_slave_set(CLOSE,0);//关闭旁观者定时器
  255. ota_timer_continuous_slave_set(CLOSE,0);//关闭连续发包定时器
  256. ota_timer_retransmit_slave_set(CLOSE,0);//关闭重传阶段定时器
  257. memset(&ota_state,0,sizeof(ota_state));
  258. updata_state_save(UPDATA_TOTAL_TIMEOUT);//存储升级状态
  259. printf("total timeout \n");
  260. }
  261. }
  262. //从设备重传请求间隔定时设置
  263. void ota_timer_retransmit_require_slave_set(uint8_t state, uint32_t timeout)
  264. {
  265. if(state == OPEN)
  266. {
  267. timeout_setValue(&ota_slave.tt_ota_retransmit_delay,timeout,1);
  268. }
  269. else if(state == CLOSE)
  270. {
  271. timeout_stop(&ota_slave.tt_ota_retransmit_delay);
  272. }
  273. }
  274. //从设备重传请求超时处理
  275. void ota_timeout_retransmit_require_slave(void)
  276. {
  277. if(timeout_isOut(&ota_slave.tt_ota_retransmit_delay))
  278. {
  279. ota_continuous_end_handle();
  280. }
  281. }
  282. //从设备主动上报升级状态总定时设置
  283. void ota_timer_upload_total_slave_set(uint8_t state, uint32_t timeout)
  284. {
  285. if(state == OPEN)
  286. {
  287. ota_state.update_mode = OTA_WORK_STATE_UPLOAD;
  288. timeout_setValue(&slave_upload_info.upload_total_time,timeout,1);
  289. }
  290. else if(state == CLOSE)
  291. {
  292. timeout_stop(&slave_upload_info.upload_total_time);
  293. }
  294. }
  295. //从设备主动上报升级状态总定时超时处理
  296. void ota_timeout_upload_total_slave(void)
  297. {
  298. if(timeout_isOut(&slave_upload_info.upload_total_time))
  299. {
  300. slave_upload_end();
  301. }
  302. }
  303. //从设备主动上报升级状态发送间隔定时设置
  304. void ota_timer_upload_cad_slave_set(uint8_t state, uint32_t timeout)
  305. {
  306. if(state == OPEN)
  307. {
  308. timeout_setValue(&slave_upload_info.upload_cad_time,timeout,1);
  309. }
  310. else if(state == CLOSE)
  311. {
  312. timeout_stop(&slave_upload_info.upload_cad_time);
  313. }
  314. }
  315. //从设备主动上报升级状态发送间隔超时处理
  316. void ota_timeout_upload_cad_slave(void)
  317. {
  318. if(timeout_isOut(&slave_upload_info.upload_cad_time))
  319. {
  320. lora_slave_ota_updatestate_resp(updata_state);
  321. }
  322. }
  323. uint32_t OTA_Continuous_Timeout = 0;
  324. //网关下发的升级命令分析
  325. void gateway_lora_ota_proc(collect_gateway_msg_format_t *p_msg)
  326. {
  327. uint8_t flash_flag=0;
  328. uint32_t i;
  329. uint32_t remain_pkg = 0;
  330. ota_start_info_t *p_start = NULL;
  331. ota_continues_info_t *p_cont = NULL;
  332. gateway_coll_offline_upgrade_t *p_off = NULL;
  333. if(device_fixed_info.Work_State != DEV_WORK_STATE_UPDATING)
  334. {
  335. device_fixed_info.Work_State = DEV_WORK_STATE_UPDATING;
  336. updata_state = 0;
  337. srand(device_info.device_sn);//随机数种子
  338. memset(&ota_state,0,sizeof(ota_state));
  339. }
  340. if(ota_state.update_mode == OTA_WORK_STATE_ONLOOKER)//旁观者
  341. {
  342. printf("p_msg->type:%x\n",p_msg->first_type);
  343. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);
  344. }
  345. else//非旁观者
  346. {
  347. switch(p_msg->first_type)
  348. {
  349. case net_msg_type_update_start://开始升级指令
  350. p_start = (ota_start_info_t *)p_msg->info;
  351. printf("p_msg->type:%x\n",p_msg->first_type);
  352. /* 校验当前设备是否为接收对象 */
  353. 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))
  354. {
  355. ota_state.update_mode = OTA_WORK_STATE_SLAVE;
  356. if(p_start->total_bytes > OTA_UPDATE_APP3_FLASH_SIZE)//升级包总长度大于存储区域大小
  357. {
  358. updata_state_save(UPDATA_PACKAGE_LENGTH_ERROR);//存储升级状态
  359. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  360. printf("总长度太大\n");
  361. return;
  362. }
  363. // if((p_start->rcv_device_type == 0xffff) && ((p_start->update_version&0xf0000000) != 0x10000000))//显示屏升级
  364. // {
  365. // //软件版本错误
  366. // updata_state_save(DEVICE_TYPE_ERROR);//存储升级状态
  367. // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  368. // printf("软件版本错误\n");
  369. // return;
  370. // }
  371. // if((p_start->update_version&0xfffff000) != (SOFTWARE_VERSION_APP&0xfffff000))//软件版本号错误
  372. // {
  373. // updata_state_save(DEVICE_TYPE_ERROR);//存储升级状态
  374. // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  375. // printf("软件版本错误\n");
  376. // return;
  377. // }
  378. if(!ota_state.flash_erase)//第一次接收到指令擦除flash
  379. {
  380. ota_state.flash_erase = 1;
  381. device_irq_set(DISABLE);
  382. flash_flag = Flash_RangeErase(OTA_UPDATE_APP3_FLASH_START_ADDR, OTA_UPDATE_APP3_FLASH_SIZE); //擦除app3
  383. printf("flash flag=%d\n",flash_flag);
  384. memset(&ota_slave, 0, sizeof(ota_pkg_info_t));
  385. //打开升级总定时器
  386. ota_timer_total_slave_set(OPEN,OTA_TOTAL_TIMEOUT_TIME_TICK);
  387. //存储升级状态
  388. updata_state_save(START_UPDATA);
  389. //清除显示屏升级标志
  390. BKP_WriteBackupRegister_code(BKP_DR4,0);
  391. //升级指示灯
  392. ota_start_led_init();
  393. Led_Single_State_Set(UPDATA_SLAVE_LED,LED_ON);
  394. }
  395. ota_state.update_state_slave = OTA_WORK_STATE_START;
  396. //存储升级信息
  397. lora_node_ota_info_Init(&ota_slave,p_start);
  398. OTA_Continuous_Timeout = (ota_slave.total_pkgs * ota_slave.Send_interval) + CONTINUOUS_TIMEOUT_DELAY_TIME;//连续接收定时时间
  399. if(OTA_Continuous_Timeout > CONTINUOUS_TIMEOUT_TIME_MUX)//发送时间太长
  400. {
  401. printf("continuous time to long\n");
  402. printf("ota continuous time:%u\n",OTA_Continuous_Timeout/1000);
  403. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  404. return;
  405. }
  406. ota_timer_continuous_slave_set(OPEN,OTA_Continuous_Timeout);//打开连续接收定时器
  407. printf("ota_totle_byte:%d, ota_totle_pkg:%d\n",ota_slave.total_bytes,ota_slave.total_pkgs);
  408. printf("send interval:%d, continuous time:%d\n",ota_slave.Send_interval,OTA_Continuous_Timeout/1000);
  409. }
  410. else
  411. {
  412. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  413. }
  414. break;
  415. case net_msg_type_update_continues://连续发包阶段
  416. p_cont = (ota_continues_info_t *)p_msg->info;
  417. if(device_type_and_id_cmp(p_cont->rcv_device_type, p_cont->rcv_device_sn)) //是当前设备
  418. {
  419. if(ota_state.update_state_slave == OTA_WORK_STATE_CONTINUOUS)//收到了升级开始指令
  420. {
  421. ota_state.update_mode = OTA_WORK_STATE_SLAVE;
  422. if(p_cont->sub_pkg_len > OTA_UPDATE_PAYLAOA_LEN)//收到的包长度大于128,收到的数据错误
  423. return;
  424. if(p_cont->sub_pkg_id < ota_slave.lastRightPkgNum)
  425. return;
  426. if(p_cont->total_pkgs == ota_slave.total_pkgs)
  427. {
  428. printf("update continues: type = 0x%x, id = 0x%x, {%d/%d/%d}\r\n",
  429. p_cont->rcv_device_type, p_cont->rcv_device_sn,
  430. p_cont->total_pkgs, p_cont->sub_pkg_id,ota_slave.lossPkgCounter);
  431. //接收本包并写入flash的对应位置
  432. Flash_BufferWrite(OTA_UPDATE_APP3_FLASH_START_ADDR + (p_cont->sub_pkg_id - 1)*OTA_UPDATE_PAYLAOA_LEN,
  433. (uint32_t *)(p_cont->pkg_content), p_cont->sub_pkg_len);
  434. //更新收包相关状态
  435. for(i = ota_slave.lastRightPkgNum;i < p_cont->sub_pkg_id;i++) {
  436. ota_slave.lossPkgCounter++;
  437. ota_slave.lossPkgId[ota_slave.lossPkgIndex++] = i;//+1因为子包编号从0开始,
  438. //丢包太多了,超过了MAX_LOSS_PKG_COUNT,结束本次升级。
  439. if(ota_slave.lossPkgCounter == MAX_LOSS_PKG_COUNT) {//降为旁观者, 放弃本次升级
  440. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  441. updata_state_save(PACKET_LOSS_MORE);//存储升级状态
  442. return;
  443. }
  444. }
  445. ota_slave.lastRightPkgNum = p_cont->sub_pkg_id + 1;
  446. if(p_cont->sub_pkg_id == ota_slave.total_pkgs)//当前是最后一包
  447. {
  448. if(ota_slave.lossPkgCounter == 0) //没有丢包
  449. {
  450. ota_state.update_state_slave = OTA_WORK_STATE_RCV_END;
  451. printf("check begin\n");
  452. ota_continuous_end_handle();
  453. }
  454. else //有丢包
  455. {
  456. ota_timer_retransmit_require_slave_set(OPEN,RETRANSMIT_DELAY_TIMEOUT_MIN);//打开重传延时定时器
  457. ota_timer_retransmit_slave_set(OPEN,RETRANSMIT_TIMEOUT_TIME); //打开重传定时器
  458. ota_timer_continuous_slave_set(CLOSE,0);//关闭连续定时器
  459. printf("retransmit begin\n");
  460. }
  461. }
  462. else
  463. {
  464. //连续接收定时时间
  465. OTA_Continuous_Timeout = ((ota_slave.total_pkgs-p_cont->sub_pkg_id) * ota_slave.Send_interval)+ CONTINUOUS_TIMEOUT_DELAY_TIME;
  466. ota_timer_continuous_slave_set(OPEN,OTA_Continuous_Timeout);//打开连续接收定时器
  467. printf("send interval:%d, continuous time:%d\n",ota_slave.Send_interval,OTA_Continuous_Timeout/1000);
  468. }
  469. //指示灯显示
  470. remain_pkg = ota_slave.total_pkgs-p_cont->sub_pkg_id;
  471. Data_last4bit_led(remain_pkg,1);
  472. }
  473. }
  474. else//未收到升级开始指令
  475. {
  476. updata_state_save(START_UPDATA_NONE);//存储升级状态
  477. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  478. ota_timer_continuous_slave_set(CLOSE,OTA_Continuous_Timeout);//关闭连续接收定时器
  479. }
  480. }
  481. else
  482. {
  483. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  484. ota_timer_continuous_slave_set(CLOSE,OTA_Continuous_Timeout);//关闭连续接收定时器
  485. }
  486. break;
  487. case net_msg_type_update_retransmit_rsp://重传发包指令
  488. p_cont = (ota_continues_info_t *)p_msg->info;
  489. if(device_type_and_id_cmp(p_cont->rcv_device_type, p_cont->rcv_device_sn))
  490. {
  491. if(ota_state.update_state_slave == OTA_WORK_STATE_RETRANSMIT)//收到升级开始指令
  492. {
  493. ota_state.update_mode = OTA_WORK_STATE_SLAVE;
  494. if(p_cont->sub_pkg_len > OTA_UPDATE_PAYLAOA_LEN)//收到的包长度大于128,收到的数据错误
  495. return;
  496. if(p_cont->total_pkgs == ota_slave.total_pkgs)
  497. {
  498. //判定该包是否为本设备丢的那些包之中的子包
  499. for(i = 0;i < ota_slave.lossPkgIndex;i++) {
  500. if(ota_slave.lossPkgId[i] == p_cont->sub_pkg_id) {
  501. ota_slave.lossPkgCounter--;
  502. ota_slave.lossPkgId[i] = 0;
  503. //接受本包并写入flash的对应位置
  504. Flash_BufferWrite(OTA_UPDATE_APP3_FLASH_START_ADDR + (p_cont->sub_pkg_id - 1)*OTA_UPDATE_PAYLAOA_LEN,
  505. (uint32_t *)(p_cont->pkg_content), p_cont->sub_pkg_len);
  506. printf("rcv retransmit pkgid:%d\n",p_cont->sub_pkg_id);
  507. break;
  508. }
  509. }
  510. printf("ota_info.lossPkgCounter = %d, lossPkgId:%d\r\n", ota_slave.lossPkgCounter,p_cont->sub_pkg_id);
  511. if(ota_slave.lossPkgCounter == 0) //丢包已收完
  512. {
  513. ota_state.update_state_slave = OTA_WORK_STATE_RCV_END;
  514. ota_continuous_end_handle();
  515. return;
  516. }
  517. //指示灯显示
  518. remain_pkg = ota_slave.lossPkgCounter;
  519. Data_last4bit_led(remain_pkg,1);
  520. }
  521. ota_timer_retransmit_slave_set(OPEN,RETRANSMIT_TIMEOUT_TIME);//打开重传定时器
  522. }
  523. else//未收到升级开始指令
  524. {
  525. // updata_state_save(START_UPDATA_NONE);//存储升级状态
  526. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  527. }
  528. }
  529. else
  530. {
  531. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  532. }
  533. break;
  534. case net_msg_type_update_offline://网关下发开始离线升级
  535. if(ota_state.update_mode == 0)
  536. {
  537. p_off = (gateway_coll_offline_upgrade_t *)p_msg->info;
  538. if(p_off->master_device_type==0x0201 && p_off->master_device_sn==device_info.device_sn)
  539. {
  540. ota_state.update_mode = OTA_WORK_STATE_MASTER;
  541. lora_master_ota_gateway_info_init(p_off);
  542. device_fixed_info.master_updata_start = 1;
  543. ota_state.offline_mode = GATEWAY_TRIGGER;
  544. printf("网关下发开始离线升级\n");
  545. printf("slave sn:%010u, des sn:%010u\n",p_off->slave_device_sn,p_off->des_device_sn);
  546. }
  547. else
  548. {
  549. if(device_info.alrd == DEVICE_ALRD)
  550. {
  551. device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
  552. }
  553. else
  554. {
  555. device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
  556. }
  557. }
  558. }
  559. break;
  560. default:
  561. if(ota_state.update_mode == 0)
  562. {
  563. if(device_info.alrd == DEVICE_ALRD)
  564. {
  565. device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
  566. }
  567. else
  568. {
  569. device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
  570. }
  571. }
  572. break;
  573. }
  574. }
  575. }
  576. //升级包校验判断
  577. uint8_t ota_update_pkg_check(uint16_t type, uint8_t state)
  578. {
  579. // uint8_t flashbuf[64];
  580. uint8_t ret = 0;
  581. uint32_t crcVl,fixChar,fileVer,filelen/*,fileaddr*/;
  582. uint32_t getcrc;
  583. // uint32_t xspl_addr,xspl_len,xspa_len;
  584. //判断升级设备类型
  585. crcVl = *(uint32_t*)(OTA_UPDATE_APP3_FLASH_START_ADDR+ota_slave.total_bytes-4); //CRC校验
  586. fixChar = *(uint32_t*)(OTA_UPDATE_APP3_FLASH_START_ADDR+ota_slave.total_bytes-8); // 固定字符 WBJW
  587. fileVer = *(uint32_t*)(OTA_UPDATE_APP3_FLASH_START_ADDR+ota_slave.total_bytes-12); // 固件版本信息
  588. // fileaddr = *(uint32_t*)(OTA_UPDATE_APP3_FLASH_START_ADDR+ota_slave.total_bytes-16); // 存储地址
  589. filelen = *(uint32_t*)(OTA_UPDATE_APP3_FLASH_START_ADDR+ota_slave.total_bytes-20); // 文件总长度
  590. if(type == 0x0201)
  591. {
  592. printf("read_fixchar:%08x, fixchar:%08x\n",fixChar,0x57424A57);
  593. if(fixChar != 0x57424A57) //固定字符错误
  594. {
  595. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  596. updata_state_save(FIXED_CHARACTER_ERROR);//存储升级状态
  597. printf("fixChar error\r\n");// 升级文件固定字符校验错误
  598. return ret;
  599. }
  600. printf("read_filever:%08x, update_ver:%08x\n",fileVer,ota_slave.update_version);
  601. if((fileVer) != ota_slave.update_version) //固件信息错误
  602. {
  603. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  604. updata_state_save(DEVICE_TYPE_ERROR);//存储升级状态
  605. printf("fileVer error\r\n");// 升级文件固件信息校验错误
  606. return ret;
  607. }
  608. printf("read_filelen:%08x, total_bytes:%08x\n",filelen,(ota_slave.total_bytes-20));
  609. if(ota_slave.total_bytes != (filelen+20))//长度错误
  610. {
  611. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  612. updata_state_save(UPDATA_PACKAGE_LENGTH_ERROR);//存储升级状态
  613. printf("filelen error\r\n");// 升级文件长度校验错误
  614. return ret;
  615. }
  616. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE);
  617. CRC_ResetDR();
  618. CRC_CalcBlockCRC((uint32_t*)OTA_UPDATE_APP3_FLASH_START_ADDR, (ota_slave.total_bytes -4)/4);
  619. getcrc = CRC_GetCRC();
  620. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE);
  621. printf("read_crc:%x, get_crc:%x\n",crcVl,getcrc);
  622. if(getcrc!=crcVl) { //校验错误
  623. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  624. updata_state_save(UPDATA_CRC_ERROR);//存储升级状态
  625. printf("update file crc error\r\n");// 升级文件crc校验错误
  626. return ret;
  627. }
  628. printf("收包正确\n");
  629. updata_state_save(UPDATA_DATA_SUCCESS);//存储升级状态
  630. ret = 1;
  631. // return ret;
  632. }
  633. else if(type == XSP_0904)
  634. {
  635. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE);
  636. CRC_ResetDR();
  637. CRC_CalcBlockCRC((uint32_t*)OTA_UPDATE_APP3_FLASH_START_ADDR, (ota_slave.total_bytes -4)/4);
  638. getcrc = CRC_GetCRC();
  639. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE);
  640. printf("read_crc:%x, get_crc:%x\n",crcVl,getcrc);
  641. if(getcrc!=crcVl) { //校验错误
  642. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  643. updata_state_save(UPDATA_CRC_ERROR);//存储升级状态
  644. printf("update file crc error\r\n");// 升级文件crc校验错误
  645. return ret;
  646. }
  647. printf("收包正确\n");
  648. updata_state_save(UPDATA_DATA_SUCCESS);//存储升级状态
  649. ret = 1;
  650. // return ret;
  651. // if(fixChar != XSPL_CHAR) //固定字符错误
  652. // {
  653. // if(state)
  654. // {
  655. // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  656. // updata_state_save(FIXED_CHARACTER_ERROR);//存储升级状态
  657. // }
  658. // printf("fixChar error\r\n");// 升级文件固定字符校验错误
  659. // return ret;
  660. // }
  661. //// printf("read_filever:%08x, update_ver:%08x\n",fileVer,ota_slave.update_version);
  662. //// if((fileVer) != ota_slave.update_version) //固件版本信息错误
  663. //// {
  664. //// ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  665. //// updata_state_save(DEVICE_TYPE_ERROR);//存储升级状态
  666. //// printf("fileVer error\r\n");// 升级文件固件信息校验错误
  667. //// return ret;
  668. //// }
  669. // XSPL_len = filelen;
  670. // XSPL_addr = fileaddr;
  671. // if(XSPL_addr != OTA_UPDATE_APP3_FLASH_START_ADDR+ota_slave.total_bytes-filelen-20)
  672. // {
  673. // //路由表的起始地址和长度错误
  674. // if(state)
  675. // {
  676. // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  677. // updata_state_save(UPDATA_PACKAGE_LENGTH_ERROR);//存储升级状态
  678. // }
  679. // printf("xspl startaddr and length err\n");
  680. // return ret;
  681. // }
  682. //
  683. // printf("xspl_addr:%08x,len:%d\n",XSPL_addr,(XSPL_len+16));
  684. //// if((XSPL_len+16) <= 64)
  685. //// {
  686. //// Flash_BufferRead(XSPL_addr,(uint8_t *)flashbuf,XSPL_len+16);
  687. //// data_dump("flash",flashbuf,XSPL_len+16);
  688. //// }
  689. // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE);
  690. // CRC_ResetDR();
  691. // CRC_CalcBlockCRC((uint32_t*)XSPL_addr, (XSPL_len+16)/4);
  692. // getcrc = CRC_GetCRC();
  693. // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE);
  694. // printf("read_crc:%x, get_crc:%x\n",crcVl,getcrc);
  695. // if(getcrc!=crcVl) { //校验错误
  696. // if(state)
  697. // {
  698. // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  699. // updata_state_save(UPDATA_CRC_ERROR);//存储升级状态
  700. // }
  701. // printf("update file crc error\r\n");// 升级文件crc校验错误
  702. // return ret;
  703. // }
  704. // //显示屏路由表校验正确,校验显示屏升级包
  705. // crcVl = *(uint32_t*)(XSPL_addr-4); //CRC校验
  706. // fixChar = *(uint32_t*)(XSPL_addr-8); // 固定字符 XSPA
  707. // fileVer = *(uint32_t*)(XSPL_addr-12); // 固件信息
  708. // filelen = *(uint32_t*)(XSPL_addr-20); // 文件总长度
  709. // XSPA_len = XSPL_addr-OTA_UPDATE_APP3_FLASH_START_ADDR-20;
  710. //
  711. // XSP_OTA_VER = fileVer;//显示屏版本号
  712. //
  713. // if(fixChar != XSPA_CHAR) //固定字符错误
  714. // {
  715. // if(state)
  716. // {
  717. // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  718. // updata_state_save(FIXED_CHARACTER_ERROR);//存储升级状态
  719. // }
  720. // printf("fixChar error\r\n");// 升级文件固定字符校验错误
  721. // return ret;
  722. // }
  723. // if(state)
  724. // {
  725. // printf("read_filever:%08x, update_ver:%08x\n",fileVer,ota_slave.update_version);
  726. // if((fileVer) != ota_slave.update_version) //固件信息错误
  727. // {
  728. // XSP_OTA_VER = 0;
  729. // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  730. // updata_state_save(DEVICE_TYPE_ERROR);//存储升级状态
  731. // printf("fileVer error\r\n");// 升级文件固件信息校验错误
  732. // return ret;
  733. // }
  734. // }
  735. // printf("read_filelen:%08x, total_bytes:%08x\n",filelen,(XSPA_len));
  736. // if(XSPA_len != (filelen))//长度错误
  737. // {
  738. // if(state)
  739. // {
  740. // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  741. // updata_state_save(UPDATA_PACKAGE_LENGTH_ERROR);//存储升级状态
  742. // }
  743. // printf("filelen error\r\n");// 升级文件长度校验错误
  744. // return ret;
  745. // }
  746. // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE);
  747. // CRC_ResetDR();
  748. // CRC_CalcBlockCRC((uint32_t*)OTA_UPDATE_APP3_FLASH_START_ADDR, (XSPA_len+16)/4);
  749. // getcrc = CRC_GetCRC();
  750. // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE);
  751. // printf("read_crc:%x, get_crc:%x\n",crcVl,getcrc);
  752. // if(getcrc!=crcVl) { //校验错误
  753. // if(state)
  754. // {
  755. // ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);//打开旁观者定时器
  756. // updata_state_save(UPDATA_CRC_ERROR);//存储升级状态
  757. // }
  758. // printf("update file crc error\r\n");// 升级文件crc校验错误
  759. // return ret;
  760. // }
  761. // printf("收包正确\n");
  762. // updata_state_save(UPDATA_DATA_SUCCESS);//存储升级状态
  763. //
  764. // //计算升级包信息
  765. // XSPH_APP_Len = *(uint16_t*)(XSPL_addr-20-2);
  766. // XSPH_APP_Addr = *(uint16_t*)(XSPL_addr-20-4);
  767. // XSPL_APP_Len = *(uint16_t*)(XSPL_addr-20-6);
  768. // XSPL_APP_Addr = *(uint16_t*)(XSPL_addr-20-8);
  769. // XSPL_APP_Crc = xsp_update_crc(OTA_UPDATE_APP3_FLASH_START_ADDR+XSPL_APP_Addr,XSPL_APP_Len);
  770. // XSPH_APP_Crc = xsp_update_crc(OTA_UPDATE_APP3_FLASH_START_ADDR+XSPH_APP_Addr,XSPH_APP_Len);
  771. // ota_xsp_pack_info.update_ver = fileVer;
  772. // printf("xspl addr=0x%04x, xspl len=%d, xspl crc=0x%04x\n",XSPL_APP_Addr,XSPL_APP_Len,XSPL_APP_Crc);
  773. // printf("xsph addr=0x%04x, xsph len=%d, xsph crc=0x%04x\n",XSPH_APP_Addr,XSPH_APP_Len,XSPH_APP_Crc);
  774. //
  775. // ret = 1;
  776. // return ret;
  777. }
  778. return ret;
  779. }
  780. //连续接收完成任务处理函数
  781. void ota_continuous_end_handle(void)
  782. {
  783. // uint32_t crcVl,fixChar,fileVer,filelen;
  784. // uint32_t getcrc;
  785. uint16_t i;
  786. uint8_t ret;
  787. switch(ota_state.update_state_slave)
  788. {
  789. case OTA_WORK_STATE_RCV_END:
  790. ota_timer_continuous_slave_set(CLOSE,0);//关闭连续定时器
  791. ota_timer_retransmit_slave_set(CLOSE,0);//关闭重传定时器
  792. ota_timer_retransmit_require_slave_set(CLOSE,0);//关闭重传请求定时器
  793. if(ota_slave.des_device_type == 0x0201)//采集器升级
  794. {
  795. ret = ota_update_pkg_check(0x0201,0);
  796. if(ret)//校验正确
  797. {
  798. if(ota_slave.des_device_type == (SOFTWARE_VERSION_APP&0xffff0000)>>16)//自身升级
  799. {
  800. //置升级标志
  801. updata_state_info.updata_flag = set_updata_state(UPDATA_FLAG);
  802. BKP_WriteBackupRegister(BKP_DR2, updata_state_info.updata_flag);
  803. //存储APP3版本
  804. device_version_info.APP3_ver = ota_slave.update_version;
  805. eeprom_write_data(EECFG_APP3_VER0,(uint8_t *)&device_version_info.APP3_ver,4);
  806. NVIC_SystemReset();
  807. }
  808. }
  809. }
  810. else if(ota_slave.des_device_type == XSP_0904)//显示屏升级
  811. {
  812. ret = ota_update_pkg_check(XSP_0904,0);
  813. if(ret)//校验正确
  814. {
  815. //存储APP3版本
  816. device_version_info.APP3_ver = ota_slave.update_version;
  817. eeprom_write_data(EECFG_APP3_VER0,(uint8_t *)&device_version_info.APP3_ver,4);
  818. ota_timer_onlooker_slave_set(CLOSE,0);//关闭旁观者定时器
  819. ota_timer_continuous_slave_set(CLOSE,0);//关闭连续发包定时器
  820. ota_timer_retransmit_slave_set(CLOSE,0);//关闭重传阶段定时器
  821. ota_timer_total_slave_set(CLOSE,0);//关闭总定时器
  822. memset(&ota_state,0,sizeof(ota_state));
  823. if(device_info.alrd == DEVICE_ALRD)//已初始化
  824. {
  825. device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
  826. }
  827. else
  828. {
  829. device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
  830. }
  831. device_fixed_info.xsp_update_start = 1;//升级显示屏标志
  832. printf("收包完成,开始升级显示屏 \n");
  833. }
  834. }
  835. // else if((ota_slave.des_device_type&0xf000) == 0x1000)//显示屏升级
  836. // {
  837. // ret = ota_update_pkg_check(DISPLAY,1);
  838. // if(ret)//校验正确
  839. // {
  840. // get_xsp_ota_info(XSPL_addr,XSPL_len);
  841. // if(check_route_xsp_ota())
  842. // {
  843. // ota_timer_onlooker_slave_set(CLOSE,0);//关闭旁观者定时器
  844. //
  845. // //显示屏升级标志存储
  846. // BKP_WriteBackupRegister_code(BKP_DR4,XSP_UPDATE);
  847. // //显示屏升级次数存储
  848. // BKP_WriteBackupRegister_code(BKP_DR5,0);
  849. //
  850. // xsp_ota_start();
  851. //// memset(&ota_xsp_info,0,sizeof(ota_xsp_info_t));
  852. // ota_xsp_init();
  853. // }
  854. //
  855. // }
  856. //
  857. //// device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
  858. //// ota_timer_onlooker_slave_set(CLOSE,0);
  859. //// ota_timer_total_slave_set(CLOSE,0);//关闭总定时器
  860. // }
  861. break;
  862. case OTA_WORK_STATE_RETRANSMIT://重传
  863. if(sys_lora_tx.lora_q.MsgQ.NbrEntries==0)//当前队列中的消息数。
  864. {
  865. for(i = 0;i < ota_slave.lossPkgIndex;i++) {
  866. if(ota_slave.lossPkgId[i])
  867. {
  868. ota_retransmit_req(ota_slave.lossPkgId[i]);
  869. printf("send retransmit pkgid:%d\n",ota_slave.lossPkgId[i]);
  870. break;
  871. }
  872. }
  873. }
  874. // ota_timer_retransmit_require_slave_set(OPEN,RETRANSMIT_DELAY_TIMEOUT_MIN);//打开重传延时定时器
  875. break;
  876. }
  877. }
  878. //从设备升级状态返回
  879. void lora_slave_ota_updatestate_resp(uint8_t updatestate)
  880. {
  881. collect_gateway_msg_format_t msg;
  882. ota_state_upload_t *p_ota = (ota_state_upload_t *)msg.info;
  883. p_ota->rcv_device_type = device_info.device_type;
  884. p_ota->rcv_device_sn = device_info.device_sn;
  885. p_ota->des_device_type = device_info.device_type;
  886. p_ota->des_device_sn = device_info.device_sn;
  887. p_ota->des_bt_ver = device_fixed_info.Soft_ver_boot;
  888. p_ota->des_app_ver = device_fixed_info.Soft_ver_app;
  889. p_ota->update_state = updatestate;
  890. p_ota->reserve = 0;
  891. coll_msg_send(&msg, net_msg_type_update_status_upload, OTA_STATE_UPLOAD_LEN);
  892. // slave_upload_info.num++;//上报次数
  893. // printf("slave send %d ",slave_upload_info.num);
  894. // data_dump("times",(uint8_t *)&msg,FRAME_HEADER_LEN + OTA_STATE_UPLOAD_LEN+2);
  895. }
  896. //从设备主动上报结束
  897. void slave_upload_end(void)
  898. {
  899. ota_timer_upload_cad_slave_set(CLOSE,0);//关闭上报间隔定时
  900. ota_timer_upload_total_slave_set(CLOSE,0);//关闭上报总定时
  901. if(device_info.alrd == DEVICE_ALRD)//已初始化
  902. {
  903. device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
  904. }
  905. else
  906. {
  907. device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
  908. }
  909. memset(&slave_upload_info,0,sizeof(slave_upload_info_t));
  910. memset(&ota_state,0,sizeof(ota_state_t));
  911. close_all_led();
  912. }
  913. //从设备升级定时器处理
  914. void ota_slave_time_handle(void)
  915. {
  916. ota_timeout_onlooker_slave(); //旁观者定时器超时处理
  917. ota_timeout_continuous_slave(); //连续阶段定时器超时处理
  918. ota_timeout_retransmit_slave(); //重传阶段定时器超时处理
  919. ota_timeout_total_slave(); //总定时器超时处理
  920. ota_timeout_retransmit_require_slave(); //重传请求超时处理
  921. ota_timeout_upload_cad_slave(); //主动上报升级状态间隔超时处理
  922. ota_timeout_upload_total_slave(); //主动上报升级状态总定时超时处理
  923. }
  924. ////////////////////////////////////////////////////////////////////////////////////
  925. ////离线升级
  926. //主设备总定时器设置
  927. void ota_timer_total_master_set(uint8_t state, uint32_t timeout)
  928. {
  929. if(state == OPEN)
  930. {
  931. timeout_setValue(&ota_master.tt_ota_total,timeout,1);
  932. }
  933. else if(state == CLOSE)
  934. {
  935. timeout_stop(&ota_master.tt_ota_total);
  936. }
  937. }
  938. //主设备总定时超时
  939. void ota_timeout_total_master(void)
  940. {
  941. if(timeout_isOut(&ota_master.tt_ota_total))
  942. {
  943. if(ota_state.update_mode != OTA_WORK_STATE_XSP)
  944. master_rcv_total_proc();//离线升级时打印
  945. if(device_info.alrd == DEVICE_ALRD)//已初始化
  946. {
  947. device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
  948. }
  949. else
  950. {
  951. device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
  952. }
  953. memset(&ota_state,0,sizeof(ota_state_t));
  954. ota_timer_retransmit_master_set(CLOSE,0);//关闭重传定时器
  955. close_all_led();
  956. printf("master total timeout \n");
  957. }
  958. }
  959. //主设备重传定时器设置
  960. void ota_timer_retransmit_master_set(uint8_t state, uint32_t timeout)
  961. {
  962. if(state == OPEN)
  963. {
  964. timeout_setValue(&ota_master.tt_ota_retransmit,timeout,1);
  965. }
  966. else if(state == CLOSE)
  967. {
  968. timeout_stop(&ota_master.tt_ota_retransmit);
  969. }
  970. }
  971. //主设备重传定时超时
  972. void ota_timeout_retransmit_master(void)
  973. {
  974. if(timeout_isOut(&ota_master.tt_ota_retransmit))
  975. {
  976. if(ota_state.update_mode != OTA_WORK_STATE_XSP)
  977. master_rcv_total_proc();//离线升级时打印
  978. memset(&ota_state,0,sizeof(ota_state_t));
  979. ota_timer_total_master_set(CLOSE,0);//关闭总定时器
  980. close_all_led();
  981. if(device_info.alrd == DEVICE_ALRD)//已初始化
  982. {
  983. device_fixed_info.Work_State = DEV_WORK_STATE_NORMAL;
  984. }
  985. else
  986. {
  987. device_fixed_info.Work_State = DEV_WORK_STATE_UNINIT;
  988. }
  989. printf("master update over\n");
  990. }
  991. }
  992. //读取app程序包的大小并校验
  993. uint32_t Read_APP_datalen_check(uint32_t addr, uint32_t len)
  994. {
  995. uint32_t datalen = 0;
  996. uint32_t crcVl,fixChar,fileVer,filelen,getcrc,t_addr;
  997. uint32_t bin_data[6] = {0};
  998. uint8_t flag = 0;
  999. uint8_t check_flag = 0;
  1000. printf("addr=%08x\n",addr);
  1001. datalen = Flash_DatalenRead(addr,len);
  1002. filelen = *(uint32_t*)(addr+datalen-20); // 文件总长度
  1003. printf("datalen=%08x,%u; filelen=%08x,%u\n",datalen,datalen,filelen,filelen);
  1004. if(datalen > (128*1024))
  1005. {
  1006. printf("升级包长度超过128K: %d\n",datalen);
  1007. datalen = 0;
  1008. }
  1009. else
  1010. {
  1011. printf("addr=%08x\n",addr);
  1012. crcVl = *(uint32_t*)(addr + datalen-4); //CRC校验
  1013. fixChar = *(uint32_t*)(addr + datalen-8); // 固定字符 WBJW
  1014. fileVer = *(uint32_t*)(addr + datalen-12); // 固件信息
  1015. filelen = *(uint32_t*)(addr + datalen-20); // 文件总长度
  1016. t_addr = *(uint32_t*)(addr + datalen-16); // 文件写入的地址
  1017. printf("crcVl=%08x,fixChar=%08x,fileVer=%08x,filelen=%08x,t_addr=%08x\n",crcVl,fixChar,fileVer,filelen,t_addr);
  1018. //判断后20字节是否正确
  1019. if((fixChar == WBJW_CHAR) && (t_addr == 0x08020000))//升级包中包含了后20字节
  1020. {
  1021. printf("升级包包含了后20字节\n");
  1022. if(datalen != (filelen+20))
  1023. {
  1024. printf("升级包长度校验错误\n");
  1025. check_flag = 1;
  1026. }
  1027. else if(fileVer != device_fixed_info.Soft_ver_app)
  1028. {
  1029. printf("固件信息错误\n");
  1030. printf("filever=%08x,app=%08x\n",fileVer,device_fixed_info.Soft_ver_app);
  1031. check_flag = 1;
  1032. }
  1033. else
  1034. {
  1035. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE);
  1036. CRC_ResetDR();
  1037. CRC_CalcBlockCRC((uint32_t*)addr, (datalen -4)/4);
  1038. getcrc = CRC_GetCRC();
  1039. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE);
  1040. printf("read_crc:%08x, get_crc:%08x\n",crcVl,getcrc);
  1041. if(crcVl != getcrc)
  1042. {
  1043. printf("crc 错误\n");
  1044. check_flag = 1;
  1045. }
  1046. }
  1047. }
  1048. else
  1049. {
  1050. check_flag = 1;
  1051. }
  1052. if(check_flag)//需要计算后20个字节
  1053. {
  1054. bin_data[0] = datalen; //原始bin文件字节长度
  1055. bin_data[1] = 0x08020000; //存储地址
  1056. bin_data[2] = device_fixed_info.Soft_ver_app; //固件版本
  1057. bin_data[3] = WBJW_CHAR; //固定标记,"WBJW"
  1058. flag = Flash_BufferWrite(addr+datalen,bin_data,16);
  1059. printf("first flag = %d\n",flag);
  1060. datalen += 20;
  1061. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE);
  1062. CRC_ResetDR();
  1063. CRC_CalcBlockCRC((uint32_t*)addr, (datalen -4)/4);
  1064. getcrc = CRC_GetCRC();
  1065. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE);
  1066. bin_data[0] = getcrc;
  1067. printf("crc=%08x\n",bin_data[0]);
  1068. flag = Flash_BufferWrite(addr+datalen-4,bin_data,4); //crc校验
  1069. printf("second flag = %d\n",flag);
  1070. }
  1071. }
  1072. return datalen;
  1073. }
  1074. //升级开始指令
  1075. void lora_master_ota_start(void)
  1076. {
  1077. uint32_t timer = 0;
  1078. collect_gateway_msg_format_t msg;
  1079. ota_start_info_t *p_ota = (ota_start_info_t *)msg.info;
  1080. p_ota->des_device_type = ota_master.des_device_type; // device_info.device_type;
  1081. p_ota->des_device_sn = ota_master.des_device_sn; //0xffffffff;
  1082. p_ota->rcv_device_type = ota_master.rcv_device_type; //device_info.device_type;
  1083. p_ota->rcv_device_sn = ota_master.rcv_device_sn; //0xffffffff;
  1084. p_ota->update_version = ota_master.update_version; //device_fixed_info.Soft_ver_app;
  1085. p_ota->total_bytes = ota_master.total_bytes;
  1086. timer = RadioTimeOnAir2(MODEM_LORA,140);
  1087. timer = (timer>UPDATE_EACH_TIME) ? timer : UPDATE_EACH_TIME;
  1088. p_ota->Send_interval = timer;
  1089. printf("total_bytes = %d, send interval = %d\n",p_ota->total_bytes, timer);
  1090. coll_msg_send(&msg, net_msg_type_update_start, OTA_START_INFO_LEN);
  1091. }
  1092. //网关下发离线升级时初始化
  1093. int lora_master_ota_gateway_info_init(gateway_coll_offline_upgrade_t *p_off)
  1094. {
  1095. memset(&ota_master, 0, sizeof(ota_pkg_info_t));
  1096. ota_master.des_device_type = p_off->des_device_type;
  1097. ota_master.des_device_sn = p_off->des_device_sn;
  1098. ota_master.rcv_device_type = p_off->slave_device_type;
  1099. ota_master.rcv_device_sn = p_off->slave_device_sn;
  1100. ota_master.update_version = device_fixed_info.Soft_ver_app;
  1101. ota_master.total_bytes = 0;
  1102. ota_master.total_crc = 0;
  1103. ota_master.total_pkgs = 0;
  1104. ota_master.lastRightPkgNum = 1;
  1105. ota_master.sub_pkg_id = 1;
  1106. return 0;
  1107. }
  1108. //按键触发离线升级时初始化
  1109. int lora_master_ota_key_info_Init(uint32_t total_len, uint32_t total_crc)
  1110. {
  1111. memset(&ota_master, 0, sizeof(ota_pkg_info_t));
  1112. ota_master.des_device_type = device_info.device_type;
  1113. ota_master.des_device_sn = 0xffffffff;
  1114. ota_master.rcv_device_sn = 0xffffffff;
  1115. ota_master.rcv_device_type = device_info.device_type;
  1116. ota_master.update_version = device_fixed_info.Soft_ver_app;
  1117. ota_master.total_bytes = total_len;
  1118. ota_master.total_crc = total_crc;
  1119. ota_master.total_pkgs = total_len/OTA_UPDATE_PAYLAOA_LEN;
  1120. ota_master.lastRightPkgNum = 1;
  1121. if(total_len%OTA_UPDATE_PAYLAOA_LEN) {
  1122. ota_master.total_pkgs += 1;
  1123. }
  1124. ota_master.sub_pkg_id = 1;
  1125. return 0;
  1126. }
  1127. //连续发送阶段
  1128. void lora_master_ota_continues(void)
  1129. {
  1130. collect_gateway_msg_format_t msg;
  1131. ota_continues_info_t *p_ota = (ota_continues_info_t *)msg.info;
  1132. OS_ERR err;
  1133. uint32_t timer = 0;
  1134. char buff[128] = {0};
  1135. // uint16_t i=0;
  1136. printf("lora_gw_ota_continues\r\n");
  1137. p_ota->total_pkgs = ota_master.total_pkgs;
  1138. p_ota->sub_pkg_len = OTA_UPDATE_PAYLAOA_LEN;
  1139. p_ota->des_device_type = ota_master.des_device_type;
  1140. p_ota->des_device_sn = ota_master.des_device_sn;
  1141. p_ota->rcv_device_sn = ota_master.rcv_device_sn;
  1142. p_ota->rcv_device_type = ota_master.rcv_device_type;
  1143. timer = RadioTimeOnAir2(MODEM_LORA,140);
  1144. timer = (timer>UPDATE_EACH_TIME) ? timer : UPDATE_EACH_TIME;
  1145. printf("timer = %d\r\n", timer);
  1146. printf("lora_update total_pkgs = %d\r\n",ota_master.total_pkgs);
  1147. while(ota_master.sub_pkg_id <= ota_master.total_pkgs) {
  1148. printf("lora_gw_ota_continues {%d/%d}\r\n", ota_master.total_pkgs,ota_master.sub_pkg_id);
  1149. p_ota->sub_pkg_id = ota_master.sub_pkg_id++;
  1150. Flash_BufferRead(OTA_UPDATE_APP1_FLASH_START_ADDR + (p_ota->sub_pkg_id - 1)*OTA_UPDATE_PAYLAOA_LEN,
  1151. (uint8_t *)buff, OTA_UPDATE_PAYLAOA_LEN);
  1152. memcpy(p_ota->pkg_content,buff,128);
  1153. coll_msg_send(&msg, net_msg_type_update_continues, OTA_CONTINUES_INFO_LEN + OTA_UPDATE_PAYLAOA_LEN);
  1154. //指示灯显示(显示剩余包数后四位)
  1155. Data_last4bit_led(ota_master.total_pkgs-ota_master.sub_pkg_id,1);
  1156. OSTimeDlyHMSM(0, 0, 0, timer, OS_OPT_TIME_DLY, &err);
  1157. }
  1158. }
  1159. //发送重传包
  1160. void lora_master_ota_retransmit_resp(ota_retransmit_req_t *p_req)
  1161. {
  1162. collect_gateway_msg_format_t msg;
  1163. ota_continues_info_t *p_ota = (ota_continues_info_t *)msg.info;
  1164. char buff[128] = {0};
  1165. p_ota->total_pkgs = ota_master.total_pkgs;
  1166. p_ota->sub_pkg_id = p_req->repeat_sub_pkg_id;
  1167. p_ota->sub_pkg_len = OTA_UPDATE_PAYLAOA_LEN;
  1168. p_ota->des_device_type = ota_master.des_device_type;
  1169. p_ota->des_device_sn = ota_master.des_device_sn;
  1170. p_ota->rcv_device_sn = ota_master.rcv_device_sn;
  1171. p_ota->rcv_device_type = ota_master.rcv_device_type;
  1172. Flash_BufferRead(OTA_UPDATE_APP1_FLASH_START_ADDR + (p_req->repeat_sub_pkg_id - 1)*OTA_UPDATE_PAYLAOA_LEN,
  1173. (uint8_t *)buff, OTA_UPDATE_PAYLAOA_LEN);
  1174. memcpy(p_ota->pkg_content,buff,128);
  1175. coll_msg_send(&msg, net_msg_type_update_retransmit_rsp, OTA_CONTINUES_INFO_LEN + OTA_UPDATE_PAYLAOA_LEN);
  1176. }
  1177. //主设备收到状态上报指令处理
  1178. void master_upload_proc(collect_gateway_msg_format_t *p_msg)
  1179. {
  1180. int i;
  1181. ota_state_upload_t *p_ota = (ota_state_upload_t *)p_msg->info;
  1182. master_rcv_info.rcv_print_num++;
  1183. 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,\
  1184. p_ota->des_device_sn,p_ota->des_bt_ver,p_ota->des_app_ver,p_ota->update_state);
  1185. if(master_rcv_info.rcv_total_num < 64)
  1186. {
  1187. if(master_rcv_info.rcv_total_num)
  1188. {
  1189. for(i = 0; i < master_rcv_info.rcv_total_num; i++)
  1190. {
  1191. if(master_rcv_info.master_rcv_update[i].des_sn == p_ota->des_device_sn)
  1192. {
  1193. master_rcv_info.master_rcv_update[i].des_type = p_ota->des_device_type;
  1194. master_rcv_info.master_rcv_update[i].des_sn = p_ota->des_device_sn;
  1195. master_rcv_info.master_rcv_update[i].des_bt = p_ota->des_bt_ver;
  1196. master_rcv_info.master_rcv_update[i].des_app = p_ota->des_app_ver;
  1197. master_rcv_info.master_rcv_update[i].state = p_ota->update_state;
  1198. return;
  1199. }
  1200. }
  1201. }
  1202. master_rcv_info.master_rcv_update[master_rcv_info.rcv_total_num].des_type = p_ota->des_device_type;
  1203. master_rcv_info.master_rcv_update[master_rcv_info.rcv_total_num].des_sn = p_ota->des_device_sn;
  1204. master_rcv_info.master_rcv_update[master_rcv_info.rcv_total_num].des_bt = p_ota->des_bt_ver;
  1205. master_rcv_info.master_rcv_update[master_rcv_info.rcv_total_num].des_app = p_ota->des_app_ver;
  1206. master_rcv_info.master_rcv_update[master_rcv_info.rcv_total_num].state = p_ota->update_state;
  1207. master_rcv_info.rcv_total_num++;
  1208. }
  1209. }
  1210. int cmp(const void *a, const void *b)
  1211. {
  1212. // return (*(uint32_t *)a - *(uint32_t *)b);
  1213. return ((*(uint32_t *)a>*(uint32_t *)b)?1:-1); //从小到大排序
  1214. }
  1215. //主设备统计状态
  1216. void master_rcv_total_proc(void)
  1217. {
  1218. uint32_t des_sn[64] = {0};
  1219. int i;
  1220. for(i = 0; i < master_rcv_info.rcv_total_num; i++)
  1221. {
  1222. if(master_rcv_info.master_rcv_update[i].state == UPDATA_SUCCESS)
  1223. {
  1224. des_sn[master_rcv_info.rcv_success_num] = master_rcv_info.master_rcv_update[i].des_sn;
  1225. master_rcv_info.rcv_success_num++;
  1226. }
  1227. }
  1228. printf("升级成功%d个:\n",master_rcv_info.rcv_success_num);
  1229. //快排(从小到大排序)
  1230. qsort(des_sn,master_rcv_info.rcv_success_num,sizeof(des_sn[0]),cmp);
  1231. for(i = 0; i < master_rcv_info.rcv_success_num; i++)
  1232. {
  1233. if((i%4 == 0)&&i) {
  1234. if((i%8 == 0)&&i) {
  1235. printf("\r\n");
  1236. } else {
  1237. printf(" ");
  1238. }
  1239. }
  1240. printf("%010u ",des_sn[i]);
  1241. }
  1242. printf("\n");
  1243. }
  1244. //收到重传请求后下发命令
  1245. void lora_master_ota_proc(collect_gateway_msg_format_t *p_msg)
  1246. {
  1247. ota_retransmit_req_t *p_req = NULL;
  1248. if(ota_state.update_mode == OTA_WORK_STATE_ONLOOKER)
  1249. {
  1250. ota_timer_onlooker_slave_set(OPEN,ONLOOKER_TIMEOUT_TIME);
  1251. return;
  1252. }
  1253. switch(p_msg->first_type) {
  1254. case net_msg_type_update_retransmit_req://从设备的重传请求
  1255. p_req = (ota_retransmit_req_t *)p_msg->info;
  1256. lora_master_ota_retransmit_resp(p_req);
  1257. ota_timer_retransmit_master_set(OPEN,OTA_MASTER_WAIT_TIME_TICK);//主设备等待定时器
  1258. break;
  1259. case net_msg_type_update_status:
  1260. break;
  1261. case net_msg_type_update_status_upload://从设备的状态返回
  1262. ota_timer_retransmit_master_set(OPEN,OTA_MASTER_WAIT_TIME_TICK);//主设备等待定时器
  1263. master_upload_proc(p_msg);
  1264. break;
  1265. default:
  1266. break;
  1267. }
  1268. }
  1269. //离线升级时的主设备
  1270. void Offline_upgrade_master(void)
  1271. {
  1272. OS_ERR err;
  1273. uint32_t total_bytes = 0;
  1274. uint32_t i = 0;
  1275. //清空接收信息
  1276. memset(&master_rcv_info,0,sizeof(master_rcv_info_t));
  1277. //打开总定时器
  1278. ota_timer_total_master_set(OPEN,OTA_TOTAL_TIMEOUT_TIME_TICK);
  1279. //升级指示灯
  1280. ota_start_led_init();
  1281. Led_Single_State_Set(UPDATA_MASTER_LED,LED_ON);
  1282. total_bytes = Read_APP_datalen_check(OTA_UPDATE_APP1_FLASH_START_ADDR,OTA_UPDATE_APP1_FLASH_SIZE);
  1283. if(total_bytes==0) //升级包长度错误,升级结束,总定时设置为1ms
  1284. {
  1285. ota_timer_total_master_set(OPEN,1);
  1286. return;
  1287. }
  1288. printf("send total_bytes=%d\n",total_bytes);
  1289. if(ota_state.offline_mode == KEY_TRIGGER)
  1290. {
  1291. lora_master_ota_key_info_Init(total_bytes,0);
  1292. printf("按键触发\n");
  1293. }
  1294. else if(ota_state.offline_mode == GATEWAY_TRIGGER)
  1295. {
  1296. printf("命令触发\n");
  1297. ota_master.total_bytes = total_bytes;
  1298. ota_master.total_pkgs = total_bytes/OTA_UPDATE_PAYLAOA_LEN;
  1299. if(total_bytes%OTA_UPDATE_PAYLAOA_LEN)
  1300. {
  1301. ota_master.total_pkgs++;
  1302. }
  1303. }
  1304. for(i = 0; i < 5; i++)
  1305. {
  1306. lora_master_ota_start();
  1307. OSTimeDlyHMSM(0, 0, 2, 0, OS_OPT_TIME_DLY, &err);
  1308. }
  1309. lora_master_ota_continues();
  1310. //连续发送完成,打开等待定时器,如果一定时间内没收到重传请求或状态上报则认为升级结束
  1311. ota_timer_retransmit_master_set(OPEN,OTA_MASTER_WAIT_TIME_TICK);
  1312. }
  1313. //离线升级主设备定时处理
  1314. void ota_master_time_handle(void)
  1315. {
  1316. ota_timeout_retransmit_master();//重传定时超时
  1317. ota_timeout_total_master();//总定时超时
  1318. }
  1319. //显示屏升级重启后升级
  1320. uint32_t xsp_update_reboot(void)
  1321. {
  1322. uint8_t ret;
  1323. uint32_t datalen = 0;
  1324. uint32_t filelen,fixChar;//fileVer,crcVl,getcrc,t_addr;
  1325. datalen = Flash_DatalenRead(OTA_UPDATE_APP3_FLASH_START_ADDR,OTA_UPDATE_APP3_FLASH_SIZE);
  1326. filelen = *(uint32_t*)(OTA_UPDATE_APP3_FLASH_START_ADDR+datalen-20); // 文件总长度
  1327. fixChar = *(uint32_t*)(OTA_UPDATE_APP3_FLASH_START_ADDR+datalen-8); // 固定字符 WBJW
  1328. printf("datalen=%08x,%u; filelen=%08x,%u\n",datalen,datalen,filelen,filelen);
  1329. if(datalen > (128*1024) || datalen == 0 || filelen > (datalen) || filelen == 0)
  1330. {
  1331. printf("升级包长度错误: %d\n",datalen);
  1332. //清除显示屏升级标志
  1333. BKP_WriteBackupRegister_code(BKP_DR4,0);
  1334. //显示屏升级次数存储
  1335. BKP_WriteBackupRegister_code(BKP_DR5,0);
  1336. return datalen;
  1337. }
  1338. memset(&ota_slave,0,sizeof(ota_pkg_info_t));
  1339. ota_slave.total_bytes = datalen;
  1340. XSPL_addr = OTA_UPDATE_APP3_FLASH_START_ADDR+datalen-filelen-20;
  1341. XSPL_len = filelen;
  1342. printf("addr=%08x\n",XSPL_addr);
  1343. // crcVl = *(uint32_t*)(XSPL_addr + datalen-4); //CRC校验
  1344. // fixChar = *(uint32_t*)(XSPL_addr + datalen-8); // 固定字符 WBJW
  1345. // fileVer = *(uint32_t*)(XSPL_addr + datalen-12); // 固件信息
  1346. // filelen = *(uint32_t*)(XSPL_addr + datalen-20); // 文件总长度
  1347. // t_addr = *(uint32_t*)(XSPL_addr + datalen-16); // 文件写入的地址
  1348. if(fixChar != XSPL_CHAR)//显示屏
  1349. {
  1350. //清除显示屏升级标志
  1351. BKP_WriteBackupRegister_code(BKP_DR4,0);
  1352. //显示屏升级次数存储
  1353. BKP_WriteBackupRegister_code(BKP_DR5,0);
  1354. return datalen;
  1355. }
  1356. ret = ota_update_pkg_check(0x0401,0);
  1357. if(ret)//校验正确
  1358. {
  1359. get_xsp_ota_info(XSPL_addr,XSPL_len);
  1360. if(check_route_xsp_ota())
  1361. {
  1362. printf("xsp ota\n");
  1363. // device_fixed_info.Work_State = DEV_WORK_STATE_XSP_UPDATING;
  1364. // ota_xsp_info.xsp_ota_begin = 1;
  1365. xsp_ota_start();
  1366. // memset(&ota_xsp_info,0,sizeof(ota_xsp_info_t));
  1367. ota_xsp_init();
  1368. }
  1369. }
  1370. else
  1371. {
  1372. //清除显示屏升级标志
  1373. BKP_WriteBackupRegister_code(BKP_DR4,0);
  1374. //显示屏升级次数存储
  1375. BKP_WriteBackupRegister_code(BKP_DR5,0);
  1376. }
  1377. return datalen;
  1378. }
  1379. //测试。下发开始离线升级命令
  1380. void lora_offline_update(uint32_t master_sn, uint32_t slave_sn, uint32_t des_sn)
  1381. {
  1382. collect_gateway_msg_format_t msg;
  1383. gateway_coll_offline_upgrade_t *p_off = (gateway_coll_offline_upgrade_t *)msg.info;
  1384. p_off->master_device_type = 0x0201;
  1385. p_off->master_device_sn = master_sn;
  1386. p_off->slave_device_type = 0x0201;
  1387. p_off->slave_device_sn = slave_sn;
  1388. p_off->des_device_type = 0x0201;
  1389. p_off->des_device_sn = des_sn;
  1390. p_off->reserve = 0;
  1391. coll_msg_send(&msg, net_msg_type_update_offline, OTA_OFFLINE_LEN);
  1392. // data_dump("offline",(uint8_t *)&msg,FRAME_HEADER_LEN+OTA_OFFLINE_LEN+2);
  1393. }