/****************************************************************************
 * 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/data/m_ipcd_devices.h"
#include "aqhome/msg/ipc/m_ipc_tag16.h"
#include "aqhome/msg/ipc/data/m_ipcd.h"
#include "aqhome/msg/ipc/m_ipc.h"

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



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



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

AQH_MESSAGE *AQH_IpcdMessageDevices_new(uint16_t code, uint32_t msgId, uint32_t refMsgId,
                                        uint32_t flags, const AQH_DEVICE_LIST *deviceList)
{
  AQH_MESSAGE *msg;
  GWEN_BUFFER *buf;
  int rv;

  buf=GWEN_Buffer_new(0, 256, 0, 1);
  GWEN_Tag16_WriteUint32TagToBuffer(AQH_MSGDATA_DEVICES_TAGS_FLAGS, flags, buf);
  rv=AQH_Tag16_WriteDeviceListAsTagsToBuffer(AQH_MSGDATA_DEVICES_TAGS_DEVICE, deviceList, buf);
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    GWEN_Buffer_free(buf);
    return NULL;
  }

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



AQH_MESSAGE *AQH_IpcdMessageDevices_newForOneDevice(uint16_t code, uint32_t msgId, uint32_t refMsgId,
                                                    uint32_t flags, const AQH_DEVICE *device)
{
  AQH_MESSAGE *msg;
  GWEN_BUFFER *buf;
  int rv;

  buf=GWEN_Buffer_new(0, 256, 0, 1);
  GWEN_Tag16_WriteUint32TagToBuffer(AQH_MSGDATA_DEVICES_TAGS_FLAGS, flags, buf);
  rv=AQH_Tag16_WriteDeviceAsTagToBuffer(AQH_MSGDATA_DEVICES_TAGS_DEVICE, device, buf);
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    GWEN_Buffer_free(buf);
    return NULL;
  }

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



AQH_DEVICE_LIST *AQH_IpcdMessageDevices_ReadDeviceList(const GWEN_TAG16_LIST *tagList)
{
  if (tagList) {
    AQH_DEVICE_LIST *deviceList;

    deviceList=AQH_Tag16_ReadDevicesFromTagList(tagList, AQH_MSGDATA_DEVICES_TAGS_DEVICE);
    if (deviceList==NULL) {
      DBG_INFO(AQH_LOGDOMAIN, "No device list received");
    }
    return deviceList;
  }
  return NULL;
}



AQH_DEVICE *AQH_IpcdMessageDevices_ReadFirstDevice(const GWEN_TAG16_LIST *tagList)
{
  if (tagList) {
    AQH_DEVICE *device;

    device=AQH_Tag16_ReadDeviceFromTagList(tagList, AQH_MSGDATA_DEVICES_TAGS_DEVICE);
    if (device==NULL) {
      DBG_INFO(AQH_LOGDOMAIN, "No device received");
    }
    return device;
  }
  return NULL;
}



uint32_t AQH_IpcdMessageDevices_GetFlags(const GWEN_TAG16_LIST *tagList)
{
  return tagList?AQH_Tag16_GetTagDataAsUint32(tagList, AQH_MSGDATA_DEVICES_TAGS_FLAGS, 0):0;
}



void AQH_IpcdMessageDevices_DumpToBuffer(const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList,
                                         GWEN_BUFFER *dbuf, const char *sText)
{
  uint32_t flags;

  flags=AQH_IpcdMessageDevices_GetFlags(tagList);
  GWEN_Buffer_AppendArgs(dbuf,
                         "DEVICES(%s) %s (code=%d, proto=%d, proto version=%d, flags=0x%08x)\n",
                         AQH_IpcdMessage_MsgTypeToChar(AQH_IpcMessage_GetCode(msg)),
                         sText?sText:"",
                         AQH_IpcMessage_GetCode(msg),
                         AQH_IpcMessage_GetProtoId(msg),
                         AQH_IpcMessage_GetProtoVersion(msg),
                         (unsigned int) flags);
}




