From 53e076c2add23d790ff7baac9052b28468f49ecf Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Fri, 12 Sep 2025 15:06:26 +0200 Subject: [PATCH] added argument "DEVICENAME" to GETDEVICES request. faster if only a specific device is requested. --- apps/aqhome-data/s_getdevices.c | 134 +++++++++++++++++------- apps/aqhome-tool/data/devicestate.c | 3 +- aqhome/dataclient/client.c | 20 +++- aqhome/dataclient/client.h | 4 +- aqhome/msg/ipc/data/0BUILD | 2 + aqhome/msg/ipc/data/m_ipcd_getdevices.c | 76 ++++++++++++++ aqhome/msg/ipc/data/m_ipcd_getdevices.h | 34 ++++++ 7 files changed, 229 insertions(+), 44 deletions(-) create mode 100644 aqhome/msg/ipc/data/m_ipcd_getdevices.c create mode 100644 aqhome/msg/ipc/data/m_ipcd_getdevices.h diff --git a/apps/aqhome-data/s_getdevices.c b/apps/aqhome-data/s_getdevices.c index f255f05..d1c080c 100644 --- a/apps/aqhome-data/s_getdevices.c +++ b/apps/aqhome-data/s_getdevices.c @@ -16,11 +16,13 @@ #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_getdevices.h" #include "aqhome/msg/ipc/data/m_ipcd_devices.h" #include "aqhome/msg/ipc/m_ipc_result.h" #include "aqhome/msg/ipc/m_ipc_tag16.h" #include +#include @@ -38,7 +40,10 @@ * ------------------------------------------------------------------------------------------------ */ -static void _sendDeviceList(AQH_OBJECT *ep, const AQH_DEVICE_LIST *vl, uint32_t flags, uint32_t refMsgId); +static AQH_DEVICE_LIST *_getMatchingDeviceList(AQHOME_SERVER *xo, const GWEN_TAG16_LIST *tagList); +static int _deviceMatches(const AQH_DEVICE *dev, const char *deviceName); +static void _sendDeviceList(AQH_OBJECT *ep, const AQH_DEVICE_LIST *vl, uint32_t refMsgId); +static void _sendDeviceListMsg(AQH_OBJECT *ep, const AQH_DEVICE_LIST *vl, uint32_t flags, uint32_t refMsgId); @@ -47,63 +52,114 @@ static void _sendDeviceList(AQH_OBJECT *ep, const AQH_DEVICE_LIST *vl, uint32_t * ------------------------------------------------------------------------------------------------ */ -void AqHomeDataServer_HandleGetDevices(AQH_OBJECT *o, AQH_OBJECT *ep, const AQH_MESSAGE *msg, GWEN_UNUSED const GWEN_TAG16_LIST *tagList) +void AqHomeDataServer_HandleGetDevices(AQH_OBJECT *o, AQH_OBJECT *ep, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList) { AQHOME_SERVER *xo; xo=AqHomeDataServer_GetServerData(o); if (xo) { - const AQH_DEVICE_LIST *origDeviceList; + AQH_DEVICE_LIST *deviceList; uint32_t refMsgId; refMsgId=AQH_IpcMessage_GetMsgId(msg); DBG_INFO(NULL, "HandleGetDevices"); - origDeviceList=AQH_Storage_GetDeviceList(xo->storage); - if (origDeviceList) { - DBG_INFO(NULL, "Have a list of %d devices", AQH_Device_List_GetCount(origDeviceList)); - if (AQH_Device_List_GetCount(origDeviceList)=AQHOMEDATA_DEVICESPERMSG) { - DBG_INFO(NULL, "Sending %d devices", AQH_Device_List_GetCount(tmpDeviceList)); - _sendDeviceList(ep, tmpDeviceList, next?0:AQH_MSGDATA_DEVICES_FLAGS_LASTMSG, refMsgId); - AQH_Device_List_Clear(tmpDeviceList); - } - v=next; - } - if (AQH_Device_List_GetCount(tmpDeviceList)) { - DBG_INFO(NULL, "Sending %d devices", AQH_Device_List_GetCount(tmpDeviceList)); - _sendDeviceList(ep, tmpDeviceList, AQH_MSGDATA_DEVICES_FLAGS_LASTMSG, refMsgId); /* send remaining */ - } - AQH_Device_List_free(tmpDeviceList); - } + deviceList=_getMatchingDeviceList(xo, tagList); + if (deviceList) { + _sendDeviceList(ep, deviceList, refMsgId); + AQH_Device_List_free(deviceList); } else { /* empty list */ - _sendDeviceList(ep, NULL, AQH_MSGDATA_DEVICES_FLAGS_LASTMSG, refMsgId); + _sendDeviceListMsg(ep, NULL, AQH_MSGDATA_DEVICES_FLAGS_LASTMSG, refMsgId); } } } -void _sendDeviceList(AQH_OBJECT *ep, const AQH_DEVICE_LIST *vl, uint32_t flags, uint32_t refMsgId) +AQH_DEVICE_LIST *_getMatchingDeviceList(AQHOME_SERVER *xo, const GWEN_TAG16_LIST *tagList) +{ + const AQH_DEVICE_LIST *origDeviceList; + AQH_DEVICE_LIST *tmpDeviceList=NULL; + char *deviceName; + + deviceName=tagList?AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSGDATA_GETDEVICES_TAGS_DEVICENAME, NULL):NULL; + + origDeviceList=AQH_Storage_GetDeviceList(xo->storage); + if (origDeviceList) { + const AQH_DEVICE *dev; + + tmpDeviceList=AQH_Device_List_new(); + dev=AQH_Device_List_First(origDeviceList); + while(dev) { + if (_deviceMatches(dev, deviceName)) { + AQH_DEVICE *copyOfDevice; + + copyOfDevice=AQH_Device_dup(dev); + AQH_Device_List_Add(copyOfDevice, tmpDeviceList); + } + dev=AQH_Device_List_Next(dev); + } + if (AQH_Device_List_GetCount(tmpDeviceList)<1) { + AQH_Device_List_free(tmpDeviceList); + tmpDeviceList=NULL; + } + } + free(deviceName); + + return tmpDeviceList; +} + + + +int _deviceMatches(const AQH_DEVICE *dev, const char *deviceName) +{ + if (deviceName && *deviceName) { + const char *s; + + s=AQH_Device_GetNameForSystem(dev); + if (s && *s && GWEN_Text_ComparePattern(s, deviceName, 0)==-1) + return 0; + } + + return 1; +} + + + +void _sendDeviceList(AQH_OBJECT *ep, const AQH_DEVICE_LIST *deviceList, uint32_t refMsgId) +{ + AQH_DEVICE_LIST *tmpDeviceList; + const AQH_DEVICE *dev; + + DBG_INFO(NULL, "Sending entries in multiple messages"); + tmpDeviceList=AQH_Device_List_new(); + dev=AQH_Device_List_First(deviceList); + while(dev) { + const AQH_DEVICE *next; + AQH_DEVICE *copyOfDevice; + + next=AQH_Device_List_Next(dev); + copyOfDevice=AQH_Device_dup(dev); + AQH_Device_List_Add(copyOfDevice, tmpDeviceList); + if (AQH_Device_List_GetCount(tmpDeviceList)>=AQHOMEDATA_DEVICESPERMSG) { + DBG_INFO(NULL, "Sending %d devices", AQH_Device_List_GetCount(tmpDeviceList)); + _sendDeviceListMsg(ep, tmpDeviceList, next?0:AQH_MSGDATA_DEVICES_FLAGS_LASTMSG, refMsgId); + AQH_Device_List_Clear(tmpDeviceList); + } + dev=next; + } + if (AQH_Device_List_GetCount(tmpDeviceList)) { + DBG_INFO(NULL, "Sending %d devices", AQH_Device_List_GetCount(tmpDeviceList)); + _sendDeviceListMsg(ep, tmpDeviceList, AQH_MSGDATA_DEVICES_FLAGS_LASTMSG, refMsgId); /* send remaining */ + } + AQH_Device_List_free(tmpDeviceList); +} + + + +void _sendDeviceListMsg(AQH_OBJECT *ep, const AQH_DEVICE_LIST *vl, uint32_t flags, uint32_t refMsgId) { AQH_MESSAGE *msg; diff --git a/apps/aqhome-tool/data/devicestate.c b/apps/aqhome-tool/data/devicestate.c index 2b7a9b8..f5c0f83 100644 --- a/apps/aqhome-tool/data/devicestate.c +++ b/apps/aqhome-tool/data/devicestate.c @@ -20,7 +20,6 @@ #include "aqhome/msg/ipc/data/m_ipcd.h" #include "aqhome/msg/ipc/data/m_ipcd_getdata.h" #include "aqhome/msg/ipc/data/m_ipcd_multidata.h" -#include "aqhome/dataclient/client.h" #include #include @@ -129,7 +128,7 @@ int _runCommand(AQH_DATACLIENT *dc) dbLocalArgs=AQH_DataClient_GetDbLocalArgs(dc); deviceName=GWEN_DB_GetCharValue(dbLocalArgs, "device", 0, "*"); - deviceList=AQH_DataClient_GetDevices(dc); + deviceList=AQH_DataClient_GetDevices(dc, deviceName); if (deviceList==NULL) { DBG_ERROR(NULL, "Error getting devices"); return GWEN_ERROR_GENERIC; diff --git a/aqhome/dataclient/client.c b/aqhome/dataclient/client.c index 578db7b..d64526f 100644 --- a/aqhome/dataclient/client.c +++ b/aqhome/dataclient/client.c @@ -22,6 +22,7 @@ #include "aqhome/msg/ipc/data/m_ipcd_devices.h" #include "aqhome/msg/ipc/data/m_ipcd_values.h" #include "aqhome/msg/ipc/data/m_ipcd_getvalues.h" +#include "aqhome/msg/ipc/data/m_ipcd_getdevices.h" #include "aqhome/msg/ipc/data/m_ipcd_getdata.h" #include "aqhome/msg/ipc/data/m_ipcd_multidata.h" #include "aqhome/msg/ipc/data/m_ipcd_setdata.h" @@ -113,6 +114,21 @@ int AQH_DataClient_ReadLocalArgs(AQH_DATACLIENT *dc, +int AQH_DataClient_ReadConfigFile(AQH_DATACLIENT *dc) +{ + GWEN_DB_NODE *dbConfig; + + dbConfig=AQH_LoadConfigFile(); + if (dbConfig) { + GWEN_DB_Group_free(dc->dbLocalArgs); + dc->dbLocalArgs=dbConfig; + return 0; + } + return GWEN_ERROR_GENERIC; +} + + + GWEN_DB_NODE *AQH_DataClient_GetDbLocalArgs(const AQH_DATACLIENT *dc) { return dc?dc->dbLocalArgs:NULL; @@ -173,7 +189,7 @@ void AQH_DataClient_SetTimeout(AQH_DATACLIENT *dc, int i) -AQH_DEVICE_LIST *AQH_DataClient_GetDevices(AQH_DATACLIENT *dc) +AQH_DEVICE_LIST *AQH_DataClient_GetDevices(AQH_DATACLIENT *dc, const char *deviceName) { if (dc) { AQH_MESSAGE *msgOut; @@ -183,7 +199,7 @@ AQH_DEVICE_LIST *AQH_DataClient_GetDevices(AQH_DATACLIENT *dc) fullDeviceList=AQH_Device_List_new(); msgId=++(dc->lastMsgId); - msgOut=AQH_IpcMessage_new(dc->protoId, dc->protoVer, AQH_MSGTYPE_IPC_DATA_GETDEVICES_REQ, msgId, 0, 0, NULL); + msgOut=AQH_IpcdMessageGetDevices_new(AQH_MSGTYPE_IPC_DATA_GETDEVICES_REQ, msgId, 0, deviceName); AQH_Endpoint_AddMsgOut(dc->ipcEndpoint, msgOut); while( (msgIn=AQH_IpcEndpoint_WaitForResponseMsg(dc->ipcEndpoint, msgId, dc->timeoutInSeconds)) ) { diff --git a/aqhome/dataclient/client.h b/aqhome/dataclient/client.h index 05db794..1a77195 100644 --- a/aqhome/dataclient/client.h +++ b/aqhome/dataclient/client.h @@ -34,7 +34,7 @@ AQHOME_API int AQH_DataClient_Connect(AQH_DATACLIENT *dc, AQHOME_API int AQH_DataClient_Disconnect(AQH_DATACLIENT *dc); -AQHOME_API AQH_DEVICE_LIST *AQH_DataClient_GetDevices(AQH_DATACLIENT *dc); +AQHOME_API AQH_DEVICE_LIST *AQH_DataClient_GetDevices(AQH_DATACLIENT *dc, const char *deviceName); AQHOME_API AQH_VALUE_LIST *AQH_DataClient_GetValues(AQH_DATACLIENT *dc, const char *deviceName, int modality); AQHOME_API uint64_t AQH_DataClient_GetFirstData(AQH_DATACLIENT *dc, const char *valueName, uint64_t *dataPtr, uint64_t maxNum); @@ -50,6 +50,8 @@ AQHOME_API int AQH_DataClient_UpdateData(AQH_DATACLIENT *dc, const AQH_VALUE *v, AQHOME_API int AQH_DataClient_ReadLocalArgs(AQH_DATACLIENT *dc, GWEN_DB_NODE *dbGlobalArgs, const GWEN_ARGS *argDescrs, int argc, char **argv); +AQHOME_API int AQH_DataClient_ReadConfigFile(AQH_DATACLIENT *dc); + AQHOME_API int AQH_DataClient_ConnectWithArgs(AQH_DATACLIENT *dc, uint32_t flags); AQHOME_API GWEN_DB_NODE *AQH_DataClient_GetDbLocalArgs(const AQH_DATACLIENT *dc); diff --git a/aqhome/msg/ipc/data/0BUILD b/aqhome/msg/ipc/data/0BUILD index 527384b..ccc7008 100644 --- a/aqhome/msg/ipc/data/0BUILD +++ b/aqhome/msg/ipc/data/0BUILD @@ -52,6 +52,7 @@ m_ipcd_getdata.h m_ipcd_setdata.h m_ipcd_getvalues.h + m_ipcd_getdevices.h @@ -69,6 +70,7 @@ m_ipcd_getdata.c m_ipcd_setdata.c m_ipcd_getvalues.c + m_ipcd_getdevices.c diff --git a/aqhome/msg/ipc/data/m_ipcd_getdevices.c b/aqhome/msg/ipc/data/m_ipcd_getdevices.c new file mode 100644 index 0000000..565a709 --- /dev/null +++ b/aqhome/msg/ipc/data/m_ipcd_getdevices.c @@ -0,0 +1,76 @@ +/**************************************************************************** + * 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/data/m_ipcd_getdevices.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 +#include +#include +#include + + + +/* ------------------------------------------------------------------------------------------------ + * forward declarations + * ------------------------------------------------------------------------------------------------ + */ + + + +/* ------------------------------------------------------------------------------------------------ + * implementation + * ------------------------------------------------------------------------------------------------ + */ + +AQH_MESSAGE *AQH_IpcdMessageGetDevices_new(uint16_t code, uint32_t msgId, uint32_t refMsgId, const char *deviceName) +{ + AQH_MESSAGE *msg; + GWEN_BUFFER *buf; + + buf=GWEN_Buffer_new(0, 256, 0, 1); + + if (deviceName && *deviceName) + GWEN_Tag16_WriteStringTagToBuffer(AQH_MSGDATA_GETDEVICES_TAGS_DEVICENAME, deviceName, buf); + + msg=AQH_IpcMessage_new(AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, code, msgId, refMsgId, + GWEN_Buffer_GetUsedBytes(buf), (const uint8_t*) GWEN_Buffer_GetStart(buf)); + GWEN_Buffer_free(buf); + return msg; +} + + + +void AQH_IpcdMessageGetDevices_DumpToBuffer(const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, + GWEN_BUFFER *dbuf, const char *sText) +{ + char *deviceName; + + deviceName=tagList?AQH_Tag16_GetTagDataAsNewString(tagList, AQH_MSGDATA_GETDEVICES_TAGS_DEVICENAME, NULL):NULL; + + GWEN_Buffer_AppendArgs(dbuf, + "GETDEVICES(%s) %s (code=%d, proto=%d, proto version=%d, device=%s)\n", + AQH_IpcdMessage_MsgTypeToChar(AQH_IpcMessage_GetCode(msg)), + sText?sText:"", + AQH_IpcMessage_GetCode(msg), + AQH_IpcMessage_GetProtoId(msg), + AQH_IpcMessage_GetProtoVersion(msg), + deviceName?deviceName:""); + free(deviceName); +} + + + + diff --git a/aqhome/msg/ipc/data/m_ipcd_getdevices.h b/aqhome/msg/ipc/data/m_ipcd_getdevices.h new file mode 100644 index 0000000..50be3e7 --- /dev/null +++ b/aqhome/msg/ipc/data/m_ipcd_getdevices.h @@ -0,0 +1,34 @@ +/**************************************************************************** + * This file is part of the project AqHome. + * AqHome (c) by 2025 Martin Preuss, all rights reserved. + * + * The license for this file can be found in the file COPYING which you + * should have received along with this file. + ****************************************************************************/ + +#ifndef AQH_M_IPCD_GETDEVICES_H +#define AQH_M_IPCD_GETDEVICES_H + + + +#include +#include +#include + +#include +#include + + + +#define AQH_MSGDATA_GETDEVICES_TAGS_DEVICENAME 0x0001 + + + +AQHOME_API AQH_MESSAGE *AQH_IpcdMessageGetDevices_new(uint16_t code, uint32_t msgId, uint32_t refMsgId, const char *deviceName); + +AQHOME_API void AQH_IpcdMessageGetDevices_DumpToBuffer(const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, + GWEN_BUFFER *dbuf, const char *sText); + + + +#endif