From 6b61763d6f186b395ea933453f15ded597d5b560 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Sat, 1 Mar 2025 15:22:34 +0100 Subject: [PATCH] ipc2: added ipc_endpoint --- aqhome/ipc2/0BUILD | 1 + aqhome/ipc2/ipc_endpoint.c | 133 +++++++++++++++++++++++++++++++++++++ aqhome/ipc2/ipc_endpoint.h | 34 ++++++++++ 3 files changed, 168 insertions(+) create mode 100644 aqhome/ipc2/ipc_endpoint.c create mode 100644 aqhome/ipc2/ipc_endpoint.h diff --git a/aqhome/ipc2/0BUILD b/aqhome/ipc2/0BUILD index acafb2a..71f75f1 100644 --- a/aqhome/ipc2/0BUILD +++ b/aqhome/ipc2/0BUILD @@ -87,6 +87,7 @@ msgrequest.c ipc_server.c ipc_client.c + ipc_endpoint.c tty_endpoint.c diff --git a/aqhome/ipc2/ipc_endpoint.c b/aqhome/ipc2/ipc_endpoint.c new file mode 100644 index 0000000..3d0b853 --- /dev/null +++ b/aqhome/ipc2/ipc_endpoint.c @@ -0,0 +1,133 @@ +/**************************************************************************** + * This file is part of the project AqHome. + * AqHome (c) by 2025 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 "./ipc_endpoint.h" +#include "aqhome/msg/ipc/m_ipc.h" +#include "aqhome/msg/ipc/m_ipc_result.h" +#include "aqhome/msg/ipc/m_ipc_tag16.h" + +#include + + + +int AQH_IpcEndpoint_ExchangeConnectMsg(AQH_OBJECT *ipcEndpoint, + uint16_t connectMsgCode, + uint16_t resultMsgCode, + const char *clientId, + const char *userId, + const char *passw, + uint32_t flags, + int timeoutInSeconds) +{ + AQH_MESSAGE *msgOut; + uint32_t msgId; + + msgId=AQH_Endpoint_GetNextMessageId(ipcEndpoint); + msgOut=AQH_IpcdMessageConnect_new(connectMsgCode, msgId, 0, clientId, userId, passw, flags); + AQH_Endpoint_AddMsgOut(ipcEndpoint, msgOut); + + return AQH_IpcEndpoint_WaitForResultMsg(ipcEndpoint, resultMsgCode, msgId, timeoutInSeconds); +} + + + +int AQH_IpcEndpoint_WaitForResultMsg(AQH_OBJECT *ipcEndpoint, uint16_t resultMsgCode, uint32_t refMsgId, int timeoutInSeconds) +{ + for (;;) { + AQH_MESSAGE *msgIn; + + msgIn=AQH_IpcEndpoint_WaitForResponseMsg(ipcEndpoint, refMsgId, timeoutInSeconds); + if (msgIn) { + GWEN_TAG16_LIST *tagList; + + tagList=AQH_IpcMessageTag16_ParsePayload(msgIn, 0); + if (tagList) { + int code; + + code=AQH_IpcMessage_GetCode(msgIn); + if (code==resultMsgCode) { + int result; + + result=AQH_IpcMessageResult_GetResult(tagList); + GWEN_Tag16_List_free(tagList); + AQH_Message_free(msgIn); + return result; + } + else { + DBG_ERROR(NULL, "Unexpected message received (%x)", code); + } + GWEN_Tag16_List_free(tagList); + } + AQH_Message_free(msgIn); + } + else { + DBG_ERROR(NULL, "No matching msg received"); + return GWEN_ERROR_TIMEOUT; + } + } /* for */ +} + + + +AQH_MESSAGE *AQH_IpcEndpoint_WaitForResponseMsg(AQH_OBJECT *ipcEndpoint, uint32_t refMsgId, int timeoutInSeconds) +{ + time_t startTime; + + startTime=time(NULL); + + for (;;) { + AQH_MESSAGE *msg; + time_t now; + + AQH_EventLoop_Run(AQH_Object_GetEventLoop(ipcEndpoint), 500); + msg=AQH_Endpoint_GetNextMsgIn(ipcEndpoint); + if (msg) { + if (refMsgId==0 || refMsgId==AQH_IpcMessage_GetRefMsgId(msg)) + return msg; + else { + uint16_t code; + + code=AQH_IpcMessage_GetCode(msg); + DBG_ERROR(NULL, "Received unexpected message %d (%x), ignoring", code, code); + AQH_Message_free(msg); + } + } + + now=time(NULL); + if (now-startTime>timeoutInSeconds) { + DBG_INFO(NULL, "Timeout"); + break; + } + } + + return NULL; +} + + + +void AQH_IpcEndpoint_SendResponseResultToEndpoint(AQH_OBJECT *ep, + uint8_t protoId, uint8_t protoVer,uint16_t code, + uint32_t refMsgId, int result) +{ + AQH_MESSAGE *msg; + + msg=AQH_IpcMessageResult_new(protoId, protoVer, code, AQH_Endpoint_GetNextMessageId(ep), refMsgId, result, NULL); + AQH_Endpoint_AddMsgOut(ep, msg); +} + + + + + + + + diff --git a/aqhome/ipc2/ipc_endpoint.h b/aqhome/ipc2/ipc_endpoint.h new file mode 100644 index 0000000..2cf4960 --- /dev/null +++ b/aqhome/ipc2/ipc_endpoint.h @@ -0,0 +1,34 @@ +/**************************************************************************** + * This file is part of the project AqHome. + * AqHome (c) by 2025 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_IPC_ENDPOINT_H +#define AQH_IPC_ENDPOINT_H + + +#include + + + +AQHOME_API int AQH_IpcEndpoint_ExchangeConnectMsg(AQH_OBJECT *ipcEndpoint, + uint16_t connectMsgCode, + uint16_t resultMsgCode, + const char *clientId, + const char *userId, + const char *passw, + uint32_t flags, + int timeoutInSeconds); +AQHOME_API int AQH_IpcEndpoint_WaitForResultMsg(AQH_OBJECT *ipcEndpoint, uint16_t resultMsgCode, uint32_t refMsgId, int timeoutInSeconds); +AQHOME_API AQH_MESSAGE *AQH_IpcEndpoint_WaitForResponseMsg(AQH_OBJECT *ipcEndpoint, uint32_t refMsgId, int timeoutInSeconds); +AQHOME_API void AQH_IpcEndpoint_SendResponseResultToEndpoint(AQH_OBJECT *ep, + uint8_t protoId, uint8_t protoVer,uint16_t code, + uint32_t refMsgId, int result); + + + +#endif +