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

#include <gwenhywfar/debug.h>

#include <ctype.h>



#define AQH_MSG_OFFS_DEVICE_UID          0 /* 4 bytes */
#define AQH_MSG_OFFS_DEVICE_MANUF        4 /* 4 bytes */
#define AQH_MSG_OFFS_DEVICE_DEVTYPE      8 /* 2 bytes */
#define AQH_MSG_OFFS_DEVICE_DEVVERSION  10 /* 1 byte */
#define AQH_MSG_OFFS_DEVICE_DEVREVISION 11 /* 1 byte */
#define AQH_MSG_OFFS_DEVICE_FWVARIANT   12 /* 1 byte */
#define AQH_MSG_OFFS_DEVICE_FWVMAJOR    13 /* 1 byte */
#define AQH_MSG_OFFS_DEVICE_FWVMINOR    14 /* 1 byte */
#define AQH_MSG_OFFS_DEVICE_FWVPATCH    15 /* 1 byte */



static void _addDeviceId(const AQH_MESSAGE *msg, GWEN_BUFFER *dbuf);
static int _isAllGraphic(uint32_t value, int len);
static void _printChars(uint32_t value, int len, GWEN_BUFFER *dbuf);




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



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



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



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



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



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



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



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



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



void AQH_DeviceMessage_DumpToBuffer(const AQH_MESSAGE *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  if (msg) {
    GWEN_Buffer_AppendArgs(dbuf,
                           "0x%02x->0x%02x: DEVICE %s (uid=0x%08x, dev=%08x:%04x v%d.%d (",
                           AQH_NodeMessage_GetSourceAddress(msg),
                           AQH_NodeMessage_GetDestAddress(msg),
                           sText,
                           (unsigned int) AQH_DeviceMessage_GetUid(msg),
                           AQH_DeviceMessage_GetManufacturer(msg),
                           AQH_DeviceMessage_GetDeviceType(msg),
                           AQH_DeviceMessage_GetDeviceVersion(msg),
                           AQH_DeviceMessage_GetDeviceRevision(msg));
    _addDeviceId(msg, dbuf);
    GWEN_Buffer_AppendArgs(dbuf,
                           "), fw=%d.%d.%d (%d))\n",
                           AQH_DeviceMessage_GetFirmwareVersionMajor(msg),
                           AQH_DeviceMessage_GetFirmwareVersionMinor(msg),
                           AQH_DeviceMessage_GetFirmwareVersionPatchlevel(msg),
                           AQH_DeviceMessage_GetFirmwareVariant(msg));
  }
}



void _addDeviceId(const AQH_MESSAGE *msg, GWEN_BUFFER *dbuf)
{
  uint32_t v;

  v=AQH_DeviceMessage_GetManufacturer(msg);
  if (_isAllGraphic(v, 4))
    _printChars(v, 4, dbuf);
  else
    GWEN_Buffer_AppendArgs(dbuf, "%08x", v);

  GWEN_Buffer_AppendByte(dbuf, ' ');

  v=AQH_DeviceMessage_GetDeviceType(msg);
  if (_isAllGraphic(v, 2)) {
    _printChars(v, 2, dbuf);
    GWEN_Buffer_AppendArgs(dbuf, "%d", AQH_DeviceMessage_GetDeviceVersion(msg));
  }
  else {
    GWEN_Buffer_AppendArgs(dbuf, "%04x", v);
    GWEN_Buffer_AppendArgs(dbuf, " v%d", AQH_DeviceMessage_GetDeviceVersion(msg));
  }
}



int _isAllGraphic(uint32_t value, int len)
{
  int i;

  for (i=0; i<len; i++) {
    uint8_t v;

    v=value & 0xff;
    if (!(isgraph(v) || isblank(v) || v==0))
      return 0;
    value=value>>8;
  }
  return 1;
}



void _printChars(uint32_t value, int len, GWEN_BUFFER *dbuf)
{
  int i;

  for (i=0; i<len; i++) {
    uint8_t v;

    v=value&0xff;
    if (isgraph(v))
      GWEN_Buffer_AppendByte(dbuf, v);
    value=value>>8;
  }
}


