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

#include <gwenhywfar/debug.h>


#define AQH_MSG_OFFS_RECVSTATS_UID            0   /* 4 bytes */
#define AQH_MSG_OFFS_RECVSTATS_IFACE          4   /* 1 byte */
#define AQH_MSG_OFFS_RECVSTATS_PACKETSIN      5   /* 2 bytes */
#define AQH_MSG_OFFS_RECVSTATS_CRCERRORS      7   /* 2 bytes */
#define AQH_MSG_OFFS_RECVSTATS_IOERRORS       9   /* 2 bytes */
#define AQH_MSG_OFFS_RECVSTATS_NOBUFFER      11   /* 2 bytes */
#define AQH_MSG_OFFS_RECVSTATS_MSGSIZEERRORS 13   /* 2 bytes */
#define AQH_MSG_OFFS_RECVSTATS_MISSED        15   /* 2 bytes */



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



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



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



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



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



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



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



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



void AQH_RecvStatsMessage_DumpToBuffer(const AQH_MESSAGE *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  GWEN_Buffer_AppendArgs(dbuf,
                         "0x%02x->0x%02x: RECVSTATS %s "
                         "(uid=0x%08x, dev=%d, in=%d, eCrc=%d, eIo=%d, eNobuf=%d, eMsgSize=%d, eMissed=%d)\n",
                         AQH_NodeMessage_GetSourceAddress(msg),
                         AQH_NodeMessage_GetDestAddress(msg),
                         sText,
                         (unsigned int) AQH_RecvStatsMessage_GetUid(msg),
                         AQH_RecvStatsMessage_GetInterface(msg),
                         AQH_RecvStatsMessage_GetPacketsIn(msg),
                         AQH_RecvStatsMessage_GetCrcErrors(msg),
                         AQH_RecvStatsMessage_GetIoErrors(msg),
                         AQH_RecvStatsMessage_GetNoBufferErrors(msg),
                         AQH_RecvStatsMessage_GetMsgSizeErrors(msg),
                         AQH_RecvStatsMessage_GetMissed(msg));
}


