/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2025 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/ipc/m_ipc_tag16.h"
#include "aqhome/msg/ipc/m_ipc.h"

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



/* ------------------------------------------------------------------------------------------------
 * defines
 * ------------------------------------------------------------------------------------------------
 */

#define AQH_IPCDATA_VALUE_TAGS_ID              0x01
#define AQH_IPCDATA_VALUE_TAGS_DRIVER          0x02
#define AQH_IPCDATA_VALUE_TAGS_NAMEFORDRIVER   0x03
#define AQH_IPCDATA_VALUE_TAGS_NAMEFORSYSTEM   0x04
#define AQH_IPCDATA_VALUE_TAGS_TYPE            0x05
#define AQH_IPCDATA_VALUE_TAGS_UNITS           0x06
#define AQH_IPCDATA_VALUE_TAGS_TIMEOFCREATION  0x07
#define AQH_IPCDATA_VALUE_TAGS_DEVFORDRIVER    0x08
#define AQH_IPCDATA_VALUE_TAGS_DEVFORSYSTEM    0x09
#define AQH_IPCDATA_VALUE_TAGS_MODALITY        0x0a

#define AQH_IPCDATA_DEVICE_TAGS_ID             0x01
#define AQH_IPCDATA_DEVICE_TAGS_DRIVER         0x02
#define AQH_IPCDATA_DEVICE_TAGS_ROOMNAME       0x03
#define AQH_IPCDATA_DEVICE_TAGS_NAMEFORDRIVER  0x04
#define AQH_IPCDATA_DEVICE_TAGS_NAMEFORSYSTEM  0x05
#define AQH_IPCDATA_DEVICE_TAGS_NAMEFORGUI     0x06
#define AQH_IPCDATA_DEVICE_TAGS_LOCATION       0x07
#define AQH_IPCDATA_DEVICE_TAGS_DESCRIPTION    0x08
#define AQH_IPCDATA_DEVICE_TAGS_DEVTYPE        0x09
#define AQH_IPCDATA_DEVICE_TAGS_MANUFACTURER   0x0a
#define AQH_IPCDATA_DEVICE_TAGS_TIMEOFCREATION 0x0b

#define AQH_IPCDATA_NODE_TAGS_BUSADDR             0x01 /* uint8_t */
#define AQH_IPCDATA_NODE_TAGS_UID                 0x02 /* uint32_t */
#define AQH_IPCDATA_NODE_TAGS_MANUFACTURER        0x03 /* uint32_t */
#define AQH_IPCDATA_NODE_TAGS_DEVICETYPE          0x04 /* uint16_t */
#define AQH_IPCDATA_NODE_TAGS_DEVICEVERSION       0x05 /* uint16_t */
#define AQH_IPCDATA_NODE_TAGS_FIRMWAREVERSION     0x06 /* uint32_t */
#define AQH_IPCDATA_NODE_TAGS_LASTCHANGE          0x07 /* uint64_t */
#define AQH_IPCDATA_NODE_TAGS_STATSPACKETSOUT     0x08 /* uint16_t */
#define AQH_IPCDATA_NODE_TAGS_STATSPACKETSIN      0x09 /* uint16_t */
#define AQH_IPCDATA_NODE_TAGS_STATSCOLLISIONS     0x0a /* uint16_t */
#define AQH_IPCDATA_NODE_TAGS_STATSBUSY           0x0b /* uint16_t */
#define AQH_IPCDATA_NODE_TAGS_STATSCRCERRORS      0x0c /* uint16_t */
#define AQH_IPCDATA_NODE_TAGS_STATSIOERRORS       0x0d /* uint16_t */
#define AQH_IPCDATA_NODE_TAGS_STATSSTACKUSED      0x0e /* uint16_t */
#define AQH_IPCDATA_NODE_TAGS_STATSBUFFERSUSED    0x0f /* uint16_t */
#define AQH_IPCDATA_NODE_TAGS_STATSMAXBUFFERSUSED 0x10 /* uint16_t */
#define AQH_IPCDATA_NODE_TAGS_STATSRECVNOBUFFER   0x11 /* uint16_t */
#define AQH_IPCDATA_NODE_TAGS_STATSHEAPUSED       0x12 /* uint16_t */
#define AQH_IPCDATA_NODE_TAGS_STATSHEAPFREE       0x13 /* uint16_t */
#define AQH_IPCDATA_NODE_TAGS_STATSXRAM           0x14 /* uint16_t */



