Yasushi TAUCHI / Mbed 2 deprecated giken9_HTMLServer_Sample

Dependencies:   EthernetInterface HttpServer TextLCD mbed-rpc mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
yueee_yt
Date:
Wed Mar 12 04:19:54 2014 +0000
Revision:
0:7766f6712673
???????????????

Who changed what in which revision?

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