/****************************************************************************
 * 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 "./ipc_msgreader.h"
#include "./msgreader_p.h"
#include <aqhome/events2/fdobject.h>

#include <gwenhywfar/inherit.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/endianfns.h>
#include <gwenhywfar/text.h>

#include <sys/socket.h>


#define AQH_MSG_READER_HEADER_SIZE 4
#define AQH_MSG_READER_MINMSGSIZE  12
#define AQH_MSG_READER_MAXMSGSIZE 20480



/* ------------------------------------------------------------------------------------------------
 * forward declarations
 * ------------------------------------------------------------------------------------------------
 */

static int _readMsg(AQH_OBJECT *o);
static int _readHeaderFromRingbuffer(AQH_MSG_READER *xo);



/* ------------------------------------------------------------------------------------------------
 * implementation
 * ------------------------------------------------------------------------------------------------
 */

AQH_OBJECT *AQH_IpcMsgReader_new(AQH_EVENT_LOOP *eventLoop, AQH_OBJECT *fdObject)
{
  AQH_OBJECT *o;

  o=AQH_MsgReader_new(eventLoop, fdObject);
  AQH_MsgReader_SetReadMsgFn(o, _readMsg);

  return o;
}



int _readMsg(AQH_OBJECT *o)
{
  AQH_MSG_READER *xo;

  xo=AQH_MsgReader_GetData(o);
  if (xo) {
    int rv;

    if (xo->bytesReceived<AQH_MSG_READER_HEADER_SIZE) {
      DBG_DEBUG(AQH_LOGDOMAIN, "Reading header");
      rv=_readHeaderFromRingbuffer(xo);
      if (rv<0) {
	DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
	return rv;
      }
    }

    if (xo->bytesReceived>=AQH_MSG_READER_HEADER_SIZE) {
      DBG_DEBUG(AQH_LOGDOMAIN, "Reading body");
      /* reading remainder of msg directly into allocated buffer */
      rv=AQH_MsgReader_ReadRemainderFromRingbuffer(o);
      if (rv<0) {
	DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
	return rv;
      }
      else if (rv==1) {
	int msgLen;
	uint8_t *msgPtr;

	msgLen=xo->bytesReceived;
	msgPtr=xo->currentMsgBuf;

	xo->bytesReceived=0;
	xo->bytesLeft=0;
	xo->currentMsgBuf=NULL;

	rv=AQH_Object_EmitSignal(o, AQH_MSG_READER_SIGNAL_MSGRECVD, msgLen, (void*) msgPtr);
	if (rv==0) {
	  DBG_ERROR(AQH_LOGDOMAIN, "Received message ignored");
	}
        free(msgPtr);
	return 1;
      }
    }
    return 0;
  }
  return GWEN_ERROR_GENERIC;
}



int _readHeaderFromRingbuffer(AQH_MSG_READER *xo)
{
  uint32_t remaining;
  int rv;
  uint32_t xferSize;
  uint32_t bytesInBuffer;

  bytesInBuffer=GWEN_RingBuffer_GetUsedBytes(xo->ringBuffer);
 
  /* still reading header */
  remaining=AQH_MSG_READER_HEADER_SIZE-xo->bytesReceived;
  if (bytesInBuffer<remaining)
    remaining=bytesInBuffer;
  xferSize=remaining;
  rv=GWEN_RingBuffer_ReadBytes(xo->ringBuffer, (char*) (xo->headerBuffer+xo->bytesReceived), &xferSize);
  if (rv<0) {
    DBG_DEBUG(AQH_LOGDOMAIN, "Ringbuffer empty");
    return 0;
  }
  if (xferSize<remaining) {
    DBG_ERROR(AQH_LOGDOMAIN, "Read fewer bytes than available?");
    return GWEN_ERROR_GENERIC;
  }
  xo->bytesReceived+=xferSize;
  if (xo->bytesReceived==AQH_MSG_READER_HEADER_SIZE) {
    uint32_t msgLen;

    /* full size received, parse msg size, allocate buffer */
    msgLen=GWEN_ENDIAN_LE32TOH(*((const uint32_t*)(xo->headerBuffer)));
    if (msgLen<AQH_MSG_READER_MINMSGSIZE || msgLen>AQH_MSG_READER_MAXMSGSIZE) {
      DBG_ERROR(AQH_LOGDOMAIN, "Bad message size(%lu)", (unsigned long int) msgLen);
      return GWEN_ERROR_GENERIC;
    }
    xo->currentMsgBuf=(uint8_t*) malloc(msgLen);
    memmove(xo->currentMsgBuf, xo->headerBuffer, xo->bytesReceived);
    xo->bytesLeft=msgLen-xo->bytesReceived;
  }

  return 0;
}




