diff --git a/aqhome/serial.c b/aqhome/serial.c index 1281459..ac3599f 100644 --- a/aqhome/serial.c +++ b/aqhome/serial.c @@ -15,6 +15,7 @@ #include #include +#include #include @@ -116,6 +117,9 @@ int AQH_Serial_Open(AQH_SERIAL *sr) } sr->fd=fd; + + _attnHigh(sr); + return 0; } @@ -380,6 +384,7 @@ int _check(const uint8_t *ptr, uint8_t len) } if (x) { DBG_ERROR(NULL, "Bad checksum"); + GWEN_Text_DumpString(ptr, len, 2); return GWEN_ERROR_GENERIC; } @@ -438,7 +443,14 @@ int _readFromFd(AQH_SERIAL *sr) } else { if (sr->bytesToRead==0) { - /* msg received, handle */ + /* 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; @@ -502,7 +514,7 @@ int AQH_Serial_StartWriting(AQH_SERIAL *sr, const uint8_t *ptr, uint8_t len) return rv; } - usleep(10); + usleep(50); return 0; } @@ -516,7 +528,7 @@ int AQH_Serial_Loop(AQH_SERIAL *sr) struct timeval tv; int rv; - tv.tv_sec=5; + tv.tv_sec=2; tv.tv_usec=0; if (sr->bytesToWrite) { @@ -528,6 +540,10 @@ int AQH_Serial_Loop(AQH_SERIAL *sr) rv=select(sr->fd+1, &readSet, (sr->bytesToWrite)?(&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)) { @@ -545,6 +561,11 @@ int AQH_Serial_Loop(AQH_SERIAL *sr) } } } + else if (rv==0) { + /* timeout */ + sr->readPos=0; + sr->bytesToRead=2; + } return 0; }