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

#ifndef SERVER_H
#define SERVER_H



#include "aqhome-nodes/types/device.h"

#include "aqhome/events2/object.h"
#include <aqhome/ipc2/msgrequest.h>



#define AQH_ENDPOINT_PERMS_LISTVALUES  0x0001
#define AQH_ENDPOINT_PERMS_READVALUE   0x0002
#define AQH_ENDPOINT_PERMS_ADDVALUE    0x0004

#define AQH_ENDPOINT_PERMS_LISTDATA    0x0010
#define AQH_ENDPOINT_PERMS_READDATA    0x0020
#define AQH_ENDPOINT_PERMS_ADDDATA     0x0040
#define AQH_ENDPOINT_PERMS_SETDATA     0x0080

#define AQH_ENDPOINT_PERMS_LISTDEVICES 0x0100
#define AQH_ENDPOINT_PERMS_READDEVICE  0x0200
#define AQH_ENDPOINT_PERMS_ADDDEVICE   0x0400
#define AQH_ENDPOINT_PERMS_MODDEVICE   0x0800



#define AQH_ENDPOINT_VID_STATS_PACKETS_IN      0xe0
#define AQH_ENDPOINT_VID_STATS_PACKETS_OUT     0xe1
#define AQH_ENDPOINT_VID_STATS_ERRS_CONTENT    0xe2
#define AQH_ENDPOINT_VID_STATS_ERRS_IO         0xe3
#define AQH_ENDPOINT_VID_STATS_ERRS_NOBUF      0xe4
#define AQH_ENDPOINT_VID_STATS_ERRS_COLLISIONS 0xe5
#define AQH_ENDPOINT_VID_STATS_ERRS_BUSY       0xe6
#define AQH_ENDPOINT_VID_STATS_HEAP_USED       0xe7
#define AQH_ENDPOINT_VID_STATS_HEAP_FREE       0xe8



AQH_OBJECT *AQH_NodeServer_new(AQH_EVENT_LOOP *eventLoop);
int AQH_NodeServer_Init(AQH_OBJECT *o, int argc, char **argv);
void AQH_NodeServer_Fini(AQH_OBJECT *o);

/* loop functions */
void AQH_NodeServer_CleanupClients(AQH_OBJECT *o);
void AQH_NodeServer_HandleTtyMsgs(AQH_OBJECT *o);
void AQH_NodeServer_HandleClientMsgs(AQH_OBJECT *o);
void AQH_NodeServer_HandleBrokerMsgs(AQH_OBJECT *o);
void AQH_NodeServer_CheckBrokerConnection(AQH_OBJECT *o);
void AQH_NodeServer_CheckTtyConnection(AQH_OBJECT *o);
void AQH_NodeServer_SendHeartbeat(AQH_OBJECT *o);


/* getters and setters */
int AQH_NodeServer_GetTimeout(const AQH_OBJECT *o);
void AQH_NodeServer_SetLogFile(AQH_OBJECT *o, const char *s);
void AQH_NodeServer_SetDbFile(AQH_OBJECT *o, const char *s);
void AQH_NodeServer_SetPidFile(AQH_OBJECT *o, const char *s);

void AQH_NodeServer_SetDevicePath(AQH_OBJECT *o, const char *s);
void AQH_NodeServer_SetTpcAddress(AQH_OBJECT *o, const char *s);
void AQH_NodeServer_SetTcpPort(AQH_OBJECT *o, int i);
void AQH_NodeServer_SetBrokerAddress(AQH_OBJECT *o, const char *s);
void AQH_NodeServer_SetBrokerPort(AQH_OBJECT *o, int i);
void AQH_NodeServer_SetBrokerClientId(AQH_OBJECT *o, const char *s);


/* device management */
const AQHNODE_DEVICE_LIST *AQH_NodeServer_GetDeviceDefList(const AQH_OBJECT *o);
const AQHNODE_DEVICE *AQH_NodeServer_FindDeviceDef(const AQH_OBJECT *o,
                                                   uint32_t manufacturer,
                                                   uint16_t deviceType, uint16_t deviceVersion);
const AQHNODE_DEVICE *AQH_NodeServer_GetDeviceDefByName(const AQH_OBJECT *o, const char *name);


/* request management */
AQH_MSG_REQUEST *AQH_NodeServer_GetRequestTree(const AQH_OBJECT *o);
void AQH_NodeServer_AddRequestToTree(AQH_OBJECT *o, AQH_MSG_REQUEST *rq);
void AQH_NodeServer_CleanupRequests(AQH_OBJECT *o);

void AQH_NodeServer_WriteTtyMsgToLogFile(AQH_OBJECT *o, const AQH_MESSAGE *msg, const char *txt);





#endif
