/* *********************************************************************
 * *                                                                   *
 * *       SIGMET Antenna and BITE Library Public Definitions          *
 * *                                                                   *
 * *********************************************************************
 * File: include/antenna_lib.h
 *
 *  COPYRIGHT (c) 1991, 1992, 1994, 1996, 1997, 1999, 2001, 2002, 2003,
 *                          2004, 2005  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.
 */
#ifndef SIGMET_ANTENNA_LIB_H
#define SIGMET_ANTENNA_LIB_H 1

/* This collection of library routines provides a uniform interface to
 * the external radar and antenna.  A variety of serial interfaces and
 * protocols can be used.  All library source files are located in the
 * libs/antenna directory.  The statically linked antenna library
 * itself is libs/lib/libantenna.a.
 */
#ifdef __cplusplus
extern "C" {
#endif

/* Messages supported within the library.
 */
#define     BAD_CONTROL       1
#define ANT_BAD_CONTROL       (ANT_FACILITY + BAD_CONTROL)
#define     BAD_DEVICE        2
#define ANT_BAD_DEVICE        (ANT_FACILITY + BAD_DEVICE)
#define     BAD_STATUS        3
#define ANT_BAD_STATUS        (ANT_FACILITY + BAD_STATUS)
#define     BITE_FILE         4
#define ANT_BITE_FILE         (ANT_FACILITY + BITE_FILE)
#define     ERR_RCV           5
#define ANT_ERR_RCV           (ANT_FACILITY + ERR_RCV)
#define     ERR_XMT           6
#define ANT_ERR_XMT           (ANT_FACILITY + ERR_XMT)
#define     SETUP_CHANGED     7
#define ANT_SETUP_CHANGED     (ANT_FACILITY + SETUP_CHANGED)
#define     SETUP_FILE        8
#define ANT_SETUP_FILE        (ANT_FACILITY + SETUP_FILE)
#define     SETUP_SIZE        9
#define ANT_SETUP_SIZE        (ANT_FACILITY + SETUP_SIZE)
#define     CLIENT_CHANGED    10
#define ANT_CLIENT_CHANGED    (ANT_FACILITY + CLIENT_CHANGED)
#define     CLIENT_INVALID    11
#define ANT_CLIENT_INVALID    (ANT_FACILITY + CLIENT_INVALID)
#define     TWO_RCV           12
#define ANT_TWO_RCV           (ANT_FACILITY + TWO_RCV)
#define     TWO_SIM_RCV       13
#define ANT_TWO_SIM_RCV       (ANT_FACILITY + TWO_SIM_RCV)
#define     TWO_SIM_XMT       14
#define ANT_TWO_SIM_XMT       (ANT_FACILITY + TWO_SIM_XMT)
#define     TWO_XMT           15
#define ANT_TWO_XMT           (ANT_FACILITY + TWO_XMT)
#define     TWO_LOG           16
#define ANT_TWO_LOG           (ANT_FACILITY + TWO_LOG)
#define     SETSTRING         17
#define ANT_SETSTRING         (ANT_FACILITY + SETSTRING)
#define     SIM_STARTFIRST    18
#define ANT_SIM_STARTFIRST    (ANT_FACILITY + SIM_STARTFIRST)
#define     STARTFIRST        19
#define ANT_STARTFIRST        (ANT_FACILITY + STARTFIRST)
#define     ANTINTERNERR      20
#define ANT_ANTINTERNERR      (ANT_FACILITY + ANTINTERNERR)
#define     REMOTEPWRUP       21
#define ANT_REMOTEPWRUP       (ANT_FACILITY + REMOTEPWRUP)
#define     REMOTEPROTO       22
#define ANT_REMOTEPROTO       (ANT_FACILITY + REMOTEPROTO)
#define     NO_ANTENNA        23
#define ANT_NO_ANTENNA        (ANT_FACILITY + NO_ANTENNA)
#define     ERR_NETRCV        24
#define ANT_ERR_NETRCV        (ANT_FACILITY + ERR_NETRCV)
#define     REMOTEREJECT      25
#define ANT_REMOTEREJECT      (ANT_FACILITY + REMOTEREJECT)
#define     TWO_CHATTER       26
#define ANT_TWO_CHATTER       (ANT_FACILITY + TWO_CHATTER)
#define     REMOTE_CHATTER    27
#define ANT_REMOTE_CHATTER    (ANT_FACILITY + REMOTE_CHATTER)
#define     LIB_SHUTDOWN      28
#define ANT_LIB_SHUTDOWN      (ANT_FACILITY + LIB_SHUTDOWN)
#define     PED_ANGLE_SKIPPED 29
#define ANT_PED_ANGLE_SKIPPED (ANT_FACILITY + PED_ANGLE_SKIPPED)
#define     PED_ANGLE_OK      30
#define ANT_PED_ANGLE_OK      (ANT_FACILITY + PED_ANGLE_OK)
#define     INU_ANGLE_SKIPPED 31
#define ANT_INU_ANGLE_SKIPPED (ANT_FACILITY + INU_ANGLE_SKIPPED)
#define     INU_ANGLE_OK      32
#define ANT_INU_ANGLE_OK      (ANT_FACILITY + INU_ANGLE_OK)
#define     WRONG_CHATTER     33
#define ANT_WRONG_CHATTER     (ANT_FACILITY + WRONG_CHATTER)
#define     BAD_LOGFILE       34
#define ANT_BAD_LOGFILE       (ANT_FACILITY + BAD_LOGFILE)
#define     BAD_TIMESPAN      35
#define ANT_BAD_TIMESPAN      (ANT_FACILITY + BAD_TIMESPAN)
#define     BITE_STATUS_UNIT  36
#define ANT_BITE_STATUS_UNIT  (ANT_FACILITY + BITE_STATUS_UNIT)
#define     BITE_STATUS_SIZE  37
#define ANT_BITE_STATUS_SIZE  (ANT_FACILITY + BITE_STATUS_SIZE)
#define     BITE_STATUS_BUSY  38
#define ANT_BITE_STATUS_BUSY  (ANT_FACILITY + BITE_STATUS_BUSY)

#ifndef MESSAGE_ONLY

/* ***************************
 * *                         *
 * * ANTENNA_INFO Structures *
 * *    and Definitions      *
 * *                         *
 * ***************************
 */

/* Types of interfaces to the RCP hardware
 */
#define ANTIFACE_SERIAL    (0)
#define ANTIFACE_NONE      (1)
#define ANTIFACE_ANTEXPORT (2)
#define ANTIFACE_NETWORK   (3)

/* Types of serial formats
 */
#define ANTFMT_XMTVOID (0)	/* No antenna available*/
#define ANTFMT_XMT02   (2)	/* Enhanced RCP */
#define ANTFMT_XMT01   (3)	/* Original RCP */
#define ANTFMT_XMT04   (4)	/* (Same as XMT02, preserve for compatability) */
#define ANTFMT_XMTSA   (5)	/* Scientific Atlanta series 3860 controller */
#define ANTFMT_XMT05   (6)	/* Expanded XMT02 */
#define ANTFMT_XMTLIMITHI (6)

#define ANTFMT_RCVVOID (0)	/* No antenna available*/
#define ANTFMT_RCV03   (1)	/* Shipboard    */
#define ANTFMT_RCV02   (2)	/* Enhanced RCP */
#define ANTFMT_RCV01   (3)	/* Original RCP */
#define ANTFMT_RCV04   (4)	/* (Same as RCV02, preserve for compatability) */
#define ANTFMT_RCVSA   (5)	/* Scientific Atlanta series 3860 controller */
#define ANTFMT_RCV05   (6)	/* Expanded RCV02 */
#define ANTFMT_RCVLIMITHI (6)

#define ANTRCV_MAX_LENGTH (60)  /* Sizes of the largest receive and transmit */
#define ANTXMT_MAX_LENGTH (80)	/*   serial packets from/to an external RCP. */

/* Control flag definitions
 */
#define ACF_SERVO      (0x00010000) /* Turn on servo power */
#define ACF_RADIATE    (0x00020000) /* Turn on transmitter */
#define ACF_TRPOWER    (0x00040000) /* Turn on power to Tx/Rx rack */
#define ACF_IRIS       (0x00080000) /* IRIS Control mode */
#define ACF_RESET      (0x00100000) /* Reset pulse */
#define ACF_AUTO_RAD   (0x00200000) /* IRIS Will automatically control radiate */
#define ACF_NOISE      (0x00400000) /* Turn on noise source */
#define ACF_SIGGEN     (0x00800000) /* Turn on signal generator */
#define ACF_SIGCW      (0x01000000) /* Signal generator set to CW */
#define ACF_PWBW       (0x02000000) /* Pulse width controlled from antenna */
#define ACF_NST        (0x04000000) /* NST reports sent to RCP */
#define ACF_POLAR      (0x08000000) /* Polarization control available */
#define ACF_ANTENNA    (0x10000000) /* Antenna control available */
#define ACF_PW_UNSET   (0x20000000) /* Pulse width unchanged control available */

/* Status flag definitions
 */
#define ASF_LOCAL      (0x00000001) /* Antenna Local Mode */
#define ASF_TRLOCAL    (0x00000002) /* T/R Local Mode */
#define ASF_LOWAIRFLOW (0x00000004) /* Low Air Flow */
#define ASF_LOW_WGP    (0x00000008) /* Low waveguide pressure */
#define ASF_INTERLOCK  (0x00000010) /* Interlock open */
#define ASF_STANDBY    (0x00000020) /* Radar is in Standby mode */
#define ASF_MAGTFLT    (0x00000040) /* Magnatron/Klystron fault */
#define ASF_SHUTDOWN   (0x00000080) /* Controller has shutdown */
#define ASF_SIGFAULT   (0x00000100) /* Signal generator fault */
#define ASF_CALIB_AZ   (0x00000200) /* Azimuth encoder calibrated */
#define ASF_CALIB_EL   (0x00000400) /* Elevation encoder calibrated */

#define ASF_SERVO      (0x00010000) /* Servo is ON */
#define ASF_RADIATE    (0x00020000) /* Transmitter is ON */
#define ASF_TRPOWER    (0x00040000) /* TX/RX Power is ON */
#define ASF_SIGGEN     (0x00800000) /* Signal generator is ON */
#define ASF_SIGCW      (0x01000000) /* Signal generator is set to CW */
#define ASF_PWBW       (0x02000000) /* Pulse width/band width sense available */
#define ASF_POLAR      (0x08000000) /* Polarization sense available */

  /* Mask containing all the status bits which can be faulted
   */
#define ASFLAGS_FAULTABLE \
  (ASF_LOWAIRFLOW | ASF_LOW_WGP | ASF_INTERLOCK | ASF_MAGTFLT | ASF_SHUTDOWN)

/* ------------------------------
 * Dual RCP System Structures.  Use 4-byte entities here to increase
 * portability in the IRIS Server.
 */
struct drcp_unitinfo {		/* Information about each unit */
  UINT4 lKnown ;		/*   Information is known about this unit */
  UINT4 lDisabled ;		/*   This unit is disabled by a KILL switch */
  UINT4 lOkay ;			/*   This unit passes all internal checks */
  UINT4 lRelinquish ;		/*   This unit is offering to relinquish control */
  UINT4 lWouldUse ;		/*   This unit would be used if available */
  UINT4 iActivity ;		/*   Activity code for this unit */
} ;

#define DRCP_ACT_INACTIVE 0	/* Activity codes */
#define DRCP_ACT_WARMUP   1
#define DRCP_ACT_ACTIVE   2

struct drcp_status {		/* Status returned by each RCP */
  UINT4 lSetupDual ;		/*   This unit is setup as a dual system */
  UINT4 lRunningDual ;		/*   This unit is actually running as a dual system */
  UINT4 lUnitA ;		/*   This unit is the "A" unit */
  UINT4 lFlipAllowed ;		/*   This unit allows voluntary flipping of control */
  UINT4 lPreferredA ;		/*   Unit "A" is the preferred unit */
  UINT4 iDrcpMode ;		/*   Mutually agreed mode of the dual system */

  struct drcp_unitinfo sysA ;	/*   Information about the "A" system */
  struct drcp_unitinfo sysB ;	/*   Information about the "B" system */
} ;

#define DRCP_MODE_VOID    0	/* In a dual system, the host computer will */
#define DRCP_MODE_A       1	/*   select one of these modes to either    */
#define DRCP_MODE_B       2	/*   force the A or B channel active, or to */
#define DRCP_MODE_AUTO    3	/*   allow them to swap on failures.        */

/* ------------------------------
 * Realtime history structure for the basic pedestal info.
 */
#define ANTPEDX_SLOTS   200	/* Number of realtime slots \ Minimum 4-sec */
#define ANTPEDX_WIDTH    20	/* Minimum slot width in ms /   of history */
#define ANTPEDX_TERMS     6	/* Number of terms in struct rtpedx */
struct rtpedx {			/* Position vector contents */
  FLT8 faz, fel ;		/*   Angles in degrees (Earth relative) */
  FLT8 fazv, felv ;		/*   Velocities in degrees/second (Earth relative)*/
  FLT8 fPedAz ;			/*   Azimuth of the radar pedestal */
  FLT8 fPedEl ;			/*   Elevation of radar pedestal */
} ;
#define ANTPEDX_MODS		/* Data modulos for each term */ \
  RTVEC_MOD_UBIN, RTVEC_MOD_EBIN, RTVEC_MOD_NONE, RTVEC_MOD_NONE, \
  RTVEC_MOD_UBIN, RTVEC_MOD_EBIN, /* pedestal Azimuth and elevation */ 
  
/* Realtime history structure for the shipboard info.
 */
#define ANTSHIP_SLOTS   200	/* Number of realtime slots \ Minimum 4-sec */
#define ANTSHIP_WIDTH    20	/* Minimum slot width in ms /   of history */
#define ANTSHIP_TERMS    12	/* Number of terms in struct rtship */
struct rtship {			/* Position vector contents */
  FLT8 fpitch  , fpitchv ;	/*   Pitch angle and rate, degrees and deg/sec */
  FLT8 froll   , frollv ;	/*   Roll angle and rate */
  FLT8 fheading, fheadingv ;	/*   Heading angle and rate */

  FLT8 flatitude ;		/*   Position of the platform */
  FLT8 flongitude ;             /*   Degrees */
  FLT8 faltitude ;              /*   Altitude in meters */

  FLT8 fvel_east ;		/*   Motion of the platform, m/s */
  FLT8 fvel_north ;
  FLT8 fvel_up ;
} ;
#define ANTSHIP_MODS		/* Data modulos for each term */  \
  RTVEC_MOD_SBIN, RTVEC_MOD_NONE, /* Pitch angle and rate */      \
  RTVEC_MOD_SBIN, RTVEC_MOD_NONE, /* Roll angle and rate */       \
  RTVEC_MOD_UBIN, RTVEC_MOD_NONE, /* Heading angle and rate */    \
  RTVEC_MOD_SBIN, RTVEC_MOD_SBIN, /* Lat, Lon */                  \
  RTVEC_MOD_NONE, RTVEC_MOD_NONE, /* Altitude, East vel */        \
  RTVEC_MOD_NONE, RTVEC_MOD_NONE  /* North vel, Up vel */

  /* Structure holding information about what network RCP8 hosts we have
   * detected out there.
   */
struct ant_nethost_info
{
  UINT4 iAddress;   /* IP V4 address in network byte order */
  UINT4 iBTime;     /* Binary time we last received data from it */
};
/* ***********************************
 * *                                 *
 * * BITE Structures and Definitions *
 * *                                 *
 * ***********************************
 */
#define BITE_MAX_UNITS       16	/* Max number of BITE units */
#define BITE_MAX_LENGTH     128	/* Max length of a BITE packet */
#define BITE_NUM_FIELD_DEFS 256	/* Number of field definitions in library */
				/*   (must be a multiple of 32) */
#define BITE_MASK_SIZE (BITE_NUM_FIELD_DEFS/32)

#define BITE_BIT_MINVAL   1
#define BITE_BIT_LOW       1	/* Constants used to represent bits   */
#define BITE_BIT_HIGH      2	/*  that are LOW, HIGH, or DONT-CARE. */
#define BITE_BIT_NONE      3
#define BITE_BIT_MAXVAL   3

#define BITE_COLOR_MINVAL 0
#define BITE_COLOR_NONE    0	/* Constants representing optional colors   */
#define BITE_COLOR_RED     1	/*  that might be used for buttons or field */
#define BITE_COLOR_YELLOW  2	/*  name backgrounds. */
#define BITE_COLOR_GREEN   3
#define BITE_COLOR_MAXVAL 3

#define BITE_SF_CRITICAL  (0x01) /* Fault is critical */

/* ------------------------------
 * Status information structure
 */
struct bitex_status_info
{
  char low_text[10] ;		/* Text to represent LOW state */
  char high_text[10] ;		/* Text to represent HIGH state */

  UINT1 flag_state ;		/* Fault generation, set to BITE_BIT_XXX */
  UINT1 iFlags ;		/* Flag bits, see BITE_SF* parameters */
  UINT2 disable_field ;		/* Disabling field, Origin 0, (BITEX displays org 1) */
  UINT1 disable_state ;		/* Disable fault condition, set to BITE_BIT_XXX */
  char  pad_end[3] ;
} ;

/* ------------------------------
 * Q-BITE Status information structure
 */
struct qbite_status_info
{
  char sUnitsText[8] ;		/* Text to represent HIGH state */

  FLT4  fCalSlope ;
  FLT4  fCalOffset ;

  FLT4  fFaultThreshold ;
  FLT4  fCriticalThreshold ;
  UINT1 iFormatLeft ;		/* Characters to the left of . */
  UINT1 iFormatRight ;		/* Characters to the right of . */
  UINT1 lSigned ;		/* Value is signed */
#define QBITE_FAULT    (0x01)	/*   Fault enabled */
#define QBITE_CRITICAL (0x02)	/*   Critical enabled */
#define QBITE_LESSTHAN (0x04)	/*   Fault if less than threshold */
  UINT1 iFaultMask ;
  UINT2 disable_field ;		/* Disabling field, Origin 0, (BITEX displays org 1) */
  UINT1 disable_state ;		/* Disable fault condition, set to BITE_BIT_XXX */
  char  pad_end[1] ;
} ;

/* ------------------------------
 * Control information structure
 */
struct bitex_control_info
{
  char out_text[10] ;		/* Button text when OUT */
  char in_text[10] ;		/* Button text when IN */

  UINT1 lbutton_in_high ;	/* Button pressed --> HIGH control bit */

  UINT1 button_flags ;		/* How the button works */
#define BITE_BUTTON_PULSE  0x01	/*   Momentary pushbutton, else toggle */
#define BITE_BUTTON_DFLTIN 0x02	/*   Default state is IN (else OUT) */

  /* The following three variables allow the color of the field
   * name to be controlled by some other field.
   */
  UINT2 fcolor_fieldnum ;	/* Field doing the control */
  UINT1 fcolor_state ;		/* One of BITE_BIT_XXX */
  UINT1 fcolor_color ;		/* One of BITE_COLOR_XXX */

  /* The following two variables set the color of the control
   * pushbutton itself.
   */
  UINT1 button_in_color ;	/* One of BITE_COLOR_XXX */
  UINT1 button_out_color ;

  UINT1 liris_flag ;		/* Flag IRIS when button is activated */
  char  pad_end[3] ;
} ;

/* ------------------------------
 * Individual field definition structure
 */
union bitex_cs_union		/* Shared storage for control or status BITEs */
{
  struct bitex_status_info  sts ;
  struct bitex_control_info cnt ;
  struct qbite_status_info  Q_Sts ;
} ;

struct bitex_field_def
{
  UINT1 __lcontrol_unit ;	/* Used only by bitex_limits() */
  UINT1 __lQBite ;		/* Used only by bitex_limits() */
  UINT1 ldisabled ;		/* Field is disabled */

  UINT1 iunit ;			/* BITE Unit number, origin 0, (BITEX displays org 1) */
  UINT1 ibyte ;			/* Byte in packet, origin 0, (BITEX displays org 1) */
  UINT1 ibit ;			/* Bit in byte, origin 0, (#bytes for Q-bite) */

  char name_text[20] ;		/* Name of this field */

  char pad26x2[2] ;
  union bitex_cs_union cs ;
} ;

/* ------------------------------
 * Definition of each byte unit
 */
struct bitex_unit_info
{
  UINT1 lcontrol_unit ;		/* Control BITE unit (else status unit) */
  UINT1 iunit_length ;		/* Number of bytes in each BITE packet */
  UINT1 iunit_id ;		/* BITE ID character in packet */
  UINT1 lQBite ;		/*   Unit is Q-bite */
  char  sName[20] ;		/*   Nice name to use for this BITE unit */
} ;

/* ------------------------------
 * Top level definition structure
 */
struct bitex_setup
{
  char sVersion[8] ;		/* IRIS version when saved */

  UINT2 ifield_cnt ;		/* Number of fields (max BITE_NUM_FIELD_DEFS) */
  UINT1 iunit_cnt ;		/* Number of units, (max BITE_MAX_UNITS) */
  UINT1 lDisplayIO ;		/* Display the I/O summary on the main screen */
  struct bitex_unit_info  units[BITE_MAX_UNITS] ;
  struct bitex_field_def fields[BITE_NUM_FIELD_DEFS] ;
} ;

/* ------------------------------
 * Summary structure of present state of a field.  This is all of
 * the temporal information that BITEX needs to know so that, in
 * conjunction with the BITEX_FIELD_DEF structure, the field can
 * be fully displayed on the top maintenance panel. Some of the
 * structure elements are redundant if the BITEX_FIELD_DEF
 * structure is already known, but it is handy to have them
 * called out here as well.
 */
#define BITE_LABEL_TEXT_SIZE (19)
struct bite_field_state
{
  FLT4  fQBiteValue ;		/* Value of Q-bite number */
  UINT1 ldata ;			/* Bit value */
  UINT1 lCriticalFault ;        /* Critical fault is detected */
  UINT1 lfault ;		/* Non-disabled fault is detected */
  UINT1 button_color ;		/* Color of button (one of BITE_COLOR_XXX) */
  UINT1 lQBite ;		/* Field is Q-bite */
  UINT1 lcontrol ;		/* Control field (else status) */
  UINT1 lbutton_in ;		/* Control button is pressed IN */
  UINT1 liris_flag ;		/* IRIS flag request */
  UINT1 label_color ;		/* Label color (one of BITE_COLOR_XXX) */
  char  label_text[BITE_LABEL_TEXT_SIZE] ;	/* Label text */
} ;

/* ------------------------------
 * Dialog and Window state for BITEX2
 */
#define BITEX_MAX_PANEL_DIAGS 50

struct bitexWgt_StatContDef {
  UINT4 iPanelNum ;
  UINT4 iX ;
  UINT4 iY ;
} ;

#define BITEX_PANEL_TITLE_SIZE (32)
struct bitexWgt_Panel {
  UINT2 lAlloced ;
  UINT2 iPanelNum ;
  UINT4 iX ;
  UINT4 iY ;
  UINT4 iNewPanelNum ;
  UINT4 iNewPanelX ;
  UINT4 iNewPanelY ;
  UINT4 iNewPanelWidth ;
  UINT4 iNewPanelHeight ;
  char  sNewPanelTitle[BITEX_PANEL_TITLE_SIZE] ;
  char  sNewPanelGIFFile[20] ;
} ;

struct bitexWgt_setup {
  char sVersion[8] ;		/* IRIS version when saved */
  struct bitexWgt_StatContDef statCont[BITE_NUM_FIELD_DEFS] ;
  struct bitexWgt_Panel       panels  [BITEX_MAX_PANEL_DIAGS] ;
} ;

/* ------------------------------
 * Identification byte for special BITE packets
 */
#define BITE_INTERROGATE   (0x4d)
#define BITE_SET_VALUES    (0x46)
#define BITE_SIGGEN_OFF    (0x45) /* (Obsolete 8/2002) */
#define BITE_TAKE_SAMPLE   (0x44)
#define BITE_RESET         (0x43)

/* ------------------------------
 * Key names for the BITE field name.  Used to form a generic
 * interface to extract bits having a certain meaning regardless
 * of where they fall within the packets.
 */
				/* |----19 char max ---| */
#define BITE_KEY_HPOSREDUCED       "Reduced Hor Pos/Vel"
#define BITE_KEY_HPOSINVALID       "Invalid Hor Pos/Vel"
#define BITE_KEY_VPOSREDUCED       "Reduced Vrt Pos/Vel"
#define BITE_KEY_VPOSINVALID       "Invalid Vrt Pos/Vel"
#define BITE_KEY_ROLLREDUCED       "Reduced Roll/Pitch"
#define BITE_KEY_ROLLINVALID       "Invalid Roll/Pitch"
#define BITE_KEY_HEADREDUCED       "Reduced Heading"
#define BITE_KEY_HEADINVALID       "Invalid Heading"
#define BITE_KEY_INUNODATA         "No INU/MRU Data"

#define BITE_KEY_FULL_LIST { \
  BITE_KEY_HPOSREDUCED, BITE_KEY_HPOSINVALID, BITE_KEY_VPOSREDUCED,  \
  BITE_KEY_VPOSINVALID, BITE_KEY_ROLLREDUCED, BITE_KEY_ROLLINVALID,  \
  BITE_KEY_HEADREDUCED, BITE_KEY_HEADINVALID, BITE_KEY_INUNODATA     \
  }

