/* System includes */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

/* libhomeradar header include */
#include "libhomeradar.h"

/* Callback function to show aircraft informations */
void newcontact(void *handle,const int *id, LHR_CONTACT *C) {

  /* Will only show ICAO24 code and reg as UNKNOWN
     because db lookup is not enabled */
  printf("Contact: %s [%s]\n",C->icao24,(
          C->reg)?C->reg:"UNKNOWN - Lookup not enabled?!");

  /* Free LHR_CONTACT struct */
  homeradar_freecontact(C);
}

/* Main program */
int main(int argc, char **argv) {
  int e;
  LHR H = homeradar_init(16,LHR_AUTOCONNECT,&e,NULL);

  /* No error */
  if (H == NULL) {
    printf("Error initializing libhomeradar. Errorcode #%d\n",e);
    return 255;
  }

  /* Add host without event listener
     Our testserver listens not on the standard SBS-1 port! */
  e = homeradar_addhost(H, "sbs1.libhomeradar.org", LHR_SBS1, 33033, 0, NULL);

  /* Setup the event listener for the LHR_ONCONTACT event */
  if (homeradar_onhandler(H, LHR_ONCONTACT, e, newcontact)==0) {
    printf("Unable to add event listener\n");
  }

  /* Do nothing - in this example - loop will be finished after 60 seconds
     In your real program you can do now what you want */
  e = 10;
  while (e--) sleep(6);

  /* Shutdown libhomeradar and free all alocated memory */
  printf("Shutdown libhomeradar...\n");
  homeradar_destroy(H);

  /* Return success */
  return 0;
}
