/****************************************************************************
 * 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/node/m_memstats.h"
#include "aqhome/msg/node/m_node.h"

#include <gwenhywfar/debug.h>



#define AQH_MSG_OFFS_MEMSTATS_UID             0  /* 4 bytes */
#define AQH_MSG_OFFS_MEMSTATS_UPHOURS         4  /* 1 byte */
#define AQH_MSG_OFFS_MEMSTATS_UPMINS          5  /* 1 byte */
#define AQH_MSG_OFFS_MEMSTATS_UPSECS          6  /* 1 byte */
#define AQH_MSG_OFFS_MEMSTATS_UPTICKS         7  /* 1 byte */
#define AQH_MSG_OFFS_MEMSTATS_STACKUSAGE      8  /* 2 bytes */
#define AQH_MSG_OFFS_MEMSTATS_BUFFERSUSED    10  /* 1 byte */
#define AQH_MSG_OFFS_MEMSTATS_MAXBUFFERSUSED 11  /* 1 byte */
#define AQH_MSG_OFFS_MEMSTATS_RECVNOBUFFER   12  /* 2 bytes */
#define AQH_MSG_OFFS_MEMSTATS_HEAPUSED       14  /* 2 bytes */
#define AQH_MSG_OFFS_MEMSTATS_HEAPFREE       16  /* 2 bytes */
#define AQH_MSG_OFFS_MEMSTATS_XRAMSIZE       18  /* 2 bytes */




uint32_t AQH_MemStatsMessage_GetUid(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_UID, 0);
}



uint8_t AQH_MemStatsMessage_GetUptimeHours(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_UPHOURS, 0);
}



uint8_t AQH_MemStatsMessage_GetUptimeMinutes(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_UPMINS, 0);
}



uint8_t AQH_MemStatsMessage_GetUptimeSeconds(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_UPSECS, 0);
}



uint8_t AQH_MemStatsMessage_GetUptimeTicks(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_UPTICKS, 0);
}



uint16_t AQH_MemStatsMessage_GetStackUsage(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_STACKUSAGE, 0);
}



uint8_t AQH_MemStatsMessage_GetBuffersUsed(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_BUFFERSUSED, 0);
}



uint8_t AQH_MemStatsMessage_GetMaxBuffersUsed(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_MAXBUFFERSUSED, 0);
}



uint16_t AQH_MemStatsMessage_GetRecvNoBufferErrors(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_RECVNOBUFFER, 0);
}



uint16_t AQH_MemStatsMessage_GetHeapUsed(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_HEAPUSED, 0);
}



uint16_t AQH_MemStatsMessage_GetHeapFree(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_HEAPFREE, 0);
}



uint16_t AQH_MemStatsMessage_GetXramSize(const AQH_MESSAGE *msg)
{
  return AQH_Message_ReadUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_XRAMSIZE, 0);
}



void AQH_MemStatsMessage_DumpToBuffer(const AQH_MESSAGE *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  GWEN_Buffer_AppendArgs(dbuf,
			 "0x%02x->0x%02x: MEMSTATS(%s) %s (uid=0x%08x, up=%dh:%dm:%ds:%dt, "
			 "stack used=%d, buffers used=%d(max=%d), no recvbuf=%d, "
			 "heap used=%d, heap free=%d, xram=%d)\n",
			 AQH_NodeMessage_GetSourceAddress(msg),
			 AQH_NodeMessage_GetDestAddress(msg),
                         AQH_NodeMessage_MsgTypeToChar(AQH_NodeMessage_GetMsgType(msg)),
			 sText,
			 (unsigned int) AQH_MemStatsMessage_GetUid(msg),
                         AQH_MemStatsMessage_GetUptimeHours(msg),
                         AQH_MemStatsMessage_GetUptimeMinutes(msg),
                         AQH_MemStatsMessage_GetUptimeSeconds(msg),
			 AQH_MemStatsMessage_GetUptimeTicks(msg),
			 AQH_MemStatsMessage_GetStackUsage(msg),
			 AQH_MemStatsMessage_GetBuffersUsed(msg),
			 AQH_MemStatsMessage_GetMaxBuffersUsed(msg),
			 AQH_MemStatsMessage_GetRecvNoBufferErrors(msg),
			 AQH_MemStatsMessage_GetHeapUsed(msg),
			 AQH_MemStatsMessage_GetHeapFree(msg),
			 AQH_MemStatsMessage_GetXramSize(msg));
}