/* ------------------------------------------------------------------------------------------------
 * forward declarations
 * ------------------------------------------------------------------------------------------------
 */

static void _writeValueFieldsAsTagsToBuffer(const AQH_VALUE *value, GWEN_BUFFER *buf);
static AQH_VALUE *_readValueFromTag(const uint8_t *ptr, uint32_t len);
static void _writeDeviceFieldsAsTagsToBuffer(const AQH_DEVICE *device, GWEN_BUFFER *buf);
static AQH_DEVICE *_readDeviceFromTag(const uint8_t *ptr, uint32_t len);
static void _writeNodeInfoFieldsAsTagsToBuffer(const AQH_NODE_INFO *ni, GWEN_BUFFER *buf);
static AQH_NODE_INFO *_readNodeInfoFromTag(const uint8_t *ptr, uint32_t len);



/* ------------------------------------------------------------------------------------------------
 * implementation
 * ------------------------------------------------------------------------------------------------
 */



GWEN_TAG16_LIST *AQH_IpcMessageTag16_ParsePayload(const AQH_MESSAGE *msg, int doCopy)
{
  if (msg) {
    const uint8_t *payloadPtr;
    uint32_t payloadSize;

    payloadPtr=AQH_IpcMessage_GetPayloadPointer(msg);
    payloadSize=AQH_IpcMessage_GetPayloadLength(msg);
    return AQH_Tag16_ParseTags(payloadPtr, payloadSize, doCopy);
  }

  return NULL;
}



GWEN_TAG16_LIST *AQH_Tag16_ParseTags(const uint8_t *payloadPtr, uint32_t payloadSize, int doCopy)
{
  if (payloadPtr && payloadSize) {
    GWEN_TAG16_LIST *tagList;

    tagList=GWEN_Tag16_List_fromBuffer(payloadPtr, payloadSize, doCopy);
    if (tagList==NULL) {
      DBG_INFO(AQH_LOGDOMAIN, "Error reading tags from message");
      return NULL;
    }
    return tagList;
  }

  return NULL;
}




char *AQH_Tag16_GetTagDataAsNewString(const GWEN_TAG16_LIST *tagList, unsigned int tagType, const char *defaultValue)
{
  if (tagList) {
    const GWEN_TAG16 *tag;

    tag=GWEN_Tag16_List_FindFirstByTagType(tagList, tagType);
    if (tag)
      return GWEN_Tag16_GetTagDataAsNewString(tag, defaultValue);
  }
  return defaultValue?strdup(defaultValue):NULL;
}



uint32_t AQH_Tag16_GetTagDataAsUint32(const GWEN_TAG16_LIST *tagList, unsigned int tagType, uint32_t defaultValue)
{
  if (tagList) {
    const GWEN_TAG16 *tag;

    tag=GWEN_Tag16_List_FindFirstByTagType(tagList, tagType);
    return tag?GWEN_Tag16_GetTagDataAsUint32(tag, defaultValue):defaultValue;
  }
  return defaultValue;
}



uint64_t AQH_Tag16_GetTagDataAsUint64(const GWEN_TAG16_LIST *tagList, unsigned int tagType, uint64_t defaultValue)
{
  if (tagList) {
    const GWEN_TAG16 *tag;

    tag=GWEN_Tag16_List_FindFirstByTagType(tagList, tagType);
    return tag?GWEN_Tag16_GetTagDataAsUint64(tag, defaultValue):defaultValue;
  }
  return defaultValue;
}



