/************************************************************************/
/* Copyright 2003 MBARI													*/
/************************************************************************/
/* Summary	: Definitions for OASIS3 mooring controller using Persistor CF2*/
/* Filename : oasis.h													*/
/* Author	: Robert Herlien (rah)										*/
/* Project	: OASIS Mooring Replacement (OASIS3)						*/
/* Revision : 0.1														*/
/* Created	: 10/14/2002												*/
/*																			*/
/* MBARI provides this documentation and code "as is", with no warranty,	*/
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium	*/
/* Research Institute to assist in its use, correction, modification, or	*/
/* enhancement. This information should not be published or distributed to	*/
/* third parties without specific written permission from MBARI.			*/
/*																			*/
/************************************************************************/
/* Modification History:												*/
/* 10oct2002 rah - created from OASIS oasis.h							*/
/* $Log: oasis.h,v $
/* Revision 1.1	 2011/10/25 22:39:22  bobh
/* Initial oasis4 revision
/*
/* Revision 1.6	 2011/02/15 18:41:58  bobh
/* Changes to support 8 or 16 bit download protocols
/*
/* Revision 1.5	 2010/10/25 16:42:50  bobh
/* Changes to support scripting
/*
 * Revision 1.4	 2009/11/19 19:05:58  bobh
 * Changes to support con to remote ports and fixes to file I/O due to testing
 *
 * Revision 1.3	 2009/04/27 22:49:43  bobh
 * First working version of remote serial ports
 *
 * Revision 1.2	 2009/04/17 18:05:04  bobh
 * Changes leading up to CDR
 *
 * Revision 1.1	 2009/03/25 20:34:41  bobh
 * Initial checkin
 *
 * Revision 1.3	 2003/09/02 21:14:48  bobh
 * Changes for configuration by oasis.cfg file
 *
 * Revision 1.2	 2003/08/26 00:24:07  headley
 * added BAD_PARM to denote invalid parameter
 *
 * Revision 1.1	 2003/08/19 18:57:35  headley
 * no message
*/
/************************************************************************/

#ifndef INCoasish
#define INCoasish		1

#include <mbariTypes.h>
#include <mbariConst.h>

#define NumberOf(arr)			((sizeof(arr) / sizeof(arr[0])))

#define Offset(ptr_type,field)	((Nat16)&(((ptr_type)0)->field))
#define OffsetOf(s_type,field)	Offset(s_type *,field)

#define MIN_PICO_VER	228		/* Must have >= 2.28 for XON/XOFF support*/

  
/****************************************/
/* Miscellaneous typedefs and defines	*/
/****************************************/

/* Error Codes */
typedef short int		Errno;			/* Error number return code			*/
typedef long int		Parm_t;			/* User Parameters are 32 bit signed*/

#ifndef ERROR					/* ERROR and OK defined in <const.h>		*/
#define ERROR			(-1)	/* ERROR return code, same as <const.h>		*/
#define OK				0		/* OK return code, same as <const.h>		*/
#endif
#define TMOUT_ERR		(-2)			/* Serial timeout					*/
#define MALLOC_ERR		(-3)			/* No buffer space from malloc		*/
#define NO_DATA			(-4)			/* No data from device				*/
#define BAD_DATA		(-5)			/* Bad data from device				*/
#define CKSUM_ERR		(-6)			/* Checksum failure					*/
#define SYNC_ERR		(-7)			/* Failure to sync to device		*/
#define NOT_AVAIL_ERR	(-8)			/* Device says not avail (eg GPS)	*/
#define USR_INTERRUPT	(-9)			/* User interrupted					*/
#define BAD_PARM		(-10)
#define WRITE_ERROR		(-11)			/* Error in writing to disk			*/
#define SEEK_ERROR		(-12)			/* Error in fseek on disk			*/

#define ABORT			(-2)			/* Rtn code to abort user interface */

#define NO_TIMEOUT		INT32_MAX		/* Infinite timeout parameter		*/
#define NO_SERIAL		(-1)			/*	NULL serial port				*/
#define XON				0x11			/* ASCII xon character				*/
#define XOFF			0x13			/* ASCII xoff character				*/

#define LOG_PWRUP		1				/* 1 in logStatus logs pwr up events*/
#define LOG_PWRDOWN		2				/* 2 in logStatus logs pwr down events*/
#define USR_BUFSIZE		256				/* User interface buffer size		*/
#define MAXLOGSIZE		65535			/* Max size of log record			*/
#define MAXADCPSIZE		2046			/* Max size for adcp record			*/

#define deblank(s)		while(isspace(*s)) ((s)++)
#define _to_upper(_c)	(isalpha(_c) ? (_c) & 0x5f : (_c))

/* Flags for xgets_tmout_flg() */
#define INCLUDE_TERMCHAR 1				/* Include termination char in buffer*/

typedef union							/************************************/
{										/* LongWord - Long <-> Word convert */
	Nat16		lw_word[2];				/* 2 Words							*/
	Nat32		lw_long;				/* Long								*/
} LongWord;								/************************************/

typedef union							/************************************/
{										/* WordByte - Word <-> 2Byte convert*/
	Byte		wb_byte[2];				/* 2 Bytes							*/
	Nat16		wb_word;				/* Word								*/
} WordByte;								/************************************/

typedef union							/************************************/
{										/* IntByte - Int <-> 2 Byte convert */
	Byte		ib_byte[2];				/* 2 Bytes							*/
	Int16		ib_int;					/* 16 bit Integer					*/
} IntByte;								/************************************/

