//------------------------------------------------------------------------------
// <copyright file="VirtualRelayCryptoProcessor.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>
// 	Declaration of VirtualRelayCryptoProcessor class
// </summary>
//
// <owner>Arash Ushani</owner>
//------------------------------------------------------------------------------

#ifndef __VirtualRelayCryptoProcessor_H__
#define __VirtualRelayCryptoProcessor_H__

#include "CryptoProcessor.h"
#include "LqrSMACLogger.h"
#include <boost/shared_ptr.hpp>
#include <boost/signals2.hpp>
#include <stdint.h>

namespace LiquidRobotics {

namespace CryptoLib {
	class CryptoProcessor;
}

namespace SMACFramework {
	class Logger;
}

namespace VirtualRelay {

class VirtualRelayCryptoProcessor
{
public:

	typedef LiquidRobotics::SMACFramework::Logger Logger;
	typedef std::vector<uint8_t> UInt8Vector;
	typedef boost::shared_ptr<CryptoLib::CryptoProcessor> CryptoProcessorPtr;
	typedef boost::signals2::mutex Mutex;
	typedef std::string string;

	VirtualRelayCryptoProcessor(Logger& _logger);
	~VirtualRelayCryptoProcessor();

	UInt8Vector IncomingLevel2Message(UInt8Vector& packet);
	UInt8Vector OutgoingLevel2Message(UInt8Vector& packet);

	void ProcessSecureOnAck2(UInt8Vector& packet);
	UInt8Vector VerifySecureAuthenticate(UInt8Vector& packet);

	bool IsEncrypted();

private:

	UInt8Vector EncryptPacket(UInt8Vector& clearTextPacket);
	UInt8Vector DecryptPacket(UInt8Vector& securePacket);

	Logger& logger;

	CryptoProcessorPtr activeCrypto, pendingCrypto;

	std::string PassPhrase;
	std::string ChannelID;

	bool connectionSecure;
	bool pendingSecurityChange;
	bool pendingConnectionSecurityState;
	uint32_t securityAckWGMSID;

	bool pendingChannelStatus;

	Mutex incomingMutex;
	Mutex outgoingMutex;
};

}
}

#endif
