前言: 上节用纯linux的函数实现了和云端通讯, 本节开始利用传说中的神器libcurl
话说一个网络程序员对书法十分感兴趣,退休后决定在这方面有所建树。 于是花重金购买了上等的文房四宝。
一日,饭后突生雅兴,一番磨墨拟纸, 并点上了上好的檀香,颇有王羲之风范, 又具颜真卿气势,定神片刻,泼墨挥毫,
郑重地写下一行字:libcurl.
由此可知libcurl的刻骨铭心!
官方文档请参考这里
http://curl.haxx.se/libcurl/c/curl_easy_setopt.html
刚接触 libcurl的时候, 自然的想到按照常规的思路来连接云端: 先连接Web, 再发送请求, 然后等待接受返回数据.
这样思路的代码如下
-
/*----------------------------------------------------------------------------------------------------
-
名称: http_cloud_curl_simple.c
-
功能: 利用libcurl的API顺序实现云端通讯, 未利用回调机制
-
-----------------------------------------------------------------------------------------------------*/
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <curl/curl.h>
-
#include <assert.h>
-
-
#include "../http_cloud.h"
-
-
#define DBG printf
-
-
//-----------------------------------------------------------------------------------------
-
static void get_local_time(char *pc_str)
-
{
-
time_t now;
-
struct tm *timenow;
-
-
assert(pc_str != NULL);
-
-
time(&now);
-
timenow = localtime(&now);
-
sprintf(pc_str, "%04d-%02d-%02dT%02d:%02d:%02d", timenow->tm_year+1900, timenow->tm_mon+1, timenow->tm_mday,
-
timenow->tm_hour, timenow->tm_min, timenow->tm_sec);
-
}
-
-
/* Auxiliary function that waits on the socket. */
-
static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
-
{
-
struct timeval tv;
-
fd_set infd, outfd, errfd;
-
int res;
-
-
tv.tv_sec = timeout_ms / 1000;
-
tv.tv_usec= (timeout_ms % 1000) * 1000;
-
-
FD_ZERO(&infd);
-
FD_ZERO(&outfd);
-
FD_ZERO(&errfd);
-
-
FD_SET(sockfd, &errfd); /* always check for error */
-
-
if(for_recv) {
-
FD_SET(sockfd, &infd);
-
}
-
else {
-
FD_SET(sockfd, &outfd);
-
}
-
-
/* select() returns the number of signalled sockets or -1 */
-
res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
-
return res;
-
}
-
-
static char connect_cloud(char *pc_ret, const char *host_addr, const int portno, const char *request)
-
{
-
CURL *curl;
-
CURLcode res;
-
/* Minimalistic http request */
-
//const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
-
//char request[1024] = "";
-
curl_socket_t sockfd; /* socket */
-
long sockextr;
-
size_t iolen;
-
curl_off_t nread;
-
int iLen = 0;
-
char cRet = 0;
-
-
assert((pc_ret != NULL) && (host_addr != NULL) && (request != NULL));
-
-
//curl_global_init(CURL_GLOBAL_DEFAULT);
-
curl = curl_easy_init();
-
if (!curl) return 0;
-
-
//curl_easy_setopt(curl, CURLOPT_URL, "");
-
curl_easy_setopt(curl, CURLOPT_URL, host_addr);
-
/* Do not do the transfer - only connect to host */
-
curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
-
res = curl_easy_perform(curl);
-
if(CURLE_OK != res) {
-
printf("Error: %s\n", strerror(res));
-
return -1;
-
}
-
-
/* Extract the socket from the curl handle - we'll need it for waiting.
-
* Note that this API takes a pointer to a 'long' while we use
-
* curl_socket_t for sockets otherwise.
-
*/
-
res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);
-
if(CURLE_OK != res) {
-
printf("Error: %s\n", curl_easy_strerror(res));
-
return -2;
-
}
-
-
sockfd = sockextr;
-
/* wait for the socket to become ready for sending */
-
if(!wait_on_socket(sockfd, 0, 60000L)) {
-
printf("Error: timeout.\n");
-
return -3;
-
}
-
-
/* Send the request. Real applications should check the iolen
-
* to see if all the request has been sent */
-
res = curl_easy_send(curl, request, strlen(request), &iolen);
-
if(CURLE_OK != res) {
-
printf("Error: %s\n", curl_easy_strerror(res));
-
return -4;
-
}
-
-
/* read the response */
-
char buf[1024];
-
while(1) {
-
wait_on_socket(sockfd, 1, 60000L);
-
res = curl_easy_recv(curl, buf, 1024, &iolen);
-
-
if(CURLE_OK != res)
-
break;
-
-
nread = (curl_off_t)iolen;
-
}
-
buf[nread] = '\0';
-
DBG("Received (%" CURL_FORMAT_CURL_OFF_T "), %s\n", nread, buf);
-
-
/* always cleanup */
-
curl_easy_cleanup(curl);
-
-
pc_ret = strstr(buf, "\r\n\r\n");
-
if (pc_ret) {
-
pc_ret += 4;
-
DBG("ret = %s\n", pc_ret);
-
}
-
-
return 1;
-
}
-
-
#if (YEELINK == 1)
-
int yeelink_create_data(const int device_id, const int sensor_id, const float device_value)
-
{
-
char pc_ret[200], request[1024], pc_json[100], pc_time[30], pc_host_file[100], pc_header[100], ret;
-
int len;
-
-
sprintf(pc_host_file, "v1.0/device/%d/sensor/%d/datapoints", device_id, sensor_id);
-
sprintf(pc_header, "U-ApiKey: %s", YEELINK_API_KEY);
-
-
get_local_time(pc_time);
-
sprintf(pc_json, "{\"timestamp\":\"%s\",\"value\":%.2f}", pc_time, device_value);
-
len = strlen(pc_json);
-
-
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",
-
pc_host_file, YEELINK_HOST, pc_header, len, pc_json);
-
DBG("request = %s\n", request);
-
-
ret = connect_cloud(pc_ret, YEELINK_HOST, YEELINK_PORT, request);
-
-
return(ret);
-
}
-
#endif
-
-
#if (LEWEI50 == 1)
-
//curl --request POST http://www.lewei50.com/api/V1/Gateway/UpdateSensors/01 --data "[{\"Name\":\"T1\",\"Value\":\"23.08\"}]" --header "userkey:36be8ff22f794f1e8a0bee3336eef237"
-
int lewei50_create_data(const char *device_id, const float device_value)
-
{
-
char pc_ret[200], request[1024], pc_json[100], pc_header[100], ret;
-
int len;
-
-
assert(device_id != NULL);
-
-
sprintf(pc_header, "userkey: %s", LEWEI50_USER_KEY);
-
sprintf(pc_json, "[{\"Name\":\"%s\",\"Value\":\"%.2f\"}]", device_id, device_value);
-
len = strlen(pc_json);
-
-
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",
-
LEWEI50_HOST_FILE, LEWEI50_HOST, pc_header, len, pc_json);
-
DBG("request = %s\n", request);
-
-
ret = connect_cloud(pc_ret, LEWEI50_HOST, LEWEI50_PORT, request);
-
-
return(ret);
-
}
-
#endif
-
-
//-------------------------------------------------------------------
-
int main(void)
-
{
-
float f_value = 15.02;
-
int i_tmp;
-
-
time_t t;
-
srand((unsigned)time(&t)); //初始化随机种子, 否则随机数不随机
-
-
i_tmp = rand();
-
i_tmp -= (i_tmp >> 4 << 4);
-
f_value += i_tmp;
-
-
#if (YEELINK == 1)
-
yeelink_create_data(YEELINK_DEVICE_ID, YEELINK_SENSOR_ID, f_value);
-
#endif
-
-
#if (LEWEI50 == 1)
-
lewei50_create_data(LEWEI50_DEVICE_ID, f_value);
-
#endif
-
-
return 1;
-
}
Makefile: 注意openwrt的连接参数要多加的, 为了这几个字母, 折腾了好长时间
-
OPENWRT = 1
-
-
ifeq ($(OPENWRT), 1)
-
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
-
CFLAGS += -I ~/openwrt-lib/include -L ~/openwrt-lib/lib
-
LFLAGS += -lcurl -lcrypto -lz -lssl
-
-
else
-
CC = gcc
-
LFLAGS += -lcurl
-
endif
-
-
CFLAGS += -Wall -O2
-
#CFLAGS += -g
-
-
#可执行文件名和相关的obj文件
-
APP_BINARY = http_cloud
-
SRCS += http_cloud_curl_simple.c
-
OBJS = $(SRCS:.c=.o)
-
-
all: APP_FILE
-
-
APP_FILE: $(OBJS)
-
$(CC) $(CFLAGS) $(OBJS) -o $(APP_BINARY) $(LFLAGS)
-
-
.PHONY: clean
-
clean:
-
@echo "cleanning project"
-
$(RM) *.a $(OBJS) *~ *.so *.lo $(APP_BINARY)
-
@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最最简单的应用, 本着'师夷长技以制夷'的原则, 继续看官方文档中......
阅读(1169) | 评论(0) | 转发(0) |