/****************************************************************************
 * 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/nodes/m_ipcn_getdevices_rsp.h"
#include "aqhome/msg/ipc/m_ipc_tag16.h"
#include "aqhome/msg/ipc/nodes/m_ipcn.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_IpcnMessageGetDevicesRsp_new(uint16_t code, uint32_t msgId, uint32_t refMsgId, uint32_t flags, const AQH_NODE_INFO *ni)
{
  AQH_MESSAGE *msg;
  GWEN_BUFFER *buf;

  buf=GWEN_Buffer_new(0, 256, 0, 1);

  GWEN_Tag16_WriteUint32TagToBuffer(AQH_MSGNODE_GETDEVICES_RSP_TAGS_FLAGS, flags, buf);
  AQH_Tag16_WriteNodeInfoAsTagToBuffer(AQH_MSGNODE_GETDEVICES_RSP_TAGS_NODEINFO, ni, buf);

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



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



AQH_NODE_INFO *AQH_IpcnMessageGetDevicesRsp_ReadNodeInfo(const GWEN_TAG16_LIST *tagList)
{
  if (tagList) {
    AQH_NODE_INFO *ni;

    ni=AQH_Tag16_ReadNodeInfoFromTagList(tagList, AQH_MSGNODE_GETDEVICES_RSP_TAGS_NODEINFO);
    if (ni==NULL) {
      DBG_INFO(AQH_LOGDOMAIN, "No node info received");
    }
    return ni;
  }
  return NULL;
}



void AQH_IpcnMessageGetDevicesRsp_DumpToBuffer(const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList,
                                               GWEN_BUFFER *dbuf, const char *sText)
{
  AQH_NODE_INFO *ni;
  uint8_t busAddr;
  uint32_t uid;
  uint32_t flags;

  ni=tagList?AQH_IpcnMessageGetDevicesRsp_ReadNodeInfo(tagList):NULL;
  busAddr=ni?AQH_NodeInfo_GetBusAddress(ni):0;
  uid=ni?AQH_NodeInfo_GetUid(ni):0;
  flags=tagList?AQH_Tag16_GetTagDataAsUint32(tagList, AQH_MSGNODE_GETDEVICES_RSP_TAGS_FLAGS, 0):0;

  GWEN_Buffer_AppendArgs(dbuf, "GETDEVICESRSP(%s) %s (code=%d, protocol=%d/%d, flags=%04x, addr=%d, uid=%08x)\n",
			 AQH_IpcnMessage_MsgTypeToChar(AQH_IpcMessage_GetCode(msg)),
			 sText?sText:"",
			 AQH_IpcMessage_GetCode(msg),
			 AQH_IpcMessage_GetProtoId(msg),
			 AQH_IpcMessage_GetProtoVersion(msg),
			 flags,
			 busAddr,
			 uid);
  AQH_NodeInfo_free(ni);
}






