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

#include "ymodem.h"

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

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

	/**
	Abstract class representing a stream of data being read.
	*/
	class InStream
		{
	public:
		/**
		Read data from the stream.

		@param[out] data	Pointer to buffer to hold data read from stream.
		@param size			Maximum size of data to read.

		@return Number of bytes successfully read, or a negative error value if failed.
		*/
		virtual int In(uint8_t* data, size_t size) =0;

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

	/**
	Send data using X-Modem.

	@param in		The stream of data to send.
	@param timeout	Time in milliseconds to wait receiver to become ready.
	@param kMode	False to use 128 byte blocks, true to use 1kB blocks
	@return Zero if transfer was successful, or a negative error value if failed.
	*/
	int SendX(InStream& in, unsigned timeout, bool kMode);

	/**
	Send data using Y-Modem.

	@param fileName	The name of the file being transferred.
	@param size		The size of the data being transferred.
	@param in		The stream of data to send.
	@param timeout	Time in milliseconds to wait receiver to become ready.

	@return Zero if transfer was successful, or a negative error value if failed.
	*/
	int SendY(const char* fileName, size_t size, InStream& in, unsigned timeout);

	/**
	Enumeration of possible error values.
	*/
	enum TxError
		{
		ErrorInputStreamError			= -300,	/**< Error with input stream */
		ErrorReceiverNotBehaving		= -301,	/**< Unexpected data received */
		ErrorTranferTerminatedByReceiver= -302,	/**< Transfer was terminated by receiver */
		ErrorFileNameTooLong			= -303	/**< File name was too long to be transmitted */
		};

private:
	int SendInitialise(unsigned timeout);
	int SendBlock(const uint8_t* data, size_t size);
	int SendData(const uint8_t* data, size_t size);
	int SendAll(InStream& in);
	int MakeBlock0(uint8_t* buffer, const char* fileName, size_t fileSize);
	int ProcessResponse(int c);
private:
	size_t BlockNumber;
	bool SendCRC;
	bool WaitForBlockACK;
	bool Use1KBlocks;
	uint8_t ModeChar;
	int CancelCount;
	};



