Start and stop gprs bearer connection


#1

Hi,
Currently I’m using TCP sample program(present in open sdk) to upload the data through GPRS connection ,
In that code It connects directly to the GPRS and open bearer connection , and it does not close the radio link .

I want to open whenever I need and close when the data upload is done… How to implement this …?

regards
Ravee


#2

I presume this refers to your other thread: https://forum.sierrawireless.com/t/q2686-tcp-sample-code/4922/6

The WIP User Guide gives a State Diagram depicting Bearer management - you need to implement that!

Similarly, there is a State Diagram depicting TCP Channel management …


#3

Hi,
The restarting problem has been solved …

But Still I’m facing problem at the GPRS upload …

The following is the code I have written for GPRS start and stop

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

/***************************************************************************/
/*  Defines                                                                */
/***************************************************************************/
//#define GPRS_APN      "AirTelGprs.com"
#define GPRS_APN      "portalnmms" //vodafone
#define GPRS_USER     "a2b"
#define GPRS_PASSWORD "access"
#define GPRS_PINCODE  "0000"

#define CREG_POLLING_PERIOD 20 /* in 100ms steps */

#define ASSERT( pred) \
if( !(pred)) wip_debug( "ASSERTION FAILURE line %i: " #pred "\n", __LINE__)
#define ASSERT_OK( v) ASSERT( 0 == (v))


/***************************************************************************/
/*  Functions                                                              */
/***************************************************************************/

/* Function to be called once the bearer is up and running. */
static void (* appli_entry_point)( void);
//void FTP_TimerHandler18MS ( u8 ID );
char* set_gprs(void);
void Flash_Read(void);///
adl_atPort_e uart2_port;

wip_bearer_t b; // Gprs
/***************************************************************************/
/*  Initialization-related event handlers                                  */
/***************************************************************************/


/***************************************************************************/
/*  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) {
	switch(event)
	{
	case WIP_BEV_CONN_FAILED:
		 send_sms("\r\nGPRS connection failure\r\n");
		 adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::GPRS connection failure\r\n" ));
		break;
	case WIP_BEV_IP_DISCONNECTED:
            TRACE((1,"IP communication terminated"));
            adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::IP communication terminated\r\n" ));
            send_sms("\r\nIP communication terminated restarting the device\r\n");
            adl_atCmdCreate("AT+CFUN=1",(int) NULL,NULL, NULL);
            break;
	case WIP_BEV_IP_CONNECTED:
	  adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::GPRS STARTED ON IP.\r\n" ));
	 // send_sms("GPRS  Started.. Now Ready For TCP Connection");
	 // adl_tmrSubscribe ( FALSE, 10, ADL_TMR_TYPE_100MS, FTP_TimerHandler18MS);
	  //adl_atCmdCreate("AT+CMGD=1,4",(int) NULL,NULL, NULL);
	  appli_entry_point() ;
	  break;
    case WIP_BEV_STOPPED:
       TRACE((1,"disconnection completed"));
       break;

	}
}

/****************************************************************************/
/*  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) {
	adl_atSendResponse ( ADL_AT_UNS, "\r\n open and start bearer\r\n" );
	 adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::open and start bearer::\r\n" ));
  int r;


  r = wip_bearerOpen( &b, "GPRS", evh_bearer, NULL);
  ASSERT_OK( r);

  r = wip_bearerSetOpts( b, WIP_BOPT_GPRS_APN, set_gprs(),
                         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);
}

static void poll_creg( u8 Id );

/****************************************************************************/
/*  Function   : poll_creg_callback()                                       */
/*--------------------------------------------------------------------------*/
/*  Object : A call to "AT+CREG?" has been done, to check the registration  */
/*           status, and the answer comes back to this handler.             */
/*           Either the registration is completed, and we can actually      */
/*           open and start the bearer, or it isn't, and we shall poll      */
/*           at "AT+CREG?" command again through a timer.                   */
/*--------------------------------------------------------------------------*/
/*  Variable Name     |IN |OUT|GLB|  Utilisation                            */
/*--------------------+---+---+---+-----------------------------------------*/
/*  Rsp               | x |   |   |  AT command response                    */
/*--------------------+---+---+---+-----------------------------------------*/
/****************************************************************************/
static bool poll_creg_callback(adl_atResponse_t *Rsp) {
	   ascii *rsp;
	    ascii regStateString[3];
	    s32 regStateInt;

	    TRACE (( 1, "(poll_creg_callback) Enter." ));

	    adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::(poll_creg_callback) Enter.\r\n" ));

	    rsp = (ascii *)adl_memGet(Rsp->StrLength);
	    wm_strRemoveCRLF(rsp, Rsp->StrData, Rsp->StrLength);

	    wm_strGetParameterString(regStateString, Rsp->StrData, 2);
	    regStateInt = wm_atoi(regStateString);

	    if ( 1 == regStateInt || 5 ==regStateInt) {
	        TRACE (( 1, "(poll_creg_callback) Registered on GPRS network." ));
	        adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::(poll_creg_callback) Registered on GPRS network.\r\n\r\n" ));
	        //open_and_start_bearer();
	    } else {
	      /* Not ready yet, we'll check again later. Set a one-off timer. */

	    	 adl_atCmdCreate("AT+CFUN=1",(int) NULL,NULL, NULL);// restarting the device
	    	adl_tmrSubscribe( FALSE, CREG_POLLING_PERIOD, ADL_TMR_TYPE_100MS,
	                        poll_creg);
	    }
	    return FALSE;
}

/****************************************************************************/
/*  Function   : poll_creg                                                  */
/*--------------------------------------------------------------------------*/
/*  Object : Monitor the network registration; the only way to do that is   */
/*           through an AT command, so we send that "AT+CREG?" command.     */
/*           Actual reaction will be performed by the callback              */
/*           poll_creg_callback().                                          */
/*--------------------------------------------------------------------------*/
/*  Variable Name     |IN |OUT|GLB|  Utilisation                            */
/*--------------------+---+---+---+-----------------------------------------*/
/*  Id                |   |   |   | Dummy parameter that makes the function */
/*                    |   |   |   | callable by a timer's adl_tmrSubscribe()*/
/*--------------------+---+---+---+-----------------------------------------*/
/****************************************************************************/
static void poll_creg( u8 Id ) {
  adl_atCmdCreate( "AT+CREG?", FALSE, poll_creg_callback, ADL_STR_CREG, NULL);
}

/***************************************************************************/
/*  Function   : evh_sim                                                   */
/*-------------------------------------------------------------------------*/
/*  Object     : sim events:                                               */
/*               when SIM initialisation is completed, check the registra- */
/*               tion status; poll_creg()'s callback will actually         */
/*               open the bearer once registration is completed.           */
/*-------------------------------------------------------------------------*/
/*  Variable Name     |IN |OUT|GLB|  Utilisation                           */
/*--------------------+---+---+---+----------------------------------------*/
/*  event             | X |   |   |SIM event                               */
/*--------------------+---+---+---+----------------------------------------*/
/***************************************************************************/
static void evh_sim( u8 event) {
  TRACE (( 1, "(evh_sim) Enter." ));
  //adl_atSendResponse ( ADL_AT_UNS, "\r\n(evh_sim) Enter..\r\n" );
  adl_atSendResponsePort(ADL_AT_RSP, uart2_port,( "\r\n(evh_sim) Enter..\r\n" ));
  if( ADL_SIM_EVENT_FULL_INIT == event) {
	  //Flash_Read();
	 // callsimsubsribe();//to subsribe SIM for SMS automatiom
    poll_creg( 0); /* argument 0 is dummy, see poll_reg() "Object" comment */
  }
}


/***************************************************************************/
/*  Function   : cfg_gprs                                                  */
/*-------------------------------------------------------------------------*/
/*  Object     : initialize GPRS connection, then launch entry_point() on  */
/*               success.                                                  */
/*                                                                         */
/*-------------------------------------------------------------------------*/
/*  Variable Name     |IN |OUT|GLB|  Utilisation                           */
/*--------------------+---+---+---+----------------------------------------*/
/*  entry_point       | X |   |   |Function run after successful connection*/
/*--------------------+---+---+---+----------------------------------------*/
/***************************************************************************/
void cfg_gprs ( void (* entry_point)(void)) {
  TRACE (( 2, "(cfg_gprs) Enter." ));
  adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::(cfg_gprs) Enter.::\r\n" ));
  adl_simSubscribe( evh_sim, GPRS_PINCODE);
  appli_entry_point = entry_point;
}
/* *********************************************
***********************************************
                 GPRS Stop
************************************************
************************************************/



  void close_and_stop_bearer( void)
  {


	  s8 close= wip_bearerClose (b);
	  
	  switch(clsoe)
	  {
      case WIP_BEV_STOPPED:
           print("The bearer is closed");             
      }
	 
 s8 stop = wip_bearerStop (b);


  switch(stop)
	  {
      case WIP_BEV_STOPPED:
           print(" The bearer is disconnected");             
      }

 }

void start_stop_gprs(int action)
{
	if (action==1)
	{
		 open_and_start_bearer();
	}
	else
	{
		adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::In close GPRS.::\r\n" ));
		close_and_stop_bearer();
	}

}

When ever I want start the GPRS I call start_stop_gprs(1) and with para ‘0’ for stop …

Just to check I send the data from external device at every 2 min once , It uploads some 6 times then it fails at the Client connecting to server( in TCP action while establishing the connection with server) and stops there and throws the error in connecting …What may be the problem …? I’m I refreshing the GPRS properly …?
pls help me
regards
Ravee


#4

How, exactly, does it fail?

You need to answer the above question first:

Once you know how, exactly, it fails then you can think about what might cause that particular failure;

Once you know what the failure mechanism is, then you think about how to fix that!

This is a standard debugging process - nothing special or specific for Open-AT or GPRS.

For more debugging tips, see: 8052.com/faqs/120313

and: techonlineindia.com/article/ … anner.aspx


#5

Thanks, i’ll go through the debugging tips … as per now every time I try to open a new TCP connection (by calling the appli_entry_point() in the below code ) . and closing the TCP channel then closing the GPRS before uploading the nex data

void appli_entry_point() {
  wip_channel_t socket;
   print("connecting to client\n\n");
  adl_atSendResponsePort(ADL_AT_RSP, uart1_port,("[SAMPLE]: connecting to client %s:%i...\n"));
print("[SAMPLE]: connecting to client %s:%i...\n");
  wip_debug( "[SAMPLE]: connecting to client %s:%i...\n", PEER_STRADDR, PEER_PORT);
  socket = wip_TCPClientCreate( PEER_STRADDR, PEER_PORT, evh, NULL);
  if( ! socket) { wip_debug( "[SAMPLE] Can't connect\n"); print("Cant connect"); return; }
}
  
 
/***************************************************************************/
/*  Function   : evh_data                                                  */
/*-------------------------------------------------------------------------*/
/*  Object     : Handling events happenning on the TCP client socket.      */
/*-------------------------------------------------------------------------*/
/*  Variable Name     |IN |OUT|GLB|  Utilisation                           */
/*--------------------+---+---+---+----------------------------------------*/
/*  ev                | X |   |   |  WIP event                             */
/*--------------------+---+---+---+----------------------------------------*/
/*  ctx               | X |   |   |  user data (unused)                    */
/*--------------------+---+---+---+----------------------------------------*/
/***************************************************************************/
static void evh( wip_event_t *ev, void *ctx) {

  switch( ev->kind) {

  case WIP_CEV_OPEN: {
    wip_debug ("[SAMPLE] Connection established successfully\n");
    adl_atSendResponsePort(ADL_AT_RSP, uart1_port,("[SAMPLE] Connection established successfully\n"));

    break;
  }
  case WIP_CEV_READ: {
    int nread;
    wip_debug ("[SAMPLE] Some data arrived\n");
    adl_atSendResponsePort(ADL_AT_RSP, uart1_port,("[SAMPLE] Some data arrived\n"));
    nread = wip_read( ev->channel, rcv_buffer + rcv_offset,
                      sizeof( rcv_buffer) - rcv_offset);
    if( nread < 0) { wip_debug( "[SAMPLE] read error %i\n", nread); return; }
    rcv_offset += nread;
    if( rcv_offset == sizeof( rcv_buffer)) {
      wip_debug( "[SAMPLE] Reception capacity exceeded, won't read more\n");
    } else {
      wip_debug( "[SAMPLE] Wrote %i bytes of data from network to rcv_buffer. "
                 "%i bytes remain available in rcv_buffer\n",
                 nread, sizeof( rcv_buffer) - rcv_offset);
    }
    break;
  }
  case WIP_CEV_DONE:
	  wip_debug (" Your Data Uploaded  successfully\n");
	    adl_atSendResponsePort(ADL_AT_RSP, uart1_port,(" Your Data Uploaded  successfully\n"));
	  break;
  case WIP_CEV_WRITE: {
    int nwrite;
    wip_debug ("[SAMPLE] Can send more data\n");
        adl_atSendResponsePort(ADL_AT_RSP, uart1_port,("[SAMPLE] Can send more data\n"));
    nwrite = wip_write( ev->channel, snd_buffer + snd_offset,
                        sizeof( snd_buffer) - snd_offset);
    if( nwrite < 0) { wip_debug( "[SAMPLE] write error %i\n", nwrite); return; }
    snd_offset += nwrite;
    if( snd_offset == sizeof( snd_buffer)) {

      wip_debug( "[SAMPLE] Everything has been sent, won't send more.\n");
      adl_atSendResponsePort(ADL_AT_RSP, uart1_port,("[SAMPLE] Everything has been sent, won't send more.\n"));
     print("[SAMPLE] Everything has been sent, won't send more.\n");
      
      snd_offset=0;

       wip_close( ev->channel);
call start_stop_gprs(0);// stoping the GPRS     	 
wm_memcpy(snd_bufferr,"",strlen(snd_bufferr)); // clearning the buffer

     } else {
      wip_debug( "[SAMPLE] Wrote %i bytes. "
                 "%i bytes left to send in snd_buffer\n",
                 nwrite, sizeof( snd_buffer) - snd_offset);
    }
    break;
  }
  case WIP_CEV_ERROR: {
    wip_debug( "[SAMPLE] Error %i on socket. Closing.\n",
               ev->content.error.errnum);
    adl_atSendResponsePort(ADL_AT_RSP, uart1_port,("[SAMPLE] Error %i on socket. Closing.\n"));
    wip_close( ev->channel);
   adl_tmrSubscribe ( FALSE, 100, ADL_TMR_TYPE_100MS, FTP_TimerHandler18MS);
    break;
  }
  case WIP_CEV_PEER_CLOSE: {
    wip_debug( "[SAMPLE] Connection closed by peer\n");
    adl_atSendResponsePort(ADL_AT_RSP, uart1_port,("[SAMPLE] Connection closed by peer\n"));
    wip_close( ev->channel);
    //adl_tmrSubscribe ( FALSE, 200, ADL_TMR_TYPE_100MS, FTP_TimerHandler18MS);
    break;
  }
  }
}

Before that I just want to clear that , my code which I have written for refreshing the GPRS connection is correct or not? Becoz once I stopped GPRS i’m not receiving WIP_BEV_STOPPED response … and If I restart the device it works fine for next 6 to 7 times
is there any sample code to do this …?

regards
Ravee


#6

Hi,

Can any one help out in writting a proper GPRS refreshing code (with reference my code)…?

reagrds
Ravee


#7

At a quick glance:
Your evh function doesn’t have a WIP_BEV_STOPPED case, that’s where you should set of a wip_bearerClose()
i.e. call wip_bearerStop() and when the evh has finished closing the WIP_BEV_STOPPED event is generated, then you can close.


#8

Hi,

Thanks for the reply … Now i’m able to close the bearer… and receiving the proper response from
if(0==wip_bearerclose(b)){ print(" bearer closed")}

But some times suddenly IP gets terminated after uploading the data , Do I need to restart my entire device again ,? becoz I called the open_and_start_bearer();, it couldnt get connected , i.e it didnt generated any event “evh_bearer” handler …

rgds
Ravee


#9

Again: What, exactly, do you mean by that?

You really need to get out of the habit of just saying “it doesn’t work” and, instead, think about what’s going on & how to fix it.
See: https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/8


#10

Hi,
thanks for nice reply …
Since I’m beginner to this I felt its better to ask this in the forum, becoz many people might have come across the same problem , and single problem may depends on many factor also …
I’m basically in to hardware design , due to some region I have to make this work , hence it requires more time for me to understand…

regards
Ravee


#11

"single problem may depends on many factor also … "
Exactly. So we don’t know what the problems is. So check your return codes to the functions, make sure you handle all events in the event handlers, and let us know what is happening.


#12

So think about how you’d debug a piece of hardware.

If someone came to you saying, “my board doesn’t work - what’s wrong with it?” would you think that’s a useful description of the problem?
Would you you be able to identify the fault from that information?


#13

Hi BenFT01 ,

I could not successfully complete the GPRS part , Here I have included my gprs code part
After debugging , I have noticed the following two problem .

  1. IP communication terminated(WIP_BEV_IP_DISCONNECTED) , while uploading the data suddenly IP will get terminated .

  2. Gprs connection failure (WIP_BEV_CONN_FAILED), When I call the gprs start bearer after few second I receive this status message .

Note : This happens irregularly(i.e data will upload some 20/30 times and in the next time this happens) .

I have come to know that these two problems are quite common in real time , and we need to try to re establish the connection after 5 to 10 sec when we have these problems .

So I went with trying this(I have including this code also i.e Re_establish()) , But when I try to re connect, status shows that WIP_BERR_ALREADY open,
Though i’m closing the connection before re connecting , it shows bearer is already opened . and it will be continuously trying this .

Pls help me to debug the “connection re establishing” part

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

/***************************************************************************/
/*  Defines                                                                */
/***************************************************************************/
//#define GPRS_APN      "AirTelGprs.com"
#define GPRS_APN      "airtelgprs.com"  
#define GPRS_USER     "tcs"
#define GPRS_PASSWORD "tcs"
#define GPRS_PINCODE  "0000"

#define CREG_POLLING_PERIOD 20 /* in 100ms steps */

#define ASSERT( pred) \
if( !(pred)) wip_debug( "ASSERTION FAILURE line %i: " #pred "\n", __LINE__)
#define ASSERT_OK( v) ASSERT( 0 == (v))


/***************************************************************************/
/*  Functions                                                              */
/***************************************************************************/

/* Function to be called once the bearer is up and running. */
static void (* appli_entry_point)( void);
 char* set_gprs(void);
 adl_atPort_e uart2_port;
wip_bearer_t b; // Gprs

/***************************************************************************/
/*  Initialization-related event handlers                                  */
/***************************************************************************/

/***************************************************************************/
/*  GPRS Re setablish                                   */
/***************************************************************************/



void Re_establish(void)
{
     
  start_stop_gprs(1); // try to connect again    
}

/***************************************************************************/
/*  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) {
	switch(event)
	{
	case WIP_BEV_CONN_FAILED:
		 send_sms("\r\nGPRS connection failure\r\n");
		 adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::GPRS connection failure\r\n" ));
		start_stop_gprs(0);/// stop the GPRS	
            //Setting timer to reestblish the GPRS connection 
            adl_tmrSubscribe( FALSE, 2000, ADL_TMR_TYPE_100MS,Re_establish);         break;
	case WIP_BEV_IP_DISCONNECTED:
            TRACE((1,"IP communication terminated"));
            adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::IP communication terminated\r\n" ));
          	start_stop_gprs(0);/// stop the GPRS	
            //Setting timer to reestblish the GPRS connection 
            adl_tmrSubscribe( FALSE, 2000, ADL_TMR_TYPE_100MS,Re_establish);          
            break;
	case WIP_BEV_IP_CONNECTED:
	  adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::GPRS STARTED ON IP.\r\n" ));
	  
	  appli_entry_point() ; //Calling the TCP part to send the data 
	  break;
    case WIP_BEV_STOPPED:
    	 adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::WIP_BEV_STOPPED.\r\n" ));
    	 s8 close= wip_bearerClose (b);

    	   switch(close)
    		  {
    		    case WIP_BERR_BAD_STATE:
    			  adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n:: Bearer was not clsoing before stopped:\r\n" ));
    			  break;
    			    case WIP_BERR_BAD_HDL:
    			  adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n:: Bad handle:\r\n" ));
    			  break;

    		}
    	 break;

	}
}

/****************************************************************************/
/*  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) {
	adl_atSendResponse ( ADL_AT_UNS, "\r\n open and start bearer\r\n" );
	 adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::open and start bearer::\r\n" ));
  int r;


  r = wip_bearerOpen( &b, "GPRS", evh_bearer, NULL);

  switch(r)
  {
           case  WIP_BERR_NO_DEV:
          	adl_atSendResponse ( ADL_AT_UNS, "\r\n The device does not exist\r\n" );
          	break;
          	case  WIP_BERR_ALREADY:
          	adl_atSendResponse ( ADL_AT_UNS, "\r\n The device is already opened\r\n" );          	break;
          	case  WIP_BERR_NO_IF:
          	adl_atSendResponse ( ADL_AT_UNS, "\r\n The network interface is not available\r\n" );
          	break;
          		case  WIP_BERR_NO_HDL:
          	adl_atSendResponse ( ADL_AT_UNS, "\r\n No free handle\r\n" );
          	break;

  }

  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);

  switch(r)
    {
             case  WIP_BERR_BAD_HDL:
            	adl_atSendResponse ( ADL_AT_UNS, "\r\n bad handle\r\n" );
            	break;
            	case  WIP_BERR_OPTION:
            	adl_atSendResponse ( ADL_AT_UNS, "\r\n The device is already opened\r\n" );
            	break;
            	case  WIP_BERR_PARAM:
            	adl_atSendResponse ( ADL_AT_UNS, "\r\n Invalid option value\r\n" );
            	break;

    }

  ASSERT_OK( r);
  r = wip_bearerStart( b);

  switch(r)
     {
              case  WIP_BERR_OK_INPROGRESS:
             	adl_atSendResponse ( ADL_AT_UNS, "\r\n WIP_BERR_OK_INPROGRESS\r\n" );
             	break;
             	case  WIP_BERR_BAD_HDL:
             	adl_atSendResponse ( ADL_AT_UNS, "\r\n  WIP_BERR_BAD_HDL\r\n" );
             	break;
             	case  WIP_BERR_BAD_STATE:
             	adl_atSendResponse ( ADL_AT_UNS, "\r\n WIP_BERR_BAD_STATE\r\n" );
             	break;
             	case  WIP_BERR_DEV:
             	             	adl_atSendResponse ( ADL_AT_UNS, "\r\n WIP_BERR_DEV\r\n" );
             	             	break;

     }

  ASSERT( 0 == r || WIP_BERR_OK_INPROGRESS == r);
}

static void poll_creg( u8 Id );

/****************************************************************************/
/*  Function   : poll_creg_callback()                                       */
/*--------------------------------------------------------------------------*/
/*  Object : A call to "AT+CREG?" has been done, to check the registration  */
/*           status, and the answer comes back to this handler.             */
/*           Either the registration is completed, and we can actually      */
/*           open and start the bearer, or it isn't, and we shall poll      */
/*           at "AT+CREG?" command again through a timer.                   */
/*--------------------------------------------------------------------------*/
/*  Variable Name     |IN |OUT|GLB|  Utilisation                            */
/*--------------------+---+---+---+-----------------------------------------*/
/*  Rsp               | x |   |   |  AT command response                    */
/*--------------------+---+---+---+-----------------------------------------*/
/****************************************************************************/
static bool poll_creg_callback(adl_atResponse_t *Rsp) {
	   ascii *rsp;
	    ascii regStateString[3];
	    s32 regStateInt;

	    TRACE (( 1, "(poll_creg_callback) Enter." ));

	    adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::(poll_creg_callback) Enter.\r\n" ));

	    rsp = (ascii *)adl_memGet(Rsp->StrLength);
	    wm_strRemoveCRLF(rsp, Rsp->StrData, Rsp->StrLength);

	    wm_strGetParameterString(regStateString, Rsp->StrData, 2);
	    regStateInt = wm_atoi(regStateString);

	    if ( 1 == regStateInt || 5 ==regStateInt) {
	        TRACE (( 1, "(poll_creg_callback) Registered on GPRS network." ));
	        adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::(poll_creg_callback) Registered on GPRS network.\r\n\r\n" ));
	    
	    } else {
	      /* Not ready yet, we'll check again later. Set a one-off timer. */
	    	 adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n:: in polling call back it will retsart the device\r\n\r\n" ));
	    	 
	    	adl_tmrSubscribe( FALSE, CREG_POLLING_PERIOD, ADL_TMR_TYPE_100MS,
	                        poll_creg);
	    }
	    return FALSE;
}

/****************************************************************************/
/*  Function   : poll_creg                                                  */
/*--------------------------------------------------------------------------*/
/*  Object : Monitor the network registration; the only way to do that is   */
/*           through an AT command, so we send that "AT+CREG?" command.     */
/*           Actual reaction will be performed by the callback              */
/*           poll_creg_callback().                                          */
/*--------------------------------------------------------------------------*/
/*  Variable Name     |IN |OUT|GLB|  Utilisation                            */
/*--------------------+---+---+---+-----------------------------------------*/
/*  Id                |   |   |   | Dummy parameter that makes the function */
/*                    |   |   |   | callable by a timer's adl_tmrSubscribe()*/
/*--------------------+---+---+---+-----------------------------------------*/
/****************************************************************************/
static void poll_creg( u8 Id ) {
  adl_atCmdCreate( "AT+CREG?", FALSE, poll_creg_callback, ADL_STR_CREG, NULL);
}

/***************************************************************************/
/*  Function   : evh_sim                                                   */
/*-------------------------------------------------------------------------*/
/*  Object     : sim events:                                               */
/*               when SIM initialisation is completed, check the registra- */
/*               tion status; poll_creg()'s callback will actually         */
/*               open the bearer once registration is completed.           */
/*-------------------------------------------------------------------------*/
/*  Variable Name     |IN |OUT|GLB|  Utilisation                           */
/*--------------------+---+---+---+----------------------------------------*/
/*  event             | X |   |   |SIM event                               */
/*--------------------+---+---+---+----------------------------------------*/
/***************************************************************************/
static void evh_sim( u8 event) {
  TRACE (( 1, "(evh_sim) Enter." ));
  //adl_atSendResponse ( ADL_AT_UNS, "\r\n(evh_sim) Enter..\r\n" );
  adl_atSendResponsePort(ADL_AT_RSP, uart2_port,( "\r\n(evh_sim) Enter..\r\n" ));
  if( ADL_SIM_EVENT_FULL_INIT == event) {
	   
    poll_creg( 0); /* argument 0 is dummy, see poll_reg() "Object" comment */
  }
}


/***************************************************************************/
/*  Function   : cfg_gprs                                                  */
/*-------------------------------------------------------------------------*/
/*  Object     : initialize GPRS connection, then launch entry_point() on  */
/*               success.                                                  */
/*                                                                         */
/*-------------------------------------------------------------------------*/
/*  Variable Name     |IN |OUT|GLB|  Utilisation                           */
/*--------------------+---+---+---+----------------------------------------*/
/*  entry_point       | X |   |   |Function run after successful connection*/
/*--------------------+---+---+---+----------------------------------------*/
/***************************************************************************/
void cfg_gprs ( void (* entry_point)(void)) {
  TRACE (( 2, "(cfg_gprs) Enter." ));
  adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::(cfg_gprs) Enter.::\r\n" ));
  adl_simSubscribe( evh_sim, GPRS_PINCODE);
  appli_entry_point = entry_point;
}
/* *********************************************
***********************************************
                 GPRS Stop
************************************************
************************************************/

 
/****************************************************
This function will be called once the data is aquired from external device 
  or when the data upload is completed / when the error in gprs connection  if set=1 : then it is for starting the bearer else stop
*/ 
void start_stop_gprs(int set)
{
	if (set==1)
	{
	
	adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n:: Calling to open the Gprs bearer . ::\r\n" ));
		 open_and_start_bearer(); // this function is called once we receive data from external device
	 
	else
	{
		adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n::Calling to close the Gprs bearer.::\r\n" ));
		

   s8 stop = wip_bearerStop (b);
	  switch(stop)
	  {
	  case WIP_BERR_OK_INPROGRESS:
		  adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n:: Disconnection in progress:\r\n" ));
		  break;
		    case WIP_BERR_BAD_HDL:
		  adl_atSendResponsePort(ADL_AT_RSP, uart2_port,("\r\n:: Bad handle:\r\n" ));
		  break;

	  }
	}

}

thanks & regards
chethan


#14

You could try putting the wip_bearerClose in the WIP_BEV_CONN_FAILED event and the wip_bearerStop in the WIP_BEV_IP_DISCONNECTED event.

I think I have seen this problem. If the lost of connection is due certain types of network failure, sometimes the only thing to do is reboot. I don’t know if you are in this situation.

Another thing you can do is only open the connection when you need to communicate, but you might be doing that already.


#15

Hi,
Thanks for the reply .

The problem to reboot the system is , I will loose the data acquired from the external device, Once I acquired from the external device I need to send that to the server .And I feel writing to flash is not a good solution .
And when I call wip_bearerStop () it responds to with WIP_BEV_STOPPED, it wont go to the WIP_BEV_IP_DISCONNECTED to call wip_bearerClose,So i’m calling first stop function out side and close function in WIP_BEV_STOPPED …

regards
chethan


#16

Have you looked at ADL_MEM_UNINIT:question:

https://forum.sierrawireless.com/t/persistent-ram/2497/1

As BenFT01 says, there are certain failures where a reset seems to be the only option… :frowning:


#17

Writing to flash could be a very good solution :wink: or persistant ram.

WIP_BEV_CONN_FAILED indicates that the connection is not started, so you don’t need to stop it. Just wip_bearerClose.

When you get a disconnect you could launch some diagnostics first
i.e. AT+CSQ
and then try again but with poll_creg to see if the network is OK.

There is an attribute for the TCP/IP connection called KEEP_ALIVE which may or may not help. It depends what has failed.

You can get a whole new bunch of diagnostics by launching adl_gprsSubscribe() and handling all the events. Might help in seeing what is happening.

I did find something odd about trying to shut down and relaunch a failed connection. I think I reboot these days if all else fails.


#18

Hello,
I have found that bearerClose does not dettach the bearer (ie at+cgatt? gives cgatt=1) if you detach with at+cgatt=0 then start over it seems to fix it.