/****************************************************************************
 * 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_MESSAGE_H
#define AQH_MESSAGE_H


#include <aqhome/api.h>
#include <aqhome/events2/object.h>

#include <gwenhywfar/list.h>
#include <gwenhywfar/inherit.h>



typedef struct AQH_MESSAGE AQH_MESSAGE;
GWEN_LIST_FUNCTION_LIB_DEFS(AQH_MESSAGE, AQH_Message, AQHOME_API)
GWEN_INHERIT_FUNCTION_LIB_DEFS(AQH_MESSAGE, AQHOME_API)



AQHOME_API AQH_MESSAGE *AQH_Message_new(void);
AQHOME_API AQH_MESSAGE *AQH_Message_dup(const AQH_MESSAGE *origMsg);
AQHOME_API void AQH_Message_IncRef(AQH_MESSAGE *msg);
AQHOME_API void AQH_Message_free(AQH_MESSAGE *msg);


/* unparsed data */
AQHOME_API uint8_t *AQH_Message_GetMsgPointer(const AQH_MESSAGE *msg);
AQHOME_API uint32_t AQH_Message_GetMsgSize(const AQH_MESSAGE *msg);
AQHOME_API void AQH_Message_SetData(AQH_MESSAGE *msg, const uint8_t *msgPtr, uint32_t msgSize);

AQHOME_API uint32_t AQH_Message_GetUsedSize(const AQH_MESSAGE *msg);
AQHOME_API void AQH_Message_SetUsedSize(AQH_MESSAGE *msg, uint32_t i);
AQHOME_API void AQH_Message_IncUsedSize(AQH_MESSAGE *msg, uint32_t i);

/* helper functions for parsing */
AQHOME_API void AQH_Message_WriteUint8At(AQH_MESSAGE *msg, uint32_t pos, uint8_t d);
AQHOME_API uint8_t AQH_Message_ReadUint8At(const AQH_MESSAGE *msg, uint32_t pos, uint8_t defaultValue);

AQHOME_API void AQH_Message_WriteUint16At(AQH_MESSAGE *msg, uint32_t pos, uint16_t d);
AQHOME_API uint16_t AQH_Message_ReadUint16At(const AQH_MESSAGE *msg, uint32_t pos, uint16_t defaultValue);

AQHOME_API void AQH_Message_WriteUint32At(AQH_MESSAGE *msg, uint32_t pos, uint32_t d);
AQHOME_API uint32_t AQH_Message_ReadUint32At(const AQH_MESSAGE *msg, uint32_t pos, uint32_t defaultValue);

AQHOME_API void AQH_Message_WriteUint64At(AQH_MESSAGE *msg, uint32_t pos, uint64_t d);
AQHOME_API uint64_t AQH_Message_ReadUint64At(const AQH_MESSAGE *msg, uint32_t pos, uint64_t defaultValue);

AQHOME_API void AQH_Message_WriteBytesAt(AQH_MESSAGE *msg, uint32_t pos, const uint8_t *bufferPtr, uint32_t bufferLen);
AQHOME_API int AQH_Message_WriteStringAt(AQH_MESSAGE *msg, uint32_t pos, uint32_t maxSize, int filler, const char *s);
AQHOME_API int AQH_Message_WriteStringWithTrailingNullAt(AQH_MESSAGE *msg, uint32_t pos, uint32_t maxSize, int filler, const char *s);

AQHOME_API AQH_OBJECT *AQH_Message_GetObject(const AQH_MESSAGE *msg);
AQHOME_API void AQH_Message_SetObject(AQH_MESSAGE *msg, AQH_OBJECT *o);


#endif

