# WIP and UDP

**URL:** <https://forum.sierrawireless.com/t/wip-and-udp/1585>\
**Category:** Open AT\
**Created:** [September 4, 2007, 8:56pm UTC](https://forum.sierrawireless.com/t/wip-and-udp/1585 "2007-09-04T20:56:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![raffaele](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@raffaele](https://forum.sierrawireless.com/u/raffaele)\
**Post date:** [September 4, 2007, 8:56pm UTC](https://forum.sierrawireless.com/t/wip-and-udp/1585/1 "2007-09-04T20:56:44Z")

</div>

Hello,

I’m new with WIP library and I’m developing an application on Q2686 using UDP via GPRS. The system must connect and disconnect many times for day.  
To check how WIP works I wrote this simple application, based on udp client example included in development system.  
In this example with AT+GPRS=1 you can start GPRS and send UDP packets and receive them, AT+GPRS=0, close UDP channel and GPRS.

After I close (AT+GPRS=0) and then open again connection, I don’t receive any UDP packets but I can send… I close and open connection for about 4-5 times and I receive an error from WIP because the handle of created UDP channel is 0.  
It looks like that the UDP channel’s never closed although I used wip\_close() command before closing GPRS.  
I tried to use a delay between wip\_close and bearer\_stop but no success.

I attach the code, hoping it can be useful and any advice is appreciate.

Thanx

Raff

```auto
void adl_main ( adl_InitType_e InitType )
{
  int r;

  TRACE (( 1, "Embedded Application : Main" ));

  /* Initialize the stack */
  r = wip_netInitOpts( 
      WIP_NET_OPT_DEBUG_PORT, WIP_NET_DEBUG_PORT_UART1, /* WIP traces on UART1 */
      //WIP_NET_OPT_DEBUG_PORT, WIP_NET_DEBUG_PORT_UART2,
      //WIP_NET_OPT_DEBUG_PORT, WIP_NET_DEBUG_PORT_TRACE,
      WIP_NET_OPT_END);

  cfg_gprs(appli_entry_point);
}

void cfg_gprs ( void (* entry_point)(void)) {
  appli_entry_point = entry_point;
  adl_simSubscribe( evh_sim, GPRS_PINCODE);
}

 void evh_bearer( wip_bearer_t b, s8 event, void *ctx) {
  	int tmr_handler;
	ascii IpAddr[16]; 
	wip_in_addr_t appIpAddr; 
	s8 ret;	
	ascii string[100];
	s8 bearer_ans;

	switch(event)
	{
		case WIP_BEV_IP_CONNECTED:
			adl_atSendResponse ( ADL_AT_UNS, "WIP: connected\r\n"); 
			wip_bearerGetOpts(b, WIP_BOPT_IP_ADDR, &appIpAddr, WIP_BOPT_END); //This API provides IP in network format (i.e u32 number) 
			wip_inet_ntoa(appIpAddr , IpAddr, 16); //This API converts the u32 IP address to a dotted notation IP address.

			wm_sprintf(string,"WIP: IP %s\r\n",IpAddr);
			adl_atSendResponse ( ADL_AT_UNS, string); 

			appli_entry_point();
			break;
		case WIP_BEV_IP_DISCONNECTED:
			adl_atSendResponse ( ADL_AT_UNS, "WIP: disconnesso\r\n"); 
			break;
		case WIP_BEV_CONN_FAILED:
			adl_atSendResponse ( ADL_AT_UNS, "WIP: WIP_BEV_CONN_FAILED\r\n"); 

			break;
		case WIP_BEV_STOPPED:
			adl_atSendResponse ( ADL_AT_UNS, "WIP: WIP_BEV_STOPPED\r\n"); 

			bearer_ans = wip_bearerClose(bearer_handle);		   
			
			break;
		default:
			adl_atSendResponse ( ADL_AT_UNS, "WIP: altri errori\r\n"); 
		   
			break;
	}
}

static void evh_sim( u8 event) {
  int r;

  if( ADL_SIM_EVENT_FULL_INIT != event) return;
  adl_atCmdSubscribe("AT+GPRS", GPRSCmdHandler, ADL_CMD_TYPE_PARA | 0x11); 
}

void GPRSCmdHandler(adl_atCmdPreParser_t *parser) 
{ 
   s8 returnValue; 
   char *parameter; 
   int startStop; 
  int r;
  wip_bearer_t b;
  s8 bearer_ans;
    
   parameter = wm_lstGetItem(parser->ParaList, 0); 
   startStop = atoi(parameter); 
   if (startStop == 1) 
   { 
	r = wip_bearerOpen( &bearer_handle, "GPRS", evh_bearer, NULL);
  
	r = wip_bearerSetOpts( bearer_handle, WIP_BOPT_GPRS_APN, GPRS_APN,
                         WIP_BOPT_LOGIN, GPRS_USER,
                         WIP_BOPT_PASSWORD, GPRS_PASSWORD,
                         WIP_BOPT_END);
	r = wip_bearerStart( bearer_handle);
   } 
   else 

   if (startStop == 0) 
   {
   adl_tmrUnSubscribe(tmr_handler,evh_timer,ADL_TMR_TYPE_100MS);
    wip_close(socket);
   bearer_ans = wip_bearerStop(&bearer_handle);
	} 
   else 
   { 
      adl_atSendResponse ( ADL_AT_RSP, "\r\nERROR\r\n" ); 
      return; 
   } 

   adl_atSendResponse ( ADL_AT_RSP, "\r\nOK\r\n" );    
}

static void evh_timer( u8 tmr_id) {
  wip_debug("write MESSAGE\n");
  wip_write( socket, MESSAGE, sizeof(MESSAGE));
}

static void evh_udp( wip_event_t *ev, void *ctx) {
  wip_in_addr_t addr;
  int nread;
  ascii string[100];
  ascii ipd[15]="";
  u16 port;
  u8 buffer [30];
  u16 len;
  

  switch( ev->kind) {

  case WIP_CEV_OPEN: 
    wip_debug( "[SAMPLE] UDP socket ready, starting the timer");
    tmr_handler = adl_tmrSubscribe( TRUE, 10*INTERVAL, 
                                    ADL_TMR_TYPE_100MS, evh_timer);
    break; 

  case WIP_CEV_READ: 

    wip_debug ("[SAMPLE] A datagram arrived\r\n");
	   nread = wip_readOpts( ev->channel, buffer, sizeof(buffer),
                          WIP_COPT_PEER_ADDR, & addr,
                          WIP_COPT_END);
		wip_inet_ntoa(addr , ipd, 16); //This API converts the u32 IP address to a dotted notation IP address.
		wm_sprintf(string,"[WIP] IP %s data %d\r\n",ipd,len);
		adl_atSendResponse ( ADL_AT_UNS, string); 

	   break;

  case WIP_CEV_ERROR:
    wip_debug( "[SAMPLE] Error %i on socket.\n", 
               ev->content.error.errnum);
    if( wip_getState( ev->channel) != WIP_CSTATE_READY) {
      wip_debug( "[SAMPLE] Error is fatal, closing\n");
      adl_tmrUnSubscribe( tmr_handler, evh_timer, ADL_TMR_TYPE_100MS);
      wip_close( ev->channel);
    }
    break;
  }
}

void appli_entry_point() {
  ascii string[100];
  socket = wip_UDPCreateOpts( evh_udp, NULL, 
                              WIP_COPT_PEER_STRADDR, PEER_STRADDR,
                              WIP_COPT_PEER_PORT, PEER_PORT,
                              WIP_COPT_END);

  wm_sprintf(string,"[WIP] UDP handle 0x%x\r\n",socket);
adl_atSendResponse ( ADL_AT_UNS, string);   

}
```

---

<div class="post-metadata">

**Author:** ![tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tobias](https://forum.sierrawireless.com/u/tobias)\
**Post date:** [September 5, 2007, 6:21am UTC](https://forum.sierrawireless.com/t/wip-and-udp/1585/2 "2007-09-05T06:21:26Z")

</div>

Please provide OpenAT SDK version and firmware version as well.

---

<div class="post-metadata">

**Author:** ![raffaele](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@raffaele](https://forum.sierrawireless.com/u/raffaele)\
**Post date:** [September 5, 2007, 3:27pm UTC](https://forum.sierrawireless.com/t/wip-and-udp/1585/3 "2007-09-05T15:27:08Z")

</div>

OpenAT 4.20.03  
Firmware 6.62\_09gg  
WIP 2.00.25

---

<div class="post-metadata">

**Author:** ![raffaele](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@raffaele](https://forum.sierrawireless.com/u/raffaele)\
**Post date:** [September 5, 2007, 9:02pm UTC](https://forum.sierrawireless.com/t/wip-and-udp/1585/4 "2007-09-05T21:02:31Z")

</div>

I have to add that the UDP channel state, after wip\_close, retrieved by wip\_getState is always 2 (WIP\_CSTATE\_TO\_CLOSE).

---

<div class="post-metadata">

**Author:** ![tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tobias](https://forum.sierrawireless.com/u/tobias)\
**Post date:** [September 6, 2007, 9:39am UTC](https://forum.sierrawireless.com/t/wip-and-udp/1585/5 "2007-09-06T09:39:57Z")

</div>

Haven’t had time to look at your code so I can’t say if there’s any problems there.

But, you might want to ask your distributor for OpenAT 4.21 SDK (or newer if available)

OpenAT 4.20.04  
Firmware 6.62  
WIP 3.00.06

---

<div class="post-metadata">

**Author:** ![raffaele](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@raffaele](https://forum.sierrawireless.com/u/raffaele)\
**Post date:** [September 6, 2007, 4:55pm UTC](https://forum.sierrawireless.com/t/wip-and-udp/1585/6 "2007-09-06T16:55:15Z")

</div>

Thank you, I’ve upgraded SDK and the problem is the same. But I have something new. I noticed the problem is with DNS too.  
So, if I put a numeric IP address as destination, I’m able to open UDP connection and to receive packets, than I close connection. The next times I open and close I can send packets but no receiving.  
If I put a domain name as destination, it’s the same as before but after 3rd-4th times I receive this error

```auto
ERRLOG U:\projet\gsmmi\int\plu\serena\openat\WIP\src\ch_udp.c:491: DNS query synchronous error
ERRLOG U:\projet\gsmmi\int\plu\serena\openat\WIP\src\ch_udp.c:492: returned WIP_CERR_INVALID
```

and UDP socket handle is 0.

---

<div class="post-metadata">

**Author:** ![togrady](https://avatars.discourse-cdn.com/v4/letter/t/c0e974/32.png) [@togrady](https://forum.sierrawireless.com/u/togrady)\
**Post date:** [September 19, 2007, 10:09am UTC](https://forum.sierrawireless.com/t/wip-and-udp/1585/7 "2007-09-19T10:09:01Z")

</div>

I also have lots of problems with DNS when connecting a client socket. I get WIP\_CEV\_ERROR when I try to connect. The first few connects work but once it starts failing it will continue to fail until a reset. I updated to the latest everything:

OpenAT 4.20.04  
Firmware 6.62  
WIP 3.00.06

but the problem remains. I’ve been having DNS problems since I started using WIP stack. This is very frustrating. I wish Wavecom would publish their test code so we (the users) could advise them how to construct more extensive regression test code. That way these problems would not be appear in every release.

Regards  
Trevor
