/****************************************************************************
 * 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 AQH_FDOBJECT_H
#define AQH_FDOBJECT_H


#include <aqhome/events2/object.h>


enum {
  AQH_FDOBJECT_FDMODE_READ=1,
  AQH_FDOBJECT_FDMODE_WRITE
};


enum {
  AQH_FDOBJECT_SIGNAL_ISREADY=AQH_OBJECT_SIGNAL_LAST,
  AQH_FDOBJECT_SIGNAL_CLOSED,
  AQH_FDOBJECT_SIGNAL_LAST
};


typedef int (*AQH_FDOBJECT_STARTMSG_FN)(AQH_OBJECT *o);
typedef void (*AQH_FDOBJECT_ENDMSG_FN)(AQH_OBJECT *o);
typedef int (*AQH_FDOBJECT_FLUSH_FN)(AQH_OBJECT *o);


/**
 * Create object for given file descriptor (takes over ownership of the given file descriptor, use dup() if
 * fd used in another object).
 */
AQHOME_API AQH_OBJECT *AQH_FdObject_new(AQH_EVENT_LOOP *eventLoop, int fd, int mode);

AQHOME_API int AQH_FdObject_GetFdMode(const AQH_OBJECT *o);
AQHOME_API void AQH_FdObject_SetFdMode(AQH_OBJECT *o, int i);

AQHOME_API int AQH_FdObject_GetFd(const AQH_OBJECT *o);

AQHOME_API int AQH_FdObject_Read(AQH_OBJECT *o, uint8_t *ptrBuffer, uint32_t lenBuffer);
AQHOME_API int AQH_FdObject_Write(AQH_OBJECT *o, const uint8_t *ptrBuffer, uint32_t lenBuffer);
AQHOME_API int AQH_FdObject_FlushInput(AQH_OBJECT *o);

AQHOME_API int AQH_FdObject_StartMsg(AQH_OBJECT *o);
AQHOME_API void AQH_FdObject_EndMsg(AQH_OBJECT *o);

AQHOME_API AQH_FDOBJECT_STARTMSG_FN AQH_FdObject_SetStartMsgFn(AQH_OBJECT *o, AQH_FDOBJECT_STARTMSG_FN f);
AQHOME_API AQH_FDOBJECT_ENDMSG_FN AQH_FdObject_SetEndMsgFn(AQH_OBJECT *o, AQH_FDOBJECT_ENDMSG_FN f);
AQHOME_API AQH_FDOBJECT_FLUSH_FN AQH_FdObject_SetFlushFn(AQH_OBJECT *o, AQH_FDOBJECT_FLUSH_FN f);

AQHOME_API int AQH_FdObject_WaitForActivity(AQH_OBJECT *o, int timeoutInMillisecs);
AQHOME_API void AQH_FdObject_Abort(AQH_OBJECT *o);


#endif
