From 20efe7d4730a0b1c3f142cf2f7eb3e7445827317 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Wed, 19 Apr 2023 15:39:15 +0200 Subject: [PATCH] aqhome-tool: recorganized code for improved reusability. --- apps/aqhome-tool/0BUILD | 2 + apps/aqhome-tool/ping.c | 101 ++-------------------------------- apps/aqhome-tool/utils.c | 113 +++++++++++++++++++++++++++++++++++++++ apps/aqhome-tool/utils.h | 29 ++++++++++ 4 files changed, 148 insertions(+), 97 deletions(-) create mode 100644 apps/aqhome-tool/utils.c create mode 100644 apps/aqhome-tool/utils.h diff --git a/apps/aqhome-tool/0BUILD b/apps/aqhome-tool/0BUILD index ac83941..32b31e1 100644 --- a/apps/aqhome-tool/0BUILD +++ b/apps/aqhome-tool/0BUILD @@ -34,12 +34,14 @@ ping.h + utils.h $(local/typefiles) main.c ping.c + utils.c diff --git a/apps/aqhome-tool/ping.c b/apps/aqhome-tool/ping.c index a665a91..31bc183 100644 --- a/apps/aqhome-tool/ping.c +++ b/apps/aqhome-tool/ping.c @@ -11,6 +11,7 @@ #endif #include "./ping.h" +#include "./utils.h" #include "aqhome/ipc/msg_ipc_setaccmsggrps.h" #include "aqhome/ipc/msg_ipc_ping.h" @@ -34,13 +35,7 @@ static int _doPing(GWEN_DB_NODE *dbArgs); -static int _sendAcceptedMsgGroups(GWEN_MSG_ENDPOINT *epTcp); static int _sendPing(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr); -static GWEN_MSG *_waitForSpecificNodeMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, - int msgCode, - int nodeAddr, - int timeoutInSeconds); -static GWEN_MSG_ENDPOINT *_setupIpcEndpoint(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_DB_NODE *dbArgs); @@ -144,7 +139,7 @@ int _doPing(GWEN_DB_NODE *dbArgs) emgr=GWEN_MsgEndpointMgr_new(); - epTcp=_setupIpcEndpoint(emgr, dbArgs); + epTcp=Utils_SetupIpcEndpoint(emgr, dbArgs); if (epTcp==NULL) { DBG_ERROR(NULL, "ERROR creating TCP connection"); return 2; @@ -154,7 +149,7 @@ int _doPing(GWEN_DB_NODE *dbArgs) nodeAddr=GWEN_DB_GetIntValue(dbArgs, "nodeAddr", 0, 0); timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5); - rv=_sendAcceptedMsgGroups(epTcp); + rv=Utils_SendAcceptedMsgGroups(epTcp, AQH_MSG_TYPEGROUP_ALL); if (rv<0) { DBG_INFO(NULL, "here (%d)", rv); return 2; @@ -166,7 +161,7 @@ int _doPing(GWEN_DB_NODE *dbArgs) return 2; } - msg=_waitForSpecificNodeMessage(emgr, epTcp, AQH_MSG_TYPE_PONG, nodeAddr, timeoutInSeconds); + msg=Utils_WaitForSpecificNodeMessage(emgr, epTcp, AQH_MSG_TYPE_PONG, nodeAddr, timeoutInSeconds); if (msg==NULL) { DBG_INFO(NULL, "No PONG response received."); GWEN_MsgEndpointMgr_free(emgr); @@ -180,69 +175,6 @@ int _doPing(GWEN_DB_NODE *dbArgs) -GWEN_MSG *_waitForSpecificNodeMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, - int msgCode, - int nodeAddr, - int timeoutInSeconds) -{ - time_t startTime; - - startTime=time(NULL); - - for (;;) { - GWEN_MSG *msg; - time_t now; - - GWEN_MsgEndpointMgr_RunAllEndpoints(emgr); - GWEN_MsgEndpointMgr_IoLoopOnce(emgr); - msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp); - if (msg) { - if (GWEN_IpcMsg_GetCode(msg)==AQH_MSGTYPE_IPC_FORWARD) { - GWEN_MSG *nodeMsg; - - DBG_INFO(NULL, "Received IPC FORWARD message"); - nodeMsg=AQH_ForwardIpcMsg_GetCopyOfNodeMsg(msg); - if (nodeMsg) { - DBG_ERROR(AQH_LOGDOMAIN, - "Received node msg from %d (%d)", - AQH_NodeMsg_GetSourceAddress(nodeMsg), - AQH_NodeMsg_GetMsgType(nodeMsg)); - if (AQH_NodeMsg_GetMsgType(nodeMsg)==msgCode && - AQH_NodeMsg_GetSourceAddress(nodeMsg)==nodeAddr) { - GWEN_Msg_free(msg); - return nodeMsg; - } - } - } - GWEN_Msg_free(msg); - } - now=time(NULL); - if (now-startTime>timeoutInSeconds) { - DBG_INFO(NULL, "Timeout"); - break; - } - } - - return NULL; -} - - - -int _sendAcceptedMsgGroups(GWEN_MSG_ENDPOINT *epTcp) -{ - GWEN_MSG *msgOut; - - msgOut=AQH_SetAcceptedMsgGroupsIpcMsg_new(AQH_MSGTYPE_IPC_SETACCMSGGRPS, AQH_MSG_TYPEGROUP_ALL); - if (msgOut==NULL) { - DBG_ERROR(NULL, "Error creating message"); - return GWEN_ERROR_GENERIC; - } - GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut); - return 0; -} - - - int _sendPing(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr) { GWEN_MSG *msgOut; @@ -261,28 +193,3 @@ int _sendPing(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr) -GWEN_MSG_ENDPOINT *_setupIpcEndpoint(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_DB_NODE *dbArgs) -{ - GWEN_MSG_ENDPOINT *epTcp; - const char *tcpAddress; - int tcpPort; - - tcpAddress=GWEN_DB_GetCharValue(dbArgs, "tcpAddress", 0, "127.0.0.1"); - tcpPort=GWEN_DB_GetIntValue(dbArgs, "tcpPort", 0, 45454); - - DBG_INFO(NULL, "Setup tcp client endpoint to %s:%d", tcpAddress, tcpPort); - epTcp=AQH_IpcTcpClientEndpoint_new(tcpAddress, tcpPort, "aqhome-tool-IPC", 0); - if (epTcp==NULL) { - DBG_ERROR(NULL, "Error creating endpoint TCPc"); - return NULL; - } - - return epTcp; -} - - - - - - - diff --git a/apps/aqhome-tool/utils.c b/apps/aqhome-tool/utils.c new file mode 100644 index 0000000..183329a --- /dev/null +++ b/apps/aqhome-tool/utils.c @@ -0,0 +1,113 @@ +/**************************************************************************** + * 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 "./utils.h" + +#include "aqhome/ipc/endpoint_ipc_tcpc.h" +#include "aqhome/ipc/msg_ipc_setaccmsggrps.h" +#include "aqhome/ipc/msg_ipc_forward.h" + +#include + +#include +#include + + + + +GWEN_MSG_ENDPOINT *Utils_SetupIpcEndpoint(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_DB_NODE *dbArgs) +{ + GWEN_MSG_ENDPOINT *epTcp; + const char *tcpAddress; + int tcpPort; + + tcpAddress=GWEN_DB_GetCharValue(dbArgs, "tcpAddress", 0, "127.0.0.1"); + tcpPort=GWEN_DB_GetIntValue(dbArgs, "tcpPort", 0, 45454); + + DBG_INFO(NULL, "Setup tcp client endpoint to %s:%d", tcpAddress, tcpPort); + epTcp=AQH_IpcTcpClientEndpoint_new(tcpAddress, tcpPort, "aqhome-tool-IPC", 0); + if (epTcp==NULL) { + DBG_ERROR(NULL, "Error creating endpoint TCPc"); + return NULL; + } + + return epTcp; +} + + + +GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, + int msgCode, + int nodeAddr, + int timeoutInSeconds) +{ + time_t startTime; + + startTime=time(NULL); + + for (;;) { + GWEN_MSG *msg; + time_t now; + + GWEN_MsgEndpointMgr_RunAllEndpoints(emgr); + GWEN_MsgEndpointMgr_IoLoopOnce(emgr); + msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp); + if (msg) { + if (GWEN_IpcMsg_GetCode(msg)==AQH_MSGTYPE_IPC_FORWARD) { + GWEN_MSG *nodeMsg; + + DBG_INFO(NULL, "Received IPC FORWARD message"); + nodeMsg=AQH_ForwardIpcMsg_GetCopyOfNodeMsg(msg); + if (nodeMsg) { + DBG_ERROR(AQH_LOGDOMAIN, + "Received node msg from %d (%d)", + AQH_NodeMsg_GetSourceAddress(nodeMsg), + AQH_NodeMsg_GetMsgType(nodeMsg)); + if (AQH_NodeMsg_GetMsgType(nodeMsg)==msgCode && + AQH_NodeMsg_GetSourceAddress(nodeMsg)==nodeAddr) { + GWEN_Msg_free(msg); + return nodeMsg; + } + } + } + GWEN_Msg_free(msg); + } + now=time(NULL); + if (now-startTime>timeoutInSeconds) { + DBG_INFO(NULL, "Timeout"); + break; + } + } + + return NULL; +} + + + +int Utils_SendAcceptedMsgGroups(GWEN_MSG_ENDPOINT *epTcp, uint32_t groups) +{ + GWEN_MSG *msgOut; + + msgOut=AQH_SetAcceptedMsgGroupsIpcMsg_new(AQH_MSGTYPE_IPC_SETACCMSGGRPS, groups); + if (msgOut==NULL) { + DBG_ERROR(NULL, "Error creating message"); + return GWEN_ERROR_GENERIC; + } + GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut); + return 0; +} + + + + + diff --git a/apps/aqhome-tool/utils.h b/apps/aqhome-tool/utils.h new file mode 100644 index 0000000..de4d4d0 --- /dev/null +++ b/apps/aqhome-tool/utils.h @@ -0,0 +1,29 @@ +/**************************************************************************** + * 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 AQHOME_TOOL_UTILS_H +#define AQHOME_TOOL_UTILS_H + + +#include +#include + + + +GWEN_MSG_ENDPOINT *Utils_SetupIpcEndpoint(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_DB_NODE *dbArgs); + +GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, + int msgCode, + int nodeAddr, + int timeoutInSeconds); + +int Utils_SendAcceptedMsgGroups(GWEN_MSG_ENDPOINT *epTcp, uint32_t groups); + + +#endif +