Help with http post


#1

Hi
I am trying to do to a http post put i am not succeeding in doing this. I keep getting this error WIP_CERR_NOT_SUPPORTED
I get WIP_CEV_OPEN then WIP_CEV_WRITE events and then the error occurs
Here is my code

// HANDLER
void HTTP_hdl_c(wip_event_t *event, void *context)
{
     s32 ret;
	 s32 code;
    TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c()"));

   
    switch(event->kind)
    {
        //response header message received
        case WIP_CEV_OPEN:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_OPEN"));
        break;
       
        //response body message received
        case WIP_CEV_READ: 
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_READ"));
            //check http_status

            wip_getOpts(http_channel_pers, WIP_COPT_HTTP_STATUS_CODE, &code, WIP_COPT_END);
            TRACE((TRACE_LEVEL_HTTP, "return code is %d", code));
           
            if (code == 200) //OK
            {
                //... 
            }
        break;
       
        case WIP_CEV_PEER_CLOSE:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_PEER_CLOSE"));
            wip_close(http_channel_pers);
        break;
       
        case WIP_CEV_ERROR:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_ERROR"));
            wip_close(http_channel_pers);
        break;
       
        //write request body
        case WIP_CEV_WRITE:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_WRITE"));

            ret = wip_write(http_channel_pers, "some=request", 13);
            TRACE((TRACE_LEVEL_HTTP, "wip_write ret: %d", ret));

            wip_shutdown(http_channel_pers,FALSE,TRUE);
        break;
    }
}


void HTTP_SendRequest()
{
	ascii* req;
    http_channel_pers = wip_HTTPClientCreateOpts(NULL, NULL, WIP_COPT_HTTP_HEADER, "User-Agent", "WIP-HTTP-Client/1.0", WIP_COPT_END);
    if (http_channel_pers == NULL)
    {
        return;
    }
   
    req = "http://xxx/xxx/test.php";

    // do post request
    wip_getFileOpts(http_channel_pers, req, HTTP_hdl_c, NULL,
         WIP_COPT_HTTP_METHOD, WIP_HTTP_METHOD_POST,
         WIP_COPT_HTTP_HEADER, "Accept", "*",
         WIP_COPT_END);
   
}

static void gprsBearerHandler(wip_bearer_t br,s8 event,void *context){
    switch (event) {
	    case WIP_BEV_IP_CONNECTED:
			HTTP_SendRequest();
			break;
        case WIP_BEV_IP_DISCONNECTED:
            break;    
		default:        
		    break;
    }
}

//*********************************************************************************************************
// This functions will create the bearer for the GPRS
//*********************************************************************************************************
/** This functions will create the bearer for the GPRS.
@return TRUE if successfull.
@return FALSE if failure.
*/
bool createBearer( ){
    wip_bearer_t gprsBearer;
    s8 resp = 0;
    if (wip_bearerOpen(&gprsBearer,"GPRS", gprsBearerHandler, NULL) != 0 ){
        // cannot open the bearer
        return FALSE;
    }
    //configure the gprs connection
    if (wip_bearerSetOpts(gprsBearer,WIP_BOPT_GPRS_APN, Configuration.apnServer, WIP_BOPT_LOGIN,
            Configuration.gprsUser, WIP_BOPT_PASSWORD, Configuration.gprsPassword, WIP_BOPT_END) !=0){
    //cannot configure the bearer
        wip_bearerClose(gprsBearer);
         return FALSE;
    }
    // start connection
    wip_debug( "try to start bearer\n");
    resp = wip_bearerStart(gprsBearer);
    switch (resp) {
        case WIP_BERR_OK_INPROGRESS:
            wip_debug( "Bearer starting\n");
            break;
        case WIP_BERR_BAD_HDL:
            wip_debug( "Invalid bearer handle\n");
            break;
        case WIP_BERR_BAD_STATE:
            wip_debug( "WIP_BERR_BAD_STATE\n");
            break;
        case WIP_BERR_DEV:
            wip_debug( "The bearer is not stoped\n");
            break;
        default:
            wip_debug( "Unknown error when starting bearer\n");
            break;
    }
    if (resp !=WIP_BERR_OK_INPROGRESS){
        wip_bearerClose(gprsBearer);
        return FALSE;
    }
    return TRUE;
}

