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

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


#define COM_USE_CRC8 1



#ifdef COM_USE_CRC8
static uint8_t _calcCrc8Checksum(const uint8_t *ptr, uint8_t len);
#else
static uint8_t _calcXorChecksum(const uint8_t *ptr, uint8_t len);
#endif



GWEN_MSG *AQH_NodeMsg_new(uint8_t destAddr, uint8_t srcAddr, uint8_t code, uint8_t payloadLen, const uint8_t *payload)
{
  GWEN_MSG *msg;
  uint32_t len;
  uint8_t *ptr;

  len=AQH_MSG_OFFS_ALL_DATA_BEGIN+payloadLen+1; /* dest, len, code, src, payload, crc8 */
  msg=GWEN_Msg_new(len);
  if (msg==NULL)
    return NULL;
  ptr=GWEN_Msg_GetBuffer(msg);

  ptr[AQH_MSG_OFFS_ALL_DEST_ADDRESS]=destAddr & 0xff;
  ptr[AQH_MSG_OFFS_ALL_PAYLOAD_LEN]=payloadLen+2;
  ptr[AQH_MSG_OFFS_ALL_MSG_TYPE]=code;
  ptr[AQH_MSG_OFFS_ALL_SRC_ADDRESS]=srcAddr;

  if (payloadLen && payload)
    memmove(ptr+AQH_MSG_OFFS_ALL_DATA_BEGIN, payload, payloadLen);
  GWEN_Msg_SetBytesInBuffer(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+payloadLen);
  GWEN_Msg_IncCurrentPos(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+payloadLen);
  return msg;
}



uint8_t AQH_NodeMsg_GetDestAddress(const GWEN_MSG *msg)
{
  if (msg && GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_OFFS_ALL_DATA_BEGIN) {
    const uint8_t *ptr;

    ptr=GWEN_Msg_GetConstBuffer(msg);
    return ptr[AQH_MSG_OFFS_ALL_DEST_ADDRESS];
  }

  return 0;
}



uint8_t AQH_NodeMsg_GetMsgType(const GWEN_MSG *msg)
{
  if (msg && GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_OFFS_ALL_DATA_BEGIN) {
    const uint8_t *ptr;

    ptr=GWEN_Msg_GetConstBuffer(msg);
    return ptr[AQH_MSG_OFFS_ALL_MSG_TYPE];
  }

  return 0;
}



uint8_t AQH_NodeMsg_GetSourceAddress(const GWEN_MSG *msg)
{
  if (msg && GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_OFFS_ALL_DATA_BEGIN) {
    const uint8_t *ptr;

    ptr=GWEN_Msg_GetConstBuffer(msg);
    return ptr[AQH_MSG_OFFS_ALL_SRC_ADDRESS];
  }

  return 0;
}



uint8_t AQH_NodeMsg_GetMsgPayloadLen(const GWEN_MSG *msg)
{
  if (msg && GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_OFFS_ALL_DATA_BEGIN) {
    const uint8_t *ptr;

    ptr=GWEN_Msg_GetConstBuffer(msg);
    return ptr[AQH_MSG_OFFS_ALL_PAYLOAD_LEN];
  }

  return 0;
}



int AQH_NodeMsg_IsMsgComplete(const GWEN_MSG *msg)
{
  if (msg) {
    uint8_t msgLen;

    msgLen=GWEN_Msg_GetBytesInBuffer(msg);
    if (msgLen>=AQH_MSG_OFFS_ALL_DATA_BEGIN) {
      const uint8_t *ptr;
      uint8_t len;

      ptr=GWEN_Msg_GetConstBuffer(msg);
      len=ptr[AQH_MSG_OFFS_ALL_PAYLOAD_LEN]+AQH_MSG_OFFS_ALL_PAYLOAD_BEGIN+1;
      if (len>AQH_MAXMSGSIZE) {
	DBG_ERROR(AQH_LOGDOMAIN, "Total length > max length (%d > %d)", len, AQH_MAXMSGSIZE);
	return -1;
      }
      else if (msgLen>=len)
        return 1;
    }
  }

  return 0;
}



int AQH_NodeMsg_IsChecksumValid(const GWEN_MSG *msg)
{
  if (msg && AQH_NodeMsg_IsMsgComplete(msg)) {
#ifdef COM_USE_CRC8
    return (_calcCrc8Checksum(GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg))==0)?1:0;
#else
    return (_calcXorChecksum(GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg))==0)?1:0;
#endif
  }
  return 0;
}



int AQH_NodeMsg_AddChecksum(GWEN_MSG *msg)
{
  if (msg) {
    int rv;

#ifdef COM_USE_CRC8
    rv=GWEN_Msg_AddByte(msg, _calcCrc8Checksum(GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg)));
#else
    rv=GWEN_Msg_AddByte(msg, _calcXorChecksum(GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg)));
#endif
    if (rv<0) {
      DBG_INFO(NULL, "here (%d)", rv);
      return rv;
    }
    return 0;
  }
  return GWEN_ERROR_GENERIC;
}