double AQH_Tag16_GetTagDataAsDouble(const GWEN_TAG16_LIST *tagList, unsigned int tagType, double defaultValue)
{
  if (tagList) {
    const GWEN_TAG16 *tag;

    tag=GWEN_Tag16_List_FindFirstByTagType(tagList, tagType);
    return tag?GWEN_Tag16_GetTagDataAsDouble(tag, defaultValue):defaultValue;
  }
  return defaultValue;
}



int AQH_Tag16_WriteValueListAsTagsToBuffer(unsigned int tagType, const AQH_VALUE_LIST *valueList, GWEN_BUFFER *buf)
{
  if (valueList) {
    const AQH_VALUE *value;

    value=AQH_Value_List_First(valueList);
    while(value) {
      int rv;

      rv=AQH_Tag16_WriteValueAsTagToBuffer(tagType, value, buf);
      if (rv<0) {
        DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
        GWEN_Buffer_free(buf);
        return rv;
      }

      value=AQH_Value_List_Next(value);
    }
  }
  return 0;
}



int AQH_Tag16_WriteValueAsTagToBuffer(unsigned int tagType, const AQH_VALUE *value, GWEN_BUFFER *buf)
{
  int startPos;
  int rv;

  startPos=GWEN_Tag16_StartTagInBuffer(tagType, buf);
  if (startPos<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", startPos);
    return startPos;
  }

  _writeValueFieldsAsTagsToBuffer(value, buf);

  rv=GWEN_Tag16_EndTagInBuffer(startPos, buf);
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    return rv;
  }

  return 0;
}



int AQH_Tag16_WriteDeviceListAsTagsToBuffer(unsigned int tagType, const AQH_DEVICE_LIST *deviceList, GWEN_BUFFER *buf)
{
  if (deviceList) {
    const AQH_DEVICE *device;

    device=AQH_Device_List_First(deviceList);
    while(device) {
      int rv;

      rv=AQH_Tag16_WriteDeviceAsTagToBuffer(tagType, device, buf);
      if (rv<0) {
        DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
        GWEN_Buffer_free(buf);
        return rv;
      }

      device=AQH_Device_List_Next(device);
    }
  }
  return 0;
}



int AQH_Tag16_WriteDeviceAsTagToBuffer(unsigned int tagType, const AQH_DEVICE *device, GWEN_BUFFER *buf)
{
  int startPos;
  int rv;

  startPos=GWEN_Tag16_StartTagInBuffer(tagType, buf);
  if (startPos<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", startPos);
    return startPos;
  }

  _writeDeviceFieldsAsTagsToBuffer(device, buf);

  rv=GWEN_Tag16_EndTagInBuffer(startPos, buf);
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    return rv;
  }

  return 0;
}



void _writeValueFieldsAsTagsToBuffer(const AQH_VALUE *value, GWEN_BUFFER *buf)
{
  const char *s;

  GWEN_Tag16_WriteUint64TagToBuffer(AQH_IPCDATA_VALUE_TAGS_ID, AQH_Value_GetId(value), buf);

  s=AQH_Value_GetDriver(value);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_VALUE_TAGS_DRIVER, s, buf);

  s=AQH_Value_GetName(value);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_VALUE_TAGS_NAMEFORDRIVER, s, buf);

  s=AQH_Value_GetNameForSystem(value);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_VALUE_TAGS_NAMEFORSYSTEM, s, buf);

  GWEN_Tag16_WriteUint32TagToBuffer(AQH_IPCDATA_VALUE_TAGS_TYPE, AQH_Value_GetValueType(value), buf);

  s=AQH_Value_GetValueUnits(value);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_VALUE_TAGS_UNITS, s, buf);

  GWEN_Tag16_WriteUint64TagToBuffer(AQH_IPCDATA_VALUE_TAGS_TIMEOFCREATION, AQH_Value_GetTimestampCreation(value), buf);

  s=AQH_Value_GetDeviceName(value);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_VALUE_TAGS_DEVFORDRIVER, s, buf);

  s=AQH_Value_GetDeviceNameForSystem(value);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_VALUE_TAGS_DEVFORSYSTEM, s, buf);

  GWEN_Tag16_WriteUint32TagToBuffer(AQH_IPCDATA_VALUE_TAGS_MODALITY, AQH_Value_GetModality(value), buf);
  
}






