717 lines
14 KiB
C
717 lines
14 KiB
C
/****************************************************************************
|
|
* This file is part of the project AqHome.
|
|
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
|
|
*
|
|
* The license for this file can be found in the file COPYING which you
|
|
* should have received along with this file.
|
|
****************************************************************************/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
# include <config.h>
|
|
#endif
|
|
|
|
|
|
#include "aqhome/serial_p.h"
|
|
|
|
#include <gwenhywfar/misc.h>
|
|
#include <gwenhywfar/debug.h>
|
|
#include <gwenhywfar/text.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
#include <fcntl.h>
|
|
#include <termios.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <stdlib.h>
|
|
#include <sys/time.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
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 _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)
|
|
{
|
|
AQH_SERIAL *sr;
|
|
|
|
GWEN_NEW_OBJECT(AQH_SERIAL, sr);
|
|
sr->deviceName=deviceName?strdup(deviceName):NULL;
|
|
sr->address=addr;
|
|
sr->fd=-1;
|
|
sr->intendedAttnState=1;
|
|
|
|
sr->receivedMessageList=AQH_Msg_List_new();
|
|
sr->sendMessageList=AQH_Msg_List_new();
|
|
return sr;
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
uint8_t AQH_Serial_GetAddress(const AQH_SERIAL *sr)
|
|
{
|
|
return sr->address;
|
|
}
|
|
|
|
|
|
|
|
int AQH_Serial_Open(AQH_SERIAL *sr, int rwMode)
|
|
{
|
|
int fd;
|
|
int status;
|
|
int i;
|
|
struct termios options;
|
|
int rv;
|
|
int m;
|
|
|
|
m=O_NOCTTY | O_NDELAY;
|
|
switch(rwMode) {
|
|
case AQH_SerialReadWriteMode_ReadOnly: m|=O_RDONLY; break;
|
|
case AQH_SerialReadWriteMode_WriteOnly: m|=O_WRONLY; break;
|
|
case AQH_SerialReadWriteMode_ReadWrite: m|=O_RDWR; break;
|
|
default: m|=O_RDONLY; break;
|
|
}
|
|
|
|
fd=open(sr->deviceName, m);
|
|
if (fd<0) {
|
|
DBG_ERROR(NULL, "Error on open(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
|
return GWEN_ERROR_IO;
|
|
}
|
|
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;
|
|
}
|
|
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, AQH_SERIAL_BAUDRATE);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "Error on cfsetospeed(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
|
return GWEN_ERROR_IO;
|
|
}
|
|
|
|
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) {
|
|
DBG_ERROR(NULL, "Error on tcsetattr(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
|
return GWEN_ERROR_IO;
|
|
}
|
|
|
|
sr->fd=fd;
|
|
|
|
_attnHigh(sr);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
void AQH_Serial_Close(AQH_SERIAL *sr)
|
|
{
|
|
if (sr->fd>=0) {
|
|
close (sr->fd);
|
|
sr->fd=-1;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int AQH_Serial_IsOpen(AQH_SERIAL *sr)
|
|
{
|
|
if (sr->fd<0) {
|
|
DBG_ERROR(NULL, "Device not open");
|
|
return 0;
|
|
}
|
|
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 0;
|
|
}
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int AQH_Serial_Recv(AQH_SERIAL *sr, uint8_t *buf, int len)
|
|
{
|
|
#if 0
|
|
int rv;
|
|
uint8_t *bufSaved;
|
|
int bytesReceived=0;
|
|
int msgLen;
|
|
|
|
bufSaved=buf;
|
|
|
|
/* destination, msg length */
|
|
rv=_readForced(sr, buf, 2);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
msgLen=buf[1];
|
|
buf+=2; /* two bytes already read */
|
|
|
|
/* read message and XOR byte */
|
|
rv=_readForced(sr, buf, msgLen+1); /* add one byte for XOR checksum */
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
buf+=msgLen;
|
|
|
|
bytesReceived=msgLen+3;
|
|
rv=_check(bufSaved, bytesReceived);
|
|
if (rv<0) {
|
|
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_ERROR(NULL, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
|
|
rv=_attnLow(sr);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
|
|
usleep(50);
|
|
|
|
rv=_writeForced(sr, ptr, len);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "here (%d)", rv);
|
|
_attnHigh(sr);
|
|
return rv;
|
|
}
|
|
|
|
_attnHigh(sr);
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
|
|
|
|
int AQH_Serial_SendPacket(AQH_SERIAL *sr, uint8_t destAddr, const uint8_t *ptr, uint8_t len)
|
|
{
|
|
int rv;
|
|
uint8_t x=0;
|
|
|
|
rv=_attnLow(sr);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
|
|
usleep(10);
|
|
|
|
/* send dest address */
|
|
rv=_writeForced(sr, &destAddr, 1);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "here (%d)", rv);
|
|
_attnHigh(sr);
|
|
return rv;
|
|
}
|
|
x^=destAddr;
|
|
|
|
/* send msg len */
|
|
rv=_writeForced(sr, &len, 1);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "here (%d)", rv);
|
|
_attnHigh(sr);
|
|
return rv;
|
|
}
|
|
x^=len;
|
|
|
|
if (len>0) {
|
|
uint8_t i;
|
|
|
|
/* send message */
|
|
rv=_writeForced(sr, ptr, len);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "here (%d)", rv);
|
|
_attnHigh(sr);
|
|
return rv;
|
|
}
|
|
|
|
for (i=0; i<len; i++)
|
|
x^=ptr[i];
|
|
}
|
|
|
|
/* send XOR checksum */
|
|
rv=_writeForced(sr, &x, 1);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "here (%d)", rv);
|
|
_attnHigh(sr);
|
|
return rv;
|
|
}
|
|
|
|
_attnHigh(sr);
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
void AQH_Serial_SetPacketReceivedFn(AQH_SERIAL *sr, AQH_SERIAL_PACKETRECEIVED_FN fn)
|
|
{
|
|
sr->packetReceivedFn=fn;
|
|
}
|
|
|
|
|
|
|
|
int _attnLow(AQH_SERIAL *sr)
|
|
{
|
|
int status;
|
|
int rv;
|
|
|
|
rv=ioctl(sr->fd, TIOCMGET, &status); /* GET the State of MODEM bits in Status */
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "Error on ioctl(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
|
return GWEN_ERROR_IO;
|
|
}
|
|
status |= TIOCM_DTR | TIOCM_RTS; /* clear the DTR pin (cave: signals inverted!) */
|
|
rv=ioctl(sr->fd, TIOCMSET, &status);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "Error on ioctl(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
|
return GWEN_ERROR_IO;
|
|
}
|
|
sr->intendedAttnState=0;
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
int _attnHigh(AQH_SERIAL *sr)
|
|
{
|
|
int status;
|
|
int rv;
|
|
|
|
rv=ioctl(sr->fd, TIOCMGET, &status);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "Error on ioctl(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
|
return GWEN_ERROR_IO;
|
|
}
|
|
status |= TIOCM_DTR; /* Set the DTR pin */
|
|
status &= ~ (TIOCM_DTR | TIOCM_RTS); /* clear the DTR pin (cave: signals inverted!) */
|
|
rv=ioctl(sr->fd, TIOCMSET, &status);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "Error on ioctl(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
|
return GWEN_ERROR_IO;
|
|
}
|
|
|
|
sr->intendedAttnState=1;
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
int _isAttnLow(AQH_SERIAL *sr)
|
|
{
|
|
int status;
|
|
int rv;
|
|
|
|
rv=ioctl(sr->fd, TIOCMGET, &status);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "Error on ioctl(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
|
return GWEN_ERROR_IO;
|
|
}
|
|
//return (status & TIOCM_CTS)?1:0;
|
|
return (status & TIOCM_CTS)?1:0;
|
|
}
|
|
|
|
|
|
|
|
int _readForced(AQH_SERIAL *sr, uint8_t *buf, int len)
|
|
{
|
|
while(len>0) {
|
|
int rv;
|
|
|
|
rv=(int) read(sr->fd, buf, len);
|
|
if (rv==0) {
|
|
DBG_ERROR(NULL, "EOF met on read(%s)", sr->deviceName);
|
|
return GWEN_ERROR_IO;
|
|
}
|
|
else if (rv<0) {
|
|
if (errno!=EINTR) {
|
|
DBG_ERROR(NULL, "Error on read(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
|
return GWEN_ERROR_IO;
|
|
}
|
|
}
|
|
else {
|
|
len-=rv;
|
|
buf+=rv;
|
|
}
|
|
} /* while */
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
int _writeForced(AQH_SERIAL *sr, const uint8_t *buf, uint8_t len)
|
|
{
|
|
while(len>0) {
|
|
int rv;
|
|
|
|
rv=(int) write(sr->fd, buf, len);
|
|
if (rv<0) {
|
|
if (errno!=EINTR) {
|
|
DBG_ERROR(NULL, "Error on write(%s): %s (%d)", sr->deviceName, strerror(errno), errno);
|
|
return GWEN_ERROR_IO;
|
|
}
|
|
}
|
|
else {
|
|
len-=rv;
|
|
buf+=rv;
|
|
}
|
|
} /* while */
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
void AQH_Serial_PacketReceived(AQH_SERIAL *sr, AQH_MSG *msg)
|
|
{
|
|
if (sr->packetReceivedFn)
|
|
sr->packetReceivedFn(sr, msg);
|
|
}
|
|
|
|
|
|
|
|
int _readFromFd(AQH_SERIAL *sr)
|
|
{
|
|
int rv;
|
|
uint8_t buffer[AQH_SERIAL_BUFFERSIZE*2];
|
|
int len;
|
|
int i;
|
|
|
|
do {
|
|
if (!AQH_Serial_IsOpen(sr)) {
|
|
DBG_ERROR(NULL, "Disconnected");
|
|
return GWEN_ERROR_IO;
|
|
}
|
|
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;
|
|
}
|
|
len=rv;
|
|
|
|
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;
|
|
}
|
|
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");
|
|
GWEN_Text_DumpString(AQH_Msg_GetBuffer(sr->currentlyReceivedMsg), AQH_Msg_GetBytesInBuffer(sr->currentlyReceivedMsg), 6);
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
int _writeToFd(AQH_SERIAL *sr)
|
|
{
|
|
AQH_MSG *msg;
|
|
|
|
msg=AQH_Msg_List_First(sr->sendMessageList);
|
|
if (msg) {
|
|
uint8_t pos;
|
|
int len;
|
|
int remaining;
|
|
int rv;
|
|
|
|
if (_isAttnLow(sr)) {
|
|
DBG_ERROR(NULL, "ATTN is low, not sending");
|
|
usleep(100);
|
|
return 0;
|
|
}
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
rv=AQH_Msg_IsMsgComplete(msg);
|
|
if (rv!=1) {
|
|
DBG_ERROR(NULL, "Message not complete");
|
|
return GWEN_ERROR_BAD_DATA;
|
|
}
|
|
if (!AQH_Msg_IsChecksumValid(msg)) {
|
|
DBG_ERROR(NULL, "Checksum is invalid");
|
|
return GWEN_ERROR_BAD_DATA;
|
|
}
|
|
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;
|
|
|
|
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, somethingToWrite?(&writeSet):NULL, NULL, &tv);
|
|
if (rv<0) {
|
|
if (errno!=EINTR) {
|
|
DBG_ERROR(NULL, "Error on select");
|
|
return GWEN_ERROR_IO;
|
|
}
|
|
}
|
|
else if (rv) {
|
|
if (FD_ISSET(sr->fd, &readSet)) {
|
|
rv=_readFromFd(sr);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
}
|
|
if (somethingToWrite && FD_ISSET(sr->fd, &writeSet)) {
|
|
rv=_writeToFd(sr);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
}
|
|
}
|
|
else if (rv==0) {
|
|
/* timeout */
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
int AQH_Serial_ReadOnlyLoop(AQH_SERIAL *sr)
|
|
{
|
|
fd_set readSet;
|
|
struct timeval tv;
|
|
int rv;
|
|
|
|
tv.tv_sec=2;
|
|
tv.tv_usec=0;
|
|
|
|
FD_ZERO(&readSet);
|
|
FD_SET(sr->fd, &readSet);
|
|
|
|
rv=select(sr->fd+1, &readSet, NULL, NULL, &tv);
|
|
if (rv<0) {
|
|
if (errno!=EINTR) {
|
|
DBG_ERROR(NULL, "Error on select");
|
|
return GWEN_ERROR_IO;
|
|
}
|
|
}
|
|
else if (rv) {
|
|
if (FD_ISSET(sr->fd, &readSet)) {
|
|
rv=_readFromFd(sr);
|
|
if (rv<0) {
|
|
DBG_ERROR(NULL, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
}
|
|
}
|
|
else if (rv==0) {
|
|
/* timeout */
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|