/*

                                   _JNJ`                        
                               .JNMH`                           
                              JMMF`       `;.                   
                            .NMM)          `MN.                 
                            MMM)            (MML                
                           (MMM`             MMML               
                     M  I  D  N  I  G  H  T    C  o  D  E       
                           (NMMF             MHNH               
                            NMML            .MMM                
                             NMML          .NMH                 
                              4MMNL       .#F                   
                               `4HNNL_   `                      
                                   `"""`                        

                          Copyright (C) 2004-2007
       "Ian (Larry) Latter" <ian dot latter at midnightcode dot org>

    Midnight Code is a registered trademark of Ian Latter.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, and mirrored at the Midnight Code web
    site; as at version 2 of the License only.
  
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
  
    You should have received a copy of the GNU General Public License
    (version 2) along with this program; if not, write to the Free
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA, or see http://midnightcode.org/gplv2.txt

*/

/* gps.c :: Midnight Code GPS agent example program			*/


#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <errno.h>
#include <time.h>


#include <libmidnightcode.h>


int
main(int argc, char **argv) {
  struct in_addr cfg_to_host;
  int cfg_port;
  int cfg_interval;
  int cfg_help = 0;
  char to_host_s[21];
  void * tmp_void_p;
  struct midnightcode_socket_head_t * sockets;
  struct midnightcode_gps_head_t * fixes;
  struct midnightcode_gps_element_t * fix;
  int ret_i;

 
  // Prepare switches
  struct midnightcode_switch_t switches[] = {
    { "s", "server", "Server to connect to",
      MIDNIGHTCODE_PARSE_TYPE_HOST, 0, 0, &cfg_to_host.s_addr },
    { "p", "port", "Port to connect to (2947)",
      MIDNIGHTCODE_PARSE_TYPE_INT, 0, 0, &cfg_port },
    { "i", "int", "Interval to poll data (1s=1000000)",
      MIDNIGHTCODE_PARSE_TYPE_INT, 0, 0, &cfg_interval },
    { "h", "help", "Display this help/usage screen",
      MIDNIGHTCODE_PARSE_TYPE_NONE, 0, 0, &cfg_help },
    { 0 }
  };



  // Set-up initial values;
  memset(&cfg_to_host, 0, sizeof(struct in_addr));
  cfg_port = MIDNIGHTCODE_GPS_PORT;
  cfg_interval = 1000000;



  // Process switches
  if((ret_i = midnightcode_switch_process(argc, argv, switches)) != 0) {
    fprintf(stdout, "\n");
    midnightcode_logo(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON);
    midnightcode_license(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON, 
      MIDNIGHTCODE_LICENSE_TEXT_COPYRIGHT);
    fprintf(stdout, "    Error in switches, switch \"%s\" is unkown"
      " at parameter number %d.\n\n", argv[ret_i], ret_i);
    midnightcode_switch_usage(MIDNIGHTCODE_SWITCH_STYLE_SOFT, argv[0],
      switches);
    exit(1);
  }
  if(cfg_help) {
    midnightcode_logo(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON);
    midnightcode_license(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON, 
      MIDNIGHTCODE_LICENSE_TEXT_COPYRIGHT);
    midnightcode_switch_usage(MIDNIGHTCODE_SWITCH_STYLE_SOFT, argv[0],
      switches);
    exit(1);
  }
  fflush(stdout);



  // Process license and logo
  midnightcode_logo(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON);
  midnightcode_license(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON, 
    MIDNIGHTCODE_LICENSE_TEXT_ALL);



  // Program title
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_H1,
    MIDNIGHTCODE_STATUS_STYLE_NORMAL, "GPS agent example: %s", argv[0]);

  fprintf(stdout, "\n");
  fflush(stdout);




  // Validate user params
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Validating user parameters");
  if(midnightcode_ip_isspecial(MIDNIGHTCODE_IP_SPECIAL_RFC3171|
    MIDNIGHTCODE_IP_SPECIAL_RFC3171, cfg_to_host.s_addr) ||
    cfg_to_host.s_addr == 0) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    fprintf(stdout, "\n");
    exit(1);
  } else {
    memset(&to_host_s, 0, 21);
    snprintf(to_host_s, 21, "%s", inet_ntoa(cfg_to_host));
  }
  midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);




  // Initialise Socket library
  midnightcode_list_init(socket_struct_head, midnightcode_socket_element_t);
  tmp_void_p = (void *)&socket_struct_head;
  sockets = tmp_void_p;

  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Initialise socket library");
  if(midnightcode_socket_initialise(sockets) < 0) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    fprintf(stdout, "\n");
    exit(1);
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }



  // Initialise GPS library
  midnightcode_list_init(gps_struct_head, midnightcode_gps_element_t);
  tmp_void_p = (void *)&gps_struct_head;
  fixes = tmp_void_p;

  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Initialise GPS library");
  if(midnightcode_gps_initialise(fixes) < 0) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    fprintf(stdout, "\n");
    exit(1);
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }



  // Open GPS session
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Open GPS session [%s:%d]",
                           to_host_s, cfg_port);
  if(midnightcode_gps_open(&fix, to_host_s, cfg_port) < 0) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    fprintf(stdout, "\n");
    exit(1);
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }



  fprintf(stdout, "\n");
  while(1) {

    // Position Data
    midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_BLURB,
                             MIDNIGHTCODE_STATUS_STYLE_UNKNOWN,
      "State: %d  Time: %lf",
      fix->state, fix->time);
    midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_BLURB,
                             MIDNIGHTCODE_STATUS_STYLE_UNKNOWN,
      "Lat: %lf  Long: %lf  Alt: %lf",
      fix->latitude, fix->longitude, fix->altitude);
    midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_BLURB,
                             MIDNIGHTCODE_STATUS_STYLE_UNKNOWN,
      "Course: %lf  Speed: %lf  Climb: %lf",
      fix->course, fix->speed, fix->climb);
    fprintf(stdout, "\n");

    // Pause
    sleep(1);

  } 
  fprintf(stdout, "\n");
  // usleep(cfg_interval);



  // Close GPS session
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Close GPS session");
  if(midnightcode_gps_close(fix) < 0) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    fprintf(stdout, "\n");
    exit(1);
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }



  // Shutdown GPS library
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Shutdown GPS library");
  if(midnightcode_gps_shutdown(fixes) < 0) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    fprintf(stdout, "\n");
    exit(1);
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }



  // Shutdown Socket library
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Shutdown socket library");
  if(midnightcode_socket_shutdown(sockets) < 0) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    fprintf(stdout, "\n");
    exit(1);
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }



  exit(0);
}
