Bearer and tcp client problems

Hi all

Im trying to connect to a server with a tcp client on my FXT009. This is the code relevant:

/*Configuration of the server to connect*/
u16 port = 1883;
//ascii IP[] = "10.128.0.190";
ascii IP[] = "test.mosquitto.org";
ascii APN[] = "machinetohuman.ts.m2m";
ascii DNS1[] = "10.128.0.190";
ascii DNS2[] = "10.128.0.190";

wip_bearer_t myBearer = NULL;
s8 ret = 0;

/*Channel ID for the client socket*/
wip_channel_t channel;

void clientEventHandler(wip_event_t *event, void *context){

	TRACE (( 1, "Inside TCP_Handler"));

	switch(event->kind){

	case WIP_CEV_READ: {
		TRACE ((1,"GOT SOMETHING TO READ"));
	}
	case WIP_CEV_OPEN: {

		TRACE (( 1, "Inside WIP_CEV_OPEN"));
		TRACE (( 1, "CLIENT IS CONNECTED!!"));

		adl_atSendResponse(ADL_AT_UNS,"Client is connected\r\n");

		//channel = event->channel;

		//PREPARE THE CONNECT PACKET

		TRACE((1,"GONNA PREPARE THE CONNECT PACKET"));
		int size = prepareConnect(outBuffer);

		TRACE((1, "SIZE OF CONNECT PACKET = %d",size));

		wip_write(channel, &outBuffer.buffer, size);

		break;
	}

	case WIP_CEV_WRITE: {
		TRACE((1,"WIP_CEV_WRITE"));
		break;
	}

	case WIP_CEV_DONE: {
		TRACE((1,"WIP_CEV_DONE"));
		break;
	}

	case WIP_CEV_ERROR: {
		TRACE((1,"WIP_CEV_ERROR"));
		TRACE((3, "WIP_CEV_ERROR %d\n",
		               event->content.error.errnum));
		//wip_closeAndClear(&event->channel);

		break;
	}

	case WIP_CEV_PING: {
		TRACE((1,"WIP_CEV_PING"));
		break;
	}

	case WIP_CEV_PEER_CLOSE: {
		TRACE((1,"WIP_CEV_PEER_CLOSE"));
		//wip_closeAndClear(&event->channel);

		break;
	}

	}

}

void bearerHandler(wip_bearer_t bearer, s8 event, void *context){

	switch(event){

		case WIP_BEV_IP_CONNECTED: {
			TRACE (( 1, "bearerHandler: <WIP_BEV_IP_CONNECTED>"));

			TRACE((1, "TIME TO MQTT CONNECT"));

			channel = wip_TCPClientCreate(IP, port, clientEventHandler, NULL);

			break;
		}

		case WIP_BEV_CONN_FAILED: {
			TRACE (( 1, "bearerHandler: <WIP_BEV_CONN_FAILED>"));
			break;
		}

		case WIP_BEV_STOPPED: {
			TRACE (( 1, "bearerHandler: <WIP_BEV_STOPPED>"));
			wip_bearerStop(myBearer);
			break;
		}

		default: {
			TRACE (( 1, "bearerHandler: <DEFAULT>"));
			break;
		}
	}
}

void simHandler (u8 event){

	switch(event){

		case ADL_SIM_EVENT_FULL_INIT:{

			TRACE((1, "ADL_SIM_EVENT_FULL_INIT"));

			if( (ret = wip_bearerOpen(&myBearer, "GPRS", bearerHandler, NULL)) != 0){
				TRACE((1,"CANNOT OPEN BEARER: %d",ret));
				return;
			} else {
				TRACE((1,"SUCCESS OPEN BEARER: %d",ret));
			}

			if((ret = wip_bearerSetOpts(myBearer,
					WIP_BOPT_GPRS_APN, APN,
					WIP_BOPT_LOGIN, "",
					WIP_BOPT_PASSWORD, "",
					//WIP_BOPT_IP_DNS1, DNS1,
					//WIP_BOPT_IP_DNS2, DNS2,
					WIP_BOPT_DEBUG_PKT, TRUE,
					WIP_BOPT_END)) != 0){
				TRACE((1,"CANNOT SET BEARER OPTS: %d",ret));
				return;
			} else {
				TRACE((1,"SUCCESS SET OPTS: %d",ret));
			}

			if((ret = wip_bearerStart(myBearer)) != 0 && ret != WIP_BERR_OK_INPROGRESS){
				TRACE((1,"CANNOT START BEARER: %d",ret));
				return;
			} else {
				TRACE((1,"SUCCESS START BEARER: %d",ret));
			}

			break;
		}

		default: {
			TRACE((1, "IN DEFAULT"));
			break;
		}
	}
}


