
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "aqhome/serial.h"

#include <gwenhywfar/debug.h>
#include <gwenhywfar/text.h>
#include <gwenhywfar/gwentime.h>
#include <gwenhywfar/buffer.h>



int testRecv()
{
  AQH_SERIAL *sr;
  int rv;
  int i;
  GWEN_BUFFER *dbuf;

  fprintf(stdout, "Opening device...\n");
  sr=AQH_Serial_new("/dev/ttyUSB0", 240);
  rv=AQH_Serial_Open(sr);
  if (rv<0) {
    DBG_ERROR(NULL, "ERROR opening device (%d)", rv);
    AQH_Serial_free(sr);
    return 2;
  }
  fprintf(stdout, "Device open, waiting for packets\n");

  dbuf=GWEN_Buffer_new(0, 256, 0, 1);
  for (i=0; ; i++) {
    uint8_t buffer[1024];
    GWEN_TIME *ti;

    fprintf(stdout, "Waiting for packet...\n");
    rv=AQH_Serial_Recv(sr, buffer, sizeof(buffer)-1);
    if (rv<0)
      break;
    ti=GWEN_CurrentTime();
    GWEN_Time_toString(ti, "YYYY-MM-DD hh:mm:ss", dbuf);
    fprintf(stdout, "%s: Received:\n", GWEN_Buffer_GetStart(dbuf));
    GWEN_Text_DumpString(buffer, rv, 2);
    GWEN_Time_free(ti);
    GWEN_Buffer_Reset(dbuf);
    fprintf(stdout, "\n");
  }
  GWEN_Buffer_free(dbuf);

  AQH_Serial_Close(sr);
  AQH_Serial_free(sr);
  return 0;
}



int main(void)
{
  return testRecv();
}


