/****************************************************************************
 * 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/ipc/data/msg_data_values.h>
#include <aqhome/ipc/data/ipc_data.h>
#include <aqhome/ipc/msg_ipc_tag16.h>

#include <gwenhywfar/msg.h>
#include <gwenhywfar/buffer.h>

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

#include <string.h>


#define AQH_MSGDATA_VALUES_MINSIZE GWEN_MSGIPC_OFFS_PAYLOAD



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



/* ------------------------------------------------------------------------------------------------
 * code
 * ------------------------------------------------------------------------------------------------
 */

GWEN_MSG *AQH_ValuesDataIpcMsg_new(uint16_t code, uint32_t flags, const AQH_VALUE_LIST *valueList)
{
  GWEN_MSG *msg;
  GWEN_BUFFER *buf;
  int rv;

  buf=GWEN_Buffer_new(0, 256, 0, 1);
  GWEN_Tag16_WriteUint32TagToBuffer(AQH_MSGDATA_VALUES_TAGS_FLAGS, flags, buf);
  rv=AQH_DataIpc_WriteValueListAsTagsToBuffer(AQH_MSGDATA_VALUES_TAGS_VALUE, valueList, buf);
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    GWEN_Buffer_free(buf);
    return NULL;
  }

  msg=AQH_Tag16IpcMsg_new(AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, code,
                          GWEN_Buffer_GetUsedBytes(buf), (const uint8_t*) GWEN_Buffer_GetStart(buf));
  GWEN_Buffer_free(buf);
  return msg;
}



GWEN_MSG *AQH_ValuesDataIpcMsg_newForOneValue(uint16_t code, uint32_t flags, const AQH_VALUE *value)
{
  GWEN_MSG *msg;
  GWEN_BUFFER *buf;
  int rv;

  buf=GWEN_Buffer_new(0, 256, 0, 1);
  GWEN_Tag16_WriteUint32TagToBuffer(AQH_MSGDATA_VALUES_TAGS_FLAGS, flags, buf);
  rv=AQH_DataIpc_WriteValueAsTagToBuffer(AQH_MSGDATA_VALUES_TAGS_VALUE, value, buf);
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    GWEN_Buffer_free(buf);
    return NULL;
  }

  msg=AQH_Tag16IpcMsg_new(AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, code,
                          GWEN_Buffer_GetUsedBytes(buf), (const uint8_t*) GWEN_Buffer_GetStart(buf));
  GWEN_Buffer_free(buf);
  return msg;
}



void AQH_ValuesDataIpcMsg_Parse(GWEN_MSG *msg, int doCopy)
{
  AQH_Tag16IpcMsg_ExtendAndParse(msg, doCopy);
}



AQH_VALUE_LIST *AQH_ValuesDataIpcMsg_ReadValueList(const GWEN_MSG *msg)
{
  const GWEN_TAG16_LIST *tagList;

  tagList=AQH_Tag16IpcMsg_GetTags(msg);
  if (tagList) {
    AQH_VALUE_LIST *valueList;

    valueList=AQH_DataIpc_ReadValuesFromTagList(tagList, AQH_MSGDATA_VALUES_TAGS_VALUE);
    if (valueList==NULL) {
      DBG_INFO(AQH_LOGDOMAIN, "No value list received");
    }
    return valueList;
  }
  else {
    DBG_INFO(AQH_LOGDOMAIN, "No tag16 list received");
    return NULL;
  }
}



AQH_VALUE *AQH_ValuesDataIpcMsg_ReadFirstValue(const GWEN_MSG *msg)
{
  return AQH_DataIpc_ReadValueFromTagList(AQH_Tag16IpcMsg_GetTags(msg), AQH_MSGDATA_VALUES_TAGS_VALUE);
}




uint32_t AQH_ValuesDataIpcMsg_GetFlags(const GWEN_MSG *msg)
{
  return AQH_Tag16IpcMsg_GetTagDataAsUint32(msg, AQH_MSGDATA_VALUES_TAGS_FLAGS, 0);
}



void AQH_ValuesDataIpcMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSGDATA_VALUES_MINSIZE) {
    GWEN_Buffer_AppendArgs(dbuf,
                           "VALUES (code=%d, proto=%d, proto version=%d, flags=0x%08x)\n",
                           GWEN_IpcMsg_GetCode(msg),
                           GWEN_IpcMsg_GetProtoId(msg),
                           GWEN_IpcMsg_GetProtoVersion(msg),
                           (unsigned int)AQH_ValuesDataIpcMsg_GetFlags(msg));
  }
}