/* ********************************************
 * *                                          *
 * * Function Prototypes for Library Routines *
 * *                                          *
 * ********************************************
 */

/* ---------------------------------------------------------------
 * ----------------- Open/Close/Powerup Routines -----------------
 * ---------------------------------------------------------------
 */
/* AntPowerUpMain() initializes internal shared memory for subsequent
 * calls to the library.  Any additional processes needed to
 * run the antenna are also created by this function.  The additional
 * process names are, for example, IRIS_ANT_XMT and IRIS_ANT_RCV.
 */
MESSAGE AntPowerUpMain( SINT4 iClientType, SINT4 iOptions_a ) ;

#define ANT_IAM_CLIENT    (0)   /* Normal client */
#define ANT_IAM_ANT_XMT0  (1)   /* Serial XMT process */
#define ANT_IAM_ANT_RCV0  (2)   /* Serial RCV process */
#define ANT_IAM_ANT_XMT1  (3)   /* Serial XMT process */
#define ANT_IAM_ANT_RCV1  (4)   /* Serial RCV process */
#define ANT_IAM_SIM_XMT   (5)   /* Antenna simulator XMT */
#define ANT_IAM_SIM_RCV   (6)   /* Antenna simulator RCV */
#define ANT_IAM_RCP       (7)   /* The external RCP */
#define ANT_IAM_RVP       (8)   /* The external RVP */
#define ANT_IAM_QANT      (9)   /* The qant program */
#define ANT_IAM_EXPORT   (10)	/* The AntExport program */
#define ANT_IAM_CHATTER  (11)	/* Client wanting to chat, not available via AntExport */
#define ANT_IAM_LOGGER   (12)	/* Antenna logging process */