/***************************************************************************/
// Function that handles all the events related to the SIM
/***************************************************************************/
/** Handles all the events related to the SIM.
@param Event The events that occurs related to the SIM.
@return nothing.
*/
void ftp_simHandler(u8 Event) { 
    bool rsp;
    switch ( Event ) { 
        case ADL_SIM_EVENT_PIN_OK : 
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_OK" ));
            break; 
        case ADL_SIM_EVENT_REMOVED : 
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_REMOVED" ));
            break; 
        case ADL_SIM_EVENT_INSERTED : 
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_INSERTED" ));
            break; 
        case ADL_SIM_EVENT_FULL_INIT : 
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_FULL_INIT" ));
		createBearer( );
            break; 
        case ADL_SIM_EVENT_PIN_ERROR : 
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_ERROR" ));
            break; 
        case ADL_SIM_EVENT_PIN_NO_ATTEMPT : 
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_NO_ATTEMPT" ));
            break; 
        case ADL_SIM_EVENT_PIN_WAIT : 
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_WAIT" ));
            break; 
        default : 
            TRACE (( TRACE_LEVEL_HTTP, "other PIN event" ));
            break; 
    } 

}

/***************************************************************************/
// initialize the SIM
/***************************************************************************/
void ftp_initSIM(void){
    adl_simState_e simState;
    ascii buf[50];
    simState = adl_simGetState();

    adl_atSendResponse(ADL_AT_RSP,"\n\rShould enter pin...\r\n");
    adl_simSubscribe((adl_simHdlr_f)ftp_simHandler, ftp_Configuration.pinCode);
    if (simState == ADL_SIM_STATE_PIN_WAIT){
       adl_atSendResponse(ADL_AT_RSP,"Entering pin");
      
    } else  {
        adl_atSendResponse(ADL_AT_RSP,"Sim state:");
        wm_sprintf(buf,"%d",simState);
        adl_atSendResponse(ADL_AT_RSP,buf);
   }
}

/***************************************************************************/
// initialize the IP stack needed to put data on ftp
/***************************************************************************/
/** Initializes the IP stack needed to put data on ftp.
@return nothing.
*/
void initIPStack(void){
    int r;
    /* Initialize the stack */
    r = wip_netInitOpts( 
        WIP_NET_OPT_DEBUG_PORT, WIP_NET_DEBUG_PORT_UART1,
        WIP_NET_OPT_END); 
    if (r !=0 ){
    }
}

void init(finit_config_t * initStruct){
    Configuration = *initStruct;
    initIPStack();
    initSIM(); 
}

What am i doing wrong?


#2
  • What function does return CERR_INVALID?
  • Have you got traces?

#3

Here is the trace
Trace IP 20 ftp_gprsBearerHandler event
Trace IP 20 2
Trace IP 20 HTTP_SendRequest()
Trace IP 20 HTTP_hdl_c()
Trace IP 20 HTTP_hdl_c(): WIP_CEV_OPEN
Trace IP 20 HTTP_hdl_c()
Trace IP 20 HTTP_hdl_c(): WIP_CEV_WRITE
Trace IP 20 wip_write ret: -998
Trace IP 20 HTTP_hdl_c()
Trace IP 20 HTTP_hdl_c(): WIP_CEV_ERROR


#4

Have you looked-up the meaning of that error code in the WIP Manual?


#5

in HTTP, wip_HTTPClientCreateOpts() creates a connection, not a request. So you must:

  • create the connection channel
  • create a request channel, by performing a wip_getFile() or wip_putFile() on the connection
  • perform read/write operations on the request.

#6

Ok i have managed to post some data put i still not am fully satisfied
Here is what i do

