硬汉嵌入式论坛

 找回密码
 立即注册
查看: 1884|回复: 2
收起左侧

[Linux] linux下OTA功能基于licurl使用http下载文件

[复制链接]

4

主题

12

回帖

24

积分

新手上路

积分
24
发表于 2020-4-21 09:10:47 | 显示全部楼层 |阅读模式
本帖最后由 lz_kwok 于 2020-4-21 09:11 编辑

版权声明:本版面文章皆为原创、或参考其他技术网站、博客后自己动手做实验所得,转载请注明出处。

商务合作:lz_kwok@163.com

易开嵌入式工作室


嵌入式系统,OTA功能是十分重要的。linux环境下,使用libcurl,通过http可以很方便实现文件下载功能,并显示文件下载进度。实现方案如下:

首先定义下载地址和下载路径

  1. #define downLoad_url         "http://xxxx/downloadExample/update.tar.gz"
  2. #define downLoad_file         "/home/xxx/update.tar.gz"
复制代码

初始化libcurl并配置

  1. CURLcode curl_base::Download(std::string strUrl,std::string filepath)
  2. {
  3.     CURLcode res;
  4.     char *progress_data = "* ";  
  5.     FILE* fp = fopen(filepath.c_str(),"wb+");

  6.     curl_handle = curl_easy_init();

  7.     /* send all data to this function */
  8.     curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, OnDownLoadFile);
  9.     /* we pass our 'chunk' struct to the callback function */
  10.     curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)fp);

  11.     curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 0L);  
  12.     curl_easy_setopt(curl_handle, CURLOPT_PROGRESSFUNCTION, my_progress_func);  
  13.     curl_easy_setopt(curl_handle, CURLOPT_PROGRESSDATA, progress_data);

  14.     curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
  15.     curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0L);
  16.     curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0L);

  17.     curl_easy_setopt(curl_handle,CURLOPT_RESUME_FROM,0);  //从0字节开始下载
  18.     /* 设置连接超时,单位:毫秒 */
  19.     curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT_MS, 10000L);
  20.     curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT_MS, 10000L);
  21.     curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 3);
  22.     curl_easy_setopt(curl_handle, CURLOPT_URL, const_cast<char*>(strUrl.c_str()));


  23.     /* get it! */
  24.     res = curl_easy_perform(curl_handle);
  25.     /* check for errors */
  26.     if (res != CURLE_OK) {
  27.         fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  28.     }

  29.     fclose(fp);

  30.     return res;
  31. }
复制代码

其中,注意要关闭CURLOPT_NOPROGRESS。看下官网解释:

If onoff is to 1, it tells the library to shut off the progress meter completely for requests done with this handle. It will also prevent the CURLOPT_XFERINFOFUNCTION or CURLOPT_PROGRESSFUNCTION from getting called.

回调函数如下:

  1. static size_t OnDownLoadFile(void* buffer,size_t size, size_t nmemb,void* fp)
  2. {
  3.    fwrite(buffer, size, nmemb, (FILE*)fp);
  4.    fflush((FILE*)fp);

  5.    return (size*nmemb);
  6. }


  7. static int my_progress_func(char *progress_data,
  8.                      double t, /* dltotal */
  9.                      double d, /* dlnow */
  10.                      double ultotal,
  11.                      double ulnow)
  12. {
  13.   printf("%s %g / %g (%g %%)\n", progress_data, d, t, d*100.0/t);
  14.   return 0;
  15. }
复制代码

下载完成后,进行clean操作:

  1. void curl_base::DownloadFinish()
  2. {
  3.     curl_easy_cleanup(curl_handle);
  4. }
复制代码

主函数:

  1. void *myDownload_run(void *para){
  2.         mycurl::my_curl::curl_base curl;

  3.         curl.Download(downLoad_url,downLoad_file);
  4.         curl.DownloadFinish();
  5.         while(1){               
  6.                 sleep(2);
  7.         }
  8. }
复制代码

编译后,查看运行效果:

*  271917 / 364519 (74.5961 %)

*  273365 / 364519 (74.9933 %)

*  273365 / 364519 (74.9933 %)

*  276261 / 364519 (75.7878 %)

