f
Fork of lwip by
examples/purehttpc/main.cpp@0:5e1631496985, 2012-05-08 (annotated)
- Committer:
- root@mbed.org
- Date:
- Tue May 08 15:32:10 2012 +0100
- Revision:
- 0:5e1631496985
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
root@mbed.org | 0:5e1631496985 | 1 | #include "lwip/opt.h" |
root@mbed.org | 0:5e1631496985 | 2 | #include "lwip/stats.h" |
root@mbed.org | 0:5e1631496985 | 3 | #include "lwip/sys.h" |
root@mbed.org | 0:5e1631496985 | 4 | #include "lwip/pbuf.h" |
root@mbed.org | 0:5e1631496985 | 5 | #include "lwip/udp.h" |
root@mbed.org | 0:5e1631496985 | 6 | #include "lwip/tcp.h" |
root@mbed.org | 0:5e1631496985 | 7 | #include "lwip/dns.h" |
root@mbed.org | 0:5e1631496985 | 8 | #include "lwip/dhcp.h" |
root@mbed.org | 0:5e1631496985 | 9 | #include "lwip/init.h" |
root@mbed.org | 0:5e1631496985 | 10 | #include "lwip/netif.h" |
root@mbed.org | 0:5e1631496985 | 11 | #include "netif/etharp.h" |
root@mbed.org | 0:5e1631496985 | 12 | #include "netif/loopif.h" |
root@mbed.org | 0:5e1631496985 | 13 | #include "device.h" |
root@mbed.org | 0:5e1631496985 | 14 | |
root@mbed.org | 0:5e1631496985 | 15 | #include "mbed.h" |
root@mbed.org | 0:5e1631496985 | 16 | |
root@mbed.org | 0:5e1631496985 | 17 | DigitalOut led(LED1); |
root@mbed.org | 0:5e1631496985 | 18 | |
root@mbed.org | 0:5e1631496985 | 19 | /* Struct with hold the Ethernet Data */ |
root@mbed.org | 0:5e1631496985 | 20 | struct netif netif_data; |
root@mbed.org | 0:5e1631496985 | 21 | |
root@mbed.org | 0:5e1631496985 | 22 | /* Every time called if a packet is received for a */ |
root@mbed.org | 0:5e1631496985 | 23 | /* TCPConnection which registerd this Callback. */ |
root@mbed.org | 0:5e1631496985 | 24 | err_t recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { |
root@mbed.org | 0:5e1631496985 | 25 | printf("recv_callback\n"); |
root@mbed.org | 0:5e1631496985 | 26 | if (p==NULL) { |
root@mbed.org | 0:5e1631496985 | 27 | printf("Connection closed by server!\n"); |
root@mbed.org | 0:5e1631496985 | 28 | return ERR_OK; |
root@mbed.org | 0:5e1631496985 | 29 | } |
root@mbed.org | 0:5e1631496985 | 30 | |
root@mbed.org | 0:5e1631496985 | 31 | while (p) { |
root@mbed.org | 0:5e1631496985 | 32 | char * ch = (char*)p->payload; |
root@mbed.org | 0:5e1631496985 | 33 | printf("\n>>>>>>>>>>>>\n"); |
root@mbed.org | 0:5e1631496985 | 34 | for (int i=0; i < p->len; i++) { |
root@mbed.org | 0:5e1631496985 | 35 | printf("%c", ch[i]); |
root@mbed.org | 0:5e1631496985 | 36 | } |
root@mbed.org | 0:5e1631496985 | 37 | printf("\n<<<<<<<<<<<<\n"); |
root@mbed.org | 0:5e1631496985 | 38 | p = p->next; |
root@mbed.org | 0:5e1631496985 | 39 | } |
root@mbed.org | 0:5e1631496985 | 40 | tcp_recved(pcb, p->tot_len); |
root@mbed.org | 0:5e1631496985 | 41 | pbuf_free(p); |
root@mbed.org | 0:5e1631496985 | 42 | return ERR_OK; |
root@mbed.org | 0:5e1631496985 | 43 | } |
root@mbed.org | 0:5e1631496985 | 44 | |
root@mbed.org | 0:5e1631496985 | 45 | /* Connection etablished, lets try to get a http page */ |
root@mbed.org | 0:5e1631496985 | 46 | err_t connected_callback(void *arg, struct tcp_pcb *pcb, err_t err) { |
root@mbed.org | 0:5e1631496985 | 47 | tcp_recv(pcb, &recv_callback); |
root@mbed.org | 0:5e1631496985 | 48 | printf("Connected Callback!\n"); |
root@mbed.org | 0:5e1631496985 | 49 | char msg[] = "GET / HTTP/1.1\r\nHost: www.google.co.uk\r\n\r\n"; |
root@mbed.org | 0:5e1631496985 | 50 | if (tcp_write(pcb, msg, strlen(msg), 1) != ERR_OK) { |
root@mbed.org | 0:5e1631496985 | 51 | error("Could not write", 0); |
root@mbed.org | 0:5e1631496985 | 52 | } |
root@mbed.org | 0:5e1631496985 | 53 | return ERR_OK; |
root@mbed.org | 0:5e1631496985 | 54 | } |
root@mbed.org | 0:5e1631496985 | 55 | |
root@mbed.org | 0:5e1631496985 | 56 | int main(void) { |
root@mbed.org | 0:5e1631496985 | 57 | /* Create and initialise variables */ |
root@mbed.org | 0:5e1631496985 | 58 | struct netif *netif = &netif_data; |
root@mbed.org | 0:5e1631496985 | 59 | struct ip_addr ipaddr; |
root@mbed.org | 0:5e1631496985 | 60 | struct ip_addr target; |
root@mbed.org | 0:5e1631496985 | 61 | struct ip_addr netmask; |
root@mbed.org | 0:5e1631496985 | 62 | struct ip_addr gateway; |
root@mbed.org | 0:5e1631496985 | 63 | |
root@mbed.org | 0:5e1631496985 | 64 | Ticker tickFast, tickSlow, tickARP, eth_tick, dns_tick, dhcp_coarse, dhcp_fine; |
root@mbed.org | 0:5e1631496985 | 65 | char *hostname = "mbed-c3p0"; |
root@mbed.org | 0:5e1631496985 | 66 | |
root@mbed.org | 0:5e1631496985 | 67 | /* Start Network with DHCP */ |
root@mbed.org | 0:5e1631496985 | 68 | IP4_ADDR(&netmask, 255,255,255,255); |
root@mbed.org | 0:5e1631496985 | 69 | IP4_ADDR(&gateway, 0,0,0,0); |
root@mbed.org | 0:5e1631496985 | 70 | IP4_ADDR(&ipaddr, 0,0,0,0); |
root@mbed.org | 0:5e1631496985 | 71 | IP4_ADDR(&target, 209,85,229,147); // www.google.co.uk |
root@mbed.org | 0:5e1631496985 | 72 | |
root@mbed.org | 0:5e1631496985 | 73 | /* Initialise after configuration */ |
root@mbed.org | 0:5e1631496985 | 74 | lwip_init(); |
root@mbed.org | 0:5e1631496985 | 75 | |
root@mbed.org | 0:5e1631496985 | 76 | netif->hwaddr_len = ETHARP_HWADDR_LEN; |
root@mbed.org | 0:5e1631496985 | 77 | device_address((char *)netif->hwaddr); |
root@mbed.org | 0:5e1631496985 | 78 | |
root@mbed.org | 0:5e1631496985 | 79 | netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, device_init, ip_input); |
root@mbed.org | 0:5e1631496985 | 80 | netif->hostname = hostname; |
root@mbed.org | 0:5e1631496985 | 81 | netif_set_default(netif); |
root@mbed.org | 0:5e1631496985 | 82 | dhcp_start(netif); // <-- Use DHCP |
root@mbed.org | 0:5e1631496985 | 83 | |
root@mbed.org | 0:5e1631496985 | 84 | /* Initialise all needed timers */ |
root@mbed.org | 0:5e1631496985 | 85 | tickARP.attach_us( ðarp_tmr, ARP_TMR_INTERVAL * 1000); |
root@mbed.org | 0:5e1631496985 | 86 | tickFast.attach_us(&tcp_fasttmr, TCP_FAST_INTERVAL * 1000); |
root@mbed.org | 0:5e1631496985 | 87 | tickSlow.attach_us(&tcp_slowtmr, TCP_SLOW_INTERVAL * 1000); |
root@mbed.org | 0:5e1631496985 | 88 | dns_tick.attach_us(&dns_tmr, DNS_TMR_INTERVAL * 1000); |
root@mbed.org | 0:5e1631496985 | 89 | dhcp_coarse.attach_us(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_MSECS * 1000); |
root@mbed.org | 0:5e1631496985 | 90 | dhcp_fine.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); |
root@mbed.org | 0:5e1631496985 | 91 | |
root@mbed.org | 0:5e1631496985 | 92 | // Wait for an IP Address |
root@mbed.org | 0:5e1631496985 | 93 | while (!netif_is_up(netif)) { |
root@mbed.org | 0:5e1631496985 | 94 | device_poll(); |
root@mbed.org | 0:5e1631496985 | 95 | } |
root@mbed.org | 0:5e1631496985 | 96 | |
root@mbed.org | 0:5e1631496985 | 97 | /* Connect do google */ |
root@mbed.org | 0:5e1631496985 | 98 | struct tcp_pcb *pcb = tcp_new(); |
root@mbed.org | 0:5e1631496985 | 99 | tcp_connect(pcb, &target, 80, &connected_callback); |
root@mbed.org | 0:5e1631496985 | 100 | |
root@mbed.org | 0:5e1631496985 | 101 | /* Give the signal that we are ready */ |
root@mbed.org | 0:5e1631496985 | 102 | printf("entering while loop\n"); |
root@mbed.org | 0:5e1631496985 | 103 | while (1) { |
root@mbed.org | 0:5e1631496985 | 104 | led = !led; |
root@mbed.org | 0:5e1631496985 | 105 | wait(0.1); |
root@mbed.org | 0:5e1631496985 | 106 | device_poll(); |
root@mbed.org | 0:5e1631496985 | 107 | } |
root@mbed.org | 0:5e1631496985 | 108 | } |
root@mbed.org | 0:5e1631496985 | 109 |