# Start and stop gprs bearer connection

**URL:** <https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923>\
**Category:** Open AT\
**Created:** [February 26, 2011, 10:11am UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923 "2011-02-26T10:11:46Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![raveesa](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@raveesa](https://forum.sierrawireless.com/u/raveesa)\
**Post date:** [February 26, 2011, 10:11am UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/1 "2011-02-26T10:11:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.sierrawireless.com/u/awneil)\
**Post date:** [February 26, 2011, 11:49am UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/2 "2011-02-26T11:49:43Z")

</div>

I presume this refers to your other thread: [https://forum.sierrawireless.com/t/q2686-tcp-sample-code/4922/6](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 …

---

<div class="post-metadata">

**Author:** ![raveesa](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@raveesa](https://forum.sierrawireless.com/u/raveesa)\
**Post date:** [February 28, 2011, 12:19pm UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/3 "2011-02-28T12:19:53Z")

</div>

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

```auto
#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

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.sierrawireless.com/u/awneil)\
**Post date:** [February 28, 2011, 12:58pm UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/4 "2011-02-28T12:58:40Z")

</div>

> [@raveesa](#):
>
> it fails at the Client connecting to server

How, exactly, does it fail?

> [@](#):
>
> What may be the problem …?

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](http://www.8052.com/faqs/120313)

and: [techonlineindia.com/article/ … anner.aspx](http://www.techonlineindia.com/article/09-09-23/Developing_a_good_bedside_manner.aspx)

---

<div class="post-metadata">

**Author:** ![raveesa](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@raveesa](https://forum.sierrawireless.com/u/raveesa)\
**Post date:** [February 28, 2011, 1:13pm UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/5 "2011-02-28T13:13:06Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![raveesa](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@raveesa](https://forum.sierrawireless.com/u/raveesa)\
**Post date:** [February 28, 2011, 4:49pm UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/6 "2011-02-28T16:49:50Z")

</div>

Hi,

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

reagrds  
Ravee

---

<div class="post-metadata">

**Author:** ![BenFT01](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@BenFT01](https://forum.sierrawireless.com/u/BenFT01)\
**Post date:** [March 1, 2011, 10:02am UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/7 "2011-03-01T10:02:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![raveesa](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@raveesa](https://forum.sierrawireless.com/u/raveesa)\
**Post date:** [March 1, 2011, 1:24pm UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/8 "2011-03-01T13:24:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.sierrawireless.com/u/awneil)\
**Post date:** [March 1, 2011, 1:54pm UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/9 "2011-03-01T13:54:56Z")

</div>

> [@raveesa](#):
>
> But some times suddenly IP gets terminated

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: [Start and stop gprs bearer connection - #8 by raveesa](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/8)

---

<div class="post-metadata">

**Author:** ![raveesa](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@raveesa](https://forum.sierrawireless.com/u/raveesa)\
**Post date:** [March 1, 2011, 4:03pm UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/10 "2011-03-01T16:03:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BenFT01](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@BenFT01](https://forum.sierrawireless.com/u/BenFT01)\
**Post date:** [March 1, 2011, 6:01pm UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/11 "2011-03-01T18:01:53Z")

</div>

"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.

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.sierrawireless.com/u/awneil)\
**Post date:** [March 1, 2011, 11:01pm UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/12 "2011-03-01T23:01:25Z")

</div>

> [@raveesa](#):
>
> I’m basically in to hardware design

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?

---

<div class="post-metadata">

**Author:** ![raveesa](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@raveesa](https://forum.sierrawireless.com/u/raveesa)\
**Post date:** [March 29, 2011, 6:28am UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/13 "2011-03-29T06:28:24Z")

</div>

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

```auto
#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

---

<div class="post-metadata">

**Author:** ![BenFT01](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@BenFT01](https://forum.sierrawireless.com/u/BenFT01)\
**Post date:** [March 29, 2011, 3:45pm UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/14 "2011-03-29T15:45:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![raveesa](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@raveesa](https://forum.sierrawireless.com/u/raveesa)\
**Post date:** [March 30, 2011, 2:57am UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/15 "2011-03-30T02:57:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.sierrawireless.com/u/awneil)\
**Post date:** [March 30, 2011, 6:49am UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/16 "2011-03-30T06:49:25Z")

</div>

> [@raveesa](#):
>
> The problem to reboot the system is , I will loose the data acquired from the external device

Have you looked at ADL\_MEM\_UNINIT… ❓

> [@Persistent RAM](https://forum.sierrawireless.com/t/persistent-ram/2497/1):
>
> Hi I am planing to store my log entries on the new Persistant RAM. for that I want to store string array in Persistant RAM area. is it possible to do ? ADL\_MEM\_UNINIT(ascii logEntries[100][101]) ? Note: this is avaliable on the new SDK 2.10 with firmware 7.2 slight_smile I checked the sample and it worked fine but with arrays and strings it gives me some issues when I am compiling to target. Thanks Asiri

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

---

<div class="post-metadata">

**Author:** ![BenFT01](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@BenFT01](https://forum.sierrawireless.com/u/BenFT01)\
**Post date:** [March 30, 2011, 9:12am UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/17 "2011-03-30T09:12:31Z")

</div>

Writing to flash could be a very good solution 😉 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.

---

<div class="post-metadata">

**Author:** ![MLCarter](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@MLCarter](https://forum.sierrawireless.com/u/MLCarter)\
**Post date:** [April 6, 2011, 2:04am UTC](https://forum.sierrawireless.com/t/start-and-stop-gprs-bearer-connection/4923/18 "2011-04-06T02:04:31Z")

</div>

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.
