/*	Copyright  (c)	Günter Woigk 2016 - 2016
					mailto:kio@little-bat.de

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	Permission to use, copy, modify, distribute, and sell this software and
	its documentation for any purpose is hereby granted without fee, provided
	that the above copyright notice appear in all copies and that both that
	copyright notice and this permission notice appear in supporting
	documentation, and that the name of the copyright holder not be used
	in advertising or publicity pertaining to distribution of the software
	without specific, written prior permission.  The copyright holder makes no
	representations about the suitability of this software for any purpose.
	It is provided "as is" without express or implied warranty.

	THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
	INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
	EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
	CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
	DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
	TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
	PERFORMANCE OF THIS SOFTWARE.
*/


#ifndef STREAM_H
#define STREAM_H
#include "kio/kio.h"
#include "IOPort.h"


class Stream
{
public:
	uchar	bu[1<<10];
	uint	i = 0;				// read index
	uint	e = 0;				// end of available data

	// Transmission mode:
	enum { plain_transmission, escaped_transmission, ec_transmission };
	uint	transmission_mode	= plain_transmission;
	bool	software_handshake	= false;		// requires escaped or ec transmission
	uint	max_streams			= MAX_STREAMS;	// max. logical streams
	uint	ec_windowsize		= 4;			// 2 or 4 (default)
	uint	ec_maxblocksize		= 256;			// 64, 128 or 256 (default)

	IOPort*	ioport  = NULL;

	// I/O Port Settings:
	bool	server_selected	= no;			// ip socket server in use?
	uint	socket_port		= 20042;		// if server_selected: port in use
	bool	pipe_selected	= no;			// named pipes in use?
	cstr	pipe_path		= "/tmp/GTermPipe";	// if pipe_selected: file system path
	bool	stdio_selected	= no;			// stdin/stdout in use?
	int		selected_sio	= -1;			// serial port in use: idx in sio_infos, -1 = none
	uint	sio_baudrate	= 19200;		// if selected_sio: baudrate in use. always 8n1

	uint	ostream = 0;		// currently selected logical stream in output stream
	uint	istream = 0;		// currently selected logical stream in input stream

public:
	Stream	()					{}

	void	reset();
	void	flushSio();

	void	setPlainTransmission();
	void	setEscapedTransmission(bool swhsk, uint max_streams);
	void	setECTransmission	(bool swhsk, uint max_streams, uint ec_windowsize, uint ec_maxblocksize);

	void	write(uchar* data, uint cnt, uint stream=999/*istream*/);
	void	write(cstr, uint stream=999);

	uint	readByte()			throw(uint)		{ return next_byte(); }
	uint	readUWord()			throw(uint);
	uint	readLong()			throw(uint);
	int		readSWord()			throw(uint);
	void	readBytes(uchar*, uint n)	throw(uint);
	uint	readAvail(uchar*, uint n);
	uint	readAvail(uchar*, uint n, bool(*)(uchar));

	void	setInputStream(uint s)	{ istream = s; }

private:
	uint	next_ctl()			throw(uint);
	uint	next_byte()			throw(uint);

	void	read_raw();			// read more bytes from ioport
	void	handle_ack()		throw(uint);
	void	handle_nak()		throw(uint);
};





#endif // STREAM_H
