/* Davicom.h: Common interface for the all project.                             */
/*                                                                              */
/*//////////////////////////////////////////////////////////////////////////////*/
/*==============================================================================*/
/* Class Name	:                                                               */
/* Purpose      :                                                               */
/* Autor        : Richard Chuang                                                */
/* Email        :                                                               */
/* Description  :                                                               */
/* Reference    :                                                               */
/* Copyright    : Copyright (c) 2000-2007 Davicom Inc.  All rights reserved.    */
/* -----------------------------------------------------------------------------*/
/* Version      : 1.0.1.0                                                       */
/* Update       : 2007-12-18                                                    */
/* Modified     : Richard Chung                                                 */
/* Description	:                                                               */
/* -----------------------------------------------------------------------------*/
/* How to use:                                                                  */
/*    - Enable Davicom Debug Mode                                               */	      
/*      sources(makefile)                                                       */
/*          DAVICOM_DEBUG_MODE declares in CDEFINES that will enable debug mode */
/*          _WIN32_CE_ETHDBG declares in CDEFINES that will enable ETHDBG mode  */
/*            Ex.CDEFINES=$(CDEFINES) -DDAVICOM_DEBUG_MODE -D_WIN32_CE_ETHDBG   */
/*      source code files(c/cpp/h)                                              */
/*          DAVICOM_DEBUG_MODE declares in file that will enable debug mode     */
/*          _WIN32_CE_ETHDBG declares in the file that will enable ETHDBG mode  */
/*            Ex.#define DAVICOM_DEBUG_MODE 	1	// Enable                   */
/*            Ex.#define _WIN32_CE_ETHDBG	    1	// ETHDBG Mode              */
/* PRINTF                                                                       */
/*                                                                              */ 

/* [Use '#' to comment, to get release mode]									*/
/* [# CDEFINES=$(CDEFINES) -DDAVICOM_DEBUG_MODE	]								*/

/*==============================================================================*/
/* Function Name: EdbgOutputDebugString/KITLOutputDebugString                   */
/* void EdbgOutputDebugString(const char *fmt, ...);                            */
/* void KITLOutputDebugString(const char *fmt, ...);                            */
/* -----------------------------------------------------------------------------*/
/* void EdbgOutputDebugString(Print messages to the debug serial port)          */
/*                                                                              */ 
/* This function is provided to allow debugging of the debug Ethernet services. */ 
/* It will format and print a message to the debug serial port, even if debug   */
/* messages (i.e. NKDbgPrintfW) are routed over Ethernet.                       */
/* If the debugger is running over the serial port, the message will be         */
/* formatted such that it shows up in the debugger output window.               */
/* The formatting capabilities of this function are limited to the following    */
/* format characters:                                                           */
/*                                                                              */
/* Various format for printf.                                                   */
/* This was needed because Windows CE version of printf gives me a different    */
/* Set of format strings than those given by standard C compiler.               */
/*                                                                              */
/*               Format string : type                                           */
/*                                                                              */
/*               u : unsigned <nl>                                              */
/*               d : int      <nl>                                              */
/*               c : char     <nl>                                              */
/*               s : string   <nl>                                              */
/*               x : 4-bit hex number  <nl>                                     */
/*               B : 8-bit hex number  <nl>  <== "%02x"                         */
/*               H : 16-bit hex number <nl>  <== "%04x"                         */
/*               X : 32-bit hex number <nl>  <== "%08x"                         */
/*                                                                              */
/* EdbgOutputDebugString() will eventually call the OEMWriteDebugByte()         */
/* function to write characters to the debug serial port.                       */
/*                                                                              */