void MainTask ( void ) {

	init();

	TRACE((1,"IN MAIN"));

	//wip_netInit();

	adl_simSubscribe((adl_simHdlr_f) simHandler, NULL);

	wip_netInitOpts(WIP_NET_OPT_DEBUG_PORT, WIP_NET_DEBUG_PORT_TRACE, WIP_NET_OPT_DEBUG_PKT_MAXLEN, 1500,WIP_NET_OPT_END);
}

And here are the traces:

2013-06-05	10:19:33:212	1	ADL	1	 Binary header at 002A0000				
2013-06-05	10:19:33:217	1	ADL	16	[ADL PORT] subs (002A3579) : 0				
2013-06-05	10:19:33:218	1	ADL	16	[ADL PORT] subs (002A519F) : 1				
2013-06-05	10:19:33:218	2	ADL	22	[ADL] flash subs 2 : -4				
2013-06-05	10:19:33:219	1	ADL	22	Flh Obj 0000 Len : 4				
2013-06-05	10:19:33:220	1	ADL	22	Read Flh Obj 0000 (4) : 0				
2013-06-05	10:19:33:222	1	ADL	16	[ADL PORT] event : 0 (port 80 	 state 0)			
2013-06-05	10:19:33:223	1	ADL	16	[ADL PORT] event : 0 (port 01 	 state 0)			
2013-06-05	10:19:33:223	2	ADL	22	Flh Obj 0000 Len : 4				
2013-06-05	10:19:33:223	3	ADL	16	[ADL PORT] unsubs (1) : 0				
2013-06-05	10:19:33:231	1	ADL	1	IN MAIN				
2013-06-05	10:19:33:231	2	ADL	20	[ADL] tmr subs 	 id 0 	 hdlr 002AA075 	 val 5 	 cycl 2
2013-06-05	10:19:33:259	1	ADL	16	[ADL PORT] event : 0 (port 03 	 state 0)			
2013-06-05	10:19:33:261	1	ADL	1	IN DEFAULT				
2013-06-05	10:19:33:653	1	ADL	1	IN DEFAULT				
2013-06-05	10:19:38:905	1	ADL	1	ADL_SIM_EVENT_FULL_INIT				
2013-06-05	10:19:38:905	2	ADL	1	[GPRS]: open: -> DISCONNECTED				
2013-06-05	10:19:38:905	3	ADL	1	SUCCESS OPEN BEARER: 0				
2013-06-05	10:19:38:905	4	ADL	1	SUCCESS SET OPTS: 0				
2013-06-05	10:19:38:905	5	ADL	27	Call subs 002B0B41 : 0				
2013-06-05	10:19:38:905	6	ADL	31	[ADL] Gprs subs 002AD221 : 0				
2013-06-05	10:19:38:905	7	ADL	16	[ADL port] IsAvailable(80) : 1				
2013-06-05	10:19:38:920	1	ADL	31	[ADL] Gprs setup 1 : 0				
2013-06-05	10:19:38:920	2	ADL	1	[GPRS]: start: -> CONNECTING				
2013-06-05	10:19:38:920	3	ADL	1	SUCCESS START BEARER: -27				
2013-06-05	10:19:38:920	4	ADL	1	[GPRS]: GPRS CTX (cid=1) found				
2013-06-05	10:19:38:920	5	ADL	1	[GPRS]: GPRS EVENT SETUP OK (cid=1): GPRS activate				
2013-06-05	10:19:38:920	6	ADL	16	[ADL port] IsAvailable(80) : 1				
2013-06-05	10:19:41:889	1	ADL	1	[GPRS]: GPRS CTX (cid=1) found				
2013-06-05	10:19:41:889	2	ADL	1	[GPRS]: GPRS EVENT: 27 (cid=1)				
2013-06-05	10:19:42:717	1	ADL	1	[GPRS]: GPRS CTX (cid=1) found				
2013-06-05	10:19:42:717	2	ADL	1	[GPRS]: GPRS EVENT ACTIVATE OK (cid=1)				
2013-06-05	10:19:42:733	1	ADL	1	[GPRS]: GPRS: -> CONNECTED				
2013-06-05	10:19:42:733	2	ADL	1	bearerHandler: <WIP_BEV_IP_CONNECTED>				
2013-06-05	10:19:42:733	3	ADL	1	TIME TO MQTT CONNECT				
2013-06-05	10:19:42:733	4	ADL	1	[WIP] new TCPCLIENT 0x1810a5d8				
2013-06-05	10:20:21:155	1	ADL	1	Inside TCP_Handler				
2013-06-05	10:20:21:155	2	ADL	1	WIP_CEV_ERROR				
2013-06-05	10:20:21:155	3	ADL	3	WIP_CEV_ERROR -986<LF>

