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

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



#define AQH_MSG_OFFS_FLASHEND_REASON      0 /* 1 byte */



AQH_MESSAGE *AQH_FlashEndMessage_new(uint8_t destAddr, uint8_t srcAddr, uint8_t code, uint8_t reason)
{
  AQH_MESSAGE *msg;
  uint8_t *ptr;

  msg=AQH_NodeMessage_prepare(destAddr, srcAddr, code, 1);

  ptr=AQH_NodeMessage_GetPayloadPointer(msg);
  *(ptr++)=reason & 0xff;
  AQH_NodeMessage_AddChecksum(msg);

  return msg;
}



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



void AQH_FlashEndMessage_DumpToBuffer(const AQH_MESSAGE *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  GWEN_Buffer_AppendArgs(dbuf, "0x%02x->0x%02x: FLASHEND(%s) %s (reason=%d)\n",
                         AQH_NodeMessage_GetSourceAddress(msg),
                         AQH_NodeMessage_GetDestAddress(msg),
                         AQH_NodeMessage_MsgTypeToChar(AQH_NodeMessage_GetMsgType(msg)),
                         sText,
                         AQH_FlashEndMessage_GetReason(msg));
}




