Files
aqhomecontrol/aqhome/serial.c
2023-01-26 18:58:32 +01:00

578 lines
11 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 _check(const uint8_t *ptr, uint8_t len);
uint8_t _calcChecksum(const uint8_t *ptr, uint8_t len);
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->bytesToRead=2;
return sr;
}
void AQH_Serial_free(AQH_SERIAL *sr)
{
if (sr) {
free(sr->deviceName);
GWEN_FREE_OBJECT(sr);
}
}
int AQH_Serial_Open(AQH_SERIAL *sr)
{
int fd;
int status;
int i;
struct termios options;
int rv;
fd=open(sr->deviceName,O_RDWR | O_NOCTTY );
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);
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);
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);
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=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_Recv(AQH_SERIAL *sr, uint8_t *buf, int len)
{
int rv;
uint8_t *bufSaved;
int bytesReceived=0;
int msgLen;
bufSaved=buf;
/* destination, msg length */
rv=_readForced(sr, buf, 2);
if (rv<0) {
DBG_INFO(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_INFO(NULL, "here (%d)", rv);
return rv;
}
buf+=msgLen;
bytesReceived=msgLen+3;
rv=_check(bufSaved, bytesReceived);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
}
return bytesReceived;
}
int AQH_Serial_Send(AQH_SERIAL *sr, const uint8_t *ptr, uint8_t len)
{
int rv;
rv=_check(ptr, len);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
}
rv=_attnLow(sr);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
}
usleep(50);
rv=_writeForced(sr, ptr, len);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
_attnHigh(sr);
return rv;
}
_attnHigh(sr);
return 0;
}
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_INFO(NULL, "here (%d)", rv);
return rv;
}
usleep(10);
/* send dest address */
rv=_writeForced(sr, &destAddr, 1);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
_attnHigh(sr);
return rv;
}
x^=destAddr;
/* send msg len */
rv=_writeForced(sr, &len, 1);
if (rv<0) {
DBG_INFO(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_INFO(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_INFO(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; /* 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;
}
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; /* 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;
}
return 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, const uint8_t *ptr, uint8_t len)
{
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, 2);
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;
}
int _readFromFd(AQH_SERIAL *sr)
{
int rv;
uint8_t pos;
uint8_t *buf;
int len;
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);
return GWEN_ERROR_IO;
}
return 0;
}
else if (rv==0) {
DBG_ERROR(NULL, "EOF met on read(%s)", sr->deviceName);
return GWEN_ERROR_IO;
}
else {
sr->readPos+=rv;
sr->bytesToRead-=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;
}
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;
}
}
return 0;
}
}
int _writeToFd(AQH_SERIAL *sr)
{
if (sr->bytesToWrite) {
int rv;
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;
}
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 rv;
if (sr->bytesToWrite) {
DBG_ERROR(NULL, "Write buffer in use");
return GWEN_ERROR_TRY_AGAIN;
}
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;
}
usleep(50);
return 0;
}
int AQH_Serial_Loop(AQH_SERIAL *sr)
{
fd_set readSet;
fd_set writeSet;
struct timeval tv;
int rv;
tv.tv_sec=2;
tv.tv_usec=0;
if (sr->bytesToWrite) {
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);
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_INFO(NULL, "here (%d)", rv);
return rv;
}
}
if (FD_ISSET(sr->fd, &writeSet)) {
rv=_writeToFd(sr);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
}
}
}
else if (rv==0) {
/* timeout */
sr->readPos=0;
sr->bytesToRead=2;
}
return 0;
}