
/*
 *   THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE
 *   CHECKED IT OUT USING THE COMMAND CHECKOUT.
 *
 *    $Id: cont_trig.h,v 1.0  2010/06/10 18:00:00  JMS Exp $
 *
 *    Revision history:
 *     $Log: cont_trig.h,v $
 *     Revision 1.0  2010/06/10 18:00:00  JMS
 *     Initial revision
 *
 *
 */

/*
 * cont_trig.h: Definitions for the Cont_Trig Earthworm Module.
 */

/*******							*********/
/*	Redefinition Exclusion						*/
/*******							*********/
#ifndef __CONT_TRIG_H__
#define __CONT_TRIG_H__

#include <earthworm.h>
#include <time.h>	/* for time_t					*/
#include <transport.h>  /* Shared memory                                */
#include <trace_buf.h>	/* TracePacket					*/

/*******							*********/
/*	Constant Definitions						*/
/*******							*********/

#define CST_VERSION     "v2.0"  /* version of TYPE_CARLSTATRIG_SCNL	*/
#define CONT_VERSION     "v2.0"  /* version of TYPE_TRIGLIST_SCNL	*/
#define NSUBSTA		100	/* Number of stations allowed in subnet	*/
#define INITBUFSIZE	10240	/* Initial size of the trigger message	*/
				/*   buffer.				*/
  /*	Error Codes - Must coincide with carl_trig.desc			*/
#define	ERR_USAGE	1	/* Module started with improper params.	*/
#define	ERR_INIT	2	/* Module initialization failed.	*/
#define	ERR_CONFIG_OPEN	3	/* Error opening configuration file.	*/
#define	ERR_CONFIG_READ	4	/* Error reading configuration file.	*/
#define	ERR_EWH_READ	5	/* Error reading earthworm.h info.	*/
#define	ERR_STA_OPEN	6	/* Error opening station file.		*/
#define	ERR_STA_READ	7	/* Error reading station file.		*/
#define	ERR_SUB_OPEN	8	/* Error opening subnet file.		*/
#define	ERR_SUB_READ	9	/* Error reading subnet file.		*/
#define	ERR_MALLOC	10	/* Memory allocation failed.		*/

#define	ERR_MISSMSG	11	/* Message missed in transport ring.	*/
#define	ERR_NOTRACK	12	/* Message retreived, but tracking	*/
				/*   limit exceeded.			*/
#define	ERR_TOOBIG	13	/* Retreived message too large for	*/
				/*   buffer.				*/
#define	ERR_MISSLAPMSG	14	/* Some messages were overwritten.	*/
#define	ERR_MISSGAPMSG	15	/* There was a gap in message seq #'s.	*/
#define	ERR_UNKNOWN	16	/* Unknown function return code.	*/
#define	ERR_READ_MSG	17	/* Error reading station message.	*/
#define	ERR_PROD_MSG	18	/* Error producing a trigger message.	*/
#define ERR_ORDER	19	/* station trigger received out of order*/
#define ERR_FULLQUEUE	20	/* station trigger queue is full	*/
#define ERR_LATE        21	/* Network clock is too late		*/
#define ERR_THRDSTUCK   22      /* a thread stopped looping             */
#define ERR_SHORT_MSG   23      /* incomplete triglist message sent     */

  /*	Buffer Lengths							*/
#define	MAXCODELEN	8	/* Maximum length of a code.		*/
#define	MAXFILENAMELEN	80	/* Maximum length of a file name.	*/
#define	MAXLINELEN	2048	/* Maximum length of a line read from	*/
				/*   or written to a file.		*/
#define	MAXMESSAGELEN	160	/* Maximum length of a status or error	*/
				/*   message.				*/
#define	MAXTRACELEN	1024	/* Maximum length of a trace data	*/
				/*   buffer.				*/


/*******							*********/
/*	Macro Definitions						*/
/*******							*********/
#define	CT_FAILED( a ) ( 0 != (a) )	/* Generic function failure	*/
					/*   test.			*/

/*******							*********/
/*	Structure Definitions						*/
/*******							*********/

  /*	Cont_Trig earthworm Parameter Structure (read from *.d files)	*/
