Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
dcoban
Date:
Tue Apr 03 18:43:13 2012 +0000
Revision:
0:1a61c61d0845

        

Who changed what in which revision?

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