// iBCN data types

typedef signed char s8;		// 8-bit signed value
typedef unsigned char u8;	// 8-bit unsigned value
typedef signed short s16;	// 16-bit signed value
typedef unsigned short u16;	// 16-bit unsigned value
typedef signed int s32;		// 32-bit signed value
typedef unsigned int u32;	// 32-bit unsigned value
// char; // 8-bit ASCii character, same as u8
typedef u8 BOOL; 			// TRUE/FALSE indicator, same as u8
typedef u32 DATE_TIME; 		// u32, seconds since Jan 6, 1980 or encoded h:m:s d/m/y

enum {	// event codes
	POWER_UP = 2,
	BATTERY_STATUS = 3,
	CONFIG_UPDATE = 7,
	GPS_TIMEOUT = 9,
	IRIDIUM_TIMEOUT = 10,
	TIME_SET = 15,
	GPS_POSITION_REQ = 16
	// event code greater than 128 (MSB set) is a GPS position report
}; // end enum
	
// The following pragmas are necessary to force the compiler to pack the structures
//  on 1 byte boundaries. If this is not done, the compiler may use a long word (4 bytes)
//  to store a single-byte value. Storing a byte in a long word speeds up access to that
//  byte, but misaligns the elements of the structure with respect to the way they were
//  created on the satellite beacon.
#pragma pack(push)			// push current alignment to stack
#pragma pack(1)				// set alignment to 1-byte boundary

typedef struct {
	DATE_TIME timestamp;	// timestamp of event report
	u8 event_code;			// MSB clear to indicate Event Report, remaining bits specify which event
	u8 event_data[11];		// data bytes dependent upon event code
} T_EVENT_REPORT; // end typedef

typedef struct {
	DATE_TIME timestamp;	// timestamp of position report
	u8 event_code;			// MSB set to indicate Position Report, remaining bits available to use
							// bit 7: set to indicate Position Report
							// bit 6: motion bit to indicate whether in motion or not
							// bit 5:3 – Number of satellites (0-7 map to 3-10+)
							// bit 2:0 – HDOP (0-7 map to 0-3.5+)
	u32 latitude;			// fixed-point integer of degrees latitude scaled by 1,000,000 and offset by +90.0
	u32 longitude;			// fixed-point integer of degrees longitude scaled by 1,000,000 and offset by +180.0
	u8 speed;				// meters per second
	u8 fix_accuracy;		// meters
	u8 time_to_fix;			// seconds
} T_POSITION_REPORT; // end typedef

typedef struct {
	u32 record_index;
	T_EVENT_REPORT records[12];
} T_RECORDS_PACKET; // end typedef

typedef struct {
	u32 record_count;
	u32 last_record_downloaded;
	DATE_TIME first_record_timestamp;
	DATE_TIME last_record_timestamp;
} T_LOG_STATUS; // end typedef

typedef struct {
	DATE_TIME time;
	u16 battery_voltage;	// voltage (V) x 10
	s8 temperature;			// in degrees Celsius
	u8 reserved1;
	u8 gps_status;
	u8 reserved2[4];
} T_STATUS; // end typedef

typedef struct {
	u8 reserved1[32];
	u8 imei[15];
	u8 reserved2[36];
} T_EXTENDED_STATUS; // end typedef

typedef struct {
	u32 reserved;
	u32 fixInterval;		// In seconds
	u32 mailboxCheckInterval; // In number of fixes
	BOOL waterSwitchEnabled;
	u32 transmitInterval;	// In number of fixes
} T_MODE; // end typedef

typedef struct {
	u8 model_number[24];	// MMI-7500 (PID7014)
	u32 serial_number;
	u8 fw_rev[4];			// major.minor.revision
	u8 iridium_fw_rev[16];	// ASCii string - TAxxxxxx
	u8 gps_fw_rev[80];		// ASCii string – 80 characters
} T_IDENTITY; // end typedef

typedef struct {
	u8 reset_reason;
} T_EVENT_DATA_POWER_UP; // end typedef

typedef struct {
	u16 voltage;			// voltage (V) x 10
	s8 temperature;			// in degrees Celsius
} T_EVENT_DATA_BATTERY_STATUS; // end typedef

typedef struct {
	u8 source;				// 0 = internal, 1 = Iridium OTA, 2 = reserved, 3 = Bluetooth
} T_EVENT_DATA_CONFIG_UPDATE; // end typedef

typedef struct {
	u32 latitude;			// fixed-point integer of degrees latitude scaled by 1,000,000 and offset by +90.0
	u32 longitude;			// fixed-point integer of degrees longitude scaled by 1,000,000 and offset by +180.0
	u8 speed;				// meters per second
	u8 fix_accuracy;		// meters
	u8 num_satellites;
} T_EVENT_DATA_GPS_TIMEOUT; // end typedef

typedef struct {
	u8 reason; // 
} T_EVENT_DATA_IRIDIUM_TIMEOUT; // end typedef

typedef struct {
	u8 source; 				// 0 = GPS, 1 = user
	u32 offset;
} T_EVENT_DATA_TIME_SET; // end typedef

typedef struct {
	u8 source; 				// 0 = reserved, 1 = command
} T_EVENT_DATA_GPS_POSITION_REQUEST; // end typedef

#pragma pack(pop)   		// restore original alignment from stack
