This library is stripped down version of NetServices library. HTTP server and client function is NOT supported.

Dependents:   imu-daq-eth

Committer:
idinor
Date:
Wed Jul 20 11:45:39 2011 +0000
Revision:
0:dcf3c92487ca

        

Who changed what in which revision?

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