/*==============================================================================*/
/* Function Name: UCHAR READ_PORT_UCHAR/                                        */
/*                                                                              */
/*                                                                              */
/*                                                                              */
/*==============================================================================*/
/* Function Name: UCHAR READ_PORT_UCHAR                                         */
/* Purpose      : This function reads a byte from the specified port address.   */
/* Description	:                                                               */
/* -----------------------------------------------------------------------------*/
/* Prototype    : UCHAR READ_PORT_UCHAR(PUCHAR Port);                           */
/* Parameters   :                                                               */
/* Port         : [in] Pointer to the port that must be a mapped memory range   */
/*                     in I/O space.                                            */
/* Return Values: Returns the byte read from the specified port address.        */


/*==============================================================================*/
/* Function Name: WRITE_PORT_UCHAR                                              */
/* Purpose      : This function writes a byte to the specified port address.    */
/* Description	:                                                               */
/* -----------------------------------------------------------------------------*/
/* Prototype    : VOID WRITE_PORT_UCHAR(PUCHAR Port, UCHAR Value );             */
/*                                                                              */
/* Parameters   :                                                               */
/* Port         : [in] Pointer to the port that must be a mapped memory range   */
/*              :      in I/O space.                                            */
/* Value        : [in] Byte to be written to the port.                          */
/* Return Values: None.                                                         */


/*==============================================================================*/
/* Function Name: READ_PORT_USHORT                                              */
/* Purpose      : This function reads a USHORT value from the specified port    */
/*                address.                                                      */
/* Description	:                                                               */
/* -----------------------------------------------------------------------------*/
/* Prototype    : USHORT READ_PORT_USHORT(PUSHORT Port );                       */
/* Parameters   :                                                               */
/* Port         : [in] Pointer to the port that must be a mapped memory range   */
/*                     in I/O space.                                            */
/* Return Values: Returns the USHORT value read from the specified port address.*/


/*==============================================================================*/
/* Function Name: WRITE_PORT_USHORT                                             */ 
/* Purpose      : This function writes a USHORT value to the specified port     */
/*                address.                                                      */
/* Description	:                                                               */
/* -----------------------------------------------------------------------------*/
/* Prototype    : VOID WRITE_PORT_USHORT(PUSHORT Port, USHORT Value );          */
/* Parameters   :                                                               */
/* Port         : [in] Pointer to the port that must be a mapped memory range   */
/*                     in I/O space.                                            */
/* Value        : [in] USHORT value to write to the port.                       */
/* Return Values: None.                                                         */


/*==============================================================================*/
/* Function Name: USHORT READ_PORT_USHORT                                       */
/* Purpose      : This function reads a USHORT value from the specified port    */
/*                address.                                                      */
/* Description	:                                                               */
/* -----------------------------------------------------------------------------*/
/* Prototype    : USHORT READ_PORT_USHORT(PUSHORT Port );                       */
/* Parameters   :                                                               */
/* Port         : [in] Pointer to the port that must be a mapped memory range   */
/*              :      in I/O space.                                            */
/* Return Values: Returns the USHORT value read from the specified port address.*/
/*                                                                              */

/*==============================================================================*/
/* Function Name: WRITE_PORT_USHORT                                             */
/* Purpose      : This function writes a USHORT value to the specified port     */
/*              : address.                                                      */
/* Description	:                                                               */
/* -----------------------------------------------------------------------------*/
/* Prototype    : VOID WRITE_PORT_USHORT(PUSHORT Port, USHORT Value );          */
/* Parameters   :                                                               */
/* Port         : [in] Pointer to the port that must be a mapped memory range   */
/*              :      in I/O space.                                            */
/* Value        : [in] USHORT value to write to the port.                       */
/* Return Values: None.                                                         */

#if !defined(_DAVICOM_COMMON_DEFINE_H_)
#define _DAVICOM_COMMON_DEFINE_H_

#if _MSC_VER > 1000
#pragma once
#endif /* _MSC_VER > 1000 */

#ifdef SETFNAME 
#undef SETFNAME
#endif
#ifdef FUNCTION_ENTER_MSG 
#undef FUNCTION_ENTER_MSG
#endif
#ifdef FUNCTION_LEAVE_MSG
#undef FUNCTION_LEAVE_MSG
#endif

