/****************************************************************************
 * 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_NODE_H
#define AQH_M_NODE_H


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

#include <gwenhywfar/buffer.h>


#define AQH_MAXMSGSIZE 128


#define AQH_MSG_OFFS_ALL_DEST_ADDRESS  0
#define AQH_MSG_OFFS_ALL_PAYLOAD_LEN   1
#define AQH_MSG_OFFS_ALL_PAYLOAD_BEGIN 2
#define AQH_MSG_OFFS_ALL_MSG_TYPE      2
#define AQH_MSG_OFFS_ALL_SRC_ADDRESS   3
#define AQH_MSG_OFFS_ALL_DATA_BEGIN    4


#define AQH_MSG_TYPE_PING           10
#define AQH_MSG_TYPE_PONG           11
#define AQH_MSG_TYPE_HEARTBEAT      12
#define AQH_MSG_TYPE_COMSENDSTATS   22
#define AQH_MSG_TYPE_COMRECVSTATS   23
#define AQH_MSG_TYPE_TWIBUSMEMBER   30
#define AQH_MSG_TYPE_DEBUG          40
#define AQH_MSG_TYPE_VALUE          50 /* deprecated */
#define AQH_MSG_TYPE_VALUE2         51 /* deprecated */
#define AQH_MSG_TYPE_NEED_ADDRESS   60
#define AQH_MSG_TYPE_HAVE_ADDRESS   61
#define AQH_MSG_TYPE_CLAIM_ADDRESS  62
#define AQH_MSG_TYPE_DENY_ADDRESS   63
#define AQH_MSG_TYPE_ADDRESS_RANGE  64
#define AQH_MSG_TYPE_REENUM         65

#define AQH_MSG_TYPE_FLASH_START    70
#define AQH_MSG_TYPE_FLASH_END      71
#define AQH_MSG_TYPE_FLASH_READY    72
#define AQH_MSG_TYPE_FLASH_DATA     73
#define AQH_MSG_TYPE_FLASH_RSP      74

#define AQH_MSG_TYPE_DEVICE         80
#define AQH_MSG_TYPE_MEMSTATS       81
#define AQH_MSG_TYPE_SYSSTATS       82

#define AQH_MSG_TYPE_REBOOT_REQ     90
#define AQH_MSG_TYPE_REBOOT_RSP     91

#define AQH_MSG_TYPE_VALUE_REPORT   100
#define AQH_MSG_TYPE_VALUE_SET      101
#define AQH_MSG_TYPE_VALUE_SET_ACK  102
#define AQH_MSG_TYPE_VALUE_SET_NACK 103

#define AQH_MSG_TYPE_TIME_ANNOUNCE  120
#define AQH_MSG_TYPE_TIME_REQSET    121
#define AQH_MSG_TYPE_TIME_RSPSET    122


/* internal msg types via NET interface */
#define AQH_MSG_TYPE_NET_SET_ACCEPTED_MSGGROUPS      200


#define AQH_MSG_TYPEGROUP_INFO    0x00000001
#define AQH_MSG_TYPEGROUP_VALUES  0x00000002
#define AQH_MSG_TYPEGROUP_ADDRESS 0x00000004
#define AQH_MSG_TYPEGROUP_FLASH   0x00000008
#define AQH_MSG_TYPEGROUP_ADMIN   0x00000010
#define AQH_MSG_TYPEGROUP_TIME    0x00000020
#define AQH_MSG_TYPEGROUP_ALL     0xffffffff



AQHOME_API AQH_MESSAGE *AQH_NodeMessage_new(uint8_t destAddr, uint8_t srcAddr, uint8_t code, uint8_t payloadLen, const uint8_t *payload);
AQHOME_API AQH_MESSAGE *AQH_NodeMessage_prepare(uint8_t destAddr, uint8_t srcAddr, uint8_t code, uint8_t payloadLen);

AQHOME_API AQH_MESSAGE *AQH_NodeMessage_fromBuffer(const uint8_t *ptr, uint32_t len);

AQHOME_API uint8_t AQH_NodeMessage_GetDestAddress(const AQH_MESSAGE *msg);
AQHOME_API uint8_t AQH_NodeMessage_GetMsgType(const AQH_MESSAGE *msg);
AQHOME_API uint8_t AQH_NodeMessage_GetSourceAddress(const AQH_MESSAGE *msg);

/**
 * Return size of payload (i.e. length of data after AQH_MSG_OFFS_ALL_DATA_BEGIN).
 */
AQHOME_API uint8_t AQH_NodeMessage_GetPayloadLength(const AQH_MESSAGE *msg);

/**
 * Return pointer to payload (i.e. data after AQH_MSG_OFFS_ALL_DATA_BEGIN).
 */
AQHOME_API uint8_t *AQH_NodeMessage_GetPayloadPointer(const AQH_MESSAGE *msg);

/**
 * Append checksum (uses @ref AQH_Message_GetUsedSize).
 */
AQHOME_API void AQH_NodeMessage_AddChecksum(AQH_MESSAGE *msg);

/**
 * Verify checksum.
 */
AQHOME_API int AQH_NodeMessage_IsValid(AQH_MESSAGE *msg);


AQHOME_API const char *AQH_NodeMessage_MsgTypeToChar(uint8_t i);
AQHOME_API uint32_t AQH_NodeMessage_GetMsgGroup(uint8_t msgType);


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

/**
 * Calls the dump function of the given message according to its message type.
 */
AQHOME_API void AQH_NodeMessage_DumpSpecificToBuffer(const AQH_MESSAGE *msg, GWEN_BUFFER *dbuf, const char *sText);

#endif