typedef struct _CONTPARAM
{
  char	debug;				/* Write out debug messages?	*/
					/*   ( 0 = No, 1 = Yes )	*/
  char	myModName[MAX_MOD_STR];	/* Name of this instance of the	*/
					/*   CarlTrig module -		*/
					/*   REQUIRED.			*/
  char	ring[MAX_RING_STR];	/* Name of ring to which	*/
					/*   triggers will be written -	*/
					/* REQUIRED.			*/
  char	staFile[MAXFILENAMELEN];	/* Name of file containing	*/
					/*   station information -	*/
					/* REQUIRED.			*/
  int	heartbeatInt;			/* Heartbeat Interval(seconds).	*/
  long	ringKey;			/* Key to output shared memory	*/
					/*   region.			*/
  char	OriginName[MAX_SUBNET_LEN];	/* Name of file containing	*/
					/*   station information -	*/
					/* REQUIRED.			*/
} CONTPARAM;

  /*	Information Retrieved from Earthworm.h				*/
typedef struct _CONTEWH
{
  unsigned char	myInstId;	/* Installation running this module.	*/
  unsigned char	myModId;	/* ID of this module.			*/
  unsigned char	typeTrigList;	/* TrigList message type.		*/
  unsigned char	typeError;	/* Error message type.			*/
  unsigned char typeHeartbeat;	/* Heartbeat message type.		*/
} CONTEWH;

  /* 	A station trigger structure					*/
typedef struct _STATRIG
{
  double	onTime;		/* When the station eta first went 	*/
				/*   above zero				*/
  double	offTime;	/* When station eta <= 0		*/
				/*   0.0 if station eta currently > 0	*/
  double	onEta;		/* Station eta when first above 0	*/
  long		sequence;	/* sequence number (from CarlStaTrig)	*/
} STATRIG;

  /*	Information about a particular station				*/
typedef struct _STATION
{
  char		compCode[TRACE2_CHAN_LEN];	/* Component code.	*/
  char		netCode[TRACE2_NET_LEN];	/* Network code.	*/
  char		staCode[TRACE2_STA_LEN];	/* Station code (name).	*/
  char   	locCode[TRACE2_LOC_LEN];	/* Location code	*/
  double	onTime;			/* Time this station triggered  */
  double	saveOnTime;		/* Time to be saved for message	*/
  double	onEta;			/* Eta value when this station	*/
					/*   first triggered		*/
  double	saveOnEta;		/* Eta value to be saved	*/
  int           listMe;                 /* Flag to mark stations that   */
                                        /*   are to be in triglist:     */
                                        /*     1: triggered station not */
                                        /*        in triggered subnet   */ 
                                        /* odd>1: triggered station in  */
                                        /*        triggered subnet(s)   */
                                        /*  even: untriggered station   */
                                        /*        in triggered subnet   */
  int		countDown;		/* station countdown timer	*/
  					/*   (seconds)			*/
  int		timeToLive;		/* How long a station trigger	*/
					/*   lasts after it's eta goes 	*/
					/*   below zero	(seconds)	*/
  int		nextIn;			/* index for the next incoming	*/
					/*   station trigger message	*/
  int		nextOut;		/* index for the next outgoing	*/
					/*   station trigger message	*/
  STATRIG*	triggers;		/* array of pending station	*/
					/*   trigger messages, oldest 	*/
					/*   first, youngest last	*/  
} STATION;

/* Information about a channel (such as time) for triglist */
typedef struct _CHANNEL
{
  char		compCode[TRACE2_CHAN_LEN];	/* Component code.	*/
  char		netCode[TRACE2_NET_LEN];	/* Network code.	*/
  char		staCode[TRACE2_STA_LEN];	/* Station code (name).	*/
  char		locCode[TRACE2_LOC_LEN];	/* Location code	*/
} CHANNEL;

