/****************************************************************************/
/* Summary	: Definitions for file I/O for BEDS								*/
/* Filename : file.h														*/
/* Author	: Robert Herlien (rah)											*/
/* Project	: BEDS (Benthic Event Detection System)							*/
/* Revision : 1.0															*/
/* Created	: 10/02/2012													*/
/****************************************************************************/
/* Modification History:													*/
/* 02oct2012 rah - created													*/
/* $Log$
 */
/****************************************************************************/

#ifndef INCfileh
#define INCfileh

#include <stdio.h>
#include <time.h>
#include <fatFs/ff.h>			/* FatFs definitions					*/

#define CFM_DRV			2

#define FMT_VERSION		0x02
#define FBUF_SIZE		16384
#define QUAT_SIZE		2				/* 2 or 3 bytes for one quaternion component?*/
#define MAX_DECIMATION	100
#define MAX_SPLIT		100

typedef enum
{
	FileHdrType, SecMarkerType, InertialDataType, PressureType, IndexType, SysRecType
} FileRcdType;

typedef struct							/****************************************/
{										/* FileHdr - File Header Record			*/
	Byte		recType;				/* Record Type = 0 for File Header		*/
	Byte		rsvd;					/* Reserved								*/
	Nat16		fmtVersion;				/* File Format version					*/
	Nat16		platformID;				/* Platform ID							*/
	Nat16		dataRate;				/* Rate primary data sample, samples/min*/
	time_t		startTime;				/* Time record started					*/
	Nat16		startMs;				/* Millisecond offset of start time		*/
	Nat16		rsvdWrd;				/* Unused, for alignment				*/
	Nat32		duration;				/* Duration of event/cycle, milliseconds*/
} FileHdr;								/****************************************/

typedef struct							/****************************************/
{										/* SecondMarker - New second in data stream*/
	Byte		recType;				/* Record Type							*/
	Byte		rsvd[3];				/* Reserved, for alignment				*/
	time_t		rcdTime;				/* Time									*/
} SecondMarker;							/****************************************/

typedef struct							/****************************************/
{										/* InertialRcd - Inertial Data Record	*/
	Byte		recType;				/* Record Type							*/
	Byte		accel[3][3];			/* XYZ accelerations, 3 bytes each		*/
	Byte		quat[4][QUAT_SIZE];		/* 4 quaternion values, 2 or 3 bytes each*/
} InertialRcd;							/****************************************/

typedef struct							/****************************************/
{										/* PressureRcd - Record for external pressure*/
	Byte		recType;				/* Record Type							*/
	Byte		rsvd;					/* Reserved								*/
	Nat16		pressure;				/* 16 bit A/D value of external pressure*/
} PressureRcd;							/****************************************/

typedef struct							/****************************************/
{										/* IdxRcd - Index Record				*/
	Byte		recType;				/* Record Type							*/
	Byte		rsvd;					/* Reserved								*/
	Nat16		startMs;				/* Millisecond offset of start time		*/
	time_t		startTime;				/* Start time of event or cycle			*/
	Nat32		duration;				/* Duration of event/cycle, milliseconds*/
	Int32		maxVal;					/* Max value of one accel component		*/
	char		fileName[32];			/* Name of file containing event or cycle*/
} IdxRcd;								/****************************************/

typedef struct							/****************************************/
{										/* SystemRcd - System parameters		*/
	Byte		recType;				/* Record Type							*/
	Byte		rsvd[3];				/* Reserved								*/
	time_t		rcdTime;				/* Time record was collected			*/
	Nat16		battVoltage;			/* 16 bit A/D value of battery voltage	*/
	Nat16		minModemBatt;			/* Minimum voltage to modem since last SystemRcd*/
	Nat16		extPressure;			/* 16 bit A/D value of external pressure*/
	Nat16		intPressure;			/* 16 bit A/D value of internal pressure*/
	Nat16		intTemp;				/* 16 bit A/D value of internal temperature*/
	Nat16		intHumidity;			/* 16 bit A/D value of internal humidity*/
} SystemRcd;							/****************************************/

typedef union							/****************************************/
{										/* Union of all Record types			*/
	FileHdr		hdr;					/* FileHdr								*/
	SecondMarker sec;					/* SecondMarker							*/
	InertialRcd inertial;				/* Inertial data record					*/
	PressureRcd press;					/* Pressure record						*/
	IdxRcd		index;					/* Index record							*/
	SystemRcd	system;					/* System data record					*/
} RcdUnion;								/****************************************/


typedef struct							/****************************************/
{										/* DataFileStruct - Holds inertial data pts*/
	char		*fileSuffix;			/* File suffix: "EVT" or "WAT"			*/
	char		*indexFile;				/* Name of index file					*/
	Nat32		fileIndex;				/* File number: BEDSnnnnn.{EVT,WAT}		*/
	Nat16		dataRate;				/* Data rate for file header			*/
	Nat32		indexRecOffset;			/* Offset of index rcd in index file	*/
	Nat32		bufBytes;				/* Number bytes used in buf				*/
	Int32		maxVal;					/* Maximum accel val for event			*/
	char		fileName[16];			/* Name of file containing event or cycle*/
	FIL			fil;					/* FatFs FIL struct (fp)				*/
	char		buf[FBUF_SIZE];			/* Data buffer							*/
} DataFileStruct;						/****************************************/


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

MBool	fileInit(void);
Errno	fileClose(Void);
void	fperr(int errnum, const char *s);
void	getDataFileName(long fileNum, char *suffix, char *nameBuf);
Errno	openNewDataFile(DataFileStruct *dfp);
Errno	writeDataFile(DataFileStruct *dfp, void *buf, Nat32 len);
Errno	closeDataFile(DataFileStruct *dfp);
Errno	writeClkDrift(Void);
Errno	writeSecondMarker(DataFileStruct *dfp, time_t secs);
char	*splitAndDecimate(char *fname, int nsplit, int ndec);

#endif /* INCfileh */
