/****************************************************************************
 * 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_MINSIZE (GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGDATA_QWORDS_OFFS_VALUES)


static void _writeQword(uint64_t i64, uint8_t *ptr);




GWEN_MSG *AQH_QwordsIpcMsg_new(uint8_t protoId, uint8_t protoVer, uint16_t code, uint32_t flags, const uint64_t *i64Ptr, int count)
{
  GWEN_MSG *msg;
  uint8_t *ptr;
  int payloadSize;
  int i;

  payloadSize=AQH_MSGDATA_QWORDS_OFFS_VALUES+(count*8);

  msg=GWEN_IpcMsg_new(protoId, protoVer, code, payloadSize, NULL);
  ptr=GWEN_Msg_GetBuffer(msg)+GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGDATA_QWORDS_OFFS_VALUES;

  *(ptr++)=flags & 0xff;
  *(ptr++)=(flags>>8) & 0xff;
  *(ptr++)=(flags>>16) & 0xff;
  *(ptr++)=(flags>>24) & 0xff;

  *(ptr++)=count & 0xff;
  *(ptr++)=(count>>8) & 0xff;
  *(ptr++)=(count>>16) & 0xff;
  *(ptr++)=(count>>24) & 0xff;

  for(i=0; i<count; i++) {
    _writeQword(*i64Ptr, ptr);
    ptr+=8;
    i64Ptr++;
  }

  return msg;
}



void _writeQword(uint64_t i64, uint8_t *ptr)
{
  *(ptr++)=i64 & 0xff;
  *(ptr++)=(i64>>8) & 0xff;
  *(ptr++)=(i64>>16) & 0xff;
  *(ptr++)=(i64>>24) & 0xff;
  *(ptr++)=(i64>>32) & 0xff;
  *(ptr++)=(i64>>40) & 0xff;
  *(ptr++)=(i64>>48) & 0xff;
  *(ptr++)=(i64>>56) & 0xff;
}



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));
  }
}