*  276261 / 364519 (75.7878 %)

*  277709 / 364519 (76.1851 %)

*  277709 / 364519 (76.1851 %)

*  279157 / 364519 (76.5823 %)

*  279157 / 364519 (76.5823 %)

*  280605 / 364519 (76.9795 %)

*  280605 / 364519 (76.9795 %)

*  282053 / 364519 (77.3768 %)

*  282053 / 364519 (77.3768 %)

*  283501 / 364519 (77.774 %)

*  283501 / 364519 (77.774 %)

*  284949 / 364519 (78.1712 %)

*  284949 / 364519 (78.1712 %)

*  296533 / 364519 (81.3491 %)

*  296533 / 364519 (81.3491 %)

*  297981 / 364519 (81.7464 %)

*  297981 / 364519 (81.7464 %)

*  302325 / 364519 (82.9381 %)

*  302325 / 364519 (82.9381 %)

*  305221 / 364519 (83.7325 %)

*  305221 / 364519 (83.7325 %)

*  306669 / 364519 (84.1298 %)

*  306669 / 364519 (84.1298 %)

*  308117 / 364519 (84.527 %)

*  308117 / 364519 (84.527 %)

*  309565 / 364519 (84.9242 %)

*  309565 / 364519 (84.9242 %)

*  312461 / 364519 (85.7187 %)

*  312461 / 364519 (85.7187 %)

*  313909 / 364519 (86.116 %)

*  313909 / 364519 (86.116 %)

*  315357 / 364519 (86.5132 %)

*  315357 / 364519 (86.5132 %)

*  316805 / 364519 (86.9104 %)

*  316805 / 364519 (86.9104 %)

*  319701 / 364519 (87.7049 %)

*  319701 / 364519 (87.7049 %)

*  321149 / 364519 (88.1021 %)

*  321149 / 364519 (88.1021 %)

*  322597 / 364519 (88.4994 %)

*  322597 / 364519 (88.4994 %)

*  334181 / 364519 (91.6773 %)

*  334181 / 364519 (91.6773 %)

*  335629 / 364519 (92.0745 %)

*  335629 / 364519 (92.0745 %)

*  337077 / 364519 (92.4717 %)

*  337077 / 364519 (92.4717 %)

*  338525 / 364519 (92.869 %)

*  338525 / 364519 (92.869 %)

*  342869 / 364519 (94.0607 %)

*  342869 / 364519 (94.0607 %)

*  344317 / 364519 (94.4579 %)

*  344317 / 364519 (94.4579 %)

*  348661 / 364519 (95.6496 %)

*  348661 / 364519 (95.6496 %)

*  350109 / 364519 (96.0468 %)

*  350109 / 364519 (96.0468 %)

*  351557 / 364519 (96.4441 %)

*  351557 / 364519 (96.4441 %)

*  353005 / 364519 (96.8413 %)

*  353005 / 364519 (96.8413 %)

*  355901 / 364519 (97.6358 %)

*  355901 / 364519 (97.6358 %)

*  358797 / 364519 (98.4303 %)

*  358797 / 364519 (98.4303 %)

*  361693 / 364519 (99.2247 %)

*  361693 / 364519 (99.2247 %)

*  363141 / 364519 (99.622 %)

*  363141 / 364519 (99.622 %)

*  364519 / 364519 (100 %)



OK,完成!!!



回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106738
QQ
发表于 2020-4-21 09:16:26 | 显示全部楼层
就是单纯下载网址上的文件吗,这个单片机实现也很简单,给下面这个地址发送http post/get消息就行。

我之前获取百度地图就是这么干的

#define downLoad_url         "http://xxxx/downloadExample/update.tar.gz"
#define downLoad_file         "/home/xxx/update.tar.gz"
回复

使用道具 举报

4

主题

12

回帖

24

积分

新手上路

积分
24
 楼主| 发表于 2020-4-21 09:54:04 | 显示全部楼层
eric2013 发表于 2020-4-21 09:16
就是单纯下载网址上的文件吗,这个单片机实现也很简单,给下面这个地址发送http post/get消息就行。

我 ...

网络协议栈支持bsd也都很容易。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-3 01:33 , Processed in 0.155973 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表