diff --git a/aqhome/0BUILD b/aqhome/0BUILD index 0a29de1..6dcdcaf 100644 --- a/aqhome/0BUILD +++ b/aqhome/0BUILD @@ -45,6 +45,7 @@ api.h + aqhome.h serial.h serial_p.h msg.h @@ -72,6 +73,7 @@ $(local/typefiles) + aqhome.c serial.c msg.c msg_value.c diff --git a/aqhome/api.h b/aqhome/api.h index 250b619..06e5ee2 100644 --- a/aqhome/api.h +++ b/aqhome/api.h @@ -10,6 +10,9 @@ #define AQH_API_H +#define AQH_LOGDOMAIN "aqhome" + + # ifdef BUILDING_AQHOME # /* building AqHome */ @@ -45,6 +48,5 @@ - #endif diff --git a/aqhome/aqhome.c b/aqhome/aqhome.c new file mode 100644 index 0000000..8c5e555 --- /dev/null +++ b/aqhome/aqhome.c @@ -0,0 +1,61 @@ +/**************************************************************************** + * 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 +#endif + +#include "aqhome/aqhome.h" + +#include +#include +#include + +#include + + + + +int AQH_Init() +{ + int rv; + const char *s; + + rv=GWEN_Init(); + if (rv) { + DBG_ERROR_ERR(AQH_LOGDOMAIN, rv); + return rv; + } + if (!GWEN_Logger_IsOpen(AQH_LOGDOMAIN)) + GWEN_Logger_Open(AQH_LOGDOMAIN, "aqhome", 0, GWEN_LoggerType_Console, GWEN_LoggerFacility_User); + + s=getenv("AQHOME_LOGLEVEL"); + if (s && *s) { + GWEN_LOGGER_LEVEL ll; + + ll=GWEN_Logger_Name2Level(s); + GWEN_Logger_SetLevel(AQH_LOGDOMAIN, ll); + } + else + GWEN_Logger_SetLevel(AQH_LOGDOMAIN, GWEN_LoggerLevel_Notice); + + return 0; +} + + + +int AQH_Fini() +{ + GWEN_Logger_Close(AQH_LOGDOMAIN); + GWEN_Fini(); + return 0; +} + + + + diff --git a/aqhome/aqhome.h b/aqhome/aqhome.h new file mode 100644 index 0000000..4ee5f78 --- /dev/null +++ b/aqhome/aqhome.h @@ -0,0 +1,23 @@ +/**************************************************************************** + * 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. + ****************************************************************************/ + +#ifndef AQH_AQHOME_H +#define AQH_AQHOME_H + + +#include + + +AQHOME_API int AQH_Init(); +AQHOME_API int AQH_Fini(); + + + + +#endif + diff --git a/aqhome/libtest.c b/aqhome/libtest.c index ff848cf..203a243 100644 --- a/aqhome/libtest.c +++ b/aqhome/libtest.c @@ -13,6 +13,11 @@ #include "aqhome/msg_haveaddr.h" #include "aqhome/msg_denyaddr.h" +#include "aqhome/aqhome.h" +#include "aqhome/msgendpointmanager.h" +#include "aqhome/msgendpointtty.h" +#include "aqhome/msgendpointlog.h" + #include #include #include @@ -372,6 +377,42 @@ int testReadLoop() +int testEndpoints() +{ + int rv; + AQH_MSG_ENDPOINT_MGR *emgr; + AQH_MSG_ENDPOINT *epTty; + AQH_MSG_ENDPOINT *epLog; + + rv=AQH_Init(); + if (rv<0) { + } + + emgr=AQH_MsgEndpointMgr_new(0xc0); + epTty=AQH_MsgEndpointTty_new("/dev/ttyUSB0"); + if (epTty==NULL) { + DBG_ERROR(NULL, "Error creating endpoint TTY"); + return 2; + } + AQH_MsgEndpointMgr_AddEndpoint(emgr, epTty); + + epLog=AQH_MsgEndpointLog_new("endpoints.log"); + if (epLog==NULL) { + DBG_ERROR(AQH_LOGDOMAIN, "Error creating endpoint LOG"); + return 2; + } + AQH_MsgEndpointMgr_AddEndpoint(emgr, epLog); + + for (;;) { + DBG_INFO(AQH_LOGDOMAIN, "Next loop"); + AQH_MsgEndpointMgr_LoopOnce(emgr); + } + + return 0; +} + + + int main(int argc, char **argv) @@ -389,6 +430,9 @@ int main(int argc, char **argv) else if (strcasecmp(cmd, "rwtest")==0) { return testLoop(); } + else if (strcasecmp(cmd, "endpoint")==0) { + return testEndpoints(); + } //return testRecv(); //return testSend(); return testLoop(); diff --git a/aqhome/msgendpoint.c b/aqhome/msgendpoint.c index ce20629..9d9a2cb 100644 --- a/aqhome/msgendpoint.c +++ b/aqhome/msgendpoint.c @@ -53,7 +53,7 @@ static int _setSocketNonBlocking(int fd); -AQH_MSG_ENDPOINT *AQH_MsgEndpoint_new(int fd, int groupId) +AQH_MSG_ENDPOINT *AQH_MsgEndpoint_new(int fd, int groupId, const char *name) { AQH_MSG_ENDPOINT *ep; @@ -64,6 +64,7 @@ AQH_MSG_ENDPOINT *AQH_MsgEndpoint_new(int fd, int groupId) ep->groupId=groupId; ep->receivedMessageList=AQH_Msg_List_new(); ep->sendMessageList=AQH_Msg_List_new(); + ep->name=name?strdup(name):""; return ep; } @@ -99,6 +100,14 @@ void AQH_MsgEndpoint_SetFd(AQH_MSG_ENDPOINT *ep, int fd) } + +const char *AQH_MsgEndpoint_GetName(const AQH_MSG_ENDPOINT *ep) +{ + return ep->name; +} + + + uint32_t AQH_MsgEndpoint_GetGroupId(const AQH_MSG_ENDPOINT *ep) { return ep->groupId; @@ -424,12 +433,14 @@ int AQH_MsgEndpoint_DiscardInput(AQH_MSG_ENDPOINT *ep) rv=read(ep->fd, buffer, sizeof(buffer)); } while( (rv>0 || (rv<0) && errno==EINTR)); if (rv<0 && errno!=EAGAIN && errno!=EWOULDBLOCK) { - DBG_ERROR(NULL, "Error on read(): %s (%d)", strerror(errno), errno); + DBG_ERROR(AQH_LOGDOMAIN, "Error on read(): %s (%d)", strerror(errno), errno); return GWEN_ERROR_IO; } else if (rv==0) { - DBG_ERROR(NULL, "EOF met on read()"); + DBG_ERROR(AQH_LOGDOMAIN, "EOF met on read()"); +#if 0 return GWEN_ERROR_IO; +#endif } return 0; } @@ -443,17 +454,18 @@ int _internalHandleReadable(AQH_MSG_ENDPOINT *ep) int len; int i; + DBG_INFO(AQH_LOGDOMAIN, "Reading from endpoint %s", AQH_MsgEndpoint_GetName(ep)); do { rv=read(ep->fd, buffer, sizeof(buffer)); } while( (rv<0) && errno==EINTR); if (rv<0) { if (errno==EAGAIN || errno==EWOULDBLOCK) return GWEN_ERROR_TRY_AGAIN; - DBG_ERROR(NULL, "Error on read(): %s (%d)", strerror(errno), errno); + DBG_ERROR(AQH_LOGDOMAIN, "Error on read(): %s (%d)", strerror(errno), errno); return GWEN_ERROR_IO; } else if (rv==0) { - DBG_ERROR(NULL, "EOF met on read()"); + DBG_ERROR(AQH_LOGDOMAIN, "EOF met on read()"); return GWEN_ERROR_IO; } len=rv; @@ -463,30 +475,30 @@ int _internalHandleReadable(AQH_MSG_ENDPOINT *ep) ep->currentlyReceivedMsg=AQH_Msg_new(); rv=AQH_Msg_AddByte(ep->currentlyReceivedMsg, buffer[i]); if (rv<0) { - DBG_ERROR(NULL, "here (%d)", rv); + DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv); return rv; } rv=AQH_Msg_IsMsgComplete(ep->currentlyReceivedMsg); if (rv<0) { /* invalid message */ - DBG_ERROR(NULL, "Invalid message, discarding"); + DBG_ERROR(AQH_LOGDOMAIN, "Invalid message, discarding"); AQH_Msg_free(ep->currentlyReceivedMsg); ep->currentlyReceivedMsg=NULL; rv=AQH_MsgEndpoint_DiscardInput(ep); if (rv<0) { - DBG_ERROR(NULL, "here (%d)", rv); + DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv); return rv; } } else if (rv>0) { if (!AQH_Msg_IsChecksumValid(ep->currentlyReceivedMsg)) { - DBG_ERROR(NULL, "Invalid checksum, discarding message"); + DBG_ERROR(AQH_LOGDOMAIN, "Invalid checksum, discarding message"); GWEN_Text_DumpString(AQH_Msg_GetBuffer(ep->currentlyReceivedMsg), AQH_Msg_GetBytesInBuffer(ep->currentlyReceivedMsg), 6); AQH_Msg_free(ep->currentlyReceivedMsg); ep->currentlyReceivedMsg=NULL; rv=AQH_MsgEndpoint_DiscardInput(ep); if (rv<0) { - DBG_ERROR(NULL, "here (%d)", rv); + DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv); return rv; } } @@ -507,6 +519,7 @@ int _internalHandleWritable(AQH_MSG_ENDPOINT *ep, AQH_MSG_ENDPOINT_MGR *emgr) { AQH_MSG *msg; + DBG_INFO(AQH_LOGDOMAIN, "Writing to endpoint %s", AQH_MsgEndpoint_GetName(ep)); msg=AQH_Msg_List_First(ep->sendMessageList); if (msg) { uint8_t pos; @@ -516,7 +529,7 @@ int _internalHandleWritable(AQH_MSG_ENDPOINT *ep, AQH_MSG_ENDPOINT_MGR *emgr) rv=AQH_MsgEndpoint_CheckMsg(ep); if (rv<0 || rv==1) { - DBG_ERROR(NULL, "Line busy, not sending"); + DBG_ERROR(AQH_LOGDOMAIN, "Line busy, not sending"); usleep(100); return 0; } @@ -527,7 +540,7 @@ int _internalHandleWritable(AQH_MSG_ENDPOINT *ep, AQH_MSG_ENDPOINT_MGR *emgr) rv=AQH_MsgEndpoint_StartMsg(ep); if (rv<0) { - DBG_ERROR(NULL, "here (%d)", rv); + DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv); return rv; } @@ -538,7 +551,7 @@ int _internalHandleWritable(AQH_MSG_ENDPOINT *ep, AQH_MSG_ENDPOINT_MGR *emgr) if (rv<0) { if (errno==EAGAIN || errno==EWOULDBLOCK) return GWEN_ERROR_TRY_AGAIN; - DBG_ERROR(NULL, "Error on write(): %s (%d)", strerror(errno), errno); + DBG_ERROR(AQH_LOGDOMAIN, "Error on write(): %s (%d)", strerror(errno), errno); return GWEN_ERROR_IO; } AQH_Msg_IncCurrentPos(msg, rv); @@ -548,7 +561,7 @@ int _internalHandleWritable(AQH_MSG_ENDPOINT *ep, AQH_MSG_ENDPOINT_MGR *emgr) AQH_Msg_List_Del(msg); AQH_Msg_free(msg); if (rv<0) { - DBG_ERROR(NULL, "here (%d)", rv); + DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv); return rv; } } diff --git a/aqhome/msgendpoint.h b/aqhome/msgendpoint.h index 63f0724..01e3a5a 100644 --- a/aqhome/msgendpoint.h +++ b/aqhome/msgendpoint.h @@ -58,12 +58,14 @@ typedef int (*AQH_MSG_ENDPOINT_CHECKMSG_FN)(AQH_MSG_ENDPOINT *ep); -AQHOME_API AQH_MSG_ENDPOINT *AQH_MsgEndpoint_new(int fd, int groupId); +AQHOME_API AQH_MSG_ENDPOINT *AQH_MsgEndpoint_new(int fd, int groupId, const char *name); AQHOME_API void AQH_MsgEndpoint_free(AQH_MSG_ENDPOINT *ep); AQHOME_API int AQH_MsgEndpoint_GetFd(const AQH_MSG_ENDPOINT *ep); AQHOME_API void AQH_MsgEndpoint_SetFd(AQH_MSG_ENDPOINT *ep, int fd); +AQHOME_API const char *AQH_MsgEndpoint_GetName(const AQH_MSG_ENDPOINT *ep); + AQHOME_API uint32_t AQH_MsgEndpoint_GetGroupId(const AQH_MSG_ENDPOINT *ep); AQHOME_API uint32_t AQH_MsgEndpoint_GetAcceptedMsgGroups(const AQH_MSG_ENDPOINT *ep); diff --git a/aqhome/msgendpoint_p.h b/aqhome/msgendpoint_p.h index 10b788f..1fdf23e 100644 --- a/aqhome/msgendpoint_p.h +++ b/aqhome/msgendpoint_p.h @@ -23,6 +23,8 @@ struct AQH_MSG_ENDPOINT { GWEN_LIST_ELEMENT(AQH_MSG_ENDPOINT) int fd; + char *name; + AQH_MSG_LIST *receivedMessageList; AQH_MSG_LIST *sendMessageList; AQH_MSG *currentlyReceivedMsg; diff --git a/aqhome/msgendpointlog.c b/aqhome/msgendpointlog.c index 29e4acc..69d22fa 100644 --- a/aqhome/msgendpointlog.c +++ b/aqhome/msgendpointlog.c @@ -47,7 +47,7 @@ AQH_MSG_ENDPOINT *AQH_MsgEndpointLog_new(const char *filename) AQH_MSG_ENDPOINT *ep; AQH_MSG_ENDPOINT_LOG *xep; - ep=AQH_MsgEndpoint_new(-1, AQH_MSG_ENDPOINT_ENDPOINTGROUP_NET); + ep=AQH_MsgEndpoint_new(-1, AQH_MSG_ENDPOINT_ENDPOINTGROUP_NET, "LOG"); GWEN_NEW_OBJECT(AQH_MSG_ENDPOINT_LOG, xep); xep->filename=strdup(filename); GWEN_INHERIT_SETDATA(AQH_MSG_ENDPOINT, AQH_MSG_ENDPOINT_LOG, ep, xep, _freeData); @@ -143,7 +143,7 @@ void _writeToLogFile(const char *filename, const char *txt) f=fopen(filename, "a+"); if (f) { if (1!=fwrite(txt, strlen(txt), 1, f)) { - DBG_ERROR(NULL, "Error logging."); + DBG_ERROR(AQH_LOGDOMAIN, "Error logging."); } fclose(f); } diff --git a/aqhome/msgendpointmanager.c b/aqhome/msgendpointmanager.c index c936160..1c22a67 100644 --- a/aqhome/msgendpointmanager.c +++ b/aqhome/msgendpointmanager.c @@ -110,42 +110,57 @@ int _ioLoopOnce(AQH_MSG_ENDPOINT_MGR *emgr) tv.tv_sec=2; tv.tv_usec=0; + DBG_INFO(AQH_LOGDOMAIN, "Sampling sockets"); ep=AQH_MsgEndpoint_List_First(emgr->endpointList); + if (ep==NULL) { + DBG_ERROR(AQH_LOGDOMAIN, "No endpoints."); + return GWEN_ERROR_GENERIC; + } while(ep) { + DBG_INFO(AQH_LOGDOMAIN, "- checking endpoint %s", AQH_MsgEndpoint_GetName(ep)); if (!(AQH_MsgEndpoint_GetFlags(ep) & AQH_MSG_ENDPOINT_FLAGS_NOIO)) { int fd; fd=AQH_MsgEndpoint_GetReadFd(ep); - if (fd!=-1) { + if (fd>=0) { + DBG_INFO(AQH_LOGDOMAIN, " - adding socket %d for read", fd); FD_SET(fd, &readSet); highestRdFd=(fd>highestRdFd)?fd:highestRdFd; } fd=AQH_MsgEndpoint_GetWriteFd(ep); - if (fd!=-1) { + if (fd>=0) { + DBG_INFO(AQH_LOGDOMAIN, " - adding socket %d for write", fd); FD_SET(fd, &writeSet); highestWrFd=(fd>highestWrFd)?fd:highestWrFd; } } + else { + DBG_INFO(AQH_LOGDOMAIN, " - endpoint %s does not support IO", AQH_MsgEndpoint_GetName(ep)); + } ep=AQH_MsgEndpoint_List_Next(ep); } + DBG_INFO(AQH_LOGDOMAIN, "Calling select (highest read socket: %d, highest write socket: %d)", highestRdFd, highestWrFd); rv=select(((highestRdFd>highestWrFd)?highestRdFd:highestWrFd)+1, (highestRdFd<0)?NULL:&readSet, (highestWrFd<0)?NULL:&writeSet, NULL, &tv); + DBG_INFO(AQH_LOGDOMAIN, "Return from select (%d, %d=%s)", rv, (rv<0)?errno:0, (rv<0)?strerror(errno):"no error"); if (rv<0) { if (errno!=EINTR) { - DBG_ERROR(NULL, "Error on select"); + DBG_ERROR(AQH_LOGDOMAIN, "Error on select"); return GWEN_ERROR_IO; } } else if (rv==0) { /* timeout */ + DBG_INFO(AQH_LOGDOMAIN, "timeout"); return GWEN_ERROR_TRY_AGAIN; } else if (rv) { + DBG_INFO(AQH_LOGDOMAIN, "Letting all endpoints handle IO"); ep=AQH_MsgEndpoint_List_First(emgr->endpointList); while(ep) { AQH_MSG_ENDPOINT *epNext; @@ -155,15 +170,19 @@ int _ioLoopOnce(AQH_MSG_ENDPOINT_MGR *emgr) epNext=AQH_MsgEndpoint_List_Next(ep); fd=AQH_MsgEndpoint_GetFd(ep); if (fd!=-1 && FD_ISSET(fd, &readSet)) { + DBG_INFO(AQH_LOGDOMAIN, "- endpoint(%s): read", AQH_MsgEndpoint_GetName(ep)); rv=AQH_MsgEndpoint_HandleReadable(ep, emgr); if (rv<0 && rv!=GWEN_ERROR_TRY_AGAIN) { + DBG_INFO(AQH_LOGDOMAIN, "error, removing endpoint %s", AQH_MsgEndpoint_GetName(ep)); fd=-1; AQH_MsgEndpointMgr_DelEndpoint(emgr, ep); } } if (fd!=-1 && FD_ISSET(fd, &writeSet)) { + DBG_INFO(AQH_LOGDOMAIN, "- endpoint(%s): write", AQH_MsgEndpoint_GetName(ep)); rv=AQH_MsgEndpoint_HandleWritable(ep, emgr); if (rv<0 && rv!=GWEN_ERROR_TRY_AGAIN) { + DBG_INFO(AQH_LOGDOMAIN, "error, removing endpoint %s", AQH_MsgEndpoint_GetName(ep)); fd=-1; AQH_MsgEndpointMgr_DelEndpoint(emgr, ep); } @@ -181,21 +200,30 @@ void _msgLoopOnce(AQH_MSG_ENDPOINT_MGR *emgr) { AQH_MSG_ENDPOINT *ep; + DBG_INFO(AQH_LOGDOMAIN, "Handle endpoint messages"); ep=AQH_MsgEndpoint_List_First(emgr->endpointList); while(ep) { AQH_MSG *msg; + DBG_INFO(AQH_LOGDOMAIN, "- endpoint(%s)", AQH_MsgEndpoint_GetName(ep)); while( (msg=AQH_MsgEndpoint_TakeFirstReceivedMessage(ep)) ) { uint32_t msgGroup; + DBG_INFO(AQH_LOGDOMAIN, + " - msg %d from %d to %d", + AQH_Msg_GetMsgType(msg), AQH_Msg_GetSourceAddress(msg), AQH_Msg_GetDestAddress(msg)); msgGroup=_getMsgGroup(AQH_Msg_GetMsgType(msg)); if (msgGroup & AQH_MSG_ENDPOINT_MSGGROUP_ADMIN) { - if (!(AQH_MsgEndpoint_GetGroupId(ep) & AQH_MSG_ENDPOINT_ENDPOINTGROUP_BUS)) + if (!(AQH_MsgEndpoint_GetGroupId(ep) & AQH_MSG_ENDPOINT_ENDPOINTGROUP_BUS)) { /* only handle admin messages not from nodes */ + DBG_INFO(AQH_LOGDOMAIN, " - handling admin message"); _handleAdminMsg(emgr, ep, msg); + } } - else + else { + DBG_INFO(AQH_LOGDOMAIN, " - distributing message"); _distributeMsg(emgr, ep, msg); + } AQH_Msg_free(msg); } @@ -209,11 +237,13 @@ void _runAllEndpoints(AQH_MSG_ENDPOINT_MGR *emgr) { AQH_MSG_ENDPOINT *ep; + DBG_INFO(AQH_LOGDOMAIN, "Running all endpoints"); ep=AQH_MsgEndpoint_List_First(emgr->endpointList); while(ep) { AQH_MSG_ENDPOINT *next; next=AQH_MsgEndpoint_List_Next(ep); + DBG_INFO(AQH_LOGDOMAIN, "- running endpoint %s", AQH_MsgEndpoint_GetName(ep)); AQH_MsgEndpoint_Run(ep); ep=next; } @@ -236,6 +266,7 @@ void _distributeMsg(AQH_MSG_ENDPOINT_MGR *emgr, AQH_MSG_ENDPOINT *srcEp, const A uint32_t acceptedGroupIds; uint32_t acceptedMsgGroups; + DBG_INFO(AQH_LOGDOMAIN, "- checking endpoint %s", AQH_MsgEndpoint_GetName(ep)); acceptedGroupIds=AQH_MsgEndpoint_GetAcceptedMsgGroups(ep); acceptedMsgGroups=AQH_MsgEndpoint_GetAcceptedMsgGroups(ep); @@ -245,8 +276,12 @@ void _distributeMsg(AQH_MSG_ENDPOINT_MGR *emgr, AQH_MSG_ENDPOINT *srcEp, const A (acceptedGroupIds & srcGroupId) ) { /* endpoint accepts this message */ + DBG_INFO(AQH_LOGDOMAIN, " - endpoint %s accepts message", AQH_MsgEndpoint_GetName(ep)); AQH_MsgEndpoint_AddSendMessage(ep, AQH_Msg_dup(msg)); } + else { + DBG_INFO(AQH_LOGDOMAIN, " - endpoint %s does not accept message", AQH_MsgEndpoint_GetName(ep)); + } } ep=AQH_MsgEndpoint_List_Next(ep); } diff --git a/aqhome/msgendpointtcp.c b/aqhome/msgendpointtcp.c index 482fc9a..c98754e 100644 --- a/aqhome/msgendpointtcp.c +++ b/aqhome/msgendpointtcp.c @@ -56,7 +56,7 @@ AQH_MSG_ENDPOINT *AQH_MsgEndpointTcp_new(const char *host, int port) return NULL; } - ep=AQH_MsgEndpoint_new(fd, AQH_MSG_ENDPOINT_ENDPOINTGROUP_NET); + ep=AQH_MsgEndpoint_new(fd, AQH_MSG_ENDPOINT_ENDPOINTGROUP_NET, "TCP Server"); AQH_MsgEndpoint_SetAcceptedEndpointGroups(ep, AQH_MSG_ENDPOINT_ENDPOINTGROUP_BUS); AQH_MsgEndpoint_AddFlags(ep, AQH_MSG_ENDPOINT_FLAGS_NOMESSAGES); @@ -88,13 +88,13 @@ int _setupListeningSocket(const char *host, int port) sk=socket(AF_INET, SOCK_STREAM, 0); if (sk<0) { /* socket error */ - DBG_ERROR(NULL, "socket(): %s", strerror(errno)); + DBG_ERROR(AQH_LOGDOMAIN, "socket(): %s", strerror(errno)); } i=1; rv=setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)); if (rv<0) { - DBG_ERROR(NULL, "setsockopt(): %s", strerror(errno)); + DBG_ERROR(AQH_LOGDOMAIN, "setsockopt(): %s", strerror(errno)); close(sk); return GWEN_ERROR_IO; } @@ -109,14 +109,14 @@ int _setupListeningSocket(const char *host, int port) rv=bind(sk, (struct sockaddr*) &addr, sizeof(addr)); if (rv<0) { - DBG_ERROR(NULL, "bind(): %s", strerror(errno)); + DBG_ERROR(AQH_LOGDOMAIN, "bind(): %s", strerror(errno)); close(sk); return GWEN_ERROR_IO; } rv=listen(sk, AQH_MSG_ENDPOINTTCP_BACKLOG); if (rv<0) { - DBG_ERROR(NULL, "listen(): %s", strerror(errno)); + DBG_ERROR(AQH_LOGDOMAIN, "listen(): %s", strerror(errno)); close(sk); return GWEN_ERROR_IO; } @@ -134,7 +134,7 @@ int _setSocketNonBlocking(int fd) /* get current socket flags */ prevFlags=fcntl(fd, F_GETFL); if (prevFlags==-1) { - DBG_ERROR(NULL, "fcntl(): %s", strerror(errno)); + DBG_ERROR(AQH_LOGDOMAIN, "fcntl(): %s", strerror(errno)); return GWEN_ERROR_IO; } @@ -142,7 +142,7 @@ int _setSocketNonBlocking(int fd) newFlags=prevFlags|O_NONBLOCK; if (-1==fcntl(fd, F_SETFL, newFlags)) { - DBG_ERROR(NULL, "fcntl(): %s", strerror(errno)); + DBG_ERROR(AQH_LOGDOMAIN, "fcntl(): %s", strerror(errno)); return GWEN_ERROR_IO; } @@ -184,7 +184,7 @@ int _handleReadable(AQH_MSG_ENDPOINT *ep, AQH_MSG_ENDPOINT_MGR *emgr) if (rv<0) { if (errno==EAGAIN || errno==EWOULDBLOCK) return GWEN_ERROR_TRY_AGAIN; - DBG_ERROR(NULL, "Error on accept(): %s (%d)", strerror(errno), errno); + DBG_ERROR(AQH_LOGDOMAIN, "Error on accept(): %s (%d)", strerror(errno), errno); return GWEN_ERROR_IO; } newSock=rv; @@ -196,7 +196,7 @@ int _handleReadable(AQH_MSG_ENDPOINT *ep, AQH_MSG_ENDPOINT_MGR *emgr) return rv; } - newEp=AQH_MsgEndpoint_new(newSock, AQH_MsgEndpoint_GetGroupId(ep)); + newEp=AQH_MsgEndpoint_new(newSock, AQH_MsgEndpoint_GetGroupId(ep), "TCP Client"); AQH_MsgEndpoint_SetFlags(newEp, AQH_MsgEndpoint_GetFlags(ep)); AQH_MsgEndpoint_SubFlags(newEp, AQH_MSG_ENDPOINT_FLAGS_NOMESSAGES); AQH_MsgEndpoint_SetAcceptedMsgGroups(newEp, AQH_MsgEndpoint_GetAcceptedMsgGroups(ep)); diff --git a/aqhome/msgendpointtty.c b/aqhome/msgendpointtty.c index a6b1a21..456c7bd 100644 --- a/aqhome/msgendpointtty.c +++ b/aqhome/msgendpointtty.c @@ -38,7 +38,7 @@ static int _startMsg(AQH_MSG_ENDPOINT *ep); static int _endMsg(AQH_MSG_ENDPOINT *ep); static int _checkMsg(AQH_MSG_ENDPOINT *ep); -static int _setupDevice(AQH_MSG_ENDPOINT *ep); +static int _openDevice(AQH_MSG_ENDPOINT *ep); static int _attnLow(AQH_MSG_ENDPOINT *ep); static int _attnHigh(AQH_MSG_ENDPOINT *ep); static int _isAttnLow(AQH_MSG_ENDPOINT *ep); @@ -53,32 +53,27 @@ AQH_MSG_ENDPOINT *AQH_MsgEndpointTty_new(const char *deviceName) AQH_MSG_ENDPOINT *ep; AQH_MSG_ENDPOINT_TTY *xep; int fd; - int rv; - fd=open(deviceName, O_NOCTTY | O_NDELAY | O_RDWR); - if (fd<0) { - DBG_ERROR(NULL, "Error on open(%s): %s (%d)", deviceName, strerror(errno), errno); - return NULL; - } - - ep=AQH_MsgEndpoint_new(fd, AQH_MSG_ENDPOINT_ENDPOINTGROUP_BUS); + ep=AQH_MsgEndpoint_new(-1, AQH_MSG_ENDPOINT_ENDPOINTGROUP_BUS, "TTY"); AQH_MsgEndpoint_SetAcceptedEndpointGroups(ep, AQH_MSG_ENDPOINT_ENDPOINTGROUP_NET); + AQH_MsgEndpoint_SetAcceptedMsgGroups(ep, AQH_MSG_ENDPOINT_MSGGROUP_ALL); GWEN_NEW_OBJECT(AQH_MSG_ENDPOINT_TTY, xep); GWEN_INHERIT_SETDATA(AQH_MSG_ENDPOINT, AQH_MSG_ENDPOINT_TTY, ep, xep, _freeData); xep->deviceName=strdup(deviceName); - _attnHigh(ep); AQH_MsgEndpoint_SetStartMsgFn(ep, _startMsg); AQH_MsgEndpoint_SetEndMsgFn(ep, _endMsg); AQH_MsgEndpoint_SetCheckMsgFn(ep, _checkMsg); - rv=_setupDevice(ep); - if (rv<0) { - DBG_INFO(NULL, "here (%d)", rv); + fd=_openDevice(ep); + if (fd<0) { + DBG_INFO(NULL, "here (%d)", fd); AQH_MsgEndpoint_free(ep); return NULL; } + AQH_MsgEndpoint_SetFd(ep, fd); + _attnHigh(ep); return ep; } @@ -112,7 +107,7 @@ int _startMsg(AQH_MSG_ENDPOINT *ep) rv=_attnLow(ep); if (rv<0) { - DBG_ERROR(NULL, "here (%d)", rv); + DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv); return rv; } usleep(AQH_MSG_ENDPOINT_TTY_BYTE_MICROSECS/5); @@ -137,7 +132,7 @@ int _checkMsg(AQH_MSG_ENDPOINT *ep) -int _setupDevice(AQH_MSG_ENDPOINT *ep) +int _openDevice(AQH_MSG_ENDPOINT *ep) { AQH_MSG_ENDPOINT_TTY *xep; int fd; @@ -148,10 +143,14 @@ int _setupDevice(AQH_MSG_ENDPOINT *ep) int m; xep=GWEN_INHERIT_GETDATA(AQH_MSG_ENDPOINT, AQH_MSG_ENDPOINT_TTY, ep); - fd=AQH_MsgEndpoint_GetFd(ep); + fd=open(xep->deviceName, O_NOCTTY | O_NDELAY | O_RDWR); + if (fd<0) { + DBG_ERROR(AQH_LOGDOMAIN, "Error on open(%s): %s (%d)", xep->deviceName, strerror(errno), errno); + return GWEN_ERROR_IO; + } rv=tcgetattr(fd, &(xep->previousOptions)); if (rv<0) { - DBG_ERROR(NULL, "Error on tcgetattr(%s): %s (%d)", xep->deviceName, strerror(errno), errno); + DBG_ERROR(AQH_LOGDOMAIN, "Error on tcgetattr(%s): %s (%d)", xep->deviceName, strerror(errno), errno); return GWEN_ERROR_IO; } memset(&options, 0, sizeof(options)); /* preset */ @@ -166,30 +165,28 @@ int _setupDevice(AQH_MSG_ENDPOINT *ep) rv=cfsetispeed(&options, AQH_MSG_ENDPOINT_TTY_BAUDRATE); if (rv<0) { - DBG_ERROR(NULL, "Error on cfsetispeed(%s): %s (%d)", xep->deviceName, strerror(errno), errno); + DBG_ERROR(AQH_LOGDOMAIN, "Error on cfsetispeed(%s): %s (%d)", xep->deviceName, strerror(errno), errno); return GWEN_ERROR_IO; } rv=cfsetospeed(&options, AQH_MSG_ENDPOINT_TTY_BAUDRATE); if (rv<0) { - DBG_ERROR(NULL, "Error on cfsetospeed(%s): %s (%d)", xep->deviceName, strerror(errno), errno); + DBG_ERROR(AQH_LOGDOMAIN, "Error on cfsetospeed(%s): %s (%d)", xep->deviceName, strerror(errno), errno); return GWEN_ERROR_IO; } rv=tcflush(fd, TCIOFLUSH); if (rv<0) { - DBG_ERROR(NULL, "Error on tcflush(%s): %s (%d)", xep->deviceName, strerror(errno), errno); + DBG_ERROR(AQH_LOGDOMAIN, "Error on tcflush(%s): %s (%d)", xep->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)", xep->deviceName, strerror(errno), errno); + DBG_ERROR(AQH_LOGDOMAIN, "Error on tcsetattr(%s): %s (%d)", xep->deviceName, strerror(errno), errno); return GWEN_ERROR_IO; } - _attnHigh(ep); - - return 0; + return fd; } @@ -205,13 +202,13 @@ int _attnLow(AQH_MSG_ENDPOINT *ep) fd=AQH_MsgEndpoint_GetFd(ep); rv=ioctl(fd, TIOCMGET, &status); /* GET the State of MODEM bits in Status */ if (rv<0) { - DBG_ERROR(NULL, "Error on ioctl(%s): %s (%d)", xep->deviceName, strerror(errno), errno); + DBG_ERROR(AQH_LOGDOMAIN, "Error on ioctl(%s): %s (%d)", xep->deviceName, strerror(errno), errno); return GWEN_ERROR_IO; } status |= TIOCM_DTR | TIOCM_RTS; /* clear the DTR pin (cave: signals inverted!) */ rv=ioctl(fd, TIOCMSET, &status); if (rv<0) { - DBG_ERROR(NULL, "Error on ioctl(%s): %s (%d)", xep->deviceName, strerror(errno), errno); + DBG_ERROR(AQH_LOGDOMAIN, "Error on ioctl(%s): %s (%d)", xep->deviceName, strerror(errno), errno); return GWEN_ERROR_IO; } xep->intendedAttnState=0; @@ -231,14 +228,14 @@ int _attnHigh(AQH_MSG_ENDPOINT *ep) fd=AQH_MsgEndpoint_GetFd(ep); rv=ioctl(fd, TIOCMGET, &status); if (rv<0) { - DBG_ERROR(NULL, "Error on ioctl(%s): %s (%d)", xep->deviceName, strerror(errno), errno); + DBG_ERROR(AQH_LOGDOMAIN, "Error on ioctl(%s): %s (%d)", xep->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(fd, TIOCMSET, &status); if (rv<0) { - DBG_ERROR(NULL, "Error on ioctl(%s): %s (%d)", xep->deviceName, strerror(errno), errno); + DBG_ERROR(AQH_LOGDOMAIN, "Error on ioctl(%s): %s (%d)", xep->deviceName, strerror(errno), errno); return GWEN_ERROR_IO; } @@ -259,7 +256,7 @@ int _isAttnLow(AQH_MSG_ENDPOINT *ep) fd=AQH_MsgEndpoint_GetFd(ep); rv=ioctl(fd, TIOCMGET, &status); if (rv<0) { - DBG_ERROR(NULL, "Error on ioctl(%s): %s (%d)", xep->deviceName, strerror(errno), errno); + DBG_ERROR(AQH_LOGDOMAIN, "Error on ioctl(%s): %s (%d)", xep->deviceName, strerror(errno), errno); return GWEN_ERROR_IO; } //return (status & TIOCM_CTS)?1:0;