← Back to team overview

mosquitto-dev team mailing list archive

[Question #136221]: Socket read error while connecting to mosquitto broker through wireless modem

 

New question #136221 on mosquitto:
https://answers.launchpad.net/mosquitto/+question/136221

Hi,
We are writing simple publisher client which connects to mosquitto broker through enfora wireless modem. After we issue the connect modem AT command, we are receiving Socket read error as mentioned below:
mosquitto version 0.9~test2 (build date 2010-12-01 14:48:14+0530) starting
Opening ipv4 listen socket on port 1883.
Opening ipv6 listen socket on port 1883.
New client connected from 223.177.151.217.
New client connected from 223.177.151.217.
*** PLUG - Inside mqtt3_net_read in net.c.***
PLUG - Inside if pollfds(mqtt3_net_read) in mosquitto.c. 1
*** PLUG - Inside mqtt3_net_read in net.c.***
inside core.in_packet
*** PLUG - initial packet.remaining_length 0 ***
*** PLUG - initial packet.remaining_mult 1,26 ***
*** PLUG - Inside packet byte 26 ***
*** PLUG - Inside packet byte 0 ***
*** PLUG - Inside packet.remaining_mult 1,0 ***
 if(context->core.in_packet.remaining_length > 0) in net.c 0
 msgs received in net.c 1
PLUG - Inside mqtt3_packet_handle in read_handle.c.
PLUG - Inside mqtt3_handle_connect in read_handle_server.c.
PLUG - Inside _mosquitto_read_string in net_mosq.c.
PLUG - Inside if packet->pos 0,packet->remaining_length 0 _mosquitto_read_uint16 in net_mosq.c. 2 
 rc in net.c 1
Socket read error on client (null), disconnecting.

We have copied the C code below. Please go through the "main" function. The same above mentioned error is occurring when we issue CONNECT command through wireless modem. We noticed in the case of regular "sockets"(using socket API in a linux computer) instead of 26 in the message *** PLUG - initial packet.remaining_mult 1,26 ***, 16 is displayed in the case of "sockets"(using socket API in a linux computer). It would be useful if you could give us some hints to fix this error.

#include <stdio.h> /* Standard input/output definitions */
#include <stdlib.h>
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h>
#include<ctype.h>/* POSIX terminal control definitions */

/*
 * 'open_port()' - Open serial port 1.
 *
 * Returns the file descriptor on success or -1 on error.
 */

#define BAUDRATE B115200

#define KEEPALIVE 15000
#define MQTTCONNECT 1<<4
#define MQTTPUBLISH 3<<4
#define MQTTSUBSCRIBE 8<<4
int fd = 0;
char * clientId = "greeting";

char connectMessageArr[] = { 0x10 //Connect
		, 0x0C + 0x04 //Remaining Length
		, 0x00 //0
		, 0x06 //6
		, 0x4d //M
		, 0x51 //Q
		, 0x49 //I
		, 0x73 //S
		, 0x64 //D
		, 0x70 //P
		, 0x03 //Protocol version = 3
		, 0x02 //Clean session only
		, 0x00 //Keepalive MSB
		, 0x3c //Keepaliave LSB = 60
		, 0x00 //String length MSB
		, 0x02 //String length LSB = 2
		, 0x4d //M
		, 0x70 //P .. Let's say client ID = MP
		};

char publishMessageArr[] = { 0x30 //Publish with QOS 0
		, 0x05 + 0x05 //Remaining length
		, 0x00 //MSB
		, 0x03 //3 bytes of topic
		, 0x61 //a
		, 0x2F ///
		, 0x62 //b (a/b) is the topic
		, 0x48, 0x45, 0x4c, 0x4c, 0x4f //HELLO is the message
		};

char disconnectMessageArr[] = { 0x0E //Disconnect
		, 0x00 };

int openPort(void) {
	int fd; /* File descriptor for the port */
	struct termios options;

	fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
	if (fd == -1) {
		/*
		 * Could not open the port.
		 */

		perror("open_port: Unable to open /dev/ttyS0 - ");
	} else {
		fcntl(fd, F_SETFL, 0);
	}

	/* get the current options */
	tcgetattr(fd, &options);
	cfsetispeed(&options, B115200);
	cfsetospeed(&options, B115200);


	/* set raw input, 1 second timeout */
	options.c_cflag |= (BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD);
	options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
	options.c_oflag &= ~OPOST;
	options.c_cc[VMIN] = 0;
	options.c_cc[VTIME] = 10;

	/*options.c_cflag &= ~PARENB;
	 options.c_cflag &= ~CSTOPB;
	 options.c_cflag &= ~CSIZE;
	 options.c_cflag |= CS8;

	 options.c_cflag &= ~CRTSCTS;
	 */

	/* set the options */
	tcsetattr(fd, TCSANOW, &options);

	return (fd);
}

int writeToPort(int fd, void * dataStr, int dataLen) {
	int n = write(fd, dataStr, /*strlen(dataStr)*/dataLen);
	if (n < 0) {
		fputs("write() of bytes to serial port failed!\n", stderr);
	}

	return 0;
}

void connect2() {
	char * atMsgCmd = "AT$MSGSND=3,\"";
	char * endQuote = "\"";
	char * endCarriageRet = "\r";
	char cmdPacket[strlen(atMsgCmd) + sizeof(connectMessageArr) + strlen(
			endQuote) + strlen(endCarriageRet)];
	memset(cmdPacket, 0, sizeof(cmdPacket));
	memcpy(cmdPacket, atMsgCmd, strlen(atMsgCmd));
	memcpy(cmdPacket + strlen(atMsgCmd), connectMessageArr,
			sizeof(connectMessageArr));
	memcpy(cmdPacket + strlen(atMsgCmd) + sizeof(connectMessageArr), endQuote,
			strlen(endQuote));
	memcpy(cmdPacket + strlen(atMsgCmd) + sizeof(connectMessageArr) + strlen(
			endQuote), endCarriageRet, strlen(endCarriageRet));

	int arrLen = strlen(atMsgCmd) + sizeof(connectMessageArr)
			+ strlen(endQuote) + strlen(endCarriageRet);
	int i = 0;
	for (; i < 13; i++) {
		//if(j<13)
		printf("%c ", cmdPacket[i]);
		//else
		//printf("%c, ", cmdPacket[j]);
	}

	int j = 13;
	printf("connect2\n");
	for (; j < (arrLen - 2); j++) {
		//if(j<13)
		printf("%x, ", cmdPacket[j]);
		//else
		//printf("%c, ", cmdPacket[j]);
	}
	printf("\n");
	printf("%c ", cmdPacket[arrLen - 2]);

	writeToPort(fd, cmdPacket, arrLen);
}


int main(void) {
	//char  clientId[32] = "greeting";
	//char * clientId = "greeting";
	char serialDataBuff[255];
	serialDataBuff[0] = '\0';

	char *  friendCmd = "AT$FRIEND=02,1,\"122.183.186.19\",1883,1\r";

	puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */

	// int fd = openPort();
	fd = openPort();
	//initModem(fd);

	fcntl(fd, F_SETFL, FNDELAY);

	// writeToPort(fd, "AT+CBST=12,1,2\r", strlen("AT+CBST=12,1,2\r"));

	read(fd, serialDataBuff, 255);

	writeToPort(fd, "AT+ICF=3,0\r", strlen("AT+ICF=3,0\r"));
	writeToPort(fd, "AT+IFC=0,0\r", strlen("AT+IFC=0,0\r"));
	//writeToPort(fd, "AT$IDLETO = 300 \r");
	writeToPort(fd, "AT$IDLETO=60\r", strlen("AT$IDLETO=100\r"));
	// writeToPort(fd, "AT$PADBS = 8\r", strlen("AT$PADBS = 8\r"));
	// writeToPort(fd, "AT$PADCMD = 1B\r", strlen("AT$PADCMD = 1B\r"));
	// writeToPort(fd, "AT$PADFWD = 0D\r", strlen("AT$PADFWD = 0D\r"));
	writeToPort(fd, "AT$ACTIVE=1\r", strlen("AT$ACTIVE=1\r"));
	// writeToPort(fd, "AT$PADBLK = 512\r", strlen("AT$PADBLK = 512\r"));
	//writeToPort(fd, "AT$CONNTO = 300 \r");
	writeToPort(fd, "AT$CONNTO=300\r", strlen("AT$CONNTO=300\r"));
	writeToPort(fd, "AT+CGDCONT=1,\"IP\",\"airtelgprs.com\",\"\",0,0\r",
			strlen("AT+CGDCONT=1,\"IP\",\"airtelgprs.com\",\"\",0,0\r"));
	writeToPort(fd, "AT$PADDST=\"122.183.186.19\",1883\r", strlen("AT$PADDST=\"122.183.186.19\",1883\r"));
	writeToPort(fd, friendCmd, strlen(friendCmd));
	// writeToPort(fd, "AT$PADDST = \"127.0.0.1\", 8800 \r", strlen(
		//	"AT$PADDST = \"127.0.0.1\", 8800 \r"));
	// writeToPort(fd, "AT$PADSRC = 1883\r", strlen("AT$PADSRC = 1883\r"));
	writeToPort(fd, "AT$HOSTIF=2\r", strlen("AT$HOSTIF=2\r"));
	writeToPort(fd, "AT&W\r", strlen("AT&W\r"));
	//writeToPort(fd, "ATD*99***1#\r", strlen("ATD*99***1#\r"));
	writeToPort(fd, "ATD*99#\r", strlen("ATD*99#\r"));
	 //writeToPort(fd, "HAI", strlen("HAI"));
	// writeToPort(fd, "ATDT\r",strlen("ATDT\r"));
	//writeToPort(fd, "ATDT127.0.0.1/8800\r", strlen("ATDT127.0.0.1/8800\r"));
    // writeToPort(fd, "AT$MSGSND=3,\"HAI\"\r", strlen("AT$MSGSND=3,\"HAI\"\r"));

	connect2();

	// pub("a/b", "message");

	close(fd);

	return EXIT_SUCCESS;
}



Regards,

Venky


-- 
You received this question notification because you are a member of
Mosquitto Dev, which is an answer contact for mosquitto.