Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:b0aa20381646, 2009-12-10 (annotated)
- Committer:
- iva2k
- Date:
- Thu Dec 10 08:59:18 2009 +0000
- Revision:
- 0:b0aa20381646
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| iva2k | 0:b0aa20381646 | 1 | #include "mbed.h" |
| iva2k | 0:b0aa20381646 | 2 | #include "HTTPServer.h" |
| iva2k | 0:b0aa20381646 | 3 | #include "HTTPFS.h" |
| iva2k | 0:b0aa20381646 | 4 | #include "HTTPDynamicPage.h" |
| iva2k | 0:b0aa20381646 | 5 | |
| iva2k | 0:b0aa20381646 | 6 | #define MAX_DYNAMIC_CONTENT_LEN 2048 |
| iva2k | 0:b0aa20381646 | 7 | char dynamic_content[MAX_DYNAMIC_CONTENT_LEN]; |
| iva2k | 0:b0aa20381646 | 8 | |
| iva2k | 0:b0aa20381646 | 9 | const char content_fmt[] = |
| iva2k | 0:b0aa20381646 | 10 | "<HTML>" |
| iva2k | 0:b0aa20381646 | 11 | "<HEAD>" |
| iva2k | 0:b0aa20381646 | 12 | "<title>Dynamic Page</title>" |
| iva2k | 0:b0aa20381646 | 13 | "</HEAD>" |
| iva2k | 0:b0aa20381646 | 14 | "<BODY>" |
| iva2k | 0:b0aa20381646 | 15 | "<H1>Hello World</H1>" |
| iva2k | 0:b0aa20381646 | 16 | "<p>Page generated dynamically from code.</p>" |
| iva2k | 0:b0aa20381646 | 17 | "<p>URL=%s</p>" |
| iva2k | 0:b0aa20381646 | 18 | "<p>Header Fields=%s</p>" |
| iva2k | 0:b0aa20381646 | 19 | "</BODY></HTML>" |
| iva2k | 0:b0aa20381646 | 20 | ; |
| iva2k | 0:b0aa20381646 | 21 | |
| iva2k | 0:b0aa20381646 | 22 | DigitalOut led1(LED1, "led1"); |
| iva2k | 0:b0aa20381646 | 23 | DigitalOut led2(LED2, "led2"); |
| iva2k | 0:b0aa20381646 | 24 | DigitalOut led3(LED3, "led3"); |
| iva2k | 0:b0aa20381646 | 25 | DigitalOut led4(LED4, "led4"); |
| iva2k | 0:b0aa20381646 | 26 | |
| iva2k | 0:b0aa20381646 | 27 | #define LED_ETH_LINK(val) do { led4=val; } while (0) |
| iva2k | 0:b0aa20381646 | 28 | |
| iva2k | 0:b0aa20381646 | 29 | extern Ethernet eth; // eth is defined elsewhere, avoid compiler error. |
| iva2k | 0:b0aa20381646 | 30 | Serial pc(USBTX, USBRX); |
| iva2k | 0:b0aa20381646 | 31 | Ticker eth_timer; |
| iva2k | 0:b0aa20381646 | 32 | |
| iva2k | 0:b0aa20381646 | 33 | void eth_link_status() { |
| iva2k | 0:b0aa20381646 | 34 | static bool first = true; // Avoid duplicate IP report on the very first pass |
| iva2k | 0:b0aa20381646 | 35 | static int eth_link = -1; // Last state of eth link |
| iva2k | 0:b0aa20381646 | 36 | static unsigned int old_ip = 0; // Last IP address |
| iva2k | 0:b0aa20381646 | 37 | int new_link = eth.link(); |
| iva2k | 0:b0aa20381646 | 38 | if (eth_link != new_link) { |
| iva2k | 0:b0aa20381646 | 39 | if (new_link) { |
| iva2k | 0:b0aa20381646 | 40 | // From http://mbed.org/forum/post/909/ |
| iva2k | 0:b0aa20381646 | 41 | NetServer *net = NetServer::get(); |
| iva2k | 0:b0aa20381646 | 42 | struct ip_addr ip = net->getIPAddr(); |
| iva2k | 0:b0aa20381646 | 43 | // struct ip_addr gw = net->getGateway(); |
| iva2k | 0:b0aa20381646 | 44 | // struct ip_addr nm = net->getNetmask(); |
| iva2k | 0:b0aa20381646 | 45 | // struct ip_addr dns = net->getDNS1(); |
| iva2k | 0:b0aa20381646 | 46 | if (!first) printf("IP: %hhu.%hhu.%hhu.%hhu\r\n", (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF); |
| iva2k | 0:b0aa20381646 | 47 | first = false; |
| iva2k | 0:b0aa20381646 | 48 | if (1 && ip.addr != old_ip) { |
| iva2k | 0:b0aa20381646 | 49 | // Create a link file to our IP. |
| iva2k | 0:b0aa20381646 | 50 | FILE *fp = fopen("/local/Start.url", "w"); // Create link to own IP |
| iva2k | 0:b0aa20381646 | 51 | if (fp) { |
| iva2k | 0:b0aa20381646 | 52 | fprintf(fp, "[InternetShortcut]\r\nURL=http://%hhu.%hhu.%hhu.%hhu/\r\n", (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF); |
| iva2k | 0:b0aa20381646 | 53 | fclose(fp); |
| iva2k | 0:b0aa20381646 | 54 | old_ip = ip.addr; |
| iva2k | 0:b0aa20381646 | 55 | } |
| iva2k | 0:b0aa20381646 | 56 | } |
| iva2k | 0:b0aa20381646 | 57 | |
| iva2k | 0:b0aa20381646 | 58 | } |
| iva2k | 0:b0aa20381646 | 59 | else { |
| iva2k | 0:b0aa20381646 | 60 | printf("IP: <link down>\r\n"); |
| iva2k | 0:b0aa20381646 | 61 | } |
| iva2k | 0:b0aa20381646 | 62 | LED_ETH_LINK(new_link); |
| iva2k | 0:b0aa20381646 | 63 | eth_link = new_link; |
| iva2k | 0:b0aa20381646 | 64 | } |
| iva2k | 0:b0aa20381646 | 65 | } |
| iva2k | 0:b0aa20381646 | 66 | |
| iva2k | 0:b0aa20381646 | 67 | HTTPStatus myDynamicPage(HTTPConnection *con, HTTPDynamicPageData *pd) { |
| iva2k | 0:b0aa20381646 | 68 | #if 0 |
| iva2k | 0:b0aa20381646 | 69 | // Static example. With this, we don't really need HTTPStaticPage |
| iva2k | 0:b0aa20381646 | 70 | pd->size = 0; // let it measure our page |
| iva2k | 0:b0aa20381646 | 71 | pd->page = (char*)content; // Nothing dynamic about that yet, but we can now get loose here. |
| iva2k | 0:b0aa20381646 | 72 | pd->page_free = NULL; // No mem free() needed |
| iva2k | 0:b0aa20381646 | 73 | #elif 0 |
| iva2k | 0:b0aa20381646 | 74 | // Dynamic example, static buffer |
| iva2k | 0:b0aa20381646 | 75 | pd->size = sprintf(dynamic_content, content_fmt, con->getURL(), con->getHeaderFields()); |
| iva2k | 0:b0aa20381646 | 76 | pd->page = (char*)dynamic_content; |
| iva2k | 0:b0aa20381646 | 77 | pd->page_free = NULL; // No mem free() needed |
| iva2k | 0:b0aa20381646 | 78 | if(pd->size > sizeof(dynamic_content)) printf("ASSERTION FAILED: %s:%d\r\n", __FILE__, __LINE__); // Buffer overrun |
| iva2k | 0:b0aa20381646 | 79 | #else |
| iva2k | 0:b0aa20381646 | 80 | // Dynamic example, dynamic buffer |
| iva2k | 0:b0aa20381646 | 81 | int size = sizeof(content_fmt) + 512; // Just guess how much the data will expand |
| iva2k | 0:b0aa20381646 | 82 | char *buf = (char *)malloc(size); |
| iva2k | 0:b0aa20381646 | 83 | if (buf) { |
| iva2k | 0:b0aa20381646 | 84 | pd->size = sprintf(buf, content_fmt, con->getURL(), con->getHeaderFields()); |
| iva2k | 0:b0aa20381646 | 85 | pd->page = buf; |
| iva2k | 0:b0aa20381646 | 86 | pd->page_free = &free; // Use free() when done |
| iva2k | 0:b0aa20381646 | 87 | if(pd->size > size) printf("ASSERTION FAILED: %s:%d\r\n", __FILE__, __LINE__); // Buffer overrun |
| iva2k | 0:b0aa20381646 | 88 | #endif |
| iva2k | 0:b0aa20381646 | 89 | } |
| iva2k | 0:b0aa20381646 | 90 | return HTTP_OK; |
| iva2k | 0:b0aa20381646 | 91 | } |
| iva2k | 0:b0aa20381646 | 92 | |
| iva2k | 0:b0aa20381646 | 93 | int main(void) { |
| iva2k | 0:b0aa20381646 | 94 | char mac[6]; |
| iva2k | 0:b0aa20381646 | 95 | |
| iva2k | 0:b0aa20381646 | 96 | led1=1; |
| iva2k | 0:b0aa20381646 | 97 | led2=1; |
| iva2k | 0:b0aa20381646 | 98 | led3=1; |
| iva2k | 0:b0aa20381646 | 99 | led4=1; |
| iva2k | 0:b0aa20381646 | 100 | |
| iva2k | 0:b0aa20381646 | 101 | char *hostname = "mbed"; |
| iva2k | 0:b0aa20381646 | 102 | HTTPServer http(hostname); // Use DHCP |
| iva2k | 0:b0aa20381646 | 103 | http.timeout(60000); // Sets the timout for a HTTP request. The timout is the time wich is allowed to spent between two incomming TCP packets. If the time is passed the connection will be closed. |
| iva2k | 0:b0aa20381646 | 104 | |
| iva2k | 0:b0aa20381646 | 105 | eth.address(mac); |
| iva2k | 0:b0aa20381646 | 106 | pc.printf("\r\n\r\nHTTPServer \"%s\" started\r\nMAC %02X:%02X:%02X:%02X:%02X:%02X\r\n", |
| iva2k | 0:b0aa20381646 | 107 | hostname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
| iva2k | 0:b0aa20381646 | 108 | |
| iva2k | 0:b0aa20381646 | 109 | led1=0; |
| iva2k | 0:b0aa20381646 | 110 | |
| iva2k | 0:b0aa20381646 | 111 | led2=0; |
| iva2k | 0:b0aa20381646 | 112 | |
| iva2k | 0:b0aa20381646 | 113 | http.addHandler(new HTTPDynamicPage("/dynamic.htm", &myDynamicPage)); |
| iva2k | 0:b0aa20381646 | 114 | led3=0; |
| iva2k | 0:b0aa20381646 | 115 | |
| iva2k | 0:b0aa20381646 | 116 | // FIXME: BUG If eth is not plugged, http.bind() hangs for awhile! |
| iva2k | 0:b0aa20381646 | 117 | http.bind(); |
| iva2k | 0:b0aa20381646 | 118 | |
| iva2k | 0:b0aa20381646 | 119 | led4 = 0; |
| iva2k | 0:b0aa20381646 | 120 | eth_timer.attach(ð_link_status, 0.1); |
| iva2k | 0:b0aa20381646 | 121 | while(1) { |
| iva2k | 0:b0aa20381646 | 122 | http.poll(); // Have to call this method at least every 250ms to let the http server run. |
| iva2k | 0:b0aa20381646 | 123 | wait(0.100); |
| iva2k | 0:b0aa20381646 | 124 | } |
| iva2k | 0:b0aa20381646 | 125 | } |
| iva2k | 0:b0aa20381646 | 126 | |
| iva2k | 0:b0aa20381646 | 127 | //END |