I think it will be helpful if you look at my code:
const ascii * HTTP_STR_URL = “http://weather.vodafonernd.com/post.php”;
typedef struct
{
wip_channel_t CnxChannel; // session channel
wip_channel_t DataChannel; // data channel
u32 dataLength; // response data length
} http_ClientTestCtx_t;
http_ClientTestCtx_t http_ClientTestCtx;
wip_cstate_t wip_state;
ascii writbuf[45];
void fillwritbuf(void)
{
s16 m,n,p,w;
m = (s16) GPIO26info_t.TenMinTemp * 10; // Decimal precision: 1 digit
n = (GPIO26info_t.TenMinTemp * 10 ) - m; // Decimal precision: 1 digit
p = (s16) GPIO26info_t.TenMinHum * 10; // Decimal precision: 1 digit
w = (GPIO26info_t.TenMinHum * 10 ) - p; // Decimal precision: 1 digit
wm_sprintf(writbuf, “date_time=%04lu-%02lu-%02lu%s%02lu%s%02lu%s%02lu&temp=%d.%01d”,
GPStimedate_t.year,
GPStimedate_t.month,
GPStimedate_t.day,
“%20”,
GPStimedate_t.hour,
“%3A”,
GPStimedate_t.minute,
“%3A”,
GPStimedate_t.seconds,
(s16)GPIO26info_t.TenMinTemp, n);
}
/********************************************************************/
/ Function : http_ClientTestDataHandler /
/-------------------------------------------------------------------------/
/ Object : Handler for the HTTP data channel /
/ /
/ Returned : none /
/ /
/ /
/--------------------±–±--±–±---------------------------------------/
/ Variable Name |IN |OUT|GLB| Use /
/--------------------±–±--±–±---------------------------------------/
/ | | | | /
/**************************************************************************/
static void http_ClientTestDataHandler( wip_event_t *ev, void *ctx)
{
ascii tmpbuf[80];
ascii readbuf[256];
s32 BytesWritten, len, tmplen;
s32 status;
ascii info[50];
http_ClientTestCtx_t *pHttpClientCtx =
(http_ClientTestCtx_t ) &http_ClientTestCtx;
/ How many bytes of [tmpbuf] have already been sent. */
static int offset = 0;
switch(ev->kind)
{
case WIP_CEV_OPEN:
TRACE(( 4, “http_ClientTestDataHandler: WIP_CEV_OPEN\n” ));
// adl_atSendResponse ( ADL_AT_UNS, “http_ClientTestDataHandler: Start\r\n” );
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1, “\r\nhttp_ClientTestDataHandler: Start\r\n”);
// ready for writing data
pHttpClientCtx->dataLength = 0;
break;
case WIP_CEV_READ:
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1, "\r\nhttp_ClientTestDataHandler: WIP_CEV_READ\r\n");
// we must read all available data to trigger WIP_CEV_READ again
tmplen = 0;
while( (len = wip_read(ev->channel, readbuf, sizeof(readbuf)-1)) > 0) {
readbuf[len] = 0;
// adl_atSendResponse ( ADL_AT_UNS, readbuf );
tmplen += len;
}
TRACE(( 4, "http_ClientTestDataHandler: read %d bytes\n", tmplen ));
wm_sprintf( tmpbuf, "\r\nhttp_ClientTestDataHandler: %d bytes read\r\n", tmplen);
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1, tmpbuf);
// compute total length of response
pHttpClientCtx->dataLength += len;
break;
case WIP_CEV_WRITE:
TRACE(( 4, "http_ClientTestDataHandler: WIP_CEV_WRITE\n" ));
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1, "\r\nhttp_ClientTestDataHandler: WIP_CEV_WRITE\r\n");
wip_state = wip_getState(ev->channel);
if (wip_state==WIP_CSTATE_READY || WIP_CSTATE_BUSY) //&& GPSposition_t.fix_ok == TRUE)
{
// we must write all available data to trigger WIP_CEV_WRITE again
BytesWritten = wip_write( ev->channel, writbuf, sizeof(writbuf)-offset);
if(BytesWritten < 0)
{/* error */
wip_debug( "HTTP write error %i\n", BytesWritten);
adl_atSendResponse ( ADL_AT_UNS, "\r\nwip_write error\r\n" );
return;
}
offset += BytesWritten; /* Update the number of bytes sent. */
pHttpClientCtx->dataLength += offset;
if( offset == sizeof( writbuf)) { /* All of [buffer] has been sent */
TRACE(( 4, "http_ClientTestDataHandler: bytes %d written\n", offset ));
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1,writbuf);
wm_sprintf( info, "\r\nhttp_ClientTestDataHandler: %d bytes written\r\n", offset);
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1, info);
}
wip_shutdown(ev->channel,FALSE,TRUE);
}
/* else if (GPSposition_t.fix_ok == FALSE){
wip_shutdown( http_ClientTestCtx.DataChannel,FALSE,TRUE);
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1, "\r\nCannot send HTTP data: No GPS fix\r\n");
}*/
else if (wip_state==WIP_CSTATE_TO_CLOSE)
{
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1, "\r\nData channel is broken\r\n");
wip_close( ev->channel);
}
break;
case WIP_CEV_PEER_CLOSE:
TRACE(( 4, "http_ClientTestDataHandler: WIP_CEV_PEER_CLOSE\n" ));
adl_atSendResponse ( ADL_AT_UNS, "\r\nhttp_ClientTestDataHandler: Done\r\n" );
// end of data
// show response information
if( wip_getOpts( http_ClientTestCtx.DataChannel,
WIP_COPT_HTTP_STATUS_CODE, &status,
WIP_COPT_HTTP_STATUS_REASON, tmpbuf, sizeof(tmpbuf),
WIP_COPT_END) == OK) {
ascii sbuf[16];
adl_atSendResponse ( ADL_AT_UNS, "http_ClientTestDataHandler: Status=" );
wm_sprintf( sbuf, "%d", status);
adl_atSendResponse ( ADL_AT_UNS, sbuf);
adl_atSendResponse ( ADL_AT_UNS, "\r\nhttp_ClientTestDataHandler: Reason=\"" );
adl_atSendResponse ( ADL_AT_UNS, tmpbuf);
adl_atSendResponse ( ADL_AT_UNS, "\"\r\n" );
TRACE(( 4, "http_ClientTestDataHandler: Status=%d\n", status));
TRACE(( 4, "http_ClientTestDataHandler: Reason=\"%s\"\n", tmpbuf));
}
if( wip_getOpts( http_ClientTestCtx.DataChannel,
WIP_COPT_HTTP_HEADER, "content-type", tmpbuf, sizeof(tmpbuf),
WIP_COPT_END) == OK) {
TRACE(( 4, "http_ClientTestDataHandler: Content Type=\"%s\"\n", tmpbuf));
}
TRACE(( 4, "http_ClientTestDataHandler: Response Length=%d bytes\n",
pHttpClientCtx->dataLength ));
// data channel must be closed
wip_close( ev->channel);
break;
case WIP_CEV_ERROR:
TRACE(( 2, "http_ClientTestDataHandler: WIP_CEV_ERROR %d\n",
ev->content.error.errnum));
//adl_atSendResponse ( ADL_AT_UNS, "http_ClientTestDataHandler: ERROR\r\n" );
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1, "\r\nhttp_ClientTestDataHandler: ERROR\r\n");
// connection to server broken
wip_close( ev->channel);
break;
default:
TRACE(( 2, "http_ClientTestDataHandler: unexpected event: %d\n",
ev->kind));
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1, "\r\nunexpected event\r\n");
break;
}
}
static s32 http_Client(void)
{
s32 ret = 0;
//fill buffer with data
fillwritbuf();
// HTTP Session creation
// ---------------------------
http_ClientTestCtx.CnxChannel = wip_HTTPClientCreateOpts(
NULL, // no handler
NULL, // no context
WIP_COPT_HTTP_VERSION, WIP_HTTP_VERSION_1_0,
WIP_COPT_END);
if (http_ClientTestCtx.CnxChannel == NULL)
{
TRACE(( 2, "cannot create http session channel\n" ));
//adl_atSendResponse ( ADL_AT_UNS, "cannot create http session channel\r\n" );
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1, "\r\ncannot create http session channel\r\n");
ret = -1;
}
else {
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1, "\r\nhttp session channel created\r\n");
// HTTP POST command
http_ClientTestCtx.DataChannel = wip_putFileOpts(
http_ClientTestCtx.CnxChannel, // session channel
HTTP_STR_URL, // URL to receive the data
http_ClientTestDataHandler, // data handler
&http_ClientTestCtx, // context
WIP_COPT_HTTP_METHOD, WIP_HTTP_METHOD_POST,
// request headers
WIP_COPT_HTTP_HEADER, "Content-Length", "45",
WIP_COPT_HTTP_HEADER, "Accept", "text/plain",
WIP_COPT_HTTP_HEADER, "Connection", "keep-alive",
WIP_COPT_HTTP_HEADER, "Content-Type", "application/x-www-form-urlencoded",
WIP_COPT_HTTP_HEADER, "Host", "weather.vodafonernd.com",
WIP_COPT_END);
if (http_ClientTestCtx.DataChannel == NULL)
{
TRACE(( 2, "cannot create http data channel\n" ));
//adl_atSendResponse ( ADL_AT_UNS, "cannot create http data channel\r\n" );
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1, "\r\ncannot create http data channel\r\n");
wip_close( http_ClientTestCtx.CnxChannel);
// ret =-1;
}
else
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1, "\r\nHTTP data channel created\r\n");
}
return(ret);
}
void GPRSTimerhdl(u8 TimerIdFive)
{
http_Client();
}
/*******************************************************************/
/ Function : evh_bearer /
/-------------------------------------------------------------------------/
/ Object : bearer events handler: when the bearer connection is /
/ completed, start IP services /
/-------------------------------------------------------------------------/
/ Variable Name |IN |OUT|GLB| Utilisation /
/--------------------±–±--±–±---------------------------------------/
/ b | X | | | bearer identifier /
/ event | X | | | bearer event type /
/ ctx | | | | unused /
/--------------------±–±--±–±---------------------------------------/
/***************************************************************************/
static void evh_bearer( wip_bearer_t b, s8 event, void *ctx) {
if( WIP_BEV_IP_CONNECTED == event) {
TRACE(( 4, “http_ClientTestDataHandler: WIP_BEV_IP_CONNECTED \n” ));
adl_atSendResponsePort ( ADL_AT_RSP, ADL_PORT_UART1, “\r\nWIP_BEV_IP_CONNECTED\r\n”);
adl_tmrSubscribe ( TRUE,300, ADL_TMR_TYPE_100MS, GPRSTimerhdl );
}
}
//
/ Function : open_and_start_bearer() /
/--------------------------------------------------------------------------/
/ Object : Open and start the GPRS bearer. Normally, the bearer will /
/ answer IN_PROGRESS, and the initialization will be finished /
/ by the callback evh_bearer(). /
/--------------------------------------------------------------------------/
/ Variable Name |IN |OUT|GLB| Utilisation /
/--------------------±–±--±–±----------------------------------------/
/--------------------±–±--±–±----------------------------------------/
/********/
static void open_and_start_bearer( void) {
int r;
wip_bearer_t b;
r = wip_bearerOpen( &b, “GPRS”, evh_bearer, NULL);
ASSERT_OK( r);
r = wip_bearerSetOpts( b, WIP_BOPT_GPRS_APN, GPRS_APN,
WIP_BOPT_LOGIN, GPRS_USER,
WIP_BOPT_PASSWORD, GPRS_PASSWORD,
WIP_BOPT_END);
ASSERT_OK( r);
r = wip_bearerStart( b);
ASSERT( 0 == r || WIP_BERR_OK_INPROGRESS == r);
}