// ************************************************************************************************
//
//	Copyright @ 2018 - Aethon-Technologies
//
//	File:		BaseTypes.h
//
//	Author:		A. Jones
//
//	Date:		15 Nov/2018
//
//	Brief:		
//
//	Notes:		
//
// ************************************************************************************************

#if !defined(BASETYPES__INCLUDED_)
#define BASETYPES__INCLUDED_

// **** I N C L U D E S ***************************************************************************
//
#ifdef WIN32
//	#include <stdafx.h>
	#include <stdint.h>
	#include <stdbool.h>
#endif

#ifdef _LINUX
	#include <stdbool.h>
#endif

#define UTM uint16_t

// *** D E F I N E S ******************************************************************************
//

// Endianness
//
#define	ENDIAN_BIG						0						// To match BIGENDIAN in WinSock2.h
#define	ENDIAN_MOTOROLA					(ENDIAN_BIG)
#define	ENDIAN_LIL						1
#define	ENDIAN_INTEL					(ENDIAN_INTEL)

#if defined(_WINDOWS) || defined(WINDOWS)
	#define	ENDIAN						(ENDIAN_LIL)
	#define	ENDIAN_NATIVE				(ENDIAN_LIL)
#endif

#if defined(_LINUX) || defined(LINUX)
	#define	ENDIAN						(ENDIAN_BIG)
	#define	ENDIAN_NATIVE				(ENDIAN_BIG)
#endif

#ifndef int8
	typedef	signed char					int8;
#endif

#ifndef uint8
	typedef unsigned char				uint8;
#endif

#ifndef int16
	typedef short						int16;
#endif

#ifndef uint16
	typedef unsigned short				uint16;
#endif

#ifndef int32
	typedef int							int32;
#endif

#ifndef uint32
	typedef unsigned int				uint32;
#endif

#ifndef int64
	#ifdef WIN32
		typedef __int64					int64;
	#endif
	#ifdef _LINUX
		typedef long long				int64;
	#endif
#endif

#ifndef uint64
	#ifdef WIN32
		typedef unsigned __int64		uint64;
	#endif
	#ifdef _LINUX
		typedef long long				uint64;
	#endif
#endif

#ifndef float32
	typedef float						float32;
#endif

#ifndef float64
	typedef double						float64;
#endif

#ifndef DWORD
	typedef unsigned long				DWORD;
#endif

#ifndef BOOL
	typedef int							BOOL;		// Ya Windoze uses an int
#endif

#ifndef NULL
	typedef ((void*)0)					NULL;
#endif

#ifndef MAX_PATH
	#define MAX_PATH					 260
#endif

#ifndef FALSE
	#define FALSE						 0
#endif

#ifndef TRUE
	#define TRUE						 1
#endif

#ifndef INT8_MIN
	#define INT8_MIN					-128
#endif

#ifndef INT8_MAX
	#define INT8_MAX					127
#endif

#ifndef UINT8_MIN
	#define UINT8_MIN					0
#endif

#ifndef UINT8_MAX
	#define UINT8_MAX					255
#endif

#ifndef INT16_MIN
	#define INT16_MIN					-32768
#endif

#ifndef INT16_MAX
	#define INT16_MAX					32767
#endif

#ifndef UINT16_MIN
	#define UINT16_MIN					0
#endif

#ifndef UINT16_MAX
	#define UINT16_MAX					65535
#endif

#ifndef INT32_MIN
	#define INT32_MIN					-2147483648
#endif

#ifndef INT32_MAX
	#define INT32_MAX					2147483647
#endif

#ifndef UINT32_MIN
	#define UINT32_MIN					0
#endif

#ifndef UINT32_MAX
	#define UINT32_MAX					4294967295
#endif

#endif // BASETYPES__INCLUDED_
	
// ************************************************************************************************
//
//	Copyright @ 2015 - Aethon-Technologies
//
//	File:		BUFF.h
//
//	Author:		A. Jones
//
//	Date:		28 Aug/2015
//
//	Brief:		Buffer Packing/Unpacking
//
//	Notes:		
//
// ************************************************************************************************

#ifndef __BUFF_H__
#define __BUFF_H__

// *** I N C L U D E S ****************************************************************************
//
//#include	"BaseTypes.h"


