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


#include "./msgreader.h"

#include <gwenhywfar/ringbuffer.h>

#define AQH_MSG_READER_RINGBUFFER_SIZE   1024
#define AQH_MSG_READER_HEADERBUFFER_SIZE 32


typedef struct AQH_MSG_READER AQH_MSG_READER;

/**
 * Read data for a message from the internal ring buffer.
 *
 * @return 1 if something has been done, 0 if not, negative on error
 * @param o object (THIS)
 */
typedef int (*AQH_MSG_READER_READMSG_FN)(AQH_OBJECT *o);



struct AQH_MSG_READER {
  GWEN_RINGBUFFER *ringBuffer;

  int bytesReceived;
  int bytesLeft;
  uint8_t headerBuffer[AQH_MSG_READER_HEADERBUFFER_SIZE];
  uint8_t *currentMsgBuf;

  uint32_t flags;
  uint64_t timestamp;

  AQH_OBJECT *fdObject;

  AQH_MSG_READER_READMSG_FN readMsgFn;
};


AQH_OBJECT *AQH_MsgReader_new(AQH_EVENT_LOOP *eventLoop, AQH_OBJECT *fdObject);

AQH_MSG_READER *AQH_MsgReader_GetData(const AQH_OBJECT *o);
int AQH_MsgReader_ReadRemainderFromRingbuffer(AQH_OBJECT *o);

int AQH_MsgReader_ReadMsg(AQH_OBJECT *o);
AQH_MSG_READER_READMSG_FN AQH_MsgReader_SetReadMsgFn(AQH_OBJECT *o, AQH_MSG_READER_READMSG_FN f);


uint32_t AQH_MsgReader_GetFlags(const AQH_OBJECT *o);
void AQH_MsgReader_SetFlags(AQH_OBJECT *o, uint32_t f);
void AQH_MsgReader_AddFlags(AQH_OBJECT *o, uint32_t f);
void AQH_MsgReader_SubFlags(AQH_OBJECT *o, uint32_t f);

void AQH_MsgReader_StartSkipping(AQH_OBJECT *o);




#endif