/*******************************************************************************
 *
 * Debug macros
 *
 *******************************************************************************/

/*#############################################################################*/
#ifdef _WIN32_WCE
/*#############################################################################*/

/*============================================================================*/
#ifndef _WIN32_CE_ETHDBG /* NDIS Mode (For NDIS Driver Mode) */
/*============================================================================*/

inline int OutputMessageW (TCHAR *pszFormat, ...)
{
	va_list ArgList;
	TCHAR	szBuffer[1024];
	int		nRetVal;

	va_start (ArgList, pszFormat);

	nRetVal = _vsntprintf(szBuffer, sizeof(szBuffer), pszFormat, ArgList);
	szBuffer[1023] = TEXT('\0'); // Force NULL-termination

	NKDbgPrintfW(szBuffer);
	va_end(ArgList);

	return nRetVal;
}

/*
inline int OutputMessageA(LPCSTR lpszFormat, ...)
{
	va_list ArgList;
	va_start(ArgList, lpszFormat);

	int nRetBuf;
	char szBuffer[1024];

	nRetBuf = _vsnprintf(szBuffer, sizeof(szBuffer), lpszFormat, ArgList);
	//ATLASSERT(nRetBuf < sizeof(szBuffer)); //Output truncated as it was > sizeof(szBuffer)
	szBuffer[1023] = '\0'; // Force NULL-termination

	OutputDebugStringA(szBuffer);
	va_end(ArgList);
	return nRetBuf;
}
*/

#define PRINTF  OutputMessageW

/*----------------------------------------------------------------------------*/
#if	DAVICOM_DEBUG_MODE	                                /* Davicom Debug Mode */
/*----------------------------------------------------------------------------*/

#define	HALT(x)	if(x) PUTS("FATAL ERROR\r\n")
#define	BREAK	__asm int 3
/* Output Message Macro */
//#define DBG_DISP_WREG(name,reg,val) NKDbgPrintfW(_T("WR: %02XH(%s),0x%02x \r\n"), reg,REGNameTab[name],val)
#define _RPT_WREG(name,reg,val) NKDbgPrintfW(_T(" WR: %02XH(%s),0x%02x \r\n"), reg,REGNameTab[name],val)
#define	_RPT_RREG(name,reg,val) NKDbgPrintfW(_T(" RD: %02XH(%s),%02xH \r\n"), reg,REGNameTab[name],val)
#define	_CUR_RREG(name,reg,val) NKDbgPrintfW(_T(" Current-RD: %02XH(%s),%02xH \r\n"), reg,REGNameTab[name],val)
#define	_RPT_STR NKDbgPrintfW
#define _RPT_PutChar(c) NKDbgPrintfW(_T("%c"),c)
#define _RPT_NextLine() NKDbgPrintfW(_T("\r\n"))
//#define DBG_DISP_RREG(name,reg,val) NKDbgPrintfW(_T("RD: %02XH(%s),%02xH \r\n"), reg,REGNameTab[name],val)
#define	PUTS(x)	NKDbgPrintfW(TEXT(x))
#define	MSG(x)	NKDbgPrintfW x
//#define	DEBUG_PRINT(msg)	NKDbgPrintfW msg
#define	DEBUG_PRINTF OutputMessageW
/* Function Macro */
#define SETFNAME(name) LPCTSTR pszFuncName = _T(name)
#define DECNAME() LPCTSTR pszFuncName
#define REFNAME(name) pszFuncName = _T(name)
#define FUNCTION_SHRINK_MSG() NKDbgPrintfW(_T("(+)(-)%s\r\n"), pszFuncName)
#define FUNCTION_ENTER_MSG() NKDbgPrintfW(_T("(+)%s\r\n"), pszFuncName)
#define FUNCTION_MDL_MSG(str) NKDbgPrintfW(_T("(.)%s.%s\r\n"), pszFuncName,str)
#define FUNCTION_LEAVE_MSG() NKDbgPrintfW(_T("(-)%s\r\n"), pszFuncName)