I understand the WIP_CEV_ERROR -986, “Timeout (for DNS request, TCP connection, PING response…)”

I always get the -27 code (Connection started, an event will be sent after completion) when Im trying to start the bearer and not 0, dont know why…

I just want some help if someone can that my bearer and tcp client code is wrong in any way.

Best regards

Hi,

Did you try changing port number for TCp connection.Please try with port 80 as well .

Thanks.

Hi

The server I want to connect to is a Mosquitto MQTT message broker which listens to port 1883.

I tried with port 80 anyway but still same result.

Your code looks ok, your bearer is starting correctly, the -27 code is normal.

The timeout is probably due to the unit not being able to resolve the address. That APN you are using, is it a private APN or does it have access to the internet? Have you tried putting the IP address of the server (i think its 85.119.83.194 based on a trace route) instead of the url?

Hi

You were right about the APN @tomridl, it is private and I was trying to connect to a server outside of that. Now I’m connecting to the correct server inside the APN.

But it still does not work, I dont get any error codes but the application is restarting pretty fast after I get inside the TCP handler:

2013-06-10	10:27:56:114	1	ADL	1	Binary header at 002A0000				
2013-06-10	10:27:56:118	1	ADL	16	[ADL PORT] subs (002A3645) : 0				
2013-06-10	10:27:56:119	1	ADL	16	[ADL PORT] subs (002A526B) : 1				
2013-06-10	10:27:56:129	1	ADL	22	[ADL] flash subs 2 : -4				
2013-06-10	10:27:56:134	1	ADL	22	Flh Obj 0000 Len : 4				
2013-06-10	10:27:56:135	1	ADL	22	Read Flh Obj 0000 (4) : 0				
2013-06-10	10:27:56:136	1	ADL	16	[ADL PORT] event : 0 (port 80 	 state 0)			
2013-06-10	10:27:56:136	2	ADL	16	[ADL PORT] event : 0 (port 01 	 state 0)			
2013-06-10	10:27:56:137	1	ADL	22	Flh Obj 0000 Len : 4				
2013-06-10	10:27:56:137	2	ADL	16	[ADL PORT] unsubs (1) : 0				
2013-06-10	10:27:56:139	1	ADL	1	IN MAIN				
2013-06-10	10:27:56:139	2	ADL	1	STARTING INIT				
2013-06-10	10:27:56:140	1	ADL	1	INIT DONE				
2013-06-10	10:27:56:140	2	ADL	20	[ADL] tmr subs 	 id 0 	 hdlr 002AA141 	 val 5 	 cycl 2
2013-06-10	10:27:56:163	1	ADL	16	[ADL PORT] event : 0 (port 03 	 state 0)			
2013-06-10	10:27:56:164	1	ADL	1	IN DEFAULT				
2013-06-10	10:27:56:596	1	ADL	1	IN DEFAULT				
2013-06-10	10:28:01:866	1	ADL	1	ADL_SIM_EVENT_FULL_INIT				
2013-06-10	10:28:01:866	2	ADL	1	[GPRS]: open: -> DISCONNECTED				
2013-06-10	10:28:01:866	3	ADL	1	SUCCESS OPEN BEARER: 0				
2013-06-10	10:28:01:866	4	ADL	1	SUCCESS SET OPTS: 0				
2013-06-10	10:28:01:866	5	ADL	27	Call subs 002B0C0D : 0				
2013-06-10	10:28:01:866	6	ADL	31	[ADL] Gprs subs 002AD2ED : 0				
2013-06-10	10:28:01:866	7	ADL	16	[ADL port] IsAvailable(80) : 1				
2013-06-10	10:28:01:866	8	ADL	31	[ADL] Gprs setup 1 : 0				
2013-06-10	10:28:01:866	9	ADL	1	[GPRS]: start: -> CONNECTING				
2013-06-10	10:28:01:866	10	ADL	1	SUCCESS START BEARER: -27				
2013-06-10	10:28:01:866	11	ADL	1	[GPRS]: GPRS CTX (cid=1) found				
2013-06-10	10:28:01:866	12	ADL	1	[GPRS]: GPRS EVENT SETUP OK (cid=1): GPRS activate				
2013-06-10	10:28:01:881	1	ADL	16	[ADL port] IsAvailable(80) : 1				
2013-06-10	10:28:04:881	1	ADL	1	[GPRS]: GPRS CTX (cid=1) found				
2013-06-10	10:28:04:881	2	ADL	1	[GPRS]: GPRS EVENT: 27 (cid=1)				
2013-06-10	10:28:05:584	1	ADL	1	[GPRS]: GPRS CTX (cid=1) found				
2013-06-10	10:28:05:584	2	ADL	1	[GPRS]: GPRS EVENT ACTIVATE OK (cid=1)				
2013-06-10	10:28:05:600	1	ADL	1	[GPRS]: GPRS: -> CONNECTED				
2013-06-10	10:28:05:600	2	ADL	1	bearerHandler: <WIP_BEV_IP_CONNECTED>				
2013-06-10	10:28:05:600	3	ADL	1	TIME TO MQTT CONNECT				
2013-06-10	10:28:05:600	4	ADL	1	[WIP] new TCPCLIENT 0x1810a5c8				
2013-06-10	10:28:05:834	1	ADL	1	Inside TCP_Handler				
2013-06-10	10:28:13:913	1	ADL	1	Binary header at 002A0000				
2013-06-10	10:28:13:914	1	ADL	16	[ADL PORT] subs (002A3645) : 0				
2013-06-10	10:28:13:917	1	ADL	16	[ADL PORT] subs (002A526B) : 1				
2013-06-10	10:28:13:918	1	ADL	22	[ADL] flash subs 2 : -4				
2013-06-10	10:28:13:920	1	ADL	22	Flh Obj 0000 Len : 4				
2013-06-10	10:28:13:921	1	ADL	22	Read Flh Obj 0000 (4) : 0				
2013-06-10	10:28:13:922	1	ADL	16	[ADL PORT] event : 0 (port 80 	 state 0)			
2013-06-10	10:28:13:924	1	ADL	16	[ADL PORT] event : 0 (port 01 	 state 0)			
2013-06-10	10:28:13:928	1	ADL	22	Flh Obj 0000 Len : 4				
2013-06-10	10:28:13:929	1	ADL	16	[ADL PORT] unsubs (1) : 0				
2013-06-10	10:28:13:935	1	ADL	1	IN MAIN				
2013-06-10	10:28:13:937	1	ADL	1	STARTING INIT				
2013-06-10	10:28:13:938	1	ADL	1	INIT DONE				
2013-06-10	10:28:13:942	1	ADL	20	[ADL] tmr subs 	 id 0 	 hdlr 002AA141 	 val 5 	 cycl 2
2013-06-10	10:28:14:103	1	ADL	16	[ADL PORT] event : 0 (port 03 	 state 0)			
2013-06-10	10:28:14:105	1	ADL	1	IN DEFAULT				
2013-06-10	10:28:14:593	1	ADL	1	IN DEFAULT

I can’t really tell if my application is designed correctly, possibly not.

When the GPRS and TCP client init is done, I want to send a connect packet to my MQTT server (the first step of a MQTT connect handshake), hence this code. The connect packet is being built and put in the buffer and when that is done, I want to send it.

case WIP_CEV_OPEN: {

      TRACE (( 1, "Inside WIP_CEV_OPEN"));
      TRACE (( 1, "CLIENT IS CONNECTED!!"));

      adl_atSendResponse(ADL_AT_UNS,"Client is connected\r\n");

      TRACE((1,"GONNA PREPARE THE CONNECT PACKET"));
      int size = prepareConnect(outBuffer);

      TRACE((1, "SIZE OF CONNECT PACKET = %d",size));

      wip_write(channel, &outBuffer.buffer, size);
      break;
   }

But since the device keeps restarting I think I may be doing this in the wrong way and by reading about Multitasking in the manual, I’m pretty certain im doing this the wrong way.

I would gladly appreciate If someone could point me in the right direction regarding the design/structure. If you need more information about my application and/or the intentions I have with it, please do not hesitate to ask.

Best regards!