aqhome: Improved serial port handling, added parsers for different msg types.
This commit is contained in:
356
aqhome/serial.c
356
aqhome/serial.c
@@ -37,11 +37,13 @@ int _attnLow(AQH_SERIAL *sr);
|
||||
int _attnHigh(AQH_SERIAL *sr);
|
||||
int _readForced(AQH_SERIAL *sr, uint8_t *buf, int len);
|
||||
int _writeForced(AQH_SERIAL *sr, const uint8_t *buf, uint8_t len);
|
||||
int _check(const uint8_t *ptr, uint8_t len);
|
||||
uint8_t _calcChecksum(const uint8_t *ptr, uint8_t len);
|
||||
int _discardInput(AQH_SERIAL *sr);
|
||||
|
||||
|
||||
|
||||
#define AQH_SERIAL_BAUDRATE B19200
|
||||
#define AQH_SERIAL_BYTE_MICROSECS 520
|
||||
|
||||
|
||||
|
||||
AQH_SERIAL *AQH_Serial_new(const char *deviceName, uint8_t addr)
|
||||
@@ -52,9 +54,10 @@ AQH_SERIAL *AQH_Serial_new(const char *deviceName, uint8_t addr)
|
||||
sr->deviceName=deviceName?strdup(deviceName):NULL;
|
||||
sr->address=addr;
|
||||
sr->fd=-1;
|
||||
sr->intendedAttnState=1;
|
||||
|
||||
sr->bytesToRead=2;
|
||||
|
||||
sr->receivedMessageList=AQH_Msg_List_new();
|
||||
sr->sendMessageList=AQH_Msg_List_new();
|
||||
return sr;
|
||||
}
|
||||
|
||||
@@ -63,6 +66,8 @@ AQH_SERIAL *AQH_Serial_new(const char *deviceName, uint8_t addr)
|
||||
void AQH_Serial_free(AQH_SERIAL *sr)
|
||||
{
|
||||
if (sr) {
|
||||
AQH_Msg_List_free(sr->receivedMessageList);
|
||||
AQH_Msg_List_free(sr->sendMessageList);
|
||||
free(sr->deviceName);
|
||||
GWEN_FREE_OBJECT(sr);
|
||||
}
|
||||
@@ -70,6 +75,13 @@ void AQH_Serial_free(AQH_SERIAL *sr)
|
||||
|
||||
|
||||
|
||||
uint8_t AQH_Serial_GetAddress(const AQH_SERIAL *sr)
|
||||
{
|
||||
return sr->address;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_Serial_Open(AQH_SERIAL *sr)
|
||||
{
|
||||
int fd;
|
||||
@@ -78,37 +90,42 @@ int AQH_Serial_Open(AQH_SERIAL *sr)
|
||||
struct termios options;
|
||||
int rv;
|
||||
|
||||
fd=open(sr->deviceName,O_RDWR | O_NOCTTY );
|
||||
fd=open(sr->deviceName,O_RDWR | O_NOCTTY | O_NDELAY);
|
||||
if (fd<0) {
|
||||
DBG_ERROR(NULL, "Error on open(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
||||
return GWEN_ERROR_IO;
|
||||
}
|
||||
rv=tcgetattr(fd, &options);
|
||||
rv=tcgetattr(fd, &(sr->previousOptions));
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "Error on tcgetattr(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
||||
return GWEN_ERROR_IO;
|
||||
}
|
||||
rv=cfsetispeed(&options, B19200);
|
||||
//rv=cfsetispeed(&options, B9600);
|
||||
memset(&options, 0, sizeof(options)); /* preset */
|
||||
|
||||
options.c_cflag=CLOCAL | CREAD | CS8;
|
||||
options.c_iflag=IGNPAR | IGNBRK;
|
||||
options.c_oflag=0;
|
||||
options.c_lflag=0;
|
||||
cfmakeraw(&options);
|
||||
options.c_cc[VTIME]=0; /* read timeout in deciseconds */
|
||||
options.c_cc[VMIN]=0; /* no minimum number of receive bytes */
|
||||
|
||||
rv=cfsetispeed(&options, AQH_SERIAL_BAUDRATE);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "Error on cfsetispeed(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
||||
return GWEN_ERROR_IO;
|
||||
}
|
||||
rv=cfsetospeed(&options, B19200);
|
||||
rv=cfsetospeed(&options, AQH_SERIAL_BAUDRATE);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "Error on cfsetospeed(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
||||
return GWEN_ERROR_IO;
|
||||
}
|
||||
|
||||
options.c_cflag &= ~PARENB;
|
||||
options.c_cflag &= ~CSTOPB; /* use one stopbit insteadof 2 */
|
||||
options.c_cflag &= ~CSIZE;
|
||||
options.c_cflag |= CS8;
|
||||
|
||||
options.c_cflag &= ~CRTSCTS; /* disable HW flow control */
|
||||
options.c_iflag &= ~(IXON | IXOFF | IXANY); /* disable SW flow control */
|
||||
|
||||
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* raw input */
|
||||
rv=tcflush(fd, TCIOFLUSH);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "Error on tcflush(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
||||
return GWEN_ERROR_IO;
|
||||
}
|
||||
|
||||
rv=tcsetattr(fd, TCSANOW, &options);
|
||||
if (rv<0) {
|
||||
@@ -135,8 +152,30 @@ void AQH_Serial_Close(AQH_SERIAL *sr)
|
||||
|
||||
|
||||
|
||||
int AQH_Serial_IsOpen(AQH_SERIAL *sr)
|
||||
{
|
||||
if (sr->fd<0) {
|
||||
DBG_ERROR(NULL, "Device not open");
|
||||
return GWEN_ERROR_NOT_OPEN;
|
||||
}
|
||||
else {
|
||||
struct termios options;
|
||||
int rv;
|
||||
|
||||
rv=tcgetattr(sr->fd, &options);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "Error on tcgetattr(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
||||
return GWEN_ERROR_IO;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_Serial_Recv(AQH_SERIAL *sr, uint8_t *buf, int len)
|
||||
{
|
||||
#if 0
|
||||
int rv;
|
||||
uint8_t *bufSaved;
|
||||
int bytesReceived=0;
|
||||
@@ -147,7 +186,7 @@ int AQH_Serial_Recv(AQH_SERIAL *sr, uint8_t *buf, int len)
|
||||
/* destination, msg length */
|
||||
rv=_readForced(sr, buf, 2);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
msgLen=buf[1];
|
||||
@@ -156,7 +195,7 @@ int AQH_Serial_Recv(AQH_SERIAL *sr, uint8_t *buf, int len)
|
||||
/* read message and XOR byte */
|
||||
rv=_readForced(sr, buf, msgLen+1); /* add one byte for XOR checksum */
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
buf+=msgLen;
|
||||
@@ -164,28 +203,30 @@ int AQH_Serial_Recv(AQH_SERIAL *sr, uint8_t *buf, int len)
|
||||
bytesReceived=msgLen+3;
|
||||
rv=_check(bufSaved, bytesReceived);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
return bytesReceived;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_Serial_Send(AQH_SERIAL *sr, const uint8_t *ptr, uint8_t len)
|
||||
{
|
||||
#if 0
|
||||
int rv;
|
||||
|
||||
rv=_check(ptr, len);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
rv=_attnLow(sr);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
@@ -193,13 +234,14 @@ int AQH_Serial_Send(AQH_SERIAL *sr, const uint8_t *ptr, uint8_t len)
|
||||
|
||||
rv=_writeForced(sr, ptr, len);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
_attnHigh(sr);
|
||||
return rv;
|
||||
}
|
||||
|
||||
_attnHigh(sr);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -211,7 +253,7 @@ int AQH_Serial_SendPacket(AQH_SERIAL *sr, uint8_t destAddr, const uint8_t *ptr,
|
||||
|
||||
rv=_attnLow(sr);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
@@ -220,7 +262,7 @@ int AQH_Serial_SendPacket(AQH_SERIAL *sr, uint8_t destAddr, const uint8_t *ptr,
|
||||
/* send dest address */
|
||||
rv=_writeForced(sr, &destAddr, 1);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
_attnHigh(sr);
|
||||
return rv;
|
||||
}
|
||||
@@ -229,7 +271,7 @@ int AQH_Serial_SendPacket(AQH_SERIAL *sr, uint8_t destAddr, const uint8_t *ptr,
|
||||
/* send msg len */
|
||||
rv=_writeForced(sr, &len, 1);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
_attnHigh(sr);
|
||||
return rv;
|
||||
}
|
||||
@@ -241,7 +283,7 @@ int AQH_Serial_SendPacket(AQH_SERIAL *sr, uint8_t destAddr, const uint8_t *ptr,
|
||||
/* send message */
|
||||
rv=_writeForced(sr, ptr, len);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
_attnHigh(sr);
|
||||
return rv;
|
||||
}
|
||||
@@ -253,7 +295,7 @@ int AQH_Serial_SendPacket(AQH_SERIAL *sr, uint8_t destAddr, const uint8_t *ptr,
|
||||
/* send XOR checksum */
|
||||
rv=_writeForced(sr, &x, 1);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
_attnHigh(sr);
|
||||
return rv;
|
||||
}
|
||||
@@ -287,7 +329,7 @@ int _attnLow(AQH_SERIAL *sr)
|
||||
DBG_ERROR(NULL, "Error on ioctl(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
||||
return GWEN_ERROR_IO;
|
||||
}
|
||||
|
||||
sr->intendedAttnState=0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -311,6 +353,7 @@ int _attnHigh(AQH_SERIAL *sr)
|
||||
return GWEN_ERROR_IO;
|
||||
}
|
||||
|
||||
sr->intendedAttnState=1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -366,43 +409,10 @@ int _writeForced(AQH_SERIAL *sr, const uint8_t *buf, uint8_t len)
|
||||
|
||||
|
||||
|
||||
void AQH_Serial_PacketReceived(AQH_SERIAL *sr, const uint8_t *ptr, uint8_t len)
|
||||
void AQH_Serial_PacketReceived(AQH_SERIAL *sr, AQH_MSG *msg)
|
||||
{
|
||||
if (sr->packetReceivedFn)
|
||||
sr->packetReceivedFn(sr, ptr, len);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _check(const uint8_t *ptr, uint8_t len)
|
||||
{
|
||||
int i;
|
||||
uint8_t x=0;
|
||||
|
||||
for (i=0; i<len; i++, ptr++) {
|
||||
x^=*ptr;
|
||||
}
|
||||
if (x) {
|
||||
DBG_ERROR(NULL, "Bad checksum");
|
||||
GWEN_Text_DumpString(ptr, len, 6);
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t _calcChecksum(const uint8_t *ptr, uint8_t len)
|
||||
{
|
||||
int i;
|
||||
uint8_t x=0;
|
||||
|
||||
for (i=0; i<len; i++, ptr++) {
|
||||
x^=*ptr;
|
||||
}
|
||||
|
||||
return x;
|
||||
sr->packetReceivedFn(sr, msg);
|
||||
}
|
||||
|
||||
|
||||
@@ -410,135 +420,180 @@ uint8_t _calcChecksum(const uint8_t *ptr, uint8_t len)
|
||||
int _readFromFd(AQH_SERIAL *sr)
|
||||
{
|
||||
int rv;
|
||||
uint8_t pos;
|
||||
uint8_t *buf;
|
||||
uint8_t buffer[AQH_SERIAL_BUFFERSIZE*2];
|
||||
int len;
|
||||
int i;
|
||||
|
||||
pos=sr->readPos;
|
||||
buf=sr->readBuffer+pos;
|
||||
len=sr->bytesToRead;
|
||||
rv=(int) read(sr->fd, buf, len);
|
||||
if (rv<0) {
|
||||
if (errno!=EINTR) {
|
||||
DBG_ERROR(NULL, "Error on readFromFd(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
||||
do {
|
||||
if (!AQH_Serial_IsOpen(sr)) {
|
||||
DBG_ERROR(NULL, "Disconnected");
|
||||
return GWEN_ERROR_IO;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else if (rv==0) {
|
||||
DBG_ERROR(NULL, "EOF met on read(%s)", sr->deviceName);
|
||||
rv=read(sr->fd, buffer, sizeof(buffer));
|
||||
} while( (rv<0) && errno==EINTR);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "Error on read(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
||||
return GWEN_ERROR_IO;
|
||||
}
|
||||
else {
|
||||
sr->readPos+=rv;
|
||||
sr->bytesToRead-=rv;
|
||||
len=rv;
|
||||
|
||||
if (sr->readPos==2) {
|
||||
uint8_t i;
|
||||
|
||||
/* we have 2 bytes now, so we now the total msg len */
|
||||
i=sr->readBuffer[1];
|
||||
i++; /* remainder of message plus XOR byte */
|
||||
sr->bytesToRead=i;
|
||||
for (i=0; i<len; i++) {
|
||||
if (sr->currentlyReceivedMsg==NULL)
|
||||
sr->currentlyReceivedMsg=AQH_Msg_new();
|
||||
rv=AQH_Msg_AddByte(sr->currentlyReceivedMsg, buffer[i]);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
else {
|
||||
if (sr->bytesToRead==0) {
|
||||
/* msg received, check */
|
||||
if (_check(sr->readBuffer, sr->readPos)<0) {
|
||||
DBG_INFO(0, "here (%d)", rv);
|
||||
sr->readPos=0;
|
||||
sr->bytesToRead=2;
|
||||
return 0;
|
||||
}
|
||||
/* valid msg received, handle */
|
||||
AQH_Serial_PacketReceived(sr, sr->readBuffer, sr->readPos);
|
||||
sr->readPos=0;
|
||||
sr->bytesToRead=2;
|
||||
rv=AQH_Msg_IsMsgComplete(sr->currentlyReceivedMsg);
|
||||
if (rv<0) {
|
||||
/* invalid message */
|
||||
DBG_ERROR(NULL, "Invalid message, discarding");
|
||||
AQH_Msg_free(sr->currentlyReceivedMsg);
|
||||
sr->currentlyReceivedMsg=NULL;
|
||||
rv=_discardInput(sr);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
else if (rv>0) {
|
||||
if (!AQH_Msg_IsChecksumValid(sr->currentlyReceivedMsg)) {
|
||||
DBG_ERROR(NULL, "Invalid checksum, discarding message");
|
||||
AQH_Msg_free(sr->currentlyReceivedMsg);
|
||||
sr->currentlyReceivedMsg=NULL;
|
||||
rv=_discardInput(sr);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* valid msg received, handle */
|
||||
AQH_Serial_PacketReceived(sr, sr->currentlyReceivedMsg);
|
||||
//AQH_Msg_List_Add(sr->currentlyReceivedMsg, sr->receivedMessageList);
|
||||
sr->currentlyReceivedMsg=NULL;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _writeToFd(AQH_SERIAL *sr)
|
||||
{
|
||||
if (sr->bytesToWrite) {
|
||||
int rv;
|
||||
AQH_MSG *msg;
|
||||
|
||||
msg=AQH_Msg_List_First(sr->sendMessageList);
|
||||
if (msg) {
|
||||
uint8_t pos;
|
||||
uint8_t *buf;
|
||||
int len;
|
||||
|
||||
pos=sr->writePos;
|
||||
buf=sr->writeBuffer+pos;
|
||||
len=sr->bytesToWrite;
|
||||
rv=(int) write(sr->fd, buf, len);
|
||||
if (rv<0) {
|
||||
if (errno!=EINTR) {
|
||||
DBG_ERROR(NULL, "Error on writeToFd(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
||||
return GWEN_ERROR_IO;
|
||||
int remaining;
|
||||
int rv;
|
||||
|
||||
pos=AQH_Msg_GetCurrentPos(msg);
|
||||
remaining=AQH_Msg_GetRemainingBytes(msg);
|
||||
if (remaining>0) {
|
||||
const uint8_t *buf;
|
||||
|
||||
if (sr->intendedAttnState==1) {
|
||||
rv=_attnLow(sr);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
usleep(AQH_SERIAL_BYTE_MICROSECS/5);
|
||||
}
|
||||
|
||||
buf=AQH_Msg_GetBuffer(msg)+pos;
|
||||
do {
|
||||
if (!AQH_Serial_IsOpen(sr)) {
|
||||
DBG_ERROR(NULL, "Disconnected");
|
||||
return GWEN_ERROR_IO;
|
||||
}
|
||||
rv=write(sr->fd, buf, remaining);
|
||||
} while(rv<0 && errno==EINTR);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "Error on write(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
||||
return GWEN_ERROR_IO;
|
||||
}
|
||||
AQH_Msg_IncCurrentPos(msg, rv);
|
||||
if (rv==remaining) {
|
||||
rv=_attnHigh(sr);
|
||||
// TODO: callback msg sent
|
||||
AQH_Msg_List_Del(msg);
|
||||
AQH_Msg_free(msg);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
sr->writePos+=rv;
|
||||
sr->bytesToWrite-=rv;
|
||||
if (sr->bytesToWrite==0)
|
||||
_attnHigh(sr);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_Serial_StartWriting(AQH_SERIAL *sr, const uint8_t *ptr, uint8_t len)
|
||||
int _discardInput(AQH_SERIAL *sr)
|
||||
{
|
||||
int rv;
|
||||
uint8_t buffer[AQH_SERIAL_BUFFERSIZE];
|
||||
|
||||
do {
|
||||
if (!AQH_Serial_IsOpen(sr)) {
|
||||
DBG_ERROR(NULL, "Disconnected");
|
||||
return GWEN_ERROR_IO;
|
||||
}
|
||||
rv=read(sr->fd, buffer, sizeof(buffer));
|
||||
} while( (rv>0 || (rv<0) && errno==EINTR));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_Serial_AddMessageToSend(AQH_SERIAL *sr, AQH_MSG *msg)
|
||||
{
|
||||
int rv;
|
||||
|
||||
if (sr->bytesToWrite) {
|
||||
DBG_ERROR(NULL, "Write buffer in use");
|
||||
return GWEN_ERROR_TRY_AGAIN;
|
||||
rv=AQH_Msg_IsMsgComplete(msg);
|
||||
if (rv!=1) {
|
||||
DBG_ERROR(NULL, "Message not complete");
|
||||
return GWEN_ERROR_BAD_DATA;
|
||||
}
|
||||
|
||||
memmove(sr->writeBuffer, ptr, len);
|
||||
sr->bytesToWrite=len;
|
||||
sr->writePos=0;
|
||||
|
||||
rv=_attnLow(sr);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
if (!AQH_Msg_IsChecksumValid(msg)) {
|
||||
DBG_ERROR(NULL, "Checksum is invalid");
|
||||
return GWEN_ERROR_BAD_DATA;
|
||||
}
|
||||
|
||||
usleep(50);
|
||||
AQH_Msg_RewindCurrentPos(msg);
|
||||
AQH_Msg_List_Add(msg, sr->sendMessageList);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int AQH_Serial_Loop(AQH_SERIAL *sr)
|
||||
{
|
||||
fd_set readSet;
|
||||
fd_set writeSet;
|
||||
int somethingToWrite;
|
||||
struct timeval tv;
|
||||
int rv;
|
||||
|
||||
tv.tv_sec=2;
|
||||
tv.tv_usec=0;
|
||||
|
||||
if (sr->bytesToWrite) {
|
||||
somethingToWrite=(AQH_Msg_List_First(sr->sendMessageList)!=NULL)?1:0;
|
||||
if (somethingToWrite) {
|
||||
FD_ZERO(&writeSet);
|
||||
FD_SET(sr->fd, &writeSet);
|
||||
}
|
||||
FD_ZERO(&readSet);
|
||||
FD_SET(sr->fd, &readSet);
|
||||
|
||||
rv=select(sr->fd+1, &readSet, (sr->bytesToWrite)?(&writeSet):NULL, NULL, &tv);
|
||||
rv=select(sr->fd+1, &readSet, somethingToWrite?(&writeSet):NULL, NULL, &tv);
|
||||
if (rv<0) {
|
||||
if (errno!=EINTR) {
|
||||
DBG_ERROR(NULL, "Error on select");
|
||||
@@ -549,22 +604,29 @@ int AQH_Serial_Loop(AQH_SERIAL *sr)
|
||||
if (FD_ISSET(sr->fd, &readSet)) {
|
||||
rv=_readFromFd(sr);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
if (FD_ISSET(sr->fd, &writeSet)) {
|
||||
if (somethingToWrite && FD_ISSET(sr->fd, &writeSet)) {
|
||||
rv=_writeToFd(sr);
|
||||
if (rv<0) {
|
||||
DBG_INFO(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (rv==0) {
|
||||
/* timeout */
|
||||
sr->readPos=0;
|
||||
sr->bytesToRead=2;
|
||||
if (sr->currentlyReceivedMsg) {
|
||||
AQH_Msg_free(sr->currentlyReceivedMsg);
|
||||
sr->currentlyReceivedMsg=NULL;
|
||||
rv=_discardInput(sr);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user