/*	Information about a particular subnet				*/
typedef struct _SUBNET
{
  int	stations[NSUBSTA];	/* stations in this subnet, by station 	*/
				/*   array index 			*/
  int	nStas;			/* number of stations in this subnet	*/
  int	minToTrigger;		/* Minimum number of stations needed to	*/
				/*   cause this subnet to trigger.	*/
  int	Triggered;		/* subnet trigger status: 		*/
				/*   1: triggered			*/
				/*   0: not triggered			*/
  /* Changed by Eugene Lublinsky, 3/31/Y2K */
  /* subnetCode expanded from 4 symbols to 7; triggerable added 	*/
  /* subnetCode changed to length MAX_SUBNET_LEN Carol 3/21/01 		*/
  char  subnetCode[MAX_SUBNET_LEN]; /* char string to identify this subnet  */
  int   triggerable[NSUBSTA];   /* a flag that is 1 if this station can trigger the subnet; 0 if not */

} SUBNET;

  /*	Cont_Trig Network structure					*/
typedef struct _NETWORK
{
  CONTEWH	contEwh;		/* Pointer to the Earthworm parameters.	*/
  CONTPARAM	contParam;	/* Network parameters.			*/
  SHM_INFO	regionOut;      /* Output shared memory region info     */
  MSG_LOGO	hrtLogo;	/* Logo of outgoing heartbeat message.	*/
  MSG_LOGO	errLogo;	/* Logo of outgoing error message.	*/
  MSG_LOGO	trgLogo;	/* Logo of outgoing triglist message.	*/
  pid_t         MyPid;		/* For restart by startstop		*/
  STATION*	stations;	/* Array of stations in the network	*/
  SUBNET*       subnets;	/* Array of subnets in the network	*/
  CHANNEL*      channels;       /* non-seismic channels                 */
  mutex_t	stationMutex;	/* Lock for station trigger queues	*/
  int		nSta;		/* Number of stations in the net	*/
  int		nSub;		/* Number of subnets in the net		*/
  int		nSlots;		/* Number of pending station trigger	*/
  				/*   slots				*/
  int           nChan;          /* Number of extra channels             */
  int           numSub;         /* Max number of subnets that triggered */
                                /*   at the same time                   */
  long		PreEventTime;	/* Event start time adjustment		*/
				/*   (seconds).				*/
  long  	TrigDur;	/* base network trigger duration	*/
				/*   (seconds).				*/
  long  	subnetContrib;	/* Subnet contribution to trigger 	*/
				/*   duration (seconds)			*/
  long  	DefaultStationDur;	/* How long a station trigger	*/
  				/*   lasts if the trigger_off message	*/
  				/*   never arrives. (seconds)		*/
  long		MaxDur;		/* Maximum duration of network trigger	*/
  long		duration;	/* How long this triggered has lasted	*/
  time_t	onTime;		/* Time the network triggered		*/  
  long		latency;	/* Number of seconds Network runs 	*/
				/*   behind real-time.			*/
  long		eventID;	/* ID of the last event detected.	*/
  int           numSubAll;      /* If this many subnets trigger, put    */
                                /*   wildcards in the trigger message   */
  int           listSubnets;    /* Which stations to put in triglist msg*/
                                /*  0: list all triggered stations      */
                                /*  1: list all stations in triggered   */ 
                                /*     subnets                          */
                                /*  2: list all stations in triggered   */
                                /*     subnets plus any other triggered */
                                /*     stations (0+1)                   */
                                /*  3: list all stations in subnets that*/ 
                                /*     had any stations triggered       */
  int           compAsWild;     /* Flag to replace component name with  */
                                /*   the wildcard in trigger message    */
  int		terminate;	/* Flag to terminate the subnet thread	*/  
  char*		trigMsgBuf;	/* Buffer for the trigger message	*/
  size_t	trigMsgBufLen;	/* Size of the trigger message buffer	*/
} NETWORK;

/*	All the CarlStaTrig function prototypes				*/
int CompareSCNs( const void *s1, const void *s2 );
int InitializeParameters( NETWORK* contNet );
int InitializeStation( STATION* station );
int IsComment( char string[] );
int ProduceTriggerMessage( NETWORK* contNet);
int ReadConfig( char* filename, NETWORK* contNet );
int ReadEWH( NETWORK* contNet );
int ReadStations( NETWORK* contNet );
thr_ret SubnetThread( void* net );
void StatusReport( NETWORK* contNet, unsigned char type, short code, 
		   char* message );

#endif /*	__CONT_TRIG_H__				   	*/
