/* **********************************************************************
 * *                                                                    *
 * *           Support Routines for BITE Configuration Files            *
 * *                                                                    *
 * **********************************************************************
 * File: bitex_limits.c
 *
 *                  COPYRIGHT (c) 2001, 2002, 2003  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 <string.h>
#include <stdio.h>
#include <stdlib.h>

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

#define LIM_DEFINITIONS
#define LIM_KERNEL bitex_limits_kernel
#include "config_lib.h"

static MESSAGE LIM_KERNEL
( const char *sname_a,		/* Name of structure */
  void *item_a,			/* Pointer to structure */
  SINT4 ioper_a,		/* Operation to perform */
  const char *spath_a,		/* Full path name of fields */
  void *ioarg_a ) ;		/* Diagnostic messages, or NULL */

MESSAGE bitex_limits
( const char *sname_a,		/* Name of structure */
  volatile void *item_a,	/* Pointer to structure */
  SINT4 ioper_a,		/* Operation to perform */
  void *file_a )		/* Diagnostic messages, or NULL */
{
  return( lim_toplevel( sname_a, item_a, ioper_a, file_a, LIM_KERNEL ) ) ;
}

/* ======================================================================
 * Internal routine to recursively descend the BITE structures.
 */
static MESSAGE LIM_KERNEL
( const char *sname_a,		/* Name of structure */
  void *item_a,			/* Pointer to structure */
  SINT4 ioper_a,		/* Operation to perform */
  const char *spath_a,		/* Full path name of fields */
  void *ioarg_a )		/* Diagnostic messages, or NULL */
{
  char spath[PATHNAME_SIZE], *sItem ; struct lim_inLine **pLine ;
  SINT4 idim1=0, idim2=0 ; MESSAGE istatus = SS_NORMAL ;
  UINT1 lActualSizes = USEACTUALSIZES( ioper_a ) ;

  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  if( 0 == strcmp( "bitex_status_info", sname_a ) ) {
    struct bitex_status_info *item = item_a ;

    STRING   (        low_text, "" ) ;
    STRING   (       high_text, "" ) ;
    SCALAR   (      flag_state,   BITE_BIT_MINVAL,   BITE_BIT_MAXVAL,   BITE_BIT_NONE ) ;
    SCALAR   (          iFlags,              0x00,              0xFF,            0x00 ) ;
    SCALAR   (   disable_state,   BITE_BIT_MINVAL,   BITE_BIT_MAXVAL,   BITE_BIT_NONE ) ;
    SCALAR   (   disable_field,             0, BITE_NUM_FIELD_DEFS-1,               0 ) ;
  }
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  else if( 0 == strcmp( "bitex_control_info", sname_a ) ) {
    struct bitex_control_info *item = item_a ;

    STRING   (        out_text, "" ) ;
    STRING   (         in_text, "" ) ;

    SCALAR   ( lbutton_in_high,                 0,                 1,               0 ) ;
    SCALAR   (    button_flags,              0x00,              0xFF,            0x00 ) ;

    if( (!lActualSizes)    || item->fcolor_fieldnum ||
	item->fcolor_state || item->fcolor_color     ) {
      SCALAR( fcolor_fieldnum,             0, BITE_NUM_FIELD_DEFS-1,               0 ) ;
      SCALAR(    fcolor_state,   BITE_BIT_MINVAL,   BITE_BIT_MAXVAL,   BITE_BIT_NONE ) ;
      SCALAR(    fcolor_color, BITE_COLOR_MINVAL, BITE_COLOR_MAXVAL, BITE_COLOR_NONE ) ;
    }
    if( (!lActualSizes) || item->button_in_color || item->button_out_color ) {
      SCALAR( button_in_color, BITE_COLOR_MINVAL, BITE_COLOR_MAXVAL, BITE_COLOR_NONE ) ;
      SCALAR(button_out_color, BITE_COLOR_MINVAL, BITE_COLOR_MAXVAL, BITE_COLOR_NONE ) ;
    }
    if( (!lActualSizes) || item->liris_flag ) {
      SCALAR(      liris_flag,                 0,                 1,               0 ) ;
    }
  }
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  else if( 0 == strcmp( "bitex_field_def", sname_a ) ) {
    struct bitex_field_def *item = item_a ;

    SCALAR   (       ldisabled,                 0,                 1,               1 ) ;
    if( ! item->ldisabled ) {
      SCALAR   (         iunit,                 0,  BITE_MAX_UNITS-1,               0 ) ;
      SCALAR   (         ibyte,                 0, BITE_MAX_LENGTH-1,               0 ) ;
      SCALAR   (          ibit,                 0,                 6,               0 ) ;

      STRING   (      name_text, "" ) ;

      SCALAR( __lcontrol_unit, 0, 1, 0 ) ;
      SCALAR(        __lQBite, 0, 1, 0 ) ;
      if( item->__lQBite )
	{
	  SUBSTRUCTURE( qbite_status_info, cs.Q_Sts ) ;
	}
      else
	{
	  if( item->__lcontrol_unit ) { SUBSTRUCTURE( bitex_control_info, cs.cnt ) }
	  else                        { SUBSTRUCTURE(  bitex_status_info, cs.sts ) }
	}
    }
  }
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  else if( 0 == strcmp( "bitex_unit_info", sname_a ) ) {
    struct bitex_unit_info *item = item_a ;

    SCALAR(  lcontrol_unit,                0,                1,              0 ) ;
    SCALAR(   iunit_length,                3,  BITE_MAX_LENGTH,              0 ) ;
    SCALAR(       iunit_id,             0x00,             0xFF,           0x00 ) ;
    SCALAR(         lQBite,                0,                1,              0 ) ;
    STRING(          sName, "" ) ;
  }
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  else if( 0 == strcmp( "bitex_setup", sname_a ) ) {
    struct bitex_setup *item = item_a ;
    SINT4 iUnits, iFields ;

    SCALAR(       iunit_cnt,              1,      BITE_MAX_UNITS,             1 ) ;
    SCALAR(      ifield_cnt,              0, BITE_NUM_FIELD_DEFS,             1 ) ;
    SCALAR(      lDisplayIO,              0,                   1,             1 ) ;

    iUnits = lActualSizes ? item->iunit_cnt : BITE_MAX_UNITS ;
    iUnits = MAX( 0, MIN( BITE_MAX_UNITS, iUnits ) ) ;
    SUBSTRUCTURE_1D( bitex_unit_info,  units, iUnits  ) ;

    iFields = lActualSizes ? item->ifield_cnt : BITE_NUM_FIELD_DEFS ;
    iFields = MAX( 0, MIN( BITE_NUM_FIELD_DEFS, iFields ) ) ;
    SUBSTRUCTURE_1D( bitex_field_def, fields, iFields ) ;
  }
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#define MAX_XY 10000
  else if( 0 == strcmp( "bitexWgt_StatContDef", sname_a ) ) {
    struct bitexWgt_StatContDef *item = item_a ;

    if( (!lActualSizes) || item->iPanelNum || item->iX || item->iY ) {
      SCALAR(       iPanelNum,                 0, BITEX_MAX_PANEL_DIAGS,      0 ) ;
      SCALAR(              iX,                 0,            MAX_XY,          0 ) ;
      SCALAR(              iY,                 0,            MAX_XY,          0 ) ;
    }
  }
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  else if( 0 == strcmp( "bitexWgt_Panel", sname_a ) ) {
    struct bitexWgt_Panel *item = item_a ;
    SCALAR(        lAlloced,                 0,                 1,               0 ) ;
    if( (!lActualSizes) || item->lAlloced ) {
      SCALAR(       iPanelNum,              0, BITEX_MAX_PANEL_DIAGS,           0 ) ;
      SCALAR(              iX,              0,            MAX_XY,               0 ) ;
      SCALAR(              iY,              0,            MAX_XY,               0 ) ;
      SCALAR(    iNewPanelNum,              0, BITEX_MAX_PANEL_DIAGS,           0 ) ;
      SCALAR(      iNewPanelX,              0,            MAX_XY,               0 ) ;
      SCALAR(      iNewPanelY,              0,            MAX_XY,               0 ) ;
      SCALAR(  iNewPanelWidth,              0,            MAX_XY,             250 ) ;
      SCALAR( iNewPanelHeight,              0,            MAX_XY,             250 ) ;
      STRING(   sNewPanelTitle, "" ) ;
      STRING( sNewPanelGIFFile, "" ) ;
    }
  }
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  else if( 0 == strcmp( "bitexWgt_setup", sname_a ) ) {
    struct bitexWgt_setup *item = item_a ;

    SUBSTRUCTURE_1D( bitexWgt_StatContDef, statCont, BITE_NUM_FIELD_DEFS  ) ;
    SUBSTRUCTURE_1D(       bitexWgt_Panel,   panels, BITEX_MAX_PANEL_DIAGS ) ;
  }
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  else if( 0 == strcmp( "qbite_status_info", sname_a ) ) {
    struct qbite_status_info *item = item_a ;
    
    STRING(         sUnitsText, "" ) ;
    SCALAR(          fCalSlope,        -1000.0,        1000.0,       1.0 ) ;
    SCALAR(         fCalOffset,        -1000.0,        1000.0,       0.0 ) ;
    SCALAR(    fFaultThreshold,        -1000.0,        1000.0,       0.0 ) ;
    SCALAR( fCriticalThreshold,        -1000.0,        1000.0,       0.0 ) ;
    SCALAR(        iFormatLeft,              1,             9,         1 ) ;
    SCALAR(       iFormatRight,              0,             9,         0 ) ;
    SCALAR(            lSigned,              0,             1,         0 ) ;
    SCALAR(         iFaultMask,              0,             7,         0 ) ;
    SCALAR(      disable_state,BITE_BIT_MINVAL, BITE_BIT_MAXVAL, BITE_BIT_NONE ) ;
    SCALAR(      disable_field,              0, BITE_NUM_FIELD_DEFS-1, 0 ) ;
  }
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  else
    istatus = iMessageMod_va( CONFIG_LIM_BADSTRUCT, 1, sname_a ) ;

  return( istatus ) ;
}

