/* **********************************************************************
 * *	                                                                *
 * *              Antenna Simulator Control Routines                    *
 * *                                                                    *
 * **********************************************************************
 * File: libs/antenna/antsim_control.c
 *
 *                  COPYRIGHT (c) 1991, 1999, 2001  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 <stdio.h>
#include <string.h>

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

#include "antsim_global.h"

/* Subroutines and Functions
 *   antsim_set_status    - Set status flags for simulator
 *   iantsim_siggen       - Return value of signal generator attenuation
 *   iantsim_siggen_cmd   - Return requested value of signal generator attenuation
 *   iantsim_stat         - Return radar/antenna status
 *   iantsim_stat_mask    - Return radar/antenna status bits available
 *   iantsim_age          - Get time since last update
 *   iantsim_cmd          - Return radar/antenna commands
 *   iantsim_cmd_mask     - Return radar/antenna command bits available
 *   iantsim_pwbw         - Return current pulse width
 *   iantsim_pwbw_cmd     - Return current requested pulse width
 *   antsim_get_activity  - Return antenna activity numbers
 */

/* ------------------------------
 * Get the diagnostic buffer containing the last transmission starting with
 * a 80 hex.
 */
void  antsim_get_diagnostic
( UINT1* sbuffer_a, 
  SINT4 ibuf_size_a, 
  SINT4* ifill_size_a )
{
  *ifill_size_a = pantsim_c->info.iant_diag_size;
  if (*ifill_size_a>ibuf_size_a) *ifill_size_a = ibuf_size_a;

  memmove( sbuffer_a, (void *)pantsim_c->info.sant_diagnostic, *ifill_size_a );
}

/* ------------------------------
 * Get the NST bits send from IRIS.
 */
void antsim_get_nst
(UINT4 *imode_a
 )
{
  *imode_a = pantsim_c->info.icurrent_nst_bits;
}

/* ------------------------------
 * Get the diagnostic buffer containing the largest error transmission starting with
 * a character other than 80 or C0 hex.
 */
void  antsim_get_odiagnostic
( UINT1* sbuffer_a, 
  SINT4 ibuf_size_a, 
  SINT4* ifill_size_a )
{
  *ifill_size_a = pantsim_c->info.iother_diag_size;
  if (*ifill_size_a>ibuf_size_a) *ifill_size_a = ibuf_size_a;

  memmove( sbuffer_a, (void *)pantsim_c->info.sother_diagnostic, *ifill_size_a );

  pantsim_c->info.iother_diag_size = 0;
}

/* ------------------------------
 * Get the antenna process IDs
 * History:
 *     jmh   7 Feb 1994  Init
 */
void antsim_get_pids
(UINT4 *ircv_pid_a,  /* RCV process ID, zero means none.  Process may have stopped */
 UINT4 *ixmt_pid_a
 )
{
  *ircv_pid_a = pantsim_c->info.ircv_pid;
  *ixmt_pid_a = pantsim_c->info.ixmt_pid;
}

/* ------------------------------
 * Get the RST mode report send from IRIS.
 * History:
 *     jmh   29 Sep 1994  Init
 */
void antsim_get_rstmode
(UINT4 *imode_a
 )
{
  *imode_a = pantsim_c->info.icurrent_rst_mode;
}

/* ------------------------------
 * Get the RST mode request being sent to IRIS.
 * History:
 *     jmh   29 Sep 1994  Init
 */
void antsim_get_rstmode_cmd
(UINT4 *imode_a
 )
{
  *imode_a = pantsim_c->info.irequested_rst_mode;
}

/* ------------------------------
 * Set the simulator status mask
 * History:
 *   jmh   18 Oct 1991   Moved to separate file
 * Entry Parameters
 *   istatus_a - New status mask
 */
void antsim_set_status( UINT4* istatus_a )
{
  pantsim_c->info.isbits = *istatus_a;
}

/* ------------------------------
 * Set the RST mode request to send to IRIS.
 * History:
 *     jmh   29 Sep 1994  Init
 */
