From 263ce00a14c831970ac5381427b8bd98bc4af929 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Tue, 11 Mar 2025 00:56:18 +0100 Subject: [PATCH] more work on new events2-based apps. --- apps/aqhome-nodes/0BUILD | 2 + apps/aqhome-nodes/r_connect.c | 1 + apps/aqhome-nodes/r_getnodes.c | 77 +++++++++++++++++++++++++++++++ apps/aqhome-nodes/r_getnodes.h | 27 +++++++++++ apps/aqhome-nodes/server.c | 24 ++++++---- apps/aqhome-tool/client.c | 10 ++-- apps/aqhome-tool/nodes/getnodes.c | 5 +- apps/aqhome-tool/nodes/ping.c | 2 +- aqhome/ipc2/endpoint.c | 2 +- aqhome/ipc2/ipc_endpoint.c | 5 +- aqhome/ipc2/msgwriter.c | 4 +- 11 files changed, 135 insertions(+), 24 deletions(-) create mode 100644 apps/aqhome-nodes/r_getnodes.c create mode 100644 apps/aqhome-nodes/r_getnodes.h diff --git a/apps/aqhome-nodes/0BUILD b/apps/aqhome-nodes/0BUILD index 316158d..b1f49a6 100644 --- a/apps/aqhome-nodes/0BUILD +++ b/apps/aqhome-nodes/0BUILD @@ -43,6 +43,7 @@ r_connect.h r_forward.h r_setaccmsggrps.h + r_getnodes.h @@ -55,6 +56,7 @@ r_connect.c r_forward.c r_setaccmsggrps.c + r_getnodes.c main.c diff --git a/apps/aqhome-nodes/r_connect.c b/apps/aqhome-nodes/r_connect.c index cd79aba..4559baf 100644 --- a/apps/aqhome-nodes/r_connect.c +++ b/apps/aqhome-nodes/r_connect.c @@ -74,6 +74,7 @@ void AQH_NodeServer_HandleConnect(GWEN_UNUSED AQH_OBJECT *o, AQH_OBJECT *ep, con AQH_IpcMessage_GetMsgId(msg), resultCode, NULL); AQH_Endpoint_AddMsgOut(ep, outMsg); + DBG_ERROR(NULL, "Client connected"); } diff --git a/apps/aqhome-nodes/r_getnodes.c b/apps/aqhome-nodes/r_getnodes.c new file mode 100644 index 0000000..28c8d8d --- /dev/null +++ b/apps/aqhome-nodes/r_getnodes.c @@ -0,0 +1,77 @@ +/**************************************************************************** + * 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 "./r_getnodes.h" +#include "./server_p.h" +#include "aqhome/ipc2/endpoint.h" +#include "aqhome/msg/ipc/m_ipc.h" +#include "aqhome/msg/ipc/nodes/m_ipcn.h" +#include "aqhome/msg/ipc/nodes/m_ipcn_setaccmsggrps.h" +#include "aqhome/msg/ipc/nodes/m_ipcn_getdevices_rsp.h" +#include "aqhome/msg/ipc/m_ipc_connect.h" +#include "aqhome/msg/ipc/m_ipc_result.h" +#include "aqhome/msg/ipc/m_ipc_tag16.h" + +#include + + + +/* ------------------------------------------------------------------------------------------------ + * code + * ------------------------------------------------------------------------------------------------ + */ + +void AQH_NodeServer_HandleGetNodes(AQH_OBJECT *o, AQH_OBJECT *ep, const AQH_MESSAGE *msg) +{ + AQH_NODE_SERVER *xo; + + xo=AQH_NodeServer_GetServerData(o); + if (xo) { + AQH_NODE_INFO_LIST *nodeInfoList; + + nodeInfoList=AQH_NodeDb_GetAllNodeInfos(xo->nodeDb); + if (nodeInfoList && AQH_NodeInfo_List_GetCount(nodeInfoList)) { + const AQH_NODE_INFO *ni; + + ni=AQH_NodeInfo_List_First(nodeInfoList); + while(ni) { + const AQH_NODE_INFO *niNext; + AQH_MESSAGE *outMsg; + + niNext=AQH_NodeInfo_List_Next(ni); + DBG_INFO(AQH_LOGDOMAIN, "Sending response for node %02x (%08x)", AQH_NodeInfo_GetBusAddress(ni), AQH_NodeInfo_GetUid(ni)); + outMsg=AQH_IpcnMessageGetDevicesRsp_new(AQH_MSGTYPE_IPC_NODES_GETDEVICES_RSP, + AQH_Endpoint_GetNextMessageId(ep), AQH_IpcMessage_GetMsgId(msg), + niNext?0:AQH_MSGNODE_GETDEVICES_RSP_FLAGS_LASTMSG, ni); + AQH_Endpoint_AddMsgOut(ep, outMsg); + DBG_ERROR(NULL, "Messages in clients out queue: %d", AQH_Message_List_GetCount(AQH_Endpoint_GetMsgOutList(ep))); + ni=niNext; + } + } + else { + AQH_MESSAGE *outMsg; + + DBG_INFO(AQH_LOGDOMAIN, "No nodes"); + outMsg=AQH_IpcMessageResult_new(AQH_IPC_PROTOCOL_NODES_ID, + AQH_IPC_PROTOCOL_NODES_VERSION, + AQH_MSGTYPE_IPC_NODES_RESULT, + AQH_Endpoint_GetNextMessageId(ep), + AQH_IpcMessage_GetMsgId(msg), + AQH_MSGDATA_RESULT_ERROR_NODATA, NULL); + AQH_Endpoint_AddMsgOut(ep, outMsg); + } + } +} + + + diff --git a/apps/aqhome-nodes/r_getnodes.h b/apps/aqhome-nodes/r_getnodes.h new file mode 100644 index 0000000..0a02bd2 --- /dev/null +++ b/apps/aqhome-nodes/r_getnodes.h @@ -0,0 +1,27 @@ +/**************************************************************************** + * 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 AQHOMED_R_GETNODES_H +#define AQHOMED_R_GETNODES_H + + +#include "./server.h" + +#include + + +void AQH_NodeServer_HandleGetNodes(AQH_OBJECT *o, AQH_OBJECT *ep, const AQH_MESSAGE *msg); + + + + + + +#endif + + diff --git a/apps/aqhome-nodes/server.c b/apps/aqhome-nodes/server.c index 869e154..d51515d 100644 --- a/apps/aqhome-nodes/server.c +++ b/apps/aqhome-nodes/server.c @@ -17,6 +17,7 @@ #include "./r_connect.h" #include "./r_forward.h" #include "./r_setaccmsggrps.h" +#include "./r_getnodes.h" #include #include @@ -44,6 +45,7 @@ #include #include +#include #include #include @@ -758,16 +760,15 @@ void _handleMsgFromClient(GWEN_UNUSED AQH_OBJECT *o, GWEN_UNUSED AQH_NODE_SERVER GWEN_TAG16_LIST *tagList; tagList=AQH_IpcMessageTag16_ParsePayload(msg, 0); - if (tagList) { - DBG_ERROR(NULL, "Received IPC packet %d (%x)", (int) code, code); - switch(code) { - case AQH_MSGTYPE_IPC_NODES_CONNECT_REQ: AQH_NodeServer_HandleConnect(o, ep, msg, tagList); break; - case AQH_MSGTYPE_IPC_NODES_FORWARD: AQH_NodeServer_HandleForward(o, ep, msg, tagList); break; - case AQH_MSGTYPE_IPC_NODES_SETACCMSGGRPS: AQH_NodeServer_HandleSetAccMsgGrps(o, ep, msg, tagList); break; - default: break; - } - GWEN_Tag16_List_free(tagList); + DBG_ERROR(NULL, "Received IPC packet %d (%x)", (int) code, code); + switch(code) { + case AQH_MSGTYPE_IPC_NODES_CONNECT_REQ: AQH_NodeServer_HandleConnect(o, ep, msg, tagList); break; + case AQH_MSGTYPE_IPC_NODES_FORWARD: AQH_NodeServer_HandleForward(o, ep, msg, tagList); break; + case AQH_MSGTYPE_IPC_NODES_SETACCMSGGRPS: AQH_NodeServer_HandleSetAccMsgGrps(o, ep, msg, tagList); break; + case AQH_MSGTYPE_IPC_NODES_GETDEVICES_REQ: AQH_NodeServer_HandleGetNodes(o, ep, msg); break; + default: break; } + GWEN_Tag16_List_free(tagList); } else { DBG_ERROR(NULL, "Invalid IPC protocol %d (%02x)", protoId, protoId); @@ -1016,8 +1017,13 @@ void _writeTtyMsgToLogFile(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg) { if (xo->logFile) { GWEN_BUFFER *dbuf; + GWEN_TIME *ti; dbuf=GWEN_Buffer_new(0, 256, 0, 1); + ti=GWEN_CurrentTime(); + GWEN_Time_toString(ti, "YYYY-MM-DD hh:mm:ss ", dbuf); + GWEN_Time_free(ti); + ti=NULL; AQH_NodeMessage_DumpSpecificToBuffer(msg, dbuf, "received"); _writeToLogFile(xo->logFile, GWEN_Buffer_GetStart(dbuf)); GWEN_Buffer_free(dbuf); diff --git a/apps/aqhome-tool/client.c b/apps/aqhome-tool/client.c index 57824a6..971808f 100644 --- a/apps/aqhome-tool/client.c +++ b/apps/aqhome-tool/client.c @@ -251,7 +251,7 @@ int AQH_ToolClient_Run(AQH_OBJECT *o) if (xo) { int rv; - rv=AQH_ToolClient_Connect(o, 0, xo->flags, 0); + rv=AQH_ToolClient_Connect(o, xo->flags, 0, 0); if (rv<0) { DBG_ERROR(NULL, "Error connecting (%d)", rv); return 2; @@ -397,6 +397,7 @@ int _exchangeConnectMsgs(AQH_TOOL_CLIENT *xo, uint32_t flags) userId=GWEN_DB_GetCharValue(xo->dbLocalArgs, "userId", 0, NULL); passw=GWEN_DB_GetCharValue(xo->dbLocalArgs, "password", 0, NULL); + DBG_ERROR(NULL, "Sending connect message for proto=%d.%d", xo->protoId, xo->protoVer); msgId=AQH_Endpoint_GetNextMessageId(xo->ipcEndpoint); msgOut=AQH_IpcMessageConnect_new(xo->protoId, xo->protoVer, AQH_MSGTYPE_IPC_CONNECT_REQ, @@ -492,7 +493,7 @@ AQH_MESSAGE *AQH_ToolClient_WaitForNodeMsg(AQH_OBJECT *o, int nodeSrcAddr, uint8 } } else { - DBG_ERROR(NULL, "Received unexpected message %d (%x), ignoring", code, code); + DBG_INFO(NULL, "Received unexpected message %d (%x), ignoring", code, code); } GWEN_Tag16_List_free(tagList); } @@ -543,7 +544,6 @@ int _sendWaitAndHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo) return 2; } AQH_Endpoint_AddMsgOut(xo->ipcEndpoint, msgOut); - return _waitAndHandle(o, xo, msgId); } @@ -630,7 +630,7 @@ int _nodesWaitAndHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo) if (msgIn) { int rv; - DBG_ERROR(NULL, "Handling node message %d", AQH_NodeMessage_GetMsgType(msgIn)); + DBG_INFO(NULL, "Handling node message %d", AQH_NodeMessage_GetMsgType(msgIn)); rv=_handleNodeResponseMessage(o, msgIn, first); AQH_Message_free(msgIn); first=0; @@ -639,7 +639,7 @@ int _nodesWaitAndHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo) return 3; } else if (rv==1) { - DBG_ERROR(NULL, "Done."); + DBG_INFO(NULL, "Done."); return 0; } } diff --git a/apps/aqhome-tool/nodes/getnodes.c b/apps/aqhome-tool/nodes/getnodes.c index 84e6e20..f3d5251 100644 --- a/apps/aqhome-tool/nodes/getnodes.c +++ b/apps/aqhome-tool/nodes/getnodes.c @@ -79,8 +79,7 @@ int AQH_Tool_GetNodes(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv) eventLoop=AQH_EventLoop_new(); o=AQH_ToolClient_new(eventLoop, AQH_IPC_PROTOCOL_NODES_ID, AQH_IPC_PROTOCOL_NODES_VERSION, dbGlobalArgs, args); - AQH_ToolClient_SetFlags(o, AQH_TOOL_CLIENT_CONNECTFLAGS_WITHCONNECTMSG | AQH_TOOL_CLIENT_CONNECTFLAGS_WITHGRPMSG); - AQH_ToolClient_SetAcceptedGroups(o, AQH_MSG_TYPEGROUP_ALL); + AQH_ToolClient_SetFlags(o, AQH_TOOL_CLIENT_CONNECTFLAGS_WITHCONNECTMSG); AQH_ToolClient_SetCreateRequestMessageFn(o, _createRequestMessage); AQH_ToolClient_SetHandleResponseMessageFn(o, _handleResponseMessage); @@ -141,7 +140,7 @@ void _printNode(const AQH_NODE_INFO *ni) uint32_t u; const GWEN_TIMESTAMP *ts; - fprintf(stdout, "- node: addr=%d, uid=0x%08x, device=", + fprintf(stdout, "- node: addr=%3d, uid=0x%08x, device=", AQH_NodeInfo_GetBusAddress(ni), (unsigned int) AQH_NodeInfo_GetUid(ni)); ts=AQH_NodeInfo_GetTimestampLastChange(ni); diff --git a/apps/aqhome-tool/nodes/ping.c b/apps/aqhome-tool/nodes/ping.c index d01f19e..0f00bb7 100644 --- a/apps/aqhome-tool/nodes/ping.c +++ b/apps/aqhome-tool/nodes/ping.c @@ -128,7 +128,7 @@ int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, GWEN_UNUSED co return 1; } else { - DBG_ERROR(NULL, "Unexpected message \"%d\"", code); + DBG_INFO(NULL, "Unexpected message \"%d\"", code); return 0; } diff --git a/aqhome/ipc2/endpoint.c b/aqhome/ipc2/endpoint.c index 4e25306..3e3bc6d 100644 --- a/aqhome/ipc2/endpoint.c +++ b/aqhome/ipc2/endpoint.c @@ -531,7 +531,7 @@ int _handleMsgSent(AQH_OBJECT *o) if (xo) { AQH_MESSAGE *msg; - DBG_INFO(AQH_LOGDOMAIN, "Messages in outlist: %d", AQH_Message_List_GetCount(xo->msgOutList)); + DBG_ERROR(AQH_LOGDOMAIN, "Messages in outlist: %d", AQH_Message_List_GetCount(xo->msgOutList)); msg=AQH_Message_List_First(xo->msgOutList); if (msg) { /* remove sent message from list */ diff --git a/aqhome/ipc2/ipc_endpoint.c b/aqhome/ipc2/ipc_endpoint.c index 9b5f768..dcca205 100644 --- a/aqhome/ipc2/ipc_endpoint.c +++ b/aqhome/ipc2/ipc_endpoint.c @@ -72,9 +72,7 @@ AQH_MESSAGE *AQH_IpcEndpoint_WaitForResponseMsg(AQH_OBJECT *ipcEndpoint, uint32_ AQH_MESSAGE *msg; time_t now; - AQH_EventLoop_Run(AQH_Object_GetEventLoop(ipcEndpoint), 500); - msg=AQH_Endpoint_GetNextMsgIn(ipcEndpoint); - if (msg) { + while( (msg=AQH_Endpoint_GetNextMsgIn(ipcEndpoint)) ) { if (refMsgId==0 || refMsgId==AQH_IpcMessage_GetRefMsgId(msg)) return msg; else { @@ -91,6 +89,7 @@ AQH_MESSAGE *AQH_IpcEndpoint_WaitForResponseMsg(AQH_OBJECT *ipcEndpoint, uint32_ DBG_INFO(NULL, "Timeout"); break; } + AQH_EventLoop_Run(AQH_Object_GetEventLoop(ipcEndpoint), 500); } return NULL; diff --git a/aqhome/ipc2/msgwriter.c b/aqhome/ipc2/msgwriter.c index 4c4322e..ddc7c7d 100644 --- a/aqhome/ipc2/msgwriter.c +++ b/aqhome/ipc2/msgwriter.c @@ -136,7 +136,7 @@ int _handleSocketReady(AQH_OBJECT *o, AQH_OBJECT *fdObject) if (xo->bytesLeft) { if (!(xo->flags & AQH_MSGWRITER_FLAGS_MSGSTARTED)) { - DBG_ERROR(NULL, "Starting message"); + DBG_INFO(NULL, "Starting message"); rv=_startMsg(xo, fdObject); if (rv<0) { if (rv==GWEN_ERROR_TRY_AGAIN) { @@ -167,7 +167,7 @@ int _handleSocketReady(AQH_OBJECT *o, AQH_OBJECT *fdObject) const uint8_t *msgPtr; _endMsg(xo, fdObject); - DBG_ERROR(NULL, "Ended message"); + DBG_INFO(NULL, "Ended message"); msgPtr=xo->msgBufPtr; msgLen=xo->msgBufLen; _resetBuffer(o);