Sergey Pastor / 1

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ethernet.h Source File

ethernet.h

Go to the documentation of this file.
00001 /**
00002  * @file ethernet.h
00003  * @brief Ethernet
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 #ifndef _ETHERNET_H
00030 #define _ETHERNET_H
00031 
00032 //Dependencies
00033 #include "core/net.h"
00034 
00035 //Ethernet support
00036 #ifndef ETH_SUPPORT
00037    #define ETH_SUPPORT ENABLED
00038 #elif (ETH_SUPPORT != ENABLED && ETH_SUPPORT != DISABLED)
00039    #error ETH_SUPPORT parameter is not valid
00040 #endif
00041 
00042 //Size of the multicast MAC filter
00043 #ifndef MAC_MULTICAST_FILTER_SIZE
00044    #define MAC_MULTICAST_FILTER_SIZE 12
00045 #elif (MAC_MULTICAST_FILTER_SIZE < 1)
00046    #error MAC_MULTICAST_FILTER_SIZE parameter is not valid
00047 #endif
00048 
00049 //CRC32 calculation using a pre-calculated lookup table
00050 #ifndef ETH_FAST_CRC_SUPPORT
00051    #define ETH_FAST_CRC_SUPPORT DISABLED
00052 #elif (ETH_FAST_CRC_SUPPORT != ENABLED && ETH_FAST_CRC_SUPPORT != DISABLED)
00053    #error ETH_FAST_CRC_SUPPORT parameter is not valid
00054 #endif
00055 
00056 //Minimum Ethernet frame size
00057 #define ETH_MIN_FRAME_SIZE 64
00058 //Maximum Ethernet frame size
00059 #define ETH_MAX_FRAME_SIZE 1518
00060 //Ethernet maximum transmission unit
00061 #define ETH_MTU 1500
00062 //Ethernet CRC field size
00063 #define ETH_CRC_SIZE 4
00064 
00065 //Copy MAC address
00066 #define macCopyAddr(destMacAddr, srcMacAddr) memcpy(destMacAddr, srcMacAddr, sizeof(MacAddr))
00067 
00068 //Compare MAC addresses
00069 #define macCompAddr(macAddr1, macAddr2) (!memcmp(macAddr1, macAddr2, sizeof(MacAddr)))
00070 
00071 //Determine whether a MAC address is a group address
00072 #define macIsMulticastAddr(macAddr) ((macAddr)->b[0] & 0x01)
00073 
00074 //Copy EUI-64 address
00075 #define eui64CopyAddr(destEui64Addr, srcEui64Addr) memcpy(destEui64Addr, srcEui64Addr, sizeof(Eui64))
00076 
00077 //Compare EUI-64 addresses
00078 #define eui64CompAddr(eui64Addr1, eui64Addr2) (!memcmp(eui64Addr1, eui64Addr2, sizeof(Eui64)))
00079 
00080 
00081 /**
00082  * @brief MAC address flags
00083  **/
00084 
00085 typedef enum
00086 {
00087    MAC_ADDR_FLAG_MULTICAST = 0x01,
00088    MAC_ADDR_FLAG_LOCAL     = 0x02
00089 } MacAddrFlags;
00090 
00091 
00092 /**
00093  * @brief Ethernet Type field
00094  **/
00095 
00096 typedef enum
00097 {
00098    ETH_TYPE_IPV4 = 0x0800,
00099    ETH_TYPE_ARP  = 0x0806,
00100    ETH_TYPE_RARP = 0x8035,
00101    ETH_TYPE_IPV6 = 0x86DD
00102 } EthType;
00103 
00104 
00105 //CodeWarrior or Win32 compiler?
00106 #if defined(__CWCC__) || defined(_WIN32)
00107    #pragma pack(push, 1)
00108 #endif
00109 
00110 
00111 /**
00112  * @brief MAC address
00113  **/
00114 
00115 typedef __start_packed struct
00116 {
00117    __start_packed union
00118    {
00119       uint8_t b[6];
00120       uint16_t w[3];
00121    };
00122 } __end_packed MacAddr;
00123 
00124 
00125 /**
00126  * @brief EUI-64 identifier
00127  **/
00128 
00129 typedef __start_packed struct
00130 {
00131    __start_packed union
00132    {
00133       uint8_t b[8];
00134       uint16_t w[4];
00135       uint32_t dw[2];
00136    };
00137 } __end_packed Eui64;
00138 
00139 
00140 /**
00141  * @brief Ethernet frame header
00142  **/
00143 
00144 typedef __start_packed struct
00145 {
00146    MacAddr destAddr; //0-5
00147    MacAddr srcAddr;  //6-11
00148    uint16_t type;    //12-13
00149    uint8_t data[];   //14
00150 } __end_packed EthHeader;
00151 
00152 
00153 //CodeWarrior or Win32 compiler?
00154 #if defined(__CWCC__) || defined(_WIN32)
00155    #pragma pack(pop)
00156 #endif
00157 
00158 
00159 /**
00160  * @brief MAC filter table entry
00161  **/
00162 
00163 typedef struct
00164 {
00165    MacAddr addr;    ///<MAC address
00166    uint_t refCount; ///<Reference count for the current entry
00167    bool_t addFlag;
00168    bool_t deleteFlag;
00169 } MacFilterEntry;
00170 
00171 
00172 //Ethernet related constants
00173 extern const MacAddr MAC_UNSPECIFIED_ADDR;
00174 extern const MacAddr MAC_BROADCAST_ADDR;
00175 extern const Eui64 EUI64_UNSPECIFIED_ADDR;
00176 extern const uint8_t ethPadding[64];
00177 
00178 //Ethernet related functions
00179 error_t ethInit(NetInterface *interface);
00180 
00181 void ethProcessFrame(NetInterface *interface, EthHeader *ethFrame, size_t length);
00182 
00183 error_t ethSendFrame(NetInterface *interface, const MacAddr *destAddr,
00184    NetBuffer *buffer, size_t offset, uint16_t type);
00185 
00186 error_t ethCheckDestAddr(NetInterface *interface, const MacAddr *macAddr);
00187 error_t ethAcceptMulticastAddr(NetInterface *interface, const MacAddr *macAddr);
00188 error_t ethDropMulticastAddr(NetInterface *interface, const MacAddr *macAddr);
00189 
00190 uint32_t ethCalcCrc(const void *data, size_t length);
00191 uint32_t ethCalcCrcEx(const NetBuffer *buffer, size_t offset, size_t length);
00192 
00193 NetBuffer *ethAllocBuffer(size_t length, size_t *offset);
00194 
00195 error_t macStringToAddr(const char_t *str, MacAddr *macAddr);
00196 char_t *macAddrToString(const MacAddr *macAddr, char_t *str);
00197 
00198 error_t eui64StringToAddr(const char_t *str, Eui64 *eui64);
00199 char_t *eui64AddrToString(const Eui64 *eui64, char_t *str);
00200 
00201 void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId);
00202 
00203 void ethDumpHeader(const EthHeader *ethHeader);
00204 
00205 #endif
00206