/*
**	$Id: IOSerial.hpp 1.2 1995/08/19 22:34:29 olsen Exp olsen $
**
**	:ts=4
*/

/*
 * Copyright © 1995 by Olaf Barthel, All Rights Reserved
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software has not been validated by the ``Bundesamt fuer Zulassungen in
 * der Telekommunikation'' of the ``Deutsche Bundepost Telekom'' and thus
 * must not be used for accessing the BTX-Network of the Telekom in Germany.
 *
 * Diese Software hat keine Zulassung durch das Bundesamt fuer Zulassungen in
 * der Telekommunikation der Deutschen Bundespost Telekom und darf daher nicht
 * am Netz der Deutschen Bundespost Telekom in Deutschland betrieben werden.
 */

#ifndef _IOSERIAL_HPP
#define _IOSERIAL_HPP 1

#include "IOChannel.hpp"
#include "IORawSerial.hpp"

#include <stdarg.h>
#include <stdio.h>

#define BLOCK_BUFFER_SIZE 3000

class IOSerial : public IOChannel
{
	public:

		IOSerial();
		~IOSerial();

		LONG Open(CONST STRPTR Channel,ULONG Unit=0,ULONG Baud=0,BOOL RTS_CTS=-1);
		VOID Close(VOID);

		LONG GetChar(LONG Timeout=0);
		VOID PutChar(UBYTE Char) { PutString((CONST STRPTR)&Char,1); }
		VOID PutString(CONST STRPTR String,LONG Len=-1);

		LONG GetCharDirect(LONG Timeout=0) { return(Raw -> GetChar(Timeout)); };
		VOID PutCharDirect(UBYTE Char) { Raw -> PutChar(Char); };
		VOID PutStringDirect(CONST STRPTR String,LONG Len=-1) { Raw -> PutString(String,Len); };

		VOID Flush(VOID) { Raw -> Flush(); };

		LONG Waiting(VOID);

		ULONG WaitMask(VOID);

	private:

		int modeminput(LONG Timeout);
		inline void send_TFI();
		inline UWORD getcheck(LONG Timeout);
		inline void calccrc(unsigned int c);
//		void log(char *fmt,...) { va_list args; va_start(args,fmt); vprintf(fmt,args); va_end(args); };
		void log() {};
		LONG getmodem(LONG Timeout);
		int putmodem(int c) { PutCharDirect((UBYTE)c); return 0; };

		IORawSerial *Raw;

		unsigned int	block, bufferlen;
		unsigned int	tfi;
		UBYTE			ack01,status;
		UBYTE			buffer[BLOCK_BUFFER_SIZE];
		UBYTE			c;
		UBYTE			lastchar;
		UWORD			crc;

		LONG			buffered;
		LONG			bufferindex;
};

#endif	// _IOSERIAL_HPP