#define ANT_OPTIONS_AUTO  (0x0001)   /* Automatic startup */

/* iant_pwrp() is the old powerup interface, and is equivalent to
 * AntPowerUpMain( ANT_IAM_CLIENT, 0 ).
 */
MESSAGE iant_pwrp( void ) ;

/* UnMap the shared memory, and disconnect this process from the
 * antenna library.  Use this when you no longer need the antenna
 * library.  For multi-threaded programs, call this after all threads
 * are closed, for example in the exit handler.
 */
MESSAGE ant_close( void ) ;

/* Get the process IDs for the Main/Aux serial receive and serial
 * transmit processes.  A returned ID of zero means that the process
 * has exited (or was never started).
 */
void ant_get_pids( UINT4 ircv_pids_a[2], UINT4 ixmt_pids_a[2], UINT4 *iLogPID_a ) ;

/* Return flag indicating that the antlib is shutting down.
 * This will allow application programs to exit, if they want.
 */
UINT1 lAntLibShutDown(void);

/* Return flag indicating that we are the controlling host.
 */
UINT1 lAntControllingHost(void);

/* Returns a pointer to the antenna setup structure.  Use with
 * Care.
 */
#ifdef SIGMET_SETUP_H
const struct ant_manual_setup *AntGetSetupPointer( void );
#endif  /* #ifdef SIGMET_SETUP_H */