/*----------------------------------------------------------------------------*/
#else	                                              /* Davicom Release Mode */
/*----------------------------------------------------------------------------*/

#define	HALT(x)
#define	BREAK

#define _RPT_WREG(name,reg,val) /* _RPT_ Output series */
#define	_RPT_RREG(name,reg,val)
#define	_CUR_RREG(name,reg,val)
#define	_RPT_STR
#define _RPT_PutChar(c)
#define _RPT_NextLine()

#define	PUTS(x)					/* Output Message Macro */
#define	MSG(x)
#define	DEBUG_PRINT(msg)
#define	DEBUG_PRINTF

#define SETFNAME(name)			/* Function Macro */
#define DECNAME()
#define REFNAME(name)
#define FUNCTION_SHRINK_MSG()
#define FUNCTION_ENTER_MSG()
#define FUNCTION_MDL_MSG(str)
#define FUNCTION_LEAVE_MSG()

/*----------------------------------------------------------------------------*/
#endif                                       /* End of #if DAVICOM_DEBUG_MODE */
/*----------------------------------------------------------------------------*/

/*============================================================================*/
#else /* ETHDBG MODE (For Ether Debug Driver Mode)*/
/*============================================================================*/

#include <Kitl.h>

#define PRINTF	KITLOutputDebugString

/*----------------------------------------------------------------------------*/
#if	DAVICOM_DEBUG_MODE	                                /* Davicom Debug Mode */
/*----------------------------------------------------------------------------*/

/* Output Message Macro */
#define DEBUG_PRINTF 			KITLOutputDebugString	/*EdbgOutputDebugString*/
/* Function Macro */
#define SETFNAME(name)			LPCTSTR pszFuncName = _T(name)
#define FUNCTION_ENTER_MSG()	NKDbgPrintfW(_T("(+)%s\r\n"), pszFuncName)
#define FUNCTION_LEAVE_MSG()	NKDbgPrintfW(_T("(-)%s\r\n"), pszFuncName)

/*----------------------------------------------------------------------------*/
#else	                                              /* Davicom Release Mode */
/*----------------------------------------------------------------------------*/

#define	DEBUG_PRINTF
#define SETFNAME(name)
#define FUNCTION_ENTER_MSG()
#define FUNCTION_LEAVE_MSG()

/*----------------------------------------------------------------------------*/
#endif                                       /* End of #if DAVICOM_DEBUG_MODE */
/*----------------------------------------------------------------------------*/

/*============================================================================*/
#endif /* End of #ifndef _WIN32_CE_ETHDBH */
/*============================================================================*/


/*#############################################################################*/
#else /* Linux MODE (For Linux Driver Mode)*/
/*#############################################################################*/


/*----------------------------------------------------------------------------*/
#if	DAVICOM_DEBUG_MODE	                                /* Davicom Debug Mode */
/*----------------------------------------------------------------------------*/
//#define DEBUG_PRINTF 	printf	
//#define SETFNAME(name) LPCTSTR pszFuncName = _T(name)
//#define FUNCTION_ENTER_MSG() NKDbgPrintfW(_T("(+)%s\r\n"), pszFuncName)
//#define FUNCTION_LEAVE_MSG() NKDbgPrintfW(_T("(-)%s\r\n"), pszFuncName)
/*----------------------------------------------------------------------------*/
#else	                                              /* Davicom Release Mode */
/*----------------------------------------------------------------------------*/
#define	DEBUG_PRINTF
#define SETFNAME(name)
#define FUNCTION_ENTER_MSG()
#define FUNCTION_LEAVE_MSG()
/*----------------------------------------------------------------------------*/
#endif                                       /* End of #if DAVICOM_DEBUG_MODE */
/*----------------------------------------------------------------------------*/

/*#############################################################################*/
#endif /* _WIN32_WCE */
/*#############################################################################*/


#endif /* !defined(_DAVICOM_COMMON_DEFINE_H_) */
