/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2023 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/msg_memstats.h"

#include <gwenhywfar/misc.h>
#include <gwenhywfar/list.h>
#include <gwenhywfar/error.h>
#include <gwenhywfar/debug.h>



#define AQH_MSG_OFFS_MEMSTATS_SECONDS         0  /* 4 bytes */
#define AQH_MSG_OFFS_MEMSTATS_UID             4  /* 4 bytes */
#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_MEMSTATS_MINSIZE          (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_RECVNOBUFFER+2)



uint32_t AQH_MemStatsMsg_GetUid(const GWEN_MSG *msg)
{
  return GWEN_Msg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_UID, 0);
}



uint32_t AQH_MemStatsMsg_GetSeconds(const GWEN_MSG *msg)
{
  return GWEN_Msg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_SECONDS, 0);
}


uint16_t AQH_MemStatsMsg_GetStackUsage(const GWEN_MSG *msg)
{
  return GWEN_Msg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_STACKUSAGE, 0);
}



uint8_t AQH_MemStatsMsg_GetBuffersUsed(const GWEN_MSG *msg)
{
  return GWEN_Msg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_BUFFERSUSED, 0);
}



uint8_t AQH_MemStatsMsg_GetMaxBuffersUsed(const GWEN_MSG *msg)
{
  return GWEN_Msg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_MAXBUFFERSUSED, 0);
}



uint16_t AQH_MemStatsMsg_GetRecvNoBufferErrors(const GWEN_MSG *msg)
{
  return GWEN_Msg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_RECVNOBUFFER, 0);
}



void AQH_MemStatsMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  GWEN_Buffer_AppendArgs(dbuf,
			 "0x%02x->0x%02x: MEMSTATS %s (uid=0x%08x, uptime=%d, stack used=%d, buffers used=%d(max=%d), no recvbuf=%d)\n",
			 AQH_NodeMsg_GetSourceAddress(msg),
			 AQH_NodeMsg_GetDestAddress(msg),
			 sText,
			 (unsigned int) AQH_MemStatsMsg_GetUid(msg),
                         AQH_MemStatsMsg_GetSeconds(msg),
			 AQH_MemStatsMsg_GetStackUsage(msg),
			 AQH_MemStatsMsg_GetBuffersUsed(msg),
			 AQH_MemStatsMsg_GetMaxBuffersUsed(msg),
			 AQH_MemStatsMsg_GetRecvNoBufferErrors(msg));
}