AQH_VALUE_LIST *AQH_Tag16_ReadValuesFromTagList(const GWEN_TAG16_LIST *tagList, unsigned int wantedTagType)
{
  AQH_VALUE_LIST *valueList;
  const GWEN_TAG16 *tag;

  valueList=AQH_Value_List_new();
  tag=GWEN_Tag16_List_First(tagList);
  while(tag) {
    unsigned int tagType;
    AQH_VALUE *value;

    tagType=GWEN_Tag16_GetTagType(tag);
    if (tagType==wantedTagType) {
      value=_readValueFromTag((const uint8_t*) GWEN_Tag16_GetTagData(tag), (uint32_t) GWEN_Tag16_GetTagLength(tag));
      if (value)
        AQH_Value_List_Add(value, valueList);
    }

    tag=GWEN_Tag16_List_Next(tag);
  }

  if (AQH_Value_List_GetCount(valueList)<1) {
    AQH_Value_List_free(valueList);
    return NULL;
  }

  return valueList;
}



AQH_VALUE *AQH_Tag16_ReadValueFromTagList(const GWEN_TAG16_LIST *tagList, unsigned int wantedTagType)
{
  if (tagList) {
    const GWEN_TAG16 *tag;

    tag=GWEN_Tag16_List_FindFirstByTagType(tagList, wantedTagType);
    return tag?_readValueFromTag((const uint8_t*) GWEN_Tag16_GetTagData(tag), (uint32_t) GWEN_Tag16_GetTagLength(tag)):NULL;
  }
  return NULL;
}



AQH_DEVICE_LIST *AQH_Tag16_ReadDevicesFromTagList(const GWEN_TAG16_LIST *tagList, unsigned int wantedTagType)
{
  AQH_DEVICE_LIST *deviceList;
  const GWEN_TAG16 *tag;

  deviceList=AQH_Device_List_new();
  tag=GWEN_Tag16_List_First(tagList);
  while(tag) {
    unsigned int tagType;
    AQH_DEVICE *device;

    tagType=GWEN_Tag16_GetTagType(tag);
    if (tagType==wantedTagType) {
      device=_readDeviceFromTag((const uint8_t*) GWEN_Tag16_GetTagData(tag), (uint32_t) GWEN_Tag16_GetTagLength(tag));
      if (device)
        AQH_Device_List_Add(device, deviceList);
    }

    tag=GWEN_Tag16_List_Next(tag);
  }

  if (AQH_Device_List_GetCount(deviceList)<1) {
    AQH_Device_List_free(deviceList);
    return NULL;
  }

  return deviceList;
}



AQH_DEVICE *AQH_Tag16_ReadDeviceFromTagList(const GWEN_TAG16_LIST *tagList, unsigned int wantedTagType)
{
  if (tagList) {
    const GWEN_TAG16 *tag;

    tag=GWEN_Tag16_List_FindFirstByTagType(tagList, wantedTagType);
    return tag?_readDeviceFromTag((const uint8_t*) GWEN_Tag16_GetTagData(tag), (uint32_t) GWEN_Tag16_GetTagLength(tag)):NULL;
  }
  return NULL;
}



