/*++
*******************************************************************************
* Copyright (c) 1999-2000 Digi International Inc., All Rights Reserved        *
*                                                                             *
* This software contains proprietary and confidential information of Digi     *
* International Inc.  By accepting transfer of this copy, Recipient agrees    *
* to retain this software in confidence, to prevent disclosure to others,     *
* and to make no use of this software other than that for which it was        *
* delivered.  This is an unpublished copyrighted work of Digi International   *
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly   *
* prohibited.                                                                 *
*                                                                             *
* Restricted Rights Legend                                                    *
*                                                                             *
* Use, duplication, or disclosure by the Government is subject to             *
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in         *
* Technical Data and Computer Software clause at DFARS 252.227-7031 or        *
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -          *
* Restricted Rights at 48 CFR 52.227-19, as applicable.                       *
*                                                                             *
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343          *
*                                                                             *
*******************************************************************************

Module Name:

	tracer.h

Abstract:

	Trace functionality.

--*/

// IOCTL Timeout defines
#define IOCTL_TIMEOUT	1000	// One Second

const int c_MaxControlLength = 512;
const int c_MaxDataLength = 3072;

// only ioctl class that uses USER_GETMSG
class CSXB_USER_GETMSG : public CSXB_IOCTL
{
public :
	OVERLAPPED Ol;
	DS_GETMSG GetMsg;
	char ControlBuffer[c_MaxControlLength];
	char DataBuffer[c_MaxDataLength];

public :
	CSXB_USER_GETMSG(PVOID);
	~CSXB_USER_GETMSG();

// OVERLAPPED structure is self contained
	BOOL DeviceIoControl(HANDLE File)
		{ return CSXB_IOCTL::DeviceIoControl(File, &Ol); };

protected:
	virtual ULONG GetIOCTLCmd() const { return (ULONG)IOCTL_DS_GETMSG; };

	virtual LPVOID GetIOCTLData() const { return (LPVOID)&GetMsg; };
	virtual DWORD GetIOCTLSize() const	{ return sizeof(DS_GETMSG); };

// pointers send down are the same as result pointers
	virtual LPVOID GetResultData() const { return GetIOCTLData(); };
	virtual DWORD GetResultDataSize() const { return GetIOCTLSize(); };
};
typedef CSXB_USER_GETMSG *PCQUEUE_INFO;

enum eSwitch
{
	sOff,
	sOn
};

// USER_IOCTL's send down for each protocol to start\stop trace
class CFRTraceSwitch : public CStat<FRSTRACE_Enable_struct>
{
public :
	CFRTraceSwitch(DWORD Snid, eSwitch Pos = sOn) : 
		CStat<FRSTRACE_Enable_struct>(Pos == sOff ? FRI_FRSTRACE_Disable : FRI_FRSTRACE_Enable)
		{ snid = Snid; }
};

// USER_IOCTL's send down for each protocol to start\stop trace
class CLAPBTraceSwitch : public CStat<trc_regioc>
{
public :
	CLAPBTraceSwitch(uint32 SNID, eSwitch Pos = sOn) : 
		CStat<trc_regioc>(Pos == sOff ? L_TRACEOFF : L_TRACEON)
		{ snid = SNID; }
};

// USER_IOCTL's send down for each protocol to start\stop trace
class CX25TraceSwitch : public CStat<trc_regioc>
{
public :
	CX25TraceSwitch(uint32 SNID, eSwitch Pos = sOn) : 
		CStat<trc_regioc>(Pos == sOff ? N_traceoff : N_traceon)
		{ snid = SNID; }
};

class CDataSource;

// main tracer class must override to use
class CTracer
{
private:
// thread routine, accesses private data
	static UINT __cdecl PrintfThread(LPVOID);

	HANDLE m_hFile; // file handle to driver
	HANDLE m_VCObject; // stream object
	CSOpen *SOpen;	// Open IOCTL Storage
	BOOL	m_bDeviceOpened; // Was the Device Opened

	DWORD m_Fail; // fail state of trace

	CWinThread *m_Thread; // MFC thread class

// FILO queue of available ioctl's
	CArray<PCQUEUE_INFO, PCQUEUE_INFO> m_Query;
// FILO queue of ioctl's sent down to driver
	CArray<PCQUEUE_INFO, PCQUEUE_INFO> m_Process;

// number of ioctl to allocate
// more allocations do not occur
// a ioctl is in either m_Query or m_Process
	static int m_EventSize;

protected :
// which line to trace on
	DWORD m_Snid;

// frames are processes here
	CDataSource *m_DataSource;

// PrintfThread waits on this event to start
	CEvent m_PrintfQueueEvent;

// Trace IOCTLS.  These memory needs to be accessible until the IOCTL
// completes.  In the case of a Timeout the IOCTL could finish later, so
// the memory needs to be available until the device is closed.  Any error
// condition will cause the user of the class to call the destructor which
// closes the device.
	CSXB_USER_IOCTL		*m_SwitchOn;
	CSXB_USER_IOCTL		*m_SwitchOff;
	
public :
	virtual ~CTracer();

	// Attributes

// check to see if trace starts or is still running
	BOOL Fail() const { return m_Fail != ERROR_SUCCESS; };

	// Operations
	void Suspend(); // stop getting\processing frames
	void Resume(); // restart processing

protected :
	CTracer(CDataSource *, eSourceType);

	// Operations
	void PumpTrace();
	void FinishPump();

	BOOL ThrowSwitch(CSXB_USER_IOCTL *, eSwitch);
	BOOL DeviceIoControl(CSXB_IOCTL *,int iTimeout);
};

// Frame Relay trace class
class CFRTracer : public CTracer
{
public :
	CFRTracer(CDataSource *);
	virtual ~CFRTracer();
};

// LAPB trace class
class CLAPBTracer : public CTracer
{
public :
	CLAPBTracer(CDataSource *);
	virtual ~CLAPBTracer();
};

// X.25 trace class
class CX25Tracer : public CTracer
{
public :
	CX25Tracer(CDataSource *);
	virtual ~CX25Tracer();
};

// SNA trace class
class CSdlcTracer : public CTracer
{
public :
	CSdlcTracer(CDataSource *);
	virtual ~CSdlcTracer();
};

class CHdlcTracer : public CTracer
{
public:
	CHdlcTracer(CDataSource *);
	virtual ~CHdlcTracer();
};