typedef enum							/************************************/
{										/* Enum for wakeup from LPStop		*/
  WakeNone = 0,							/* Invalid, init'd to this before wake*/
  WakeTmout,							/* Timeout from PIT					*/
  WakePin,								/* External /WAKE pin				*/
  WakeConsoleChar,						/* Character from console port		*/
  WakeTPU2Char,							/* Character from ser port 2		*/
  WakePinOrConsoleChar					/* Used by int hndlr until we know which*/
} WhatWokeUs;							/************************************/


/****************************************/
/*		Time Data Types					*/
/****************************************/

#ifdef _TIME_T_DEFINED
typedef time_t	TimeOfDay;				/* Time of Day format				*/
#else
typedef long	TimeOfDay;				/* Time of Day format				*/
#endif
		
										/* seconds since 00:00:00 of 1/1/70 */

#define YEAR0			70				/* Start year for TimeOfDay			*/
#define SECS_PER_DAY	86400L			/* Seconds per day					*/

typedef struct							/************************************/
{										/* DateTime Struct					*/
	Byte		dt_hr;					/* Hour								*/
	Byte		dt_min;					/* Minute							*/
	Byte		dt_sec;					/* Second							*/
	Byte		dt_yr;					/* Year								*/
	Byte		dt_mo;					/* Month							*/
	Byte		dt_day;					/* Day								*/
} DateTime;								/************************************/


/****************************************/
/*		Error Types						*/
/****************************************/

/* The following error types are used general system error indicators.		*/
										/************************************/
#define RAM_ERR			0x01			/* RAM not initialized				*/
#define LOG_ERR			0x02			/* Bogus log memory					*/
#define CLOCK_ERR		0x04			/* Real-time clock failure			*/
#define INT_ERR			0x08			/* Received spurious interrupt		*/
/* note: RESTART_ERROR sent to errorlog whenever oasis is rebooted -rs*/
#define RESTART_ERR		0x10			/* Restarted with bad keepalive		*/
#define COMM_ERR		0x20			/* Restartd due to no comm for 24hrs*/
#define DISK_ERR		0x40			/* Disk I/O error					*/
#define ARGOS_ERR		0x80			/* Error uploading to ARGOS			*/
										/************************************/
#define ERR_LOG_BIT		0x8000			/* Error is logged with MSB set		*/
										/************************************/

/****************************************/
/*		Drivers							*/
/****************************************/

typedef Void	*Tid;					/* Task ID, used in task.h			*/
#define NULLTID ((Tid)0)

/* Driver Parameters			*/
#define INTERVAL		 0				/* Seconds between samples			*/
#define SER_PORT		 1				/* Serial port to use (ERROR if none)*/
#define SER_BAUD		 2				/* Serial port Baud rate			*/
#define SER_SETUP		 3				/* Serial Port control bits			*/
#define PWR_CTRL		 4				/* Power control bit(s) to use		*/
#define SAMPLE_TYPE		 5				/* Sample type field				*/
#define TOD_MASK		 6				/* 24 bit mask for hours to be off	*/
#define TIMEOUT			 7				/* Normal timeout in seconds		*/
#define PARM0			 8				/* Extra parameter					*/
#define PARM1			 9				/* Extra parameter					*/
#define PARM2			10				/* Extra parameter					*/
#define PARM3			11				/* Extra parameter					*/
#define PARM4			12				/* Extra parameter					*/
#define PARM5			13				/* Extra parameter					*/
#define PARM6			14				/* Extra parameter					*/
#define PARM7			15				/* Extra parameter					*/

#define DRV_PARMS		(PARM7 + 1)		/* Number driver parameters			*/

typedef struct							/************************************/
{										/* DrvDesc - Driver Descriptor		*/
	char		*dd_name;				/* Name of driver					*/
	Void		(*dd_task)();			/* Entry point for driver task		*/
	MBool		(*dd_serwake)();		/* Serial wakeup function			*/
	Nat32		dd_parms[DRV_PARMS];	/* Driver parameters (see defns)	*/
} DrvDesc;								/************************************/

typedef struct drvr_hdr Driver;			/* Driver struct type				*/

struct drvr_hdr							/************************************/
{										/* drvr_hdr - Driver header struct	*/
	Driver		*drv_next;				/* For linked lists					*/
	Driver		*drv_prev;				/* For linked lists					*/
	char		*drv_name;				/* Name of driver					*/
	Void		(*drv_task)();			/* Entry point for driver task		*/
	MBool		(*drv_serwake)();		/* Serial wakeup function			*/
	Nat32		drv_parms[DRV_PARMS];	/* Driver parameters (see defns)	*/
	TimeOfDay	drv_wakeup;				/* Nominal time to next wakeup		*/
	Tid			drv_td;					/* Task descriptor for driver		*/
	DWord		drv_flags;				/* See "Driver Flags" below			*/
	Int32		drv_cnt;				/* Miscellaneous counter			*/
	Nat32		drv_usrparm;			/* Parameter passed directly to drvr*/
};										/************************************/

#define DRV_NULL		((Driver *)0)	/* NULL driver pointer				*/

/* Driver Flags			*/
#define DO_SYNC			1L				/* Sync to incoming data			*/
#define DO_INIT			2L				/* Initialize driver				*/
#define DO_AUX			4L				/* Do aux function (cal, tape, etc) */
#define DO_ARGOS		8L				/* Do ARGOS sampling				*/
#define FLAG_PERM		0x8000L			/* Don't erase flags at task_exit	*/


#endif	/* INCoasish */
