Joe Danlloh / lwip

Dependencies:   mbed

Fork of lwip by mbed unsupported

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "TCPCallbackConnection.h"
00002 #include "NetServer.h"
00003 #include "mbed.h"
00004 
00005 /* Some Lights */
00006 DigitalOut led1(LED1);
00007 DigitalOut led2(LED2);
00008 int x = 0;
00009 
00010 /* Two static html pages ready for serving, includeing http header */
00011 const char *page[] = {
00012     "HTTP/1.1 200 OK\r\n"
00013     "Server:mbed embedded\r\n"
00014     "Content-Type: text/html\r\n"
00015     "Content-Length: 190\r\n"
00016     "\r\n"
00017     "<html>\r\n"
00018     "  <header>\r\n"
00019     "    <title>Whats up?<title>\r\n"
00020     "  </header>\r\n"
00021     "  <body>\r\n"
00022     "    <h1>Hello World!!11elf</h1>\r\n"
00023     "    <p>Have a greate day.</p>\r\n"
00024     "    <a href=\"/1.html\">turn  </a>\r\n"
00025     "  </body>\r\n"
00026     "</html>\r\n"
00027     "\r\n"
00028     ,
00029     "HTTP/1.1 200 OK\r\n"
00030     "Server:mbed embedded\r\n"
00031     "Content-Type: text/html\r\n"
00032     "Content-Length: 178\r\n"
00033     "\r\n"
00034     "<html>\r\n"
00035     "  <header>\r\n"
00036     "    <title>LED Power<title>\r\n"
00037     "  </header>\r\n"
00038     "  <body>\r\n"
00039     "    <h1>Lets rotate!!11elf</h1>\r\n"
00040     "    <p>Bam!!!!!!!!!!.</p>\r\n"
00041     "    <a href=\"/\">back</a>\r\n"
00042     "  </body>\r\n"
00043     "</html>\r\n"
00044     "\r\n"
00045 };
00046 
00047 /* Every time called if a packet is received for a */
00048 /* TCPConnection which registerd this Callback. */
00049 err_t recv_callback(TCPCallbackConnection *arg, struct pbuf *p, err_t err) {
00050     int i;
00051     char *data;
00052 
00053     /* Check if status is ok and data is arrived. */
00054     if (err == ERR_OK && p != NULL) {
00055         /* Inform TCP that we have taken the data. */
00056         arg->recved(p->tot_len);
00057         data = static_cast<char *>(p->payload);
00058 
00059         /* If the data is a GET request we can handle it. */
00060         if (strncmp(data, "GET ", 4) == 0) {
00061             for (i = 0; i < 40; i++) {
00062                 if (((char *)data + 4)[i] == ' ' ||
00063                         ((char *)data + 4)[i] == '\r' ||
00064                         ((char *)data + 4)[i] == '\n') {
00065                     ((char *)data + 4)[i] = 0;
00066                 }
00067             }
00068 
00069             /* Check the URL */
00070             /* We only know two URLs /1.html and default */
00071             /* store the URL information in our global x variable to change twinkle behaviour. */
00072             x = (strncmp((char *)(data + 4), "/1.html", 7) == 0)? 1: 0;
00073             /* Clean up the Memory */
00074 
00075             /* Send the right page as answere. */
00076             if (arg->write((void *)page[x], strlen(page[x])) != ERR_OK) {
00077                 error("Could not write", 0);
00078             }
00079         } else {
00080             /* This message was not a GET request.
00081                Maybe it was a follow message or another kind of request. */
00082         }
00083     } else {
00084         /* No data arrived */
00085         /* That means the client closes the connection and sent us a packet with FIN flag set to 1. */
00086         /* We have to cleanup and destroy out TCPConnection. */
00087         arg->close();
00088         NetServer::get()->free(arg);
00089     }
00090 
00091     pbuf_free(p);
00092     /* Don't panic! Everything is fine. */
00093     return ERR_OK;
00094 }
00095 
00096 /* Accept an incomming call on the registered port */
00097 err_t accept_callback(TCPCallbackListener *arg, struct tcp_pcb *npcb, err_t err) {
00098     /* Create a new TCPConnection for the new incomming call. */
00099     TCPCallbackConnection *con = new TCPCallbackConnection((TCPListener *)arg, npcb,
00100             NO_SENT_FNC,
00101             &recv_callback, /* set only a callback for receiving data. */
00102             NO_POLL_FNC,
00103             NO_CONNECT_FNC,
00104             NO_ERR_FNC
00105                                                           );
00106 
00107     /* Don't panic! Everything is fine. */
00108     return ERR_OK;
00109 }
00110 
00111 int main(void) {
00112     /* Bind a function to a tcp port */
00113     NetServer::ready()->bindTCPPort(80, &accept_callback);
00114 
00115     /* Give the signal that we are ready */
00116     printf("entering while loop\n");
00117     while (1) {
00118         wait(0.1);
00119         NetServer::poll();
00120         if (x) {
00121             led1 = !led1;
00122         } else {
00123             led2 = !led2;
00124         }
00125     }
00126 }
00127