NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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