//------------------------------------------------------------------------------
// <copyright file="HDLCPort.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>
// </summary>
//
// <owner></owner>
//------------------------------------------------------------------------------


#ifndef HDLCPORT_H_
#define HDLCPORT_H_

#include <deque>
#include <stdexcept>
#include <string>
#include <vector>
#include <boost/serialization/access.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/utility.hpp>
#include "LqrSMACByteArray.h"
#include "LqrSMACSerialPort.h"
#include "LqrSMACLogger.h"
#include "LqrSMACSharedLib.h"
#include "LqrSMACDeclareException.h"

namespace LiquidRobotics {
namespace PayloadInterface {

class IHDLCPort
{
public:

	typedef SMACFramework::ByteArray::byte byte;
	typedef unsigned int Timeout;
	typedef SMACFramework::ByteArray ByteArray;
	typedef std::string string;
	typedef boost::shared_ptr<IHDLCPort> Ptr;

	static const Timeout NO_TIMEOUT;

	DECLARE_EXCEPTION(NoPortConfigured, std::runtime_error);

	virtual void FlushInput() = 0;
	virtual void Receive(ByteArray& result) = 0;
	virtual void Send(const ByteArray& data) = 0;
	virtual void SendRawBytes(const ByteArray& rawBytes) = 0;

	virtual Timeout GetIntercharacterTimeout() const = 0;
	virtual void SetIntercharacterTimeout(Timeout newValue) = 0;

	virtual Timeout GetReceiveTimeout() const = 0;
	virtual void SetReceiveTimeout(Timeout newValue) = 0;

	virtual string ToString() const = 0;
};

class HDLCPort : public IHDLCPort, public boost::noncopyable
{
public:

	typedef SMACFramework::ISerialPort ISerialPort;
	typedef ISerialPort::Ptr SerialPortPtr;
	typedef LiquidRobotics::SMACFramework::Logger Logger;

	HDLCPort(unsigned _maxLength, Logger& _logger)
		throw (std::invalid_argument);
	HDLCPort(SerialPortPtr _pSerialPort, unsigned _maxLength, Logger& _logger)
		throw (std::invalid_argument);

	virtual ~HDLCPort();

	void FlushInput()
		throw (NoPortConfigured);
	void Receive(ByteArray& result)
		throw (NoPortConfigured);
	static void GetRawTransmitBytes(const ByteArray& bytesIn, ByteArray& bytesOut)
		throw (std::invalid_argument, NoPortConfigured);
	void Send(const ByteArray& data)
		throw (std::invalid_argument, NoPortConfigured);
	void SendRawBytes(const ByteArray& rawBytes)
		throw (std::invalid_argument, NoPortConfigured);

	ISerialPort& GetSerialPort() const;
	void SetSerialPort(SerialPortPtr newValue);
	bool HaveSerialPort() const;

	Timeout GetIntercharacterTimeout() const;
	void SetIntercharacterTimeout(Timeout newValue);

	unsigned GetMaxLength() const;

	Timeout GetReceiveTimeout() const;
	void SetReceiveTimeout(Timeout newValue);

	string ToString() const;

private:

	friend class boost::serialization::access;

	template<class Archive>
	void serialize(Archive& ar, const unsigned int version)
	{
		ar & BOOST_SERIALIZATION_NVP(intercharacterTimeout);
		ar & BOOST_SERIALIZATION_NVP(receiveTimeout);
	}

	SerialPortPtr pSerialPort;
	Timeout intercharacterTimeout;
	Timeout receiveTimeout;
	const unsigned maxLength;
	Logger& logger;
};

std::ostream& operator<<(std::ostream& out, const HDLCPort& hdlcPort);

}
}

#endif /* HDLCPORT_H_ */