/* ======================================================================
 * These routines load/save new values to the BITEX config file
 */
MESSAGE bitex_load_conf
( const char *sRemoteHostname_a,
  volatile struct bitex_setup *pBitexSetup_a )
{
  memset( (void *)pBitexSetup_a, 0, sizeof(struct bitex_setup) ) ;

  return( lim_loadFile( sRemoteHostname_a, "bitex.conf", "bitex_setup",
			pBitexSetup_a, bitex_limits ) ) ;
}
MESSAGE bitex_save_conf
( const char *sRemoteHostname_a, 
  volatile const struct bitex_setup *pBitexSetup_a )
{
  struct bitex_setup BitexSetup = *pBitexSetup_a ; SINT4 iField ;

  /* Fill in the version number, setup the temporary CNT/STS
   * discriminant, and write out the structure contents in ASCII.
   */
  strncpy( BitexSetup.sVersion, IRIS_VERSION, 8 ) ; BitexSetup.sVersion[7] = '\0' ;

  for( iField=0 ; iField < BitexSetup.ifield_cnt ; iField++ )
    {
      struct bitex_field_def *pField = &BitexSetup.fields[iField] ;
      pField->__lcontrol_unit = BitexSetup.units[ pField->iunit ].lcontrol_unit ;
      pField->__lQBite        = BitexSetup.units[ pField->iunit ].lQBite ;
    }
  return( lim_saveFile( sRemoteHostname_a, "bitex.conf", "bitex_setup",
			&BitexSetup, bitex_limits ) ) ;
}

/* ======================================================================
 * These routines load/save new values to the BITEX Widget config file
 */
MESSAGE bitexWgt_load_conf
( const char *sRemoteHostname_a,
  volatile struct bitexWgt_setup *pBitexWgtSetup_a )
{
  memset( (void *)pBitexWgtSetup_a, 0, sizeof(struct bitexWgt_setup) ) ;

  return( lim_loadFile( sRemoteHostname_a, "bitex_wgt.conf", "bitexWgt_setup",
			pBitexWgtSetup_a, bitex_limits ) ) ;
}
MESSAGE bitexWgt_save_conf
( const char *sRemoteHostname_a, 
  volatile const struct bitexWgt_setup *pBitexWgtSetup_a )
{
  struct bitexWgt_setup BitexWgtSetup = *pBitexWgtSetup_a ;

  strncpy( BitexWgtSetup.sVersion, IRIS_VERSION, 8 ) ; BitexWgtSetup.sVersion[7] = '\0' ;

  return( lim_saveFile( sRemoteHostname_a, "bitex_wgt.conf", "bitexWgt_setup",
			&BitexWgtSetup, bitex_limits ) ) ;
}