// HANDLER
void HTTP_hdl_c(wip_event_t *event, void *context)
{
     s32 ret;
    s32 code;
    TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c()"));

    switch(event->kind)
    {
        //response header message received
        case WIP_CEV_OPEN:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_OPEN"));
        break;
       
        //response body message received
        case WIP_CEV_READ:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_READ"));
            //check http_status

            wip_getOpts(http_channel_comm, WIP_COPT_HTTP_STATUS_CODE, &code, WIP_COPT_END);
            TRACE((TRACE_LEVEL_HTTP, "return code is %d", code));
           
            if (code == 200) //OK
            {
                // do here the channel closing 
            }
        break;
       
        case WIP_CEV_PEER_CLOSE:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_PEER_CLOSE"));
            wip_close(http_channel_comm);
        break;
       
        case WIP_CEV_ERROR:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_ERROR"));
            wip_close(http_channel_comm);
        break;
       
        //write request body
        case WIP_CEV_WRITE:
            TRACE((TRACE_LEVEL_HTTP, "HTTP_hdl_c(): WIP_CEV_WRITE"));

            ret = wip_write(http_channel_comm, "some=request", 12);
            TRACE((TRACE_LEVEL_HTTP, "wip_write ret: %d", ret));

            wip_shutdown(http_channel_comm,FALSE,TRUE);
        break;
    }
}


void HTTP_SendRequest()
{
   ascii* req;
    http_channel_pers = wip_HTTPClientCreateOpts(NULL, NULL, WIP_COPT_HTTP_HEADER, "User-Agent", "WIP-HTTP-Client/1.0", WIP_COPT_END);
    if (http_channel_pers == NULL)
    {
        return;
    }
   
    req = "http://xxx/xxx/test.php";

    // do post request
   [b]http_channel_comm [/b] = wip_getFileOpts(http_channel_pers, req, HTTP_hdl_c, NULL,
         WIP_COPT_HTTP_METHOD, WIP_HTTP_METHOD_POST,
         WIP_COPT_HTTP_HEADER, "Accept", "*",
         WIP_COPT_END);
   
}

static void gprsBearerHandler(wip_bearer_t br,s8 event,void *context){
    switch (event) {
       case WIP_BEV_IP_CONNECTED:
         HTTP_SendRequest();
         break;
        case WIP_BEV_IP_DISCONNECTED:
            break;   
      default:       
          break;
    }
}

//*********************************************************************************************************
// This functions will create the bearer for the GPRS
//*********************************************************************************************************
/** This functions will create the bearer for the GPRS.
@return TRUE if successfull.
@return FALSE if failure.
*/
bool createBearer( ){
    wip_bearer_t gprsBearer;
    s8 resp = 0;
    if (wip_bearerOpen(&gprsBearer,"GPRS", gprsBearerHandler, NULL) != 0 ){
        // cannot open the bearer
        return FALSE;
    }
    //configure the gprs connection
    if (wip_bearerSetOpts(gprsBearer,WIP_BOPT_GPRS_APN, Configuration.apnServer, WIP_BOPT_LOGIN,
            Configuration.gprsUser, WIP_BOPT_PASSWORD, Configuration.gprsPassword, WIP_BOPT_END) !=0){
    //cannot configure the bearer
        wip_bearerClose(gprsBearer);
         return FALSE;
    }
    // start connection
    wip_debug( "try to start bearer\n");
    resp = wip_bearerStart(gprsBearer);
    switch (resp) {
        case WIP_BERR_OK_INPROGRESS:
            wip_debug( "Bearer starting\n");
            break;
        case WIP_BERR_BAD_HDL:
            wip_debug( "Invalid bearer handle\n");
            break;
        case WIP_BERR_BAD_STATE:
            wip_debug( "WIP_BERR_BAD_STATE\n");
            break;
        case WIP_BERR_DEV:
            wip_debug( "The bearer is not stoped\n");
            break;
        default:
            wip_debug( "Unknown error when starting bearer\n");
            break;
    }
    if (resp !=WIP_BERR_OK_INPROGRESS){
        wip_bearerClose(gprsBearer);
        return FALSE;
    }
    return TRUE;
}

