NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Committer:
donatien
Date:
Thu Aug 05 15:01:33 2010 +0000
Revision:
11:da4498f591ee
Parent:
0:632c9925f013

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:632c9925f013 1 /**
donatien 0:632c9925f013 2 * @file
donatien 0:632c9925f013 3 * Ethernet Interface Skeleton
donatien 0:632c9925f013 4 *
donatien 0:632c9925f013 5 */
donatien 0:632c9925f013 6
donatien 0:632c9925f013 7 /*
donatien 0:632c9925f013 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
donatien 0:632c9925f013 9 * All rights reserved.
donatien 0:632c9925f013 10 *
donatien 0:632c9925f013 11 * Redistribution and use in source and binary forms, with or without modification,
donatien 0:632c9925f013 12 * are permitted provided that the following conditions are met:
donatien 0:632c9925f013 13 *
donatien 0:632c9925f013 14 * 1. Redistributions of source code must retain the above copyright notice,
donatien 0:632c9925f013 15 * this list of conditions and the following disclaimer.
donatien 0:632c9925f013 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
donatien 0:632c9925f013 17 * this list of conditions and the following disclaimer in the documentation
donatien 0:632c9925f013 18 * and/or other materials provided with the distribution.
donatien 0:632c9925f013 19 * 3. The name of the author may not be used to endorse or promote products
donatien 0:632c9925f013 20 * derived from this software without specific prior written permission.
donatien 0:632c9925f013 21 *
donatien 0:632c9925f013 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
donatien 0:632c9925f013 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
donatien 0:632c9925f013 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
donatien 0:632c9925f013 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
donatien 0:632c9925f013 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
donatien 0:632c9925f013 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
donatien 0:632c9925f013 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
donatien 0:632c9925f013 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
donatien 0:632c9925f013 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
donatien 0:632c9925f013 31 * OF SUCH DAMAGE.
donatien 0:632c9925f013 32 *
donatien 0:632c9925f013 33 * This file is part of the lwIP TCP/IP stack.
donatien 0:632c9925f013 34 *
donatien 0:632c9925f013 35 * Author: Adam Dunkels <adam@sics.se>
donatien 0:632c9925f013 36 *
donatien 0:632c9925f013 37 */
donatien 0:632c9925f013 38
donatien 0:632c9925f013 39 /*
donatien 0:632c9925f013 40 * This file is a skeleton for developing Ethernet network interface
donatien 0:632c9925f013 41 * drivers for lwIP. Add code to the low_level functions and do a
donatien 0:632c9925f013 42 * search-and-replace for the word "ethernetif" to replace it with
donatien 0:632c9925f013 43 * something that better describes your network interface.
donatien 0:632c9925f013 44 */
donatien 0:632c9925f013 45
donatien 0:632c9925f013 46 #include "lwip/opt.h"
donatien 0:632c9925f013 47
donatien 0:632c9925f013 48 #if 0 /* don't build, this is only a skeleton, see previous comment */
donatien 0:632c9925f013 49
donatien 0:632c9925f013 50 #include "lwip/def.h"
donatien 0:632c9925f013 51 #include "lwip/mem.h"
donatien 0:632c9925f013 52 #include "lwip/pbuf.h"
donatien 0:632c9925f013 53 #include "lwip/sys.h"
donatien 0:632c9925f013 54 #include <lwip/stats.h>
donatien 0:632c9925f013 55 #include <lwip/snmp.h>
donatien 0:632c9925f013 56 #include "netif/etharp.h"
donatien 0:632c9925f013 57 #include "netif/ppp_oe.h"
donatien 0:632c9925f013 58
donatien 0:632c9925f013 59 /* Define those to better describe your network interface. */
donatien 0:632c9925f013 60 #define IFNAME0 'e'
donatien 0:632c9925f013 61 #define IFNAME1 'n'
donatien 0:632c9925f013 62
donatien 0:632c9925f013 63 /**
donatien 0:632c9925f013 64 * Helper struct to hold private data used to operate your ethernet interface.
donatien 0:632c9925f013 65 * Keeping the ethernet address of the MAC in this struct is not necessary
donatien 0:632c9925f013 66 * as it is already kept in the struct netif.
donatien 0:632c9925f013 67 * But this is only an example, anyway...
donatien 0:632c9925f013 68 */
donatien 0:632c9925f013 69 struct ethernetif {
donatien 0:632c9925f013 70 struct eth_addr *ethaddr;
donatien 0:632c9925f013 71 /* Add whatever per-interface state that is needed here. */
donatien 0:632c9925f013 72 };
donatien 0:632c9925f013 73
donatien 0:632c9925f013 74 /* Forward declarations. */
donatien 0:632c9925f013 75 static void ethernetif_input(struct netif *netif);
donatien 0:632c9925f013 76
donatien 0:632c9925f013 77 /**
donatien 0:632c9925f013 78 * In this function, the hardware should be initialized.
donatien 0:632c9925f013 79 * Called from ethernetif_init().
donatien 0:632c9925f013 80 *
donatien 0:632c9925f013 81 * @param netif the already initialized lwip network interface structure
donatien 0:632c9925f013 82 * for this ethernetif
donatien 0:632c9925f013 83 */
donatien 0:632c9925f013 84 static void
donatien 0:632c9925f013 85 low_level_init(struct netif *netif)
donatien 0:632c9925f013 86 {
donatien 0:632c9925f013 87 struct ethernetif *ethernetif = netif->state;
donatien 0:632c9925f013 88
donatien 0:632c9925f013 89 /* set MAC hardware address length */
donatien 0:632c9925f013 90 netif->hwaddr_len = ETHARP_HWADDR_LEN;
donatien 0:632c9925f013 91
donatien 0:632c9925f013 92 /* set MAC hardware address */
donatien 0:632c9925f013 93 netif->hwaddr[0] = ;
donatien 0:632c9925f013 94 ...
donatien 0:632c9925f013 95 netif->hwaddr[5] = ;
donatien 0:632c9925f013 96
donatien 0:632c9925f013 97 /* maximum transfer unit */
donatien 0:632c9925f013 98 netif->mtu = 1500;
donatien 0:632c9925f013 99
donatien 0:632c9925f013 100 /* device capabilities */
donatien 0:632c9925f013 101 /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
donatien 0:632c9925f013 102 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
donatien 0:632c9925f013 103
donatien 0:632c9925f013 104 /* Do whatever else is needed to initialize interface. */
donatien 0:632c9925f013 105 }
donatien 0:632c9925f013 106
donatien 0:632c9925f013 107 /**
donatien 0:632c9925f013 108 * This function should do the actual transmission of the packet. The packet is
donatien 0:632c9925f013 109 * contained in the pbuf that is passed to the function. This pbuf
donatien 0:632c9925f013 110 * might be chained.
donatien 0:632c9925f013 111 *
donatien 0:632c9925f013 112 * @param netif the lwip network interface structure for this ethernetif
donatien 0:632c9925f013 113 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
donatien 0:632c9925f013 114 * @return ERR_OK if the packet could be sent
donatien 0:632c9925f013 115 * an err_t value if the packet couldn't be sent
donatien 0:632c9925f013 116 *
donatien 0:632c9925f013 117 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
donatien 0:632c9925f013 118 * strange results. You might consider waiting for space in the DMA queue
donatien 0:632c9925f013 119 * to become availale since the stack doesn't retry to send a packet
donatien 0:632c9925f013 120 * dropped because of memory failure (except for the TCP timers).
donatien 0:632c9925f013 121 */
donatien 0:632c9925f013 122
donatien 0:632c9925f013 123 static err_t
donatien 0:632c9925f013 124 low_level_output(struct netif *netif, struct pbuf *p)
donatien 0:632c9925f013 125 {
donatien 0:632c9925f013 126 struct ethernetif *ethernetif = netif->state;
donatien 0:632c9925f013 127 struct pbuf *q;
donatien 0:632c9925f013 128
donatien 0:632c9925f013 129 initiate transfer();
donatien 0:632c9925f013 130
donatien 0:632c9925f013 131 #if ETH_PAD_SIZE
donatien 0:632c9925f013 132 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
donatien 0:632c9925f013 133 #endif
donatien 0:632c9925f013 134
donatien 0:632c9925f013 135 for(q = p; q != NULL; q = q->next) {
donatien 0:632c9925f013 136 /* Send the data from the pbuf to the interface, one pbuf at a
donatien 0:632c9925f013 137 time. The size of the data in each pbuf is kept in the ->len
donatien 0:632c9925f013 138 variable. */
donatien 0:632c9925f013 139 send data from(q->payload, q->len);
donatien 0:632c9925f013 140 }
donatien 0:632c9925f013 141
donatien 0:632c9925f013 142 signal that packet should be sent();
donatien 0:632c9925f013 143
donatien 0:632c9925f013 144 #if ETH_PAD_SIZE
donatien 0:632c9925f013 145 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
donatien 0:632c9925f013 146 #endif
donatien 0:632c9925f013 147
donatien 0:632c9925f013 148 LINK_STATS_INC(link.xmit);
donatien 0:632c9925f013 149
donatien 0:632c9925f013 150 return ERR_OK;
donatien 0:632c9925f013 151 }
donatien 0:632c9925f013 152
donatien 0:632c9925f013 153 /**
donatien 0:632c9925f013 154 * Should allocate a pbuf and transfer the bytes of the incoming
donatien 0:632c9925f013 155 * packet from the interface into the pbuf.
donatien 0:632c9925f013 156 *
donatien 0:632c9925f013 157 * @param netif the lwip network interface structure for this ethernetif
donatien 0:632c9925f013 158 * @return a pbuf filled with the received packet (including MAC header)
donatien 0:632c9925f013 159 * NULL on memory error
donatien 0:632c9925f013 160 */
donatien 0:632c9925f013 161 static struct pbuf *
donatien 0:632c9925f013 162 low_level_input(struct netif *netif)
donatien 0:632c9925f013 163 {
donatien 0:632c9925f013 164 struct ethernetif *ethernetif = netif->state;
donatien 0:632c9925f013 165 struct pbuf *p, *q;
donatien 0:632c9925f013 166 u16_t len;
donatien 0:632c9925f013 167
donatien 0:632c9925f013 168 /* Obtain the size of the packet and put it into the "len"
donatien 0:632c9925f013 169 variable. */
donatien 0:632c9925f013 170 len = ;
donatien 0:632c9925f013 171
donatien 0:632c9925f013 172 #if ETH_PAD_SIZE
donatien 0:632c9925f013 173 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
donatien 0:632c9925f013 174 #endif
donatien 0:632c9925f013 175
donatien 0:632c9925f013 176 /* We allocate a pbuf chain of pbufs from the pool. */
donatien 0:632c9925f013 177 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
donatien 0:632c9925f013 178
donatien 0:632c9925f013 179 if (p != NULL) {
donatien 0:632c9925f013 180
donatien 0:632c9925f013 181 #if ETH_PAD_SIZE
donatien 0:632c9925f013 182 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
donatien 0:632c9925f013 183 #endif
donatien 0:632c9925f013 184
donatien 0:632c9925f013 185 /* We iterate over the pbuf chain until we have read the entire
donatien 0:632c9925f013 186 * packet into the pbuf. */
donatien 0:632c9925f013 187 for(q = p; q != NULL; q = q->next) {
donatien 0:632c9925f013 188 /* Read enough bytes to fill this pbuf in the chain. The
donatien 0:632c9925f013 189 * available data in the pbuf is given by the q->len
donatien 0:632c9925f013 190 * variable.
donatien 0:632c9925f013 191 * This does not necessarily have to be a memcpy, you can also preallocate
donatien 0:632c9925f013 192 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
donatien 0:632c9925f013 193 * actually received size. In this case, ensure the tot_len member of the
donatien 0:632c9925f013 194 * pbuf is the sum of the chained pbuf len members.
donatien 0:632c9925f013 195 */
donatien 0:632c9925f013 196 read data into(q->payload, q->len);
donatien 0:632c9925f013 197 }
donatien 0:632c9925f013 198 acknowledge that packet has been read();
donatien 0:632c9925f013 199
donatien 0:632c9925f013 200 #if ETH_PAD_SIZE
donatien 0:632c9925f013 201 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
donatien 0:632c9925f013 202 #endif
donatien 0:632c9925f013 203
donatien 0:632c9925f013 204 LINK_STATS_INC(link.recv);
donatien 0:632c9925f013 205 } else {
donatien 0:632c9925f013 206 drop packet();
donatien 0:632c9925f013 207 LINK_STATS_INC(link.memerr);
donatien 0:632c9925f013 208 LINK_STATS_INC(link.drop);
donatien 0:632c9925f013 209 }
donatien 0:632c9925f013 210
donatien 0:632c9925f013 211 return p;
donatien 0:632c9925f013 212 }
donatien 0:632c9925f013 213
donatien 0:632c9925f013 214 /**
donatien 0:632c9925f013 215 * This function should be called when a packet is ready to be read
donatien 0:632c9925f013 216 * from the interface. It uses the function low_level_input() that
donatien 0:632c9925f013 217 * should handle the actual reception of bytes from the network
donatien 0:632c9925f013 218 * interface. Then the type of the received packet is determined and
donatien 0:632c9925f013 219 * the appropriate input function is called.
donatien 0:632c9925f013 220 *
donatien 0:632c9925f013 221 * @param netif the lwip network interface structure for this ethernetif
donatien 0:632c9925f013 222 */
donatien 0:632c9925f013 223 static void
donatien 0:632c9925f013 224 ethernetif_input(struct netif *netif)
donatien 0:632c9925f013 225 {
donatien 0:632c9925f013 226 struct ethernetif *ethernetif;
donatien 0:632c9925f013 227 struct eth_hdr *ethhdr;
donatien 0:632c9925f013 228 struct pbuf *p;
donatien 0:632c9925f013 229
donatien 0:632c9925f013 230 ethernetif = netif->state;
donatien 0:632c9925f013 231
donatien 0:632c9925f013 232 /* move received packet into a new pbuf */
donatien 0:632c9925f013 233 p = low_level_input(netif);
donatien 0:632c9925f013 234 /* no packet could be read, silently ignore this */
donatien 0:632c9925f013 235 if (p == NULL) return;
donatien 0:632c9925f013 236 /* points to packet payload, which starts with an Ethernet header */
donatien 0:632c9925f013 237 ethhdr = p->payload;
donatien 0:632c9925f013 238
donatien 0:632c9925f013 239 switch (htons(ethhdr->type)) {
donatien 0:632c9925f013 240 /* IP or ARP packet? */
donatien 0:632c9925f013 241 case ETHTYPE_IP:
donatien 0:632c9925f013 242 case ETHTYPE_ARP:
donatien 0:632c9925f013 243 #if PPPOE_SUPPORT
donatien 0:632c9925f013 244 /* PPPoE packet? */
donatien 0:632c9925f013 245 case ETHTYPE_PPPOEDISC:
donatien 0:632c9925f013 246 case ETHTYPE_PPPOE:
donatien 0:632c9925f013 247 #endif /* PPPOE_SUPPORT */
donatien 0:632c9925f013 248 /* full packet send to tcpip_thread to process */
donatien 0:632c9925f013 249 if (netif->input(p, netif)!=ERR_OK)
donatien 0:632c9925f013 250 { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
donatien 0:632c9925f013 251 pbuf_free(p);
donatien 0:632c9925f013 252 p = NULL;
donatien 0:632c9925f013 253 }
donatien 0:632c9925f013 254 break;
donatien 0:632c9925f013 255
donatien 0:632c9925f013 256 default:
donatien 0:632c9925f013 257 pbuf_free(p);
donatien 0:632c9925f013 258 p = NULL;
donatien 0:632c9925f013 259 break;
donatien 0:632c9925f013 260 }
donatien 0:632c9925f013 261 }
donatien 0:632c9925f013 262
donatien 0:632c9925f013 263 /**
donatien 0:632c9925f013 264 * Should be called at the beginning of the program to set up the
donatien 0:632c9925f013 265 * network interface. It calls the function low_level_init() to do the
donatien 0:632c9925f013 266 * actual setup of the hardware.
donatien 0:632c9925f013 267 *
donatien 0:632c9925f013 268 * This function should be passed as a parameter to netif_add().
donatien 0:632c9925f013 269 *
donatien 0:632c9925f013 270 * @param netif the lwip network interface structure for this ethernetif
donatien 0:632c9925f013 271 * @return ERR_OK if the loopif is initialized
donatien 0:632c9925f013 272 * ERR_MEM if private data couldn't be allocated
donatien 0:632c9925f013 273 * any other err_t on error
donatien 0:632c9925f013 274 */
donatien 0:632c9925f013 275 err_t
donatien 0:632c9925f013 276 ethernetif_init(struct netif *netif)
donatien 0:632c9925f013 277 {
donatien 0:632c9925f013 278 struct ethernetif *ethernetif;
donatien 0:632c9925f013 279
donatien 0:632c9925f013 280 LWIP_ASSERT("netif != NULL", (netif != NULL));
donatien 0:632c9925f013 281
donatien 0:632c9925f013 282 ethernetif = mem_malloc(sizeof(struct ethernetif));
donatien 0:632c9925f013 283 if (ethernetif == NULL) {
donatien 0:632c9925f013 284 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
donatien 0:632c9925f013 285 return ERR_MEM;
donatien 0:632c9925f013 286 }
donatien 0:632c9925f013 287
donatien 0:632c9925f013 288 #if LWIP_NETIF_HOSTNAME
donatien 0:632c9925f013 289 /* Initialize interface hostname */
donatien 0:632c9925f013 290 netif->hostname = "lwip";
donatien 0:632c9925f013 291 #endif /* LWIP_NETIF_HOSTNAME */
donatien 0:632c9925f013 292
donatien 0:632c9925f013 293 /*
donatien 0:632c9925f013 294 * Initialize the snmp variables and counters inside the struct netif.
donatien 0:632c9925f013 295 * The last argument should be replaced with your link speed, in units
donatien 0:632c9925f013 296 * of bits per second.
donatien 0:632c9925f013 297 */
donatien 0:632c9925f013 298 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
donatien 0:632c9925f013 299
donatien 0:632c9925f013 300 netif->state = ethernetif;
donatien 0:632c9925f013 301 netif->name[0] = IFNAME0;
donatien 0:632c9925f013 302 netif->name[1] = IFNAME1;
donatien 0:632c9925f013 303 /* We directly use etharp_output() here to save a function call.
donatien 0:632c9925f013 304 * You can instead declare your own function an call etharp_output()
donatien 0:632c9925f013 305 * from it if you have to do some checks before sending (e.g. if link
donatien 0:632c9925f013 306 * is available...) */
donatien 0:632c9925f013 307 netif->output = etharp_output;
donatien 0:632c9925f013 308 netif->linkoutput = low_level_output;
donatien 0:632c9925f013 309
donatien 0:632c9925f013 310 ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
donatien 0:632c9925f013 311
donatien 0:632c9925f013 312 /* initialize the hardware */
donatien 0:632c9925f013 313 low_level_init(netif);
donatien 0:632c9925f013 314
donatien 0:632c9925f013 315 return ERR_OK;
donatien 0:632c9925f013 316 }
donatien 0:632c9925f013 317
donatien 0:632c9925f013 318 #endif /* 0 */