/*

Copyright (c) 2000-2001  BBC Technology Ltd

Module Name:

    com32.h

Abstract:

    Class definitions for serial port library

Author:

    Tim Alden (tim.alden@bbc.co.uk)   8-Jan-2001

Sevision History:

  v1.00			9th January 2001
  v1.01			1st February 2001	Added return value to notify
  v1.02			26 March 2001	Added ConfigDialog and Flushing, set and get commstate
  v1.03			28 March 2001	Moved port allocation to open() fro constructor
								Default params are 9600,8,N,1
  v1.04			4 April 2001	Fixed bug in received data function - it didn't get put in the
								RX buffer unlesw Notify was set
  v1.05			25 June 2001	Class will return 
  v1.06			20 Nov 2001		Added COMMSDLL_API MACRO for DLL version
  v1.09			23 July 2002	Added overload to txdata for variable argwment list
  v1.10			7 August 2002	Added DeviceIoControl
								Added overload to open for named device rather than numeric COM port
  v1.11							Subtle change to Notify thing: moved variable assignment of timeout away from declaration in case 
								there is an issue with the order, and miscalculation of the timeout period.
  v1.12							Fixed transmit handle bug for Daly

  v1.1x							AMW Fixed function return types for use with VS 2013.

  NOTE: for VS2013 need to set linker option /SAFESEH:NO as teh library is from an older compiler and the safe handler data is not present.

*/



#ifndef COMMSDLL_API
#define COMMSDLL_API
#endif

///////////////////////////
// SERIAL PORT CONNECTIVITY
///////////////////////////
class COMMSDLL_API serialport
{
private:
	HANDLE hCom;
	int iPort;
	long lBRate;
	int iDB,iPB,iSB;
	BOOL fPortState;

	//added extra function for DeviceIoControl
/*	int iDynamicType;	// flag for type of port (e.g. failover capable)
	HANDLE hDeviceCom;	// driver for virtual ports (e.g. Comtrol)
	int freeport(int iPortNum);*/

public:
	serialport();								// Default constructor
	~serialport();								// Default destructor
	void baudrate(long lBaud);					// Set the serial bit rate
	void databits(int iDatabits);				// Set the number of databits
	void paritybits(int iParity);				// Set the parity type - Symbolic constants:
												// EVENPARITY, MARKPARITY, NOPARITY, ODDPARITY, SPACEPARITY
	void stopbits(int iStopbits);				// Set the number of stopbits. Symbolic Constants
												// ONESTOPBIT, ONE5STOPBITS, TWOSTOPBITS
	int port();									// Return the port number used by this instance??
	int open(UINT iPortNum);					// iPortNum is the com port to open. Returns -1 (no such port) or -2 (already open) on failure
	void state (LPCOMSTAT,LPDWORD);				// LPCOMSTAT is a long pointer to a Comms Status structure.												// GUESS: LPDWORD = Read or Write?
	void close();								// Close this instance of the comms port
	void txdata(PBYTE bData,long lLen);			// Send lLen bytes of data from byte buffer bData
	void txdata(LPCSTR szFmt, ...);				// Send a zero teminated string. Can use formatting of printf/sprintf
	BOOL notify(void(*)(serialport*),UINT);		// Define a callback function, and the number of bytes at which to notify.
	BOOL notify(HWND hWndNot,UINT iNot);		// Define window procedure that will receive WM_COMMNOTIFY messages.  
												// Parameter hWndNot is window handle, parameter iNot is number of bytes expected in a message
												// wParam has serial port identifier from open()
												// lParam has low 16 bits undefined, high 16 bits have number of bytes available.
	long rxdata(PBYTE bData,long lLen);			// Fetch a maximum of lLen bytes from receive buffer into byte buffer bData.
												// Returns the actual number of bytes transferred.
	BOOL flushbuf(ULONG flags);					// Parameter flags is one of:
												// PURGE_TXABORT - terminates all write operations immediately
												// PURGE_RXABORT - terminates all read operations immediately
												// PURGE_TXCLEAR - Clears the output buffer
												// PURGE_RXCLEAR - Clears the input buffer
	BOOL configdialog(HWND hWnd);				// Use interactive dialogue form to choose comms properties on already open port
	BOOL getdcb(DCB*);							// Get the device control block from the port
	BOOL setdcb(DCB*);							// Set teh Device Control Block values to the port

/*	// functions to find status of port and force it
	int setredstate(int iRedState-;
	BOOL configioctl(	DWORD dwIoControlCode,       // operation
						LPVOID lpInBuffer,           // input data buffer
						DWORD nInBufferSize,         // size of input data buffer
						LPVOID lpOutBuffer,          // output data buffer
						DWORD nOutBufferSize,        // size of output data buffer
						LPDWORD lpBytesReturned)?    // byte count*/

};



////////////////////////////////
// CIRCULAR BUFFER FUNCTIONALITY
////////////////////////////////
class COMMSDLL_API circbuf
{
	HANDLE hGBuf;
public:											// Buffers are 16kBytes
	circbuf(UINT iTok);							// iTok is a programmer defined unique token for buffer semaphore. Ensures correct operation in multi-threaded.
	circbuf(void);								// Generates a random token
	~circbuf();									// Destructor
	BOOL bAdd(BYTE* bData, UINT iLen);			// Add iLen bytes of data from bData to circular buffer. TRUE if OK, FALSE if buffer does not exist
	UINT bGet(BYTE* bData, UINT iLen);			// Get iLen bytes from buffer to bData. returns number of bytes actually read
	BOOL bAddEx(BYTE* bData, UINT iLen);		// ???
	UINT bGetEx(BYTE* bData, UINT iMaxLen);		// ???
};
