Ilya I / Mbed 2 deprecated pub_iva2k_HTTPDynamicPage

Dependencies:   mbed lwip

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "HTTPServer.h"
00003 #include "HTTPFS.h"
00004 #include "HTTPDynamicPage.h"
00005 
00006 #define MAX_DYNAMIC_CONTENT_LEN     2048
00007 char dynamic_content[MAX_DYNAMIC_CONTENT_LEN];
00008 
00009 const char content_fmt[] = 
00010 "<HTML>"
00011 "<HEAD>"
00012 "<title>Dynamic Page</title>"
00013 "</HEAD>"
00014 "<BODY>"
00015 "<H1>Hello World</H1>"
00016 "<p>Page generated dynamically from code.</p>"
00017 "<p>URL=%s</p>"
00018 "<p>Header Fields=%s</p>"
00019 "</BODY></HTML>"
00020 ;
00021 
00022 DigitalOut led1(LED1, "led1");
00023 DigitalOut led2(LED2, "led2");
00024 DigitalOut led3(LED3, "led3");
00025 DigitalOut led4(LED4, "led4");
00026 
00027 #define LED_ETH_LINK(val) do { led4=val; } while (0)
00028 
00029 extern Ethernet eth;        // eth is defined elsewhere, avoid compiler error.
00030 Serial pc(USBTX, USBRX);
00031 Ticker eth_timer;
00032 
00033 void eth_link_status() {
00034     static bool first = true;           // Avoid duplicate IP report on the very first pass
00035     static int eth_link = -1;           // Last state of eth link
00036     static unsigned int old_ip = 0;     // Last IP address
00037     int new_link = eth.link();
00038     if (eth_link != new_link) {
00039         if (new_link) {
00040             // From http://mbed.org/forum/post/909/
00041             NetServer *net = NetServer::get();
00042             struct ip_addr ip = net->getIPAddr();
00043 //            struct ip_addr gw = net->getGateway();
00044 //            struct ip_addr nm = net->getNetmask();
00045 //            struct ip_addr dns = net->getDNS1();
00046             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);
00047             first = false;
00048             if (1 && ip.addr != old_ip) {
00049                 // Create a link file to our IP.
00050                 FILE *fp = fopen("/local/Start.url", "w");  // Create link to own IP
00051                 if (fp) {
00052                     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);
00053                     fclose(fp);
00054                     old_ip = ip.addr;
00055                 }
00056             }
00057             
00058         }
00059         else {
00060             printf("IP: <link down>\r\n");
00061         }
00062         LED_ETH_LINK(new_link);
00063         eth_link = new_link;
00064     }
00065 }
00066 
00067 HTTPStatus myDynamicPage(HTTPConnection *con, HTTPDynamicPageData *pd) {
00068 #if 0
00069     // Static example. With this, we don't really need HTTPStaticPage
00070     pd->size = 0;    // let it measure our page
00071     pd->page = (char*)content;    // Nothing dynamic about that yet, but we can now get loose here.
00072     pd->page_free = NULL;    // No mem free() needed
00073 #elif 0
00074     // Dynamic example, static buffer
00075     pd->size = sprintf(dynamic_content, content_fmt, con->getURL(), con->getHeaderFields());
00076     pd->page = (char*)dynamic_content;
00077     pd->page_free = NULL;    // No mem free() needed
00078 if(pd->size > sizeof(dynamic_content)) printf("ASSERTION FAILED: %s:%d\r\n", __FILE__, __LINE__);    // Buffer overrun
00079 #else
00080     // Dynamic example, dynamic buffer
00081     int size = sizeof(content_fmt) + 512;    // Just guess how much the data will expand
00082     char *buf = (char *)malloc(size);
00083     if (buf) {
00084         pd->size = sprintf(buf, content_fmt, con->getURL(), con->getHeaderFields());
00085         pd->page = buf;
00086         pd->page_free = &free;    // Use free() when done
00087 if(pd->size > size) printf("ASSERTION FAILED: %s:%d\r\n", __FILE__, __LINE__);    // Buffer overrun
00088 #endif
00089     }
00090     return HTTP_OK;
00091 }
00092 
00093 int main(void) {
00094     char mac[6];
00095 
00096     led1=1;
00097     led2=1;
00098     led3=1;
00099     led4=1;
00100 
00101     char *hostname = "mbed";
00102     HTTPServer http(hostname);    // Use DHCP
00103     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.
00104 
00105     eth.address(mac);
00106     pc.printf("\r\n\r\nHTTPServer \"%s\" started\r\nMAC %02X:%02X:%02X:%02X:%02X:%02X\r\n",
00107         hostname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
00108 
00109     led1=0;
00110 
00111     led2=0;
00112 
00113     http.addHandler(new HTTPDynamicPage("/dynamic.htm", &myDynamicPage));
00114     led3=0;
00115 
00116 // FIXME: BUG If eth is not plugged, http.bind() hangs for awhile!
00117     http.bind();
00118 
00119     led4 = 0;
00120     eth_timer.attach(&eth_link_status, 0.1);
00121     while(1) {
00122         http.poll();    // Have to call this method at least every 250ms to let the http server run.
00123         wait(0.100);
00124     }
00125 }
00126 
00127 //END