/* *******************************************************************
 * *                                                                 *
 * *   Print Out Check Numbers From Iris Antenna Simulator Driver    *
 * *                                                                 *
 * *******************************************************************
 * File: utils/antenna/antsimcheck.C
 *
 *                 COPYRIGHT (c) 1994, 1999, 2002 BY
 *         SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
 *
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED.
 */
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sigtypes.h"
#include "user_lib.h"
#include "antenna_lib.h"
#include "antenna_int.h"

static void usage(void);
/*
 * ======================================================================
 */
int main( int argc_a, char** argv_a )
{
  MESSAGE istatus;
  SINT4 ii, in_cnt, iout_cnt, in_err, iout_err, index, idiag_size;
  SINT4 iBiteCount, iUnit, iBiteIn[BITE_MAX_UNITS], iBiteOut[BITE_MAX_UNITS];
  UINT4 iage, icurrent_mode, irequested_mode, ireported_nst, imode;
  UINT2 lmode_found;
  double fage;
  char *sptr;
  UINT1 ibuffer[100];
  UINT2 lauto ;
  UINT2 lpause ;
  UINT2 lskip_printf;

  /* ---------------------------------------------------------------- */
  if (!liris_operator(sig_username()))
    {
      printf( "Sorry, you must be an IRIS operator to run ANTSIMCHECK\n" );
      exit(EXIT_FAILURE);
    }

  setlocale( LC_ALL, "" );

  /* ==================================================
   * Parse command line options
   */
  lauto = 0 ;
  lpause = FALSE ;
  lskip_printf = FALSE ;
  lmode_found = FALSE ;

  for( ii=1 ; ii<argc_a ; ii++ )
    {
      int ilen = strlen( argv_a[ii] ) - 1 ;
      if( (argv_a[ii][0] == '-' || argv_a[ii][0] == '/') && (ilen > 0) )
	{
	  if( 0 == strncmp( &argv_a[ii][1], "auto", ilen ) ) {
	    lauto = 1 ;
	    continue;
	  }
	  if( 0 == strncmp( &argv_a[ii][1], "pause", ilen ) ) {
	    lpause = TRUE ;
	    continue;
	  }
	  if( 0 == strncmp( &argv_a[ii][1], "skip", ilen ) ) {
	    lskip_printf = TRUE ;
	    continue;
	  }
	  if( argv_a[ii][1] == 'm' ) {
	    imode = strtol( &(argv_a[ii][2]), &sptr, 10 );
	    lmode_found = TRUE ;
	    continue;
	  }
	  usage();
	}
      else
	{
	  usage();
	}
    }

  /* Power up antenna driver */
  istatus = iantsim_pwrp_opt( lauto );
  if (istatus != SS_NORMAL) { sig_signal( istatus ); exit(1);}

  antsim_get_activity( &in_cnt, &iout_cnt, &in_err, &iout_err );
  iage = iantsim_age();
  fage = ((double)iage)/1000.;
  iBiteCount = bitesim_get_activity( iBiteIn, iBiteOut, BITE_MAX_UNITS );

  if (lmode_found) antsim_set_rstmode( imode );

  antsim_get_rstmode( &icurrent_mode );
  antsim_get_rstmode_cmd( &irequested_mode );
  antsim_get_nst( &ireported_nst );
  antsim_get_diagnostic( ibuffer, 100, &idiag_size );

  if (!lskip_printf)
    {
      printf( "            Age: %6.2f seconds\n", fage );
      printf( "       In count: %d bytes\n", in_cnt );
      printf( "      Out count: %d bytes\n", iout_cnt );
      printf( " In error count: %d bytes\n", in_err );
      printf( "Out error count: %d bytes\n", iout_err );
      for (iUnit=0; iUnit<iBiteCount; iUnit++)
	{
	  printf( "      BITE %d in: %d, out: %d\n", iUnit+1, iBiteIn[iUnit], iBiteOut[iUnit] );
	}
      printf( "   Current mode: %d\n", icurrent_mode );
      printf( " Requested mode: %d\n", irequested_mode );
      printf( "   Reported NST: %#X\n", ireported_nst );
    }

  antsim_get_diagnostic( ibuffer, 100, &idiag_size );
  if (idiag_size)
    {
      printf("%d bytes in antenna diagnostic buffer:", idiag_size );
      for (index=0;index<idiag_size;index++)
	{
	  printf(" %2.2X", (int)(UINT2)ibuffer[index] );
	}
      printf("\n");
    }

  antsim_get_odiagnostic( ibuffer, 100, &idiag_size );
  if (idiag_size)
    {
      printf("%d bytes in other diagnostic buffer:", idiag_size );
      for (index=0;index<idiag_size;index++)
	{
	  printf(" %2.2X", (int)(UINT2)ibuffer[index] );
	}
      printf("\n");
    }

  /* Disconnect from antenna driver */
  istatus = antsim_close();
  if (istatus != SS_NORMAL) sig_signal( istatus );

  if (lpause)
    {
      printf("\n-- Hit carriage return to exit --"); 
      getchar();
    }

  exit(EXIT_SUCCESS) ;
}

/*
 * Print command line useage information 
 */
static void usage()
{
  fprintf( stderr, "usage: antsimcheck [-m#] [-auto] [-pause] [-skip]\n" );
  exit(1);
}