const char *sAntClientType( SINT4 iClientType_a ) ;

/* -------------------------------------------------------------
 * -------------------- Serial I/O Activity --------------------
 * -------------------------------------------------------------
 */
/* These routines return the input and output serial byte activity
 * counts for channels #1 (main) and #2 (aux) of the antenna library.
 */
void ant_get_activity
( SINT4* incnt_a,		/* Number of characters received */
  SINT4* ioutcnt_a,		/* Number of characters sent */
  SINT4* inerr_a,		/* Number of bad characters received */
  SINT4* iouterr_a ) ;		/* Number of bad characters sent */

void  ant_get_activity2
( SINT4* incnt_a, SINT4* ioutcnt_a, SINT4* inerr_a, SINT4* iouterr_a ) ;

void  AntGetNetActivity
( struct ant_nethost_info *pNetHost_a,  /* Table to hold net host info */
  SINT4 iSize_a ) ;                     /* Size of the supplied table */

/* Returns the number of milliseconds since the last valid antenna
 * information was received by the library.  If several streams
 * of data are expected, returns the maximum.
 */
UINT4 iAntAgeSummary( void ) ;

/* Gets the ages in milliseconds since the last valid antenna
 * packet, angle report, and shipboard INU report.
 * Returns a mask of which ages are applicable to this system.
 */
#define ANT_EXPECT_PKT    (0x01)   /* Antenna status packet */
#define ANT_EXPECT_ANG    (0x02)   /* Antenna angle info*/
#define ANT_EXPECT_INU    (0x04)   /* INU info */
#define ANT_TIMEOUT_MS    (5000)   /* Timeout value for antenna info */
UINT1 AntGetAges
( UINT4 *pAgePkt_a,
  UINT4 *pAgeAng_a,
  UINT4 *pAgeInu_a );

/* BITE_GET_AGE() returns the number of milliseconds since a valid BITE
 * packet was received for each of the BITE units.  The unit count is
 * limited to BITE_MAX_UNITS.  BITE_GET_ACTIVITY() returns the actual
 * character counts for serial input and output from each BITE unit.
 * It returns the number of configured BITE units.
 */
void bite_get_age( SINT4 age_a[], SINT4 bite_count_a ) ;

SINT4 bite_get_activity
( SINT4 in_count_a[], SINT4 out_count_a[], SINT4 bite_count_a ) ;

/* Get the I/O diagnostic buffer containing the last received packet
 * that started with a 0x80.
 *   Entry parameters:
 *     ibuf_size_a - The size of the buffer in bytes
 *   Exit parameters:
 *     sbuffer_a    - Buffer with stuff in it
 *     ifill_size_a - Number of characters placed in the buffer
 */
void  ant_get_diagnostic
( UINT1* sbuffer_a, SINT4 ibuf_size_a, SINT4* ifill_size_a ) ;

/* Get the I/O diagnostic buffer containing the largest received error
 * packet starting with a character other than the known characters
 * 0x80, 0xAF, 0xB0, or 0xC0.
 */
void  ant_get_odiagnostic
( UINT1* sbuffer_a, SINT4 ibuf_size_a, SINT4* ifill_size_a ) ;

/* Get the diagnostic buffer containing the last received packet that
 * started with a 0xC0.
 */
void  bite_get_diagnostic
( UINT1* sbuffer_a, SINT4 ibuf_size_a, SINT4* ifill_size_a ) ;

/* -----------------------------------------------------------------
 * ----------------- Get AZ/EL Position & Velocity -----------------
 * -----------------------------------------------------------------
 */
/* Get AZ and EL positions and velocities, either for the current
 * time, or for the indicated BTIME in miliseconds.  The RTVEC
 * modeling package is used to extrapolate the current values based on
 * the antenna's recent history.
 */
void  ant_get_azel_pos( volatile BIN2* iaz_a , volatile BIN2* iel_a  ) ;
void  ant_get_azel_vel( volatile FLT4* fazv_a, volatile FLT4* felv_a ) ;

void  ant_get_azel_pos_btime
( volatile BIN2* iaz_a , volatile BIN2* iel_a , UINT4 iBtime_a ) ;

void  ant_get_azel_vel_btime
( volatile FLT4* fazv_a, volatile FLT4* felv_a, UINT4 iBtime_a ) ;

/* Get AZ and EL position error, i.e., the difference between the
 * actual and desired antenna positions.  If a position servo is not
 * being implemented then the position error is zero.
 */
void  ant_get_azel_err( volatile BIN2* iazerr_a, volatile BIN2* ielerr_a ) ;

/* This is the fundamental routine to extract the antenna parameters
 * for a given binary time.  It is called internally by all of the
 * other "get" routines.  Returns the age of the last antenna 
 * position packet in ms.
 */
UINT4 ant_get_rtpedx( struct rtpedx *rtpedx_a, UINT4 iBtime_a ) ;

#define ANT_MAXEXTRAPMS  1000	/* Max extrapolate time (ms) */

/* -----------------------------------------------------------------
 * ----------------- Set AZ/EL Position & Velocity -----------------
 * -----------------------------------------------------------------
 */
/* Sets up a position servo to move the antenna to the desired AZ/EL
 * position.  Argument is a 16-bit binary angle.
 */
void  ant_set_az_pos( BIN2 iangle_a ) ;
void  ant_set_el_pos( BIN2 iangle_a ) ;

/* Sets up a velocity servo to move the antenna at a specified speed
 * in AZ/EL.  Velocity argument is signed, with positive indicating
 * clockwise/upward movement.  Units are degrees/second.
 */
void  ant_set_az_vel( FLT4 fspeed_a ) ;
void  ant_set_el_vel( FLT4 fspeed_a ) ;