// *** P R O T O T Y P E S ************************************************************************
//
extern uint8*			BUFF_PutU8(uint8* pu8Buffer, uint8 u8Value, uint8 u8Endian);
extern uint8*			BUFF_PutU16(uint8* pu8Buffer, uint16 u16Value, uint8 u8Endian);
extern uint8*			BUFF_PutU32(uint8* pu8Buffer, uint32 u32Value, uint8 u8Endian);
extern uint8*			BUFF_PutU64(uint8* pu8Buffer, uint64 u64Value, uint8 u8Endian);
extern uint8*			BUFF_PutString8(uint8* pu8Buffer, const char* pS, uint8 u8Endian);
extern uint8*			BUFF_PutString16(uint8* pu8Buffer, const char* pS, uint8 u8Endian);
extern uint8*			BUFF_PutData(uint8* pu8Buffer, const uint8* pData, uint32 u32Len, uint8 u8Endian);
extern uint8*			BUFF_Concate(uint8* pu8Buffer, uint8* pData, uint32 u32Len, uint8 u8Endian);
extern uint8*			BUFF_PutFloat(uint8* pu8Buffer, float fValue, uint8 u8Endian);
extern uint8*			BUFF_PutDouble(uint8* pu8Buffer, double dValue, uint8 u8Endian);
extern uint8*			BUFF_PutVariable(uint8* pu8Buffer, uint8* pu8Variable, uint16 u16Len, uint8 u8Endian);

extern uint8*			BUFF_GetU8(uint8* pu8Buffer, uint8* pu8Value, uint16* pu16Len, uint8 u8Endian);
extern uint8*			BUFF_GetU16(uint8* pu8Buffer, uint16* pu16Value, uint16* pu16Len, uint8 u8Endian);
extern uint8*			BUFF_GetU32(uint8* pu8Buffer, uint32* pu32Value, uint16* pu16Len, uint8 u8Endian);
extern uint8*			BUFF_GetU64(uint8* pu8Buffer, uint64* pu64Value, uint16* pu16Len, uint8 u8Endian);
extern uint8*			BUFF_GetString8(uint8* pu8Buffer, char* pD, uint16* pu16Len, uint8 u8Endian);
extern uint8*			BUFF_GetString16(uint8* pu8Buffer, char* pD, uint16* pu16Len, uint8 u8Endian);
extern uint8*			BUFF_GetData(uint8* pu8Buffer, uint8* pData, uint32 u32Len, uint16* pu16Len, uint8 u8Endian);
extern uint8*			BUFF_GetFloat(uint8* pu8Buffer, float* pfValue, uint16* pu16Len, uint8 u8Endian);
extern uint8*			BUFF_GetDouble(uint8* pu8Buffer, double* pdValue, uint16* pu16Len, uint8 u8Endian);
extern uint8*			BUFF_GetVariable(uint8* pu8Buffer, uint8* pu8Variable, uint16 u16Len, uint16* pu16Len, uint8 u8Endian);

#endif // __BUFF_H__

// BUFF.h
// ************************************************************************************************
//
//	Copyright @ 2015 - Aethon-Technologies
//
//	File:		CRC.h
//
//	Author:		A. Jones
//
//	Date:		28 Aug/2015
//
//	Brief:		
//
//	Notes:		
//
// ************************************************************************************************

#ifndef __CRC_H__
#define __CRC_H__


// *** I N C L U D E S ****************************************************************************
//
//#include "BaseTypes.h"


// *** E X T E R N S ******************************************************************************
//
extern const uint8	crc8tab[256];
extern const uint16	crc16tab[256];
extern const uint32	crc32tab[256];


// *** M A C R O S ********************************************************************************
//
#define CRC8(CRC,BYT)	( crc8tab[(CRC) ^ (BYT)] )
#define CRC16(CRC,BYT)	( (uint16)((CRC) << 8U) ^ crc16tab[(((CRC) >> 8U) ^ (BYT)) & 0xFFU] )
#define CRC32(CRC,BYT)	( crc32tab[((uint32)(CRC) ^ (BYT)) & 0xFFU] ^ (((CRC) >> 8U) & 0x00FFFFFFU) )


// *** P R O T O T Y P E S ************************************************************************
//
extern uint8	CRC8Buffer(uint8 u8CRC, uint8* pu8Data, uint32 u32Len);
extern uint16	CRC16Buffer(uint16 u16CRC, uint8* pu8Data, uint32 u32Len);
extern uint32	CRC32Buffer(uint32 u32CRC, uint8* pu8Data, uint32 u32Len);

#endif // __CRC_H__

// CRC.h
// ************************************************************************************************
//
//	Copyright @ 2018 - Aethon-Technologies
//
//	File:		Gimbal.h
//
//	Author:		A. Jones
//
//	Date:		12 Nov/2018
//
//	Brief:		Gimbal API Definitions
//
//	Notes:		
//
// ************************************************************************************************

#ifndef _M_GIMAPI_H_
#define _M_GIMAPI_H_

// *** I N C L U D E S ****************************************************************************

//#include "BaseTypes.h"
//#include "UTM.h"


// *** D E F I N E S ******************************************************************************

// ************************************************************************************************
// ***                                      Gimbal                                              ***
// ************************************************************************************************

