/** * \file * * * \author Andrea Righi * * \brief Ethernet standard descriptors * * $WIZ$ module_name = "eth" * $WIZ$ module_configuration = "bertos/cfg/cfg_eth.h" * $WIZ$ module_supports = "at91sam7x or sam3x" * $WIZ$ module_hw = "bertos/hw/hw_eth.h", "bertos/hw/hw_eth.c" */ #ifndef DRV_ETH_H #define DRV_ETH_H #include "hw/hw_eth.h" #include #define ETH_ADDR_LEN 6 #define ETH_HEAD_LEN 14 #define ETH_DATA_LEN 1500 #define ETH_FRAME_LEN (ETH_HEAD_LEN + ETH_DATA_LEN) #define ETH_TYPE_IP 0x0800 typedef union Ethernet { struct { uint8_t dst[ETH_ADDR_LEN]; uint8_t src[ETH_ADDR_LEN]; uint16_t type; uint8_t data[0]; }; uint8_t raw[ETH_FRAME_LEN]; } PACKED Ethernet; /** * Determine if ethernet address \a addr is a all zero. */ INLINE int eth_addrIsZero(const uint8_t *addr) { return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]); } /** * Determine if ethernet address \a addr is a multicast address. */ INLINE int eth_addrIsMcast(const uint8_t *addr) { return (0x01 & addr[0]); } /** * Determine if ethernet address \a addr is locally-assigned (IEEE 802). */ INLINE int eth_addrIsLocal(const uint8_t *addr) { return (0x02 & addr[0]); } /** * Determine if ethernet address \a addr is broadcast. */ INLINE bool eth_addrIsBcast(const uint8_t *addr) { return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff; } /** * Check if the ethernet address \a addr is not all zero, is not a multicast * address, and is not broadcast. */ INLINE bool eth_addrIsValid(const uint8_t *addr) { return !eth_addrIsMcast(addr) && !eth_addrIsZero(addr); } /** * Compare two ethernet addresses: \a addr1 and \a addr2, returns 0 if equal. */ INLINE bool eth_addrCmp(const uint8_t *addr1, const uint8_t *addr2) { return !!((addr1[0] ^ addr2[0]) | (addr1[1] ^ addr2[1]) | (addr1[2] ^ addr2[2]) | (addr1[3] ^ addr2[3]) | (addr1[4] ^ addr2[4]) | (addr1[5] ^ addr2[5])); } ssize_t eth_putFrame(const uint8_t *buf, size_t len); void eth_sendFrame(void); size_t eth_getFrameLen(void); ssize_t eth_getFrame(uint8_t *buf, size_t len); ssize_t eth_send(const uint8_t *buf, size_t len); ssize_t eth_recv(uint8_t *buf, size_t len); int eth_init(void); #endif /* DRV_ETH_H */