AQH_VALUE *_readValueFromTag(const uint8_t *ptr, uint32_t len)
{
  GWEN_TAG16_LIST *tagList;

  tagList=GWEN_Tag16_List_fromBuffer(ptr, len, 0);
  if (tagList) {
    GWEN_TAG16 *tag;
    AQH_VALUE *value;

    value=AQH_Value_new();
    tag=GWEN_Tag16_List_First(tagList);
    while(tag) {
      unsigned int tagType;
      char *s;

      tagType=GWEN_Tag16_GetTagType(tag);
      switch(tagType) {
      case AQH_IPCDATA_VALUE_TAGS_ID:
        AQH_Value_SetId(value, GWEN_Tag16_GetTagDataAsUint64(tag, 0));
        break;
      case AQH_IPCDATA_VALUE_TAGS_DRIVER:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Value_SetDriver(value, s);
        free(s);
        break;
      case AQH_IPCDATA_VALUE_TAGS_NAMEFORDRIVER:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Value_SetName(value, s);
        free(s);
        break;
      case AQH_IPCDATA_VALUE_TAGS_NAMEFORSYSTEM:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Value_SetNameForSystem(value, s);
        free(s);
        break;
      case AQH_IPCDATA_VALUE_TAGS_TYPE:
        AQH_Value_SetValueType(value, GWEN_Tag16_GetTagDataAsUint32(tag, 0));
        break;
      case AQH_IPCDATA_VALUE_TAGS_UNITS:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Value_SetValueUnits(value, s);
        free(s);
        break;
      case AQH_IPCDATA_VALUE_TAGS_TIMEOFCREATION:
        AQH_Value_SetTimestampCreation(value, GWEN_Tag16_GetTagDataAsUint64(tag, 0));
        break;
      case AQH_IPCDATA_VALUE_TAGS_DEVFORDRIVER:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Value_SetDeviceName(value, s);
        free(s);
        break;
      case AQH_IPCDATA_VALUE_TAGS_DEVFORSYSTEM:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Value_SetDeviceNameForSystem(value, s);
        free(s);
        break;
      case AQH_IPCDATA_VALUE_TAGS_MODALITY:
        AQH_Value_SetModality(value, GWEN_Tag16_GetTagDataAsUint32(tag, 0));
        break;
      default:
        DBG_INFO(AQH_LOGDOMAIN, "Unhandled tag typ %d (%02x)", tagType, tagType);
        break;
      }
      tag=GWEN_Tag16_List_Next(tag);
    }
    GWEN_Tag16_List_free(tagList);
    return value;
  }

  return NULL;
}



void _writeDeviceFieldsAsTagsToBuffer(const AQH_DEVICE *device, GWEN_BUFFER *buf)
{
  const char *s;

  GWEN_Tag16_WriteUint64TagToBuffer(AQH_IPCDATA_DEVICE_TAGS_ID, AQH_Device_GetId(device), buf);

  s=AQH_Device_GetDriver(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_DRIVER, s, buf);

  s=AQH_Device_GetRoomName(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_ROOMNAME, s, buf);

  s=AQH_Device_GetName(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_NAMEFORDRIVER, s, buf);

  s=AQH_Device_GetNameForSystem(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_NAMEFORSYSTEM, s, buf);

  s=AQH_Device_GetNameForGui(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_NAMEFORGUI, s, buf);

  s=AQH_Device_GetLocation(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_LOCATION, s, buf);

  s=AQH_Device_GetDescription(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_DESCRIPTION, s, buf);

  s=AQH_Device_GetDeviceType(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_DEVTYPE, s, buf);

  s=AQH_Device_GetManufacturer(device);
  if (s && *s)
    GWEN_Tag16_WriteStringTagToBuffer(AQH_IPCDATA_DEVICE_TAGS_MANUFACTURER, s, buf);

  GWEN_Tag16_WriteUint64TagToBuffer(AQH_IPCDATA_DEVICE_TAGS_TIMEOFCREATION, AQH_Device_GetTimestampCreation(device), buf);
}