// GIM Status Validity <Tokenize>
//
#define GIM_VALID_MODE							0x0001
#define GIM_VALID_FLAGS							0x0002
#define GIM_VALID_STATUS						0x0004
#define GIM_VALID_PAN							0x0008
#define GIM_VALID_TILT							0x0010
#define GIM_VALID_ROLL							0x0020
#define GIM_VALID_UNDULATION					0x0040
#define GIM_VALID_GYRO_RATES					0x0080
#define GIM_VALID_ACCELERATIONS					0x0100
#define GIM_VALID_CLUSTER						0x0200
//
// </Tokenize>


// GIM Type <Tokenize>
//
#define GIM_TYPE_INVALID						0
#define GIM_TYPE_GENERIC						1
#define GIM_TYPE_SIM							2
#define GIM_TYPE_TRILLIUM						3
#define GIM_TYPE_TRAKKA							4
#define GIM_TYPE_CINEFLEX						5
//
// </Tokenize>


// GIM Status <Tokenize>
//
#define	GIM_STATUS_COMM_GOOD					0x0001				// Comms Good
#define GIM_STATUS_SUBCLASS_MASK				0xFF00				// These Flags are Reserved for Specific Gimbals
//
// </Tokenize>


// Gimbal Steering Modes <Tokenize>
//
#define GIM_MODE_UNKNOWN						0					// Unknown Mode
#define GIM_MODE_INIT							1					// Startup
#define GIM_MODE_BIT							2					// Built in Test
#define GIM_MODE_RATE							3					// Rate Mode
#define GIM_MODE_POS							4					// Position Mode
#define GIM_MODE_STOW							5					// Stow
#define GIM_MODE_GEO							6					// Geo Pointing Mode
#define GIM_MODE_FAULT							7					// Fault Condition
#define GIM_MODE_CUSTOM							8					// An Extended Mode specific to type of Gimbal
//
// </Tokenize>


// GIM Flags <Tokenize>
//
#define GIM_FLAG_TBD							0x0000
//
// </Tokenize>


// GIM Axis <Tokenize>
//
#define	AZ										0
#define	EL										1
#define	RL										2
//
// </Tokenize>


// ************************************************************************************************
// ***                                       GEO                                                ***
// ************************************************************************************************

// <Tokenize> Geo Validity
//
#define GEO_VALID_FLAGS							0x0001	// GEO_FLAG_
#define GEO_VALID_STATUS						0x0002	// GEO_STATUS_
#define GEO_VALID_RANGE							0x0004	// Min/Max Range is Valid
#define GEO_VALID_NORMAL						0x0008	// Min/Max Angle is Valid
#define GEO_VALID_TARGET						0x0010	// Lat/Lng/Alt Target(s) Valid
#define GEO_VALID_CORNERS						0x0020	// Corners Valid (Status)
//
// </Tokenize>

// <Tokenize> Geo Flags
//
#define GEO_FLAG_TBD							0x0001
//
// </Tokenize>


// <Tokenize> Geo Status
//
#define GEO_STATUS_INS_ALIGNING					0x0001
#define GEO_STATUS_INS_ALIGNED					0x0002
#define GEO_STATUS_INS_FAIL						0x0004
//
// </Tokenize>


// <Tokenize> Geo Event
//
#define GEO_EVENT_INVALID						0x00		// Invalid
#define GEO_EVENT_STARE							0x01		// Settled on Target
#define GEO_EVENT_CAPTURE						0x02		// Image Captured
#define GEO_EVENT_CLUSTER_COMPLETE				0x03		// Cluster Complete
//
// </Tokenize>


// ************************************************************************************************
// ***                                      Target                                              ***
// ************************************************************************************************

// Target/Cluster Status <Tokenize>
//
#define TRG_STATUS_STATE_CAPTURED				0x00000001	// +					Captured
#define TRG_STATUS_STATE_STARE					0x00000002	// | 					Stare received (Stare Packet)
#define TRG_STATUS_STATE_IMAGE					0x00000004	// | State				Image received (ImageCaptureStatus Packet)
#define TRG_STATUS_STATE_TARGETED				0x00000008	// |					Point-List Sent
#define TRG_STATUS_STATE_FAILED					0x00000010	// |					Timeout on Target
#define TRG_STATUS_STATE_NOT_CONSIDERED			0x00000020	// |					Target is not close enough to consider
#define TRG_STATUS_STATE_MASK					0x000000FF	// +					Mask of Above

#define TRG_STATUS_CAPTURE_ANGLES_GOOD			0x00000100	// +					Captured with Good Angles
#define TRG_STATUS_CAPTURE_RANGE_GOOD			0x00000200	// | Capture			Captured with Good Range
#define TRG_STATUS_CAPTURE_ALL_GOOD				0x00000300	// | State				Captured with Good Angle and Range
#define TRG_STATUS_CAPTURE_MASK					0x00000F00	// +					Mask of all Above

