Hisi3516E-基于ARM的linux平台移植curllib

一、LibCurl简介

LibCurl是免费的客户端URL传输库,支持FTP,FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE ,LDAP等协议,其主页是http://curl.haxx.se/。Libcurl具备线程安全、IpV6兼容、易于使用的特点。curl也是Linux下一个非常著名的下载库,通过这个库,可以很简单的实现文件的下载等操作。
这个库的接口直接输入url的链接就可以获取网络上的数据了。
比如输入http://www.xxxx.com/test.mp3
就可以拉这个MP3的文件数据下来了。

二、源码下载:

 https://curl.haxx.se/download.html

我这里下的是.tar.gz的,用tar -xzvf 命令解压就可以了

三、交叉编译环境设置

3.1开源软件移植技巧:

1.configure文件
./configure是用来检测你的安装平台的目标特征的。比如它会检测你是不是有CC或GCC,并不是需要CC或GCC,它是个shell脚本
./configure –help查看其他可选参数。
可以查看需要的功能

./configure –prefix=./out/curl –disable-shared –enable-static –without-libidn –without-ssl –without-librtmp –without-gnutls –without-nss –without-libssh2 –without-zlib –without-winidn –disable-rtsp –disable-ldap –disable-ldaps CC=arm-hisiv500-linux-gcc

CC= 可以指定编译器

3.2执行以下命令设置当前编译项:

./configure --prefix= --disable-shared --enable-static   --enable-http  --enable-ftp --enable-file  --disable-pop3  --disable-imap   --disable-smtp       --disable-gopher  --disable-proxy  --disable-ntlm-wb --disable-sspi --without-libidn --without-ssl --without-librtmp --without-gnutls --without-nss --without-libssh2 --without-zlib --without-winidn --disable-rtsp --disable-ldap --disable-ldaps  --without-darwinssl      --without-PACKAGE  --without-ssl     --without-gnutls  --without-polarssl  --without-cyassl   --without-nss  --without-axtls    --without-ca-path  --without-winssl  --without-libmetalink  --without-libssh2   --without-winidn   --without-libidn  --enable-ipv6 CC=arm-hisiv500-linux-gcc CPP=arm-hisiv600-linux-cpp --host=arm

./configure –prefix= –disable-shared –enable-static –enable-http –enable-ftp –enable-file –disable-pop3 –disable-imap –disable-smtp –disable-gopher –disable-proxy –disable-ntlm-wb –disable-sspi –without-libidn –without-ssl –without-librtmp –without-gnutls –without-nss –without-libssh2 –without-zlib –without-winidn –disable-rtsp –disable-ldap –disable-ldaps –without-darwinssl –without-PACKAGE –without-ssl –without-gnutls –without-polarssl –without-cyassl –without-nss –without-axtls –without-ca-path –without-winssl –without-libmetalink –without-libssh2 –without-winidn –without-libidn –enable-ipv6 CC=arm-hisiv500-linux-gcc CPP=arm-hisiv600-linux-cpp –host=arm
注意:不需要其它功能就disable,不然会在连接时报错, 如果你没有disable掉,但你的机器上又没有安装相应的库,link时会报错。
–host-arm 表示目标文件是arm平台
CC=arm-hisiv500-linux-gcc 指定C文件编译器
CPP=arm-hisiv600-linux-cpp 指定Cpp文件编译器

其他为选用相关的功能
我的目标平台是hisi的arm CPU。

