From f9f52d786a01928b3eb9c6ddfacd67a9b61703fc Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Mon, 20 Feb 2023 23:45:39 +0100 Subject: [PATCH] Work on now deprecated module AQH_Serial. --- aqhome/serial.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++--- aqhome/serial.h | 10 +++++- 2 files changed, 89 insertions(+), 5 deletions(-) diff --git a/aqhome/serial.c b/aqhome/serial.c index a09a873..1b80e5d 100644 --- a/aqhome/serial.c +++ b/aqhome/serial.c @@ -82,15 +82,24 @@ uint8_t AQH_Serial_GetAddress(const AQH_SERIAL *sr) -int AQH_Serial_Open(AQH_SERIAL *sr) +int AQH_Serial_Open(AQH_SERIAL *sr, int rwMode) { int fd; int status; int i; struct termios options; int rv; + int m; - fd=open(sr->deviceName,O_RDWR | O_NOCTTY | O_NDELAY); + 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; @@ -323,7 +332,7 @@ int _attnLow(AQH_SERIAL *sr) DBG_ERROR(NULL, "Error on ioctl(%s): %s (%d)", sr->deviceName, strerror(errno), errno); return GWEN_ERROR_IO; } - status |= TIOCM_DTR; /* clear the DTR pin (cave: signals inverted!) */ + 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); @@ -346,7 +355,7 @@ int _attnHigh(AQH_SERIAL *sr) return GWEN_ERROR_IO; } status |= TIOCM_DTR; /* Set the DTR pin */ - status &= ~ TIOCM_DTR; /* clear the DTR pin (cave: signals inverted!) */ + 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); @@ -359,6 +368,22 @@ int _attnHigh(AQH_SERIAL *sr) +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) { @@ -494,6 +519,11 @@ int _writeToFd(AQH_SERIAL *sr) 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) { @@ -635,6 +665,52 @@ int AQH_Serial_Loop(AQH_SERIAL *sr) +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; +} + + + diff --git a/aqhome/serial.h b/aqhome/serial.h index ba953ed..b867664 100644 --- a/aqhome/serial.h +++ b/aqhome/serial.h @@ -28,6 +28,13 @@ typedef struct AQH_SERIAL AQH_SERIAL; typedef void (*AQH_SERIAL_PACKETRECEIVED_FN)(AQH_SERIAL *sr, AQH_MSG *msg); +enum { + AQH_SerialReadWriteMode_ReadOnly=1, + AQH_SerialReadWriteMode_WriteOnly=2, + AQH_SerialReadWriteMode_ReadWrite=3 +}; + + AQHOME_API AQH_SERIAL *AQH_Serial_new(const char *deviceName, uint8_t addr); AQHOME_API void AQH_Serial_free(AQH_SERIAL *sr); @@ -35,7 +42,7 @@ AQHOME_API void AQH_Serial_free(AQH_SERIAL *sr); AQHOME_API uint8_t AQH_Serial_GetAddress(const AQH_SERIAL *sr); -AQHOME_API int AQH_Serial_Open(AQH_SERIAL *sr); +AQHOME_API int AQH_Serial_Open(AQH_SERIAL *sr, int rwMode); AQHOME_API void AQH_Serial_Close(AQH_SERIAL *sr); AQHOME_API int AQH_Serial_Recv(AQH_SERIAL *sr, uint8_t *buf, int len); @@ -45,6 +52,7 @@ AQHOME_API int AQH_Serial_SendPacket(AQH_SERIAL *sr, uint8_t destAddr, const uin AQHOME_API int AQH_Serial_Loop(AQH_SERIAL *sr); +AQHOME_API int AQH_Serial_ReadOnlyLoop(AQH_SERIAL *sr); AQHOME_API int AQH_Serial_AddMessageToSend(AQH_SERIAL *sr, AQH_MSG *msg); AQHOME_API int AQH_Serial_StartWriting(AQH_SERIAL *sr, const uint8_t *ptr, uint8_t len);