#define TRG_STATUS_VALID_ALT_ELL				0x00001000	// +					ELL Alt valid
#define TRG_STATUS_VALID_ALT_MSL				0x00002000	// | Validity			MSL Alt Valid
#define TRG_STATUS_VALID_ALT_AGL				0x00004000	// |					AGL Alt Valid
#define TRG_STATUS_VALID_ALT_MASK				0x0000F000	// +					Mask of Above

#define TRG_STATUS_CURRENT_ANGLES_OK			0x00010000	// +					Gimbal is "in View" based on Target Normals
#define TRG_STATUS_CURRENT_RANGE_OK				0x00020000	// | 					Gimbal is "in Range" based on Target Min/Max Range
#define TRG_STATUS_CURRENT_ALL_OK				0x00030000	// | Immediate 			Mask of Above
#define TRG_STATUS_CURRENT_APPROACHING			0x00100000	// | State				Approaching Target
#define TRG_STATUS_CURRENT_LEAVING				0x00200000	// |					Leaving Target
#define TRG_STATUS_CURRENT_MASK					0x00FF0000	// +					Mask of Above
//
// </Tokenize>

#define TRG_STATUS_FULLY_CAPTURED				(TRG_STATUS_CAPTURED|TRG_STATUS_CAPTURED_ANGLES_GOOD|TRG_STATUS_CAPTURED_RANGE_GOOD)
#define TRG_STATUS_VOLATILE_MASK				(TRG_STATUS_RESULTS_MASK|TRG_STATUS_TARGETING_MASK)

// SLIP Src/Dsts
//
#define SLIP_ID_HOST							0x03		// Odin
#define SLIP_ID_GIMBAL							0x10		// Aethon Gimbal

// SLIP Messages
//
#define SLIP_MSG_TARGETS						0x10	// [IN]
#define SLIP_MSG_TARGET_QUERY					0x11	// [IN]
#define SLIP_MSG_TARGET_STATUS					0x12	// [OUT]
#define SLIP_MSG_TRAJECTORY						0x13	// [IN]

#define SLIP_MSG_GIMBAL_CTRL					0x50	// [OUT]
#define SLIP_MSG_GIMBAL_STATUS					0x51	// [IN]
#define SLIP_MSG_GEO_POINT						0x52	// [OUT]
#define SLIP_MSG_GEO_EVENT						0x53	// [IN]
#define SLIP_MSG_GEO_FOCUS						0x54	// [IN]

#define SLIP_MSG_GPS_CTRL						0x60	// [OUT]
#define SLIP_MSG_GPS_STATUS						0x61	// [IN]

#define SLIP_MSG_SENSOR_CTRL					0x70	// [OUT]
#define SLIP_MSG_SENSOR_STATUS					0x71	// [IN]


// *** F O R E C A S T ****************************************************************************

struct TargetDEF;
struct ClusterDEF;


// *** T Y P E D E F S ****************************************************************************

// RangeDEF
//
//	Defines the Min/Max Range for a Target/Cluster to be suitable for Capture
//
typedef struct RangeDEF
{
	BOOL					bValid;
	double					dMin;
	double					dMax;

} RangeDEF;


// NormalDEF
//
//	Defines the Min/Max Facing Angles for a Target/Cluster to be suitable for Capture
//
typedef struct NormalDEF
{
	BOOL					bValid;
	double					x;
	double					y;
	double					z;	
	double					x2D;
	double					y2D;
	double					dAzMin;
	double					dAzCenter;
	double					dAzMax;
	double					dAzWidth;

} NormalDEF;


