/****************************************************************************
 * 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.
 ****************************************************************************/

#ifndef AQHOME_TOOL_CLIENT_H
#define AQHOME_TOOL_CLIENT_H



#include "aqhome/msg/ipc/m_ipc_tag16.h"

#include <gwenhywfar/args.h>


#define AQH_TOOL_CLIENT_NODEADDR 240

#define AQH_TOOL_CLIENT_CONNECTFLAGS_WITHCONNECTMSG 0x0001
#define AQH_TOOL_CLIENT_CONNECTFLAGS_WITHGRPMSG     0x0002



typedef AQH_MESSAGE* (*AQH_TOOLCLIENT_CREATEREQUESTMESSAGE_FN)(AQH_OBJECT *o, uint32_t msgId);
typedef int (*AQH_TOOLCLIENT_HANDLERESPONSEMESSAGE_FN)(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, int first);


AQH_OBJECT *AQH_ToolClient_new(AQH_EVENT_LOOP *eventLoop,
                               uint8_t protoId, uint8_t protoVer,
                               GWEN_DB_NODE *dbGlobalArgs, const GWEN_ARGS *argDescrs);
int AQH_ToolClient_ReadLocalArgs(AQH_OBJECT *o, int argc, char **argv);

int AQH_ToolClient_Run(AQH_OBJECT *o);
int AQH_ToolClient_Watch(AQH_OBJECT *o);

int AQH_ToolClient_Connect(AQH_OBJECT *o, uint32_t connFlags, uint32_t connMsgFlags, uint32_t grps);
int AQH_ToolClient_RunConnected(AQH_OBJECT *o);
int AQH_ToolClient_WatchConnected(AQH_OBJECT *o);

int AQH_ToolClient_RunConnectedWithNodeMsgs(AQH_OBJECT *o);


void AQH_ToolClient_SendNodeMsg(AQH_OBJECT *o, const AQH_MESSAGE *nodeMsg);
AQH_MESSAGE *AQH_ToolClient_WaitForNodeMsg(AQH_OBJECT *o, int nodeSrcAddr, uint8_t nodeMsgType, int timeoutInSeconds);

AQH_OBJECT *AQH_ToolClient_GetEndpoint(const AQH_OBJECT *o);



GWEN_DB_NODE *AQH_ToolClient_GetDbGlobalArgs(const AQH_OBJECT *o);
GWEN_DB_NODE *AQH_ToolClient_GetDbLocalArgs(const AQH_OBJECT *o);

uint32_t AQH_ToolClient_GetFlags(const AQH_OBJECT *o);
void AQH_ToolClient_SetFlags(AQH_OBJECT *o, uint32_t f);
void AQH_ToolClient_AddFlags(AQH_OBJECT *o, uint32_t f);
void AQH_ToolClient_SubFlags(AQH_OBJECT *o, uint32_t f);

void AQH_ToolClient_SetAcceptedGroups(AQH_OBJECT *o, uint32_t f);


/**
 * @param o client object
 * @param msg received message
 * @param tagList list of GWEN_TAG16 objects
 * @param first msg is the first response received
 */
int AQH_ToolClient_HandleResultMsg(const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, int first);

void AQH_ToolClient_SetCreateRequestMessageFn(AQH_OBJECT *o, AQH_TOOLCLIENT_CREATEREQUESTMESSAGE_FN f);
void AQH_ToolClient_SetHandleResponseMessageFn(AQH_OBJECT *o, AQH_TOOLCLIENT_HANDLERESPONSEMESSAGE_FN f);



#endif

