/****************************************************************************
 * 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 <config.h>
#endif


#include "aqhome/msg/ipc/m_ipc.h"

#include <gwenhywfar/text.h>


/* ------------------------------------------------------------------------------------------------
 * forward declarations
 * ------------------------------------------------------------------------------------------------
 */



/* ------------------------------------------------------------------------------------------------
 * implementation
 * ------------------------------------------------------------------------------------------------
 */

AQH_MESSAGE *AQH_IpcMessage_new(uint8_t protoId, uint8_t protoVer, uint16_t code,
                                uint32_t msgId, uint32_t refMsgId,
                                uint32_t payloadLen, const uint8_t *payload)
{
  AQH_MESSAGE *msg;
  uint32_t len;

  len=AQH_IPCMSG_OFFS_PAYLOAD+payloadLen;
  msg=AQH_Message_new();
  AQH_Message_SetData(msg, NULL, len);                /* auto-malloc len bytes */

  AQH_Message_WriteUint32At(msg, AQH_IPCMSG_OFFS_SIZE, len);
  AQH_Message_WriteUint8At(msg, AQH_IPCMSG_OFFS_PROTOID, protoId);
  AQH_Message_WriteUint8At(msg, AQH_IPCMSG_OFFS_PROTOVER, protoVer);
  AQH_Message_WriteUint16At(msg, AQH_IPCMSG_OFFS_CODE, code);
  AQH_Message_WriteUint32At(msg, AQH_IPCMSG_OFFS_ID, msgId);
  AQH_Message_WriteUint32At(msg, AQH_IPCMSG_OFFS_REFID, refMsgId);
  if (payloadLen && payload)
    AQH_Message_WriteBytesAt(msg, AQH_IPCMSG_OFFS_PAYLOAD, payload, payloadLen);
  AQH_Message_SetUsedSize(msg, len);
  return msg;
}



AQH_MESSAGE *AQH_IpcMessage_fromBuffer(const uint8_t *ptr, uint32_t len)
{
  if (ptr && len) {
    AQH_MESSAGE *msg;

    msg=AQH_Message_new();
    AQH_Message_SetData(msg, ptr, len);
    AQH_Message_SetUsedSize(msg, len);
    return msg;
  }
  return NULL;
}



void AQH_IpcMessage_AdjustMsgSize(AQH_MESSAGE *msg)
{
  if (msg)
    AQH_Message_WriteUint32At(msg, AQH_IPCMSG_OFFS_SIZE, AQH_Message_GetUsedSize(msg));
}



uint32_t AQH_IpcMessage_GetMsgSize(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint32At(msg, AQH_IPCMSG_OFFS_SIZE, 0);
}



uint8_t AQH_IpcMessage_GetProtoId(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint8At(msg, AQH_IPCMSG_OFFS_PROTOID, 0);
}



uint8_t AQH_IpcMessage_GetProtoVersion(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint8At(msg, AQH_IPCMSG_OFFS_PROTOVER, 0);
}



uint16_t AQH_IpcMessage_GetCode(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint16At(msg, AQH_IPCMSG_OFFS_CODE, 0);
}



uint32_t AQH_IpcMessage_GetMsgId(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint32At(msg, AQH_IPCMSG_OFFS_ID, 0);
}



uint32_t AQH_IpcMessage_GetRefMsgId(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint32At(msg, AQH_IPCMSG_OFFS_REFID, 0);
}



uint32_t AQH_IpcMessage_GetPayloadLength(const AQH_MESSAGE *msg)
{
  return msg?(AQH_IpcMessage_GetMsgSize(msg)-AQH_IPCMSG_OFFS_PAYLOAD):0;
}



uint8_t *AQH_IpcMessage_GetPayloadPointer(const AQH_MESSAGE *msg)
{
  return msg?(AQH_Message_GetMsgPointer(msg)+AQH_IPCMSG_OFFS_PAYLOAD):NULL;
}



void AQH_IpcMessage_DumpToBuffer(const AQH_MESSAGE *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  GWEN_Buffer_AppendArgs(dbuf, "IPC message %s (code=%d, protocol=%d, protocol version=%d)\n",
                         sText?sText:"",
                         AQH_IpcMessage_GetCode(msg),
                         AQH_IpcMessage_GetProtoId(msg),
                         AQH_IpcMessage_GetProtoVersion(msg));
}