AQH_DEVICE *_readDeviceFromTag(const uint8_t *ptr, uint32_t len)
{
  GWEN_TAG16_LIST *tagList;

  tagList=GWEN_Tag16_List_fromBuffer(ptr, len, 0);
  if (tagList) {
    GWEN_TAG16 *tag;
    AQH_DEVICE *device;

    device=AQH_Device_new();
    tag=GWEN_Tag16_List_First(tagList);
    while(tag) {
      unsigned int tagType;
      char *s;

      tagType=GWEN_Tag16_GetTagType(tag);
      switch(tagType) {
      case AQH_IPCDATA_DEVICE_TAGS_ID:
        AQH_Device_SetId(device, GWEN_Tag16_GetTagDataAsUint64(tag, 0));
        break;
      case AQH_IPCDATA_DEVICE_TAGS_DRIVER:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetDriver(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_ROOMNAME:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetRoomName(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_NAMEFORDRIVER:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetName(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_NAMEFORSYSTEM:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetNameForSystem(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_NAMEFORGUI:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetNameForGui(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_LOCATION:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetLocation(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_DESCRIPTION:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetDescription(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_DEVTYPE:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetDeviceType(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_MANUFACTURER:
        s=GWEN_Tag16_GetTagDataAsNewString(tag, NULL);
        AQH_Device_SetManufacturer(device, s);
        free(s);
        break;
      case AQH_IPCDATA_DEVICE_TAGS_TIMEOFCREATION:
        AQH_Device_SetTimestampCreation(device, GWEN_Tag16_GetTagDataAsUint64(tag, 0));
        break;
      default:
        DBG_INFO(AQH_LOGDOMAIN, "Unhandled tag typ %d (%02x)", tagType, tagType);
        break;
      }
      tag=GWEN_Tag16_List_Next(tag);
    }
    GWEN_Tag16_List_free(tagList);
    return device;
  }

  return NULL;
}





int AQH_Tag16_WriteNodeInfoListAsTagsToBuffer(unsigned int tagType, const AQH_NODE_INFO_LIST *nodeInfoList, GWEN_BUFFER *buf)
{
  if (nodeInfoList) {
    const AQH_NODE_INFO *nodeInfo;

    nodeInfo=AQH_NodeInfo_List_First(nodeInfoList);
    while(nodeInfo) {
      int rv;

      rv=AQH_Tag16_WriteNodeInfoAsTagToBuffer(tagType, nodeInfo, buf);
      if (rv<0) {
        DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
        GWEN_Buffer_free(buf);
        return rv;
      }

      nodeInfo=AQH_NodeInfo_List_Next(nodeInfo);
    }
  }
  return 0;
}




int AQH_Tag16_WriteNodeInfoAsTagToBuffer(unsigned int tagType, const AQH_NODE_INFO *ni, GWEN_BUFFER *buf)
{
  int startPos;
  int rv;

  startPos=GWEN_Tag16_StartTagInBuffer(tagType, buf);
  if (startPos<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", startPos);
    return startPos;
  }

  _writeNodeInfoFieldsAsTagsToBuffer(ni, buf);

  rv=GWEN_Tag16_EndTagInBuffer(startPos, buf);
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    return rv;
  }

  return 0;
}



void _writeNodeInfoFieldsAsTagsToBuffer(const AQH_NODE_INFO *ni, GWEN_BUFFER *buf)
{
  const GWEN_TIMESTAMP *t;

  GWEN_Tag16_WriteUint8TagToBuffer(AQH_IPCDATA_NODE_TAGS_BUSADDR, AQH_NodeInfo_GetBusAddress(ni), buf);
  GWEN_Tag16_WriteUint32TagToBuffer(AQH_IPCDATA_NODE_TAGS_UID, AQH_NodeInfo_GetUid(ni), buf);
  GWEN_Tag16_WriteUint32TagToBuffer(AQH_IPCDATA_NODE_TAGS_MANUFACTURER, AQH_NodeInfo_GetManufacturer(ni), buf);
  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_DEVICETYPE, AQH_NodeInfo_GetDeviceType(ni), buf);
  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_DEVICEVERSION, AQH_NodeInfo_GetDeviceVersion(ni), buf);
  GWEN_Tag16_WriteUint32TagToBuffer(AQH_IPCDATA_NODE_TAGS_FIRMWAREVERSION, AQH_NodeInfo_GetFirmwareVersion(ni), buf);
  t=AQH_NodeInfo_GetTimestampLastChange(ni);
  GWEN_Tag16_WriteUint64TagToBuffer(AQH_IPCDATA_NODE_TAGS_LASTCHANGE, t?((uint64_t)GWEN_Timestamp_toInt64(t)):0L, buf);

  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_STATSPACKETSOUT, AQH_NodeInfo_GetStatsPacketsOut(ni), buf);
  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_STATSPACKETSIN, AQH_NodeInfo_GetStatsPacketsIn(ni), buf);
  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_STATSCOLLISIONS, AQH_NodeInfo_GetStatsCollisions(ni), buf);
  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_STATSBUSY, AQH_NodeInfo_GetStatsBusy(ni), buf);
  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_STATSCRCERRORS, AQH_NodeInfo_GetStatsCrcErrors(ni), buf);
  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_STATSIOERRORS, AQH_NodeInfo_GetStatsIoErrors(ni), buf);
  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_STATSSTACKUSED, AQH_NodeInfo_GetStatsStackUsed(ni), buf);
  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_STATSBUFFERSUSED, AQH_NodeInfo_GetStatsBuffersUsed(ni), buf);
  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_STATSMAXBUFFERSUSED, AQH_NodeInfo_GetStatsMaxBuffersUsed(ni), buf);
  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_STATSRECVNOBUFFER, AQH_NodeInfo_GetStatsRecvNoBuffer(ni), buf);
  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_STATSHEAPUSED, AQH_NodeInfo_GetStatsHeapUsed(ni), buf);
  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_STATSHEAPFREE, AQH_NodeInfo_GetStatsHeapFree(ni), buf);
  GWEN_Tag16_WriteUint16TagToBuffer(AQH_IPCDATA_NODE_TAGS_STATSXRAM, AQH_NodeInfo_GetStatsXramSize(ni), buf);
}



