Files
aqhomecontrol/aqhome/ipc/data/cmd_data_getdata.c
Martin Preuss 03f9178dd2 Revert "Revert "aqhome: convenience code.""
This reverts commit bb77c6acd1.
2024-09-30 18:28:38 +02:00

111 lines
2.7 KiB
C

/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <aqhome/ipc/data/msg_data_datapoints.h>
#include <aqhome/ipc/data/ipc_data.h>
#include <aqhome/ipc/data/msg_data_multidata.h>
#include <aqhome/ipc/data/msg_data_getdata.h>
#include <aqhome/ipc/data/ipc_data.h>
#include <aqhome/ipc/msg_ipc_tag16.h>
#include <gwenhywfar/msg.h>
#include <gwenhywfar/buffer.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/msg_ipc.h>
GWEN_MSG *_waitForMatchingResponse(GWEN_MSG_ENDPOINT *epTcp, int msgCode, int timeoutInSeconds);
GWEN_MSG *AQH_CmdDataIpc_RequestDatapointsMsg(GWEN_MSG_ENDPOINT *ep,
const char *valueName, uint64_t tsBegin, uint64_t tsEnd, uint64_t num,
int timeoutInSeconds)
{
GWEN_MSG *msg;
time_t startTime;
msg=AQH_GetDataDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_GETDATA_REQ, valueName, tsBegin, tsEnd, num);
GWEN_MsgEndpoint_AddSendMessage(ep, msg);
msg=_waitForMatchingResponse(ep, AQH_MSGTYPE_IPC_DATA_GETDATA_RSP, timeoutInSeconds);
if (msg) {
uint16_t code;
code=GWEN_IpcMsg_GetCode(msg);
if (code==AQH_MSGTYPE_IPC_DATA_RESULT) {
uint32_t resultCode;
resultCode=AQH_ResultIpcMsg_GetResultCode(msg);
DBG_INFO(AQH_LOGDOMAIN, "IPC error: %d", resultCode);
GWEN_Msg_free(msg);
return NULL;
}
else if (code==AQH_MSGTYPE_IPC_DATA_GETDATA_RSP) {
int rv;
AQH_MultiDataDataIpcMsg_Parse(msg, 0);
return msg;
}
GWEN_Msg_free(msg);
}
return NULL;
}
GWEN_MSG *_waitForMatchingResponse(GWEN_MSG_ENDPOINT *epTcp, int msgCode, int timeoutInSeconds)
{
time_t startTime;
startTime=time(NULL);
for (;;) {
GWEN_MSG *msg;
time_t now;
while( (msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp)) ) {
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), ignoring", code, code);
}
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;
}