Chinaunix首页 | 论坛 | 博客
  • 博客访问: 342373
  • 博文数量: 45
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 111
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-31 12:31
文章分类

全部博文(45)

文章存档

2015年(2)

2014年(19)

2013年(14)

2012年(10)

我的朋友

分类: 项目管理

2014-07-22 11:00:39

前言: 上节用纯linux的函数实现了和云端通讯, 本节开始利用传说中的神器libcurl

话说一个网络程序员对书法十分感兴趣,退休后决定在这方面有所建树。 于是花重金购买了上等的文房四宝。    
一日,饭后突生雅兴,一番磨墨拟纸,  并点上了上好的檀香,颇有王羲之风范,  又具颜真卿气势,定神片刻,泼墨挥毫,    
郑重地写下一行字:libcurl.
由此可知libcurl的刻骨铭心!
官方文档请参考这里 http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

刚接触 libcurl的时候, 自然的想到按照常规的思路来连接云端: 先连接Web, 再发送请求, 然后等待接受返回数据. 
这样思路的代码如下

点击(此处)折叠或打开

  1. /*----------------------------------------------------------------------------------------------------
  2. 名称: http_cloud_curl_simple.c
  3. 功能: 利用libcurl的API顺序实现云端通讯, 未利用回调机制
  4. -----------------------------------------------------------------------------------------------------*/
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <curl/curl.h>
  9. #include <assert.h>

  10. #include "../http_cloud.h"

  11. #define DBG printf

  12. //-----------------------------------------------------------------------------------------
  13. static void get_local_time(char *pc_str)
  14. {
  15.     time_t now;
  16.     struct tm *timenow;

  17.     assert(pc_str != NULL);

  18.     time(&now);
  19.     timenow = localtime(&now);
  20.     sprintf(pc_str, "%04d-%02d-%02dT%02d:%02d:%02d", timenow->tm_year+1900, timenow->tm_mon+1, timenow->tm_mday,
  21.                     timenow->tm_hour, timenow->tm_min, timenow->tm_sec);
  22. }

  23. /* Auxiliary function that waits on the socket. */
  24. static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
  25. {
  26.   struct timeval tv;
  27.   fd_set infd, outfd, errfd;
  28.   int res;

  29.   tv.tv_sec = timeout_ms / 1000;
  30.   tv.tv_usec= (timeout_ms % 1000) * 1000;

  31.   FD_ZERO(&infd);
  32.   FD_ZERO(&outfd);
  33.   FD_ZERO(&errfd);

  34.   FD_SET(sockfd, &errfd); /* always check for error */

  35.   if(for_recv) {
  36.     FD_SET(sockfd, &infd);
  37.   }
  38.   else {
  39.     FD_SET(sockfd, &outfd);
  40.   }

  41.   /* select() returns the number of signalled sockets or -1 */
  42.   res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
  43.   return res;
  44. }

  45. static char connect_cloud(char *pc_ret, const char *host_addr, const int portno, const char *request)
  46. {
  47.     CURL *curl;
  48.   CURLcode res;
  49.   /* Minimalistic http request */
  50.   //const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
  51.   //char request[1024] = "";
  52.   curl_socket_t sockfd; /* socket */
  53.   long sockextr;
  54.   size_t iolen;
  55.   curl_off_t nread;
  56.   int iLen = 0;
  57.   char cRet = 0;

  58.   assert((pc_ret != NULL) && (host_addr != NULL) && (request != NULL));

  59.   //curl_global_init(CURL_GLOBAL_DEFAULT);
  60.   curl = curl_easy_init();
  61.   if (!curl)    return 0;

  62.   //curl_easy_setopt(curl, CURLOPT_URL, "");
  63.   curl_easy_setopt(curl, CURLOPT_URL, host_addr);
  64.   /* Do not do the transfer - only connect to host */
  65.   curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
  66.   res = curl_easy_perform(curl);
  67.   if(CURLE_OK != res) {
  68.     printf("Error: %s\n", strerror(res));
  69.     return -1;
  70.   }

  71.   /* Extract the socket from the curl handle - we'll need it for waiting.
  72.    * Note that this API takes a pointer to a 'long' while we use
  73.    * curl_socket_t for sockets otherwise.
  74.    */
  75.   res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);
  76.   if(CURLE_OK != res) {
  77.     printf("Error: %s\n", curl_easy_strerror(res));
  78.     return -2;
  79.   }

  80.   sockfd = sockextr;
  81.   /* wait for the socket to become ready for sending */
  82.   if(!wait_on_socket(sockfd, 0, 60000L)) {
  83.     printf("Error: timeout.\n");
  84.     return -3;
  85.   }

  86.   /* Send the request. Real applications should check the iolen
  87.    * to see if all the request has been sent */
  88.   res = curl_easy_send(curl, request, strlen(request), &iolen);
  89.   if(CURLE_OK != res) {
  90.     printf("Error: %s\n", curl_easy_strerror(res));
  91.     return -4;
  92.   }

  93.   /* read the response */
  94.   char buf[1024];
  95.   while(1) {
  96.     wait_on_socket(sockfd, 1, 60000L);
  97.     res = curl_easy_recv(curl, buf, 1024, &iolen);

  98.     if(CURLE_OK != res)
  99.       break;

  100.     nread = (curl_off_t)iolen;
  101.   }
  102.   buf[nread] = '\0';
  103.   DBG("Received (%" CURL_FORMAT_CURL_OFF_T "), %s\n", nread, buf);

  104.   /* always cleanup */
  105.   curl_easy_cleanup(curl);

  106.   pc_ret = strstr(buf, "\r\n\r\n");
  107.     if (pc_ret) {
  108.         pc_ret += 4;
  109.         DBG("ret = %s\n", pc_ret);
  110.     }

  111.   return 1;
  112. }

  113. #if (YEELINK == 1)
  114. int yeelink_create_data(const int device_id, const int sensor_id, const float device_value)
  115. {
  116.     char pc_ret[200], request[1024], pc_json[100], pc_time[30], pc_host_file[100], pc_header[100], ret;
  117.     int len;

  118.     sprintf(pc_host_file, "v1.0/device/%d/sensor/%d/datapoints", device_id, sensor_id);
  119.     sprintf(pc_header, "U-ApiKey: %s", YEELINK_API_KEY);

  120.     get_local_time(pc_time);
  121.     sprintf(pc_json, "{\"timestamp\":\"%s\",\"value\":%.2f}", pc_time, device_value);
  122.     len = strlen(pc_json);

  123.     sprintf(request, "POST /%s HTTP/1.1\r\nHost: %s\r\nAccept: */*\r\n%s\r\nContent-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\nConnection: Close\r\n\r\n%s\r\n",
  124.         pc_host_file, YEELINK_HOST, pc_header, len, pc_json);
  125.     DBG("request = %s\n", request);

  126.     ret = connect_cloud(pc_ret, YEELINK_HOST, YEELINK_PORT, request);

  127.     return(ret);
  128. }
  129. #endif

  130. #if (LEWEI50 == 1)
  131. //curl --request POST http://www.lewei50.com/api/V1/Gateway/UpdateSensors/01 --data "[{\"Name\":\"T1\",\"Value\":\"23.08\"}]" --header "userkey:36be8ff22f794f1e8a0bee3336eef237"
  132. int lewei50_create_data(const char *device_id, const float device_value)
  133. {
  134.     char pc_ret[200], request[1024], pc_json[100], pc_header[100], ret;
  135.     int len;

  136.     assert(device_id != NULL);

  137.     sprintf(pc_header, "userkey: %s", LEWEI50_USER_KEY);
  138.     sprintf(pc_json, "[{\"Name\":\"%s\",\"Value\":\"%.2f\"}]", device_id, device_value);
  139.     len = strlen(pc_json);

  140.     sprintf(request, "POST /%s HTTP/1.1\r\nHost: %s\r\nAccept: */*\r\n%s\r\nContent-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\nConnection: Close\r\n\r\n%s\r\n",
  141.         LEWEI50_HOST_FILE, LEWEI50_HOST, pc_header, len, pc_json);
  142.     DBG("request = %s\n", request);

  143.     ret = connect_cloud(pc_ret, LEWEI50_HOST, LEWEI50_PORT, request);

  144.     return(ret);
  145. }
  146. #endif

  147. //-------------------------------------------------------------------
  148. int main(void)
  149. {
  150.     float f_value = 15.02;
  151.     int i_tmp;

  152.     time_t t;
  153.   srand((unsigned)time(&t));    //初始化随机种子, 否则随机数不随机

  154.     i_tmp = rand();
  155.     i_tmp -= (i_tmp >> 4 << 4);
  156.   f_value += i_tmp;

  157. #if (YEELINK == 1)
  158.     yeelink_create_data(YEELINK_DEVICE_ID, YEELINK_SENSOR_ID, f_value);
  159. #endif

  160. #if (LEWEI50 == 1)
  161.     lewei50_create_data(LEWEI50_DEVICE_ID, f_value);
  162. #endif

  163.     return 1;
  164. }


