Automatic GPRS reconnection

I have a project now with Q2687 module for data collection of remote sites. It interfaces with some PLCs to collect their data remotely via GPRS network. when the GPRS network fails those devices must automatically connect again after the network is back.

So I’ve tried to make automatic reconnection when bearer is disconnected by any reason.

I’ve added this in the evh_bearer function:

else if(WIP_BEV_IP_DISCONNECTED == event)
                                                          result = wip_bearerStop(b);
  else if(WIP_BEV_STOPPED == event)
  {
    result = wip_bearerClose(b);
     wip_debug("[GPRS]: Bearer Disconnected, trying to reconnect\r\n");
     poll_creg( 0); //try to reconnect
  }

this works well ,but the problem is when the GPRS fails when I’m trying to reconnect. the wip_bearerStart() function returns -27 (which is WIP_BERR_OK_INPROGRESS) then this progress fails without throwing any events in the evh_bearer function!

I’ve added the WIP_BOPT_RESTART = TRUE in the wip_bearerSetOpts() function but the problem still exist.
how could I konw that the bearer failed to start?
Is there any way to konw that? because the only way I have now is to reset the module manually.

Please help!

If you’re getting no events to go on, the only option is to have a timeout?

You mean to subscribe a timer for timeout?
But how could I know inside the timer handler the state of the bearer?
Is it already connected or failed to connect?

You maintain a global variable with that information?

Or, instead of doing it in the timer handler, you have the timer handler feed a “Timeout” event into your Bearer state machine…

Briliant…
but how I could know the time it takes to connect? If it fails or still trying?

I think there is somewhere in the code where I can know that the bearer failed to connect, without timeouts, because I have some debug info coming from the UART.

[GPRS]: → PEER DISCONNECTING
[GPRS]: GPRS deactivate
[GPRS]: stop: → DISCONNECTING
[GPRS]: GPRS EVENT: 2 (cid=1)
[GPRS]: GPRS EVENT: 2 (cid=1)
[GPRS]: GPRS EVENT DEACTIVATE OK/KO (cid=5): → DISCONNECTED
[GPRS]: close: → CLOSED
[GPRS]: Bearer Disconnected, trying to reconnect

[GPRS]: open: → DISCONNECTED
[GPRS]: GPRS EVENT DEACTIVATE OK/KO (cid=5): → DISCONNECTED

I don’t know where the last line came from. I think it’s in the WIP code. Can I reach this line or find any event related to it?

It’s your tace from your evh_bearer function, isn’t it?
As you showed at the start of the thread:

else if(WIP_BEV_STOPPED == event)
  {
    result = wip_bearerClose(b);
     wip_debug("[GPRS]: Bearer Disconnected, trying to reconnect\r\n");
     poll_creg( 0); //try to reconnect
  }

I suggest the you have your evh_bearer function trace every event that it receives - so that you can see exactly what’s happening, and if there are any events happening that you aren’t handling, but should be…

You could also look at the ADL_GPRS_EVENT_… events…

After some traces I found that the last line:

[GPRS]: GPRS EVENT DEACTIVATE OK/KO (cid=5): -> DISCONNECTED

comes after I receive -27 from the wip_bearerStart( b); function

it’s not in my code.

I want to make it to retry connection after that line, but I don’t know where is it.

Have you done as I suggested and ensured that you trace all Bearer events?

something along the lines of:

my_event_handler( event )
{
    TRACE(( MY_EVENT_TRACE_LEVEL, "Event=%d", event ));
    :
    :
}

After WIP_BERR_OK_INPROGRESS you certainly should get an event that either tells you that the connection has succeeded, or that it has failed. If you have omitted to handle one of these events, then your state machine will get stuck if it happens!

yes, but no events come.

I searched the datasheets for the available events and found only 3:

WIP_BEV_IP_CONNECTED
WIP_BEV_IP_DISCONNECTED
WIP_BEV_STOPPED

Is there any other events?

Ok I found another one which I was missing:

WIP_BEV_CONN_FAILED

that one comes already when the reconnection fails.

thank you very much.

here is the evh code if you have any comment:

else if(WIP_BEV_IP_DISCONNECTED == event)
  {
	  result = adl_fcmUnsubscribe(V24M_Handle); //unsubscribe FCM service
	  result = wip_bearerStop(b);

  }
  else if(WIP_BEV_STOPPED == event)
  {
	  result = adl_fcmUnsubscribe(V24M_Handle); //unsubscribe FCM service
	  result = wip_bearerClose(b);
	  wip_debug("[GPRS]: Bearer Disconnected, trying to reconnect\r\n");
	  poll_creg( 0); //try to reconnect
  }
  else if(WIP_BEV_CONN_FAILED == event)
  {
	  wip_debug("[GPRS]: Connection Failed\r\n");
	  poll_creg( 0); //try to reconnect
  }
  else
  {
	  wip_debug("\n\r[GPRS]: Unknown Event=%i\n\r",event);
  }

thank you again…

Same comment again: Trace every event that comes into the handler - before you do anything else; outside of any conditionals - not just the ones you handle!

Hiya,

Been here, fought it to a standstill.

Subscribe to the ADL GPRS events using adl_gprsSubscribe() - this was the only way I could reliably be informed that the network had ‘gone away’.

See this thread for some more comments.

ciao, Dave

thank you Dave,

after reading that post I realized that my solution and this solution are not suffitient. One of my colleagues got a new idea which I still don’t know if it is possible or not.

Can I program the module to ping on itself every now and then?
I mean if we send a ping to our IP and wait for this ping to comeback, if it fails then restart the connection. Is it possible?
will it solve the problem?

because if we ping the server we’re afraid of too much traffic on it.
waiting for reply…

Hiya,

Pinging yourself probably won’t do much good, as the ping packet will (should?) be handled by the routing code in the local TCP stack.

However, you could try pinging the other end of the GPRS link (assuming your ISP suports ICMP PING on thei gateway). You also don’t need to ping very often - three pings, 1000ms apart every 5 minutes?

ciao, Dave