// ClusterDEF
//
//	Defines a Cluster of Points or Targets
//
typedef struct ClusterDEF
{
	// Static Info
	//	
	uint16					u16ClusterIndex;		// 0-Based Cluster Index
	char					szName[32];				// Cluster Name 'cluster003'
	char					szID[32];				// Target ID '3'
	NormalDEF				normal;					// Normal
	RangeDEF				range;					// Min/Max Range

	// Targets
	//
	struct TargetDEF*		pTargetHead;			// First Target
	struct TargetDEF*		pTargetTail;			// Last Target
	uint16					u16Targets;				// Target Count

	// Centroid
	//
	double					dLat;					// Degrees
	double					dLng;					// Degrees
	double					dAltMSL;				// Alt MSL meters
	UTM						utm;

	// Tracking Data
	//
	uint32					u32ClusterStatus;		// TRG_STATUS_
	double					dGroundRange;			// Ground Distance to Gimbal (m)
	double					dSlantRange;			// Slant Distance to Gimbal (m)
	double					dClosestRange;			// Closest ever to Gimbal (m)
	double					dGimbalBearing;			// m (Bearing of Gimbal from Target (North=0))
	double					dFacing;				// +/- Angle to Normal (deg) (Error between the Bearing and the center of the Normal)
	double					dClosestFacing;			// Closest ever Facing

	// Target Selection
	//
	int32					s32CandidateWeight;		// Candidate Weight
	uint32					u32ClusterTargeted_ms;	// Time when the Cluster became the Target
	
	// Capture Summary (from sub Targets)
	//
	uint16					u16CapturesFull;		// Targets Captured Fully (both Range and Angles good)
	uint16					u16CapturesSoft;		// Targets Captured with just Bad Angles or Bad Distance
	uint16					u16Captures;			// Sum of Above
	
	// UI/Sim
	//
	int32					x0;						// Actual Center
	int32					y0;
	int32					x1;						// Where it's drawn
	int32					y1;
	int32					dx;						// offset from x0 -> x1
	int32					dy;
	BOOL					bCoincidental;
	BOOL					bFirstCoincidence;
	BOOL					bVisible;
	uint8					u8ZLevel;				// 0 = Not Visible, 1 = Bottom (Full Black), 2 = Full Green, 3 = Top (Partial)
	uint16					u16Radius;
	uint32					u32OpenTimeout_ms;
		
	struct ClusterDEF*		pPrev;
	struct ClusterDEF*		pNext;

} ClusterDEF;


// TargetDEF
//
//	Defines a Point or Target within a Cluster
//
typedef struct TargetDEF
{
	// Static Info
	//
	uint16					u16TargetIndex;			// 0-Based Index within Cluster
	char					szName[32];				// Target Name 'horzA1_2'
	char					szID[32];				// Target ID '02'
	double					dLat;					// Degrees
	double					dLng;					// Degrees	
	double					dAltMSL;				// m
	UTM						utm;					// UTM
	NormalDEF				normal;					// Normal
	RangeDEF				range;					// Min/Max Range
	struct ClusterDEF*		pCluster;				// Owner Cluster

//	uint32					u32DwellTime_ms;
//	double					dExtent_m;
//	double					dMaxGsdMMPerPx;

	// Tracking Data
	//
	uint32					u32TargetStatus;		// TRG_STATUS_
	double					dGroundRange;			// Current Ground Distance to Gimbal (m)
	double					dSlantRange;			// Current Slant Distance to Gimbal (m)
	double					dClosestRange;			// Closest ever to Gimbal (m)
	double					dGimbalBearing;			// m (Bearing of Gimbal from Target (North=0))
	double					dFacing;				// Current Facing (Error between the Bearing and the center of the Normal)
	double					dClosestFacing;			// Closest ever Facing

	// Capture Summary
	//
	uint16					u16CapturesFull;		// Number of Captures with both Good Range and Good Angles
	uint16					u16CapturesSoft;		// Number of Captures with either a Bad Range or Bad Angle
	uint16					u16Captures;			// Sum of Above

	// UI/Sim
	//
	float					fTargetAngle;			// Angle to display Target at when Cluster is Exploded
	double					dLatDown;
	double					dLngDown;
	int32					x;						// Rendered
	int32					y;
	
	struct TargetDEF*		pPrev;
	struct TargetDEF*		pNext;

} TargetDEF;


// TrajDEF
//
//	Defines a Vertex in a Tragectory
//
typedef struct TrajDEF
{
	int16					s16Index;	// -1 Terminates
	float					fLat;
	float					fLng;
	float					fAltMSL;
	UTM						utm;

} TrajDEF;


// SensorInfoDEF
//
//	Describes Sensor dimensions for FOV Calculations
//
typedef struct SensorInfoDEF
{
	char					szDescription[64];
	double					dFocalLength_px;
	uint32					u32Width_px;
	uint32					u32Height_px;
	double					dPixelSize_m;
	double					dTriggerDelay_sec;
	float					fHFOV;
	float					fVFOV;

} SensorInfoDEF;


#endif // _M_GIMAPI_H_

// Gimbal.h
// ************************************************************************************************
//
//	Copyright @ 2015 - Aethon-Technologies
//
//	File:		GPS.h
//
//	Author:		A. Jones
//
//	Date:		30 Aug/2015
//
//	Brief:		
//
//	Notes:		
//
// ************************************************************************************************

#ifndef _M_GPSAPI_H_
#define _M_GPSAPI_H_

// *** I N C L U D E S ****************************************************************************
//
//#include "BaseTypes.h"


// *** D E F I N E S ******************************************************************************
//

