f

Dependencies:   mbed

Fork of lwip by mbed unsupported

Committer:
idijoeteque
Date:
Thu Aug 03 10:24:16 2017 +0000
Revision:
1:803fdc96fbd7
Parent:
0:5e1631496985
hello

Who changed what in which revision?

UserRevisionLine numberNew contents of line
root@mbed.org 0:5e1631496985 1 #include "TCPCallbackConnection.h"
root@mbed.org 0:5e1631496985 2 #include "NetServer.h"
root@mbed.org 0:5e1631496985 3 #include "mbed.h"
root@mbed.org 0:5e1631496985 4
root@mbed.org 0:5e1631496985 5 /* Some Lights */
root@mbed.org 0:5e1631496985 6 DigitalOut led1(LED1);
root@mbed.org 0:5e1631496985 7 DigitalOut led2(LED2);
root@mbed.org 0:5e1631496985 8 int x = 0;
root@mbed.org 0:5e1631496985 9
root@mbed.org 0:5e1631496985 10 /* Two static html pages ready for serving, includeing http header */
root@mbed.org 0:5e1631496985 11 const char *page[] = {
root@mbed.org 0:5e1631496985 12 "HTTP/1.1 200 OK\r\n"
root@mbed.org 0:5e1631496985 13 "Server:mbed embedded\r\n"
root@mbed.org 0:5e1631496985 14 "Content-Type: text/html\r\n"
root@mbed.org 0:5e1631496985 15 "Content-Length: 190\r\n"
root@mbed.org 0:5e1631496985 16 "\r\n"
root@mbed.org 0:5e1631496985 17 "<html>\r\n"
root@mbed.org 0:5e1631496985 18 " <header>\r\n"
root@mbed.org 0:5e1631496985 19 " <title>Whats up?<title>\r\n"
root@mbed.org 0:5e1631496985 20 " </header>\r\n"
root@mbed.org 0:5e1631496985 21 " <body>\r\n"
root@mbed.org 0:5e1631496985 22 " <h1>Hello World!!11elf</h1>\r\n"
root@mbed.org 0:5e1631496985 23 " <p>Have a greate day.</p>\r\n"
root@mbed.org 0:5e1631496985 24 " <a href=\"/1.html\">turn </a>\r\n"
root@mbed.org 0:5e1631496985 25 " </body>\r\n"
root@mbed.org 0:5e1631496985 26 "</html>\r\n"
root@mbed.org 0:5e1631496985 27 "\r\n"
root@mbed.org 0:5e1631496985 28 ,
root@mbed.org 0:5e1631496985 29 "HTTP/1.1 200 OK\r\n"
root@mbed.org 0:5e1631496985 30 "Server:mbed embedded\r\n"
root@mbed.org 0:5e1631496985 31 "Content-Type: text/html\r\n"
root@mbed.org 0:5e1631496985 32 "Content-Length: 178\r\n"
root@mbed.org 0:5e1631496985 33 "\r\n"
root@mbed.org 0:5e1631496985 34 "<html>\r\n"
root@mbed.org 0:5e1631496985 35 " <header>\r\n"
root@mbed.org 0:5e1631496985 36 " <title>LED Power<title>\r\n"
root@mbed.org 0:5e1631496985 37 " </header>\r\n"
root@mbed.org 0:5e1631496985 38 " <body>\r\n"
root@mbed.org 0:5e1631496985 39 " <h1>Lets rotate!!11elf</h1>\r\n"
root@mbed.org 0:5e1631496985 40 " <p>Bam!!!!!!!!!!.</p>\r\n"
root@mbed.org 0:5e1631496985 41 " <a href=\"/\">back</a>\r\n"
root@mbed.org 0:5e1631496985 42 " </body>\r\n"
root@mbed.org 0:5e1631496985 43 "</html>\r\n"
root@mbed.org 0:5e1631496985 44 "\r\n"
root@mbed.org 0:5e1631496985 45 };
root@mbed.org 0:5e1631496985 46
root@mbed.org 0:5e1631496985 47 /* Every time called if a packet is received for a */
root@mbed.org 0:5e1631496985 48 /* TCPConnection which registerd this Callback. */
root@mbed.org 0:5e1631496985 49 err_t recv_callback(TCPCallbackConnection *arg, struct pbuf *p, err_t err) {
root@mbed.org 0:5e1631496985 50 int i;
root@mbed.org 0:5e1631496985 51 char *data;
root@mbed.org 0:5e1631496985 52
root@mbed.org 0:5e1631496985 53 /* Check if status is ok and data is arrived. */
root@mbed.org 0:5e1631496985 54 if (err == ERR_OK && p != NULL) {
root@mbed.org 0:5e1631496985 55 /* Inform TCP that we have taken the data. */
root@mbed.org 0:5e1631496985 56 arg->recved(p->tot_len);
root@mbed.org 0:5e1631496985 57 data = static_cast<char *>(p->payload);
root@mbed.org 0:5e1631496985 58
root@mbed.org 0:5e1631496985 59 /* If the data is a GET request we can handle it. */
root@mbed.org 0:5e1631496985 60 if (strncmp(data, "GET ", 4) == 0) {
root@mbed.org 0:5e1631496985 61 for (i = 0; i < 40; i++) {
root@mbed.org 0:5e1631496985 62 if (((char *)data + 4)[i] == ' ' ||
root@mbed.org 0:5e1631496985 63 ((char *)data + 4)[i] == '\r' ||
root@mbed.org 0:5e1631496985 64 ((char *)data + 4)[i] == '\n') {
root@mbed.org 0:5e1631496985 65 ((char *)data + 4)[i] = 0;
root@mbed.org 0:5e1631496985 66 }
root@mbed.org 0:5e1631496985 67 }
root@mbed.org 0:5e1631496985 68
root@mbed.org 0:5e1631496985 69 /* Check the URL */
root@mbed.org 0:5e1631496985 70 /* We only know two URLs /1.html and default */
root@mbed.org 0:5e1631496985 71 /* store the URL information in our global x variable to change twinkle behaviour. */
root@mbed.org 0:5e1631496985 72 x = (strncmp((char *)(data + 4), "/1.html", 7) == 0)? 1: 0;
root@mbed.org 0:5e1631496985 73 /* Clean up the Memory */
root@mbed.org 0:5e1631496985 74
root@mbed.org 0:5e1631496985 75 /* Send the right page as answere. */
root@mbed.org 0:5e1631496985 76 if (arg->write((void *)page[x], strlen(page[x])) != ERR_OK) {
root@mbed.org 0:5e1631496985 77 error("Could not write", 0);
root@mbed.org 0:5e1631496985 78 }
root@mbed.org 0:5e1631496985 79 } else {
root@mbed.org 0:5e1631496985 80 /* This message was not a GET request.
root@mbed.org 0:5e1631496985 81 Maybe it was a follow message or another kind of request. */
root@mbed.org 0:5e1631496985 82 }
root@mbed.org 0:5e1631496985 83 } else {
root@mbed.org 0:5e1631496985 84 /* No data arrived */
root@mbed.org 0:5e1631496985 85 /* That means the client closes the connection and sent us a packet with FIN flag set to 1. */
root@mbed.org 0:5e1631496985 86 /* We have to cleanup and destroy out TCPConnection. */
root@mbed.org 0:5e1631496985 87 arg->close();
root@mbed.org 0:5e1631496985 88 NetServer::get()->free(arg);
root@mbed.org 0:5e1631496985 89 }
root@mbed.org 0:5e1631496985 90
root@mbed.org 0:5e1631496985 91 pbuf_free(p);
root@mbed.org 0:5e1631496985 92 /* Don't panic! Everything is fine. */
root@mbed.org 0:5e1631496985 93 return ERR_OK;
root@mbed.org 0:5e1631496985 94 }
root@mbed.org 0:5e1631496985 95
root@mbed.org 0:5e1631496985 96 /* Accept an incomming call on the registered port */
root@mbed.org 0:5e1631496985 97 err_t accept_callback(TCPCallbackListener *arg, struct tcp_pcb *npcb, err_t err) {
root@mbed.org 0:5e1631496985 98 /* Create a new TCPConnection for the new incomming call. */
root@mbed.org 0:5e1631496985 99 TCPCallbackConnection *con = new TCPCallbackConnection((TCPListener *)arg, npcb,
root@mbed.org 0:5e1631496985 100 NO_SENT_FNC,
root@mbed.org 0:5e1631496985 101 &recv_callback, /* set only a callback for receiving data. */
root@mbed.org 0:5e1631496985 102 NO_POLL_FNC,
root@mbed.org 0:5e1631496985 103 NO_CONNECT_FNC,
root@mbed.org 0:5e1631496985 104 NO_ERR_FNC
root@mbed.org 0:5e1631496985 105 );
root@mbed.org 0:5e1631496985 106
root@mbed.org 0:5e1631496985 107 /* Don't panic! Everything is fine. */
root@mbed.org 0:5e1631496985 108 return ERR_OK;
root@mbed.org 0:5e1631496985 109 }
root@mbed.org 0:5e1631496985 110
root@mbed.org 0:5e1631496985 111 int main(void) {
root@mbed.org 0:5e1631496985 112 /* Bind a function to a tcp port */
root@mbed.org 0:5e1631496985 113 NetServer::ready()->bindTCPPort(80, &accept_callback);
root@mbed.org 0:5e1631496985 114
root@mbed.org 0:5e1631496985 115 /* Give the signal that we are ready */
root@mbed.org 0:5e1631496985 116 printf("entering while loop\n");
root@mbed.org 0:5e1631496985 117 while (1) {
root@mbed.org 0:5e1631496985 118 wait(0.1);
root@mbed.org 0:5e1631496985 119 NetServer::poll();
root@mbed.org 0:5e1631496985 120 if (x) {
root@mbed.org 0:5e1631496985 121 led1 = !led1;
root@mbed.org 0:5e1631496985 122 } else {
root@mbed.org 0:5e1631496985 123 led2 = !led2;
root@mbed.org 0:5e1631496985 124 }
root@mbed.org 0:5e1631496985 125 }
root@mbed.org 0:5e1631496985 126 }
root@mbed.org 0:5e1631496985 127