| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- /*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2018-03-22 Murphy the first version
- */
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <rtthread.h>
- #include <finsh.h>
- #include <easyflash.h>
- #include "webclient.h"
- #include <fal.h>
- #include "sys_config.h"
- rt_mutex_t OTA_mutex = RT_NULL;//OTA,保证ota升级过程中独享网络资源
- #define DBG_ENABLE
- #define DBG_SECTION_NAME "http_ota"
- #ifdef OTA_DOWNLOADER_DEBUG
- #define DBG_LEVEL DBG_LOG
- #else
- #define DBG_LEVEL DBG_INFO
- #endif
- #define DBG_COLOR
- #include <rtdbg.h>
- #ifdef PKG_USING_HTTP_OTA
- //目前测试4k的循环获取数据大小比较稳定
- #define HTTP_OTA_BUFF_LEN 1024 * 4
- #define GET_HEADER_BUFSZ 1024
- #define GET_RESP_BUFSZ 1024
- #define HTTP_OTA_DL_DELAY (10 * RT_TICK_PER_SECOND)
- #define DEFAULT_DOWNLOAD_PART_AKA "fm_area"
- #define HTTP_OTA_URL PKG_HTTP_OTA_URL
- #define STATUS_DIS_LEN 100 //显示缓冲区长度
- #define DIS_POS "reset.t3.txt=\"%s\""
- #define DIS_POS_value "reset.t3.txt=\"%s%d\%\""
- #define DIS_POS_progress "reset.j0.val=\%d"
- #define pagebootloader "page bootloader"
- #define url_err "升级路径错误"
- #define req_fail "http请求失败"
- #define filesize "升级文件大小:"
- #define file_fail "获取文件失败"
- #define startwritfw "准备写入固件"
- #define writingfw "正在下载固件"
- #define finddownload_part "找到固件分区,准备格式化"
- #define nodownload_part "未找到固件分区"
- #define erase_fail "固件分区格式化失败"
- #define erase_succ "固件分区格式化成功"
- #define malloc_fail "分配内存失败"
- #define write_error "固件写入错误"
- #define downloadfail "固件下载失败"
- #define downloadsucc "固件下载成功"
- #define reboot_5s "配置成功,5秒后重启"
- extern rt_err_t HMI_Display(void *Data, rt_size_t length);
- /*屏幕显示相应的操作状态*/
- extern void Status_Display(const char * content, uint16_t value);
- static void print_progress(size_t cur_size, size_t total_size)
- {
- static unsigned char progress_sign[100 + 1];
- uint8_t i, per = cur_size * 100 / total_size;
- if (per > 100)
- {
- per = 100;
- }
- for (i = 0; i < 100; i++)
- {
- if (i < per)
- {
- progress_sign[i] = '=';
- }
- else if (per == i)
- {
- progress_sign[i] = '>';
- }
- else
- {
- progress_sign[i] = ' ';
- }
-
- }
- Status_Display(writingfw, per);
- Status_Display(RT_NULL, per);
- progress_sign[sizeof(progress_sign) - 1] = '\0';
- LOG_I("\033[2A");
- LOG_I("Download: [%s] %d%%", progress_sign, per);
- }
- static int http_ota_fw_download(const char* uri)
- {
- int ret = 0, resp_status;
- int file_size = 0, length, total_length = 0;
- rt_uint8_t *buffer_read = RT_NULL;
- struct webclient_session* session = RT_NULL;
- const struct fal_partition * dl_part = RT_NULL;
- /* create webclient session and set header response size */
- session = webclient_session_create(GET_HEADER_BUFSZ);
- if (!session)
- {
- Status_Display(url_err, RT_NULL);rt_thread_mdelay(500);
- LOG_E("open uri failed.");
- ret = -RT_ERROR;
- goto __exit;
- }
- /* send GET request by default header */
- // if ((resp_status = webclient_get(session, uri)) != 200)webclient_get_position
- if ((resp_status = webclient_get_position(session, uri,500)) != 200)
- {
- Status_Display(req_fail, RT_NULL); rt_thread_mdelay(500);
- LOG_E("webclient GET request failed, response(%d) error.", resp_status);
- ret = -RT_ERROR;
- goto __exit;
- }
- file_size = webclient_content_length_get(session);
- rt_kprintf("http file_size:%d\n",file_size);
- Status_Display(DIS_POS_value, file_size);rt_thread_mdelay(500);
- if (file_size == 0)
- {
- LOG_E("Request file size is 0!");
- ret = -RT_ERROR;
- goto __exit;
- }
- else if (file_size < 0)
- {
- Status_Display(file_fail, RT_NULL);rt_thread_mdelay(500);
- LOG_E("webclient GET request type is chunked.");
- ret = -RT_ERROR;
- goto __exit;
- }
- /* Get download partition information and erase download partition data */
- if ((dl_part = fal_partition_find("fm_area")) == RT_NULL)
- {
- Status_Display(nodownload_part, RT_NULL);rt_thread_mdelay(500);
- LOG_E("Firmware download failed! Partition >>> (%s) find error!", "fm_area");
- ret = -RT_ERROR;
- goto __exit;
- }
- LOG_I("Start erase flash (%s) partition!", dl_part->name);
- if (fal_partition_erase(dl_part, 0, file_size) < 0)
- {
- Status_Display(erase_fail, RT_NULL);rt_thread_mdelay(500);
- LOG_E("Firmware download failed! Partition (%s) erase error!", dl_part->name);
- ret = -RT_ERROR;
- goto __exit;
- }
- Status_Display(erase_succ, RT_NULL);
- LOG_I("Erase flash (%s) partition success!", dl_part->name);
- buffer_read = web_malloc(HTTP_OTA_BUFF_LEN);
- if (buffer_read == RT_NULL)
- {
- Status_Display(malloc_fail, RT_NULL);
- LOG_E("No memory for http ota!");
- ret = -RT_ERROR;
- goto __exit;
- }
- rt_memset(buffer_read, 0, HTTP_OTA_BUFF_LEN);
- Status_Display(startwritfw, RT_NULL);rt_thread_mdelay(500);
- LOG_I("OTA file size is (%d)", file_size);
- // list_mem();
- do
- {
- length = webclient_read(session, buffer_read, file_size - total_length > HTTP_OTA_BUFF_LEN ?
- HTTP_OTA_BUFF_LEN : file_size - total_length);
- if (length > 0)
- {
- /* Write the data to the corresponding partition address */
- if (fal_partition_write(dl_part, total_length, buffer_read, length) < 0)
- {
- Status_Display(write_error, RT_NULL);
- LOG_E("Firmware download failed! Partition (%s) write data error!", dl_part->name);
- ret = -RT_ERROR;
- goto __exit;
- }
- total_length += length;
- print_progress(total_length, file_size);
- // list_mem();
- }
- else
- {
- Status_Display(downloadfail, RT_NULL);
- LOG_E("Exit: server return err (%d)!", length);
- ret = -RT_ERROR;
- goto __exit;
- }
- LOG_W("length:%d", length);
- rt_thread_mdelay(100);//mqtt任务中调用,优先级较高,增加延时,避免线程阻塞,引起错误
- } while(total_length != file_size);
- if (total_length == file_size)
- {
- Status_Display(downloadsucc, RT_NULL);rt_thread_mdelay(500);
- if (session){webclient_close(session);session = RT_NULL;}
- if (buffer_read){web_free(buffer_read);buffer_read = RT_NULL;}
- rt_thread_delay(rt_tick_from_millisecond(5));
-
- Status_Display("\0", RT_NULL);//显示状态清空
- ret = RT_EOK;
- }
- __exit:
- if (session){webclient_close(session);session = RT_NULL;}
- if (buffer_read){web_free(buffer_read);buffer_read = RT_NULL;}
- return ret;
- }
- #define ZK_DOWNLOAD_PART "zk_area"
- static int http_ota_zk_download(const char* uri)
- {
- int ret = 0, resp_status;
- int file_size = 0, length, total_length = 0;
- rt_uint8_t *buffer_read = RT_NULL;
- struct webclient_session* session = RT_NULL;
- const struct fal_partition * dl_part = RT_NULL;
- /* create webclient session and set header response size */
- session = webclient_session_create(GET_HEADER_BUFSZ);
- if (!session)
- {
- LOG_E("open uri failed.");
- ret = -RT_ERROR;
- goto __exit;
- }
- /* send GET request by default header */
- // if ((resp_status = webclient_get(session, uri)) != 200)webclient_get_position
- if ((resp_status = webclient_get_position(session, uri,500)) != 200)
- {
- LOG_E("webclient GET request failed, response(%d) error.", resp_status);
- ret = -RT_ERROR;
- goto __exit;
- }
- file_size = webclient_content_length_get(session);
- rt_kprintf("http file_size:%d\n",file_size);
- if (file_size == 0)
- {
- LOG_E("Request file size is 0!");
- ret = -RT_ERROR;
- goto __exit;
- }
- else if (file_size < 0)
- {
- LOG_E("webclient GET request type is chunked.");
- ret = -RT_ERROR;
- goto __exit;
- }
- /* Get download partition information and erase download partition data */
- if ((dl_part = fal_partition_find(ZK_DOWNLOAD_PART)) == RT_NULL)
- {
- LOG_E("Firmware download failed! <<< Partition (%s) find error!", ZK_DOWNLOAD_PART);
- ret = -RT_ERROR;
- goto __exit;
- }
- LOG_I("Start erase flash (%s) partition!", dl_part->name);
- if (fal_partition_erase(dl_part, 0, file_size) < 0)
- {
- LOG_E("Firmware download failed! Partition (%s) erase error!", dl_part->name);
- ret = -RT_ERROR;
- goto __exit;
- }
- LOG_I("Erase flash (%s) partition success!", dl_part->name);
- buffer_read = web_malloc(HTTP_OTA_BUFF_LEN);
- if (buffer_read == RT_NULL)
- {
- LOG_E("No memory for http ota!");
- ret = -RT_ERROR;
- goto __exit;
- }
- memset(buffer_read, 0, HTTP_OTA_BUFF_LEN);
- LOG_I("OTA file size is (%d)", file_size);
- // list_mem();
- do
- {
- length = webclient_read(session, buffer_read, file_size - total_length > HTTP_OTA_BUFF_LEN ?
- HTTP_OTA_BUFF_LEN : file_size - total_length);
- if (length > 0)
- {
- /* Write the data to the corresponding partition address */
- if (fal_partition_write(dl_part, total_length, buffer_read, length) < 0)
- {
- LOG_E("Firmware download failed! Partition (%s) write data error!", dl_part->name);
- ret = -RT_ERROR;
- goto __exit;
- }
- total_length += length;
- print_progress(total_length, file_size);
- // list_mem();
- }
- else
- {
- LOG_E("Exit: server return err (%d)!", length);
- ret = -RT_ERROR;
- goto __exit;
- }
- rt_thread_mdelay(100);//mqtt任务中调用,优先级较高,增加延时,避免线程阻塞,引起错误
- } while(total_length != file_size);
-
- if (total_length == file_size)
- {
- if(session){webclient_close(session);session = RT_NULL;}
- if(buffer_read){web_free(buffer_read);buffer_read = RT_NULL;}
- LOG_I("Download ZK to flash success.");
- rt_thread_delay(rt_tick_from_millisecond(5));
- ret = RT_EOK;
- return ret;
- }
- __exit:
- if (session){webclient_close(session);session = RT_NULL;}
- if (buffer_read){web_free(buffer_read);buffer_read = RT_NULL;}
- return ret;
- }
-
- #define HTTP_OTA_URL1 "http://www.yfzhwlw.com/update/qxz_bin/qxz.rbl"
- #define HTTP_ZK_URL "http://www.yfzhwlw.com/update/qxz_bin/hexc.bin"
- void http_ota_app(uint8_t argc, char **argv)
- {
-
- if (argc < 2)
- {
- rt_kprintf("using uri: " HTTP_OTA_URL1 "\n");
- http_ota_fw_download(HTTP_OTA_URL1);
- }
- else
- {
- http_ota_fw_download(argv[1]);
- }
- }
- extern void Recode_e_code(uint8_t code, uint8_t para, uint8_t level);
- void http_ota_yf(const char* url)
- {
- uint8_t update_flag = 0;
-
- if(rt_strlen(url) == 0)return;
- /* 创建互斥锁 */
- OTA_mutex = rt_mutex_create("OTA_mutex", RT_IPC_FLAG_FIFO);
- if (OTA_mutex == RT_NULL)
- {
- LOG_E("create OTA mutex failed.\n");
- return;
- }
- if(OTA_mutex)rt_mutex_take(OTA_mutex, RT_WAITING_FOREVER);
-
- //读出升级标志
- // if(ef_get_env_blob("update_flag", &update_flag, sizeof(update_flag), RT_NULL))
- // {
- // update_flag ++;//升级变量+1并重新写入
- // }
- // ef_set_env_blob("update_flag", &update_flag, sizeof(update_flag));
-
- if(http_ota_fw_download(url) == RT_EOK)
- {
- // update_flag = 0;//升级成功升级变量置零并写入保存
- // ef_set_env_blob("update_flag", &update_flag, sizeof(update_flag));
- /* Reset the device, Start new firmware */
- LOG_I("Download firmware to flash success.");
- LOG_I("System now will restart...");
-
- /**
- * @brief 与bootloader对称出现
- *
- */
- uint8_t en_upgrade = 0x9D;
- //rt_kprintf("en_upgrade SET %d \n",ef_set_env_blob("en_upgrade" , &en_upgrade , sizeof(en_upgrade)));
- ef_set_env_blob("en_upgrade" , &en_upgrade , sizeof(en_upgrade));
- Recode_e_code(9,0,3); //下载成功
- update_flag = 0;//升级成功升级变量置零并写入保存
- ef_set_env_blob("update_flag", &update_flag, sizeof(update_flag));
- Status_Display(reboot_5s, RT_NULL);rt_thread_mdelay(500);
- rt_thread_delay(rt_tick_from_millisecond(5000));
- Status_Display(RT_NULL, RT_NULL);//切换到bootloader页面
- extern void RESET_SYSTEM(void);//成功后重启
- RESET_SYSTEM();
- return;
- }
- else
- {//升级失败重启
- if(OTA_mutex)rt_mutex_release(OTA_mutex);//升级失败释放互斥量
- extern void RESET_SYSTEM(void);//成功后重启
- Recode_e_code(9,1,3); //下载失败
- update_flag = 0;//升级成功升级变量置零并写入保存
- ef_set_env_blob("update_flag", &update_flag, sizeof(update_flag));
- RESET_SYSTEM();
-
- }
- }
- int http_ota_zk(void)
- {
- if(rt_strlen(HTTP_ZK_URL) == 0)return 0;
- if(http_ota_zk_download(HTTP_ZK_URL) == RT_EOK) return RT_EOK;
- }
- /**
- * msh />http_ota_app [url]
- */
- MSH_CMD_EXPORT(http_ota_app, Use HTTP to download the firmware);
- #endif /* PKG_USING_HTTP_OTA */
- //void http_ota_yf(uint8_t argc, char **argv)
- //{
- // if (argc < 2)
- // {
- //
- // rt_sprintf(url,"%s?imei=%s&firmware_name=%s%d.%d.%d",HTTP_OTA_URL_YF,qxz.Param.device_sn,"qxz_",
- // qxz.Param.version/100, qxz.Param.version%100/10,qxz.Param.version%10);
- // http_ota_fw_download(url);
- //
- // rt_kprintf("using uri:%s\n",url);
- // }
- // else
- // {
- // http_ota_fw_download(argv[1]);
- // }
- //}
|