Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Email2Screen by
ethernetif.c
00001 /** 00002 * @file 00003 * Ethernet Interface Skeleton 00004 * 00005 */ 00006 00007 /* 00008 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00009 * All rights reserved. 00010 * 00011 * Redistribution and use in source and binary forms, with or without modification, 00012 * are permitted provided that the following conditions are met: 00013 * 00014 * 1. Redistributions of source code must retain the above copyright notice, 00015 * this list of conditions and the following disclaimer. 00016 * 2. Redistributions in binary form must reproduce the above copyright notice, 00017 * this list of conditions and the following disclaimer in the documentation 00018 * and/or other materials provided with the distribution. 00019 * 3. The name of the author may not be used to endorse or promote products 00020 * derived from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00023 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00025 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00026 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00027 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00028 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00029 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00030 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00031 * OF SUCH DAMAGE. 00032 * 00033 * This file is part of the lwIP TCP/IP stack. 00034 * 00035 * Author: Adam Dunkels <adam@sics.se> 00036 * 00037 */ 00038 00039 /* 00040 * This file is a skeleton for developing Ethernet network interface 00041 * drivers for lwIP. Add code to the low_level functions and do a 00042 * search-and-replace for the word "ethernetif" to replace it with 00043 * something that better describes your network interface. 00044 */ 00045 00046 #include "lwip/opt.h" 00047 00048 #if 0 /* don't build, this is only a skeleton, see previous comment */ 00049 00050 #include "lwip/def.h" 00051 #include "lwip/mem.h" 00052 #include "lwip/pbuf.h" 00053 #include "lwip/sys.h" 00054 #include <lwip/stats.h> 00055 #include <lwip/snmp.h> 00056 #include "netif/etharp.h" 00057 #include "netif/ppp_oe.h" 00058 00059 /* Define those to better describe your network interface. */ 00060 #define IFNAME0 'e' 00061 #define IFNAME1 'n' 00062 00063 /** 00064 * Helper struct to hold private data used to operate your ethernet interface. 00065 * Keeping the ethernet address of the MAC in this struct is not necessary 00066 * as it is already kept in the struct netif. 00067 * But this is only an example, anyway... 00068 */ 00069 struct ethernetif { 00070 struct eth_addr *ethaddr; 00071 /* Add whatever per-interface state that is needed here. */ 00072 }; 00073 00074 /* Forward declarations. */ 00075 static void ethernetif_input(struct netif *netif); 00076 00077 /** 00078 * In this function, the hardware should be initialized. 00079 * Called from ethernetif_init(). 00080 * 00081 * @param netif the already initialized lwip network interface structure 00082 * for this ethernetif 00083 */ 00084 static void 00085 low_level_init(struct netif *netif) 00086 { 00087 struct ethernetif *ethernetif = netif->state; 00088 00089 /* set MAC hardware address length */ 00090 netif->hwaddr_len = ETHARP_HWADDR_LEN; 00091 00092 /* set MAC hardware address */ 00093 netif->hwaddr[0] = 8C; 00094 netif->hwaddr[1] = 70; 00095 netif->hwaddr[2] = 5a; 00096 netif->hwaddr[3] = d4; 00097 netif->hwaddr[4] = 96; 00098 ... 00099 netif->hwaddr[5] = c4; 00100 00101 /* maximum transfer unit */ 00102 netif->mtu = 1500; 00103 00104 /* device capabilities */ 00105 /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ 00106 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; 00107 00108 /* Do whatever else is needed to initialize interface. */ 00109 } 00110 00111 /** 00112 * This function should do the actual transmission of the packet. The packet is 00113 * contained in the pbuf that is passed to the function. This pbuf 00114 * might be chained. 00115 * 00116 * @param netif the lwip network interface structure for this ethernetif 00117 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type) 00118 * @return ERR_OK if the packet could be sent 00119 * an err_t value if the packet couldn't be sent 00120 * 00121 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to 00122 * strange results. You might consider waiting for space in the DMA queue 00123 * to become availale since the stack doesn't retry to send a packet 00124 * dropped because of memory failure (except for the TCP timers). 00125 */ 00126 00127 static err_t 00128 low_level_output(struct netif *netif, struct pbuf *p) 00129 { 00130 struct ethernetif *ethernetif = netif->state; 00131 struct pbuf *q; 00132 00133 initiate transfer(); 00134 00135 #if ETH_PAD_SIZE 00136 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ 00137 #endif 00138 00139 for(q = p; q != NULL; q = q->next) { 00140 /* Send the data from the pbuf to the interface, one pbuf at a 00141 time. The size of the data in each pbuf is kept in the ->len 00142 variable. */ 00143 send data from(q->payload, q->len); 00144 } 00145 00146 signal that packet should be sent(); 00147 00148 #if ETH_PAD_SIZE 00149 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ 00150 #endif 00151 00152 LINK_STATS_INC(link.xmit); 00153 00154 return ERR_OK; 00155 } 00156 00157 /** 00158 * Should allocate a pbuf and transfer the bytes of the incoming 00159 * packet from the interface into the pbuf. 00160 * 00161 * @param netif the lwip network interface structure for this ethernetif 00162 * @return a pbuf filled with the received packet (including MAC header) 00163 * NULL on memory error 00164 */ 00165 static struct pbuf * 00166 low_level_input(struct netif *netif) 00167 { 00168 struct ethernetif *ethernetif = netif->state; 00169 struct pbuf *p, *q; 00170 u16_t len; 00171 00172 /* Obtain the size of the packet and put it into the "len" 00173 variable. */ 00174 len = ; 00175 00176 #if ETH_PAD_SIZE 00177 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */ 00178 #endif 00179 00180 /* We allocate a pbuf chain of pbufs from the pool. */ 00181 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); 00182 00183 if (p != NULL) { 00184 00185 #if ETH_PAD_SIZE 00186 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ 00187 #endif 00188 00189 /* We iterate over the pbuf chain until we have read the entire 00190 * packet into the pbuf. */ 00191 for(q = p; q != NULL; q = q->next) { 00192 /* Read enough bytes to fill this pbuf in the chain. The 00193 * available data in the pbuf is given by the q->len 00194 * variable. 00195 * This does not necessarily have to be a memcpy, you can also preallocate 00196 * pbufs for a DMA-enabled MAC and after receiving truncate it to the 00197 * actually received size. In this case, ensure the tot_len member of the 00198 * pbuf is the sum of the chained pbuf len members. 00199 */ 00200 read data into(q->payload, q->len); 00201 } 00202 acknowledge that packet has been read(); 00203 00204 #if ETH_PAD_SIZE 00205 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ 00206 #endif 00207 00208 LINK_STATS_INC(link.recv); 00209 } else { 00210 drop packet(); 00211 LINK_STATS_INC(link.memerr); 00212 LINK_STATS_INC(link.drop); 00213 } 00214 00215 return p; 00216 } 00217 00218 /** 00219 * This function should be called when a packet is ready to be read 00220 * from the interface. It uses the function low_level_input() that 00221 * should handle the actual reception of bytes from the network 00222 * interface. Then the type of the received packet is determined and 00223 * the appropriate input function is called. 00224 * 00225 * @param netif the lwip network interface structure for this ethernetif 00226 */ 00227 static void 00228 ethernetif_input(struct netif *netif) 00229 { 00230 struct ethernetif *ethernetif; 00231 struct eth_hdr *ethhdr; 00232 struct pbuf *p; 00233 00234 ethernetif = netif->state; 00235 00236 /* move received packet into a new pbuf */ 00237 p = low_level_input(netif); 00238 /* no packet could be read, silently ignore this */ 00239 if (p == NULL) return; 00240 /* points to packet payload, which starts with an Ethernet header */ 00241 ethhdr = p->payload; 00242 00243 switch (htons(ethhdr->type)) { 00244 /* IP or ARP packet? */ 00245 case ETHTYPE_IP: 00246 case ETHTYPE_ARP: 00247 #if PPPOE_SUPPORT 00248 /* PPPoE packet? */ 00249 case ETHTYPE_PPPOEDISC: 00250 case ETHTYPE_PPPOE: 00251 #endif /* PPPOE_SUPPORT */ 00252 /* full packet send to tcpip_thread to process */ 00253 if (netif->input(p, netif)!=ERR_OK) 00254 { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); 00255 pbuf_free(p); 00256 p = NULL; 00257 } 00258 break; 00259 00260 default: 00261 pbuf_free(p); 00262 p = NULL; 00263 break; 00264 } 00265 } 00266 00267 /** 00268 * Should be called at the beginning of the program to set up the 00269 * network interface. It calls the function low_level_init() to do the 00270 * actual setup of the hardware. 00271 * 00272 * This function should be passed as a parameter to netif_add(). 00273 * 00274 * @param netif the lwip network interface structure for this ethernetif 00275 * @return ERR_OK if the loopif is initialized 00276 * ERR_MEM if private data couldn't be allocated 00277 * any other err_t on error 00278 */ 00279 err_t 00280 ethernetif_init(struct netif *netif) 00281 { 00282 struct ethernetif *ethernetif; 00283 00284 LWIP_ASSERT("netif != NULL", (netif != NULL)); 00285 00286 ethernetif = mem_malloc(sizeof(struct ethernetif)); 00287 if (ethernetif == NULL) { 00288 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n")); 00289 return ERR_MEM; 00290 } 00291 00292 #if LWIP_NETIF_HOSTNAME 00293 /* Initialize interface hostname */ 00294 netif->hostname = "lwip"; 00295 #endif /* LWIP_NETIF_HOSTNAME */ 00296 00297 /* 00298 * Initialize the snmp variables and counters inside the struct netif. 00299 * The last argument should be replaced with your link speed, in units 00300 * of bits per second. 00301 */ 00302 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS); 00303 00304 netif->state = ethernetif; 00305 netif->name[0] = IFNAME0; 00306 netif->name[1] = IFNAME1; 00307 /* We directly use etharp_output() here to save a function call. 00308 * You can instead declare your own function an call etharp_output() 00309 * from it if you have to do some checks before sending (e.g. if link 00310 * is available...) */ 00311 netif->output = etharp_output; 00312 netif->linkoutput = low_level_output; 00313 00314 ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]); 00315 00316 /* initialize the hardware */ 00317 low_level_init(netif); 00318 00319 return ERR_OK; 00320 } 00321 00322 #endif /* 0 */
Generated on Tue Jul 12 2022 14:12:34 by
