177 lines
4.2 KiB
C
177 lines
4.2 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 "./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_INFO(AQH_LOGDOMAIN,
|
|
"Received node msg from %d (%d)",
|
|
AQH_NodeMsg_GetSourceAddress(nodeMsg),
|
|
AQH_NodeMsg_GetMsgType(nodeMsg));
|
|
if (AQH_NodeMsg_GetMsgType(nodeMsg)==msgCode && (nodeAddr==0 || 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;
|
|
}
|
|
|
|
|
|
|
|
GWEN_MSG *Utils_WaitForSpecificIpcMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp,
|
|
int msgCode,
|
|
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) {
|
|
uint16_t code;
|
|
|
|
code=GWEN_IpcMsg_GetCode(msg);
|
|
if (code==msgCode) {
|
|
DBG_INFO(NULL, "Received expected IPC message");
|
|
return msg;
|
|
}
|
|
else if (code==AQH_MSGTYPE_IPC_ERROR) {
|
|
DBG_INFO(NULL, "Received IPC error message");
|
|
return msg;
|
|
}
|
|
GWEN_Msg_free(msg);
|
|
}
|
|
now=time(NULL);
|
|
if (now-startTime>timeoutInSeconds) {
|
|
DBG_INFO(NULL, "Timeout");
|
|
break;
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
|
|
|
|
int Utils_FlushOutMessageQueue(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds)
|
|
{
|
|
time_t startTime;
|
|
|
|
startTime=time(NULL);
|
|
|
|
while(GWEN_MsgEndpoint_HaveMessageToSend(epTcp)) {
|
|
time_t now;
|
|
|
|
GWEN_MsgEndpointMgr_RunAllEndpoints(emgr);
|
|
GWEN_MsgEndpointMgr_IoLoopOnce(emgr);
|
|
now=time(NULL);
|
|
if (now-startTime>timeoutInSeconds) {
|
|
DBG_INFO(NULL, "Timeout");
|
|
return GWEN_ERROR_TIMEOUT;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
|
|
|