void antsim_set_rstmode
(UINT4 imode_a
 )
{
  pantsim_c->info.irequested_rst_mode = imode_a;
}

/* ------------------------------
 * Return the current signal generator desired value.
 */
SINT4 iantsim_siggen( void )
{
  return pantsim_c->info.isiggen_value;
}

/* ------------------------------
 * Return the current signal generator desired value.
 */
SINT4 iantsim_siggen_cmd( void )
{
  return pantsim_c->info.idsiggen_value;
}

/* ------------------------------
 * Return current radar/antenna status word
 * History
 *   jmh   14 Nov 1991   Added and with mask
 *   jmh   18 Oct 1991   Init
 */
UINT4 iantsim_stat( )
{
  return (pantsim_c->info.isbits & pantsim_c->info.is_mask);
}

/* ------------------------------
 * Return current radar/antenna status word mask of bits available
 * History:
 *   jmh   18 Oct 1991  Init
 */
UINT4 iantsim_stat_mask( )
{
  return pantsim_c->info.is_mask;
}

/* ------------------------------
 * Get time since the last update.
 * History:
 *     jmh   18 Oct 1991  Init
 */
UINT4 iantsim_age( )
{
  UINT4 ibtime;

  getbtime( &ibtime ) ;
  return( btimeGrace(ibtime - pantsim_c->info.ibtime) ) ;
}

/* ------------------------------
 * Return current radar/antenna command word
 * History
 *     jmh   14 Nov 1991   Added and with mask
 *     jmh   18 Oct 1991   Init
 */
UINT4 iantsim_cmd(void)
{
  return (pantsim_c->info.icbits & pantsim_c->info.ic_mask);
}

/* ------------------------------
 * Return current radar/antenna command word
 * History
 *     jmh   18 Oct 1991   Init
 */
UINT4 iantsim_cmd_mask(void)
{
  return pantsim_c->info.ic_mask;
}

/* ------------------------------
 * Return current pulse width
 * History
 *     jmh   24 Jan 1994   Init
 */
UINT4 iantsim_pwbw(void)
{
  return pantsim_c->info.iActualPwbw;
}

/* ------------------------------
 * Return current pulse width command
 * History
 *     jmh   24 Jan 1994   Init
 */
UINT4 iantsim_pwbw_cmd(void)
{
  return pantsim_c->info.iDesiredPwbw;
}

/* ------------------------------
 * Return whether input and output byte activity counts for the antenna
 * History:
 *     jmh   18 Oct 1991   init
 *
 * Exit parameters:
 *   incnt_a   - Number of characters received by antenna driver
 *   ioutcnt_a - Number of characters sent by antenna driver
 *   inerr_a   - Number of errors received by antenna driver
 *   iouterr_a - Number of errors sent by antenna driver
 */
void antsim_get_activity( SINT4* incnt_a, SINT4* ioutcnt_a, SINT4* inerr_a,
			  SINT4* iouterr_a )
{
  *incnt_a   = pantsim_c->info.iin_cnt;
  *ioutcnt_a = pantsim_c->info.iout_cnt;
  *inerr_a   = pantsim_c->info.iin_ercnt;
  *iouterr_a = pantsim_c->info.iout_ercnt;
}

/* ------------------------------
 * Determine whether the antenna format contains information appropriate for
 * a moving platform system
 * History:
 *     jmh   4 May 1992   Init
 */
UINT2 lantsim_moving_platform( )
{
  if (ANTFMT_RCV03 == pantsim_c->setup.istatus_format) return TRUE;

  return FALSE;
}

/* ------------------------------
 * Dual RCP System control and status calls
 */
void antsim_get_drcp( struct drcp_status *drcp_a )
{
  *drcp_a = pantsim_c->info.drcp ;
}

void antsim_set_drcp_mode( UINT2 iDrcpMode_a )
{
  pantsim_c->info.iDrcpModeReq = iDrcpMode_a ;
}
