aqhome-tool: recorganized code for improved reusability.
This commit is contained in:
@@ -34,12 +34,14 @@
|
||||
|
||||
<headers dist="true" >
|
||||
ping.h
|
||||
utils.h
|
||||
</headers>
|
||||
|
||||
<sources>
|
||||
$(local/typefiles)
|
||||
main.c
|
||||
ping.c
|
||||
utils.c
|
||||
</sources>
|
||||
|
||||
<useTargets>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
113
apps/aqhome-tool/utils.c
Normal file
113
apps/aqhome-tool/utils.c
Normal file
@@ -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 <config.h>
|
||||
#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 <gwenhywfar/debug.h>
|
||||
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
29
apps/aqhome-tool/utils.h
Normal file
29
apps/aqhome-tool/utils.h
Normal file
@@ -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 <gwenhywfar/db.h>
|
||||
#include <gwenhywfar/endpointmgr.h>
|
||||
|
||||
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user