Richard Blimmel / Mbed 2 deprecated CIG_HTTPClient

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ethernetif.c Source File

ethernetif.c

Go to the documentation of this file.
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] = ;
00094   ...
00095   netif->hwaddr[5] = ;
00096 
00097   /* maximum transfer unit */
00098   netif->mtu = 1500;
00099   
00100   /* device capabilities */
00101   /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
00102   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
00103  
00104   /* Do whatever else is needed to initialize interface. */  
00105 }
00106 
00107 /**
00108  * This function should do the actual transmission of the packet. The packet is
00109  * contained in the pbuf that is passed to the function. This pbuf
00110  * might be chained.
00111  *
00112  * @param netif the lwip network interface structure for this ethernetif
00113  * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
00114  * @return ERR_OK if the packet could be sent
00115  *         an err_t value if the packet couldn't be sent
00116  *
00117  * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
00118  *       strange results. You might consider waiting for space in the DMA queue
00119  *       to become availale since the stack doesn't retry to send a packet
00120  *       dropped because of memory failure (except for the TCP timers).
00121  */
00122 
00123 static err_t
00124 low_level_output(struct netif *netif, struct pbuf *p)
00125 {
00126   struct ethernetif *ethernetif = netif->state;
00127   struct pbuf *q;
00128 
00129   initiate transfer();
00130  
00131 printf("Ethernet low level output\r\n");
00132   
00133 #if ETH_PAD_SIZE
00134   pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
00135 #endif
00136 
00137   for(q = p; q != NULL; q = q->next) {
00138     /* Send the data from the pbuf to the interface, one pbuf at a
00139        time. The size of the data in each pbuf is kept in the ->len
00140        variable. */
00141     send data from(q->payload, q->len);
00142   }
00143 
00144   signal that packet should be sent();
00145 
00146 #if ETH_PAD_SIZE
00147   pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
00148 #endif
00149   
00150   LINK_STATS_INC(link.xmit);
00151 
00152   return ERR_OK;
00153 }
00154 
00155 /**
00156  * Should allocate a pbuf and transfer the bytes of the incoming
00157  * packet from the interface into the pbuf.
00158  *
00159  * @param netif the lwip network interface structure for this ethernetif
00160  * @return a pbuf filled with the received packet (including MAC header)
00161  *         NULL on memory error
00162  */
00163 static struct pbuf *
00164 low_level_input(struct netif *netif)
00165 {
00166   struct ethernetif *ethernetif = netif->state;
00167   struct pbuf *p, *q;
00168   u16_t len;
00169 
00170   /* Obtain the size of the packet and put it into the "len"
00171      variable. */
00172   len = ;
00173 
00174 #if ETH_PAD_SIZE
00175   len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
00176 #endif
00177 
00178   /* We allocate a pbuf chain of pbufs from the pool. */
00179   p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
00180   
00181   if (p != NULL) {
00182 
00183 #if ETH_PAD_SIZE
00184     pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
00185 #endif
00186 
00187     /* We iterate over the pbuf chain until we have read the entire
00188      * packet into the pbuf. */
00189     for(q = p; q != NULL; q = q->next) {
00190       /* Read enough bytes to fill this pbuf in the chain. The
00191        * available data in the pbuf is given by the q->len
00192        * variable.
00193        * This does not necessarily have to be a memcpy, you can also preallocate
00194        * pbufs for a DMA-enabled MAC and after receiving truncate it to the
00195        * actually received size. In this case, ensure the tot_len member of the
00196        * pbuf is the sum of the chained pbuf len members.
00197        */
00198       read data into(q->payload, q->len);
00199     }
00200     acknowledge that packet has been read();
00201 
00202 #if ETH_PAD_SIZE
00203     pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
00204 #endif
00205 
00206     LINK_STATS_INC(link.recv);
00207   } else {
00208     drop packet();
00209     LINK_STATS_INC(link.memerr);
00210     LINK_STATS_INC(link.drop);
00211   }
00212 
00213   return p;  
00214 }
00215 
00216 /**
00217  * This function should be called when a packet is ready to be read
00218  * from the interface. It uses the function low_level_input() that
00219  * should handle the actual reception of bytes from the network
00220  * interface. Then the type of the received packet is determined and
00221  * the appropriate input function is called.
00222  *
00223  * @param netif the lwip network interface structure for this ethernetif
00224  */
00225 static void
00226 ethernetif_input(struct netif *netif)
00227 {
00228   
00229   struct ethernetif *ethernetif;
00230   struct eth_hdr *ethhdr;
00231   struct pbuf *p;
00232 
00233   ethernetif = netif->state;
00234 
00235 printf("Ethernet low level input\r\n");
00236 
00237 
00238   /* move received packet into a new pbuf */
00239   p = low_level_input(netif);
00240   /* no packet could be read, silently ignore this */
00241   if (p == NULL) return;
00242   /* points to packet payload, which starts with an Ethernet header */
00243   ethhdr = p->payload;
00244 
00245   switch (htons(ethhdr->type)) {
00246   /* IP or ARP packet? */
00247   case ETHTYPE_IP:
00248   case ETHTYPE_ARP:
00249 #if PPPOE_SUPPORT
00250   /* PPPoE packet? */
00251   case ETHTYPE_PPPOEDISC:
00252   case ETHTYPE_PPPOE:
00253 #endif /* PPPOE_SUPPORT */
00254     /* full packet send to tcpip_thread to process */
00255     if (netif->input(p, netif)!=ERR_OK)
00256      { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
00257        pbuf_free(p);
00258        p = NULL;
00259      }
00260     break;
00261 
00262   default:
00263     pbuf_free(p);
00264     p = NULL;
00265     break;
00266   }
00267 }
00268 
00269 /**
00270  * Should be called at the beginning of the program to set up the
00271  * network interface. It calls the function low_level_init() to do the
00272  * actual setup of the hardware.
00273  *
00274  * This function should be passed as a parameter to netif_add().
00275  *
00276  * @param netif the lwip network interface structure for this ethernetif
00277  * @return ERR_OK if the loopif is initialized
00278  *         ERR_MEM if private data couldn't be allocated
00279  *         any other err_t on error
00280  */
00281 err_t
00282 ethernetif_init(struct netif *netif)
00283 {
00284   struct ethernetif *ethernetif;
00285 
00286   LWIP_ASSERT("netif != NULL", (netif != NULL));
00287     
00288   ethernetif = mem_malloc(sizeof(struct ethernetif));
00289   if (ethernetif == NULL) {
00290     LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
00291     return ERR_MEM;
00292   }
00293 
00294 #if LWIP_NETIF_HOSTNAME
00295   /* Initialize interface hostname */
00296   netif->hostname = "lwip";
00297 #endif /* LWIP_NETIF_HOSTNAME */
00298 
00299   /*
00300    * Initialize the snmp variables and counters inside the struct netif.
00301    * The last argument should be replaced with your link speed, in units
00302    * of bits per second.
00303    */
00304   NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
00305 
00306   netif->state = ethernetif;
00307   netif->name[0] = IFNAME0;
00308   netif->name[1] = IFNAME1;
00309   /* We directly use etharp_output() here to save a function call.
00310    * You can instead declare your own function an call etharp_output()
00311    * from it if you have to do some checks before sending (e.g. if link
00312    * is available...) */
00313   netif->output = etharp_output;
00314   netif->linkoutput = low_level_output;
00315   
00316   ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
00317   
00318   /* initialize the hardware */
00319   low_level_init(netif);
00320 
00321   return ERR_OK;
00322 }
00323 
00324 #endif /* 0 */