#include
#include
#include
#include
#include "zyf_app.h"
#include "zyf_trace.h"
#include "zyf_timer.h"
#include "zyf_thread.h"
#include "httpclient.h"
#include "example_httpclient.h"
static Uart_Param_t g_uart1param;
void UartWriteCallBack(void* Param) // general com
{
Uart_Param_t *uartparam = (Uart_Param_t *)Param;
if(Param == NULL)
{
return;
}
ZYF_UartWrite(uartparam->port,(uint8_t *)"UartWrite succeed\r\n",strlen("UartWrite succeed\r\n"));
ZYF_UartWriteCallbackSwitch(uartparam->port,false);
}
void UartReadCallBack(void* Param) //
{
uint32_t recvlen = 0;
Uart_Param_t *uartparam = (Uart_Param_t *)Param;
/*
UART_PORT1 = 0,
UART_PORT2 = 1,
UART_PORT3 = 2,
*/
ZYF_LOG("Uart%d recv",uartparam->port);
while(ZYF_UartRead(uartparam->port, &(uartparam->uartbuf[recvlen]), 1))
{
ZYF_LOG("recv :%02x",uartparam->uartbuf[recvlen]);
recvlen++;
}
ZYF_UartWrite(uartparam->port,uartparam->uartbuf,recvlen);
ZYF_UartWriteCallbackSwitch(uartparam->port,true);
}
static void AppUartInit(void)
{
int32_t ret;
g_uart1param.port = DEBUG_PORT;
ZYF_UartRegister(g_uart1param.port, UartReadCallBack,&g_uart1param);
ZYF_UartWriteCbRegister(g_uart1param.port,UartWriteCallBack,&g_uart1param);
ZYF_UartOpen(g_uart1param.port, 115200, ZYF_FC_NONE);
ZYF_LOG("AppUartInit");
return;
}
void ZYF_HttpPdpActCallback(uint8_t status)
{
ZYF_HttpClient_t *ptClient = &ZYF_HttpClient;
ZYF_AppMsg_t tMsg;
if (ptClient->ctx.ptMsg != NULL) {
tMsg.wMsgId = status;
ZYF_MsgQPut(ptClient->ctx.ptMsg, (void *)&tMsg);
}
}
void ZYF_PdpActiveCnf(uint8_t contextId, int32_t errCode)
{
ZYF_LOG("PDP: Active");
ZYF_HttpPdpActCallback(APP_MSG_PDP_ACTIVE);
}
void ZYF_PdpDeactiveCnf(uint8_t contextId, int32_t errCode)
{
ZYF_LOG("PDP: Deactive");
}
#if 1
/**
* @brief Http client "get" method working flow.
* @param None
* @return None
*/
void httpClientHead(void)
{
#if 1
#define HTTP_GET_URL "http://120.79.143.209:80/file/download.do?filename=BSJIOT.txt"
char *get_url = HTTP_GET_URL;
httpclient_t client = {0};
httpclient_data_t client_data = {0};
char *buf,*header_buf;
HTTPCLIENT_RESULT ret;
int val_pos, val_len;
buf = pvPortMalloc(BUF_SIZE);
if (buf == NULL) {
ZYF_LOG("memory malloc failed.");
return;
}
header_buf = pvPortMalloc(BUF_SIZE);
if (header_buf == NULL) {
vPortFree(buf);
ZYF_LOG("memory malloc failed.");
return;
}
memset(header_buf,0,BUF_SIZE);
client_data.header_buf_len = BUF_SIZE;
client_data.header_buf = header_buf;
client_data.response_buf = buf;
client_data.response_buf_len = BUF_SIZE;
client_data.response_buf[0] = '\0';
ret = httpclient_head(&client, get_url, &client_data);
if (ret < 0)
goto fail;
ZYF_LOG("heads received: %s", client_data.header_buf);
// get response header
if(0 == httpclient_get_response_header_value(client_data.header_buf, "Content-length", &val_pos, &val_len))
ZYF_LOG("Content-length: %.*s", val_len, client_data.header_buf + val_pos);
fail:
vPortFree(buf);
vPortFree(header_buf);
// Print final log
if (ret >= 0)
ZYF_LOG("example project test success.");
else
ZYF_LOG( "httpclient_get fail, reason:%d.", ret);
#endif
}
void httpClientGet(void)
{
char *url = "http://www.hizyf.com/";
httpclient_t client = {0};
httpclient_data_t client_data = {0};
char *buf = NULL;
int32_t recvlen = 0;
buf = pvPortMalloc(BUF_SIZE);
if (buf == NULL) {
ZYF_LOG("Malloc failed.\r\n");
return;
}
memset(buf, 0, BUF_SIZE);
client_data.response_buf = buf; //Sets a buffer to store the result.
client_data.response_buf_len = BUF_SIZE; //Sets the buffer size.
#if 0
int ret = httpclient_get(&client, url, &client_data);
#else
ZYF_LOG("url:: %s\r\n", url);
HTTPCLIENT_RESULT ret = HTTPCLIENT_ERROR_CONN;
ret = httpclient_connect(&client, url);
if (!ret) {
ret = httpclient_send_request(&client, url, HTTPCLIENT_GET, &client_data);
if (!ret) {
do{
ret = httpclient_recv_response(&client, &client_data,HTTPCLIENT_GET);
recvlen += client_data.content_block_len;
ZYF_LOG("Data received: %d,content_block_len:%d\r\n", recvlen,client_data.content_block_len);
ZYF_LOG("Data received: %s\r\n", client_data.response_buf);
}while(ret == HTTPCLIENT_RETRIEVE_MORE_DATA);
}
}
httpclient_close(&client);
#endif
vPortFree(buf);
return;
}
void httpClientPost(void)
{
char *url = "http://api.mediatek.com/mcs/v2/devices/D0n2yhrl/datapoints.csv";
char *header = "deviceKey:FZoo0S07CpwUHcrt\r\n";
char *content_type = "text/csv";
char *post_data = "1,,I am string!";
httpclient_t client = {0};
httpclient_data_t client_data = {0};
char *buf = NULL;
buf = pvPortMalloc(BUF_SIZE);
if (buf == NULL) {
ZYF_LOG("Malloc failed.\r\n");
return;
}
memset(buf, 0, BUF_SIZE);
client_data.response_buf = buf; //Sets a buffer to store the result.
client_data.response_buf_len = BUF_SIZE; //Sets the buffer size.
httpclient_set_custom_header(&client, header); //Sets the custom header if needed.
client_data.post_buf = post_data; //Sets the user data to be posted.
client_data.post_buf_len = strlen(post_data); //Sets the post data length.
client_data.post_content_type = content_type; //Sets the content type.
httpclient_post(&client, url, &client_data);
ZYF_LOG("Data received: %s\r\n", client_data.response_buf);
vPortFree(buf);
}
void httpClientPut(void)
{
char *url = "http://ec2-52-76-74-57.ap-southeast-1.compute.amazonaws.com/mcs/test/ok/200";
char *content_type = "text/csv";
char *put_data = "1,,I am string!";
httpclient_t client = {0};
httpclient_data_t client_data = {0};
char *buf = NULL;
buf = pvPortMalloc(BUF_SIZE);
if (buf == NULL) {
ZYF_LOG("Malloc failed.\r\n");
return;
}
memset(buf, 0, BUF_SIZE);
client_data.response_buf = buf; //Sets a buffer to store the result.
client_data.response_buf_len = BUF_SIZE; //Sets the buffer size.
client_data.post_buf = put_data; //Sets the user data to be put.
client_data.post_buf_len = strlen(put_data); //Sets the put data length.
client_data.post_content_type = content_type; //Sets the content type.
httpclient_put(&client, url, &client_data);
ZYF_LOG("Data received: %s\r\n", client_data.response_buf);
}
#endif
void HttpClienThread(void *pvParam)
{
ZYF_HttpCtx_t * ctx = (ZYF_HttpCtx_t*)pvParam;
ZYF_AppMsg_t tMsg;
ZYF_LOG("HttpClienThread enter!");
int iRet = -1;
while (1) {
iRet = ZYF_MsgQGet(ctx->ptMsg, (void *)&tMsg);
if (iRet < 0) {
ZYF_LOG("Failed to get msg");
ZYF_ThreadSleep(10);
}
switch(tMsg.wMsgId)
{
case APP_MSG_PDP_ACTIVE:
case APP_MSG_HTTP_TEST:
{
//httpClientHead();
httpClientGet();
//httpClientPost();
//httpClientPut();
ZYF_StartTimer(ctx->timer, 20000);
break;
}
default:
{
break;
}
}
}
}
static void ZYF_HttpTimerCallback(void *pvParam)
{
ZYF_HttpCtx_t * ctx = (ZYF_HttpCtx_t*)pvParam;
ZYF_AppMsg_t tMsg;
if (ctx->ptMsg != NULL) {
tMsg.wMsgId = APP_MSG_PDP_ACTIVE;
ZYF_MsgQPut(ctx->ptMsg, (void *)&tMsg);
}
}
void ZYF_HttpClientTest(void)
{
ZYF_HttpClient.ctx.ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t));
if(ZYF_HttpClient.ctx.ptMsg != NULL)
{
ZYF_LOG("Succeeded to create network msg");
}
ZYF_HttpClient.ctx.timer = ZYF_CreateTimer(ZYF_HttpTimerCallback, (void *)&ZYF_HttpClient.ctx);
ZYF_ThreadCreate("HttpClienThread", HttpClienThread, (void *)&ZYF_HttpClient.ctx, ZYF_PRIORITY_NORMAL, 1024 * 8);
}
void HttpClient_Example(void * Param)
{
ZYF_MsgQ_t *ptMsg;
ZYF_AppMsg_t tMsg;
int iRet = -1;
ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t));
ZYF_LOG("thread enter!");
ZYF_NetworkInit();
ZYF_HttpClientTest();
while (1) {
ZYF_LOG("in while.");
iRet = ZYF_MsgQGet(ptMsg, (void *)&tMsg);
if (iRet < 0) {
ZYF_LOG("Failed to get msg");
ZYF_ThreadSleep(1000);
}
}
}
static void prvInvokeGlobalCtors(void)
{
extern void (*__init_array_start[])();
extern void (*__init_array_end[])();
size_t count = __init_array_end - __init_array_start;
for (size_t i = 0; i < count; ++i)
__init_array_start[i]();
}
int appimg_enter(void *param)
{
AppUartInit();
ZYF_LOG("application image enter, param 0x%x", param);
prvInvokeGlobalCtors();
ZYF_ThreadCreate("HttpClient_Example", HttpClient_Example, NULL, ZYF_PRIORITY_HIGH, 10*1024);
return 0;
}
void appimg_exit(void)
{
OSI_LOGI(0, "application image exit");
}