-
#include <stdio.h>
-
#include <string.h>
-
#include <sys/socket.h>
-
#include <sys/types.h>
-
#include <netinet/in.h>
-
#include <stdlib.h>
-
-
#define _IPV4_VAL_(addr3,addr2,addr1,addr0) ((((unsigned long)addr3 << 24) & 0xFF000000) | (((unsigned long)addr2 << 16) & 0xFF0000) | (((unsigned long)addr1 << 8) & 0xFF00) | (((unsigned long)addr0) & 0xFF))
-
#define _VERSION_ "0321_1915_1913_5"
-
#define _UID_ "952700000ABNIJJF"
-
-
#define _HTTP_SERVER_PORT_ 80
-
//unsigned char gUpdate_server_ip[4] = {52,20,136,123};
-
unsigned char gUpdate_server_ip[4] = {54,172,84,104};
-
-
-
/*
-
http://api.reolink.com/v1.0/roms/upgrades/latest?build=0321_1915_1913_5&board=BI&type=BI&uid=952700000ABNIJJF
-
*/
-
int Update_online(char *version, char *uid, unsigned long *update_flag)
-
{
-
int ret_val = -1;
-
int sock_fd = -1;
-
int total_size = 0;
-
int recv_size = 0;
-
char *pXml_start = NULL;
-
socklen_t socklen = 0;
-
struct sockaddr_in server_addr;
-
char *pBuffer = NULL;
-
#define _BUFFER_SIZE_ 512
-
-
/**/
-
pBuffer = (char *)malloc(_BUFFER_SIZE_);
-
if(pBuffer == NULL)
-
{
-
printf("malloc Failed\n");
-
goto _OVER_;
-
}
-
-
/**/
-
memset(&server_addr, 0, sizeof(struct sockaddr_in));
-
server_addr.sin_family = AF_INET;
-
server_addr.sin_port = htons(_HTTP_SERVER_PORT_);
-
server_addr.sin_addr.s_addr = htonl(_IPV4_VAL_(gUpdate_server_ip[0], gUpdate_server_ip[1], gUpdate_server_ip[2], gUpdate_server_ip[3]));
-
-
-
/**/
-
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
-
if(sock_fd < 0)
-
{
-
printf("socket Failed\n");
-
goto _OVER_;
-
}
-
-
/**/
-
socklen = sizeof(struct sockaddr_in);
-
ret_val = connect(sock_fd, (struct sockaddr *)&server_addr, socklen);
-
if(ret_val < 0)
-
{
-
printf("connect Failed\n");
-
goto _OVER_;
-
}
-
-
/**/
-
memset(pBuffer, 0, _BUFFER_SIZE_);
-
snprintf(pBuffer + strlen(pBuffer), _BUFFER_SIZE_ - strlen(pBuffer),
-
"GET /v1.0/roms/upgrades/latest?build=%s&board=BI&type=BI&uid=%s HTTP/1.1\r\n", version, uid);
-
-
snprintf(pBuffer + strlen(pBuffer), _BUFFER_SIZE_ - strlen(pBuffer),
-
"Host: %d.%d.&d.%d:%d\r\n", gUpdate_server_ip[0], gUpdate_server_ip[1],
-
gUpdate_server_ip[2], gUpdate_server_ip[3], _HTTP_SERVER_PORT_);
-
snprintf(pBuffer + strlen(pBuffer), _BUFFER_SIZE_ - strlen(pBuffer), "\r\n");
-
-
printf("pBuffer:%s\n", pBuffer);
-
ret_val = send(sock_fd, pBuffer, strlen(pBuffer), 0);
-
if(ret_val != strlen(pBuffer))
-
{
-
printf("send Failed\n");
-
goto _OVER_;
-
}
-
-
total_size = 0;
-
while(1)
-
{
-
recv_size = recv(sock_fd, pBuffer + total_size, _BUFFER_SIZE_ - total_size - 1, 0);
-
printf("pBuffer:%s\n", pBuffer);
-
if(recv_size > 0)
-
{
-
total_size += recv_size;
-
}
-
else
-
{
-
printf("recv Failed\n");
-
goto _OVER_;
-
}
-
-
/**/
-
pBuffer[total_size] = '\0';
-
-
/**/
-
if(strstr(pBuffer, "/>") != NULL)
-
{
-
printf("Online_update_detect_ _OU_recv_ OK\n");
-
break;
-
}
-
-
/**/
-
pXml_start = strstr(pBuffer, "<");
-
if(pXml_start != NULL)
-
{
-
if(pXml_start != pBuffer)
-
{
-
total_size -= (pXml_start - pBuffer);
-
memcpy(pBuffer, pXml_start, total_size);
-
}
-
}
-
else
-
{
-
total_size = 0;
-
}
-
-
/**/
-
if((_BUFFER_SIZE_ - total_size - 1) <= 0)
-
{
-
printf("Online_update_detect_ message too long\n\r");
-
goto _OVER_;
-
}
-
}
-
-
/**/
-
if(strstr(pBuffer, "needupdate=\"1\"") != NULL)
-
{
-
*update_flag = 1;
-
}
-
else
-
{
-
*update_flag = 0;
-
}
-
-
_OVER_:
-
if(pBuffer != NULL)
-
{
-
free(pBuffer);
-
}
-
if(sock_fd >= 0)
-
{
-
close(sock_fd);
-
}
-
-
return ret_val;
-
}
-
-
int main(int argc,char **argv)
-
{
-
unsigned long update_flag = 0;
-
Update_online(_VERSION_, _UID_, &update_flag);
-
printf("update_flag:%ld\n", update_flag);
-
return 0;
-
}
运行代码的结果:
root@tay:/home/tay/code/update_online# ./a.out
pBuffer:GET /v1.0/roms/upgrades/latest?build=0321_1915_1913_5&board=BI&type=BI&uid=952700000ABNIJJF HTTP/1.1
Host: 54.172.&d.84:104
pBuffer:HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: bc-access-key, bc-machine-code, bc-captcha, bc-captcha-id, bc-auth-code, bc-expires, bc-nonce, bc-timestamp, accept, Content-Type
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,PATCH,OPTIONS,HEAD
Access-Control-Max-Age: 1728000
Cache-Control: no-store, no-cache, must-revalidate
Content-Type: application/xml; charset=utf-8
Date: Sat, 13 May 2017 13:41:12 GMT
Expires: Mon, 26 Jul 1970 05:00:00 GMT
Server: Reolink
pBuffer: Cloud
X-Powered-By: Reolink-Cloud-API/1.0
Content-Length: 327
Connection: keep-alive
needupdate="1"
img_name=""
type="BI"
img_version="0328_1915_1945_7"
board_name="BI"
changelog="no change logs"
filesize="4202064"
crc="-898126697"
forcerestore="1"
burn="0"
/>Date: Sat, 13 May 2017 13:41:12 GMT
Expires: Mon, 26 Jul 1970 05:00:00 GMT
Server: Reolink
Online_update_detect_ _OU_recv_ OK
need update
update_flag:1
阅读(712) | 评论(0) | 转发(0) |