GPRS code failing sometimes

Hello again.

I have one little thing that is puzzling me... I'm still programming my GPRS modem, and it already works, but one more thing is missing... It sometimes opens, set options and start the bearer, and sometimes does not... I'll paste some of the debug and the code...
DEBUG: (not starting correctly)

+WIND: 13         
+T2RST      

No APN defined. Using "airtelnet.es" (Login: VODAFONE, Password: VODAFONE) for this session.            

forcedIP: 0           
MyIP: 0.0.0.0

SIM DETECTED. Starting TCP services.

+WIND: 1

PIN OK

+WIND: 16

ADL_SIM_EVENT_FULL_INIT detected.
+T2RSIM
wip_netInit ( ) returns: -24
+WIND: 4

+WIND: 10,"SM",0,"FD",0,"ON",0,"SN",1,"EN",0

+WIND: 11,,,,"966B57D8B06881FC361DBF5E8B842DE1","9E281DB9C906A7B9D8F48E29189F610
2",

+WIND: 7

+WIND: 15,1,"vodafone ES",2,"vodafone E",3,"+08",4,"07/09/04,07:53:09+08"



DEBUG: (starting correctly)

+WIND: 13         
+T2RST      

No APN defined. Using "airtelnet.es" (Login: VODAFONE, Password: VODAFONE) for t                                                                                
his session.            

forcedIP: 0           
MyIP: 0.0.0.0             

SIM DETECTED. Starting TCP services.                                    

+WIND: 1        

PIN OK      

+WIND: 16         

ADL_SIM_EVENT_FULL_INIT detected.
+T2RSIM
wip_netInit ( ) returns: -24
+WIND: 4

+WIND: 10,"SM",0,"FD",0,"ON",0,"SN",1,"EN",0

+WIND: 11,,,,"966B57D8B06881FC361DBF5E8B842DE1","D3187F6166F65892919646E969784EB
D",

+WIND: 7

Inside T2R_InitGPRSbearer. wip_bearerOpen returns: 0

Inside T2R_InitGPRSbearer. wip_bearerSetOpts returns: 0

Inside T2R_InitGPRSbearer. wip_bearerStart returns: -27
+T2RBE

+WIND: 15,1,"vodafone ES",2,"vodafone E",3,"+08",4,"07/09/04,07:56:03+08"
+T2RIP: 212.166.171.52
+TR2TCP

And here is some of my code:

void adl_main (...){
...
adl_simSubscribe ( Hdlr_SubscripcionSim, MyPIN );
...
}

static void Hdlr_SubscripcionSim ( u8 event ) {
...
case ADL_SIM_EVENT_FULL_INIT:
			TRACE ( ( 1, "Hdlr_SubscripcionSim: ADL_SIM_EVENT_FULL_INIT" ) );
			adl_atCmdCreate ( "AT+CGATT=1", TRUE, (adl_atRspHandler_t) Hdlr_GPRSAttach, "*", NULL );
			TRACE ( ( 1, "Hdlr_SubscripcionSim: Send AT+CGATT=1 command" ) );
			//Debug
			wm_sprintf ( rspStr, "\r\nADL_SIM_EVENT_FULL_INIT detected.\r\n");
			adl_atSendResponse ( ADL_AT_RSP, rspStr );
			//
			adl_atSendResponsePort ( ADL_AT_UNS, ADL_PORT_UART1, "+T2RSIM" );
			debug = wip_netInit ( );
			wm_sprintf ( rspStr, "\r\nwip_netInit ( ) devuelve: %d", debug );
			adl_atSendResponse ( ADL_AT_RSP, rspStr );
...
}

