/****************************************************************************
 * 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_IPC_H
#define AQH_M_IPC_H


#include <aqhome/api.h>
#include <aqhome/ipc2/message.h>

#include <gwenhywfar/buffer.h>


#define AQH_IPCMSG_OFFS_SIZE     0   /* 4 bytes: number of all bytes including size, protoid, protover and code */
#define AQH_IPCMSG_OFFS_PROTOID  4   /* 1 byte: protocol id (free to use) */
#define AQH_IPCMSG_OFFS_PROTOVER 5   /* 1 byte: protocol version (free to use) */
#define AQH_IPCMSG_OFFS_CODE     6   /* 2 bytes  msg code (meaning depends on protocol) */
#define AQH_IPCMSG_OFFS_ID       8   /* 4 bytes  msg id */
#define AQH_IPCMSG_OFFS_REFID    12  /* 4 bytes  reference msg id */
#define AQH_IPCMSG_OFFS_PAYLOAD  16  /* begin of payload for a given message */


#define AQH_MSGTYPE_IPC_RESULT               0x0001
#define AQH_MSGTYPE_IPC_CONNECT_REQ          0x0010



AQHOME_API 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);
AQHOME_API AQH_MESSAGE *AQH_IpcMessage_fromBuffer(const uint8_t *ptr, uint32_t len);

AQHOME_API void AQH_IpcMessage_AdjustMsgSize(AQH_MESSAGE *msg);

AQHOME_API uint32_t AQH_IpcMessage_GetMsgSize(const AQH_MESSAGE *msg);
AQHOME_API uint8_t AQH_IpcMessage_GetProtoId(const AQH_MESSAGE *msg);
AQHOME_API uint8_t AQH_IpcMessage_GetProtoVersion(const AQH_MESSAGE *msg);
AQHOME_API uint16_t AQH_IpcMessage_GetCode(const AQH_MESSAGE *msg);
AQHOME_API uint32_t AQH_IpcMessage_GetMsgId(const AQH_MESSAGE *msg);
AQHOME_API uint32_t AQH_IpcMessage_GetRefMsgId(const AQH_MESSAGE *msg);

AQHOME_API uint32_t AQH_IpcMessage_GetPayloadLength(const AQH_MESSAGE *msg);
AQHOME_API uint8_t *AQH_IpcMessage_GetPayloadPointer(const AQH_MESSAGE *msg);

AQHOME_API void AQH_IpcMessage_DumpToBuffer(const AQH_MESSAGE *msg, GWEN_BUFFER *dbuf, const char *sText);





#endif