/* Returns the Min/Max AZ/EL velocities in degrees/second, and the
 * Min/Max EL positions in signed degrees.
 */
void  ant_get_az_limits( FLT4 fvel_limits_a[2] ) ;
void  ant_get_el_limits( FLT4 fvel_limits_a[2], FLT4 fpos_limits_a[2] ) ;

/* Returns the current AZ/EL axis control requests for the antenna.
 * The returned MODE will be one of ANTMODE_xxx.  The desired angle
 * will be filled in for ANTMODE_POS, and the desired velocity will be
 * returned for ANTMODE_VEL.
 */
void  ant_get_az_cmd
( volatile UINT2* imode_a, volatile BIN2* idaz_a, volatile FLT4* fdazv_a ) ;

void  ant_get_el_cmd
( volatile UINT2* imode_a, volatile BIN2* idel_a, volatile FLT4* fdelv_a ) ;

/* Servo modes for each axis
 */
#define ANTMODE_STOP    (0)	/* Stopped (tolerates user interventions) */
#define ANTMODE_VEL     (1)	/* Velocity Servo */
#define ANTMODE_POS     (2)	/* Position Servo */

/* Places the AZ/EL axis into "stop mode".  Stop mode stops the
 * antenna gracefully and without long term drift; yet still allows
 * the antenna to be moved in local mode.
 */
void  ant_stop_az( void ) ;
void  ant_stop_el( void ) ;

/* ------------------------------------------------------------
 * ------------------- Logging and History --------------------
 * ------------------------------------------------------------
 */
#define ANTLOGCOL_YMDS     5	/* Column where the time begins */
#define ANTLOGCOL_TEXT    28	/* Column where the log text begins */
#define ANTLOGCOL_MAX    180	/* Maximum length of log line entries */

#define ANTLOGMAXFILES    40	/* Maximum number of kept log files */

/* Write a text string to the common log file.  Prefix the current
 * time and other information to help identify who made this entry.
 * This routine may be called from client applications outside the
 * antenna driver to insert log messages where needed/desired.
 */
void antWriteLog( const char *sLogText_a ) ;

/* Routine to log an array of bytes in human readable format.  This
 * is typically used to make a record of I/O packets.
 */
void antWriteLogBinary
( const char *sPrefix_a, const UINT1 *str_a, SINT4 iLength_a ) ;

/* Return the time of the most recent entry in the log file.
 */
void antGetLatestLogTime( volatile struct ymds_time *pTime_a ) ;

/* Support routine to format a single line of text for a log file
 * entry.  Optionally return the current time as it was assigned to
 * this text.
 */
SINT4 antFormatLogEntry
( char sLogLine_a[ ANTLOGCOL_MAX ], const char *sLogText_a,
  volatile struct ymds_time *pLineTime_a ) ;

/* Prepare for extracting information from the antenna log files right
 * at this moment.  Subsequent analysis routines will only see those
 * entries that presently exist, even if some of the log files change
 * thereafter.  A fresh look is obtained by releasing this snapshot
 * and then acquiring another.
 */
struct antHistInfo ;	   /* Opaque outside of the antenna library */

struct antHistInfo *antHistPrepare( MESSAGE *iStatus_a ) ; 
MESSAGE antHistRelease( const struct antHistInfo *pInfo_a ) ;

/* Given a BITE unit ID and bit offset within the BITE records, fill
 * in a multi-state array representing the activity of that bit over a
 * specified time interval.  The ANTHISTBITE_xxx bits can be examined
 * directly, or the ANTHISTPLOT_xxx plotting macros can be applied to
 * each output slot to determine whether to draw a Hi/Lo data point,
 * or a Hi/Lo fine connecting line, or a fine vertical line.
 */
MESSAGE antHistGetBITE
( const struct antHistInfo *pInfo_a, UINT1 iID_a, SINT4 iByte_a, SINT4 iBit_a,
  const struct ymds_time *pBegin_a, const struct ymds_time *pEnd_a,
  UINT1 *pOut_a, SINT4 nOutPoints_a, SINT4 iExtrapMS_a ) ;

#define ANTHISTBITE_LOW    0x01	/* These bits will be set whenever either */
#define ANTHISTBITE_HIGH   0x02	/*   logic level was placed in this slot. */
#define ANTHISTBITE_LAST   0x04	/* Most recent level written to this slot */
#define ANTHISTBITE_XLEVEL 0x10	/* Logic level extrapolated forward */
#define ANTHISTBITE_XVALID 0x20	/* Extrapolated logic level is valid */

#define ANTHISTPLOT_LOPOINT(X) (((X) & ANTHISTBITE_LOW ) != 0)
#define ANTHISTPLOT_HIPOINT(X) (((X) & ANTHISTBITE_HIGH) != 0)
#define ANTHISTPLOT_LOLINE(X)  (((X) &							\
  (ANTHISTBITE_XVALID | ANTHISTBITE_XLEVEL | ANTHISTBITE_HIGH | ANTHISTBITE_LOW)) ==	\
  (ANTHISTBITE_XVALID                                                          ) )
#define ANTHISTPLOT_HILINE(X)  (((X) &							\
  (ANTHISTBITE_XVALID | ANTHISTBITE_XLEVEL | ANTHISTBITE_HIGH | ANTHISTBITE_LOW)) ==	\
  (ANTHISTBITE_XVALID | ANTHISTBITE_XLEVEL                                     ) )
#define ANTHISTPLOT_VLINE(X) (								\
  (((X) &										\
  (ANTHISTBITE_XVALID | ANTHISTBITE_XLEVEL | ANTHISTBITE_HIGH | ANTHISTBITE_LOW)) ==	\
  (ANTHISTBITE_XVALID                      | ANTHISTBITE_HIGH                  ) ) ||	\
  (((X) &										\
  (ANTHISTBITE_XVALID | ANTHISTBITE_XLEVEL | ANTHISTBITE_HIGH | ANTHISTBITE_LOW)) ==	\
  (ANTHISTBITE_XVALID | ANTHISTBITE_XLEVEL                    | ANTHISTBITE_LOW) ) )

/* Given a QBITE unit ID, byte offset and field length within the
 * QBITE records, and slope/offset conversion parameters, create an
 * output array representing the activity of that field over a given
 * time interval.
 */
struct antHistQBITE {		/* Return values for antHistGetQBITE() */
  FLT8 fMeanVal ;		/*   Mean value within this slot */
  FLT4 fLoVal, fHiVal ;		/*   Low/High values within this slot */
  SINT4 nValues ;		/*   Number of values deposited here */
} ;
MESSAGE antHistGetQBITE
( const struct antHistInfo *pInfo_a, UINT1 iID_a, SINT4 iByte_a, SINT4 nBytes_a,
  const struct ymds_time *pBegin_a, const struct ymds_time *pEnd_a,
  FLT4 fCalSlope_a, FLT4 fCalOffset_a, UINT1 lSigned_a,
  struct antHistQBITE *pOut_a, SINT4 nOutPoints_a ) ;

/* ------------------------------------------------------------
 * -------------------- Interfaces to IRIS --------------------
 * ------------------------------------------------------------
 */
/* Tells the antenna library whether it is being controlled exclusively
 * by IRIS, or whether it can be controlled by other APPs.
 */
void  ant_set_iris( UINT2 lon_a ) ;

/* ANT_GET_NST_CMD() gets the NST fault report information for all
 * four sites, along with their site codes.  ANT_SET_NST() sets the
 * NST report information for one site to be reported to the RCP.
 */
void  ant_get_nst_cmd
( char snst_sites_a[4][4], UINT4* lfaults_a ) ;

void  ant_set_nst( const char* snst_site_a, UINT2 lfault_a ) ;

/* Set/Get timeout settings for network status updates.  A site will
 * be considered faulted if its status has not been updated for longer
 * than the indicated times in miliseconds.
 */
void  ant_set_nst_timeout( UINT4 imilisec_a ) ;
UINT4 ant_get_nst_timeout( void ) ;

/* Gets the Radar Status menu mode that is being received from the
 * external RCP.  SRST_NAME_A is the name of a Radar Status menu
 * configuration; INAME_SIZE_A is the maximum length of the name;
 * IMODE_A is a number from 1 to 7 associated with the configuration.
 * A mode of 0 indicates that automatic mode switching is enabled.
 */
void ant_get_rstmode
( char* srst_name_a, SINT4 iname_size_a, UINT4 *imode_a ) ;

/* Gets the Radar Status menu mode that is being sent from IRIS to the
 * RCP.  Whenever the mode changes, IRIS reports the change back to
 * the RCP.  A mode value of 0 indicates an unrecognized
 * configuration.
 */