Makefile: 注意openwrt的连接参数要多加的, 为了这几个字母, 折腾了好长时间

点击(此处)折叠或打开

  1. OPENWRT = 1

  2. ifeq ($(OPENWRT), 1)
  3.     CC = ~/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc
  4.     CFLAGS += -I ~/openwrt-lib/include -L ~/openwrt-lib/lib
  5.     LFLAGS += -lcurl -lcrypto -lz -lssl

  6. else
  7.     CC = gcc
  8.     LFLAGS += -lcurl
  9. endif

  10. CFLAGS += -Wall -O2
  11. #CFLAGS += -g

  12. #可执行文件名和相关的obj文件
  13. APP_BINARY = http_cloud
  14. SRCS += http_cloud_curl_simple.c
  15. OBJS = $(SRCS:.c=.o)

  16. all: APP_FILE

  17. APP_FILE: $(OBJS)
  18.     $(CC) $(CFLAGS) $(OBJS) -o $(APP_BINARY) $(LFLAGS)

  19. .PHONY: clean
  20. clean:
  21.     @echo "cleanning project"
  22.     $(RM) *.a $(OBJS) *~ *.so *.lo $(APP_BINARY)
  23.     @echo "clean completed"

openwrt下的运行结果如下
root@OpenWrt:/xutest# ./http_cloud_libcurl
yeelini ret = HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Thu, 07 Nov 2013 00:56:56 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Set-Cookie: CAKEPHP=dfvacsdpb8ls802dulnt695kk2; expires=Fri, 15-Nov-2013 08:56:56 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"

lewei50 ret = HTTP/1.1 200 OK
Date: Thu, 07 Nov 2013 00:56:56 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 44
Connection: close
Cache-Control: private
Set-Cookie: SERVERID=a4a5b2bbca16d8c8b2ba6d5b6e55f36e|1383785816|1383785816;Path=/

{"Successful":true,"Message":"Successful. "}

root@OpenWrt:/xutest# 


这仅仅是libcurl最最简单的应用, 本着'师夷长技以制夷'的原则, 继续看官方文档中......


阅读(3863) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~