// GPS Validity <Tokenize>
//
#define GPS_VALID_STATUS						0x0000000000000001
#define GPS_VALID_UTC_TIME						0x0000000000000002
#define GPS_VALID_GPS_TIME						0x0000000000000004
#define GPS_VALID_LAT							0x0000000000000008
#define GPS_VALID_LNG							0x0000000000000010
#define GPS_VALID_ALT_ELL						0x0000000000000020
#define GPS_VALID_ALT_MSL						0x0000000000000040
#define GPS_VALID_ALT_AGL						0x0000000000000080
#define GPS_VALID_ALT_OPT						0x0000000000000100
#define GPS_VALID_ELEVATION						0x0000000000000200
#define GPS_VALID_UNDULATION					0x0000000000000400
#define GPS_VALID_MAGVAR						0x0000000000000800
#define GPS_VALID_SPEED							0x0000000000001000
#define GPS_VALID_SPEED_OPT						0x0000000000002000
#define GPS_VALID_TRACKT						0x0000000000004000
#define GPS_VALID_TRACKM						0x0000000000008000
#define GPS_VALID_VELN							0x0000000000010000
#define GPS_VALID_VELE							0x0000000000020000
#define GPS_VALID_VELD							0x0000000000040000
#define GPS_VALID_VEL							0x0000000000080000
#define GPS_VALID_PITCH							0x0000000000100000
#define GPS_VALID_ROLL							0x0000000000200000
#define GPS_VALID_HEADINGT						0x0000000000400000
#define GPS_VALID_HEADINGM						0x0000000000800000
#define GPS_VALID_TIME							0x0000000001000000	// Hour/Min/Sec/etc.
#define GPS_VALID_SATT							0x0000000002000000
#define GPS_VALID_SATV							0x0000000004000000
#define GPS_VALID_SAT_CHNS						0x0000000008000000
#define GPS_VALID_ALIGN							0x0000000010000000
#define GPS_VALID_POS_FOM						0x0000000020000000
#define GPS_VALID_VEL_FOM						0x0000000040000000
#define GPS_VALID_HDG_FOM						0x0000000080000000
#define GPS_VALID_TIME_FOM						0x0000000100000000
#define GPS_VALID_QUALITY						0x0000000200000000
#define GPS_VALID_PDOP							0x0000000400000000
#define GPS_VALID_HDOP							0x0000000800000000
#define GPS_VALID_VDOP							0x0000001000000000
#define GPS_VALID_HORZ_POS_ERR					0x0000002000000000
#define GPS_VALID_VERT_POS_ERR					0x0000004000000000
#define GPS_VALID_VEL_POS_ERR					0x0000008000000000
#define GPS_VALID_ANG_VEL						0x0000010000000000
#define GPS_VALID_MAX_ANG_VEL					0x0000020000000000
#define GPS_VALID_ACC							0x0000040000000000
#define GPS_VALID_MAX_ACC						0x0000080000000000
//
// </Tokenize>

#define GPS_VALID_ALT_ALL						(GPS_VALID_ALT_ELL|GPS_VALID_ALT_MSL|GPS_VALID_ALT_AGL|GPS_VALID_ALT_OPT)

// GPS Type <Tokenize>
//
#define GPS_TYPE_INVALID						0
#define GPS_TYPE_GENERIC						1
#define GPS_TYPE_SIM							2
#define GPS_TYPE_NMEA0183						3
#define GPS_TYPE_GARMIN							4
#define GPS_TYPE_TRIMBLE						5
#define GPS_TYPE_APPLANIX						6
#define GPS_TYPE_SBET							7
#define GPS_TYPE_TRILLIUM						8
//
// </Tokenize>

// GPS DOP Values <Tokenize>
//
#define GPS_DOP_IDEAL							0					// < 1		Highest possible confidence level to be used for applications demanding the highest possible precision at all times.
#define GPS_DOP_EXCELLENT						2					// 1-2		At this confidence level, positional measurements are considered accurate enough to meet all but the most sensitive applications.
#define GPS_DOP_GOOD							5					// 2-5		Represents a level that marks the minimum appropriate for making business decisions. Positional measurements could be used to make reliable in-route navigation suggestions to the user.
#define GPS_DOP_MODERATE						10					// 5-10		Positional measurements could be used for calculations, but the fix quality could still be improved. A more open view of the sky is recommended.
#define GPS_DOP_FAIR							20					// 10-20	Represents a low confidence level. Positional measurements should be discarded or used only to indicate a very rough estimate of the current location.
#define GPS_DOP_POOR							100					// >20 		At this level, measurements are inaccurate by as much as 300 meters with a 6-meter accurate device (50 DOP � 6 meters) and should be discarded.
//
// </Tokenize>

// GPS Align <Tokenize>
//
#define GPS_ALIGN_INVALID						0
#define GPS_ALIGN_LAND_COARSE					1
#define GPS_ALIGN_LAND_FINE						2
#define GPS_ALIGN_AIR_COARSE					3
#define GPS_ALIGN_AIR_FINE						4
#define GPS_ALIGN_FULL							5
#define GPS_ALIGN_NA							6
//
// </Tokenize>