void AQH_NodeMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  GWEN_Buffer_AppendArgs(dbuf,
                         "0x%02x->0x%02x: %d %s\n",
                         AQH_NodeMsg_GetSourceAddress(msg),
                         AQH_NodeMsg_GetDestAddress(msg),
                         AQH_NodeMsg_GetMsgType(msg),
                         sText);
  GWEN_Text_DumpString2Buffer((const char*)GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg), dbuf, 34);
}




uint32_t AQH_NodeMsg_GetMsgGroup(uint8_t msgType)
{
  switch(msgType) {
  case AQH_MSG_TYPE_PING:
  case AQH_MSG_TYPE_PONG:
  case AQH_MSG_TYPE_COMSENDSTATS:
  case AQH_MSG_TYPE_COMRECVSTATS:
  case AQH_MSG_TYPE_TWIBUSMEMBER:
  case AQH_MSG_TYPE_DEBUG:
  case AQH_MSG_TYPE_DEVICE:
  case AQH_MSG_TYPE_MEMSTATS:
  case AQH_MSG_TYPE_SYSSTATS:
    return AQH_MSG_TYPEGROUP_INFO;

  case AQH_MSG_TYPE_VALUE:
  case AQH_MSG_TYPE_VALUE2:
    return AQH_MSG_TYPEGROUP_VALUES;

  case AQH_MSG_TYPE_NEED_ADDRESS:
  case AQH_MSG_TYPE_HAVE_ADDRESS:
  case AQH_MSG_TYPE_CLAIM_ADDRESS:
  case AQH_MSG_TYPE_DENY_ADDRESS:
  case AQH_MSG_TYPE_ADDRESS_RANGE:
    return AQH_MSG_TYPEGROUP_ADDRESS;

  case AQH_MSG_TYPE_NET_SET_ACCEPTED_MSGGROUPS:
    return AQH_MSG_TYPEGROUP_ADMIN;

  case AQH_MSG_TYPE_FLASH_START:
  case AQH_MSG_TYPE_FLASH_END:
  case AQH_MSG_TYPE_FLASH_READY:
  case AQH_MSG_TYPE_FLASH_DATA:
  case AQH_MSG_TYPE_FLASH_RSP:
  case AQH_MSG_TYPE_REBOOT_REQ:
  case AQH_MSG_TYPE_REBOOT_RSP:
    return AQH_MSG_TYPEGROUP_FLASH;

  default:
    return 0;
  }
}




#ifdef COM_USE_CRC8
uint8_t _calcCrc8Checksum(const uint8_t *ptr, uint8_t len)
{
  int i;
  uint8_t x=0xff;

  for (i=0; i<len; i++, ptr++) {
    int j;

    x^=*ptr;
    for (j=0; j<8; j++) {
      if (x & 0x80)
	x=(uint8_t) (x<<1)^0x97;
      else
	x<<=1;
    }
  }

  return x;
}
#else
uint8_t _calcXorChecksum(const uint8_t *ptr, uint8_t len)
{
  int i;
  uint8_t x=0;

  for (i=0; i<len; i++, ptr++) {
    x^=*ptr;
  }

  return x;
}
#endif




const char *AQH_NodeMsg_MsgTypeToChar(uint8_t i)
{
  switch(i) {
  case AQH_MSG_TYPE_PING:          return "Ping";
  case AQH_MSG_TYPE_PONG:          return "Pong";
  case AQH_MSG_TYPE_COMSENDSTATS:  return "SendStats";
  case AQH_MSG_TYPE_COMRECVSTATS:  return "RecvStats";
  case AQH_MSG_TYPE_TWIBUSMEMBER:  return "TwiBusMember";
  case AQH_MSG_TYPE_DEBUG:         return "Debug";
  case AQH_MSG_TYPE_VALUE:         return "Value";
  case AQH_MSG_TYPE_VALUE2:        return "Value2";
  case AQH_MSG_TYPE_NEED_ADDRESS:  return "NeedAddress";
  case AQH_MSG_TYPE_HAVE_ADDRESS:  return "HaveAddress";
  case AQH_MSG_TYPE_CLAIM_ADDRESS: return "ClaimAddress";
  case AQH_MSG_TYPE_DENY_ADDRESS:  return "DenyAddress";
  case AQH_MSG_TYPE_ADDRESS_RANGE: return "Range";

  case AQH_MSG_TYPE_FLASH_START:   return "FlashStart";
  case AQH_MSG_TYPE_FLASH_END:     return "FlashEnd";
  case AQH_MSG_TYPE_FLASH_READY:   return "FlashReady";
  case AQH_MSG_TYPE_FLASH_DATA:    return "FlashData";
  case AQH_MSG_TYPE_FLASH_RSP:     return "FlashResponse";

  case AQH_MSG_TYPE_DEVICE:        return "Device";
  case AQH_MSG_TYPE_MEMSTATS:      return "MemStats";
  case AQH_MSG_TYPE_SYSSTATS:      return "SysStats";
  case AQH_MSG_TYPE_REBOOT_REQ:    return "RebootRequest";
  case AQH_MSG_TYPE_REBOOT_RSP:    return "RebootResponse";
  default:                         return "(unknown)";
  }
}