执行完之后会打印出选用的功能:

 curl version:     7.38.0
  Host setup:       arm-unknown-none
  Install prefix:   
  Compiler:         arm-hisiv500-linux-gcc
  SSL support:      no      (--with-{ssl,gnutls,nss,polarssl,cyassl,axtls,winssl,darwinssl} )
  SSH support:      no      (--with-libssh2)
  zlib support:     no      (--with-zlib)
  GSS-API support:  no      (--with-gssapi)
  TLS-SRP support:  no      (--enable-tls-srp)
  resolver:         default (--enable-ares / --enable-threaded-resolver)
  ipv6 support:     enabled
  IDN support:      no      (--with-{libidn,winidn})
  Build libcurl:    Shared=no, Static=yes
  Built-in manual:  enabled
  --libcurl option: enabled (--disable-libcurl-option)
  Verbose errors:   enabled (--disable-verbose)
  SSPI support:     no      (--enable-sspi)
  ca cert bundle:   no
  ca cert path:     no
  LDAP support:     no      (--enable-ldap / --with-ldap-lib / --with-lber-lib)
  LDAPS support:    no      (--enable-ldaps)
  RTSP support:     no      (--enable-rtsp)
  RTMP support:     no      (--with-librtmp)
  metalink support: no      (--with-libmetalink)
  HTTP2 support:    disabled (--with-nghttp2)
  Protocols:        DICT FILE FTP HTTP TELNET TFTP

四. 编译
执行make
在 lib/.lib/ 目录下会生成libcurl.a

五、 LibCurl编程流程

在基于LibCurl的程序里,主要采用callback function (回调函数)的形式完成传输任务,用户在启动传输前设置好各类参数和回调函数,当满足条件时libcurl将调用用户的回调函数实现特定功能。下面是利用libcurl完成传输任务的流程:

  1. 调用curl_global_init()初始化libcurl

  2. 调用 curl_easy_init()函数得到 easy interface型指针

  3. 调用curl_easy_setopt设置传输选项

  4. 根据curl_easy_setopt设置的传输选项,实现回调函数以完成用户特定任务

  5. 调用curl_easy_perform()函数完成传输任务

  6. 调用curl_easy_cleanup()释放内存

在整过过程中设置curl_easy_setopt()参数是最关键的,几乎所有的libcurl程序都要使它。

2) 重要函数

1.CURLcode curl_global_init(long flags);

描述:这个函数只能用一次。(其实在调用curl_global_cleanup 函数后仍然可再用)

如果这个函数在curl_easy_init函数调用时还没调用,它讲由libcurl库自动完成。

参数:flags

CURL_GLOBAL_ALL //初始化所有的可能的调用。

CURL_GLOBAL_SSL //初始化支持 安全套接字层。

CURL_GLOBAL_WIN32 //初始化win32套接字库。

CURL_GLOBAL_NOTHING //没有额外的初始化

六、libcurl 编程实例

初始化函数

int PDT_LibCurl_Init(void)
{
    curl_global_init(CURL_GLOBAL_NOTHING);  
    curl = curl_easy_init();  
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, CURL_BACK_TIMEOUT);        //设置超时  
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);        //屏蔽其它信号 
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, PDT_LibCurl_callback_read_file); //设置下载数据的回调函数 

    return HI_SUCCESS;
}

设置URL,并且开始获取的函数

int PDT_LibCurl_GetData(char *url)
{
    int ret =0;
    curl_easy_setopt(curl, CURLOPT_URL,url);
    ret = curl_easy_perform(curl);  
    if(ret != CURLE_OK)  
    {  
        MLOGD("curl execute fail  %d \n",ret);  
    } 

    sem_post(&sem_RingRI_input);
    MLOGD("------end \n");

    return ret;
}

回调函数,获取数据的函数:

size_t PDT_LibCurl_callback_read_file(char *ptr, size_t size, size_t nmemb, void *userdata)
{
    size_t Ret;
//  MLOGD("size = %d \n",size);
    MLOGD("nmemb = %d \n",nmemb);

//  Ret = fwrite(ptr,1,size*nmemb,read_fp);

    Ret = PDT_COMM_InputRingBuff_Write(ptr,size*nmemb);

    return Ret;
}

PDT_COMM_InputRingBuff_Write是自己把获取到的数据导到其他地方去处理了。

由于作者水平有限,不足之处欢迎指正。

猜你喜欢

转载自blog.csdn.net/zhenglie110/article/details/79158183