f
Fork of lwip by
examples/purehttpd/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 | /* Some Lights */ |
root@mbed.org | 0:5e1631496985 | 18 | DigitalOut led1(LED1); |
root@mbed.org | 0:5e1631496985 | 19 | DigitalOut led2(LED2); |
root@mbed.org | 0:5e1631496985 | 20 | int x = 0; |
root@mbed.org | 0:5e1631496985 | 21 | |
root@mbed.org | 0:5e1631496985 | 22 | /* Struct with hold the Ethernet Data */ |
root@mbed.org | 0:5e1631496985 | 23 | struct netif netif_data; |
root@mbed.org | 0:5e1631496985 | 24 | |
root@mbed.org | 0:5e1631496985 | 25 | /* Two static html pages ready for serving, includeing http header */ |
root@mbed.org | 0:5e1631496985 | 26 | const char *page[] = { |
root@mbed.org | 0:5e1631496985 | 27 | "HTTP/1.1 200 OK\r\n" |
root@mbed.org | 0:5e1631496985 | 28 | "Server:mbed embedded\r\n" |
root@mbed.org | 0:5e1631496985 | 29 | "Content-Type: text/html\r\n" |
root@mbed.org | 0:5e1631496985 | 30 | "Content-Length: 190\r\n" |
root@mbed.org | 0:5e1631496985 | 31 | "\r\n" |
root@mbed.org | 0:5e1631496985 | 32 | "<html>\r\n" |
root@mbed.org | 0:5e1631496985 | 33 | " <header>\r\n" |
root@mbed.org | 0:5e1631496985 | 34 | " <title>Whats up?<title>\r\n" |
root@mbed.org | 0:5e1631496985 | 35 | " </header>\r\n" |
root@mbed.org | 0:5e1631496985 | 36 | " <body>\r\n" |
root@mbed.org | 0:5e1631496985 | 37 | " <h1>Hello World!!11elf</h1>\r\n" |
root@mbed.org | 0:5e1631496985 | 38 | " <p>Have a greate day.</p>\r\n" |
root@mbed.org | 0:5e1631496985 | 39 | " <a href=\"/1.html\">turn </a>\r\n" |
root@mbed.org | 0:5e1631496985 | 40 | " </body>\r\n" |
root@mbed.org | 0:5e1631496985 | 41 | "</html>\r\n" |
root@mbed.org | 0:5e1631496985 | 42 | "\r\n" |
root@mbed.org | 0:5e1631496985 | 43 | , |
root@mbed.org | 0:5e1631496985 | 44 | "HTTP/1.1 200 OK\r\n" |
root@mbed.org | 0:5e1631496985 | 45 | "Server:mbed embedded\r\n" |
root@mbed.org | 0:5e1631496985 | 46 | "Content-Type: text/html\r\n" |
root@mbed.org | 0:5e1631496985 | 47 | "Content-Length: 178\r\n" |
root@mbed.org | 0:5e1631496985 | 48 | "\r\n" |
root@mbed.org | 0:5e1631496985 | 49 | "<html>\r\n" |
root@mbed.org | 0:5e1631496985 | 50 | " <header>\r\n" |
root@mbed.org | 0:5e1631496985 | 51 | " <title>LED Power<title>\r\n" |
root@mbed.org | 0:5e1631496985 | 52 | " </header>\r\n" |
root@mbed.org | 0:5e1631496985 | 53 | " <body>\r\n" |
root@mbed.org | 0:5e1631496985 | 54 | " <h1>Lets rotate!!11elf</h1>\r\n" |
root@mbed.org | 0:5e1631496985 | 55 | " <p>Bam!!!!!!!!!!.</p>\r\n" |
root@mbed.org | 0:5e1631496985 | 56 | " <a href=\"/\">back</a>\r\n" |
root@mbed.org | 0:5e1631496985 | 57 | " </body>\r\n" |
root@mbed.org | 0:5e1631496985 | 58 | "</html>\r\n" |
root@mbed.org | 0:5e1631496985 | 59 | "\r\n" |
root@mbed.org | 0:5e1631496985 | 60 | }; |
root@mbed.org | 0:5e1631496985 | 61 | |
root@mbed.org | 0:5e1631496985 | 62 | /* Every time called if a packet is received for a */ |
root@mbed.org | 0:5e1631496985 | 63 | /* TCPConnection which registerd this Callback. */ |
root@mbed.org | 0:5e1631496985 | 64 | err_t recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { |
root@mbed.org | 0:5e1631496985 | 65 | int i; |
root@mbed.org | 0:5e1631496985 | 66 | char *data; |
root@mbed.org | 0:5e1631496985 | 67 | |
root@mbed.org | 0:5e1631496985 | 68 | /* Check if status is ok and data is arrived. */ |
root@mbed.org | 0:5e1631496985 | 69 | if (err == ERR_OK && p != NULL) { |
root@mbed.org | 0:5e1631496985 | 70 | /* Inform TCP that we have taken the data. */ |
root@mbed.org | 0:5e1631496985 | 71 | tcp_recved(pcb, p->tot_len); |
root@mbed.org | 0:5e1631496985 | 72 | data = static_cast<char *>(p->payload); |
root@mbed.org | 0:5e1631496985 | 73 | |
root@mbed.org | 0:5e1631496985 | 74 | /* If the data is a GET request we can handle it. */ |
root@mbed.org | 0:5e1631496985 | 75 | if (strncmp(data, "GET ", 4) == 0) { |
root@mbed.org | 0:5e1631496985 | 76 | for (i = 0; i < 40; i++) { |
root@mbed.org | 0:5e1631496985 | 77 | if (((char *)data + 4)[i] == ' ' || |
root@mbed.org | 0:5e1631496985 | 78 | ((char *)data + 4)[i] == '\r' || |
root@mbed.org | 0:5e1631496985 | 79 | ((char *)data + 4)[i] == '\n') { |
root@mbed.org | 0:5e1631496985 | 80 | ((char *)data + 4)[i] = 0; |
root@mbed.org | 0:5e1631496985 | 81 | } |
root@mbed.org | 0:5e1631496985 | 82 | } |
root@mbed.org | 0:5e1631496985 | 83 | |
root@mbed.org | 0:5e1631496985 | 84 | /* Check the URL */ |
root@mbed.org | 0:5e1631496985 | 85 | /* We only know two URLs /1.html and default */ |
root@mbed.org | 0:5e1631496985 | 86 | /* store the URL information in our global x variable to change twinkle behaviour. */ |
root@mbed.org | 0:5e1631496985 | 87 | x = (strncmp((char *)(data + 4), "/1.html", 7) == 0)? 1: 0; |
root@mbed.org | 0:5e1631496985 | 88 | /* Clean up the Memory */ |
root@mbed.org | 0:5e1631496985 | 89 | pbuf_free(p); |
root@mbed.org | 0:5e1631496985 | 90 | |
root@mbed.org | 0:5e1631496985 | 91 | /* Send the right page as answere. */ |
root@mbed.org | 0:5e1631496985 | 92 | if (tcp_write(pcb, (void *)page[x], strlen(page[x]), 1) != ERR_OK) { |
root@mbed.org | 0:5e1631496985 | 93 | //error("Could not write", 0); |
root@mbed.org | 0:5e1631496985 | 94 | } |
root@mbed.org | 0:5e1631496985 | 95 | } else { |
root@mbed.org | 0:5e1631496985 | 96 | /* No data arrived */ |
root@mbed.org | 0:5e1631496985 | 97 | /* That means the client closes the connection and sent us a packet with FIN flag set to 1. */ |
root@mbed.org | 0:5e1631496985 | 98 | /* We have to cleanup and destroy out TCPConnection. */ |
root@mbed.org | 0:5e1631496985 | 99 | pbuf_free(p); |
root@mbed.org | 0:5e1631496985 | 100 | } |
root@mbed.org | 0:5e1631496985 | 101 | } |
root@mbed.org | 0:5e1631496985 | 102 | |
root@mbed.org | 0:5e1631496985 | 103 | /* Don't panic! Everything is fine. */ |
root@mbed.org | 0:5e1631496985 | 104 | return ERR_OK; |
root@mbed.org | 0:5e1631496985 | 105 | } |
root@mbed.org | 0:5e1631496985 | 106 | |
root@mbed.org | 0:5e1631496985 | 107 | /* Accept an incomming call on the registered port */ |
root@mbed.org | 0:5e1631496985 | 108 | err_t accept_callback(void *arg, struct tcp_pcb *npcb, err_t err) { |
root@mbed.org | 0:5e1631496985 | 109 | LWIP_UNUSED_ARG(arg); |
root@mbed.org | 0:5e1631496985 | 110 | /* Subscribe a receive callback function */ |
root@mbed.org | 0:5e1631496985 | 111 | tcp_recv(npcb, &recv_callback); |
root@mbed.org | 0:5e1631496985 | 112 | |
root@mbed.org | 0:5e1631496985 | 113 | /* Don't panic! Everything is fine. */ |
root@mbed.org | 0:5e1631496985 | 114 | return ERR_OK; |
root@mbed.org | 0:5e1631496985 | 115 | } |
root@mbed.org | 0:5e1631496985 | 116 | |
root@mbed.org | 0:5e1631496985 | 117 | int main(void) { |
root@mbed.org | 0:5e1631496985 | 118 | /* Create and initialise variables */ |
root@mbed.org | 0:5e1631496985 | 119 | struct netif *netif = &netif_data; |
root@mbed.org | 0:5e1631496985 | 120 | struct ip_addr ipaddr; |
root@mbed.org | 0:5e1631496985 | 121 | struct ip_addr netmask; |
root@mbed.org | 0:5e1631496985 | 122 | struct ip_addr gateway; |
root@mbed.org | 0:5e1631496985 | 123 | |
root@mbed.org | 0:5e1631496985 | 124 | Ticker tickFast, tickSlow, tickARP, eth_tick, dns_tick, dhcp_coarse, dhcp_fine; |
root@mbed.org | 0:5e1631496985 | 125 | char *hostname = "mbed-c3p0"; |
root@mbed.org | 0:5e1631496985 | 126 | x = 0; |
root@mbed.org | 0:5e1631496985 | 127 | |
root@mbed.org | 0:5e1631496985 | 128 | /* Start Network with DHCP */ |
root@mbed.org | 0:5e1631496985 | 129 | IP4_ADDR(&netmask, 255,255,255,255); |
root@mbed.org | 0:5e1631496985 | 130 | IP4_ADDR(&gateway, 0,0,0,0); |
root@mbed.org | 0:5e1631496985 | 131 | IP4_ADDR(&ipaddr, 0,0,0,0); |
root@mbed.org | 0:5e1631496985 | 132 | |
root@mbed.org | 0:5e1631496985 | 133 | /* Initialise after configuration */ |
root@mbed.org | 0:5e1631496985 | 134 | lwip_init(); |
root@mbed.org | 0:5e1631496985 | 135 | |
root@mbed.org | 0:5e1631496985 | 136 | netif->hwaddr_len = ETHARP_HWADDR_LEN; |
root@mbed.org | 0:5e1631496985 | 137 | device_address((char *)netif->hwaddr); |
root@mbed.org | 0:5e1631496985 | 138 | |
root@mbed.org | 0:5e1631496985 | 139 | netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, device_init, ip_input); |
root@mbed.org | 0:5e1631496985 | 140 | netif->hostname = hostname; |
root@mbed.org | 0:5e1631496985 | 141 | netif_set_default(netif); |
root@mbed.org | 0:5e1631496985 | 142 | dhcp_start(netif); // <-- Use DHCP |
root@mbed.org | 0:5e1631496985 | 143 | |
root@mbed.org | 0:5e1631496985 | 144 | /* Initialise all needed timers */ |
root@mbed.org | 0:5e1631496985 | 145 | tickARP.attach_us( ðarp_tmr, ARP_TMR_INTERVAL * 1000); |
root@mbed.org | 0:5e1631496985 | 146 | tickFast.attach_us(&tcp_fasttmr, TCP_FAST_INTERVAL * 1000); |
root@mbed.org | 0:5e1631496985 | 147 | tickSlow.attach_us(&tcp_slowtmr, TCP_SLOW_INTERVAL * 1000); |
root@mbed.org | 0:5e1631496985 | 148 | dns_tick.attach_us(&dns_tmr, DNS_TMR_INTERVAL * 1000); |
root@mbed.org | 0:5e1631496985 | 149 | dhcp_coarse.attach_us(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_MSECS * 1000); |
root@mbed.org | 0:5e1631496985 | 150 | dhcp_fine.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); |
root@mbed.org | 0:5e1631496985 | 151 | |
root@mbed.org | 0:5e1631496985 | 152 | /* Bind a function to a tcp port */ |
root@mbed.org | 0:5e1631496985 | 153 | struct tcp_pcb *pcb = tcp_new(); |
root@mbed.org | 0:5e1631496985 | 154 | if (tcp_bind(pcb, IP_ADDR_ANY, 80) == ERR_OK) { |
root@mbed.org | 0:5e1631496985 | 155 | pcb = tcp_listen(pcb); |
root@mbed.org | 0:5e1631496985 | 156 | tcp_accept(pcb, &accept_callback); |
root@mbed.org | 0:5e1631496985 | 157 | } |
root@mbed.org | 0:5e1631496985 | 158 | |
root@mbed.org | 0:5e1631496985 | 159 | /* Give the signal that we are ready */ |
root@mbed.org | 0:5e1631496985 | 160 | printf("entering while loop\n"); |
root@mbed.org | 0:5e1631496985 | 161 | while(1) { |
root@mbed.org | 0:5e1631496985 | 162 | device_poll(); |
root@mbed.org | 0:5e1631496985 | 163 | if (x) { |
root@mbed.org | 0:5e1631496985 | 164 | led1 = !led1; |
root@mbed.org | 0:5e1631496985 | 165 | wait(0.2); |
root@mbed.org | 0:5e1631496985 | 166 | } else { |
root@mbed.org | 0:5e1631496985 | 167 | led2 = !led2; |
root@mbed.org | 0:5e1631496985 | 168 | wait(0.2); |
root@mbed.org | 0:5e1631496985 | 169 | } |
root@mbed.org | 0:5e1631496985 | 170 | } |
root@mbed.org | 0:5e1631496985 | 171 | } |
root@mbed.org | 0:5e1631496985 | 172 |