/****************************************************************************
 * 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_reboot.h"
#include "aqhome/msg/msg_node.h"

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

#include <gwenhywfar/text.h>


#define AQH_MSG_OFFS_REBOOT_UID         0 /* 4 bytes */

#define AQH_MSG_REBOOT_MINSIZE          (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_REBOOT_UID+4)



static GWEN_MSG *_rebootMsg_new(uint8_t srcAddr, uint8_t destAddr, uint8_t code, uint32_t uid);
static uint32_t _getUid(const GWEN_MSG *msg);
static void _rebootMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText, const char *cmd);





GWEN_MSG *AQH_RebootRequestMsg_new(uint8_t srcAddr, uint8_t destAddr, uint8_t code, uint32_t uid)
{
  return _rebootMsg_new(srcAddr, destAddr, code, uid);
}



uint32_t AQH_RebootRequestMsg_GetUid(const GWEN_MSG *msg)
{
  return _getUid(msg);
}



void AQH_RebootRequestMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  _rebootMsg_DumpToBuffer(msg, dbuf, sText, "REBOOT_REQUEST");
}





GWEN_MSG *AQH_RebootResponseMsg_new(uint8_t srcAddr, uint8_t destAddr, uint8_t code, uint32_t uid)
{
  return _rebootMsg_new(srcAddr, destAddr, code, uid);
}



uint32_t AQH_RebootResponseMsg_GetUid(const GWEN_MSG *msg)
{
  return _getUid(msg);
}



void AQH_RebootResponseMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  _rebootMsg_DumpToBuffer(msg, dbuf, sText, "REBOOT_RESPONSE");
}





GWEN_MSG *_rebootMsg_new(uint8_t srcAddr, uint8_t destAddr, uint8_t code, uint32_t uid)
{
  GWEN_MSG *msg;
  uint8_t *ptr;
  int rv;

  msg=AQH_NodeMsg_new(destAddr, srcAddr, code, 4, NULL);
  ptr=GWEN_Msg_GetBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_REBOOT_UID;

  *(ptr++)=uid & 0xff;               /* uid */
  *(ptr++)=(uid>>8) & 0xff;
  *(ptr++)=(uid>>16) & 0xff;
  *(ptr++)=(uid>>24) & 0xff;


  rv=AQH_NodeMsg_AddChecksum(msg);
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    GWEN_Msg_free(msg);
    return NULL;
  }

  return msg;
}



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



void _rebootMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText, const char *cmd)
{
  if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_REBOOT_MINSIZE) {
    GWEN_Buffer_AppendArgs(dbuf,
			   "0x%02x->0x%02x: %s %s (uid=0x%08x)\n",
			   AQH_NodeMsg_GetSourceAddress(msg),
			   AQH_NodeMsg_GetDestAddress(msg),
			   cmd,
                           sText,
                           _getUid(msg));
  }
}






