http_ota_app.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-03-22 Murphy the first version
  9. */
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <rtthread.h>
  14. #include <finsh.h>
  15. #include <easyflash.h>
  16. #include "webclient.h"
  17. #include <fal.h>
  18. #include "sys_config.h"
  19. rt_mutex_t OTA_mutex = RT_NULL;//OTA,保证ota升级过程中独享网络资源
  20. #define DBG_ENABLE
  21. #define DBG_SECTION_NAME "http_ota"
  22. #ifdef OTA_DOWNLOADER_DEBUG
  23. #define DBG_LEVEL DBG_LOG
  24. #else
  25. #define DBG_LEVEL DBG_INFO
  26. #endif
  27. #define DBG_COLOR
  28. #include <rtdbg.h>
  29. #ifdef PKG_USING_HTTP_OTA
  30. //目前测试4k的循环获取数据大小比较稳定
  31. #define HTTP_OTA_BUFF_LEN 1024 * 4
  32. #define GET_HEADER_BUFSZ 1024
  33. #define GET_RESP_BUFSZ 1024
  34. #define HTTP_OTA_DL_DELAY (10 * RT_TICK_PER_SECOND)
  35. #define DEFAULT_DOWNLOAD_PART_AKA "fm_area"
  36. #define HTTP_OTA_URL PKG_HTTP_OTA_URL
  37. #define STATUS_DIS_LEN 100 //显示缓冲区长度
  38. #define DIS_POS "reset.t3.txt=\"%s\""
  39. #define DIS_POS_value "reset.t3.txt=\"%s%d\%\""
  40. #define DIS_POS_progress "reset.j0.val=\%d"
  41. #define pagebootloader "page bootloader"
  42. #define url_err "升级路径错误"
  43. #define req_fail "http请求失败"
  44. #define filesize "升级文件大小:"
  45. #define file_fail "获取文件失败"
  46. #define startwritfw "准备写入固件"
  47. #define writingfw "正在下载固件"
  48. #define finddownload_part "找到固件分区,准备格式化"
  49. #define nodownload_part "未找到固件分区"
  50. #define erase_fail "固件分区格式化失败"
  51. #define erase_succ "固件分区格式化成功"
  52. #define malloc_fail "分配内存失败"
  53. #define write_error "固件写入错误"
  54. #define downloadfail "固件下载失败"
  55. #define downloadsucc "固件下载成功"
  56. #define reboot_5s "配置成功,5秒后重启"
  57. extern rt_err_t HMI_Display(void *Data, rt_size_t length);
  58. /*屏幕显示相应的操作状态*/
  59. extern void Status_Display(const char * content, uint16_t value);
  60. static void print_progress(size_t cur_size, size_t total_size)
  61. {
  62. static unsigned char progress_sign[100 + 1];
  63. uint8_t i, per = cur_size * 100 / total_size;
  64. if (per > 100)
  65. {
  66. per = 100;
  67. }
  68. for (i = 0; i < 100; i++)
  69. {
  70. if (i < per)
  71. {
  72. progress_sign[i] = '=';
  73. }
  74. else if (per == i)
  75. {
  76. progress_sign[i] = '>';
  77. }
  78. else
  79. {
  80. progress_sign[i] = ' ';
  81. }
  82. }
  83. Status_Display(writingfw, per);
  84. Status_Display(RT_NULL, per);
  85. progress_sign[sizeof(progress_sign) - 1] = '\0';
  86. LOG_I("\033[2A");
  87. LOG_I("Download: [%s] %d%%", progress_sign, per);
  88. }
  89. static int http_ota_fw_download(const char* uri)
  90. {
  91. int ret = 0, resp_status;
  92. int file_size = 0, length, total_length = 0;
  93. rt_uint8_t *buffer_read = RT_NULL;
  94. struct webclient_session* session = RT_NULL;
  95. const struct fal_partition * dl_part = RT_NULL;
  96. /* create webclient session and set header response size */
  97. session = webclient_session_create(GET_HEADER_BUFSZ);
  98. if (!session)
  99. {
  100. Status_Display(url_err, RT_NULL);rt_thread_mdelay(500);
  101. LOG_E("open uri failed.");
  102. ret = -RT_ERROR;
  103. goto __exit;
  104. }
  105. /* send GET request by default header */
  106. // if ((resp_status = webclient_get(session, uri)) != 200)webclient_get_position
  107. if ((resp_status = webclient_get_position(session, uri,500)) != 200)
  108. {
  109. Status_Display(req_fail, RT_NULL); rt_thread_mdelay(500);
  110. LOG_E("webclient GET request failed, response(%d) error.", resp_status);
  111. ret = -RT_ERROR;
  112. goto __exit;
  113. }
  114. file_size = webclient_content_length_get(session);
  115. rt_kprintf("http file_size:%d\n",file_size);
  116. Status_Display(DIS_POS_value, file_size);rt_thread_mdelay(500);
  117. if (file_size == 0)
  118. {
  119. LOG_E("Request file size is 0!");
  120. ret = -RT_ERROR;
  121. goto __exit;
  122. }
  123. else if (file_size < 0)
  124. {
  125. Status_Display(file_fail, RT_NULL);rt_thread_mdelay(500);
  126. LOG_E("webclient GET request type is chunked.");
  127. ret = -RT_ERROR;
  128. goto __exit;
  129. }
  130. /* Get download partition information and erase download partition data */
  131. if ((dl_part = fal_partition_find("fm_area")) == RT_NULL)
  132. {
  133. Status_Display(nodownload_part, RT_NULL);rt_thread_mdelay(500);
  134. LOG_E("Firmware download failed! Partition >>> (%s) find error!", "fm_area");
  135. ret = -RT_ERROR;
  136. goto __exit;
  137. }
  138. LOG_I("Start erase flash (%s) partition!", dl_part->name);
  139. if (fal_partition_erase(dl_part, 0, file_size) < 0)
  140. {
  141. Status_Display(erase_fail, RT_NULL);rt_thread_mdelay(500);
  142. LOG_E("Firmware download failed! Partition (%s) erase error!", dl_part->name);
  143. ret = -RT_ERROR;
  144. goto __exit;
  145. }
  146. Status_Display(erase_succ, RT_NULL);
  147. LOG_I("Erase flash (%s) partition success!", dl_part->name);
  148. buffer_read = web_malloc(HTTP_OTA_BUFF_LEN);
  149. if (buffer_read == RT_NULL)
  150. {
  151. Status_Display(malloc_fail, RT_NULL);
  152. LOG_E("No memory for http ota!");
  153. ret = -RT_ERROR;
  154. goto __exit;
  155. }
  156. rt_memset(buffer_read, 0, HTTP_OTA_BUFF_LEN);
  157. Status_Display(startwritfw, RT_NULL);rt_thread_mdelay(500);
  158. LOG_I("OTA file size is (%d)", file_size);
  159. // list_mem();
  160. do
  161. {
  162. length = webclient_read(session, buffer_read, file_size - total_length > HTTP_OTA_BUFF_LEN ?
  163. HTTP_OTA_BUFF_LEN : file_size - total_length);
  164. if (length > 0)
  165. {
  166. /* Write the data to the corresponding partition address */
  167. if (fal_partition_write(dl_part, total_length, buffer_read, length) < 0)
  168. {
  169. Status_Display(write_error, RT_NULL);
  170. LOG_E("Firmware download failed! Partition (%s) write data error!", dl_part->name);
  171. ret = -RT_ERROR;
  172. goto __exit;
  173. }
  174. total_length += length;
  175. print_progress(total_length, file_size);
  176. // list_mem();
  177. }
  178. else
  179. {
  180. Status_Display(downloadfail, RT_NULL);
  181. LOG_E("Exit: server return err (%d)!", length);
  182. ret = -RT_ERROR;
  183. goto __exit;
  184. }
  185. LOG_W("length:%d", length);
  186. rt_thread_mdelay(100);//mqtt任务中调用,优先级较高,增加延时,避免线程阻塞,引起错误
  187. } while(total_length != file_size);
  188. if (total_length == file_size)
  189. {
  190. Status_Display(downloadsucc, RT_NULL);rt_thread_mdelay(500);
  191. if (session){webclient_close(session);session = RT_NULL;}
  192. if (buffer_read){web_free(buffer_read);buffer_read = RT_NULL;}
  193. rt_thread_delay(rt_tick_from_millisecond(5));
  194. Status_Display("\0", RT_NULL);//显示状态清空
  195. ret = RT_EOK;
  196. }
  197. __exit:
  198. if (session){webclient_close(session);session = RT_NULL;}
  199. if (buffer_read){web_free(buffer_read);buffer_read = RT_NULL;}
  200. return ret;
  201. }
  202. #define ZK_DOWNLOAD_PART "zk_area"
  203. static int http_ota_zk_download(const char* uri)
  204. {
  205. int ret = 0, resp_status;
  206. int file_size = 0, length, total_length = 0;
  207. rt_uint8_t *buffer_read = RT_NULL;
  208. struct webclient_session* session = RT_NULL;
  209. const struct fal_partition * dl_part = RT_NULL;
  210. /* create webclient session and set header response size */
  211. session = webclient_session_create(GET_HEADER_BUFSZ);
  212. if (!session)
  213. {
  214. LOG_E("open uri failed.");
  215. ret = -RT_ERROR;
  216. goto __exit;
  217. }
  218. /* send GET request by default header */
  219. // if ((resp_status = webclient_get(session, uri)) != 200)webclient_get_position
  220. if ((resp_status = webclient_get_position(session, uri,500)) != 200)
  221. {
  222. LOG_E("webclient GET request failed, response(%d) error.", resp_status);
  223. ret = -RT_ERROR;
  224. goto __exit;
  225. }
  226. file_size = webclient_content_length_get(session);
  227. rt_kprintf("http file_size:%d\n",file_size);
  228. if (file_size == 0)
  229. {
  230. LOG_E("Request file size is 0!");
  231. ret = -RT_ERROR;
  232. goto __exit;
  233. }
  234. else if (file_size < 0)
  235. {
  236. LOG_E("webclient GET request type is chunked.");
  237. ret = -RT_ERROR;
  238. goto __exit;
  239. }
  240. /* Get download partition information and erase download partition data */
  241. if ((dl_part = fal_partition_find(ZK_DOWNLOAD_PART)) == RT_NULL)
  242. {
  243. LOG_E("Firmware download failed! <<< Partition (%s) find error!", ZK_DOWNLOAD_PART);
  244. ret = -RT_ERROR;
  245. goto __exit;
  246. }
  247. LOG_I("Start erase flash (%s) partition!", dl_part->name);
  248. if (fal_partition_erase(dl_part, 0, file_size) < 0)
  249. {
  250. LOG_E("Firmware download failed! Partition (%s) erase error!", dl_part->name);
  251. ret = -RT_ERROR;
  252. goto __exit;
  253. }
  254. LOG_I("Erase flash (%s) partition success!", dl_part->name);
  255. buffer_read = web_malloc(HTTP_OTA_BUFF_LEN);
  256. if (buffer_read == RT_NULL)
  257. {
  258. LOG_E("No memory for http ota!");
  259. ret = -RT_ERROR;
  260. goto __exit;
  261. }
  262. memset(buffer_read, 0, HTTP_OTA_BUFF_LEN);
  263. LOG_I("OTA file size is (%d)", file_size);
  264. // list_mem();
  265. do
  266. {
  267. length = webclient_read(session, buffer_read, file_size - total_length > HTTP_OTA_BUFF_LEN ?
  268. HTTP_OTA_BUFF_LEN : file_size - total_length);
  269. if (length > 0)
  270. {
  271. /* Write the data to the corresponding partition address */
  272. if (fal_partition_write(dl_part, total_length, buffer_read, length) < 0)
  273. {
  274. LOG_E("Firmware download failed! Partition (%s) write data error!", dl_part->name);
  275. ret = -RT_ERROR;
  276. goto __exit;
  277. }
  278. total_length += length;
  279. print_progress(total_length, file_size);
  280. // list_mem();
  281. }
  282. else
  283. {
  284. LOG_E("Exit: server return err (%d)!", length);
  285. ret = -RT_ERROR;
  286. goto __exit;
  287. }
  288. rt_thread_mdelay(100);//mqtt任务中调用,优先级较高,增加延时,避免线程阻塞,引起错误
  289. } while(total_length != file_size);
  290. if (total_length == file_size)
  291. {
  292. if(session){webclient_close(session);session = RT_NULL;}
  293. if(buffer_read){web_free(buffer_read);buffer_read = RT_NULL;}
  294. LOG_I("Download ZK to flash success.");
  295. rt_thread_delay(rt_tick_from_millisecond(5));
  296. ret = RT_EOK;
  297. return ret;
  298. }
  299. __exit:
  300. if (session){webclient_close(session);session = RT_NULL;}
  301. if (buffer_read){web_free(buffer_read);buffer_read = RT_NULL;}
  302. return ret;
  303. }
  304. #define HTTP_OTA_URL1 "http://www.yfzhwlw.com/update/qxz_bin/qxz.rbl"
  305. #define HTTP_ZK_URL "http://www.yfzhwlw.com/update/qxz_bin/hexc.bin"
  306. void http_ota_app(uint8_t argc, char **argv)
  307. {
  308. if (argc < 2)
  309. {
  310. rt_kprintf("using uri: " HTTP_OTA_URL1 "\n");
  311. http_ota_fw_download(HTTP_OTA_URL1);
  312. }
  313. else
  314. {
  315. http_ota_fw_download(argv[1]);
  316. }
  317. }
  318. extern void Recode_e_code(uint8_t code, uint8_t para, uint8_t level);
  319. void http_ota_yf(const char* url)
  320. {
  321. uint8_t update_flag = 0;
  322. if(rt_strlen(url) == 0)return;
  323. /* 创建互斥锁 */
  324. OTA_mutex = rt_mutex_create("OTA_mutex", RT_IPC_FLAG_FIFO);
  325. if (OTA_mutex == RT_NULL)
  326. {
  327. LOG_E("create OTA mutex failed.\n");
  328. return;
  329. }
  330. if(OTA_mutex)rt_mutex_take(OTA_mutex, RT_WAITING_FOREVER);
  331. //读出升级标志
  332. // if(ef_get_env_blob("update_flag", &update_flag, sizeof(update_flag), RT_NULL))
  333. // {
  334. // update_flag ++;//升级变量+1并重新写入
  335. // }
  336. // ef_set_env_blob("update_flag", &update_flag, sizeof(update_flag));
  337. if(http_ota_fw_download(url) == RT_EOK)
  338. {
  339. // update_flag = 0;//升级成功升级变量置零并写入保存
  340. // ef_set_env_blob("update_flag", &update_flag, sizeof(update_flag));
  341. /* Reset the device, Start new firmware */
  342. LOG_I("Download firmware to flash success.");
  343. LOG_I("System now will restart...");
  344. /**
  345. * @brief 与bootloader对称出现
  346. *
  347. */
  348. uint8_t en_upgrade = 0x9D;
  349. //rt_kprintf("en_upgrade SET %d \n",ef_set_env_blob("en_upgrade" , &en_upgrade , sizeof(en_upgrade)));
  350. ef_set_env_blob("en_upgrade" , &en_upgrade , sizeof(en_upgrade));
  351. Recode_e_code(9,0,3); //下载成功
  352. update_flag = 0;//升级成功升级变量置零并写入保存
  353. ef_set_env_blob("update_flag", &update_flag, sizeof(update_flag));
  354. Status_Display(reboot_5s, RT_NULL);rt_thread_mdelay(500);
  355. rt_thread_delay(rt_tick_from_millisecond(5000));
  356. Status_Display(RT_NULL, RT_NULL);//切换到bootloader页面
  357. extern void RESET_SYSTEM(void);//成功后重启
  358. RESET_SYSTEM();
  359. return;
  360. }
  361. else
  362. {//升级失败重启
  363. if(OTA_mutex)rt_mutex_release(OTA_mutex);//升级失败释放互斥量
  364. extern void RESET_SYSTEM(void);//成功后重启
  365. Recode_e_code(9,1,3); //下载失败
  366. update_flag = 0;//升级成功升级变量置零并写入保存
  367. ef_set_env_blob("update_flag", &update_flag, sizeof(update_flag));
  368. RESET_SYSTEM();
  369. }
  370. }
  371. int http_ota_zk(void)
  372. {
  373. if(rt_strlen(HTTP_ZK_URL) == 0)return 0;
  374. if(http_ota_zk_download(HTTP_ZK_URL) == RT_EOK) return RT_EOK;
  375. }
  376. /**
  377. * msh />http_ota_app [url]
  378. */
  379. MSH_CMD_EXPORT(http_ota_app, Use HTTP to download the firmware);
  380. #endif /* PKG_USING_HTTP_OTA */
  381. //void http_ota_yf(uint8_t argc, char **argv)
  382. //{
  383. // if (argc < 2)
  384. // {
  385. //
  386. // rt_sprintf(url,"%s?imei=%s&firmware_name=%s%d.%d.%d",HTTP_OTA_URL_YF,qxz.Param.device_sn,"qxz_",
  387. // qxz.Param.version/100, qxz.Param.version%100/10,qxz.Param.version%10);
  388. // http_ota_fw_download(url);
  389. //
  390. // rt_kprintf("using uri:%s\n",url);
  391. // }
  392. // else
  393. // {
  394. // http_ota_fw_download(argv[1]);
  395. // }
  396. //}