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

#include <gwenhywfar/debug.h>
#include <gwenhywfar/endianfns.h>



#define AQH_MSG_OFFS_PING_TIMESTAMP  0   /* 4 bytes */



AQH_MESSAGE *AQH_PingMessage_new(uint8_t destAddr, uint8_t srcAddr, uint8_t code, uint32_t timestamp)
{
  uint32_t payload;

  payload=GWEN_ENDIAN_HTOLE32(timestamp);
  return AQH_NodeMessage_new(destAddr, srcAddr, code, sizeof(payload), (const uint8_t*)&payload);
}



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



void AQH_PingMessage_DumpToBuffer(const AQH_MESSAGE *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  GWEN_Buffer_AppendArgs(dbuf,
                         "0x%02x->0x%02x: PING(%s) %s (timestamp=0x%08x)\n",
                         AQH_NodeMessage_GetSourceAddress(msg),
                         AQH_NodeMessage_GetDestAddress(msg),
                         AQH_NodeMessage_MsgTypeToChar(AQH_NodeMessage_GetMsgType(msg)),
                         sText,
                         (unsigned int) AQH_PingMessage_GetTimestamp(msg));
}


