/*
 *  Copyright (c) 1996-2008 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
 *
 */

#ifndef	EWBMACRO_H
#define	EWBMACRO_H

#define	EWB_USE_TRECK_BUFFERS TRUE
//#define	EWB_TRACE_ENABLED

#define EWB_MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
#define EWB_MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]

/* for packet buffer flags */
#define	BM_PKTF_USER_DEV_OWNS 0x400

#define bm_global(a) ewbGlobal.a
#define bm_context(a) ewbContext.a

#define BM_PACKET_NULL_PTR (btPacketPtr) 0

extern int ewb_mallocs;
extern int ewb_frees;
extern int ewb_buffergets;
extern int ewb_bufferfrees;

#define EWB_MALLOC(count) malloc(count);ewb_mallocs++;
#define EWB_FREE(pMem) free(pMem); ewb_frees++;

#ifdef	EWB_TRACE_ENABLED
#define DPRINTF(...) trace(EWB_TRACE, TRACE_DEBUG, __VA_ARGS__)
#define XPRINTF(level, ...) trace(EWB_TRACE, level, __VA_ARGS__)
#define	ASSERT(x)
#else
#ifdef DEBUG
#define	DPRINTF				printf
#define XPRINTF(...)
#define	ASSERT(x)			{ if (!(x)) assert_fail (#x, __FILE__, __LINE__); }
#else
#define XPRINTF(...)
#define	DPRINTF(...)
#define	ASSERT(x)
#endif	/* not DEBUG */
#endif	/* not EWB_TRACE_ENABLED */

/*
 * The following macros are endian-agnostic and alignment-agnostic
 * Digi macro versions courtesy of Charles Gordon (18 September 2007).
 */

/* Check that 2 Ethernet Addresses match */
#define bm_ether_match(eth1, eth2) \
    (    ( ( ((bt8BitPtr)(eth1))[0] ) == ( ((bt8BitPtr)(eth2))[0] ) )     \
      && ( ( ((bt8BitPtr)(eth1))[1] ) == ( ((bt8BitPtr)(eth2))[1] ) )     \
      && ( ( ((bt8BitPtr)(eth1))[2] ) == ( ((bt8BitPtr)(eth2))[2] ) )     \
      && ( ( ((bt8BitPtr)(eth1))[3] ) == ( ((bt8BitPtr)(eth2))[3] ) )     \
      && ( ( ((bt8BitPtr)(eth1))[4] ) == ( ((bt8BitPtr)(eth2))[4] ) )     \
      && ( ( ((bt8BitPtr)(eth1))[5] ) == ( ((bt8BitPtr)(eth2))[5] ) ) )

/* Check that an Ethernet Addresses is broadcast */
#define bm_ether_broadcast(eth) \
    (    ( ( ((bt8BitPtr)(eth))[0] )  ==   (bt8Bit)0xFF )                 \
      && ( ( ((bt8BitPtr)(eth))[1] )  ==   (bt8Bit)0xFF )                 \
      && ( ( ((bt8BitPtr)(eth))[2] )  ==   (bt8Bit)0xFF )                 \
      && ( ( ((bt8BitPtr)(eth))[3] )  ==   (bt8Bit)0xFF )                 \
      && ( ( ((bt8BitPtr)(eth))[4] )  ==   (bt8Bit)0xFF )                 \
      && ( ( ((bt8BitPtr)(eth))[5] )  ==   (bt8Bit)0xFF ) )

/* Check that an Ethernet Addresses is multicast */
#define bm_4_ether_mcast(eth) \
    (    ( ((bt8BitPtr)(eth))[0] == (bt8Bit)0x01 )                        \
      && ( ((bt8BitPtr)(eth))[1] == (bt8Bit)0x00 )                        \
      && ( ((bt8BitPtr)(eth))[2] == (bt8Bit)0x5E ) )

#define bm_ether_mcast(eth) bm_4_ether_mcast(eth) 

#define bm_6_ether_mcast(eth) \
    (    ( ((bt8BitPtr)(eth))[0] == (bt8Bit)0x33 )                        \
      && ( ((bt8BitPtr)(eth))[1] == (bt8Bit)0x33 ) )

/* Copy Ethernet address from to */
#define bm_ether_copy(from, to) \
    { \
        ((bt8BitPtr)(to))[0] = ((bt8BitPtr)(from))[0];                    \
        ((bt8BitPtr)(to))[1] = ((bt8BitPtr)(from))[1];                    \
        ((bt8BitPtr)(to))[2] = ((bt8BitPtr)(from))[2];                    \
        ((bt8BitPtr)(to))[3] = ((bt8BitPtr)(from))[3];                    \
        ((bt8BitPtr)(to))[4] = ((bt8BitPtr)(from))[4];                    \
        ((bt8BitPtr)(to))[5] = ((bt8BitPtr)(from))[5];                    \
    }

#ifdef TM_LITTLE_ENDIAN

/* Ethernet type constant for IP packets in network byte order */
#define TM_ETHER_IP_TYPE            0x0008
/* Ethernet type for ARP packets, constant in network byte order */
#define TM_ETHER_ARP_TYPE           0x0608

#else /* TM_BIG_ENDIAN */

/* Ethernet type constant for IP packets in network byte order */
#define TM_ETHER_IP_TYPE            0x0800
/* Ethernet type for ARP packets, constant in network byte order */
#define TM_ETHER_ARP_TYPE           0x0806

#endif /*TM_LITTLE_ENDIAN */

#endif /* EWBMACRO_H */
