/**
 * \file endian.h
 *
 * \brief Provides conversions between endianess.
 *
 * The code is copied and modified from 
 * http://www.netrino.com/Publications/Glossary/Endianness.html
 * 
 * Brown, Christopher and Michael Barr. "Introduction to Endianness" 
 * Embedded Systems Programming, January 2002 , pp. 55-56.
 */

#define USE_LITTLE_ENDIAN

#if defined(USE_BIG_ENDIAN) && !defined(USE_LITTLE_ENDIAN)
	#define htons(A) (A)
	#define htonl(A) (A)
	#define ntohs(A) (A)
	#define ntohl(A) (A)
#elif defined(USE_LITTLE_ENDIAN) && !defined(USE_BIG_ENDIAN)
    #define htons(A) ((((uint16_t)(A) & 0xff00) >> 8) | \
            (((uint16_t)(A) & 0x00ff) << 8))
    #define htonl(A) ((((uint32_t)(A) & 0xff000000) >> 24) | \
            (((uint32_t)(A) & 0x00ff0000) >> 8)  | \
            (((uint32_t)(A) & 0x0000ff00) << 8)  | \
            (((uint32_t)(A) & 0x000000ff) << 24))

    #define ntohs  htons
    #define ntohl  htonl
#else
    #error "Must define one of USE_BIG_ENDIAN or USE_LITTLE_ENDIAN"
#endif
