//------------------------------------------------------------------------------
// <copyright file="VirtualRelayMessage.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
// 	Defines some utility methods for Virtual Relay messages to WGMS
// </summary>
//
// <owner>Arash Ushani</owner>
//------------------------------------------------------------------------------

#ifndef _VIRTUAL_RELAY_MESSAGE_H_
#define _VIRTUAL_RELAY_MESSAGE_H_

#include "EventPropertyValue.h"
#include <stdint.h>

#define bswap16(x) ((((x)&0xFF)<<8) | ((x)>>8))

namespace LiquidRobotics {

namespace VirtualRelay {

class VirtualRelayPacketComparision
{

public:

	typedef std::vector<uint8_t> VR_LEVEL_2_Packet;

	enum Priority
	{
		OTHER = 0,
		FILE_TRANSFER_EOF = 1, // MUST BE SMALLER THAN FILE_TRANSFER TO ENSURE EOF IS RECEIVED LAST!!!!
		FILE_TRANSFER = 2,
		TELEMETRY = 3,
		COMMAND_RESPONSE = 4,
		SECURITY = 5
	};

	Priority GetPriority(VR_LEVEL_2_Packet& p);

	bool operator()(VR_LEVEL_2_Packet& p1, VR_LEVEL_2_Packet& p2)
	{
		return GetPriority(p1) < GetPriority(p2);
	}
};

class SHARED_LIB_PUBLIC VirtualRelayMessage
{
public:

	typedef unsigned char byte;
	typedef EventLib::EventPropertyValue::UInt8Vector UInt8Vector;

	#pragma pack(push,1)
	typedef struct _VR_SHIPTOSHOREHADER_
	{
		byte protocolRevision;
		uint16_t messageLength;
		byte MOHeaderIEI;
		uint16_t MOHeaderLength;
		char CDR[4];
		uint32_t uniqueID;
		uint32_t ipAddr;
		uint32_t sessionID;
		uint16_t length;
		byte spare;
		byte sessionStatus;
		uint16_t MOMSN;
		uint16_t MTMSN;
		uint32_t timeOfSession;
		byte MOPayloadIEI;
		uint16_t MOPayloadLength;
		byte MOLocationInfoIEI;
		uint16_t MOLocationInfoLength;
		byte MOLatLng[7];
		uint32_t CEPRadius;

	}VR_SHIPTOSHOREHADER, *PVR_SHIPTOSHOREHADER;

	typedef struct _VR_SHORETOSHIPHEADER_
	{
		byte protocolRevision;
		uint16_t messageLength;
		byte MOHeaderIEI;
		uint16_t MOHeaderLength;
		char CDR[4];
		uint32_t uniqueID;
		uint32_t ipAddr;
		uint32_t sessionID;
		uint16_t spare;
		byte spare2;
		uint16_t MTDispositionFlags;
		byte MTPayloadIEI;
		uint16_t MTPayloadLength;
	}VR_SHORETOSHIPHEADER, *PVR_SHORETOSHIPHEADER;

	typedef struct _VR_RELAYSTATUS_MSG_
	{
		byte channelState:1;
		byte messageNotSent:1;
		byte communicationsErrorAlarm:1;
		byte channelNotAuthenaticated:1;
		byte notUsed2:3;
		byte channelEncryptedStatus:1;

		byte channelRevMajor;
		byte channelRevMinor;
		byte channelRevSubMinor;

		byte hour;
		byte minute;
		byte second;
		byte month;
		byte day;
		byte year;

		byte signalStrength;
	}VR_RELAYSTATUS_MSG, *PVR_RELAYSTATUS_MSG;

	typedef struct _VR_RELAYSTATUS_
	{
		byte type;
		uint32_t notUsed1;
		uint16_t size;

		VR_RELAYSTATUS_MSG msg;

		uint16_t crc16;

	}VR_RELAYSTATUS, *PVR_RELAYSTATUS;

	typedef struct _VR_CHANNELSTATUS_
	{
		uint16_t relayStatusSize;
		uint16_t relayStatusCRC;
		byte disposition;
	}VR_CHANNELSTATUS, *PVR_CHANNELSTATUS;

	typedef struct _VR_ACK_BASIC_
	{
		byte type;
		uint32_t commandID;
		uint16_t responseSize;
		// empty response
		// uint16_t CRC;
	} VR_ACK_BASIC, *PVR_ACK_BASIC;

	typedef struct _VR_SECURE_PACKET_HEADER_
	{
		byte type;
		byte sizeIV;
		uint16_t sizeCipher;
	} VR_SECURE_PACKET_HEADER, *PVR_SECURE_PACKET_HEADER;
	#pragma pack(pop)

	enum EncryptedPacketTypes
	{
		EncryptedShiptoShore = 0xA6,
		EncryptedShoretoShip = 0xA7
	};

	static void GetRelayStatusMessage(PVR_RELAYSTATUS relayStatus, bool channelState, bool messageNotSent, bool commError, bool channelNotAuth, bool encrypted, uint8_t strength);
	static void GetRelayStatusMessage(PVR_RELAYSTATUS relayStatus, bool channelState, bool messageNotSent, bool commError, bool channelNotAuth, bool encrypted);
	static UInt8Vector GetOutgoingRelayMessage(UInt8Vector& msg, byte protocolRevision, int vehicleUniqueID, int sessionID, uint16_t socketBufferSize);
	static UInt8Vector GetOutgoingRelayMessage(char* msg, int msgLength, byte protocolRevision, int vehicleUniqueID, int sessionID, uint16_t socketBufferSize);
	static void AppendCRC(UInt8Vector& msg);
	static uint16_t GetCRC(UInt8Vector& msg);
};
}
}

#endif