AQH_NODE_INFO_LIST *AQH_Tag16_ReadNodeInfosFromTagList(const GWEN_TAG16_LIST *tagList, unsigned int wantedTagType)
{
  AQH_NODE_INFO_LIST *nodeInfoList;
  const GWEN_TAG16 *tag;

  nodeInfoList=AQH_NodeInfo_List_new();
  tag=GWEN_Tag16_List_First(tagList);
  while(tag) {
    unsigned int tagType;

    tagType=GWEN_Tag16_GetTagType(tag);
    if (tagType==wantedTagType) {
      AQH_NODE_INFO *nodeInfo;

      nodeInfo=_readNodeInfoFromTag((const uint8_t*) GWEN_Tag16_GetTagData(tag), (uint32_t) GWEN_Tag16_GetTagLength(tag));
      if (nodeInfo)
        AQH_NodeInfo_List_Add(nodeInfo, nodeInfoList);
    }

    tag=GWEN_Tag16_List_Next(tag);
  }

  if (AQH_NodeInfo_List_GetCount(nodeInfoList)<1) {
    AQH_NodeInfo_List_free(nodeInfoList);
    return NULL;
  }

  return nodeInfoList;
}



AQH_NODE_INFO *AQH_Tag16_ReadNodeInfoFromTagList(const GWEN_TAG16_LIST *tagList, unsigned int wantedTagType)
{
  if (tagList) {
    const GWEN_TAG16 *tag;

    tag=GWEN_Tag16_List_FindFirstByTagType(tagList, wantedTagType);
    return tag?_readNodeInfoFromTag((const uint8_t*) GWEN_Tag16_GetTagData(tag), (uint32_t) GWEN_Tag16_GetTagLength(tag)):NULL;
  }
  return NULL;
}



