Example self-announcing webserver which controls a servo through a smallHTML userinterface.

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:56:01 2010 +0000
Revision:
0:a259777c45a3

        

Who changed what in which revision?

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