Simple GET request with wip_getFile()


#1

I am having difficulty relating API wip_getFile() to it’s corresponding at commands +wipcreate and +wipdata. I understand HTTP and can easily retrieve data from a web host either manually through hyperterm or by at commands in an app. I can’t seem to do the same with the HTTP API’s…

I want to send this simple GET method to google home page, as I have done on hyperterm…

at+wipcfg=1
OK
at+wipbr=1,6
OK
at+wipbr=2,6,11,“orbcomm.t-mobile.com
OK
at+wipbr=4,6,0
OK

+CGREG: 5,“23A0”,“DC3B”

+WIND: 15,3,"-32",4,“11/11/18,21:22:30-32”,6,“0”
at+wipcreate=2,1,“www.google.com”,80
OK

+WIPREADY: 2,1
at+wipdata=2,1,1
CONNECT

[b]GET / HTTP/1.1

Host: google.com

Connection: keep-alive[/b]

HTTP/1.1 200 OK

Date: Fri, 18 Nov 2011 21:23:40 GMT

… google data

… by using the wip_getFile. Now I have based my code on the http ex. in the Open AT tutorial book. When I run the app the bearer initializes fine but return value from wip_getFileOpts is always NULL indicating error. I have included both message headers and specified the GET method.

Can anyone see where I am going wrong?
Thanks

#include "adl_global.h"
#include "wip_net.h"
#include "wip_bearer.h"
#include "wip_http.h"
#include "adl_at.h"
#include "generated.h"

const u16 wm_apmCustomStackSize = 4096;

//APN settings
#define gprs_apn	"orbcomm.t-mobile.com"
#define gprs_login	""
#define gprs_passw	""

//HTTP settings
ascii *HTTP_HOSTNAME = "www.google.com";
ascii *HTTP_URLNAME  = "www.google.com";

//Other variables
wip_bearer_t bearerHandle = NULL;
wip_channel_t httpCnxCh   = NULL;
wip_channel_t httpDataCh  = NULL;
adl_tmr_t *timer_ptr;
u32 timeout_period = 5;

//Function definitions
void sim_handler(u8 Event);
void evh_bearer(wip_bearer_t b, s8 event, void *ctx);
void timer_handler(u8 Id, void *context);
void httpData_handler(wip_event_t *ev, void *ctx);



void httpData_handler(wip_event_t *ev, void *ctx)
{
	adl_atSendResponse(ADL_AT_UNS,"Inside httpData_handler\r\n");
	switch(ev->kind)
	{
	case WIP_CEV_OPEN:
		adl_atSendResponse(ADL_AT_UNS,"httpData_handler open\r\n");
		break;

	case WIP_CEV_READ:
		{
		u8 buf[256];
		wm_memset(buf,0,256);
		//Collect reponse from server
		adl_atSendResponse(ADL_AT_UNS,"httpData_handler read\r\n");
		while (wip_read(httpDataCh, buf, 256) > 0)
			adl_atSendResponse(ADL_AT_UNS,"Reading data...\r\n");
		}
		break;

	case WIP_CEV_WRITE:
		adl_atSendResponse(ADL_AT_UNS,"httpData_handler write\r\n");
		break;

	case WIP_CEV_PEER_CLOSE:
		adl_atSendResponse(ADL_AT_UNS,"httpData_handler peer closed\r\n");
		wip_close(httpDataCh);
		wip_close(httpCnxCh);
		break;

	case WIP_CEV_ERROR:
		adl_atSendResponse(ADL_AT_UNS,"httpData_handler error\r\n");
		break;

	case WIP_CEV_PING:
		adl_atSendResponse(ADL_AT_UNS,"httpData_handler ping\r\n");
		break;

	case WIP_CEV_DONE:
		adl_atSendResponse(ADL_AT_UNS,"httpData_handler done\r\n");
		break;
	}
}


void timer_handler(u8 Id, void *context)
{
	adl_atSendResponse(ADL_AT_UNS,"\r\nInside timer_handler\r\n");
	//Start HTTP data session
	httpDataCh = wip_getFileOpts   (httpCnxCh,
									HTTP_URLNAME,
									(wip_eventHandler_f) httpData_handler,
									NULL,
									WIP_COPT_HTTP_METHOD, WIP_HTTP_METHOD_GET,
									WIP_COPT_HTTP_HEADER, "Host", HTTP_HOSTNAME,
									WIP_COPT_HTTP_HEADER, "Connection", "keep-alive",
									WIP_COPT_END);
	if (httpDataCh == NULL)
		adl_atSendResponse(ADL_AT_UNS,"wip_getFileOpts error occurred!!\r\n");
}


