/****************************************************************************
 * 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_flashready.h"

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


#define AQH_MSG_OFFS_FLASHREADY_UID         0 /* 4 bytes */
#define AQH_MSG_OFFS_FLASHREADY_FWTYPE      4 /* 2 bytes */
#define AQH_MSG_OFFS_FLASHREADY_FWVER       6 /* 2 bytes */
#define AQH_MSG_OFFS_FLASHREADY_PAGESIZE    8 /* 2 bytes */

#define AQH_MSG_FLASHREADY_MINSIZE          (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_FLASHREADY_PAGESIZE+2)



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



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



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



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






void AQH_FlashReadyMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_FLASH_READY) &&
      (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_FLASHREADY_MINSIZE)) {
    uint16_t fwVersion;

    fwVersion=AQH_FlashReadyMsg_GetFirmwareVersion(msg);
    GWEN_Buffer_AppendArgs(dbuf, "0x%02x->0x%02x: FLASHREADY %s (uid=0x%08x, fw type=%d, fw ver=%d.%d, pagesize=%d)\n",
                           AQH_NodeMsg_GetSourceAddress(msg),
                           AQH_NodeMsg_GetDestAddress(msg),
                           sText,
                           (unsigned int) AQH_FlashReadyMsg_GetUid(msg),
                           AQH_FlashReadyMsg_GetFirmwareType(msg),
                           (fwVersion>>8) & 0xff,
                           fwVersion & 0xff,
                           AQH_FlashReadyMsg_GetPagesize(msg));
  }
}







