Files
aqhomecontrol/aqhome/ipc/msg_ipc_qwords.c

123 lines
3.2 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/msg_ipc_qwords.h>
#include <gwenhywfar/msg.h>
#include <gwenhywfar/buffer.h>
#include <gwenhywfar/misc.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/msg_ipc.h>
#define AQH_MSGDATA_QWORDS_OFFS_FLAGS 0 /* 4 bytes */
#define AQH_MSGDATA_QWORDS_OFFS_NUMVALUES 4 /* 4 bytes */
#define AQH_MSGDATA_QWORDS_OFFS_VALUES 8 /* 8 byte */
#define AQH_MSGDATA_QWORDS_PAYLOADSIZE (AQH_MSGDATA_QWORDS_OFFS_VALUES)
#define AQH_MSGDATA_QWORDS_MINSIZE (GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGDATA_QWORDS_PAYLOADSIZE)
GWEN_MSG *AQH_QwordsIpcMsg_new(uint8_t protoId, uint8_t protoVer, uint16_t code,
uint32_t msgId, uint32_t refMsgId,
uint32_t flags, const uint64_t *i64Ptr, int count)
{
GWEN_MSG *msg;
uint8_t *ptr;
int payloadSize;
int i;
payloadSize=AQH_MSGDATA_QWORDS_PAYLOADSIZE+(count*8);
msg=GWEN_IpcMsg_new(protoId, protoVer, code, msgId, refMsgId, payloadSize, NULL);
GWEN_Msg_AddUint32(msg, flags);
GWEN_Msg_AddUint32(msg, count);
for(i=0; i<count; i++)
GWEN_Msg_AddUint64(msg, *(i64Ptr++));
return msg;
}
uint32_t AQH_QwordsIpcMsg_GetFlags(const GWEN_MSG *msg)
{
return GWEN_Msg_GetUint32At(msg, GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGDATA_QWORDS_OFFS_FLAGS, 0);
}
uint32_t AQH_QwordsIpcMsg_GetNumValues(const GWEN_MSG *msg)
{
return GWEN_Msg_GetUint32At(msg, GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGDATA_QWORDS_OFFS_NUMVALUES, 0);
}
uint64_t AQH_QwordsIpcMsg_GetValue(const GWEN_MSG *msg, int idx)
{
uint32_t pos;
pos=AQH_MSGDATA_QWORDS_OFFS_VALUES+(idx*8);
return GWEN_Msg_GetUint64At(msg, GWEN_MSGIPC_OFFS_PAYLOAD+pos, 0);
}
int AQH_QwordsIpcMsg_IsValid(const GWEN_MSG *msg)
{
int msgLen;
int numValues;
msgLen=GWEN_Msg_GetBytesInBuffer(msg);
if (msgLen<AQH_MSGDATA_QWORDS_MINSIZE) {
DBG_ERROR(AQH_LOGDOMAIN, "Message too small (%d<%d)", GWEN_Msg_GetBytesInBuffer(msg), AQH_MSGDATA_QWORDS_MINSIZE);
return 0;
}
numValues=(int)AQH_QwordsIpcMsg_GetNumValues(msg);
if (msgLen<(numValues*8)+AQH_MSGDATA_QWORDS_OFFS_VALUES+GWEN_MSGIPC_OFFS_PAYLOAD) {
DBG_ERROR(AQH_LOGDOMAIN, "Message too small to contain %d values", numValues);
return 0;
}
return 1;
}
void AQH_QwordsIpcMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSGDATA_QWORDS_MINSIZE) {
GWEN_Buffer_AppendArgs(dbuf,
"QWORDS (code=%d, proto=%d, proto version=%d, flags=0x%08x, values=%d)\n",
GWEN_IpcMsg_GetCode(msg),
GWEN_IpcMsg_GetProtoId(msg),
GWEN_IpcMsg_GetProtoVersion(msg),
(unsigned int)AQH_QwordsIpcMsg_GetFlags(msg),
AQH_QwordsIpcMsg_GetNumValues(msg));
}
}