/***************************************************************************/
// Function that handles all the events related to the SIM
/***************************************************************************/
/** Handles all the events related to the SIM.
@param Event The events that occurs related to the SIM.
@return nothing.
*/
void ftp_simHandler(u8 Event) {
    bool rsp;
    switch ( Event ) {
        case ADL_SIM_EVENT_PIN_OK :
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_OK" ));
            break;
        case ADL_SIM_EVENT_REMOVED :
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_REMOVED" ));
            break;
        case ADL_SIM_EVENT_INSERTED :
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_INSERTED" ));
            break;
        case ADL_SIM_EVENT_FULL_INIT :
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_FULL_INIT" ));
      createBearer( );
            break;
        case ADL_SIM_EVENT_PIN_ERROR :
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_ERROR" ));
            break;
        case ADL_SIM_EVENT_PIN_NO_ATTEMPT :
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_NO_ATTEMPT" ));
            break;
        case ADL_SIM_EVENT_PIN_WAIT :
            TRACE (( TRACE_LEVEL_HTTP, "ADL_SIM_EVENT_PIN_WAIT" ));
            break;
        default :
            TRACE (( TRACE_LEVEL_HTTP, "other PIN event" ));
            break;
    }

}

/***************************************************************************/
// initialize the SIM
/***************************************************************************/
void ftp_initSIM(void){
    adl_simState_e simState;
    ascii buf[50];
    simState = adl_simGetState();

    adl_atSendResponse(ADL_AT_RSP,"\n\rShould enter pin...\r\n");
    adl_simSubscribe((adl_simHdlr_f)ftp_simHandler, ftp_Configuration.pinCode);
    if (simState == ADL_SIM_STATE_PIN_WAIT){
       adl_atSendResponse(ADL_AT_RSP,"Entering pin");
     
    } else  {
        adl_atSendResponse(ADL_AT_RSP,"Sim state:");
        wm_sprintf(buf,"%d",simState);
        adl_atSendResponse(ADL_AT_RSP,buf);
   }
}

/***************************************************************************/
// initialize the IP stack needed to put data on ftp
/***************************************************************************/
/** Initializes the IP stack needed to put data on ftp.
@return nothing.
*/
void initIPStack(void){
    int r;
    /* Initialize the stack */
    r = wip_netInitOpts(
        WIP_NET_OPT_DEBUG_PORT, WIP_NET_DEBUG_PORT_UART1,
        WIP_NET_OPT_END);
    if (r !=0 ){
    }
}

void init(finit_config_t * initStruct){
    Configuration = *initStruct;
    initIPStack();
    initSIM();
}

So what i was doing wrong is that i was trying to use the connection channel instead of the request channel when i was doing the actual request.
So thanks guys for the help.
I still have some problems when closing the connection. After i do wip_write i do shutdown and i get the WIP_CEV_READ event and the return code 200 (at this point the data appears on the site) This is the trace

Trace IP 20 HTTP_SendRequest()
Trace IP 20 HTTP_hdl_c()
Trace IP 20 HTTP_hdl_c(): WIP_CEV_OPEN
Trace IP 20 HTTP_hdl_c()
Trace IP 20 HTTP_hdl_c(): WIP_CEV_WRITE
Trace IP 20 wip_write ret: 25
Trace IP 20 wip_shutdown ret: 0
Trace IP 20 HTTP_hdl_c()
Trace IP 20 HTTP_hdl_c(): WIP_CEV_READ
Trace IP 20 return code is 200

At this point i would like to close all the connections if i do wip_shutdown(http_channel_comm,FALSE,TRUE); again i get wip_cerr_cstate -999 (the channel is not in WIP_CSTATE_READY state) if i do wip_close(http_channel_pers) i get the return code 0 (OK) but after that i get WIP_CEV_ERROR event
So my question is how do i close all the channels right after completing the http post? (from what i figured i need to wait for code 200 after post so i can see the data on the site)


#7

I need to close all the connections because i do the posting in a loop so if i keep looping i figure that the channel created wip_HTTPClientCreateOpts is left just hanging around and with each loop i do a new one until i get WIP_CEV_ERROR a and Can’t create socket: error -39


#8

Ok i figured it out. You call wip_shutdown when you sent all the data, this will cause to wip_cev_read event if the code is 200 then you can close everything like this

ret=wip_close(http_channel_comm);
TRACE((TRACE_LEVEL_FTP, “wip_close ret: %d”, ret));
ret=wip_close(http_channel_pers);
TRACE((TRACE_LEVEL_FTP, “wip_close ret: %d”, ret));


#9

wip_close() will do the shutdown for you if you didn’t already request it.