// GPS Quality <Tokenize>
//
#define GPS_QUAL_NA								0					// Fix not available
#define GPS_QUAL_FIX							1					// GPS fix
#define GPS_QUAL_3D								2					// Differential 3D fix
//
// </Tokenize>

// GPS Satellite Status <Tokenize>
//
#define GPS_SAT_STATUS_VALID					0x0001				// Valid/Known (Visible)
#define GPS_SAT_STATUS_TRACKING					0x0002				// Tracking
//
// </Tokenize>

#define	GPS_ALT_INVALID							-32768				// Invalid Elevation


#endif // _M_GPSAPI_H_

// GPS.h
// ************************************************************************************************
//
//	Copyright @ 2015 - Aethon-Technologies
//
//	File:		SLIP.h
//
//	Author:		A. Jones
//
//	Date:		28 Aug/2015
//
//	Brief:		Serial Link Interface Protocol
//
//	Notes:		
//
// ************************************************************************************************

#ifndef _SLIP_H_
#define _SLIP_H_


// *** I N C L U D E S ****************************************************************************
//
//#include "BaseTypes.h"


// *** D E F I N E S ******************************************************************************
//
#define	SLIP_VERSION_1							0x0100										// v1.00
#define	SLIP_VERSION_LATEST						SLIP_VERSION_1

#define SLIP_SYNC								0xAE										// Standard Protocol Framing Byte
#define SLIP_HEADER_SIZE 						8											// <u8Sync> <u8Seq> <u8SrcID> <u8DstID> <u8MsgID> <u16PayloadLen> <u8HeaderCheckSum>
#define SLIP_FOOTER_SIZE 						2											// <u16PayloadCRC16>
#define SLIP_OVERHEAD_SIZE 						(SLIP_HEADER_SIZE + SLIP_FOOTER_SIZE)		// Cost of Header and Footer
#define SLIP_MAX_PAYLOAD_SIZE					2048										// Maximum Payload Size
#define SLIP_MAX_PACKET_SIZE					(SLIP_OVERHEAD_SIZE+SLIP_MAX_PAYLOAD_SIZE)	// Maximum Packet Size

// Source and Destinations
//
#define SLIP_ID_ANY								0xFF

// *** Internal Message Types *********************************************************************
//
#define	SLIP_MSG_INVALID						0x00	// Invalid
#define	SLIP_MSG_NULL							0x01	// Empty						[void]									- Used for Keep-Alive
#define	SLIP_MSG_NULL_ECHO						0x02	// Empty						[void]									- Resonse to a NULL
#define	SLIP_MSG_SHELL							0x03	// Tunnel Shell					[<u8Len>"String"]						- Transfer of Shell typing/output thru SLIP
#define	SLIP_MSG_DISCOVER						0x04	// Broadcast Discover			[<u8Len>"Config"]						- Broadcast our Info to find a Device to Connect to
#define	SLIP_MSG_DISCOVER_RESP					0x05	// Broadcast Discover			[<u8Len>"Config"]						- Discover Response
#define SLIP_MSG_TABLE							0x06	// A chunk of Message Table		[<u32CRC> ... <u16Index><u8Flags><u16DataSize><Data> ...]


// Forecast
//
struct SLIPStreamDEF;

// Function Pointer
//
typedef BOOL (*SlipFN)							(struct SLIPStreamDEF* pSLIP);


// *** T Y P E D E F S ****************************************************************************


// SLIPStreamDEF **********************************************************************************
//
typedef struct SLIPStreamDEF
{
	uint8				u8OurID;							// Our Device ID
	SlipFN				pFuncProcessPacket;					// Function that handles Complete Packets
	void*				vpUser;								// User defineable data associated with Stream

	// Protocol State Machine
	//
	uint8				u8State;
	uint8				u8Sync;
	uint8				u8Seq;
	uint8				u8Src;
	uint8				u8Dst;
	uint8				u8Msg;
	uint16				u16PayloadLen;
	uint8				u8CalcChecksum;
	uint8				u8HeaderChecksum;
	uint16				u16CRCPayloadCalc;
	uint16				u16CRCPayload;
	uint16				u16RXPayloadAcquired;
	uint8				u8RXPayload[SLIP_MAX_PAYLOAD_SIZE];
	uint8*				pu8RXByte;
	uint8				u8RXSeqNum;
	uint8				u8TXPacket[SLIP_MAX_PACKET_SIZE];
	uint16				u16TXPacketSize;
	uint8				u8TXSeqNum;

	// Statistics
	//
	uint32				u32RXBytes;
	uint32				u32RXPackets;
	uint32				u32RXExtra;
	uint32				u32RXErrors;
	uint32				u32RXBadHeaderChk;
	uint32				u32RXBadPayloadCRC;
	uint32				u32RXSeqErrors;
	uint32				u32TXPackets;

} SLIPStreamDEF;


