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

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


#define AQH_MSG_OFFS_DEVICE_UID         0  /* 4 bytes */
#define AQH_MSG_OFFS_DEVICE_FWTYPE      4  /* 2 bytes */
#define AQH_MSG_OFFS_DEVICE_FWLOW       6  /* 1 byte */
#define AQH_MSG_OFFS_DEVICE_FWHIGH      7  /* 1 byte */
#define AQH_MSG_OFFS_DEVICE_MODULES     8  /* 2 byte */

#define AQH_MSG_DEVICE_MINSIZE          (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_MODULES+2)



uint32_t AQH_DeviceMsg_GetUid(const GWEN_MSG *msg)
{
  return AQH_NodeMsg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_UID, 0);
}



uint16_t AQH_DeviceMsg_GetFirmwareType(const GWEN_MSG *msg)
{
  return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_FWTYPE, 0);
}



uint8_t AQH_DeviceMsg_GetFirmwareLow(const GWEN_MSG *msg)
{
  return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_FWLOW, 0);
}



uint8_t AQH_DeviceMsg_GetFirmwareHigh(const GWEN_MSG *msg)
{
  return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_FWHIGH, 0);
}



uint16_t AQH_DeviceMsg_GetModuleMask(const GWEN_MSG *msg)
{
  return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_MODULES, 0);
}



void AQH_DeviceMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_DEVICE_MINSIZE) {
    uint16_t modules;

    modules=AQH_DeviceMsg_GetModuleMask(msg);
    GWEN_Buffer_AppendArgs(dbuf, "0x%02x->0x%02x: DEVICE %s (uid=0x%08x, fw type=%d, fw ver=%d.%d, mods=0x%04x",
                           AQH_NodeMsg_GetSourceAddress(msg),
                           AQH_NodeMsg_GetDestAddress(msg),
                           sText,
                           (unsigned int) AQH_DeviceMsg_GetUid(msg),
                           AQH_DeviceMsg_GetFirmwareType(msg),
                           AQH_DeviceMsg_GetFirmwareHigh(msg),
                           AQH_DeviceMsg_GetFirmwareLow(msg),
                           modules);
    if (modules) {
      GWEN_Buffer_AppendString(dbuf, "[");
      if (modules & AQH_MSG_DEVICE_MASK_TIMER)
        GWEN_Buffer_AppendString(dbuf, " TIMER");
      if (modules & AQH_MSG_DEVICE_MASK_COM)
        GWEN_Buffer_AppendString(dbuf, " COM");
      if (modules & AQH_MSG_DEVICE_MASK_LED)
        GWEN_Buffer_AppendString(dbuf, " LED");
      if (modules & AQH_MSG_DEVICE_MASK_TWIMASTER)
        GWEN_Buffer_AppendString(dbuf, " TWIMASTER");
      if (modules & AQH_MSG_DEVICE_MASK_LCD)
        GWEN_Buffer_AppendString(dbuf, " LCD");
      if (modules & AQH_MSG_DEVICE_MASK_SI7021)
        GWEN_Buffer_AppendString(dbuf, " SI7021");
      GWEN_Buffer_AppendString(dbuf, " ]");
    }
    GWEN_Buffer_AppendString(dbuf, ")\n");
  }
}