AQH_NODE_INFO *_readNodeInfoFromTag(const uint8_t *ptr, uint32_t len)
{
  GWEN_TAG16_LIST *tagList;

  tagList=GWEN_Tag16_List_fromBuffer(ptr, len, 0);
  if (tagList) {
    GWEN_TAG16 *tag;
    AQH_NODE_INFO *ni;

    ni=AQH_NodeInfo_new();
    tag=GWEN_Tag16_List_First(tagList);
    while(tag) {
      unsigned int tagType;
      uint64_t u64;

      tagType=GWEN_Tag16_GetTagType(tag);
      switch(tagType) {
      case AQH_IPCDATA_NODE_TAGS_BUSADDR:
        AQH_NodeInfo_SetBusAddress(ni, GWEN_Tag16_GetTagDataAsUint8(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_UID:
        AQH_NodeInfo_SetUid(ni, GWEN_Tag16_GetTagDataAsUint32(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_MANUFACTURER:
        AQH_NodeInfo_SetManufacturer(ni, GWEN_Tag16_GetTagDataAsUint32(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_DEVICETYPE:
        AQH_NodeInfo_SetDeviceType(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_DEVICEVERSION:
        AQH_NodeInfo_SetDeviceVersion(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_FIRMWAREVERSION:
        AQH_NodeInfo_SetFirmwareVersion(ni, GWEN_Tag16_GetTagDataAsUint32(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_LASTCHANGE:
        u64=GWEN_Tag16_GetTagDataAsUint64(tag, 0);
        if (u64>0) {
          GWEN_TIMESTAMP *t;

          t=GWEN_Timestamp_fromInt64((int64_t) u64);
          if (t) {
            AQH_NodeInfo_SetTimestampLastChange(ni, t);
            GWEN_Timestamp_free(t);
          }
        }
        break;
      case AQH_IPCDATA_NODE_TAGS_STATSPACKETSOUT:
        AQH_NodeInfo_SetStatsPacketsOut(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_STATSPACKETSIN:
        AQH_NodeInfo_SetStatsPacketsIn(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_STATSCOLLISIONS:
        AQH_NodeInfo_SetStatsCollisions(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_STATSBUSY:
        AQH_NodeInfo_SetStatsBusy(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_STATSCRCERRORS:
        AQH_NodeInfo_SetStatsCrcErrors(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_STATSIOERRORS:
        AQH_NodeInfo_SetStatsIoErrors(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_STATSSTACKUSED:
        AQH_NodeInfo_SetStatsStackUsed(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_STATSBUFFERSUSED:
        AQH_NodeInfo_SetStatsBuffersUsed(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_STATSMAXBUFFERSUSED:
        AQH_NodeInfo_SetStatsMaxBuffersUsed(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_STATSRECVNOBUFFER:
        AQH_NodeInfo_SetStatsRecvNoBuffer(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_STATSHEAPUSED:
        AQH_NodeInfo_SetStatsHeapUsed(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_STATSHEAPFREE:
        AQH_NodeInfo_SetStatsHeapFree(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      case AQH_IPCDATA_NODE_TAGS_STATSXRAM:
        AQH_NodeInfo_SetStatsXramSize(ni, GWEN_Tag16_GetTagDataAsUint16(tag, 0));
        break;
      default:
        DBG_INFO(AQH_LOGDOMAIN, "Unhandled tag typ %d (%02x)", tagType, tagType);
        break;
      }
      tag=GWEN_Tag16_List_Next(tag);
    }
    GWEN_Tag16_List_free(tagList);
    return ni;
  }

  return NULL;
}




