/************************************************************************/
/* Copyright 2003 - 2016 MBARI											*/
/************************************************************************/
/* Summary	: Definitions for OASIS5 mooring controller using PIC32		*/
/* Filename : oasis.h													*/
/* Author	: Robert Herlien (rah)										*/
/* Project	: OASIS Mooring Replacement (OASIS3)						*/
/* Revision : 1.0														*/
/* Created	: 7/6/2016 from Oasis3/4 oasis.h							*/
/*																			*/
/* 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 - OASIS3 oasis.h created from OASIS oasis.h			*/
/* 6jul2016 rah  - OASIS5 oasis.h created from OASIS3 oasis.h			*/
/* $Log$
*/
/************************************************************************/

#ifndef INCoasish
#define INCoasish		1

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

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

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

  
/****************************************/
/* Miscellaneous typedefs				*/
/****************************************/

typedef long int		Parm_t;			/* User Parameters are 32 bit signed*/
typedef int				Port_t;			/* Serial port number				*/
typedef unsigned int	ParmMask_t;		/* Parm mask to user commands		*/
typedef Int16			CmdRtn;			/* Return from user commands		*/

typedef struct							/************************************/
{										/* Configuration of current serial port*/
	Port_t				serport;		/* Serial port number				*/
	Nat32				sermode;		/* Serial mode						*/
	int					lastChar;		/* Last char sent to port			*/
} SerDesc;	 							/************************************/


/****************************************/
/* Miscellaneous defines				*/
/****************************************/

#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 RTC					*/
  WakeConsoleChar						/* Character from console port	    */
} WhatWokeUs;							/************************************/


/****************************************/
/*		Time Constants					*/
/****************************************/

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


/****************************************/
/*		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							*/
/****************************************/

/* 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 FLAGS			 4				/* See Driver Flags Parm below		*/
#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 drvr_hdr Driver;			/* Driver struct type				*/

typedef struct							/************************************/
{										/* DrvDesc - Driver Descriptor		*/
	char	*dd_name;					/* Name of driver					*/
	void	(*dd_task)(Driver *);		/* Entry point for driver task		*/
	MBool	(*dd_serwake)(Driver *, MBool, Byte *);
	CmdRtn	(*drv_lastSample)(Driver *); /* Func to print last sample		*/
	const char **drv_parmNames;			/* Names of PARM0-PARM7				*/	
	Nat32	dd_parms[DRV_PARMS];		/* Driver parameters (see defns)	*/
} DrvDesc;								/************************************/

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)(Driver *);		/* Entry point for driver task		*/
	MBool	(*drv_serwake)(Driver *, MBool, Byte *);
	CmdRtn	(*drv_lastSample)(Driver *); /* Func to print last sample		*/
	const char **drv_parmNames;			/* Names of PARM0-PARM7				*/	
	Nat32	drv_parms[DRV_PARMS];		/* Driver parameters (see defns)	*/
	time_t	drv_wakeup;					/* Nominal time to next wakeup		*/
	Tid		drv_td;						/* Task descriptor for driver		*/
	DWord	drv_runflags;				/* See "Driver Temp Flags" below	*/
	Nat32	drv_lastlog;				/* Last record logged by this driver*/
	SerDesc	drv_serdesc;				/* Serial port parameters			*/
	Int32	drv_cnt;					/* Miscellaneous counter			*/
	Nat32	drv_usrparm;				/* Parameter passed directly to drvr*/
};										/************************************/

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

/*		 Driver Parm Flags, drv_parm[FLAGS]				*/
/* These flags modify driver behavior					*/
#define PWRPERM			1				/* Permanently power the instrument	*/
#define PWR_PERM		PWRPERM			/* Alias for PWRPERM				*/
#define CONSOLE_MODE	2				/* Wake on serial char				*/

/*		 Driver Run Flags, drv_runflags					*/
/* These are temporary flags, to indicate what the next	*/
/* instrument cycle should do (unless FLAG_PERM is set,	*/
/* and that flag appears to be deprecated				*/
#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 FLAG_PERM		0x8000L			/* Don't erase flags at task_exit	*/


/****************************************/
/* Function Declarations				*/
/****************************************/

MBool		rtosIsRunning(void);
void		setRtosRunMode(MBool runMode);
TaskHandle_t getOasisTask(void);

#endif	/* INCoasish */
