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

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



#define AQH_MSG_OFFS_FLASHREADY_UID          0 /* 4 bytes */
#define AQH_MSG_OFFS_FLASHREADY_MANUF        4 /* 4 bytes */
#define AQH_MSG_OFFS_FLASHREADY_DEVTYPE      8 /* 2 bytes */
#define AQH_MSG_OFFS_FLASHREADY_DEVVERSION  10 /* 1 byte */
#define AQH_MSG_OFFS_FLASHREADY_DEVREVISION 11 /* 1 byte */
#define AQH_MSG_OFFS_FLASHREADY_FWVARIANT   12 /* 1 byte */
#define AQH_MSG_OFFS_FLASHREADY_FWVMAJOR    13 /* 1 byte */
#define AQH_MSG_OFFS_FLASHREADY_FWVMINOR    14 /* 1 byte */
#define AQH_MSG_OFFS_FLASHREADY_FWVPATCH    15 /* 1 byte */
#define AQH_MSG_OFFS_FLASHREADY_PAGESIZE    16 /* 2 bytes */



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



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



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



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



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



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


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



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



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



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



void AQH_FlashReadyMessage_DumpToBuffer(const AQH_MESSAGE *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  GWEN_Buffer_AppendArgs(dbuf,
			 "0x%02x->0x%02x: FLASHREADY(%s) %s (uid=0x%08x, dev=%08x:%04x v%d.%d, fw=%d.%d.%d (%d) pagesize=%d)\n",
			 AQH_NodeMessage_GetSourceAddress(msg),
			 AQH_NodeMessage_GetDestAddress(msg),
			 AQH_NodeMessage_MsgTypeToChar(AQH_NodeMessage_GetMsgType(msg)),
			 sText,
			 (unsigned int) AQH_FlashReadyMessage_GetUid(msg),
			 AQH_FlashReadyMessage_GetManufacturer(msg),
			 AQH_FlashReadyMessage_GetDeviceType(msg),
			 AQH_FlashReadyMessage_GetDeviceVersion(msg),
			 AQH_FlashReadyMessage_GetDeviceRevision(msg),
			 AQH_FlashReadyMessage_GetFirmwareVersionMajor(msg),
			 AQH_FlashReadyMessage_GetFirmwareVersionMinor(msg),
			 AQH_FlashReadyMessage_GetFirmwareVersionPatchlevel(msg),
			 AQH_FlashReadyMessage_GetFirmwareVariant(msg),
			 AQH_FlashReadyMessage_GetPagesize(msg));
}