// *** P R O T O T Y P E S ************************************************************************
//
extern BOOL			SLIP_Create(SLIPStreamDEF* pSLIP, uint8 u8OurID, SlipFN pFunc, void* vpUser);
extern uint16 		SLIP_FramePayload(SLIPStreamDEF* pSLIP, uint8 u8SrcID, uint8 u8DstID, uint8 u8MsgID, uint8* pu8Payload, uint16 u16PayloadLen);
extern BOOL			SLIP_Consume(SLIPStreamDEF* pSLIP, uint8* pu8Buffer, uint16 u16Len);

#endif // _SLIP_H_

//	-------------------------------------------------------------------
//	GimbalStatusMessage_t
//	-------------------------------------------------------------------

typedef struct {
	uint16_t u16Validity;
	uint8_t u8Mode;
	uint16_t u16Flags;
	float fPanAngle;
	float fTiltAngle;
	float fRollAngle;
	float fGyroRate[3];
}GimbalStatusMessage_t; // GIMBAL_STATUS

//	-------------------------------------------------------------------
//	GimbalCtrlMessage_t
//	-------------------------------------------------------------------

typedef struct {
	uint16_t u16Validity;
	uint8_t u8Mode;
	uint16_t u16Flags;
	float fPan;
	float fTilt;
	float fRoll;
} GimbalCtrlMessage_t;

//	-------------------------------------------------------------------
//	GeoPoint_t
//	-------------------------------------------------------------------

typedef struct {
	uint16_t u16TargetID;
	double dLatitude;
	double dLongitude;
	float fAltELL;
} GeoPoint_t;

//	-------------------------------------------------------------------
//	GeoPointMessage_t
//	-------------------------------------------------------------------

#define MAX_GEO_POINTS 30

typedef struct {
	uint16_t u16Sequence;
	uint16_t u16Validity;
	uint16_t u16Flags;
	uint16_t u16ClusterID;
	float fMinRng;
	float fMaxRng;
	float fMinAz;
	float fMaxAz;
	uint16_t u16Targets;
	GeoPoint_t points[MAX_GEO_POINTS];
} GeoPointMessage_t;

//	-------------------------------------------------------------------
//	GeoPointEvent_t
//	-------------------------------------------------------------------

typedef struct {
	uint8_t u8Event;
	uint16_t u16Sequence;
	uint8_t u8Sensor;
	uint32_t u32Frame;
	uint16_t u16Cluster;
	uint16_t u16Target;
	float fGimRange;
	float fGimBearing;
	uint16_t u16Validity;
	float fPanAngle;
	float fTiltAngle;
	float fRollAngle;
	uint64_t u64Validity;
	uint64_t u64GPSTime;
	uint16_t u16GPSStatus;
	uint8_t u8SatV;
	uint8_t u8SatT;
	uint8_t u8Align;
	uint8_t u8Qual;
	double dLatitude;
	double dLongitude;
	double dAltELL;
	float fPitch;
	float fRoll;
	float fYawHdgT;
	float fTrackT;
	float fSpeed;
	float fVelN;
	float fVelE;
	float fVelD;
} GeoPointEvent_t;

//	-------------------------------------------------------------------
//	GeoFocusMessage_t
//	-------------------------------------------------------------------

typedef struct {
	uint16_t u16Cluster;
	uint16_t u16Target;
	float fSlantRange;
} GeoFocusMessage_t;

//	-------------------------------------------------------------------
//	
//	-------------------------------------------------------------------

	extern GimbalStatusMessage_t GimbalStatusMessage;
	extern GimbalCtrlMessage_t GimbalCtrlMessage;
	extern GeoPointMessage_t GeoPointMessage;
	extern GeoPointEvent_t GeoPointEvent;
	extern GeoFocusMessage_t GeoFocusMessage;
	
//	-------------------------------------------------------------------
//	get/set Message
//	-------------------------------------------------------------------

	void getGimbalStatusMessage(uint8_t *payload, uint16_t len);
	void getGimbalCtrlMessage(uint8_t *payload, uint16_t len);
	void getGeoPointMessage(uint8_t *payload, uint16_t len);
	void getGeoPointEvent(uint8_t *payload, uint16_t len);
	void getGeoFocusMessage(uint8_t *payload, uint16_t len);

	uint16_t packGimbalStatusMessage(uint8_t *payload);
	uint16_t packGimbalCtrlMessage(uint8_t *payload);
	uint16_t packGeoPointMessage(uint8_t *payload);
	uint16_t packGeoPointEvent(uint8_t *payload);
	uint16_t packGeoFocusMessage(uint8_t *payload);