void evh_bearer(wip_bearer_t b, s8 event, void *ctx)
{
	switch(event)
	{
	case WIP_BEV_IP_CONNECTED:
		adl_atSendResponse(ADL_AT_UNS,"\r\nevh_bearer connected\r\n");
		//Start HTTP session
		httpCnxCh = wip_HTTPClientCreate(NULL, NULL);
		if (httpCnxCh == NULL)
			adl_atSendResponse(ADL_AT_UNS,"wip_HTTPClientCreate error occurred!!\r\n");
		timer_ptr = (adl_tmr_t*) adl_tmrSubscribe(FALSE, timeout_period, ADL_TMR_TYPE_100MS, (adl_tmrHandler_t) timer_handler);
		break;

	case WIP_BEV_CONN_FAILED:
		adl_atSendResponse(ADL_AT_UNS,"\r\nevh_bearer connection failed\r\n");
		wip_bearerClose(bearerHandle);
		break;

	case WIP_BEV_STOPPED:
		adl_atSendResponse(ADL_AT_UNS,"r\nevh_bearer connection stopped\r\n");
		wip_bearerClose(bearerHandle);
		break;

	default:
		adl_atSendResponse(ADL_AT_UNS,"r\nevh_bearer actions unknown\r\n");
		break;
	}

}


void sim_handler(u8 Event)
{
	if (ADL_SIM_EVENT_FULL_INIT == Event)
	{
		s8 ret;
		ascii retStr[30];

		//Open the GPRS bearer
		ret = wip_bearerOpen(&bearerHandle, "GPRS", (wip_bearerHandler_f) evh_bearer, NULL);
		wm_itoa(ret, retStr);
		adl_atSendResponse(ADL_AT_UNS,"\r\nReturn of wip_bearerOpen: ");
		adl_atSendResponse(ADL_AT_UNS,retStr);

		ret = wip_bearerSetOpts(bearerHandle, WIP_BOPT_GPRS_APN, gprs_apn, WIP_BOPT_LOGIN, gprs_login, WIP_BOPT_PASSWORD, gprs_passw, WIP_BOPT_END);
		wm_itoa(ret, retStr);
		adl_atSendResponse(ADL_AT_UNS,"\r\nReturn of wip_bearerSetOpts: ");
		adl_atSendResponse(ADL_AT_UNS,retStr);

		ret = wip_bearerStart(bearerHandle);
	}
}


void main_task(adl_InitType_e InitType)
{
	adl_atSendResponse(ADL_AT_UNS,"Program started...\r\n");
	adl_simSubscribe((adl_simHdlr_f) sim_handler, NULL);
	wip_netInit();
}

#2

You need to create a HTTP session channel first.

http_ClientTestCtx.CnxChannel = wip_HTTPClientCreateOpts (  NULL, // no handler
                                                            NULL, // no context
                                                         );

#3

I have HTTPClientCreate() in evh_bearer and the channel opens fine… Any other ideas?

void evh_bearer(wip_bearer_t b, s8 event, void *ctx)
{
   switch(event)
   {
   case WIP_BEV_IP_CONNECTED:
      adl_atSendResponse(ADL_AT_UNS,"\r\nevh_bearer connected\r\n");
      //Start HTTP session
      httpCnxCh = wip_HTTPClientCreate(NULL, NULL);
      if (httpCnxCh == NULL)

#4

Hi,

ADL_SIM_EVENT_FULL_INIT does not mean that network registration has occurred yet.
You can use “AT+CREG?” to check for network registration, or at least check the result of

ret = wip_bearerStart(bearerHandle);

#5

It is necessary - but not sufficient :exclamation:


#6

You’re both right I should poll creg to ensure proper registration before proceeding. I added that loop to my code as well as a check on the return of wip_bearerStart. wip_bearerStart fist returns WIP_BERR_OK_INPROGRESS and then goes into the bearer handler with event WIP_BEV_IP_CONNECTED indicating a good connection.

So it seems everything is fine up until I send wip_getFileOpts… Unfortunately it simply returns NULL on failure rather than a code indicating the cause. Is wip_getFileOpts the ideal function to use to send a simple GET command with a few headers? I am not “getting a file”… Maybe I should be using wip_TCPClientCreate and sending the headers with wip_write?

I appreciate your guys’ advice :mrgreen:


#7

Hi,

Your problem is that your HOST_NAME and URL_NAME are not properly defined.

You should have,for example you have:

ascii *HTTP_URLNAME  = "www.google.com";

and should be:

ascii *HTTP_URLNAME  = "http://www.google.com";

#8

Thanks Terrence. I now have it working now with wip_TCPClientCreate