HTTP Demo
```language
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include "osa.h"
#include "utils.h"
#include "libhttpclient.h"
#undef printf
#define printf(fmt, args...) fatal_printf("[http]: "fmt, ##args)
#define sleep(x) OSATaskSleep((x) * 200)//second
#define _TASK_STACK_SIZE 1024*10
static UINT32 _task_stack[_TASK_STACK_SIZE/sizeof(UINT32)];
static OSTaskRef _task_ref = NULL;
static OSATimerRef _timer_ref = NULL;
static OSFlagRef _flag_ref = NULL;
#define TASK_TIMER_CHANGE_FLAG_BIT 0x01
static void _timer_callback(UINT32 tmrId);
static void _task(void *ptr);
struct http_data_s {
unsigned data_sz;
UINT8 data[45*1024];
};
// Device bootup hook before Phase1Inits.
// If you have some work to be init, you may implete it here.
// ex: you may start your task here. or do some initialize here.
extern void Phase1Inits_enter(void);
// Device bootup hook after Phase1Inits.
// If you have some work to be init, you may implete it here.
// ex: you may start your task here. or do some initialize here.
extern void Phase1Inits_exit(void);
// Device bootup hook before Phase2Inits.
// If you have some work to be init, you may implete it here.
// ex: you may start your task here. or do some initialize here.
extern void Phase2Inits_enter(void);
// Device bootup hook after Phase2Inits.
// If you have some work to be init, you may implete it here.
// ex: you may start your task here. or do some initialize here.
extern void Phase2Inits_exit(void);
void Phase1Inits_enter(void)
{
}
void Phase1Inits_exit(void)
{
}
void Phase2Inits_enter(void)
{
}
void Phase2Inits_exit(void)
{int ret;
ret = OSAFlagCreate(&_flag_ref);
ASSERT(ret == OS_SUCCESS);
ret = OSATimerCreate(&_timer_ref);
ASSERT(ret == OS_SUCCESS);
ret = OSATaskCreate(&_task_ref, _task_stack, _TASK_STACK_SIZE, 200, "test-task", _task, NULL);
ASSERT(ret == OS_SUCCESS);
OSATimerStart(_timer_ref, 20 * 200, 20 * 200, _timer_callback, 0); // 10 seconds timer
}
extern int SendATCMDWaitResp(int sATPInd,char *in_str, int timeout, char *ok_fmt, int ok_flag,
char *err_fmt, char *out_str, int resplen);
//detect net status, enable or disable
static void wait_dev_reg_net(void)
{
char mmRspBuf[100] = {0};
int err;
wait_reg:
memset(mmRspBuf, 0, sizeof(mmRspBuf));
err = SendATCMDWaitResp(8, "AT^SYSINFO\r\n", 150, NULL, 1, "^SYSINFO ERROR", mmRspBuf, sizeof(mmRspBuf));
printf("data==> %s\n", mmRspBuf);
if(err != 0){
printf("AT^SYSINFO return error%d msg : %s\n ",err, mmRspBuf);
sleep(2);
goto wait_reg;
}
if(strstr(mmRspBuf, "^SYSINFO: 2,3") != NULL || strstr(mmRspBuf, "^SYSINFO: 2,2") != NULL){
return ;
}else{
sleep(2);
goto wait_reg;
}
}
static int _response_cb(char *buffer, int size, int nitems, void *private_data)
{
struct http_data_s *client_data = private_data;
if ((client_data->data_sz + size) < sizeof(client_data->data)) {
memcpy(client_data->data + client_data->data_sz, buffer, size);
client_data->data_sz += size;
return 0;
}
return -1;
}
static void _timer_callback(UINT32 tmrId)
{
OSAFlagSet(_flag_ref, TASK_TIMER_CHANGE_FLAG_BIT, OSA_FLAG_OR);
}
static void _task(void *ptr)
{
printf("enter task!");
OSA_STATUS status;
UINT32 flag_value;
UINT32 flag_mask = TASK_TIMER_CHANGE_FLAG_BIT;
//wait_dev_reg_net();
struct http_client *client = NULL;
struct http_data_s *client_data = NULL;
int response_code = 0;
int i = 0;
PRO_START:
client_data = malloc(sizeof(*client_data));
if (!client_data)
goto clean;
memset(client_data, 0, sizeof(*client_data));
//memset(&client_data,0,sizeof(client_data));
while(1) {
status = OSAFlagWait(_flag_ref, flag_mask, OSA_FLAG_OR_CLEAR, &flag_value, OSA_SUSPEND);
ASSERT(status == OS_SUCCESS);
response_code = 0;
client = http_client_init();
if (!client)
goto clean;
// http_client_setopt(client, HTTPCLIENT_OPT_URL, _USER_URL);
http_client_setopt(client, HTTPCLIENT_OPT_URL, "http://www.baidu.com");
http_client_setopt(client, HTTPCLIENT_OPT_RESPONSECB, _response_cb);
http_client_setopt(client, HTTPCLIENT_OPT_RESPONSECB_DATA, client_data);
http_client_setopt(client, HTTPCLIENT_OPT_METHOD, HTTPCLIENT_REQUEST_GET);
//http_client_setopt(client, HTTPCLIENT_OPT_HTTPHEADER, client_hdr);
http_client_perform(client);
http_client_getinfo(client, HTTPCLIENT_GETINFO_RESPONSE_CODE, &response_code);
printf("[http_client_test]Get tcp state %d\n", response_code);
if (response_code >= 200 && response_code < 300)
{
if(client_data->data_sz)
{
printf("\r\n data_sz=%u, %s", client_data->data_sz,client_data->data);
printf("\r\nresult:");
for (i = 0; i < 100; i++)
{
printf("%02x ",client_data->data[i]);
}
}
}else if (response_code == 404) {
printf("response_code == %d\r\n%s",response_code ,client_data->data);
}
if (flag_value & TASK_TIMER_CHANGE_FLAG_BIT) {
static int count = 0;
count++;
printf("asr_test _task: count: %d\n", count);
sleep(3);
if (count > 10) {
printf("asr_test _task: stop timer");
OSATimerStop(_timer_ref);
}
}
}
clean:
if (client)
http_client_shutdown(client);
if (client_data)
free(client_data);
OSATimerStop(_timer_ref);
goto PRO_START;
}
```