/*
This program is distributed under the terms of the 'MIT license'. The text
of this licence follows...

Copyright (c) 2006 J.D.Medhurst (a.k.a. Tixy)

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.
*/

/**
@file

@brief Y-Modem receive protocol.
*/

#include "ymodem.h"

/**
@brief Y-Modem receiver object.
@ingroup ymodem
*/
class YModemRx : public YModem
	{
public:
	/**
	Construct a Y-Modem object which will transmit data over the given port.

	@param port		The port.
	*/
	YModemRx(SerialPort& port);

	/**
	Abstract class representing a stream of data being read.
	*/
	class OutStream
		{
	public:
		/**
		Open the stream.

		If no data has yet been written to the stream, this function may be
		called a second time in order to update the file size.

		@param fileName	Name for the file being received.
		@param size		Size of data being received.
		*/
		virtual int Open(const char* fileName, size_t size) =0;

		/**
		Write data to the stream.

		@param data		Pointer to data to write.
		@param size		Size of data.

		@return Zero if successful, or a negative error value if failed.
		*/
		virtual int Out(const uint8_t* data, size_t size) =0;

		/**
		Empty destructor to avoid compiler warnings.
		*/
		inline virtual ~OutStream() {}
		};

	/**
	Receive data using X-Modem.

	@param out		The stream of data write received data to.
	@param timeout	Time in milliseconds to wait sender to become ready.
	@param useCrc	If true the CRC checking protocol wil be used.

	@return Zero if transfer was successful, or a negative error value if failed.
	*/
	int ReceiveX(OutStream& out, unsigned timeout, bool useCrc);

	/**
	Receive data using Y-Modem.

	@param out		The stream of data write received data to.
	@param timeout	Time in milliseconds to wait sender to become ready.
	@param gMode	If true the Y-Modem G protocol wil be used.

	@return Zero if transfer was successful, or a negative error value if failed.
	*/
	int ReceiveY(OutStream& out, unsigned timeout, bool gMode);

	/**
	Enumeration of possible error values.
	*/
	enum RxError
		{
		ErrorOutputStreamError				= -400,	/**< Error with output stream */
		ErrorTransmitterNotBehaving			= -401,	/**< Unexpected data received */
		ErrorTranferTerminatedByTransmitter	= -402,	/**< Transfer was terminated by sender */
		ErrorReceivedBadData				= -403,	/**< Received invalid data */
		ErrorMultipleFilesSent				= -404	/**< More than one file was sent. (We only support one.) */
		};

private:
	/**
	OutStream object for receiving block zero of Y-Modem protocol.
	*/
	class OutBlock0 : public OutStream
		{
	public:
		OutBlock0();
		int Open(const char* fileName, size_t size);
		int Out(const uint8_t* data, size_t size);
		int Parse(const char*& fileName, size_t& fileSize);
	private:
		uint8_t Buffer[1024];	///< Copy of the data received via #Out. Size of data is #Size.
		size_t Size;			///< Size of data in #Buffer
		};

	int ReceiveInitialise(OutStream& out,unsigned timeout);
	int ReceiveBlock(OutStream& out);
	int OutChar(uint8_t c);
private:
	size_t BlockNumber;
	bool ExpectCRC;		///< Set to true if blocks will have a CRC rather than a checksum.
	bool SendBlockACK;
	uint8_t ModeChar;
	uint8_t NAKChar;
	};


