Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed

Dependencies:   mbed

Committer:
pehrhovey
Date:
Sun Mar 14 00:54:12 2010 +0000
Revision:
0:a548a085de55

        

Who changed what in which revision?

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