/*
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 Base class for YModemRx and YModemTx.
*/

#ifndef YMODEM_H
#define YMODEM_H

#include "serial_port.h"

/**
@defgroup ymodem Comms - Y-Modem (and X-Modem) transmission protocol
@brief Y-Modem (and X-Modem) transmission protocol.
@{
*/

/**
@brief Base class for YModemRx and YModemTx.
*/
class YModem
	{
protected:
	inline YModem(SerialPort& port)
		: Port(port)
		{}

	/**
	Checksum a block of data.

	@param data		Start of data to checksum.
	@param size		Size of data.

	@return Sum of bytes in data, modulo 256.
	*/
	uint8_t Checksum(const uint8_t* data, size_t size);

	/**
	Calculate CRC for a block of data.

	@param data		Start of data to checksum.
	@param size		Size of data.

	@return CRC of data.
	*/
	uint16_t CRC16(const uint8_t* data, size_t size);

	/**
	Update CRC value by accumulating another byte of data.

	@param crcIn	Previous CRC value.
	@param byte		A byte of data.

	@return Updated CRC value.
	*/
	uint16_t UpdateCRC16(uint16_t crcIn, uint8_t byte);

	/**
	Receive a single character.
	If the timeout period is exceeded, ErrorTimeout is returned.

	@param timeout	Time in milliseconds to wait if no data available.

	@return The character received, or a negative error value if failed.
	*/
	int InChar(unsigned timeout);

	/**
	Send CANcel sequence.
	*/
	void Cancel();

	/**
	Enumeration of control characted used in communications protocol.
	*/
	enum ControlCharacters
		{
		SOH = 0x01,
		STX = 0x02,
		EOT = 0x04,
		ACK = 0x06,
		NAK = 0x15,
		CAN = 0x18
		};

	/**
	Enumeration of possible error values.
	*/
	enum Error
		{
		ErrorTimeout					= -200,	/**< Timed out trying to communicate with other device */
		ErrorBlockRetriesExceded		= -201	/**< A block could not be sent */
		};

protected:
	/**
	The serial port to use for communications.
	*/
	SerialPort& Port;
	};


/** @} */ // End of group

#endif