void ant_get_rstmode_cmd
( char* srst_name_a, SINT4 iname_size_a, UINT4 *imode_a ) ;

/* Sets the Radar Status menu mode to be sent to the RCP.
 */
void ant_set_rstmode
( const char* srst_name_a, UINT2 lwait_a ) ;

/* --------------------------------------------------------------
 * ------------------ Moving Platform Routines ------------------
 * --------------------------------------------------------------
 */
/* Returns TRUE if moving platform parameters are included in the data
 * being received from the external RCP.
 */
UINT2 lant_moving_platform( void ) ;

/* The INU position vector consists of longitude (degrees, -180 to
 * +180), latitude (degrees, -90 to +90) and altitude (meters above
 * sea level).
 */
void ant_get_inu_pos( FLT8 fposition_a[3] ) ;

/* The INU velocity vector consists of velocity East, North, and Up,
 * all in meters/second.
 */
void ant_get_inu_vel( FLT4 fvelocity_a[3] ) ;

/* The pedestal offset vector is the distance between the antenna
 * pedestal and the INU sensor itself.  Distances in meters are
 * antenna position forward, to port side, and above the INU.
 */
void ant_get_pedestal_off( FLT4 foffset_a[3] ) ;

/* The pedestal velocity vector has the same interpretation as the INU
 * velocity vector (see above), except that the rotational dynamics of
 * the ship are also taken into account.  As the ship rotates along
 * its three axes, there is a component of translational motion at the
 * antenna.
 */
void ant_get_pedestal_vel
( FLT4 fvelocity_a[3], const struct rtship *rtship_a ) ;

/* This is the fundamental routine to extract the shipboard parameters
 * for a given binary time.  It is called internally by all of the
 * other "get" routines.  Returns the age of the last ship report.
 */
UINT4 ant_get_rtship( struct rtship *rtship_a, UINT4 iBtime_a ) ;

/* Returns the fault bits reported from the INU.
 */
#define ANT_INU_INVHORZ 0x02	/* Invalid lat/long pos/vel */
#define ANT_INU_INVVERT 0x08	/* Invalid vertical pos/vel */
#define ANT_INU_INVROLL 0x20	/* Invalid roll/pitch angle/vel */
#define ANT_INU_INVHEAD 0x80	/* Invalid heading angle/vel */
UINT4 AntGetInuFaults( void );

/* ---------------------------------------------------------------
 * -------------------- Time Get/Set Routines --------------------
 * ---------------------------------------------------------------
 */
/* Returns the time of day information, along with supporting info,
 * from the most recent time record that has been received from the
 * RCP.
 */
void  ant_get_time
( struct ymds_time* time,	/* Time stamped into packet */
  char  sTZName[TZNAME_SIZE],   /* Assumed time zone of time in packet */
  UINT4* iage,			/* How long ago the packet was received (ms) */
  SINT4* idiff,			/* Time error (OS - RCP) (ms) */
  UINT2* lflag ) ;		/* NZ if a time packet has been received */

/* Set the system time clock.  A request is made to the antenna
 * transmit process to actually do the work.  This is necessary
 * because the calling process probably does not have the required
 * priveleges to actually set the system time.
 */
void ant_set_time( void ) ;

/* LANT_OKAY_TO_SET_TIME() returns TRUE if the system time could be
 * set from the time information that we currently have from the RCP.
 */
UINT2 lant_okay_to_set_time( void ) ;

/* LANT_SET_TIME_WAIT() returns TRUE if the system time currently has
 * a pending time set request.  PLASTCHANGE_A will hold the binary
 * time at which the last change was made, and is valid only if the
 * function returns FALSE.
 */
UINT4 lant_set_time_wait( SINT4 *pLastChange_a ) ;

/* ----------------------------------------------------------------
 * -------------------- Radar Tx/Rx Interfaces --------------------
 * ----------------------------------------------------------------
 */
/* ANT_SET_AUTO() sets the IRIS automatic radiate flag in the antenna
 * library.  IRIS will automatically request that the transmitter
 * radiate in time to start each task when call with TRUE arg.
 * Otherwise, radiate is not affected by IRIS tasks starting and
 * stopping.  ANT_SET_RADIATE() turns the transmitter On/Off right
 * away as requested.
 */
void  ant_set_auto( UINT2 lon_a ) ;

void  ant_set_radiate( UINT2 lon_a ) ;

/* ant_toggle_radiate implements the radiate toggle feature.  This
 * is an attempt to turn radiate on if it has faulted off.
 */
void  ant_toggle_radiate(SINT4 iWarmTimeMs_a);

/* Wait until radiate has been on the required time.
 */
void  ant_wait_for_radiate(SINT4 iWarmTimeMs_a);

/* ANT_SET_PWBW() tells the library to send a command to the RCP to set
 * the 2-bit transmit pulsewidth.  IANT_PWBW_CMD() returns that very
 * same request, and IANT_PWBW() returns the actual pulsewidth being
 * reported back from the RCP.
 *
 * void  ant_set_pwbw( UINT2 ipwbw_a ) ; Do not call from applications! 
 */
#define ANT_PWBW_MASK       (0x03)
#define ANT_PWBW_UNCHANGED  (0x04)

UINT4 iant_pwbw_cmd( void ) ;
UINT4 iant_pwbw( void ) ;

/* ANT_SET_POLAR() tells the library to send a command to the RCP to
 * set the transmit polarization.  Argument is one of the POL_xxx
 * parameters that are defined in dsp.h.  IANT_POLAR_CMD() returns
 * that very same request, and IANT_POLAR() returns the actual
 * polarization being reported back from the RCP.
 */
void  ant_set_polar( UINT2 iPolar_a ) ;
UINT2 iant_polar_cmd( void ) ;
UINT2 iant_polar( UINT1 *lOK_a ) ;

/* Turn On/Off the power supplies to the transmitter.
 */
void  ant_set_trpower( UINT2 lon_a ) ;

/* --------------------------------------------------------------
 * ------------------ Antenna Status & Control ------------------
 * --------------------------------------------------------------
 */
/* Reset the remote RCP from any shutdown or fault state that it may
 * be in right now.
 */
void  ant_set_reset( void ) ;

/* Turn On/Off the pedestal servo amplifiers
 */
void  ant_set_servo( UINT2 lon_a ) ;

/* Fills in the antenna status as a string that can be displayed.
 * Possible returns are "Shutdown", "Local", "IRIS", "Computer".  The
 * function's return value is one of DEV_xxx parameters from ingest.h.
 */
UINT4 AntGetStateString( char sName_a[16] ) ;

/* IANT_CMD() returns a summary of the control bits being sent to the
 * remote RCP.  The bit mask is a combination of the ACF_xxx macros
 * that are defined earlier in this file.  IANT_CMD_MASK() returns the
 * mask of which control bits are actually being used by the RCP.
 */
UINT4 iant_cmd( void ) ;
UINT4 iant_cmd_mask( void ) ;

/* IANT_STAT() returns a summary of the status bits being received by
 * the remote RCP.  The bit mask is a combination of the ASF_xxx
 * macros that are defined earlier in this file.  IANT_STAT_MASK()
 * returns the mask of which status bits are actually being used by
 * the RCP.  IANT_STAT_CRITICAL() returns a mask of status bits which
 * result in a critical system fault whenever any of them are set.
 */
UINT4 iant_stat( void ) ;
UINT4 iant_stat_mask( void ) ;
UINT4 iant_stat_critical( void ) ;

/* Returns the height of the ground at the antenna site, in meters.
 */
FLT4  fant_ground_height( void ) ;

/* --------------------------------------------------------------
 * ------------------ Signal Generator Control ------------------
 * --------------------------------------------------------------
 */
/* ANT_SET_SIGGEN() turns the RF signal generator (if any) On/Off.  A
 * CW waveform will be output if ANT_SET_SIGCW() is called with a TRUE
 * arg, else a pulse waveform will be produced.
 */
void ant_set_siggen( UINT2 lon_a ) ;
void ant_set_sigcw( UINT2 lon_a ) ;

/* The actual power output level is set by ANT_SET_SIGGEN_VALUE(),
 * which can optionally wait a predetermined settling time (from
 * SETUP) before returning.  IANT_SIGGEN_CMD() returns the level that
 * is being requested, and IANT_SIGGEN() returns the actual setting of
 * the signal generator as reported back from the RCP.
 */
void  ant_set_siggen_value
( const SINT4 ivalue_a, const UINT2 lwait_a ) ;

SINT4 iant_siggen_cmd( void ) ;
SINT4 iant_siggen( void ) ;

