Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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