/*
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 Abstract interfrace to a serial port device.
*/

#ifndef SERIAL_PORT_H
#define SERIAL_PORT_H

/**
@defgroup serialport Comms - Serial Port interface
@brief Serial port interface.
@{
*/

/**
@brief Abstract interfrace to a serial port device.
*/
class SerialPort
	{
public:
	/**
	Construct a serial port.

	@return The port object, or zero if an error ocurred.
	*/
	static SerialPort* New();

	/**
	Open port for communications over a specified port number.

	@param port	Port number.

	@return Zero if successful, or a negative error value if failed.
	*/
	virtual int Open(const char* port) =0;

	/**
	Initialise port.

	@param baud		Baud rate for port.

	@return Zero if successful, or a negative error value if failed.

	@pre Open() must have been called.
	*/
	virtual int Initialise(unsigned baud) =0;

	/**
	Transmit data.

	This function does not wait if only some of data could be transmitted,
	instead it returns immediately.

	@param data		Pointer to data to be transmitted.
	@param size		Size of data.
	@param timeout  Time in milliseconds to wait if output is not ready.

	@return Number of bytes transmitted or negative error value if failed.

	@pre Initialise() must have been called.
	*/
	virtual int Out(const uint8_t* data, size_t size, unsigned timeout) =0;

	/**
	Receive data.

	@param[out] data	Pointer to buffer to hold received data.
	@param maxSize		Size of data.
	@param timeout		Time in milliseconds to wait if no data available.

	@return Number of bytes received or negative error value if failed.

	@pre Initialise() must have been called.
	*/
	virtual int In(uint8_t* data, size_t maxSize, unsigned timeout) =0;

	/**
	Close port. Port must not be used again until Open() has been called.
	*/
	virtual void Close() =0;

	/**
	Destructor which also performs a Close().
	*/
	virtual ~SerialPort() =0;

	/**
	Enumeration of possible error values.
	*/
	enum Errors
		{
		ErrorUnspecified	= -100,	/**< Unknown error type. */
		ErrorInvalidPort	= -101, /**< Port number specified in Open() is not valid, or port does not exist. */
		ErrorPortInUse		= -102, /**< Port specified in Open() is alread in use. */
		ErrorInvalidSettings= -103, /**< Values specified in Initialise() are not valid for the device. */
		ErrorTransmitError	= -104, /**< An error occured during data transmission by Out(). */
		ErrorReceiveError	= -105  /**< An error occured during data reception by In(). */
		};
	};


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

#endif