/* Gets the minimum and maximum signal generator values in dBm.
 */
void  ant_get_siggen_limits( SINT4 ilimits_a[2] ) ;

/* ANT_SET_NOISE() turns the noise souce (if any) On/Off.  The noise
 * source can also be toggled On-->Wait-->Off for a predetermined time
 * (from SETUP) using ANT_TOGGLE_NOISE().
 */
void  ant_set_noise( UINT2 lon_a ) ;
void  ant_toggle_noise( void ) ;

/* ---------------------------------------------------------------
 * -------------------- Dual Redundant System --------------------
 * ---------------------------------------------------------------
 */
/* The operating mode of a dual-redundant system is set to either "A",
 * "B", or "Auto" using ANT_SET_DRCP_MODE().  Arg is one of the macros
 * DRCP_MODE_xxx, whose meaning can be translated to text form using
 * DRCP_MODE_TEXT().
 */
void ant_set_drcp_mode( UINT2 iDrcpMode_a ) ;
const char *AntGetDrcpModeString( UINT2 iDrcpMode_a ) ;

/* The complete status of a dual-redundant system, as reported back
 * from whichever RCP(s) is/are actually up and running, is available
 * here.
 */
void ant_get_drcp( struct drcp_status *drcp_a ) ;

/* When a dual-redundant RCP system would be used by some application,
 * this is indicated by calling ANT_SET_DRCP_WOULDUSE() with a TRUE
 * arg.  Then, while the RCP is being used by a given APP, it can be
 * momentarily given up by calling ANT_SET_DRCP_RELINQUISH().  Both of
 * these routines define switchover barriers which guide the RCP's
 * decisions about when to change control between the "A" and "B"
 * systems.
 */
void ant_set_drcp_wouldUse( UINT2 lWouldUse_a ) ;
void ant_set_drcp_relinquish( UINT2 lRelinquish_a ) ;

/* --------------------------------------------------------------
 * -------------------- BITE Suport Routines --------------------
 * --------------------------------------------------------------
 */
/* Get a copy of the complete bitex definition structures from the
 * library.  Returns TRUE if a fresh copy has been written to BITEX_A,
 * i.e., if either the definitions have changed since the last
 * time that this routine was called, or LFORCE_GET_A was TRUE on
 * entry.  BITEX_A can be NULL if you are only interested in the
 * return value.
 */
UINT2 lbite_get_def
( struct bitex_setup *bitex_a, UINT2 lforce_get_a ) ;

/* BITE_GET_ONE_DEF() gets a copy of the fixed definition of a single
 * field of the BITE system.  BITE_GET_FIELD_STATE() returns all of
 * the temporal (changing with time) information about a given field.
 * See the structure definitions for a description of the information
 * passed by these routines.
 */
void bite_get_one_def
( struct bitex_field_def *definition_a,
  SINT4 ifield_a ) ;

void bite_get_field_state
( struct bite_field_state *fstate_a, SINT4 ifield_a ) ;

/* Check whether there is a BITE field with a particular (reserved)
 * text name, and if so, return the state of that bit.
 */
UINT2 bite_check_keyname( const char *keyname_a ) ;
#define BITE_CHKEY_UNDEF  0	/* Key is undefined */
#define BITE_CHKEY_FALSE  1	/* Key is FALSE */
#define BITE_CHKEY_TRUE   2	/* Key is TRUE  */

/* The summary status of all possible BITE faults is obtained using
 * BITE_GET_FLAG(), which returns the type of fault, and the field (if
 * any) that is causing it.
 */
void  bite_get_flag
( UINT4* flag_a, SINT4 *ifield_a ) ;
#define BITE_FLAG_OK       (0)
#define BITE_FLAG_FAULT    (1)
#define BITE_FLAG_CRITICAL (2)

/* Get the button presses flagged for IRIS.  The flags are cleared, so
 * we only see presses since the last call.
 */
void  bite_get_iris_flags
( UINT4 mask_a[BITE_MASK_SIZE] ) ;

/* Get the current mask of one bit for each defined field.  There is a
 * maximum of BITE_NUM_FIELD_DEFS bits returned.
 */
void  BiteGetMasks
( UINT4 iData_a[BITE_MASK_SIZE], 
  UINT4 iFaults_a[BITE_MASK_SIZE],
  UINT4 iCriticals_a[BITE_MASK_SIZE] ) ;

/* Sets the state of a BITE bit. 
 */
void bite_set_button( UINT2 lbutton_in_a, SINT4 ifield_a ) ;

/* Sends one of the following commands, either to all BITE units, or
 * to a specified BITE units:
 *
 *   BITE_INTERROGATE: Request a new BITE packet transmission from the RCP
 *   BITE_SET_VALUES:  Request that RCP send commands to external units
 *   BITE_TAKE_SAMPLE: Request that RCP sample data from external units
 *   BITE_RESET:       Reset the hardware associated with this BITE unit
 */
void bite_send_command( UINT1 icommand_a ) ;
void bite_send_unit_command( UINT1 icommand_a, UINT1 iUnitId_a ) ;

/* Request that a client send a BITE status packet.  The full packet
 * is provided, and some error checking is done.  Returns message on
 * error, and the packet is not sent.
 */
MESSAGE bite_send_client_status
( SINT4 iUnit_a,            /* Packet unit number, origin 0 */
  const UINT1 iPacket_a[],  /* Packet to send */
  SINT4 iPacketLength_a );  /* Number of bytes in packet */

/* ----------------------------------------------------------------
 * ------------------- Measured Angle Insertion -------------------
 * ----------------------------------------------------------------
 */
/* Function to process an antenna packet in any of the RCVxx formats,
 * once we know that it is formatted correctly.  The contents are
 * inserted into the antenna information database.
 */
void AntInsertPacket
( const UINT1 str_a[],		/* Array of data received */
  UINT4 iread_time_a,		/* Binary time of packet arrival */
  UINT4 iformat_a,		/* One of ANTFMT_RCVxxx */
  SINT4 iLength_a ) ;           /* Total length of the packet */

/* Returns TRUE if this client (the process that opened this version
 * of the antenna library) is allowed to insert measured antenna
 * angles.
 */
UINT1 lAntCanInsertAngles( void ) ;

/* Insert into the library a measured angle AZ/EL pair that was sampled
 * at a particular time.  Use the PosAndVel routine if the velocity is
 * known; otherwise just insert the positions and the library will take
 * care of computing velocities as needed.
 */
UINT1 AntInsertPosition
( BIN2 iEarthAzPos_a, BIN2 iEarthElPos_a,
  BIN2 iPedAzPos_a,   BIN2 iPedElPos_a,
  BIN2 iPedAzJump_a,  BIN2 iPedElJump_a,   /* Position jumps due to encoder cal */
  UINT4 iBtime_a ) ;

UINT1 AntInsertPosAndVel
( BIN2 iEarthAzPos_a, BIN2 iEarthElPos_a,
  BIN2 iPedAzPos_a, BIN2 iPedElPos_a,
  BIN2 iPedAzJump_a,  BIN2 iPedElJump_a,   /* Position jumps due to encoder cal */
  FLT4 fAzVel_a, FLT4 fElVel_a, UINT4 iBtime_a ) ;

UINT1 AntInsertRtship
( const struct rtship *pRtship_a, UINT4 iBtime_a ) ;

/* ----------------------------------------------------------------
 * ------------------- Antenna Motion Modelling -------------------
 * ----------------------------------------------------------------
 */
struct antAxisModel {		/* Simple antenna model state on one axis */
  BIN4  iPos32 ;		/*   Current position (32-bit binang) */
  SINT4 iVel32 ;		/*   Current velocity (32-bit binang/sec)*/
  UINT1 iAntMode ;		/*   Servo type, one of ANTMODE_xxx  */
  UINT1 lUseLimits ;		/*   Enforce lower/upper limits of travel */
  UINT1 lOverTheTop ;		/*   Don't cross -90, else take shortest path */
  UINT1 lHysteresisInner ;	/*   Flag for inner position hysteresis zone */
  char  pad12x2[2] ;
  BIN2  iRequestedPos ;		/*   Requested position (binang), or */
  SINT2 iRequestedVel ;		/*     requested velocity (binang/sec). */
  SINT2 iVelLimit ;		/*   Upper bound on velocity (binang/sec) */
  BIN2  iPosLimits[2] ;		/*   Optional lower/upper position limits */
} ;

/* Maximum acceleration in degrees/sec^2, and velocity servo gain term
 * having units of acceleration per degree/sec of velocity error.
 */
#define ANTMOD_MAXACCEL   20.0
#define ANTMOD_ACCELGAIN   8.0

