HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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