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 /*
simon 0:350011bf8be7 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
simon 0:350011bf8be7 4
simon 0:350011bf8be7 5 Permission is hereby granted, free of charge, to any person obtaining a copy
simon 0:350011bf8be7 6 of this software and associated documentation files (the "Software"), to deal
simon 0:350011bf8be7 7 in the Software without restriction, including without limitation the rights
simon 0:350011bf8be7 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 0:350011bf8be7 9 copies of the Software, and to permit persons to whom the Software is
simon 0:350011bf8be7 10 furnished to do so, subject to the following conditions:
simon 0:350011bf8be7 11
simon 0:350011bf8be7 12 The above copyright notice and this permission notice shall be included in
simon 0:350011bf8be7 13 all copies or substantial portions of the Software.
simon 0:350011bf8be7 14
simon 0:350011bf8be7 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 0:350011bf8be7 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 0:350011bf8be7 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 0:350011bf8be7 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 0:350011bf8be7 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 0:350011bf8be7 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 0:350011bf8be7 21 THE SOFTWARE.
simon 0:350011bf8be7 22 */
simon 0:350011bf8be7 23
simon 0:350011bf8be7 24 #include "netCfg.h"
simon 0:350011bf8be7 25 #if NET_ETH
simon 0:350011bf8be7 26
simon 0:350011bf8be7 27 #include "mbed.h"
simon 0:350011bf8be7 28
simon 0:350011bf8be7 29 Ethernet *pEth = NULL;
simon 0:350011bf8be7 30 #ifdef __cplusplus
simon 0:350011bf8be7 31 extern "C" {
simon 0:350011bf8be7 32 #endif
simon 0:350011bf8be7 33
simon 0:350011bf8be7 34 #include "lwip/opt.h"
simon 0:350011bf8be7 35
simon 0:350011bf8be7 36 #include "lwip/def.h"
simon 0:350011bf8be7 37 #include "lwip/pbuf.h"
simon 0:350011bf8be7 38 #include "lwip/sys.h"
simon 0:350011bf8be7 39 #include "lwip/stats.h"
simon 0:350011bf8be7 40 #include "netif/etharp.h"
simon 0:350011bf8be7 41 #include "string.h"
simon 0:350011bf8be7 42
simon 0:350011bf8be7 43 //#include "eth_drv.h"
simon 0:350011bf8be7 44
simon 0:350011bf8be7 45 #define IFNAME0 'E'
simon 0:350011bf8be7 46 #define IFNAME1 'X'
simon 0:350011bf8be7 47
simon 0:350011bf8be7 48 #define min(x,y) (((x)<(y))?(x):(y))
simon 0:350011bf8be7 49
simon 0:350011bf8be7 50 struct netif* eth_netif;
simon 0:350011bf8be7 51
simon 0:350011bf8be7 52 static err_t eth_output(struct netif *netif, struct pbuf *p) {
simon 0:350011bf8be7 53 #if ETH_PAD_SIZE
simon 0:350011bf8be7 54 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
simon 0:350011bf8be7 55 #endif
simon 0:350011bf8be7 56
simon 0:350011bf8be7 57 do {
simon 0:350011bf8be7 58 pEth->write((const char *)p->payload, p->len);
simon 0:350011bf8be7 59 } while((p = p->next)!=NULL);
simon 0:350011bf8be7 60
simon 0:350011bf8be7 61 pEth->send();
simon 0:350011bf8be7 62
simon 0:350011bf8be7 63 #if ETH_PAD_SIZE
simon 0:350011bf8be7 64 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
simon 0:350011bf8be7 65 #endif
simon 0:350011bf8be7 66
simon 0:350011bf8be7 67 LINK_STATS_INC(link.xmit);
simon 0:350011bf8be7 68 return ERR_OK;
simon 0:350011bf8be7 69 }
simon 0:350011bf8be7 70
simon 0:350011bf8be7 71 /*
simon 0:350011bf8be7 72 void show(char *buf, int size) {
simon 0:350011bf8be7 73 printf("Destination: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
simon 0:350011bf8be7 74 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
simon 0:350011bf8be7 75 printf("Source: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
simon 0:350011bf8be7 76 buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
simon 0:350011bf8be7 77
simon 0:350011bf8be7 78 printf("Type %hd\n", htons((short)buf[12]));
simon 0:350011bf8be7 79
simon 0:350011bf8be7 80 // hexview(buf, size);
simon 0:350011bf8be7 81 }
simon 0:350011bf8be7 82 */
simon 0:350011bf8be7 83
simon 0:350011bf8be7 84 void eth_poll() {
simon 0:350011bf8be7 85 struct eth_hdr *ethhdr;
simon 0:350011bf8be7 86 struct pbuf *frame, *p;
simon 0:350011bf8be7 87 int len, read;
simon 0:350011bf8be7 88
simon 0:350011bf8be7 89 while((len = pEth->receive()) != 0) {
simon 0:350011bf8be7 90 frame = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
simon 0:350011bf8be7 91 if(frame == NULL) {
simon 0:350011bf8be7 92 return;
simon 0:350011bf8be7 93 }
simon 0:350011bf8be7 94 p = frame;
simon 0:350011bf8be7 95 /* no packet could be read, silently ignore this */
simon 0:350011bf8be7 96 if (p == NULL) return;
simon 0:350011bf8be7 97 do {
simon 0:350011bf8be7 98 read = pEth->read((char *)p->payload, p->len);
simon 0:350011bf8be7 99 p = p->next;
simon 0:350011bf8be7 100 } while(p != NULL && read != 0);
simon 0:350011bf8be7 101
simon 0:350011bf8be7 102 #if ETH_PAD_SIZE
simon 0:350011bf8be7 103 pbuf_header(p, ETH_PAD_SIZE);
simon 0:350011bf8be7 104 #endif
simon 0:350011bf8be7 105
simon 0:350011bf8be7 106 ethhdr = (struct eth_hdr *)(frame->payload);
simon 0:350011bf8be7 107
simon 0:350011bf8be7 108 // show((char*)ethhdr, 13);
simon 0:350011bf8be7 109
simon 0:350011bf8be7 110 /*
simon 0:350011bf8be7 111 switch(htons(ethhdr->type)) {
simon 0:350011bf8be7 112
simon 0:350011bf8be7 113 case ETHTYPE_IP:
simon 0:350011bf8be7 114 etharp_ip_input(gnetif, frame);
simon 0:350011bf8be7 115 pbuf_header(frame, -((s16_t) sizeof(struct eth_hdr)));
simon 0:350011bf8be7 116 gnetif->input(frame, gnetif);
simon 0:350011bf8be7 117 break;
simon 0:350011bf8be7 118
simon 0:350011bf8be7 119 case ETHTYPE_ARP:
simon 0:350011bf8be7 120 etharp_arp_input(gnetif, (struct eth_addr *)(gnetif->hwaddr), frame);
simon 0:350011bf8be7 121 break;
simon 0:350011bf8be7 122
simon 0:350011bf8be7 123 default:
simon 0:350011bf8be7 124 break;
simon 0:350011bf8be7 125 }*/
simon 0:350011bf8be7 126
simon 0:350011bf8be7 127
simon 0:350011bf8be7 128
simon 0:350011bf8be7 129 //ethernet_input(frame, gnetif);
simon 0:350011bf8be7 130
simon 0:350011bf8be7 131 switch (htons(ethhdr->type)) {
simon 0:350011bf8be7 132 /* IP or ARP packet? */
simon 0:350011bf8be7 133 case ETHTYPE_IP:
simon 0:350011bf8be7 134 case ETHTYPE_ARP:
simon 0:350011bf8be7 135 #if PPPOE_SUPPORT
simon 0:350011bf8be7 136 /* PPPoE packet? */
simon 0:350011bf8be7 137 case ETHTYPE_PPPOEDISC:
simon 0:350011bf8be7 138 case ETHTYPE_PPPOE:
simon 0:350011bf8be7 139 #endif /* PPPOE_SUPPORT */
simon 0:350011bf8be7 140 /* full packet send to tcpip_thread to process */
simon 0:350011bf8be7 141 //if (netif->input(p, gnetif)!=ERR_OK)
simon 0:350011bf8be7 142 if (ethernet_input(frame, eth_netif)!=ERR_OK)
simon 0:350011bf8be7 143 { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
simon 0:350011bf8be7 144 pbuf_free(frame);
simon 0:350011bf8be7 145 frame = NULL;
simon 0:350011bf8be7 146 }
simon 0:350011bf8be7 147 break;
simon 0:350011bf8be7 148
simon 0:350011bf8be7 149 default:
simon 0:350011bf8be7 150 pbuf_free(frame);
simon 0:350011bf8be7 151 frame = NULL;
simon 0:350011bf8be7 152 break;
simon 0:350011bf8be7 153 }
simon 0:350011bf8be7 154
simon 0:350011bf8be7 155 /* pbuf_free(frame); */
simon 0:350011bf8be7 156 }
simon 0:350011bf8be7 157
simon 0:350011bf8be7 158
simon 0:350011bf8be7 159
simon 0:350011bf8be7 160
simon 0:350011bf8be7 161 }
simon 0:350011bf8be7 162
simon 0:350011bf8be7 163 err_t eth_init(struct netif *netif) {
simon 0:350011bf8be7 164 LWIP_ASSERT("netif != NULL", (netif != NULL));
simon 0:350011bf8be7 165
simon 0:350011bf8be7 166 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 0x2EA);
simon 0:350011bf8be7 167
simon 0:350011bf8be7 168 /* maximum transfer unit */
simon 0:350011bf8be7 169 netif->mtu = 0x2EA;
simon 0:350011bf8be7 170
simon 0:350011bf8be7 171 /* device capabilities */
simon 0:350011bf8be7 172 /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
simon 0:350011bf8be7 173 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_IGMP;
simon 0:350011bf8be7 174
simon 0:350011bf8be7 175 netif->state = NULL;
simon 0:350011bf8be7 176 eth_netif = netif;
simon 0:350011bf8be7 177
simon 0:350011bf8be7 178 netif->name[0] = IFNAME0;
simon 0:350011bf8be7 179 netif->name[1] = IFNAME1;
simon 0:350011bf8be7 180
simon 0:350011bf8be7 181 /* We directly use etharp_output() here to save a function call.
simon 0:350011bf8be7 182 * You can instead declare your own function an call etharp_output()
simon 0:350011bf8be7 183 * from it if you have to do some checks before sending (e.g. if link
simon 0:350011bf8be7 184 * is available...) */
simon 0:350011bf8be7 185 netif->output = etharp_output;
simon 0:350011bf8be7 186 netif->linkoutput = eth_output;
simon 0:350011bf8be7 187
simon 0:350011bf8be7 188 if (!pEth) pEth = new Ethernet(); // only create Ethernet object if required
simon 0:350011bf8be7 189
simon 0:350011bf8be7 190 return ERR_OK;
simon 0:350011bf8be7 191 }
simon 0:350011bf8be7 192
simon 0:350011bf8be7 193 void eth_free()
simon 0:350011bf8be7 194 {
simon 0:350011bf8be7 195 if(pEth)
simon 0:350011bf8be7 196 delete pEth;
simon 0:350011bf8be7 197 pEth = NULL;
simon 0:350011bf8be7 198 }
simon 0:350011bf8be7 199
simon 0:350011bf8be7 200 void eth_address(char* mac) {
simon 0:350011bf8be7 201 pEth->address(mac);
simon 0:350011bf8be7 202 }
simon 0:350011bf8be7 203
simon 0:350011bf8be7 204 Ethernet* eth_interface() {
simon 0:350011bf8be7 205 return pEth;
simon 0:350011bf8be7 206 }
simon 0:350011bf8be7 207
simon 0:350011bf8be7 208 #ifdef __cplusplus
simon 0:350011bf8be7 209 };
simon 0:350011bf8be7 210 #endif
simon 0:350011bf8be7 211
simon 0:350011bf8be7 212 #endif