/* Position feedback term is dimensionless because the feedback is
 * nonlinear.  However, making this number bigger makes the position
 * servos 'tighter'.
 */
#define ANTMOD_POSGAIN    30.0

/* Routine to update one axis of a modelled antenna, simulating the
 * passage of FTICK seconds since the last call.
 */
void antAxisModelUpdate
( volatile struct antAxisModel *pModel_a, FLT4 fTick_a ) ;

/* ----------------------------------------------------------------
 * ------------------- Miscellaneous Interfaces -------------------
 * ----------------------------------------------------------------
 */
/* Get/Put a chat-mode character.  The PUT routine will sleep until
 * there is room in the outgoing chat FIFO.  Since chat packets can be
 * sent whenever any data are ready, any sleeps that might happen will
 * always be very short.  A return value of -1 from the GET routine
 * indicates that no characters were available to be read.  The GET
 * routine will never sleep.
 */
SINT4 iant_get_chat( void ) ;
void  ant_put_chat( UINT1 ichar_a ) ;
void AntSetChatDestination
( UINT4 iDestination_a );  /* Destination in network byte order */

/* Write the packet to the network with the appropriate header for an
 * antenna packet.  The type string should be chosen from the list of
 * well known types.
 */
SINT4 AntWriteNetwork
( int iFD_a,
  const UINT1 *pData_a, 
  SINT4 iSize_a,
  const char *sType_a,
  UINT4 iDestination_a );  /* Destination address in network order */

#define ANT_PKT_TYPE_XMT01      "XMT01"	   /* Standard antenna XMTnn packets */
#define ANT_PKT_TYPE_XMT02      "XMT02"
#define ANT_PKT_TYPE_XMT05      "XMT05"
#define ANT_PKT_TYPE_RCV01      "RCV01"	   /* Standard antenna RCVnn packets */
#define ANT_PKT_TYPE_RCV02      "RCV02"
#define ANT_PKT_TYPE_RCV03      "RCV03"
#define ANT_PKT_TYPE_RCV05      "RCV05"
#define ANT_PKT_TYPE_XMTSA      "XMTSA"	   /* Scientific Atlanta controller */
#define ANT_PKT_TYPE_RCVSA      "RCVSA"
#define ANT_PKT_TYPE_XMTCHAT    "XCHAT"	   /* Chat mode packet */
#define ANT_PKT_TYPE_RCVCHAT    "RCHAT"
#define ANT_PKT_TYPE_RCVTIME    "RTIME"    /* Time packet */
#define ANT_PKT_TYPE_XMTBITEC   "XBITEC"   /* BITE control/status packet */
#define ANT_PKT_TYPE_RCVBITES   "RBITES"
#define ANT_PKT_TYPE_XMTBINTROG "XBINTROG" /* BITE 'Interrogate' packet */
#define ANT_PKT_TYPE_RCVBINTROG "RBINTROG"
#define ANT_PKT_TYPE_XMTBSAMPLE "XBSAMPLE" /* BITE 'Take Sample' packet */
#define ANT_PKT_TYPE_RCVBSAMPLE "RBSAMPLE"
#define ANT_PKT_TYPE_XMTBSETVAL "XBSETVAL" /* BITE 'Set Values' packet */
#define ANT_PKT_TYPE_RCVBSETVAL "RBSETVAL"
#define ANT_PKT_TYPE_XMTBRESET  "XBRESET"  /* BITE 'Reset' packet */
#define ANT_PKT_TYPE_RCVBRESET  "RBRESET"

/* Create a running subprocess that is connected to the calling parent
 * process via a single readable file descriptor.  The child process
 * ID is also returned so that it can be cleaned up later.  The parent
 * must put a kill of this PID in it's exit handler.  Returns -1 on error,
 * else a file descriptor
 */
struct ant_net_spec
{
  char *sAddress;    /* Network address, can be "0.0.0.0" for any */
  UINT2 iPort;
  UINT2 iFlags;
#define ANTNET_FLG_RCV  (0x0001)
#define ANTNET_FLG_XMT  (0x0002)
  char *sInterface;
};

int AntNetRcvOpen
( const struct ant_net_spec *pSpec_a, SINT4 *pPID_a ) ;

/* The antenna network receive process reads incoming network packets
 * and produces a virtual serial antenna/BITE stream on the requested
 * file descriptor.  It runs until getting an external request to
 * quit, or encountering a fatal internal error.
 */
MESSAGE AntNetRcvProcess
( const struct ant_net_spec *pSpec_a,
  int iFD_a,
  volatile const UINT1 *lQuitFlag_a,
  void (*AntLock_a)(int) ) ; /* Optional antenna library lock function */

/* ----------------------------------------------------------------
 * ------------------- Common Control Interface -------------------
 * ----------------------------------------------------------------
 */
/* Common interface function to handle all control requests from the
 * antenna library API calls.  Having this single interface allows a
 * history of all control requests to be maintained for exporting to
 * other systems.
 */
#define ANTSET_AZCMD      "AZCMD"
#define ANTSET_ELCMD      "ELCMD"
#define ANTSET_AUTORAD    "AUTORAD"
#define ANTSET_IRISMODE   "IRISMODE"
#define ANTSET_NOISEGEN   "NOISEGEN"
#define ANTSET_NOISETOG   "NOISETOG"
#define ANTSET_NSTOUT     "NSTOUT"
#define ANTSET_NSTFLT     "NSTFLT"
#define ANTSET_RSTMODE    "RSTMODE"
#define ANTSET_SERVO      "SERVO"
#define ANTSET_RADIATE    "RADIATE"
#define ANTSET_TOGGLE     "TOGGLE"
#define ANTSET_WAITRAD    "WAITRAD"
#define ANTSET_SIGGENCW   "SIGGENCW"
#define ANTSET_SIGGENOUT  "SIGGENOUT"
#define ANTSET_SIGGENVAL  "SIGGENVAL"
#define ANTSET_TRPOWER    "TRPOWER"
#define ANTSET_RESET      "RESET"
#define ANTSET_PWBW       "PWBW"
#define ANTSET_POLAR      "POLAR"
#define ANTSET_TIME       "TIME"
#define ANTSET_CHAT       "CHAT"
#define ANTSET_DRCPMODE   "DRCPMODE"
#define ANTSET_DRCPRLNQ   "DRCPRLNQ"
#define ANTSET_DRCPWUSE   "DRCPWUSE"
#define ANTSET_BITEBUTN   "BITEBUTN"
#define ANTSET_BITESCMD   "BITESCMD"
#define ANTSET_BITEUCMD   "BITEUCMD"
#define ANTSET_NUMCOMMANDS  (28)
#define ANTSETCMDLEN       (120)

/* NOTE: The number of commands should match the above list, so that
 * the history table will have a slot for each one.  Max command
 * length must be less than 128 so that the length can be coded into a
 * 7-bit packet value (MSB clear).
 */
void AntSet
( const char *pStr_a ) ;

/* Handy invocations, passing an opcode and numeric arg(s).
 */
void AntSetArg ( const char *pOpStr_a, FLT8 fValue_a ) ;
void AntSetArg2( const char *pOpStr_a, FLT8 fVal1_a, FLT8 fVal2_a ) ;

/* Return a string of packets containing the data and command updates
 * that have been entered into the library and have not yet been sent
 * to the specified client.
 * Return value is the length of the data string, which must
 * be free'd when you're done with it.  The contents of the string can
 * be exported directly to update a remote antenna receive process.
 */
SINT4 iAntGetUpdatePackets
( UINT4 iClientMask_a, /* Mask of which client we are */
  UINT1 iGet_a,        /* Type of get to do */
  char **pData_a );     /* Data returned here */

#define ANTGET_RCV_MASK (0x80000000)  /* Mask for the ant_rcv process */

#define ANTGET_ALL      0	/* Get all packets, regardless of time/purpose */
#define ANTGET_EXPORT   1	/* Get unsent packets needing to be exported */
#define ANTGET_RETURN   2	/* Get unsent packets to return to real antenna */
/* Return orientation angle to the sun.
 */
void sun_position 
(SINT4 iyear_a,          /* Current year */
 SINT4 iyday_a,          /* Current day in the year */
 SINT4 idsec_a,          /* Current second in the day */
 FLT8  fposition_a[2],   /* Longitude and latitude of your position in degrees */
 FLT8  *pfAzimuth_a,     /* Azimuth in radians */
 FLT8  *pfElevation_a ); /* Elevation in radians */

#ifdef __cplusplus
}
#endif

#endif /* #ifndef MESSAGE_ONLY */
#endif /* #ifndef SIGMET_ANTENNA_LIB_H */
