diff --git a/apps/aqhome-data/s_connect.c b/apps/aqhome-data/s_connect.c index 631229e..8486d39 100644 --- a/apps/aqhome-data/s_connect.c +++ b/apps/aqhome-data/s_connect.c @@ -1,6 +1,6 @@ /**************************************************************************** * This file is part of the project AqHome. - * AqHome (c) by 2023 Martin Preuss, all rights reserved. + * 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. @@ -16,7 +16,7 @@ #include "aqhome/ipc2/endpoint.h" #include "aqhome/msg/ipc/m_ipc.h" #include "aqhome/msg/ipc/data/m_ipcd.h" -#include "aqhome/msg/ipc/data/m_ipcd_connect.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" @@ -40,17 +40,17 @@ void AqHomeDataServer_HandleConnect(GWEN_UNUSED AQH_OBJECT *o, AQH_OBJECT *ep, c uint32_t flags; tagList=AQH_IpcMessageTag16_ParsePayload(msg, 0); - clientId=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSGDATA_CONNECT_TAGS_CLIENTID, NULL); - userId=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSGDATA_CONNECT_TAGS_USERID, NULL); - flags=AQH_Tag16_GetTagDataAsUint32(tagList, AQH_MSGDATA_CONNECT_TAGS_FLAGS, 0); - passw=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSGDATA_CONNECT_TAGS_PASSWORD, NULL); + clientId=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSG_CONNECT_TAGS_CLIENTID, NULL); + userId=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSG_CONNECT_TAGS_USERID, NULL); + flags=AQH_Tag16_GetTagDataAsUint32(tagList, AQH_MSG_CONNECT_TAGS_FLAGS, 0); + passw=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSG_CONNECT_TAGS_PASSWORD, NULL); if (clientId) AQH_Endpoint_SetServiceName(ep, clientId); if (userId) AQH_Endpoint_SetUserName(ep, userId); - if (flags & AQH_MSGDATA_CONNECT_FLAGS_WANTUPDATES) + if (flags & AQH_MSG_CONNECT_FLAGS_WANTUPDATES) AQH_Endpoint_AddFlags(ep, AQH_ENDPOINT_FLAGS_WANTUPDATES); /* TODO: add user management, for now we allow all */ diff --git a/apps/aqhome-nodes/0BUILD b/apps/aqhome-nodes/0BUILD index 9f2545f..7721d69 100644 --- a/apps/aqhome-nodes/0BUILD +++ b/apps/aqhome-nodes/0BUILD @@ -40,6 +40,7 @@ devicesread.h devicesdump.h r_setdata.h + r_connect.h @@ -49,6 +50,7 @@ devicesread.c devicesdump.c r_setdata.c + r_connect.c main.c diff --git a/apps/aqhome-nodes/r_connect.c b/apps/aqhome-nodes/r_connect.c new file mode 100644 index 0000000..a59fda4 --- /dev/null +++ b/apps/aqhome-nodes/r_connect.c @@ -0,0 +1,82 @@ +/**************************************************************************** + * 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_connect.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/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_HandleConnect(GWEN_UNUSED AQH_OBJECT *o, AQH_OBJECT *ep, const AQH_MESSAGE *msg) +{ + GWEN_TAG16_LIST *tagList; + AQH_MESSAGE *outMsg; + int resultCode=AQH_MSGDATA_RESULT_SUCCESS; + char *clientId=NULL; + char *userId=NULL; + char *passw=NULL; + uint32_t flags; + + tagList=AQH_IpcMessageTag16_ParsePayload(msg, 0); + clientId=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSG_CONNECT_TAGS_CLIENTID, NULL); + userId=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSG_CONNECT_TAGS_USERID, NULL); + flags=AQH_Tag16_GetTagDataAsUint32(tagList, AQH_MSG_CONNECT_TAGS_FLAGS, 0); + passw=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSG_CONNECT_TAGS_PASSWORD, NULL); + + if (clientId) + AQH_Endpoint_SetServiceName(ep, clientId); + if (userId) + AQH_Endpoint_SetUserName(ep, userId); + + if (flags & AQH_MSG_CONNECT_FLAGS_WANTUPDATES) + AQH_Endpoint_AddFlags(ep, AQH_ENDPOINT_FLAGS_WANTUPDATES); + + /* TODO: add user management, for now we allow all */ + AQH_Endpoint_SetPermissions(ep, + AQH_ENDPOINT_PERMS_LISTVALUES | + AQH_ENDPOINT_PERMS_READVALUE | + AQH_ENDPOINT_PERMS_ADDVALUE | + AQH_ENDPOINT_PERMS_LISTDATA | + AQH_ENDPOINT_PERMS_READDATA | + AQH_ENDPOINT_PERMS_ADDDATA | + AQH_ENDPOINT_PERMS_LISTDEVICES | + AQH_ENDPOINT_PERMS_READDEVICE | + AQH_ENDPOINT_PERMS_ADDDEVICE | + AQH_ENDPOINT_PERMS_MODDEVICE); + free(passw); + free(userId); + free(clientId); + + 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), + resultCode, NULL); + AQH_Endpoint_AddMsgOut(ep, outMsg); +} + + + diff --git a/apps/aqhome-tool/nodes/setnodevalue.h b/apps/aqhome-nodes/r_connect.h similarity index 55% rename from apps/aqhome-tool/nodes/setnodevalue.h rename to apps/aqhome-nodes/r_connect.h index d143d6e..7b30024 100644 --- a/apps/aqhome-tool/nodes/setnodevalue.h +++ b/apps/aqhome-nodes/r_connect.h @@ -1,20 +1,27 @@ /**************************************************************************** * This file is part of the project AqHome. - * AqHome (c) by 2024 Martin Preuss, all rights reserved. + * 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 AQHOME_TOOL_SETNODEVALUE_H -#define AQHOME_TOOL_SETNODEVALUE_H +#ifndef AQHOMED_R_CONNECT_H +#define AQHOMED_R_CONNECT_H -#include +#include "./server.h" + +#include + + + +void AQH_NodeServer_HandleConnect(AQH_OBJECT *o, AQH_OBJECT *ep, const AQH_MESSAGE *recvdMsg); + -int AQH_Tool_SetNodeValue(GWEN_DB_NODE *dbArgs, int argc, char **argv); #endif + diff --git a/apps/aqhome-nodes/server.c b/apps/aqhome-nodes/server.c index 8876119..e780c69 100644 --- a/apps/aqhome-nodes/server.c +++ b/apps/aqhome-nodes/server.c @@ -14,6 +14,7 @@ #include "./db.h" #include "./devicesread.h" #include "./r_setdata.h" +#include "./r_connect.h" #include #include @@ -27,8 +28,11 @@ #include #include #include +#include #include #include +#include +#include #include #include #include @@ -94,6 +98,7 @@ static int readIntConfigWithAlt(GWEN_DB_NODE *dbArgs, const char *varName, const static int _startIpc(AQH_OBJECT *o, AQH_NODE_SERVER *xo); static int _startTty(AQH_OBJECT *o, AQH_NODE_SERVER *xo); static int _startBroker(AQH_OBJECT *o, AQH_NODE_SERVER *xo); +static int _exchangeConnect(AQH_OBJECT *o, AQH_NODE_SERVER *xo, uint32_t flags); static void _setupDb(AQH_NODE_SERVER *xo); static int _loadDeviceList(AQH_NODE_SERVER *xo); @@ -109,9 +114,10 @@ static void _handleMsgFromClient(AQH_OBJECT *o, AQH_NODE_SERVER *xo, AQH_OBJECT static void _handleMsgFromTty(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg); static void _writeTtyMsgToLogFile(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg); static void _forwardTtyMsgToBroker(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg); -static void _forwardValueMessage(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg); -static void _forwardDataFromSendStatsMessage(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg); -static void _forwardDataFromRecvStatsMessage(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg); +static void _forwardValueMessageToBroker(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg); +static void _forwardDataFromSendStatsMsgToBroker(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg); +static void _forwardDataFromRecvStatsMsgToBroker(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg); +static void _forwardTtyMsgToClients(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg); static void _publishInt(AQH_NODE_SERVER *xo, uint32_t uid, const char *vPath, int vModality, const char *vUnits, int v); static void _publishDouble(AQH_NODE_SERVER *xo, uint32_t uid, const char *vPath, int vModality, const char *vUnits, double v); static void _setDeviceName(AQH_VALUE *value, uint32_t uid); @@ -562,14 +568,7 @@ int _startBroker(AQH_OBJECT *o, AQH_NODE_SERVER *xo) AQH_Object_AddLink(ep, AQH_ENDPOINT_SIGNAL_CLOSED, AQH_NODE_SERVER_SLOT_BROKERCLOSED, o); AQH_Object_Enable(ep); - rv=AQH_IpcEndpoint_ExchangeConnectMsg(ep, - AQH_MSGTYPE_IPC_DATA_CONNECT_REQ, - AQH_MSGTYPE_IPC_DATA_RESULT, - xo->brokerClientId, - NULL, - NULL, - 0, - xo->timeoutInSeconds); + rv=_exchangeConnect(o, xo, 0); if (rv!=0) { DBG_ERROR(NULL, "Error connecting to broker: %d", rv); return (rv<0)?rv:GWEN_ERROR_PERMISSIONS; @@ -588,6 +587,24 @@ int _startBroker(AQH_OBJECT *o, AQH_NODE_SERVER *xo) +int _exchangeConnect(AQH_OBJECT *o, AQH_NODE_SERVER *xo, uint32_t flags) +{ + AQH_MESSAGE *msgOut; + uint32_t msgId; + + msgId=AQH_Endpoint_GetNextMessageId(xo->ipcEndpoint); + msgOut=AQH_IpcMessageConnect_new(xo->protoId, xo->protoVer, + AQH_MSGTYPE_IPC_CONNECT_REQ, + msgId, 0, + xo->brokerClientId, NULL, NULL, flags); + AQH_Endpoint_AddMsgOut(xo->ipcEndpoint, msgOut); + return AQH_IpcEndpoint_WaitForResultMsg(xo->ipcEndpoint, + xo->protoId, xo->protoVer, AQH_MSGTYPE_IPC_RESULT, + msgId, xo->timeoutInSeconds); +} + + + void _setupDb(AQH_NODE_SERVER *xo) { if (xo->dbFile) { @@ -700,6 +717,7 @@ void _handleMsgFromClient(GWEN_UNUSED AQH_OBJECT *o, GWEN_UNUSED AQH_NODE_SERVER if (protoId==AQH_IPC_PROTOCOL_DATA_ID) { DBG_ERROR(NULL, "Received IPC packet %d (%x)", (int) code, code); switch(code) { + case AQH_MSGTYPE_IPC_DATA_CONNECT_REQ: AQH_NodeServer_HandleConnect(o, ep, msg); break; default: break; } } @@ -745,6 +763,7 @@ void _handleMsgFromTty(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAGE *ms AQH_NodeServer_NodeMsgToDb(o, msg); _writeTtyMsgToLogFile(xo, msg); _forwardTtyMsgToBroker(o, xo, msg); + _forwardTtyMsgToClients(o, xo, msg); } @@ -755,16 +774,44 @@ void _forwardTtyMsgToBroker(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAG code=AQH_NodeMessage_GetMsgType(msg); switch(code) { - case AQH_MSG_TYPE_VALUE_REPORT: _forwardValueMessage(o, xo, msg); break; - case AQH_MSG_TYPE_COMSENDSTATS: _forwardDataFromSendStatsMessage(xo, msg); break; - case AQH_MSG_TYPE_COMRECVSTATS: _forwardDataFromRecvStatsMessage(xo, msg); break; + case AQH_MSG_TYPE_VALUE_REPORT: _forwardValueMessageToBroker(o, xo, msg); break; + case AQH_MSG_TYPE_COMSENDSTATS: _forwardDataFromSendStatsMsgToBroker(xo, msg); break; + case AQH_MSG_TYPE_COMRECVSTATS: _forwardDataFromRecvStatsMsgToBroker(xo, msg); break; default: break; } } -void _forwardValueMessage(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg) +void _forwardTtyMsgToClients(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg) +{ + uint8_t code; + uint32_t msgGroup; + + code=AQH_NodeMessage_GetMsgType(msg); + msgGroup=AQH_NodeMessage_GetMsgGroup(code); + if (msgGroup) { + AQH_OBJECT *ep; + + ep=AQH_Object_List_First(xo->ipcClientList); + while(ep) { + if (AQH_Endpoint_GetAcceptedMsgGroups(ep) & msgGroup) { + AQH_MESSAGE *outMsg; + + outMsg=AQH_IpcnMessageForward_new(AQH_MSGTYPE_IPC_NODES_FORWARD, + AQH_Endpoint_GetNextMessageId(ep), 0, + AQH_Message_GetMsgPointer(msg), AQH_Message_GetUsedSize(msg)); + AQH_Endpoint_AddMsgOut(ep, outMsg); + } + ep=AQH_Object_List_Next(ep); + } + } +} + + + + +void _forwardValueMessageToBroker(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg) { uint8_t valueId; double v; @@ -790,7 +837,7 @@ void _forwardValueMessage(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAGE -void _forwardDataFromSendStatsMessage(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg) +void _forwardDataFromSendStatsMsgToBroker(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg) { uint16_t packetsOutInt; @@ -820,7 +867,7 @@ void _forwardDataFromSendStatsMessage(AQH_NODE_SERVER *xo, const AQH_MESSAGE *ms -void _forwardDataFromRecvStatsMessage(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg) +void _forwardDataFromRecvStatsMsgToBroker(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg) { uint16_t packetsInInt; diff --git a/apps/aqhome-nodes/server.h b/apps/aqhome-nodes/server.h index fe8a0a1..2ce1bbf 100644 --- a/apps/aqhome-nodes/server.h +++ b/apps/aqhome-nodes/server.h @@ -18,6 +18,22 @@ +#define AQH_ENDPOINT_PERMS_LISTVALUES 0x0001 +#define AQH_ENDPOINT_PERMS_READVALUE 0x0002 +#define AQH_ENDPOINT_PERMS_ADDVALUE 0x0004 + +#define AQH_ENDPOINT_PERMS_LISTDATA 0x0010 +#define AQH_ENDPOINT_PERMS_READDATA 0x0020 +#define AQH_ENDPOINT_PERMS_ADDDATA 0x0040 +#define AQH_ENDPOINT_PERMS_SETDATA 0x0080 + +#define AQH_ENDPOINT_PERMS_LISTDEVICES 0x0100 +#define AQH_ENDPOINT_PERMS_READDEVICE 0x0200 +#define AQH_ENDPOINT_PERMS_ADDDEVICE 0x0400 +#define AQH_ENDPOINT_PERMS_MODDEVICE 0x0800 + + + AQH_OBJECT *AQH_NodeServer_new(AQH_EVENT_LOOP *eventLoop); int AQH_NodeServer_Init(AQH_OBJECT *o, int argc, char **argv); diff --git a/apps/aqhome-nodes/server_p.h b/apps/aqhome-nodes/server_p.h index a5a0211..04a7860 100644 --- a/apps/aqhome-nodes/server_p.h +++ b/apps/aqhome-nodes/server_p.h @@ -66,6 +66,10 @@ struct AQH_NODE_SERVER { struct termios initialTermiosState; int timeoutInSeconds; + + uint8_t protoId; + uint8_t protoVer; + }; diff --git a/apps/aqhome-tool/client.c b/apps/aqhome-tool/client.c index 929baf8..98135d7 100644 --- a/apps/aqhome-tool/client.c +++ b/apps/aqhome-tool/client.c @@ -18,7 +18,11 @@ #include "aqhome/msg/ipc/m_ipc_tag16.h" #include "aqhome/msg/ipc/m_ipc_result.h" #include "aqhome/msg/ipc/data/m_ipcd.h" -#include "aqhome/msg/ipc/data/m_ipcd_connect.h" +#include "aqhome/msg/ipc/m_ipc_connect.h" +#include "aqhome/msg/ipc/nodes/m_ipcn.h" +#include "aqhome/msg/ipc/nodes/m_ipcn_forward.h" +#include "aqhome/msg/ipc/nodes/m_ipcn_setaccmsggrps.h" +#include "aqhome/msg/node/m_node.h" #include "aqhome/ipc2/ipc_endpoint.h" #include @@ -35,9 +39,15 @@ GWEN_INHERIT(AQH_OBJECT, AQH_TOOL_CLIENT) static void GWENHYWFAR_CB _freeData(void *bp, void *p); -static int _sendWaitHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo); +static int _connectEndpoint(AQH_OBJECT *o); +static int _exchangeConnectMsgs(AQH_TOOL_CLIENT *xo, uint32_t flags); +static int _exchangeAcceptedMsgGroups(AQH_TOOL_CLIENT *xo, uint32_t groups); + +static int _sendWaitAndHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo); static int _waitAndHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo, uint32_t msgId); -static int _exchangeConnect(AQH_TOOL_CLIENT *xo, uint32_t flags); +static int _nodesSendWaitAndHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo); +static int _nodesWaitAndHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo); + static AQH_MESSAGE *_createRequestMessage(AQH_OBJECT *o, uint32_t msgId); static int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, int first); @@ -45,7 +55,9 @@ static int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, const G -AQH_OBJECT *AQH_ToolClient_new(AQH_EVENT_LOOP *eventLoop, GWEN_DB_NODE *dbGlobalArgs, const GWEN_ARGS *argDescrs) +AQH_OBJECT *AQH_ToolClient_new(AQH_EVENT_LOOP *eventLoop, + uint8_t protoId, uint8_t protoVer, + GWEN_DB_NODE *dbGlobalArgs, const GWEN_ARGS *argDescrs) { AQH_OBJECT *o; AQH_TOOL_CLIENT *xo; @@ -55,6 +67,8 @@ AQH_OBJECT *AQH_ToolClient_new(AQH_EVENT_LOOP *eventLoop, GWEN_DB_NODE *dbGlobal GWEN_INHERIT_SETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o, xo, _freeData); xo->dbGlobalArgs=dbGlobalArgs; xo->args=argDescrs; + xo->protoId=protoId; + xo->protoVer=protoVer; return o; } @@ -72,6 +86,18 @@ void GWENHYWFAR_CB _freeData(GWEN_UNUSED void *bp, void *p) +AQH_OBJECT *AQH_ToolClient_GetEndpoint(const AQH_OBJECT *o) +{ + AQH_TOOL_CLIENT *xo; + + xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o); + if (xo) + return xo->ipcEndpoint; + return NULL; +} + + + int AQH_ToolClient_ReadLocalArgs(AQH_OBJECT *o, int argc, char **argv) { if (o) { @@ -216,18 +242,43 @@ int AQH_ToolClient_Run(AQH_OBJECT *o) return 2; } - rv=_exchangeConnect(xo, xo->flags); + rv=_exchangeConnectMsgs(xo, xo->flags); if (rv!=AQH_MSGDATA_RESULT_SUCCESS) { DBG_ERROR(NULL, "Connect response: %d", rv); return 2; } - return _sendWaitHandle(o, xo); + return _sendWaitAndHandle(o, xo); } return GWEN_ERROR_INVALID; } +int AQH_ToolClient_RunConnected(AQH_OBJECT *o) +{ + AQH_TOOL_CLIENT *xo; + + xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o); + if (xo) + return _sendWaitAndHandle(o, xo); + return GWEN_ERROR_INVALID; +} + + + +int AQH_ToolClient_RunConnectedWithNodeMsgs(AQH_OBJECT *o) +{ + AQH_TOOL_CLIENT *xo; + + xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o); + if (xo) + return _nodesSendWaitAndHandle(o, xo); + return GWEN_ERROR_INVALID; +} + + + + int AQH_ToolClient_Watch(AQH_OBJECT *o) { AQH_TOOL_CLIENT *xo; @@ -242,7 +293,7 @@ int AQH_ToolClient_Watch(AQH_OBJECT *o) return 2; } - rv=_exchangeConnect(xo, xo->flags); + rv=_exchangeConnectMsgs(xo, xo->flags); if (rv!=AQH_MSGDATA_RESULT_SUCCESS) { DBG_ERROR(NULL, "Connect response: %d", rv); return 2; @@ -254,6 +305,201 @@ int AQH_ToolClient_Watch(AQH_OBJECT *o) +int AQH_ToolClient_WatchConnected(AQH_OBJECT *o) +{ + AQH_TOOL_CLIENT *xo; + + xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o); + if (xo) + return _waitAndHandle(o, xo, 0); + return GWEN_ERROR_INVALID; +} + + + +int AQH_ToolClient_Connect(AQH_OBJECT *o, uint32_t connFlags, uint32_t connMsgFlags, uint32_t grps) +{ + AQH_TOOL_CLIENT *xo; + + xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o); + if (xo) { + int rv; + + rv=_connectEndpoint(o); + if (rv<0) { + DBG_INFO(NULL, "here(%d)", rv); + return rv; + } + if (connFlags & AQH_TOOL_CLIENT_CONNECTFLAGS_WITHCONNECTMSG) { + rv=_exchangeConnectMsgs(xo, connMsgFlags); + if (rv<0) { + DBG_INFO(NULL, "here(%d)", rv); + return rv; + } + } + + if (connFlags & AQH_TOOL_CLIENT_CONNECTFLAGS_WITHGRPMSG) { + rv=_exchangeAcceptedMsgGroups(xo, grps); + if (rv<0) { + DBG_INFO(NULL, "here(%d)", rv); + return rv; + } + } + + return 0; + } + return GWEN_ERROR_INVALID; +} + + + +int _connectEndpoint(AQH_OBJECT *o) +{ + AQH_TOOL_CLIENT *xo; + + xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o); + if (xo) { + xo->ipcEndpoint=Utils2_SetupBrokerClientEndpoint(AQH_Object_GetEventLoop(o), xo->dbLocalArgs, 0); + if (xo->ipcEndpoint==NULL) { + DBG_ERROR(NULL, "ERROR creating TCP connection"); + return 2; + } + return 0; + } + return GWEN_ERROR_INVALID; +} + + + +int _exchangeConnectMsgs(AQH_TOOL_CLIENT *xo, uint32_t flags) +{ + AQH_MESSAGE *msgOut; + uint32_t msgId; + const char *clientId; + const char *userId; + const char *passw; + + clientId=GWEN_DB_GetCharValue(xo->dbLocalArgs, "brokerClientId", 0, "aqhome-tool"); + userId=GWEN_DB_GetCharValue(xo->dbLocalArgs, "userId", 0, NULL); + passw=GWEN_DB_GetCharValue(xo->dbLocalArgs, "password", 0, NULL); + + msgId=AQH_Endpoint_GetNextMessageId(xo->ipcEndpoint); + msgOut=AQH_IpcMessageConnect_new(xo->protoId, xo->protoVer, + AQH_MSGTYPE_IPC_CONNECT_REQ, + msgId, 0, + clientId, userId, passw, flags); + AQH_Endpoint_AddMsgOut(xo->ipcEndpoint, msgOut); + return AQH_IpcEndpoint_WaitForResultMsg(xo->ipcEndpoint, + xo->protoId, xo->protoVer, AQH_MSGTYPE_IPC_RESULT, + msgId, xo->timeoutInSeconds); +} + + + +int _exchangeAcceptedMsgGroups(AQH_TOOL_CLIENT *xo, uint32_t groups) +{ + AQH_MESSAGE *msgOut; + uint32_t msgId; + + msgId=AQH_Endpoint_GetNextMessageId(xo->ipcEndpoint); + msgOut=AQH_IpcnMessageSetAcceptedMsgGroups_new(AQH_MSGTYPE_IPC_NODES_SETACCMSGGRPS, msgId, 0, groups); + AQH_Endpoint_AddMsgOut(xo->ipcEndpoint, msgOut); + return AQH_IpcEndpoint_WaitForResultMsg(xo->ipcEndpoint, + xo->protoId, xo->protoVer, AQH_MSGTYPE_IPC_RESULT, + msgId, xo->timeoutInSeconds); +} + + + +void AQH_ToolClient_SendNodeMsg(AQH_OBJECT *o, const AQH_MESSAGE *nodeMsg) +{ + AQH_TOOL_CLIENT *xo; + + xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o); + if (xo) { + AQH_MESSAGE *msgOut; + + msgOut=AQH_IpcnMessageForward_new(AQH_MSGTYPE_IPC_NODES_FORWARD, AQH_Endpoint_GetNextMessageId(xo->ipcEndpoint), 0, + AQH_Message_GetMsgPointer(nodeMsg), AQH_Message_GetUsedSize(nodeMsg)); + AQH_Endpoint_AddMsgOut(xo->ipcEndpoint, msgOut); + } +} + + + +AQH_MESSAGE *AQH_ToolClient_WaitForNodeMsg(AQH_OBJECT *o, int nodeSrcAddr, uint8_t nodeMsgType, int timeoutInSeconds) +{ + AQH_TOOL_CLIENT *xo; + + xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o); + if (xo) { + time_t startTime; + + startTime=time(NULL); + + for (;;) { + AQH_MESSAGE *msg; + time_t now; + + AQH_EventLoop_Run(AQH_Object_GetEventLoop(xo->ipcEndpoint), 500); + msg=AQH_Endpoint_GetNextMsgIn(xo->ipcEndpoint); + if (msg) { + GWEN_TAG16_LIST *tagList; + + tagList=AQH_IpcMessageTag16_ParsePayload(msg, 0); + if (tagList) { + uint16_t code; + + code=AQH_IpcMessage_GetCode(msg); + if (code==AQH_MSGTYPE_IPC_NODES_FORWARD) { + const GWEN_TAG16 *tag; + const uint8_t *ptr; + uint32_t len; + + tag=tagList?GWEN_Tag16_List_FindFirstByTagType(tagList, AQH_MSGNODE_FORWARD_TAGS_MSG):NULL; + ptr=tag?GWEN_Tag16_GetTagData(tag):NULL; + len=tag?GWEN_Tag16_GetTagLength(tag):0; + if (ptr && len) { + AQH_MESSAGE *nodeMsg; + + nodeMsg=AQH_NodeMessage_fromBuffer(ptr, len); + if (nodeMsg) { + if ((nodeSrcAddr==0xff || nodeSrcAddr==0x00 || nodeSrcAddr==AQH_NodeMessage_GetSourceAddress(nodeMsg)) && + (nodeMsgType==0 || nodeMsgType==AQH_NodeMessage_GetMsgType(nodeMsg))) { + GWEN_Tag16_List_free(tagList); + AQH_Message_free(msg); + return nodeMsg; + } + AQH_Message_free(nodeMsg); + } + } + else { + DBG_ERROR(NULL, "Empty node msg"); + } + } + else { + DBG_ERROR(NULL, "Received unexpected message %d (%x), ignoring", code, code); + } + GWEN_Tag16_List_free(tagList); + } + AQH_Message_free(msg); + } + + now=time(NULL); + if (now-startTime>timeoutInSeconds) { + DBG_INFO(NULL, "Timeout"); + break; + } + } + } + return NULL; +} + + + + + + int AQH_ToolClient_HandleResultMsg(GWEN_UNUSED const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, GWEN_UNUSED int first) { int result; @@ -271,7 +517,7 @@ int AQH_ToolClient_HandleResultMsg(GWEN_UNUSED const AQH_MESSAGE *msg, const GWE -int _sendWaitHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo) +int _sendWaitAndHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo) { AQH_MESSAGE *msgOut; uint32_t msgId; @@ -324,24 +570,53 @@ int _waitAndHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo, uint32_t msgId) -int _exchangeConnect(AQH_TOOL_CLIENT *xo, uint32_t flags) +int _nodesSendWaitAndHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo) { - const char *clientId; - const char *userId; - const char *passw; + AQH_MESSAGE *nodeMsg; + AQH_MESSAGE *msgOut; + uint32_t msgId; - clientId=GWEN_DB_GetCharValue(xo->dbLocalArgs, "brokerClientId", 0, "aqhome-tool"); - userId=GWEN_DB_GetCharValue(xo->dbLocalArgs, "userId", 0, NULL); - passw=GWEN_DB_GetCharValue(xo->dbLocalArgs, "password", 0, NULL); + msgId=AQH_Endpoint_GetNextMessageId(xo->ipcEndpoint); + nodeMsg=_createRequestMessage(o, msgId); + if (nodeMsg==NULL) { + DBG_ERROR(NULL, "Error creating outbound message"); + return 2; + } + msgOut=AQH_IpcnMessageForward_new(AQH_MSGTYPE_IPC_NODES_FORWARD, msgId, 0, + AQH_Message_GetMsgPointer(nodeMsg), AQH_Message_GetUsedSize(nodeMsg)); + AQH_Message_free(nodeMsg); + AQH_Endpoint_AddMsgOut(xo->ipcEndpoint, msgOut); - return AQH_IpcEndpoint_ExchangeConnectMsg(xo->ipcEndpoint, - AQH_MSGTYPE_IPC_DATA_CONNECT_REQ, - AQH_MSGTYPE_IPC_DATA_RESULT, - clientId, - userId, - passw, - flags, - xo->timeoutInSeconds); + return _nodesWaitAndHandle(o, xo); +} + + + +int _nodesWaitAndHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo) +{ + int first=1; + + for (;;) { + AQH_MESSAGE *msgIn; + + msgIn=AQH_ToolClient_WaitForNodeMsg(o, 0, 0, xo->timeoutInSeconds); + if (msgIn) { + int rv; + + rv=_handleResponseMessage(o, msgIn, NULL, first); + AQH_Message_free(msgIn); + first=0; + if (rv<0) { + DBG_ERROR(NULL, "here (%d)", rv); + return 3; + } + else if (rv==1) { + DBG_ERROR(NULL, "Done."); + return 0; + } + } + } /* for */ + return 1; } diff --git a/apps/aqhome-tool/client.h b/apps/aqhome-tool/client.h index cd4775e..44cee91 100644 --- a/apps/aqhome-tool/client.h +++ b/apps/aqhome-tool/client.h @@ -16,15 +16,39 @@ #include +#define AQH_TOOL_CLIENT_NODEADDR 240 + +#define AQH_TOOL_CLIENT_CONNECTFLAGS_WITHCONNECTMSG 0x0001 +#define AQH_TOOL_CLIENT_CONNECTFLAGS_WITHGRPMSG 0x0002 + + + typedef AQH_MESSAGE* (*AQH_TOOLCLIENT_CREATEREQUESTMESSAGE_FN)(AQH_OBJECT *o, uint32_t msgId); typedef int (*AQH_TOOLCLIENT_HANDLERESPONSEMESSAGE_FN)(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, int first); -AQH_OBJECT *AQH_ToolClient_new(AQH_EVENT_LOOP *eventLoop, GWEN_DB_NODE *dbGlobalArgs, const GWEN_ARGS *argDescrs); +AQH_OBJECT *AQH_ToolClient_new(AQH_EVENT_LOOP *eventLoop, + uint8_t protoId, uint8_t protoVer, + GWEN_DB_NODE *dbGlobalArgs, const GWEN_ARGS *argDescrs); int AQH_ToolClient_ReadLocalArgs(AQH_OBJECT *o, int argc, char **argv); + int AQH_ToolClient_Run(AQH_OBJECT *o); int AQH_ToolClient_Watch(AQH_OBJECT *o); +int AQH_ToolClient_Connect(AQH_OBJECT *o, uint32_t connFlags, uint32_t connMsgFlags, uint32_t grps); +int AQH_ToolClient_RunConnected(AQH_OBJECT *o); +int AQH_ToolClient_WatchConnected(AQH_OBJECT *o); + +int AQH_ToolClient_RunConnectedWithNodeMsgs(AQH_OBJECT *o); + + +void AQH_ToolClient_SendNodeMsg(AQH_OBJECT *o, const AQH_MESSAGE *nodeMsg); +AQH_MESSAGE *AQH_ToolClient_WaitForNodeMsg(AQH_OBJECT *o, int nodeSrcAddr, uint8_t nodeMsgType, int timeoutInSeconds); + +AQH_OBJECT *AQH_ToolClient_GetEndpoint(const AQH_OBJECT *o); + + + GWEN_DB_NODE *AQH_ToolClient_GetDbGlobalArgs(const AQH_OBJECT *o); GWEN_DB_NODE *AQH_ToolClient_GetDbLocalArgs(const AQH_OBJECT *o); diff --git a/apps/aqhome-tool/client_p.h b/apps/aqhome-tool/client_p.h index c1cc813..e7c52bb 100644 --- a/apps/aqhome-tool/client_p.h +++ b/apps/aqhome-tool/client_p.h @@ -27,6 +27,9 @@ struct AQH_TOOL_CLIENT { AQH_OBJECT *ipcEndpoint; int timeoutInSeconds; uint32_t flags; + + uint8_t protoId; + uint8_t protoVer; }; diff --git a/apps/aqhome-tool/data/adddata.c b/apps/aqhome-tool/data/adddata.c index cea8394..cb83337 100644 --- a/apps/aqhome-tool/data/adddata.c +++ b/apps/aqhome-tool/data/adddata.c @@ -79,7 +79,7 @@ int AQH_Tool_AddDataPoint(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv) }; eventLoop=AQH_EventLoop_new(); - o=AQH_ToolClient_new(eventLoop, dbGlobalArgs, args); + o=AQH_ToolClient_new(eventLoop, AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, dbGlobalArgs, args); AQH_ToolClient_SetCreateRequestMessageFn(o, _createRequestMessage); rv=AQH_ToolClient_ReadLocalArgs(o, argc, argv); if (rv!=0) diff --git a/apps/aqhome-tool/data/getdatapoints.c b/apps/aqhome-tool/data/getdatapoints.c index b7fc7af..b94576b 100644 --- a/apps/aqhome-tool/data/getdatapoints.c +++ b/apps/aqhome-tool/data/getdatapoints.c @@ -84,7 +84,7 @@ int AQH_Tool_GetDataPoints(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv) }; eventLoop=AQH_EventLoop_new(); - o=AQH_ToolClient_new(eventLoop, dbGlobalArgs, args); + o=AQH_ToolClient_new(eventLoop, AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, dbGlobalArgs, args); AQH_ToolClient_SetCreateRequestMessageFn(o, _createRequestMessage); AQH_ToolClient_SetHandleResponseMessageFn(o, _handleResponseMessage); rv=AQH_ToolClient_ReadLocalArgs(o, argc, argv); diff --git a/apps/aqhome-tool/data/getdevices.c b/apps/aqhome-tool/data/getdevices.c index b1e7000..d017cfe 100644 --- a/apps/aqhome-tool/data/getdevices.c +++ b/apps/aqhome-tool/data/getdevices.c @@ -74,7 +74,7 @@ int AQH_Tool_GetDevices(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv) }; eventLoop=AQH_EventLoop_new(); - o=AQH_ToolClient_new(eventLoop, dbGlobalArgs, args); + o=AQH_ToolClient_new(eventLoop, AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, dbGlobalArgs, args); AQH_ToolClient_SetCreateRequestMessageFn(o, _createRequestMessage); AQH_ToolClient_SetHandleResponseMessageFn(o, _handleResponseMessage); rv=AQH_ToolClient_ReadLocalArgs(o, argc, argv); diff --git a/apps/aqhome-tool/data/getvalues.c b/apps/aqhome-tool/data/getvalues.c index 0b036ec..e1a093f 100644 --- a/apps/aqhome-tool/data/getvalues.c +++ b/apps/aqhome-tool/data/getvalues.c @@ -73,7 +73,7 @@ int AQH_Tool_GetValues(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv) }; eventLoop=AQH_EventLoop_new(); - o=AQH_ToolClient_new(eventLoop, dbGlobalArgs, args); + o=AQH_ToolClient_new(eventLoop, AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, dbGlobalArgs, args); AQH_ToolClient_SetCreateRequestMessageFn(o, _createRequestMessage); AQH_ToolClient_SetHandleResponseMessageFn(o, _handleResponseMessage); rv=AQH_ToolClient_ReadLocalArgs(o, argc, argv); diff --git a/apps/aqhome-tool/data/moddevice.c b/apps/aqhome-tool/data/moddevice.c index d6b124e..2c53f70 100644 --- a/apps/aqhome-tool/data/moddevice.c +++ b/apps/aqhome-tool/data/moddevice.c @@ -75,7 +75,7 @@ int AQH_Tool_ModDevice(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv) }; eventLoop=AQH_EventLoop_new(); - o=AQH_ToolClient_new(eventLoop, dbGlobalArgs, args); + o=AQH_ToolClient_new(eventLoop, AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, dbGlobalArgs, args); AQH_ToolClient_SetCreateRequestMessageFn(o, _createRequestMessage); rv=AQH_ToolClient_ReadLocalArgs(o, argc, argv); if (rv!=0) diff --git a/apps/aqhome-tool/data/setdata.c b/apps/aqhome-tool/data/setdata.c index 4ef1008..c366926 100644 --- a/apps/aqhome-tool/data/setdata.c +++ b/apps/aqhome-tool/data/setdata.c @@ -73,7 +73,7 @@ int AQH_Tool_SetData(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv) }; eventLoop=AQH_EventLoop_new(); - o=AQH_ToolClient_new(eventLoop, dbGlobalArgs, args); + o=AQH_ToolClient_new(eventLoop, AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, dbGlobalArgs, args); AQH_ToolClient_SetCreateRequestMessageFn(o, _createRequestMessage); rv=AQH_ToolClient_ReadLocalArgs(o, argc, argv); if (rv!=0) diff --git a/apps/aqhome-tool/data/watch.c b/apps/aqhome-tool/data/watch.c index aff6cb2..5ef55e4 100644 --- a/apps/aqhome-tool/data/watch.c +++ b/apps/aqhome-tool/data/watch.c @@ -75,7 +75,7 @@ int AQH_Tool_Watch(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv) }; eventLoop=AQH_EventLoop_new(); - o=AQH_ToolClient_new(eventLoop, dbGlobalArgs, args); + o=AQH_ToolClient_new(eventLoop, AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, dbGlobalArgs, args); AQH_ToolClient_AddFlags(o, AQH_ENDPOINT_FLAGS_WANTUPDATES); AQH_ToolClient_SetHandleResponseMessageFn(o, _handleResponseMessage); rv=AQH_ToolClient_ReadLocalArgs(o, argc, argv); diff --git a/apps/aqhome-tool/main.c b/apps/aqhome-tool/main.c index 4c75049..a062873 100644 --- a/apps/aqhome-tool/main.c +++ b/apps/aqhome-tool/main.c @@ -13,7 +13,6 @@ #include "./nodes/ping.h" #include "./nodes/flash.h" #include "./nodes/getnodes.h" -#include "./nodes/setnodevalue.h" #include "./data/getvalues.h" #include "./data/getdevices.h" #include "./data/adddata.h" @@ -87,7 +86,6 @@ int main(int argc, char **argv) const GWEN_FUNCS cmdDefArray[]= { GWEN_FE_DAH("ping", AQH_Tool_Ping, I18N("Ping a given node on the network")), GWEN_FE_DAH("flash", AQH_Tool_Flash, I18N("Flash a given node on the network")), - GWEN_FE_DAH("setnodevalue", AQH_Tool_SetNodeValue, I18N("Set a raw value in a node")), GWEN_FE_DAH("getnodes", AQH_Tool_GetNodes, I18N("Request list of known devices on the network")), GWEN_FE_DAH("getvalues", AQH_Tool_GetValues, I18N("Request list of known values on the data server")), GWEN_FE_DAH("getdevices", AQH_Tool_GetDevices, I18N("Request list of known devices on the data server")), diff --git a/apps/aqhome-tool/nodes/0BUILD b/apps/aqhome-tool/nodes/0BUILD index 9a91fb6..7563f9a 100644 --- a/apps/aqhome-tool/nodes/0BUILD +++ b/apps/aqhome-tool/nodes/0BUILD @@ -36,7 +36,6 @@ ping.h flash.h getnodes.h - setnodevalue.h @@ -45,7 +44,6 @@ ping.c flash.c getnodes.c - setnodevalue.c diff --git a/apps/aqhome-tool/nodes/flash.c b/apps/aqhome-tool/nodes/flash.c index 34ef1d1..40068fc 100644 --- a/apps/aqhome-tool/nodes/flash.c +++ b/apps/aqhome-tool/nodes/flash.c @@ -1,6 +1,6 @@ /**************************************************************************** * This file is part of the project AqHome. - * AqHome (c) by 2023 Martin Preuss, all rights reserved. + * 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. @@ -11,181 +11,124 @@ #endif #include "./flash.h" -#include "../utils.h" +#include "../client.h" -#include "aqhome/ipc/endpoint_ipc.h" -#include "aqhome/ipc/nodes/msg_ipc_setaccmsggrps.h" -#include "aqhome/ipc/nodes/msg_ipc_forward.h" -#include "aqhome/msg/msg_node.h" -#include "aqhome/msg/msg_flashready.h" -#include "aqhome/msg/msg_flashstart.h" -#include "aqhome/msg/msg_flashresponse.h" -#include "aqhome/msg/msg_flashdata.h" -#include "aqhome/msg/msg_flashend.h" -#include "aqhome/msg/msg_reboot.h" +#include "aqhome/msg/ipc/nodes/m_ipcn.h" +#include "aqhome/msg/node/m_node.h" +#include "aqhome/msg/node/m_flashready.h" +#include "aqhome/msg/node/m_flashstart.h" +#include "aqhome/msg/node/m_flashend.h" +#include "aqhome/msg/node/m_flashdata.h" +#include "aqhome/msg/node/m_flashresponse.h" +#include "aqhome/msg/node/m_reboot.h" #include "aqhome/hexfile/hexfile.h" #include "aqhome/hexfile/flashrecord.h" -#include #include #include -#include -#include #include + +/* ------------------------------------------------------------------------------------------------ + * defs + * ------------------------------------------------------------------------------------------------ + */ + #define I18S(msg) msg #define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg) -#define FLASH_TOOL_MAX_REPEAT 16 +#define A_ARG GWEN_ARGS_FLAGS_HAS_ARGUMENT +#define A_END (GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST) +#define A_CHAR GWEN_ArgsType_Char +#define A_INT GWEN_ArgsType_Int -/*#define DEBUG_FLASH*/ +#define FLASH_TOOL_MAX_REPEAT 16 +#define FLASH_TOOL_DEFAULT_TIMEOUTINSECS 5 -static int _doFlash(GWEN_DB_NODE *dbArgs); -static AQH_FLASHRECORD_LIST *_readHexfileIntoFlashRecordList(const char *hexFilename); +/* ------------------------------------------------------------------------------------------------ + * forward declarations + * ------------------------------------------------------------------------------------------------ + */ -static int _rebootNode(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid, int timeoutInSeconds); -static int _performFlashProcedure(GWEN_MSG_ENDPOINT *epTcp, +static int doFlash(AQH_OBJECT *o); +static int _rebootNode(AQH_OBJECT *o, unsigned int uid, int timeoutInSeconds); +static int _performFlashProcedure(AQH_OBJECT *o, unsigned int uid, const AQH_FLASHRECORD_LIST *flashRecordList, int pageSize, int timeoutInSeconds); +static int _flashStart(AQH_OBJECT *o, unsigned int uid, int timeoutInSeconds); +static int _flashRecord(AQH_OBJECT *o, + const AQH_FLASHRECORD *flashRecord, + uint16_t pageSize, + int timeoutInSeconds); +static int _flashData(AQH_OBJECT *o, uint32_t address, const uint8_t *ptr, uint32_t len, int timeoutInSeconds); +static int _flashEnd(AQH_OBJECT *o, int reason, int timeoutInSeconds); +static int _waitForFlashResponse(AQH_OBJECT *o, int timeoutInSeconds); +static AQH_FLASHRECORD_LIST *_readHexfileIntoFlashRecordList(const char *hexFilename); -static int _flashStart(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid, int timeoutInSeconds); - - -static GWEN_MSG *_waitForFlashReadyMessageForUid(GWEN_MSG_ENDPOINT *epTcp, - unsigned int uid, int timeoutInSeconds); -static int _sendRebootRequest(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid); -static int _sendFlashStart(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid); -static int _waitForFlashResponseMessage(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds); -static int _waitForRebootResponseMessage(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds); -static int _sendFlashRecord(GWEN_MSG_ENDPOINT *epTcp, - const AQH_FLASHRECORD *flashRecord, - uint16_t pageSize, - int timeoutInSeconds); -static int _sendFlashEnd(GWEN_MSG_ENDPOINT *epTcp, int reason, int timeoutInSeconds); - +/* ------------------------------------------------------------------------------------------------ + * code + * ------------------------------------------------------------------------------------------------ + */ int AQH_Tool_Flash(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv) { - GWEN_DB_NODE *dbLocalArgs; + AQH_EVENT_LOOP *eventLoop; + AQH_OBJECT *o; int rv; const GWEN_ARGS args[]= { - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Char, /* type */ - "tcpAddress", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "t", /* short option */ - "tcpaddress", /* long option */ - I18S("Specify TCP address to connect to (defaults to 127.0.0.1)"), - I18S("Specify TCP address to connect to (defaults to 127.0.0.1)") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Int, /* type */ - "tcpPort", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "P", /* short option */ - "tcpport", /* long option */ - I18S("Specify the TCP port to listen on"), - I18S("Specify the TCP port to listen on") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Char, /* type */ - "uid", /* name */ - 1, /* minnum */ - 1, /* maxnum */ - "u", /* short option */ - "uid", /* long option */ - I18S("Specify UID of the node to flash"), - I18S("Specify UID of the node to flash)") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Char, /* type */ - "hexFilename", /* name */ - 1, /* minnum */ - 1, /* maxnum */ - "H", /* short option */ - NULL, /* long option */ - I18S("Specify hexfile to flash"), - I18S("Specify hexfile to flash)") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Int, /* type */ - "timeout", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "T", /* short option */ - NULL, /* long option */ - I18S("Specify timeout in seconds for PONG response"), - I18S("Specify timeout in seconds for PONG response") - }, - { - 0, /* flags */ - GWEN_ArgsType_Int, /* type */ - "reboot", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "R", /* short option */ - NULL, /* long option */ - I18S("Request node reboot before trying to flash"), - I18S("Request node reboot before trying to flash") - }, - { - GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */ - GWEN_ArgsType_Int, /* type */ - "help", /* name */ - 0, /* minnum */ - 0, /* maxnum */ - "h", /* short option */ - "help", /* long option */ - "Show this help screen", /* short description */ - "Show this help screen" /* long description */ - } + /* flags type name min max s long short_descr, long_descr */ + { A_ARG, A_CHAR, "tcpAddress", 0, 1, "t", "tcpaddress", I18S("TCP address to connect to [127.0.0.1]"), NULL}, + { A_ARG, A_INT, "tcpPort", 0, 1, "P", "tcpport", I18S("Specify the TCP port to listen on"), NULL}, + { A_ARG, A_INT, "timeout", 0, 1, "T", NULL, I18S("Specify timeout in seconds for response"), NULL}, + { A_ARG, A_CHAR, "userId", 0, 1, "u", "userid", I18S("Specify user id"), NULL}, + { A_ARG, A_CHAR, "password", 0, 1, "p", "password", I18S("Specify service password"), NULL}, + { A_ARG, A_CHAR, "uid", 1, 1, "u", "uid", I18S("Specify UID of the node to flash"),NULL}, + { A_ARG, A_CHAR, "hexFilename", 1, 1, "H", NULL, I18S("Specify hexfile to flash"), NULL}, + { 0, A_INT, "reboot", 0, 1, "R", NULL, I18S("Request node reboot before trying to flash"), NULL}, + { A_END, A_INT, "help", 0, 0, "h", "help", I18S("Show this help screen"), NULL} }; - dbLocalArgs=GWEN_DB_GetGroup(dbGlobalArgs, GWEN_DB_FLAGS_DEFAULT, "local"); - rv=GWEN_Args_Check(argc, argv, 1, - GWEN_ARGS_MODE_ALLOW_FREEPARAM, - args, - dbLocalArgs); - if (rv==GWEN_ARGS_RESULT_ERROR) { - fprintf(stderr, "ERROR: Could not parse arguments\n"); - return 1; - } - else if (rv==GWEN_ARGS_RESULT_HELP) { - GWEN_BUFFER *ubuf; + eventLoop=AQH_EventLoop_new(); + o=AQH_ToolClient_new(eventLoop, AQH_IPC_PROTOCOL_NODES_ID, AQH_IPC_PROTOCOL_NODES_VERSION, dbGlobalArgs, args); + rv=AQH_ToolClient_ReadLocalArgs(o, argc, argv); + if (rv!=0) + return rv; - ubuf=GWEN_Buffer_new(0, 1024, 0, 1); - if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) { - fprintf(stderr, "ERROR: Could not create help string\n"); - return 1; - } - fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf)); - GWEN_Buffer_free(ubuf); - return 0; + rv=AQH_ToolClient_Connect(o, AQH_TOOL_CLIENT_CONNECTFLAGS_WITHCONNECTMSG | AQH_TOOL_CLIENT_CONNECTFLAGS_WITHGRPMSG, + 0, AQH_MSG_TYPEGROUP_FLASH); + if (rv<0) { + DBG_INFO(NULL, "here(%d)", rv); + AQH_Object_free(o); + AQH_EventLoop_free(eventLoop); + return rv; } - return _doFlash(dbLocalArgs); + rv=doFlash(o); + if (rv<0) { + DBG_INFO(NULL, "here(%d)", rv); + AQH_Object_free(o); + AQH_EventLoop_free(eventLoop); + return rv; + } + + AQH_Object_free(o); + AQH_EventLoop_free(eventLoop); + return 0; } -int _doFlash(GWEN_DB_NODE *dbArgs) +int doFlash(AQH_OBJECT *o) { - GWEN_MSG_ENDPOINT *epTcp; + GWEN_DB_NODE *dbArgs; int rv; int timeoutInSeconds; int doReboot; @@ -193,17 +136,21 @@ int _doFlash(GWEN_DB_NODE *dbArgs) unsigned int uid; const char *hexFilename; AQH_FLASHRECORD_LIST *flashRecordList=NULL; - GWEN_MSG *msg; + AQH_MESSAGE *msg; uint16_t pageSize; + dbArgs=AQH_ToolClient_GetDbLocalArgs(o); + /* read data */ - timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 30); + timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, FLASH_TOOL_DEFAULT_TIMEOUTINSECS); doReboot=GWEN_DB_GetIntValue(dbArgs, "reboot", 0, 0); s=GWEN_DB_GetCharValue(dbArgs, "uid", 0, NULL); if (1!=sscanf(s, "%x", &uid)) { DBG_ERROR(NULL, "Bad uid \"%s\"", s); return 1; } + + /* read hex file */ hexFilename=GWEN_DB_GetCharValue(dbArgs, "hexFilename", 0, NULL); flashRecordList=_readHexfileIntoFlashRecordList(hexFilename); if (flashRecordList==NULL) { @@ -211,27 +158,12 @@ int _doFlash(GWEN_DB_NODE *dbArgs) return 2; } - /* setup client connection */ - epTcp=Utils_SetupNodesClientEndpoint(dbArgs); - if (epTcp==NULL) { - DBG_ERROR(NULL, "ERROR creating TCP connection"); - return 3; - } - - /* declare accepted message type groups */ - rv=Utils_SendAcceptedMsgGroups(epTcp, AQH_MSG_TYPEGROUP_FLASH); - if (rv<0) { - DBG_INFO(NULL, "here (%d)", rv); - GWEN_MsgEndpoint_free(epTcp); - return 3; - } - + /* probably reboot node */ if (doReboot) { fprintf(stdout, "Sending REBOOT request\n"); - rv=_rebootNode(epTcp, uid, timeoutInSeconds); + rv=_rebootNode(o, uid, timeoutInSeconds); if (rv<0) { DBG_INFO(NULL, "here (%d)", rv); - GWEN_MsgEndpoint_free(epTcp); return 3; } fprintf(stdout, "Reboot in progress\n"); @@ -239,35 +171,258 @@ int _doFlash(GWEN_DB_NODE *dbArgs) /* wait for FLASH_READY message */ fprintf(stdout, "Waiting for node to become ready for flashing\n"); - msg=_waitForFlashReadyMessageForUid(epTcp, uid, timeoutInSeconds); + msg=AQH_ToolClient_WaitForNodeMsg(o, 0, AQH_MSG_TYPE_FLASH_READY, timeoutInSeconds); if (msg==NULL) { DBG_INFO(NULL, "No FLASH_READY message received."); - GWEN_MsgEndpoint_free(epTcp); return 3; } DBG_INFO(NULL, "FLASH_READY message received"); - pageSize=AQH_FlashReadyMsg_GetPagesize(msg); + pageSize=AQH_FlashReadyMessage_GetPagesize(msg); fprintf(stdout, "Node is ready for flashing (pagesize=%d bytes)\n", pageSize); - GWEN_Msg_free(msg); + AQH_Message_free(msg); /* perform flash */ - rv=_performFlashProcedure(epTcp, uid, flashRecordList, pageSize, timeoutInSeconds); + rv=_performFlashProcedure(o, uid, flashRecordList, pageSize, timeoutInSeconds); if (rv<0) { if (rv<0) { DBG_INFO(NULL, "here (%d)", rv); - GWEN_MsgEndpoint_free(epTcp); AQH_FlashRecord_List_free(flashRecordList); return 4; } } AQH_FlashRecord_List_free(flashRecordList); - GWEN_MsgEndpoint_free(epTcp); return 0; } +int _rebootNode(AQH_OBJECT *o, unsigned int uid, int timeoutInSeconds) +{ + AQH_MESSAGE *nodeMsg; + + /* send REBOOT_REQUEST message */ + fprintf(stdout, "- sending REBOOT request\n"); + DBG_INFO(NULL, "Sending REBOOT REQUEST message"); + nodeMsg=AQH_RebootMessage_new(0, 0xff, AQH_MSG_TYPE_REBOOT_REQ, uid); + if (nodeMsg==NULL) { + DBG_ERROR(NULL, "Error creating message"); + return GWEN_ERROR_GENERIC; + } + + AQH_ToolClient_SendNodeMsg(o, nodeMsg); + nodeMsg=AQH_ToolClient_WaitForNodeMsg(o, 0, AQH_MSG_TYPE_REBOOT_RSP, timeoutInSeconds); + if (nodeMsg==NULL) { + DBG_INFO(NULL, "Bad or no reboot response received."); + return GWEN_ERROR_GENERIC; + } + + DBG_INFO(NULL, "REBOOT_RESPONSE message received"); + AQH_Message_free(nodeMsg); + return 0; +} + + + +int _performFlashProcedure(AQH_OBJECT *o, + unsigned int uid, + const AQH_FLASHRECORD_LIST *flashRecordList, + int pageSize, + int timeoutInSeconds) +{ + int rv; + const AQH_FLASHRECORD *flashRecord; + + rv=_flashStart(o, uid, timeoutInSeconds); + if (rv<0) { + DBG_INFO(NULL, "here (%d)", rv); + return rv; + } + + flashRecord=AQH_FlashRecord_List_First(flashRecordList); + while(flashRecord) { + DBG_ERROR(NULL, "Sending flash record at %08x", AQH_FlashRecord_GetAddress(flashRecord)); + rv=_flashRecord(o, flashRecord, pageSize, timeoutInSeconds); + if (rv!=0) { + DBG_ERROR(NULL, "Error sending flash data (%d)", rv); + return rv; + } + flashRecord=AQH_FlashRecord_List_Next(flashRecord); + } + + rv=_flashEnd(o, 0, timeoutInSeconds); + if (rv<0) { + DBG_INFO(NULL, "here (%d)", rv); + return rv; + } + DBG_ERROR(NULL, "Flash finished."); + return 0; +} + + + +int _flashStart(AQH_OBJECT *o, unsigned int uid, int timeoutInSeconds) +{ + int i; + + for (i=0; ipageSize)?pageSize:len; + rv=_flashData(o, address, ptr, sendLen, timeoutInSeconds); + if (rv<0) { + DBG_INFO(NULL, "here (%d)", rv); + return rv; + } + ptr+=sendLen; + address+=sendLen; + len-=sendLen; + } /* while */ + DBG_ERROR(NULL, "Full record sent and acknowledged"); + return 0; +} + + + +int _flashData(AQH_OBJECT *o, uint32_t address, const uint8_t *ptr, uint32_t len, int timeoutInSeconds) +{ + int i; + + for (i=0;i ACK received.\n"); - break; - } - else { - DBG_ERROR(NULL, "Negative response to flash end message (%d)", rv); - } - } /* for */ - if (i>=FLASH_TOOL_MAX_REPEAT) { - DBG_ERROR(NULL, "Too many errors (tried %d times), giving up", i); - return GWEN_ERROR_IO; - } - return 0; -} - - - -int _sendFlashRecord(GWEN_MSG_ENDPOINT *epTcp, - const AQH_FLASHRECORD *flashRecord, - uint16_t pageSize, - int timeoutInSeconds) -{ - const uint8_t *ptr; - uint32_t len; - uint32_t address; - - ptr=AQH_FlashRecord_GetDataPointer(flashRecord); - len=AQH_FlashRecord_GetDataLength(flashRecord); - address=AQH_FlashRecord_GetAddress(flashRecord); - while(ptr && len) { - uint32_t sendLen; - int i; - - sendLen=(len>pageSize)?pageSize:len; - for (i=0;i ACK received.\n"); - break; - } - else { - DBG_ERROR(NULL, "Negative response to flash data message (%d)", rv); - } - } /* for */ - if (i>=FLASH_TOOL_MAX_REPEAT) { - DBG_ERROR(NULL, "Too many errors (tried %d times), giving up", i); - /* TODO: send flash end message */ - return GWEN_ERROR_IO; - } - ptr+=sendLen; - address+=sendLen; - len-=sendLen; - } /* while */ - return 0; -} - - - - - diff --git a/apps/aqhome-tool/nodes/getnodes.c b/apps/aqhome-tool/nodes/getnodes.c index 0c5ea8f..770103f 100644 --- a/apps/aqhome-tool/nodes/getnodes.c +++ b/apps/aqhome-tool/nodes/getnodes.c @@ -1,6 +1,6 @@ /**************************************************************************** * This file is part of the project AqHome. - * AqHome (c) by 2023 Martin Preuss, all rights reserved. + * 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. @@ -11,213 +11,156 @@ #endif #include "./getnodes.h" +#include "../client.h" #include "../utils.h" -#include "aqhome/ipc/endpoint_ipc.h" -#include "aqhome/ipc/nodes/msg_ipc_getdevices_req.h" -#include "aqhome/ipc/nodes/msg_ipc_getdevices_rsp.h" -#include "aqhome/ipc/msg_ipc_result.h" -#include "aqhome/msg/msg_node.h" +#include "aqhome/msg/ipc/m_ipc.h" +#include "aqhome/msg/ipc/m_ipc_result.h" +#include "aqhome/msg/ipc/nodes/m_ipcn.h" +#include "aqhome/msg/ipc/nodes/m_ipcn_getdevices_req.h" +#include "aqhome/msg/ipc/nodes/m_ipcn_getdevices_rsp.h" +#include #include #include #include #include -#include -#include #include + +/* ------------------------------------------------------------------------------------------------ + * defs + * ------------------------------------------------------------------------------------------------ + */ + #define I18S(msg) msg #define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg) +#define A_ARG GWEN_ARGS_FLAGS_HAS_ARGUMENT +#define A_END (GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST) +#define A_CHAR GWEN_ArgsType_Char +#define A_INT GWEN_ArgsType_Int -static int _doGetNodes(GWEN_DB_NODE *dbArgs); -static int _sendGetDevices(GWEN_MSG_ENDPOINT *epTcp); -static void _printNodeFromMsg(const GWEN_MSG *msg); + +/* ------------------------------------------------------------------------------------------------ + * forward declarations + * ------------------------------------------------------------------------------------------------ + */ + +static AQH_MESSAGE *_createRequestMessage(AQH_OBJECT *o, uint32_t msgId); +static int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, int first); +static void _printNode(const AQH_NODE_INFO *ni); static void _printUintAsTextOrHex(uint32_t u, int bits); +/* ------------------------------------------------------------------------------------------------ + * code + * ------------------------------------------------------------------------------------------------ + */ + int AQH_Tool_GetNodes(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv) { - GWEN_DB_NODE *dbLocalArgs; + AQH_EVENT_LOOP *eventLoop; + AQH_OBJECT *o; int rv; const GWEN_ARGS args[]= { - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Char, /* type */ - "tcpAddress", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "t", /* short option */ - "tcpaddress", /* long option */ - I18S("Specify TCP address to connect to (defaults to 127.0.0.1)"), - I18S("Specify TCP address to connect to (defaults to 127.0.0.1)") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Int, /* type */ - "tcpPort", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "P", /* short option */ - "tcpport", /* long option */ - I18S("Specify the TCP port to listen on"), - I18S("Specify the TCP port to listen on") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Int, /* type */ - "timeout", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "T", /* short option */ - NULL, /* long option */ - I18S("Specify timeout in seconds for PONG response"), - I18S("Specify timeout in seconds for PONG response") - }, - { - GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */ - GWEN_ArgsType_Int, /* type */ - "help", /* name */ - 0, /* minnum */ - 0, /* maxnum */ - "h", /* short option */ - "help", /* long option */ - "Show this help screen", /* short description */ - "Show this help screen" /* long description */ - } + /* flags type name min max s long short_descr, long_descr */ + { A_ARG, A_CHAR, "tcpAddress", 0, 1, "t", "tcpaddress", I18S("TCP address to connect to [127.0.0.1]"), NULL}, + { A_ARG, A_INT, "tcpPort", 0, 1, "P", "tcpport", I18S("TCP port to connect to"), NULL}, + { A_ARG, A_INT, "timeout", 0, 1, "T", NULL, I18S("Specify timeout in seconds for response"), NULL}, + { 0, A_INT, "printHeader", 0, 1, "H", "printheader", I18S("Print header if given"), NULL}, + { A_END, A_INT, "help", 0, 0, "h", "help", I18S("Show this help screen"), NULL} }; - dbLocalArgs=GWEN_DB_GetGroup(dbGlobalArgs, GWEN_DB_FLAGS_DEFAULT, "local"); - rv=GWEN_Args_Check(argc, argv, 1, - GWEN_ARGS_MODE_ALLOW_FREEPARAM, - args, - dbLocalArgs); - if (rv==GWEN_ARGS_RESULT_ERROR) { - fprintf(stderr, "ERROR: Could not parse arguments\n"); - return 1; - } - else if (rv==GWEN_ARGS_RESULT_HELP) { - GWEN_BUFFER *ubuf; + eventLoop=AQH_EventLoop_new(); + o=AQH_ToolClient_new(eventLoop, AQH_IPC_PROTOCOL_NODES_ID, AQH_IPC_PROTOCOL_NODES_VERSION, dbGlobalArgs, args); + AQH_ToolClient_SetCreateRequestMessageFn(o, _createRequestMessage); + AQH_ToolClient_SetHandleResponseMessageFn(o, _handleResponseMessage); + rv=AQH_ToolClient_ReadLocalArgs(o, argc, argv); + if (rv!=0) + return rv; + rv=AQH_ToolClient_Run(o); + AQH_Object_free(o); + AQH_EventLoop_free(eventLoop); + return rv; +} - ubuf=GWEN_Buffer_new(0, 1024, 0, 1); - if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) { - fprintf(stderr, "ERROR: Could not create help string\n"); + + +AQH_MESSAGE *_createRequestMessage(GWEN_UNUSED AQH_OBJECT *o, uint32_t msgId) +{ + return AQH_IpcnMessageGetDevicesReq_new(AQH_MSGTYPE_IPC_NODES_GETDEVICES_REQ, msgId, 0); +} + + + +int _handleResponseMessage(GWEN_UNUSED AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, int first) +{ + uint16_t code; + //GWEN_DB_NODE *dbArgs; + //int printHeader; + + //dbArgs=AQH_ToolClient_GetDbLocalArgs(o); + //printHeader=first?GWEN_DB_GetIntValue(dbArgs, "printHeader", 0, 0):0; + + code=AQH_IpcMessage_GetCode(msg); + if (code==AQH_MSGTYPE_IPC_NODES_GETDEVICES_RSP) { + AQH_NODE_INFO *ni; + + ni=AQH_IpcnMessageGetDevicesRsp_ReadNodeInfo(tagList); + if (ni) + _printNode(ni); + + if (AQH_IpcnMessageGetDevicesRsp_GetFlags(tagList) & AQH_MSGNODE_GETDEVICES_RSP_FLAGS_LASTMSG) { + DBG_INFO(NULL, "Last message received"); return 1; } - fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf)); - GWEN_Buffer_free(ubuf); return 0; } - - return _doGetNodes(dbLocalArgs); + else if (code==AQH_MSGTYPE_IPC_NODES_RESULT) + return AQH_ToolClient_HandleResultMsg(msg, tagList, first); + else { + DBG_INFO(NULL, "Unexpected message \"%d\"", code); + return 3; + } } -int _doGetNodes(GWEN_DB_NODE *dbArgs) -{ - GWEN_MSG_ENDPOINT *epTcp; - int rv; - int timeoutInSeconds; - epTcp=Utils_SetupNodesClientEndpoint(dbArgs); - if (epTcp==NULL) { - DBG_ERROR(NULL, "ERROR creating TCP connection"); - return 2; - } - - timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5); - - rv=Utils_SendAcceptedMsgGroups(epTcp, 0); - if (rv<0) { - DBG_INFO(NULL, "here (%d)", rv); - return 2; - } - - rv=_sendGetDevices(epTcp); - if (rv<0) { - DBG_INFO(NULL, "here (%d)", rv); - return 2; - } - - for (;;) { - GWEN_MSG *msg; - uint16_t code; - - msg=Utils_WaitForSpecificIpcMessage(epTcp, AQH_MSGTYPE_IPC_NODES_GETDEVICES_RSP, timeoutInSeconds); - if (msg==NULL) { - DBG_INFO(NULL, "No GET_DEVICE response received."); - GWEN_MsgEndpoint_free(epTcp); - return 2; - } - code=GWEN_IpcMsg_GetCode(msg); - if (code==AQH_MSGTYPE_IPC_NODES_RESULT) { - fprintf(stdout, "No device list (%d)\n", AQH_ResultIpcMsg_GetResultCode(msg)); - GWEN_Msg_free(msg); - break; - } - else if (code==AQH_MSGTYPE_IPC_NODES_GETDEVICES_RSP) { - uint8_t flags; - - flags=AQH_GetDevicesResponseIpcMsg_GetFlags(msg); - _printNodeFromMsg(msg); - - GWEN_Msg_free(msg); - if (flags & AQH_MSGIPC_GETDEVICES_RSP_FLAGS_LAST) { - DBG_INFO(NULL, "Last"); - break; - } - } - else { - GWEN_Msg_free(msg); - return GWEN_ERROR_GENERIC; - } - } - - GWEN_MsgEndpoint_free(epTcp); - return 0; -} - - - -void _printNodeFromMsg(const GWEN_MSG *msg) +void _printNode(const AQH_NODE_INFO *ni) { uint32_t u; - int64_t ts64; - GWEN_TIMESTAMP *ts; - - ts64=AQH_GetDevicesResponseIpcMsg_GetTimestamp(msg); - ts=GWEN_Timestamp_fromInt64(ts64); /* TODO: fix timestamp */ + const GWEN_TIMESTAMP *ts; fprintf(stdout, "- node: addr=%d, uid=0x%08x, device=", - AQH_GetDevicesResponseIpcMsg_GetBusAddress(msg), - (unsigned int) AQH_GetDevicesResponseIpcMsg_GetUid(msg)); - u=AQH_GetDevicesResponseIpcMsg_GetManufacturer(msg); + AQH_NodeInfo_GetBusAddress(ni), + (unsigned int) AQH_NodeInfo_GetUid(ni)); + ts=AQH_NodeInfo_GetTimestampLastChange(ni); + + u=AQH_NodeInfo_GetManufacturer(ni); _printUintAsTextOrHex(u, 32); fprintf(stdout, ":"); - u=AQH_GetDevicesResponseIpcMsg_GetDeviceType(msg); + u=AQH_NodeInfo_GetDeviceType(ni); _printUintAsTextOrHex(u, 16); - u=AQH_GetDevicesResponseIpcMsg_GetDeviceVersion(msg); + u=AQH_NodeInfo_GetDeviceVersion(ni); fprintf(stdout, " v%d.%d", (u>>8) & 0xff, u & 0xff); - u=AQH_GetDevicesResponseIpcMsg_GetFirmwareVersion(msg); + u=AQH_NodeInfo_GetFirmwareVersion(ni); fprintf(stdout, ", firmware=%d.%d.%d (%d), ", (u>>16) & 0xff, (u>>8) & 0xff, u & 0xff, (u>>24) & 0xff); - if (ts) { + if (ts) fprintf(stdout, "last seen %s, ", GWEN_Timestamp_GetString(ts)); - GWEN_Timestamp_free(ts); - } fprintf(stdout, "pkg out: %d, pkg in: %d, collisions: %d, busy: %d, crc: %d, io: %d\n", - AQH_GetDevicesResponseIpcMsg_GetPkgOut(msg), - AQH_GetDevicesResponseIpcMsg_GetPkgIn(msg), - AQH_GetDevicesResponseIpcMsg_GetCollisions(msg), - AQH_GetDevicesResponseIpcMsg_GetBusy(msg), - AQH_GetDevicesResponseIpcMsg_GetCrcErrors(msg), - AQH_GetDevicesResponseIpcMsg_GetIoErrors(msg)); + AQH_NodeInfo_GetStatsPacketsOut(ni), + AQH_NodeInfo_GetStatsPacketsIn(ni), + AQH_NodeInfo_GetStatsCollisions(ni), + AQH_NodeInfo_GetStatsBusy(ni), + AQH_NodeInfo_GetStatsCrcErrors(ni), + AQH_NodeInfo_GetStatsIoErrors(ni)); } @@ -250,22 +193,3 @@ void _printUintAsTextOrHex(uint32_t u, int bits) } -int _sendGetDevices(GWEN_MSG_ENDPOINT *epTcp) -{ - GWEN_MSG *msgOut; - - msgOut=AQH_GetDevicesRequestIpcMsg_new(AQH_MSGTYPE_IPC_NODES_GETDEVICES_REQ, - GWEN_MsgEndpoint_GetNextMessageId(epTcp), 0); - 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/nodes/ping.c b/apps/aqhome-tool/nodes/ping.c index 60d0fd1..7cbddc4 100644 --- a/apps/aqhome-tool/nodes/ping.c +++ b/apps/aqhome-tool/nodes/ping.c @@ -1,6 +1,6 @@ /**************************************************************************** * This file is part of the project AqHome. - * AqHome (c) by 2023 Martin Preuss, all rights reserved. + * 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. @@ -11,181 +11,128 @@ #endif #include "./ping.h" +#include "../client.h" #include "../utils.h" -#include "aqhome/ipc/nodes/msg_ipc_setaccmsggrps.h" -#include "aqhome/ipc/nodes/msg_ipc_ping.h" -#include "aqhome/ipc/nodes/msg_ipc_forward.h" -#include "aqhome/ipc/endpoint_ipc.h" -#include "aqhome/msg/msg_node.h" +#include "aqhome/msg/ipc/m_ipc.h" +#include "aqhome/msg/ipc/m_ipc_result.h" +#include "aqhome/msg/ipc/nodes/m_ipcn.h" +#include "aqhome/msg/ipc/nodes/m_ipcn_forward.h" +#include "aqhome/msg/node/m_node.h" +#include "aqhome/msg/node/m_ping.h" +#include "aqhome/msg/node/m_pong.h" #include #include #include #include -#include -#include +/* ------------------------------------------------------------------------------------------------ + * defs + * ------------------------------------------------------------------------------------------------ + */ #define I18S(msg) msg #define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg) +#define A_ARG GWEN_ARGS_FLAGS_HAS_ARGUMENT +#define A_END (GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST) +#define A_CHAR GWEN_ArgsType_Char +#define A_INT GWEN_ArgsType_Int -static int _doPing(GWEN_DB_NODE *dbArgs); -static int _sendPing(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr); +/* ------------------------------------------------------------------------------------------------ + * forward declarations + * ------------------------------------------------------------------------------------------------ + */ + +static AQH_MESSAGE *_createRequestMessage(AQH_OBJECT *o, uint32_t msgId); +static int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, int first); +/* ------------------------------------------------------------------------------------------------ + * implementations + * ------------------------------------------------------------------------------------------------ + */ + int AQH_Tool_Ping(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv) { - GWEN_DB_NODE *dbLocalArgs; + AQH_EVENT_LOOP *eventLoop; + AQH_OBJECT *o; int rv; const GWEN_ARGS args[]= { - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Char, /* type */ - "tcpAddress", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "t", /* short option */ - "tcpaddress", /* long option */ - I18S("Specify TCP address to connect to (defaults to 127.0.0.1)"), - I18S("Specify TCP address to connect to (defaults to 127.0.0.1)") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Int, /* type */ - "tcpPort", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "P", /* short option */ - "tcpport", /* long option */ - I18S("Specify the TCP port to listen on"), - I18S("Specify the TCP port to listen on") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Int, /* type */ - "nodeAddr", /* name */ - 1, /* minnum */ - 1, /* maxnum */ - "n", /* short option */ - "nodeaddr", /* long option */ - I18S("Specify bus addr of the node to ping"), - I18S("Specify bus addr of the node to ping") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Int, /* type */ - "timeout", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "T", /* short option */ - NULL, /* long option */ - I18S("Specify timeout in seconds for PONG response"), - I18S("Specify timeout in seconds for PONG response") - }, - { - GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */ - GWEN_ArgsType_Int, /* type */ - "help", /* name */ - 0, /* minnum */ - 0, /* maxnum */ - "h", /* short option */ - "help", /* long option */ - "Show this help screen", /* short description */ - "Show this help screen" /* long description */ - } + /* flags type name min max s long short_descr, long_descr */ + { A_ARG, A_CHAR, "tcpAddress", 0, 1, "t", "tcpaddress", I18S("TCP address to connect to [127.0.0.1]"), NULL}, + { A_ARG, A_INT, "tcpPort", 0, 1, "P", "tcpport", I18S("Specify the TCP port to listen on"), NULL}, + { A_ARG, A_INT, "timeout", 0, 1, "T", NULL, I18S("Specify timeout in seconds for response"), NULL}, + { A_ARG, A_CHAR, "userId", 0, 1, "u", "userid", I18S("Specify user id"), NULL}, + { A_ARG, A_CHAR, "password", 0, 1, "p", "password", I18S("Specify service password"), NULL}, + { A_ARG, A_INT, "nodeAddr", 1, 1, "n", "nodeaddr", I18S("Specify bus addr of the node to ping"), NULL}, + { A_END, A_INT, "help", 0, 0, "h", "help", I18S("Show this help screen"), NULL} }; - dbLocalArgs=GWEN_DB_GetGroup(dbGlobalArgs, GWEN_DB_FLAGS_DEFAULT, "local"); - rv=GWEN_Args_Check(argc, argv, 1, - GWEN_ARGS_MODE_ALLOW_FREEPARAM, - args, - dbLocalArgs); - if (rv==GWEN_ARGS_RESULT_ERROR) { - fprintf(stderr, "ERROR: Could not parse arguments\n"); - return 1; - } - else if (rv==GWEN_ARGS_RESULT_HELP) { - GWEN_BUFFER *ubuf; + eventLoop=AQH_EventLoop_new(); + o=AQH_ToolClient_new(eventLoop, AQH_IPC_PROTOCOL_NODES_ID, AQH_IPC_PROTOCOL_NODES_VERSION, dbGlobalArgs, args); + AQH_ToolClient_SetCreateRequestMessageFn(o, _createRequestMessage); + AQH_ToolClient_SetHandleResponseMessageFn(o, _handleResponseMessage); + rv=AQH_ToolClient_ReadLocalArgs(o, argc, argv); + if (rv!=0) + return rv; - ubuf=GWEN_Buffer_new(0, 1024, 0, 1); - if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) { - fprintf(stderr, "ERROR: Could not create help string\n"); - return 1; - } - fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf)); - GWEN_Buffer_free(ubuf); + rv=AQH_ToolClient_Connect(o, AQH_TOOL_CLIENT_CONNECTFLAGS_WITHCONNECTMSG | AQH_TOOL_CLIENT_CONNECTFLAGS_WITHGRPMSG, + 0, AQH_MSG_TYPEGROUP_ALL); + if (rv<0) { + DBG_INFO(NULL, "here(%d)", rv); + AQH_Object_free(o); + AQH_EventLoop_free(eventLoop); + return rv; + } + + rv=AQH_ToolClient_RunConnectedWithNodeMsgs(o); + AQH_Object_free(o); + AQH_EventLoop_free(eventLoop); + return rv; +} + + + +AQH_MESSAGE *_createRequestMessage(GWEN_UNUSED AQH_OBJECT *o, GWEN_UNUSED uint32_t msgId) +{ + AQH_MESSAGE *nodeMsg; + GWEN_DB_NODE *dbArgs; + int nodeAddr; + + dbArgs=AQH_ToolClient_GetDbLocalArgs(o); + nodeAddr=GWEN_DB_GetIntValue(dbArgs, "nodeAddr", 0, 0); + + nodeMsg=AQH_PingMessage_new(nodeAddr, AQH_TOOL_CLIENT_NODEADDR, AQH_MSG_TYPE_PING, 0); + return nodeMsg; +} + + + +int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, GWEN_UNUSED const GWEN_TAG16_LIST *tagList, GWEN_UNUSED int first) +{ + GWEN_DB_NODE *dbArgs; + uint16_t code; + int nodeAddr; + + dbArgs=AQH_ToolClient_GetDbLocalArgs(o); + nodeAddr=GWEN_DB_GetIntValue(dbArgs, "nodeAddr", 0, 0); + + code=AQH_NodeMessage_GetMsgType(msg); + if ((code==AQH_MSG_TYPE_PONG) && + (nodeAddr==0 || nodeAddr==0xff || nodeAddr==AQH_NodeMessage_GetSourceAddress(msg))) { return 0; } + else { + DBG_INFO(NULL, "Unexpected message \"%d\"", code); + return 3; + } - return _doPing(dbLocalArgs); } -int _doPing(GWEN_DB_NODE *dbArgs) -{ - GWEN_MSG_ENDPOINT *epTcp; - int rv; - int nodeAddr; - int timeoutInSeconds; - GWEN_MSG *msg; - - epTcp=Utils_SetupNodesClientEndpoint(dbArgs); - if (epTcp==NULL) { - DBG_ERROR(NULL, "ERROR creating TCP connection"); - return 2; - } - - nodeAddr=GWEN_DB_GetIntValue(dbArgs, "nodeAddr", 0, 0); - timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5); - - rv=Utils_SendAcceptedMsgGroups(epTcp, AQH_MSG_TYPEGROUP_ALL); - if (rv<0) { - DBG_INFO(NULL, "here (%d)", rv); - return 2; - } - - fprintf(stdout, "Sending PING request\n"); - - rv=_sendPing(epTcp, nodeAddr); - if (rv<0) { - DBG_INFO(NULL, "here (%d)", rv); - return 2; - } - - msg=Utils_WaitForSpecificNodeMessage(epTcp, AQH_MSG_TYPE_PONG, nodeAddr, timeoutInSeconds); - if (msg==NULL) { - DBG_INFO(NULL, "No PONG response received."); - return 2; - } - - fprintf(stdout, "PONG response received\n"); - GWEN_MsgEndpoint_free(epTcp); - return 0; -} - - - -int _sendPing(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr) -{ - GWEN_MSG *msgOut; - - msgOut=AQH_PingIpcMsg_new(AQH_MSGTYPE_IPC_NODES_PING, GWEN_MsgEndpoint_GetNextMessageId(epTcp), 0, nodeAddr); - 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/nodes/setnodevalue.c b/apps/aqhome-tool/nodes/setnodevalue.c deleted file mode 100644 index 4b3a2a2..0000000 --- a/apps/aqhome-tool/nodes/setnodevalue.c +++ /dev/null @@ -1,297 +0,0 @@ -/**************************************************************************** - * This file is part of the project AqHome. - * AqHome (c) by 2024 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 "./setnodevalue.h" -#include "../utils.h" - -#include "aqhome/ipc/nodes/msg_ipc_setaccmsggrps.h" -#include "aqhome/ipc/nodes/msg_ipc_ping.h" -#include "aqhome/ipc/nodes/msg_ipc_forward.h" -#include "aqhome/ipc/endpoint_ipc.h" -#include "aqhome/msg/msg_node.h" -#include "aqhome/msg/msg_value3.h" - -#include -#include -#include -#include - -#include -#include - - -#define I18S(msg) msg -#define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg) - - - -static int _doSetValue(GWEN_DB_NODE *dbArgs); -static GWEN_MSG *_waitForSetValueResponse(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr, int msgId, int timeoutInSeconds); - - - -int AQH_Tool_SetNodeValue(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv) -{ - GWEN_DB_NODE *dbLocalArgs; - int rv; - const GWEN_ARGS args[]= { - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Char, /* type */ - "tcpAddress", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "t", /* short option */ - "tcpaddress", /* long option */ - I18S("Specify TCP address to connect to (defaults to 127.0.0.1)"), - I18S("Specify TCP address to connect to (defaults to 127.0.0.1)") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Int, /* type */ - "tcpPort", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "P", /* short option */ - "tcpport", /* long option */ - I18S("Specify the TCP port to listen on"), - I18S("Specify the TCP port to listen on") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Int, /* type */ - "nodeAddr", /* name */ - 1, /* minnum */ - 1, /* maxnum */ - "n", /* short option */ - "nodeaddr", /* long option */ - I18S("Specify bus addr of the node to send to"), - I18S("Specify bus addr of the node to send to") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Int, /* type */ - "timeout", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "T", /* short option */ - NULL, /* long option */ - I18S("Specify timeout in seconds for response"), - I18S("Specify timeout in seconds for response") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Int, /* type */ - "valueId", /* name */ - 1, /* minnum */ - 1, /* maxnum */ - "V", /* short option */ - "valueId", /* long option */ - I18S("Specify id of value to change"), - I18S("Specify id of value to change") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Int, /* type */ - "value", /* name */ - 1, /* minnum */ - 1, /* maxnum */ - "v", /* short option */ - "value", /* long option */ - I18S("Specify value to send"), - I18S("Specify value to send") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Int, /* type */ - "denom", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "d", /* short option */ - "denom", /* long option */ - I18S("Specify denominator to send"), - I18S("Specify denominator to send") - }, - { - GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ - GWEN_ArgsType_Int, /* type */ - "msgId", /* name */ - 0, /* minnum */ - 1, /* maxnum */ - "m", /* short option */ - "msgId", /* long option */ - I18S("Specify a msg id"), - I18S("Specify a msg id") - }, - { - GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */ - GWEN_ArgsType_Int, /* type */ - "help", /* name */ - 0, /* minnum */ - 0, /* maxnum */ - "h", /* short option */ - "help", /* long option */ - "Show this help screen", /* short description */ - "Show this help screen" /* long description */ - } - }; - - dbLocalArgs=GWEN_DB_GetGroup(dbGlobalArgs, GWEN_DB_FLAGS_DEFAULT, "local"); - rv=GWEN_Args_Check(argc, argv, 1, - GWEN_ARGS_MODE_ALLOW_FREEPARAM, - args, - dbLocalArgs); - if (rv==GWEN_ARGS_RESULT_ERROR) { - fprintf(stderr, "ERROR: Could not parse arguments\n"); - return 1; - } - else if (rv==GWEN_ARGS_RESULT_HELP) { - GWEN_BUFFER *ubuf; - - ubuf=GWEN_Buffer_new(0, 1024, 0, 1); - if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) { - fprintf(stderr, "ERROR: Could not create help string\n"); - return 1; - } - fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf)); - GWEN_Buffer_free(ubuf); - return 0; - } - - return _doSetValue(dbLocalArgs); -} - - - -int _doSetValue(GWEN_DB_NODE *dbArgs) -{ - GWEN_MSG_ENDPOINT *epTcp; - int rv; - int nodeAddr; - int timeoutInSeconds; - int valueId; - int value; - int denom; - int msgId; - GWEN_MSG *msgNode; - GWEN_MSG *msgOut; - GWEN_MSG *msg; - - epTcp=Utils_SetupNodesClientEndpoint(dbArgs); - if (epTcp==NULL) { - DBG_ERROR(NULL, "ERROR creating TCP connection"); - return 2; - } - - nodeAddr=GWEN_DB_GetIntValue(dbArgs, "nodeAddr", 0, 0); - timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5); - - valueId=GWEN_DB_GetIntValue(dbArgs, "valueId", 0, 0); - msgId=GWEN_DB_GetIntValue(dbArgs, "msgId", 0, 0); - value=GWEN_DB_GetIntValue(dbArgs, "value", 0, 0); - denom=GWEN_DB_GetIntValue(dbArgs, "denom", 0, 1); - - rv=Utils_SendAcceptedMsgGroups(epTcp, AQH_MSG_TYPEGROUP_ALL); - if (rv<0) { - DBG_INFO(NULL, "here (%d)", rv); - return 2; - } - - fprintf(stdout, "Sending SETVALUE request\n"); - msgNode=AQH_Value3Msg_new(0xc1, nodeAddr, AQH_MSG_TYPE_VALUE_SET, msgId, valueId, value, denom); - if (msgNode==NULL) { - DBG_ERROR(NULL, "Error creating message"); - return GWEN_ERROR_GENERIC; - } - msgOut=AQH_ForwardIpcMsg_new(AQH_MSGTYPE_IPC_NODES_FORWARD, - GWEN_MsgEndpoint_GetNextMessageId(epTcp), 0, - GWEN_Msg_GetConstBuffer(msgNode), GWEN_Msg_GetBytesInBuffer(msgNode)); - if (msgOut==NULL) { - DBG_ERROR(NULL, "Error creating message"); - return GWEN_ERROR_GENERIC; - } - GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut); - - msg=_waitForSetValueResponse(epTcp, nodeAddr, msgId, timeoutInSeconds); - if (msg==NULL) { - DBG_INFO(NULL, "No SETVALUE response received."); - return 2; - } - - if (AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_VALUE_SET_ACK) { - fprintf(stdout, "Value set\n"); - } - else { - fprintf(stdout, "Value not set\n"); - } - GWEN_Msg_free(msg); - GWEN_MsgEndpoint_free(epTcp); - return 0; -} - - - -GWEN_MSG *_waitForSetValueResponse(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr, int msgId, int timeoutInSeconds) -{ - time_t startTime; - - startTime=time(NULL); - - for (;;) { - GWEN_MSG *msg; - time_t now; - - while( (msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp)) ) { - if (GWEN_IpcMsg_GetCode(msg)==AQH_MSGTYPE_IPC_NODES_FORWARD) { - GWEN_MSG *nodeMsg; - - DBG_INFO(NULL, "Received IPC FORWARD message"); - nodeMsg=AQH_ForwardIpcMsg_GetCopyOfNodeMsg(msg); - if (nodeMsg) { - int recvNodeAddr; - int recvMsgType; - - recvNodeAddr=AQH_NodeMsg_GetSourceAddress(nodeMsg); - recvMsgType=AQH_NodeMsg_GetMsgType(nodeMsg); - DBG_INFO(AQH_LOGDOMAIN, "Received node msg from %d (%d)", recvNodeAddr, recvNodeAddr); - if ((nodeAddr==0 || recvNodeAddr==nodeAddr) && - (recvMsgType==AQH_MSG_TYPE_VALUE_SET_ACK || recvMsgType==AQH_MSG_TYPE_VALUE_SET_NACK)) { - int recvMsgId; - - recvMsgId=AQH_Value3Msg_GetMsgId(nodeMsg); - if (recvMsgId==msgId) { - GWEN_Msg_free(msg); - return nodeMsg; - } - } - } - } - else { - DBG_INFO(NULL, "Received IPC message %d, ignoring", GWEN_IpcMsg_GetCode(msg)); - } - GWEN_Msg_free(msg); - } /* while */ - now=time(NULL); - if (now-startTime>timeoutInSeconds) { - DBG_INFO(NULL, "Timeout"); - break; - } - GWEN_MsgEndpoint_IoLoop(epTcp, 2000); /* 2000 ms */ - } - - return NULL; -} - - - - - diff --git a/apps/aqhome-tool/utils.c b/apps/aqhome-tool/utils.c index 6ffdbe7..00c2228 100644 --- a/apps/aqhome-tool/utils.c +++ b/apps/aqhome-tool/utils.c @@ -13,22 +13,14 @@ #include "./utils.h" -#include "aqhome/ipc/endpoint_ipc.h" -#include "aqhome/ipc/nodes/msg_ipc_setaccmsggrps.h" -#include "aqhome/ipc/nodes/msg_ipc_forward.h" -#include "aqhome/ipc/data/ipc_data.h" -#include "aqhome/ipc/data/msg_data_connect.h" -#include "aqhome/ipc/msg_ipc_result.h" -#include "aqhome/ipc/endpoint_ipcclient.h" - #include "aqhome/msg/ipc/m_ipc.h" #include "aqhome/msg/ipc/data/m_ipcd.h" +#include "aqhome/msg/ipc/nodes/m_ipcn.h" +#include "aqhome/msg/ipc/nodes/m_ipcn_setaccmsggrps.h" #include "aqhome/ipc2/tcp_object.h" #include "aqhome/ipc2/ipc_client.h" -#include -#include #include #include #include @@ -78,315 +70,23 @@ AQH_OBJECT *Utils2_SetupBrokerClientEndpoint(AQH_EVENT_LOOP *eventLoop, GWEN_DB_ -GWEN_MSG_ENDPOINT *Utils_SetupBrokerClientEndpoint(GWEN_DB_NODE *dbArgs, uint32_t flags) +int Utils_SendAcceptedMsgGroups(AQH_OBJECT *ep, uint32_t groups) { - const char *brokerAddress; - int brokerPort; - const char *brokerClientId; + AQH_MESSAGE *msgOut; - brokerAddress=GWEN_DB_GetCharValue(dbArgs, "brokerAddress", 0, NULL); - if (!(brokerAddress && *brokerAddress)) - brokerAddress=GWEN_DB_GetCharValue(dbArgs, "ConfigFile/brokerAddress", 0, "127.0.0.1"); - - brokerPort=GWEN_DB_GetIntValue(dbArgs, "brokerPort", 0, -1); - if (brokerPort<0) - brokerPort=GWEN_DB_GetIntValue(dbArgs, "ConfigFile/brokerPort", 0, 45456); - - brokerClientId=GWEN_DB_GetCharValue(dbArgs, "brokerClientId", 0, "aqhome-tool"); - - if (brokerAddress && *brokerAddress && brokerPort) { - GWEN_MSG_ENDPOINT *ep; - GWEN_MSG_ENDPOINT *ipcBaseEndpoint; - int rv; - - ep=AQH_ClientIpcEndpoint_new("brokerIpcClient", 0); - GWEN_MsgEndpoint_AddFlags(ep, flags); - - ipcBaseEndpoint=AQH_IpcEndpoint_CreateIpcTcpClient(brokerAddress, brokerPort, "brokerPhysEndpoint", 0); - AQH_IpcEndpoint_SetServiceName(ipcBaseEndpoint, brokerClientId); - GWEN_MsgEndpoint_Tree2_AddChild(ep, ipcBaseEndpoint); - - rv=GWEN_MultilayerEndpoint_StartConnect(ep); - if (rv<0 && rv!=GWEN_ERROR_IN_PROGRESS) { - DBG_ERROR(NULL, "Error connecting to broker server %s:%d (%d), will retry later", brokerAddress, brokerPort, rv); - GWEN_MsgEndpoint_free(ep); - return NULL; - } - - return ep; - } - - return NULL; -} - - - -GWEN_MSG_ENDPOINT *Utils_SetupNodesClientEndpoint(GWEN_DB_NODE *dbArgs) -{ - return Utils_SetupIpcEndpoint(dbArgs, "tcpAddress", "tcpPort", "ConfigFile/nodesAddress", "ConfigFile/nodesPort", 45454); -} - - - -GWEN_MSG_ENDPOINT *Utils_SetupIpcEndpoint(GWEN_DB_NODE *dbArgs, - const char *varNameAddr, - const char *varNamePort, - const char *fileVarNameAddr, - const char *fileVarNamePort, - int defaultPort) -{ - GWEN_MSG_ENDPOINT *epTcp; - const char *tcpAddress; - int tcpPort; - int rv; - - tcpAddress=GWEN_DB_GetCharValue(dbArgs, varNameAddr, 0, NULL); - if (!(tcpAddress && *tcpAddress)) - tcpAddress=GWEN_DB_GetCharValue(dbArgs, fileVarNameAddr, 0, "127.0.0.1"); - - tcpPort=GWEN_DB_GetIntValue(dbArgs, varNamePort, 0, -1); - if (tcpPort<0) - tcpPort=GWEN_DB_GetIntValue(dbArgs, fileVarNamePort, 0, defaultPort); - - DBG_INFO(NULL, "Setup tcp client endpoint to %s:%d", tcpAddress, tcpPort); - epTcp=AQH_IpcEndpoint_CreateIpcTcpClient(tcpAddress, tcpPort, "aqhome-tool-IPC", 0); - if (epTcp==NULL) { - DBG_ERROR(NULL, "Error creating endpoint TCPc"); - return NULL; - } - GWEN_MsgEndpoint_SetDefaultMessageSize(epTcp, UTILS_IPC_ENDPOINT_DEFAULT_MSGSIZE); - rv=GWEN_TcpcEndpoint_StartConnect(epTcp); - if (rv<0 && rv!=GWEN_ERROR_IN_PROGRESS) { - DBG_ERROR(NULL, "Error connecting (%d)", rv); - GWEN_MsgEndpoint_free(epTcp); - return NULL; - } - - return epTcp; -} - - - -GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT *epTcp, - int msgCode, - int nodeAddr, - int timeoutInSeconds) -{ - time_t startTime; - - startTime=time(NULL); - - for (;;) { - GWEN_MSG *msg; - time_t now; - - while( (msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp)) ) { - if (GWEN_IpcMsg_GetCode(msg)==AQH_MSGTYPE_IPC_NODES_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; - } - } - } - else { - DBG_INFO(NULL, "Received IPC message %d, ignoring", GWEN_IpcMsg_GetCode(msg)); - } - GWEN_Msg_free(msg); - } /* while */ - now=time(NULL); - if (now-startTime>timeoutInSeconds) { - DBG_INFO(NULL, "Timeout"); - break; - } - GWEN_MsgEndpoint_IoLoop(epTcp, 2000); /* 2000 ms */ - } - - return NULL; -} - - - -GWEN_MSG *Utils_WaitForSpecificIpcMessage(GWEN_MSG_ENDPOINT *epTcp, - int msgCode, - int timeoutInSeconds) -{ - time_t startTime; - - startTime=time(NULL); - - for (;;) { - GWEN_MSG *msg; - time_t now; - - GWEN_MsgEndpoint_IoLoop(epTcp, 2000); /* 2000 ms */ - 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_DATA_RESULT) { - DBG_INFO(NULL, "Received IPC result message"); - return msg; - } - else { - DBG_INFO(NULL, "Received unexpected message %d (%x)", code, code); - } - GWEN_Msg_free(msg); - } - now=time(NULL); - if (now-startTime>timeoutInSeconds) { - DBG_INFO(NULL, "Timeout"); - break; - } - } - - return NULL; -} - - - -GWEN_MSG *Utils_WaitForResponse(GWEN_MSG_ENDPOINT *epTcp, uint32_t msgId, int timeoutInSeconds) -{ - time_t startTime; - - startTime=time(NULL); - - for (;;) { - GWEN_MSG *msg; - time_t now; - - GWEN_MsgEndpoint_IoLoop(epTcp, 2000); /* 2000 ms */ - msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp); - if (msg) { - if (GWEN_IpcMsg_GetRefMsgId(msg)==msgId) { - DBG_INFO(NULL, "Received expected IPC message"); - return msg; - } - else { - uint16_t code; - - code=GWEN_IpcMsg_GetCode(msg); - DBG_ERROR(NULL, - "Received unexpected message %d (%x) [msgId=%d, refMsgId=%d]", - code, code, GWEN_IpcMsg_GetMsgId(msg), GWEN_IpcMsg_GetRefMsgId(msg)); - } - GWEN_Msg_free(msg); - } - now=time(NULL); - if (now-startTime>timeoutInSeconds) { - DBG_ERROR(NULL, "Timeout"); - break; - } - } - - return NULL; -} - - - -int Utils_FlushOutMessageQueue(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds) -{ - time_t startTime; - - startTime=time(NULL); - - while(GWEN_MsgEndpoint_HaveMessageToSend(epTcp)) { - time_t now; - - GWEN_MsgEndpoint_IoLoop(epTcp, 2000); /* 2000 ms */ - 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_NODES_SETACCMSGGRPS, - GWEN_MsgEndpoint_GetNextMessageId(epTcp),0, - groups); + msgOut=AQH_IpcnMessageSetAcceptedMsgGroups_new(AQH_MSGTYPE_IPC_NODES_SETACCMSGGRPS, + AQH_Endpoint_GetNextMessageId(ep),0, + groups); if (msgOut==NULL) { DBG_ERROR(NULL, "Error creating message"); return GWEN_ERROR_GENERIC; } - GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut); + AQH_Endpoint_AddMsgOut(ep, msgOut); return 0; } -GWEN_MSG_ENDPOINT *Utils_OpenBrokerConnection(GWEN_DB_NODE *dbArgs, uint32_t flags, int timeoutInSeconds) -{ - GWEN_MSG_ENDPOINT *epTcp; - GWEN_MSG *msgOut; - GWEN_MSG *msgIn; - uint32_t result; - const char *clientId; - const char *userId; - const char *password; - - clientId=GWEN_DB_GetCharValue(dbArgs, "clientId", 0, NULL); - userId=GWEN_DB_GetCharValue(dbArgs, "userId", 0, NULL); - password=GWEN_DB_GetCharValue(dbArgs, "password", 0, NULL); - - epTcp=Utils_SetupBrokerClientEndpoint(dbArgs, 0); - if (epTcp==NULL) { - DBG_ERROR(NULL, "ERROR creating TCP connection"); - return NULL; - } - - msgOut=AQH_ConnectDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_CONNECT_REQ, - GWEN_MsgEndpoint_GetNextMessageId(epTcp), 0, - clientId, userId, password, flags); - if (msgOut==NULL) { - DBG_ERROR(NULL, "Error creating message"); - GWEN_MsgEndpoint_free(epTcp); - return NULL; - } - GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut); - - msgIn=Utils_WaitForSpecificIpcMessage(epTcp, AQH_MSGTYPE_IPC_DATA_RESULT, timeoutInSeconds); - if (msgIn==NULL) { - DBG_ERROR(NULL, "No response received"); - GWEN_MsgEndpoint_free(epTcp); - return NULL; - } - - result=AQH_ResultIpcMsg_GetResultCode(msgIn); - GWEN_Msg_free(msgIn); - - if (result!=AQH_MSG_IPC_SUCCESS) { - DBG_ERROR(NULL, "Response: %d", result); - GWEN_MsgEndpoint_free(epTcp); - return NULL; - } - - return epTcp; -} - - - void Utils_PrintDataPoints(const uint64_t *dataPoints, uint32_t numValues, const char *valueUnits) { uint32_t i; diff --git a/apps/aqhome-tool/utils.h b/apps/aqhome-tool/utils.h index d6c5fc0..575d9e7 100644 --- a/apps/aqhome-tool/utils.h +++ b/apps/aqhome-tool/utils.h @@ -24,30 +24,9 @@ AQH_OBJECT *Utils2_SetupBrokerClientEndpoint(AQH_EVENT_LOOP *eventLoop, GWEN_DB_NODE *dbArgs, uint32_t flags); AQH_MESSAGE *Utils2_WaitForResponseMsg(AQH_EVENT_LOOP *eventLoop, AQH_OBJECT *epTcp, uint32_t refMsgId, int timeoutInSeconds); +int Utils_SendAcceptedMsgGroups(AQH_OBJECT *ep, uint32_t groups); - -GWEN_MSG_ENDPOINT *Utils_SetupIpcEndpoint(GWEN_DB_NODE *dbArgs, - const char *varNameAddr, - const char *varNamePort, - const char *fileVarNameAddr, - const char *fileVarNamePort, - int defaultPort); -GWEN_MSG_ENDPOINT *Utils_SetupBrokerClientEndpoint(GWEN_DB_NODE *dbArgs, uint32_t flags); -GWEN_MSG_ENDPOINT *Utils_SetupNodesClientEndpoint(GWEN_DB_NODE *dbArgs); - -GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT *epTcp, int msgCode, int nodeAddr, int timeoutInSeconds); - -GWEN_MSG *Utils_WaitForSpecificIpcMessage(GWEN_MSG_ENDPOINT *epTcp, int msgCode, int timeoutInSeconds); - -GWEN_MSG *Utils_WaitForResponse(GWEN_MSG_ENDPOINT *epTcp, uint32_t msgId, int timeoutInSeconds); - -int Utils_FlushOutMessageQueue(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds); - -int Utils_SendAcceptedMsgGroups(GWEN_MSG_ENDPOINT *epTcp, uint32_t groups); - -GWEN_MSG_ENDPOINT *Utils_OpenBrokerConnection(GWEN_DB_NODE *dbArgs, uint32_t flags, int timeoutInSeconds); - void Utils_PrintDataPoints(const uint64_t *dataPoints, uint32_t numValues, const char *valueUnits); void Utils_PrintSingleDataPoint(uint64_t timestamp, double data, const char *valueUnits); void Utils_PrintMeanData(const uint64_t *dataPoints, uint32_t numValues, const char *valueUnits); diff --git a/aqhome/data/vars_dbread.c b/aqhome/data/vars_dbread.c index a894254..a2c99a7 100644 --- a/aqhome/data/vars_dbread.c +++ b/aqhome/data/vars_dbread.c @@ -168,7 +168,7 @@ const char *_readGroupOrVar(AQH_VARS **pVars, const char *src, GWEN_BUFFER *wbuf /* read untyped var, assume string */ s=_contAsVar(pVars, AQH_Vars_DataType_ValueString, s, GWEN_Buffer_GetStart(wbuf), wbuf); if (s==NULL) { - DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv); + DBG_INFO(AQH_LOGDOMAIN, "here"); return NULL; } } @@ -180,7 +180,7 @@ const char *_readGroupOrVar(AQH_VARS **pVars, const char *src, GWEN_BUFFER *wbuf else { s=_contAsTypedVar(pVars, s, GWEN_Buffer_GetStart(wbuf), wbuf); if (s==NULL) { - DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv); + DBG_INFO(AQH_LOGDOMAIN, "here"); return NULL; } } diff --git a/aqhome/ipc2/endpoint.c b/aqhome/ipc2/endpoint.c index f8c6b10..cef7b6b 100644 --- a/aqhome/ipc2/endpoint.c +++ b/aqhome/ipc2/endpoint.c @@ -333,6 +333,33 @@ void AQH_Endpoint_SetState(AQH_OBJECT *o, int i) +uint32_t AQH_Endpoint_GetAcceptedMsgGroups(const AQH_OBJECT *o) +{ + if (o) { + AQH_ENDPOINT *xo; + + xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_ENDPOINT, o); + if (xo) + return xo->acceptedMsgGroups; + } + return 0; +} + + + +void AQH_Endpoint_SetAcceptedMsgGroups(AQH_OBJECT *o, uint32_t i) +{ + if (o) { + AQH_ENDPOINT *xo; + + xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_ENDPOINT, o); + if (xo) + xo->acceptedMsgGroups=i; + } +} + + + AQH_MESSAGE_LIST *AQH_Endpoint_GetMsgOutList(const AQH_OBJECT *o) { if (o) { diff --git a/aqhome/ipc2/endpoint.h b/aqhome/ipc2/endpoint.h index 722ea50..18ebe32 100644 --- a/aqhome/ipc2/endpoint.h +++ b/aqhome/ipc2/endpoint.h @@ -56,6 +56,9 @@ AQHOME_API void AQH_Endpoint_SubFlags(AQH_OBJECT *o, uint32_t i); AQHOME_API int AQH_Endpoint_GetState(const AQH_OBJECT *o); AQHOME_API void AQH_Endpoint_SetState(AQH_OBJECT *o, int i); +AQHOME_API uint32_t AQH_Endpoint_GetAcceptedMsgGroups(const AQH_OBJECT *o); +AQHOME_API void AQH_Endpoint_SetAcceptedMsgGroups(AQH_OBJECT *o, uint32_t i); + AQHOME_API AQH_MESSAGE_LIST *AQH_Endpoint_GetMsgOutList(const AQH_OBJECT *o); AQHOME_API AQH_MESSAGE *AQH_Endpoint_GetNextMsgOut(AQH_OBJECT *o); AQHOME_API void AQH_Endpoint_AddMsgOut(AQH_OBJECT *o, AQH_MESSAGE *msg); diff --git a/aqhome/ipc2/endpoint_p.h b/aqhome/ipc2/endpoint_p.h index d2de283..9102b13 100644 --- a/aqhome/ipc2/endpoint_p.h +++ b/aqhome/ipc2/endpoint_p.h @@ -27,6 +27,8 @@ struct AQH_ENDPOINT { uint32_t flags; char *serviceName; char *userName; + + uint32_t acceptedMsgGroups; }; diff --git a/aqhome/ipc2/ipc_endpoint.c b/aqhome/ipc2/ipc_endpoint.c index e9a5087..9b5f768 100644 --- a/aqhome/ipc2/ipc_endpoint.c +++ b/aqhome/ipc2/ipc_endpoint.c @@ -14,34 +14,15 @@ #include "aqhome/msg/ipc/m_ipc.h" #include "aqhome/msg/ipc/m_ipc_result.h" #include "aqhome/msg/ipc/m_ipc_tag16.h" -#include "aqhome/msg/ipc/data/m_ipcd_connect.h" +#include "aqhome/msg/ipc/m_ipc_connect.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) +int AQH_IpcEndpoint_WaitForResultMsg(AQH_OBJECT *ipcEndpoint, + uint8_t protoId, uint8_t protoVer, uint16_t resultMsgCode, + uint32_t refMsgId, int timeoutInSeconds) { for (;;) { AQH_MESSAGE *msgIn; @@ -55,7 +36,9 @@ int AQH_IpcEndpoint_WaitForResultMsg(AQH_OBJECT *ipcEndpoint, uint16_t resultMsg int code; code=AQH_IpcMessage_GetCode(msgIn); - if (code==resultMsgCode) { + if (AQH_IpcMessage_GetProtoId(msgIn)==protoId && + AQH_IpcMessage_GetProtoVersion(msgIn)==protoVer && + code==resultMsgCode) { int result; result=AQH_IpcMessageResult_GetResult(tagList); diff --git a/aqhome/ipc2/ipc_endpoint.h b/aqhome/ipc2/ipc_endpoint.h index 2cf4960..0db64bf 100644 --- a/aqhome/ipc2/ipc_endpoint.h +++ b/aqhome/ipc2/ipc_endpoint.h @@ -14,18 +14,12 @@ -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 int AQH_IpcEndpoint_WaitForResultMsg(AQH_OBJECT *ipcEndpoint, + uint8_t protoId, uint8_t protoVer, 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, + uint8_t protoId, uint8_t protoVer, uint16_t code, uint32_t refMsgId, int result); diff --git a/aqhome/msg/ipc/0BUILD b/aqhome/msg/ipc/0BUILD index 84e7fcb..5d5043f 100644 --- a/aqhome/msg/ipc/0BUILD +++ b/aqhome/msg/ipc/0BUILD @@ -47,6 +47,7 @@ m_ipc.h m_ipc_tag16.h + m_ipc_connect.h m_ipc_result.h @@ -60,6 +61,7 @@ m_ipc.c m_ipc_tag16.c + m_ipc_connect.c m_ipc_result.c diff --git a/aqhome/msg/ipc/data/0BUILD b/aqhome/msg/ipc/data/0BUILD index f86f805..3962eb1 100644 --- a/aqhome/msg/ipc/data/0BUILD +++ b/aqhome/msg/ipc/data/0BUILD @@ -46,7 +46,6 @@ m_ipcd.h - m_ipcd_connect.h m_ipcd_multidata.h m_ipcd_devices.h m_ipcd_values.h @@ -63,7 +62,6 @@ $(local/typefiles) m_ipcd.c - m_ipcd_connect.c m_ipcd_multidata.c m_ipcd_devices.c m_ipcd_values.c diff --git a/aqhome/msg/ipc/data/m_ipcd.h b/aqhome/msg/ipc/data/m_ipcd.h index b52818b..d204416 100644 --- a/aqhome/msg/ipc/data/m_ipcd.h +++ b/aqhome/msg/ipc/data/m_ipcd.h @@ -22,9 +22,8 @@ -#define AQH_MSGTYPE_IPC_DATA_RESULT 0x0001 /* AQH_ResultIpcMsg */ - -#define AQH_MSGTYPE_IPC_DATA_CONNECT_REQ 0x0010 /* serviceName, userName, password */ +#define AQH_MSGTYPE_IPC_DATA_RESULT AQH_MSGTYPE_IPC_RESULT +#define AQH_MSGTYPE_IPC_DATA_CONNECT_REQ AQH_MSGTYPE_IPC_CONNECT_REQ #define AQH_MSGTYPE_IPC_DATA_UPDATEDATA 0x0100 /* AQH_MultiDataDataIpcMsg */ #define AQH_MSGTYPE_IPC_DATA_DATACHANGED 0x0200 /* AQH_MultiDataDataIpcMsg */ diff --git a/aqhome/msg/ipc/data/m_ipcd_connect.h b/aqhome/msg/ipc/data/m_ipcd_connect.h deleted file mode 100644 index f2006f0..0000000 --- a/aqhome/msg/ipc/data/m_ipcd_connect.h +++ /dev/null @@ -1,40 +0,0 @@ -/**************************************************************************** - * 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_M_IPCD_CONNECT_H -#define AQH_M_IPCD_CONNECT_H - - - -#include -#include - -#include - - -#define AQH_MSGDATA_CONNECT_TAGS_CLIENTID 0x0001 -#define AQH_MSGDATA_CONNECT_TAGS_USERID 0x0002 -#define AQH_MSGDATA_CONNECT_TAGS_PASSWORD 0x0003 -#define AQH_MSGDATA_CONNECT_TAGS_FLAGS 0x0004 - -#define AQH_MSGDATA_CONNECT_FLAGS_WANTUPDATES 0x0001 - - - -AQHOME_API AQH_MESSAGE *AQH_IpcdMessageConnect_new(uint16_t code, - uint32_t msgId, uint32_t refMsgId, - const char *clientId, const char *userId, const char *password, - uint32_t flags); - -AQHOME_API void AQH_IpcdMessageConnect_DumpToBuffer(const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, - GWEN_BUFFER *dbuf, const char *sText); - - - - -#endif diff --git a/aqhome/msg/ipc/m_ipc.h b/aqhome/msg/ipc/m_ipc.h index d110f4d..2868441 100644 --- a/aqhome/msg/ipc/m_ipc.h +++ b/aqhome/msg/ipc/m_ipc.h @@ -25,6 +25,8 @@ #define AQH_IPCMSG_OFFS_PAYLOAD 16 /* begin of payload for a given message */ +#define AQH_MSGTYPE_IPC_RESULT 0x0001 +#define AQH_MSGTYPE_IPC_CONNECT_REQ 0x0010 diff --git a/aqhome/msg/ipc/data/m_ipcd_connect.c b/aqhome/msg/ipc/m_ipc_connect.c similarity index 64% rename from aqhome/msg/ipc/data/m_ipcd_connect.c rename to aqhome/msg/ipc/m_ipc_connect.c index d3de792..3494d2a 100644 --- a/aqhome/msg/ipc/data/m_ipcd_connect.c +++ b/aqhome/msg/ipc/m_ipc_connect.c @@ -11,9 +11,8 @@ #endif -#include "aqhome/msg/ipc/data/m_ipcd_connect.h" +#include "aqhome/msg/ipc/m_ipc_connect.h" #include "aqhome/msg/ipc/m_ipc_tag16.h" -#include "aqhome/msg/ipc/data/m_ipcd.h" #include "aqhome/msg/ipc/m_ipc.h" #include @@ -34,22 +33,23 @@ * ------------------------------------------------------------------------------------------------ */ -AQH_MESSAGE *AQH_IpcdMessageConnect_new(uint16_t code, uint32_t msgId, uint32_t refMsgId, - const char *clientId, const char *userId, const char *password, uint32_t flags) +AQH_MESSAGE *AQH_IpcMessageConnect_new(uint8_t protoId, uint8_t protoVer, uint16_t code, + uint32_t msgId, uint32_t refMsgId, + const char *clientId, const char *userId, const char *password, uint32_t flags) { AQH_MESSAGE *msg; GWEN_BUFFER *buf; buf=GWEN_Buffer_new(0, 256, 0, 1); if (clientId && *clientId) - GWEN_Tag16_WriteStringTagToBuffer(AQH_MSGDATA_CONNECT_TAGS_CLIENTID, clientId, buf); + GWEN_Tag16_WriteStringTagToBuffer(AQH_MSG_CONNECT_TAGS_CLIENTID, clientId, buf); if (userId && *userId) - GWEN_Tag16_WriteStringTagToBuffer(AQH_MSGDATA_CONNECT_TAGS_USERID, userId, buf); + GWEN_Tag16_WriteStringTagToBuffer(AQH_MSG_CONNECT_TAGS_USERID, userId, buf); if (password && *password) - GWEN_Tag16_WriteStringTagToBuffer(AQH_MSGDATA_CONNECT_TAGS_PASSWORD, password, buf); - GWEN_Tag16_WriteUint32TagToBuffer(AQH_MSGDATA_CONNECT_TAGS_FLAGS, flags, buf); + GWEN_Tag16_WriteStringTagToBuffer(AQH_MSG_CONNECT_TAGS_PASSWORD, password, buf); + GWEN_Tag16_WriteUint32TagToBuffer(AQH_MSG_CONNECT_TAGS_FLAGS, flags, buf); - msg=AQH_IpcMessage_new(AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, code, msgId, refMsgId, + msg=AQH_IpcMessage_new(protoId, protoVer, code, msgId, refMsgId, GWEN_Buffer_GetUsedBytes(buf), (const uint8_t*) GWEN_Buffer_GetStart(buf)); GWEN_Buffer_free(buf); @@ -58,16 +58,16 @@ AQH_MESSAGE *AQH_IpcdMessageConnect_new(uint16_t code, uint32_t msgId, uint32_t -void AQH_IpcdMessageConnect_DumpToBuffer(const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, GWEN_BUFFER *dbuf, const char *sText) +void AQH_IpcMessageConnect_DumpToBuffer(const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, GWEN_BUFFER *dbuf, const char *sText) { char *clientId=NULL; char *userId=NULL; uint32_t flags=0; if (tagList) { - clientId=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSGDATA_CONNECT_TAGS_CLIENTID, NULL); - userId=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSGDATA_CONNECT_TAGS_USERID, NULL); - flags=AQH_Tag16_GetTagDataAsUint32(tagList, AQH_MSGDATA_CONNECT_TAGS_FLAGS, 0); + clientId=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSG_CONNECT_TAGS_CLIENTID, NULL); + userId=AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSG_CONNECT_TAGS_USERID, NULL); + flags=AQH_Tag16_GetTagDataAsUint32(tagList, AQH_MSG_CONNECT_TAGS_FLAGS, 0); } GWEN_Buffer_AppendArgs(dbuf, diff --git a/aqhome/msg/ipc/m_ipc_connect.h b/aqhome/msg/ipc/m_ipc_connect.h new file mode 100644 index 0000000..2897728 --- /dev/null +++ b/aqhome/msg/ipc/m_ipc_connect.h @@ -0,0 +1,40 @@ +/**************************************************************************** + * 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_M_IPCD_CONNECT_H +#define AQH_M_IPCD_CONNECT_H + + + +#include +#include + +#include + + +#define AQH_MSG_CONNECT_TAGS_CLIENTID 0x0001 +#define AQH_MSG_CONNECT_TAGS_USERID 0x0002 +#define AQH_MSG_CONNECT_TAGS_PASSWORD 0x0003 +#define AQH_MSG_CONNECT_TAGS_FLAGS 0x0004 + +#define AQH_MSG_CONNECT_FLAGS_WANTUPDATES 0x0001 + + + +AQHOME_API AQH_MESSAGE *AQH_IpcMessageConnect_new(uint8_t protoId, uint8_t protoVer, uint16_t code, + uint32_t msgId, uint32_t refMsgId, + const char *clientId, const char *userId, const char *password, + uint32_t flags); + +AQHOME_API void AQH_IpcMessageConnect_DumpToBuffer(const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, + GWEN_BUFFER *dbuf, const char *sText); + + + + +#endif diff --git a/aqhome/msg/ipc/nodes/0BUILD b/aqhome/msg/ipc/nodes/0BUILD index 3149e6f..6415793 100644 --- a/aqhome/msg/ipc/nodes/0BUILD +++ b/aqhome/msg/ipc/nodes/0BUILD @@ -49,6 +49,7 @@ m_ipcn_forward.h m_ipcn_getdevices_req.h m_ipcn_getdevices_rsp.h + m_ipcn_setaccmsggrps.h @@ -63,6 +64,7 @@ m_ipcn_forward.c m_ipcn_getdevices_req.c m_ipcn_getdevices_rsp.c + m_ipcn_setaccmsggrps.c diff --git a/aqhome/msg/ipc/nodes/m_ipcn.h b/aqhome/msg/ipc/nodes/m_ipcn.h index 5d45bc7..8d4e005 100644 --- a/aqhome/msg/ipc/nodes/m_ipcn.h +++ b/aqhome/msg/ipc/nodes/m_ipcn.h @@ -23,7 +23,8 @@ #define AQH_IPC_PROTOCOL_NODES_VERSION 1 -#define AQH_MSGTYPE_IPC_NODES_RESULT 0xf001 +#define AQH_MSGTYPE_IPC_NODES_RESULT AQH_MSGTYPE_IPC_RESULT +#define AQH_MSGTYPE_IPC_NODES_CONNECT_REQ AQH_MSGTYPE_IPC_CONNECT_REQ #define AQH_MSGTYPE_IPC_NODES_FORWARD 0xf100 #define AQH_MSGTYPE_IPC_NODES_VALUE 0xf200 diff --git a/aqhome/msg/ipc/nodes/m_ipcn_getdevices_rsp.c b/aqhome/msg/ipc/nodes/m_ipcn_getdevices_rsp.c index 36920da..f0b1464 100644 --- a/aqhome/msg/ipc/nodes/m_ipcn_getdevices_rsp.c +++ b/aqhome/msg/ipc/nodes/m_ipcn_getdevices_rsp.c @@ -52,6 +52,13 @@ AQH_MESSAGE *AQH_IpcnMessageGetDevicesRsp_new(uint16_t code, uint32_t msgId, uin +uint32_t AQH_IpcnMessageGetDevicesRsp_GetFlags(const GWEN_TAG16_LIST *tagList) +{ + return tagList?AQH_Tag16_GetTagDataAsUint32(tagList, AQH_MSGNODE_GETDEVICES_RSP_TAGS_FLAGS, 0):0; +} + + + AQH_NODE_INFO *AQH_IpcnMessageGetDevicesRsp_ReadNodeInfo(const GWEN_TAG16_LIST *tagList) { if (tagList) { diff --git a/aqhome/msg/ipc/nodes/m_ipcn_getdevices_rsp.h b/aqhome/msg/ipc/nodes/m_ipcn_getdevices_rsp.h index d74389f..a0a99c9 100644 --- a/aqhome/msg/ipc/nodes/m_ipcn_getdevices_rsp.h +++ b/aqhome/msg/ipc/nodes/m_ipcn_getdevices_rsp.h @@ -20,6 +20,7 @@ #include +#define AQH_MSGNODE_GETDEVICES_RSP_FLAGS_LASTMSG 0x01 #define AQH_MSGNODE_GETDEVICES_RSP_TAGS_FLAGS 0x01 #define AQH_MSGNODE_GETDEVICES_RSP_TAGS_NODEINFO 0xc2 @@ -28,6 +29,7 @@ AQHOME_API AQH_MESSAGE *AQH_IpcnMessageGetDevicesRsp_new(uint16_t code, uint32_t msgId, uint32_t refMsgId, uint32_t flags, const AQH_NODE_INFO *ni); +AQHOME_API uint32_t AQH_IpcnMessageGetDevicesRsp_GetFlags(const GWEN_TAG16_LIST *tagList); AQHOME_API AQH_NODE_INFO *AQH_IpcnMessageGetDevicesRsp_ReadNodeInfo(const GWEN_TAG16_LIST *tagList); AQHOME_API void AQH_IpcnMessageGetDevicesRsp_DumpToBuffer(const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, diff --git a/aqhome/msg/ipc/nodes/m_ipcn_setaccmsggrps.c b/aqhome/msg/ipc/nodes/m_ipcn_setaccmsggrps.c new file mode 100644 index 0000000..c7534e5 --- /dev/null +++ b/aqhome/msg/ipc/nodes/m_ipcn_setaccmsggrps.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 "aqhome/msg/ipc/nodes/m_ipcn_setaccmsggrps.h" +#include "aqhome/msg/ipc/nodes/m_ipcn.h" +#include "aqhome/msg/ipc/m_ipc_tag16.h" +#include "aqhome/msg/ipc/m_ipc.h" + +#include +#include +#include +#include + + + +/* ------------------------------------------------------------------------------------------------ + * forward declarations + * ------------------------------------------------------------------------------------------------ + */ + + + +/* ------------------------------------------------------------------------------------------------ + * implementation + * ------------------------------------------------------------------------------------------------ + */ + +AQH_MESSAGE *AQH_IpcnMessageSetAcceptedMsgGroups_new(uint16_t code, uint32_t msgId, uint32_t refMsgId, uint32_t grps) +{ + AQH_MESSAGE *msg; + GWEN_BUFFER *buf; + + buf=GWEN_Buffer_new(0, 256, 0, 1); + + GWEN_Tag16_WriteUint32TagToBuffer(AQH_MSGNODES_SETACCMSGGRPS_TAGS_GROUPS, grps, buf); + + msg=AQH_IpcMessage_new(AQH_IPC_PROTOCOL_NODES_ID, AQH_IPC_PROTOCOL_NODES_VERSION, code, msgId, refMsgId, + GWEN_Buffer_GetUsedBytes(buf), (const uint8_t*) GWEN_Buffer_GetStart(buf)); + GWEN_Buffer_free(buf); + return msg; +} + + + +uint32_t AQH_IpcnMessageSetAcceptedMsgGroups_GetGroups(const GWEN_TAG16_LIST *tagList) +{ + return tagList?AQH_Tag16_GetTagDataAsUint32(tagList, AQH_MSGNODES_SETACCMSGGRPS_TAGS_GROUPS, 0):0; +} + + + +void AQH_IpcnMessageSetAcceptedMsgGroups_DumpToBuffer(const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, + GWEN_BUFFER *dbuf, const char *sText) +{ + GWEN_Buffer_AppendArgs(dbuf, + "SET_ACCEPTED_MSG_GROUPS(%s) %s (code=%d, proto=%d, proto version=%d, groups=%08x)\n", + AQH_IpcnMessage_MsgTypeToChar(AQH_IpcMessage_GetCode(msg)), + sText?sText:"", + AQH_IpcMessage_GetCode(msg), + AQH_IpcMessage_GetProtoId(msg), + AQH_IpcMessage_GetProtoVersion(msg), + AQH_IpcnMessageSetAcceptedMsgGroups_GetGroups(tagList)); +} + + + + diff --git a/aqhome/msg/ipc/nodes/m_ipcn_setaccmsggrps.h b/aqhome/msg/ipc/nodes/m_ipcn_setaccmsggrps.h new file mode 100644 index 0000000..db21a1c --- /dev/null +++ b/aqhome/msg/ipc/nodes/m_ipcn_setaccmsggrps.h @@ -0,0 +1,42 @@ +/**************************************************************************** + * 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_MSGNODES_SETACCMSGGRPS_H +#define AQH_MSGNODES_SETACCMSGGRPS_H + + + +#include +#include +#include + +#include +#include + + + +#define AQH_MSGNODES_SETACCMSGGRPS_TAGS_GROUPS 0x0001 + + + +AQHOME_API AQH_MESSAGE *AQH_IpcnMessageSetAcceptedMsgGroups_new(uint16_t code, uint32_t msgId, uint32_t refMsgId, uint32_t grps); + +AQHOME_API void AQH_IpcdMessageSetAcceptedMsgGroups_DumpToBuffer(const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, + GWEN_BUFFER *dbuf, const char *sText); + +uint32_t AQH_IpcnMessageSetAcceptedMsgGroups_GetGroups(const GWEN_TAG16_LIST *tagList); + + +#endif + + + + + + +