bool T2R_InitGPRSBearer ( ) {
...
s8GPRSreturn = wip_bearerOpen ( &t2r_Bearer, "GPRS", Hdlr_GPRS_Bearer,NULL );
...
if ( s8GPRSreturn == 0 || forcedIP == 0 ) { //Everything OK

		TRACE ( ( 1, "T2R_InitGPRSBearer: Bearer Open correctly" ) );
		s8GPRSreturn = wip_bearerSetOpts ( t2r_Bearer, WIP_BOPT_GPRS_APN, MyAPN,
                         WIP_BOPT_LOGIN, MyLogin, WIP_BOPT_PASSWORD, MyPassword, 
						 WIP_BOPT_END );
		//Debugging
		wm_sprintf ( rspStr, "\r\nInside T2R_InitGPRSbearer. wip_bearerSetOpts returns: %d\r\n", s8GPRSreturn );
		adl_atSendResponse ( ADL_AT_RSP, rspStr );
		//

		s8GPRSreturn = wip_bearerStart ( t2r_Bearer );
		//Debugging
		wm_sprintf ( rspStr, "\r\nInside T2R_InitGPRSbearer. wip_bearerStart returns: %d\r\n", s8GPRSreturn );
		adl_atSendResponse ( ADL_AT_RSP, rspStr );
}

Well, I think that’s too much code, but I’ve been dancing with this for a few days, and it’s something I can’t solve. Any idea will be welcomed :slight_smile:. Thanks in advance.

That’s an error code, isn’t it?

Have you looked it up?

Yes, and it doesn’t make sense. The error code means WIP_BEV_BAD_HDL, invalid handle. It’s always the same, even if the connection is done, but I don’t know how to fix that…

My wip options are like this, and the handle that is said to be “bad” I suposse its “t2rbearer”:

s8GPRSreturn = wip_bearerSetOpts ( t2r_Bearer, WIP_BOPT_GPRS_APN, MyAPN, WIP_BOPT_LOGIN, MyLogin,                                     WIP_BOPT_PASSWORD, MyPassword, WIP_BOPT_END );

s8GPRSreturn returns a ‘0’… when it starts correctly… when doesn’t start correctly, this line is not executed, that’s why I’m so lost…

What happens if you instead of running AT+CGATT=1 have AT+WGPRS=0,0 for auto-attach? (Needs to be set once, with restart after setting, possibly with &W to make sure it remembers the setting)

Other than that, I can’t recommend running CGATT at the place in the code you are running it.
wip_netInit() can be run at the very start (at least if run without PIN-code, haven’t tried without), and you should wait for the answer from CGATT before proceeding in the code.

I have changed the at+cgatt for at+wgprs, but now I have an infinite loop, as something is always generating the WIP_BEV_STOPPED event, so it keeps trying to start once and again… :S

Is it wise to be using both the WIP API and AT Commands?

If you’re going to use the WIP API, I’d have thought it would be better to do everything via the API - otherwise you might confuse it?

What I meant was that during startup you do a
AT+WGPRS?
to check if you need to do a
AT+WGPRS=0,0
at all.

In my application I have that check at startup, and run the setting if needed followed by a reset, but if not needed, I simply move on in the code.

I’m not sure if you need to use this or the CGATT command at all with WIP though. You might want to try without as well.

I have already tried and stays the same… The only good thing is that I no longer have the infinite loop (actually, it was not infinite… somewhere between 30 minutes and 1 hour the loop stopped and got connected…), as I have undone the WGPRS change, which was what was producing the loop.

Have you tried to do the wip_netInit either as first thing you run in the application or delayed so it’s run later than it is now?

Yes, I have tried moving wip_netInit throughout my code, and the thing is still the same. I have tried one more thing. I have started a new proyect, just to start a TCP connection, but I have the same problem… here is the new code, it’s a little bit long, but i’ll try to delete all of my debug lines:

#define GPRS_APN      "airtelnet.es"
#define GPRS_LOGIN    "VODAFONE"
#define GPRS_PASSWORD "VODAFONE"
#define GPRS_PINCODE   ""

#include "adl_global.h"
#include "wip.h"

/***************************************************************************/
/*  Mandatory variables                                                    */
/***************************************************************************/
u32 wm_apmCustomStack [ 256 ];
const u16 wm_apmCustomStackSize = sizeof ( wm_apmCustomStack );

/***************************************************************************/
/*  Local variables                                                        */
/***************************************************************************/

wip_bearer_t MyBearer;
wip_in_addr_t addr;
static ascii TCPport [ 6 ];
static ascii Data [ 6 ];
wip_channel_t ServerChannel;
wip_channel_t SpawnServerChannel;
wip_in_addr_t wipAddress;
ascii dotAddress[20];

/***************************************************************************/
/*  Local functions                                                        */
/***************************************************************************/

void TCP_Handler (wip_event_t *Event, void *ctx)
{
u32 ReadData;
u32 WriteData;
ascii *DataBuffer;

SpawnServerChannel = Event->channel;
switch(Event->kind)
    {
    case WIP_CEV_READ:
                       
        DataBuffer = adl_memGet((Event->content.read.readable+1)+100);
        wm_memset(DataBuffer,0x00,Event->content.read.readable);
      
        while ((ReadData = wip_read (SpawnServerChannel, DataBuffer, Event->content.read.readable)) > 0) 
                  adl_atSendResponsePort(ADL_AT_UNS, ADL_PORT_UART1, DataBuffer); 

		adl_memRelease(DataBuffer);
        break;
     case WIP_CEV_OPEN:
        WriteData = wip_write(SpawnServerChannel,"Data\n",5);
        break;
    } 
}

void SendData()
{
	wip_write(SpawnServerChannel,Data,wm_strlen(Data));
}

void OpenSocket ()
{
	u16 Port;

	Port = wm_atoi (TCPport);
	wip_TCPServerCreate(Port, TCP_Handler, NULL);
}

void CloseSocket ()
{
	wip_close(SpawnServerChannel);
}

void Cmd_Port_Handler(adl_atCmdPreParser_t *paras)
{
    switch(paras->Type)
    {
		case ADL_CMD_TYPE_PARA:
		    OpenSocket();
            break;
		default: 
		    adl_atSendResponse(ADL_AT_PORT_TYPE ( paras->Port, ADL_AT_RSP ), "\r\nError\r\n");
            break;    
    }
    adl_atSendResponse(ADL_AT_PORT_TYPE ( paras->Port, ADL_AT_RSP ), "\r\nOK\r\n");
}

void Cmd_Close_Handler(adl_atCmdPreParser_t *paras)
{
    switch(paras->Type)
    {
        case ADL_CMD_TYPE_ACT:
            CloseSocket();
        break;
    default:
		 adl_atSendResponse(ADL_AT_PORT_TYPE ( paras->Port, ADL_AT_RSP ), "\r\nError\r\n");
         break;
    }
    adl_atSendResponse(ADL_AT_PORT_TYPE ( paras->Port, ADL_AT_RSP ), "\r\nOK\r\n");
}

void Cmd_Send_Handler(adl_atCmdPreParser_t *paras)
{
    switch(paras->Type)
    {
        case ADL_CMD_TYPE_PARA:
            SendData();
            break;
       default: 
           adl_atSendResponse(ADL_AT_PORT_TYPE ( paras->Port, ADL_AT_RSP ), "\r\nError\r\n");
       break;
    }
    adl_atSendResponse(ADL_AT_PORT_TYPE ( paras->Port, ADL_AT_RSP ), "\r\nOK\r\n");
}
static void handler_portadoraGPRS( wip_bearer_t MyBearer, s8 event, void *ctx)
{
	if( event == WIP_BEV_IP_CONNECTED) {
	    wip_bearerGetOpts (MyBearer,WIP_BOPT_IP_ADDR,&wipAddress, WIP_BOPT_END);
		wip_inet_ntoa( wipAddress, dotAddress,20);
	}
}

void inicialitzacioGPRS()
{
    int retornGPRS;

	retornGPRS = wip_bearerOpen( &MyBearer, "GPRS", handler_portadoraGPRS, NULL);
	if (retornGPRS == 0)
    {
		retornGPRS = wip_bearerSetOpts( MyBearer, WIP_BOPT_GPRS_APN, GPRS_APN,
                         WIP_BOPT_LOGIN,       GPRS_LOGIN,
                         WIP_BOPT_PASSWORD,    GPRS_PASSWORD,
                         WIP_BOPT_END);
	    if (retornGPRS == 0)
        	retornGPRS = wip_bearerStart( MyBearer);
	}  
}

s16 gprsAttachHandler(adl_atResponse_t *parametres)

{
	if (parametres->RspID == ADL_STR_OK) {
		inicialitzacioGPRS();
	}
}

static void suscripciosim( u8 event) 
{
	if( event == ADL_SIM_EVENT_FULL_INIT ) 
	    adl_atCmdCreate("AT+CGATT=1",TRUE,(adl_atRspHandler_t)gprsAttachHandler,"*", NULL);
}

void adl_main ( adl_InitType_e InitType )
{
    int retornGPRS;

    retornGPRS=wip_netInit();
    adl_simSubscribe( suscripciosim, GPRS_PINCODE);
	
	adl_atCmdSubscribe ( "at+port",
                         Cmd_Port_Handler,
                         ADL_CMD_TYPE_TEST | ADL_CMD_TYPE_READ | ADL_CMD_TYPE_PARA | 0x0011 );
    adl_atCmdSubscribe ( "at+close",
                         Cmd_Close_Handler,
                         ADL_CMD_TYPE_ACT | ADL_CMD_TYPE_READ | ADL_CMD_TYPE_PARA | 0x0011 );
    adl_atCmdSubscribe ( "at+send",
                         Cmd_Send_Handler,
                         ADL_CMD_TYPE_TEST | ADL_CMD_TYPE_READ | ADL_CMD_TYPE_PARA | 0x0011 );
}

Hello,

First of all. Please tell us the version of the OS, ADL libary and the WIP libary.
As far as i know in the current libary the wip_netInit ( ) function shouldn’t return -24 value.

I also recommend to use only WIP API-s for establishing the GPRS connection. Actually, the WIP uses these AT commands, so you shouldn’t have to do that yourself.

tom

These are the versions I’m using:

OS : 3.13.02
ADL : 3.12, though it’s supossed to be 3.13, but I don’t know why I get 3.12 when I use at+wopen=2…
WIP : 3.00.07

And about the use of WIP API-s alone I’m afraid that choice is not in my hands… Maybe if you could explain that a little more… Thank you for all the time you are spending on this :slight_smile:

The latest libs I have (from Open AT SDK 3.15) have the following version numbers.

adl 3.13.1.1.0
wip 3.0.0.2.0

Not sure if there is any later than that in the 3.1x series.

I don’t have this installed so I can’t compare with the numbers you supplied.