Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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