Simple server for AGH accademic purpose

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of HTTP_SD_Server_K64F by FRDM-K64F Code Share

Committer:
patlas
Date:
Thu Dec 01 23:09:37 2016 +0000
Revision:
9:aebb88e6e653
Parent:
8:a27748c1fc33
semifinal demo version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gsteiert 0:28bc7ce77e20 1 #include "mbed.h"
gsteiert 0:28bc7ce77e20 2 #include "EthernetInterface.h"
patlas 8:a27748c1fc33 3
gsteiert 0:28bc7ce77e20 4 #include <stdio.h>
gsteiert 0:28bc7ce77e20 5 #include <string.h>
gsteiert 0:28bc7ce77e20 6
gsteiert 0:28bc7ce77e20 7 #define HTTPD_SERVER_PORT 80
gsteiert 0:28bc7ce77e20 8 #define HTTPD_MAX_REQ_LENGTH 1023
patlas 9:aebb88e6e653 9 #define HTTPD_MAX_HDR_LENGTH 2056
patlas 9:aebb88e6e653 10
patlas 9:aebb88e6e653 11 #define LED_R 26
patlas 9:aebb88e6e653 12 #define LED_G 22
patlas 9:aebb88e6e653 13 #define LED_B 21
patlas 8:a27748c1fc33 14
gsteiert 3:4f71a37a1ed2 15
gsteiert 3:4f71a37a1ed2 16 Serial uart(USBTX, USBRX);
gsteiert 0:28bc7ce77e20 17
gsteiert 3:4f71a37a1ed2 18
gsteiert 0:28bc7ce77e20 19 EthernetInterface eth;
gsteiert 0:28bc7ce77e20 20 TCPSocketServer server;
gsteiert 0:28bc7ce77e20 21 TCPSocketConnection client;
gsteiert 0:28bc7ce77e20 22
gsteiert 0:28bc7ce77e20 23 char buffer[HTTPD_MAX_REQ_LENGTH+1];
gsteiert 0:28bc7ce77e20 24 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
patlas 8:a27748c1fc33 25
gsteiert 0:28bc7ce77e20 26 char *uristr;
gsteiert 0:28bc7ce77e20 27 char *eou;
gsteiert 0:28bc7ce77e20 28 char *qrystr;
gsteiert 0:28bc7ce77e20 29
patlas 8:a27748c1fc33 30
gsteiert 0:28bc7ce77e20 31 int rdCnt;
gsteiert 0:28bc7ce77e20 32
patlas 8:a27748c1fc33 33 char *first = "/first";
patlas 8:a27748c1fc33 34 char *sec = "/second";
patlas 9:aebb88e6e653 35 char *led = "/led";
patlas 9:aebb88e6e653 36 char *ledr = "/led/r";
patlas 9:aebb88e6e653 37 char *ledg = "/led/g";
patlas 9:aebb88e6e653 38 char *ledb = "/led/b";
patlas 9:aebb88e6e653 39
patlas 9:aebb88e6e653 40 DigitalOut ledR(LED1,1);
patlas 9:aebb88e6e653 41 DigitalOut ledG(LED2,1);
patlas 9:aebb88e6e653 42 DigitalOut ledB(LED3,1);
patlas 9:aebb88e6e653 43
patlas 9:aebb88e6e653 44 char led_stat = 0;
patlas 8:a27748c1fc33 45 void show_page(char* uri)
gsteiert 0:28bc7ce77e20 46 {
patlas 9:aebb88e6e653 47 uart.printf("Trying to open requested uri\r\n");
patlas 9:aebb88e6e653 48 uart.printf("%s\r\n",uri);
patlas 8:a27748c1fc33 49 char *lstchr_ptr = strrchr(uri, NULL) -1; //function try to find char which is non ascii (recently set to 0 so no ascii)
patlas 9:aebb88e6e653 50 if(!strcmp(uri, first)){
patlas 8:a27748c1fc33 51 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
patlas 8:a27748c1fc33 52 client.send(httpHeader,strlen(httpHeader));
patlas 8:a27748c1fc33 53 sprintf(httpHeader,"<html><head><title>First_one</title></head><body><h1>First page has been opened</h1></body></html>");
gsteiert 0:28bc7ce77e20 54 client.send(httpHeader,strlen(httpHeader));
patlas 8:a27748c1fc33 55 }
patlas 9:aebb88e6e653 56 else if(!strcmp(uri, sec)){
patlas 8:a27748c1fc33 57 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
patlas 8:a27748c1fc33 58 client.send(httpHeader,strlen(httpHeader));
patlas 8:a27748c1fc33 59 sprintf(httpHeader,"<html><head><title>Seond_one</title></head><body><h1>Second page has been opened</h1></body></html>");
patlas 8:a27748c1fc33 60 client.send(httpHeader,strlen(httpHeader));
patlas 8:a27748c1fc33 61 }
patlas 9:aebb88e6e653 62 else if(!strcmp(uri, led)){
patlas 9:aebb88e6e653 63 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
patlas 9:aebb88e6e653 64 client.send(httpHeader,strlen(httpHeader));
patlas 9:aebb88e6e653 65 sprintf(httpHeader,"<html><head><title>LED</title></head><body> \n"
patlas 9:aebb88e6e653 66 "<button id='rb' onclick='hget(\"r\")'>RED</button><br/> \n"
patlas 9:aebb88e6e653 67 "<button id='gb' onclick='hget(\"g\")'>GREEN</button><br/> \n"
patlas 9:aebb88e6e653 68 "<button id='bb' onclick='hget(\"b\")'>BLUE</button><br/> \n"
patlas 9:aebb88e6e653 69 "<div id='ramka'>NAPIS</div><br/> \n"
patlas 9:aebb88e6e653 70 "</h1> \n"
patlas 9:aebb88e6e653 71 "</body>\n"
patlas 9:aebb88e6e653 72 "<script type='text/javascript'>\n"
patlas 9:aebb88e6e653 73 "function hget(val){ \n"
patlas 9:aebb88e6e653 74 " var x = new XMLHttpRequest(); \n"
patlas 9:aebb88e6e653 75 " x.onreadystatechange = function() { \n"
patlas 9:aebb88e6e653 76 " if (x.readyState == 4 && x.status == 200){ \n"
patlas 9:aebb88e6e653 77 " if(x.responseText=='r') document.body.style.backgroundColor='red';\n"
patlas 9:aebb88e6e653 78 " else if (x.responseText=='b') document.body.style.backgroundColor='blue';\n"
patlas 9:aebb88e6e653 79 " else if (x.responseText=='g') document.body.style.backgroundColor='green';\n"
patlas 9:aebb88e6e653 80 " else document.body.style.backgroundColor='white';}}\n"
patlas 9:aebb88e6e653 81 " x.open('GET', '/led/'+val, true);\n"
patlas 9:aebb88e6e653 82 " x.send(null);}\n"
patlas 9:aebb88e6e653 83 " </script>\n"
patlas 9:aebb88e6e653 84 "</html>");
patlas 9:aebb88e6e653 85 client.send(httpHeader,strlen(httpHeader));
patlas 9:aebb88e6e653 86 }
patlas 9:aebb88e6e653 87 else if(!strcmp(uri, ledr)){
patlas 9:aebb88e6e653 88 led_stat ^= 1;
patlas 9:aebb88e6e653 89 ledR.write(!(led_stat&1));
patlas 9:aebb88e6e653 90 if(led_stat & 1){
patlas 9:aebb88e6e653 91 client.send("r",1); //+zapalic led
patlas 9:aebb88e6e653 92 } else {
patlas 9:aebb88e6e653 93 client.send("w",1);
patlas 9:aebb88e6e653 94 }
patlas 9:aebb88e6e653 95 //uart.printf("RED\n");
patlas 9:aebb88e6e653 96 }
patlas 9:aebb88e6e653 97 else if(!strcmp(uri, ledg)){
patlas 9:aebb88e6e653 98 led_stat ^= 2;
patlas 9:aebb88e6e653 99 ledG.write(led_stat&2);
patlas 9:aebb88e6e653 100 if(!(led_stat & 2)){
patlas 9:aebb88e6e653 101 client.send("g",1); //+zapalic led
patlas 9:aebb88e6e653 102 } else {
patlas 9:aebb88e6e653 103 client.send("w",1);
patlas 9:aebb88e6e653 104 }
patlas 9:aebb88e6e653 105 //uart.printf("GREEN\n");
patlas 9:aebb88e6e653 106 }
patlas 9:aebb88e6e653 107 else if(!strcmp(uri, ledb)){
patlas 9:aebb88e6e653 108 led_stat ^= 4;
patlas 9:aebb88e6e653 109 ledB.write(!(led_stat&4));
patlas 9:aebb88e6e653 110 if(led_stat & 4){
patlas 9:aebb88e6e653 111 client.send("b",1); //+zapalic led
patlas 9:aebb88e6e653 112 } else {
patlas 9:aebb88e6e653 113 client.send("w",1);
patlas 9:aebb88e6e653 114 }
patlas 9:aebb88e6e653 115 //uart.printf("BLUE\n");
patlas 9:aebb88e6e653 116 }
patlas 8:a27748c1fc33 117 else{
patlas 8:a27748c1fc33 118 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
patlas 8:a27748c1fc33 119 client.send(httpHeader,strlen(httpHeader));
patlas 8:a27748c1fc33 120 sprintf(httpHeader,"<html><head><title>ERROR</title></head><body><h1>THERE IS NO SUCH PAGE</h1></body></html>");
patlas 8:a27748c1fc33 121 client.send(httpHeader,strlen(httpHeader));
gsteiert 0:28bc7ce77e20 122 }
gsteiert 0:28bc7ce77e20 123 }
gsteiert 0:28bc7ce77e20 124
gsteiert 0:28bc7ce77e20 125 int main (void)
gsteiert 0:28bc7ce77e20 126 {
patlas 9:aebb88e6e653 127 //ledR.write(1);
patlas 9:aebb88e6e653 128 //ledB.write(1);
gsteiert 3:4f71a37a1ed2 129 // Serial Interface eth;
gsteiert 3:4f71a37a1ed2 130 uart.baud(115200);
patlas 9:aebb88e6e653 131 uart.printf("Initializing\r\n");
gsteiert 3:4f71a37a1ed2 132
patlas 8:a27748c1fc33 133 // EthernetInterface eth;
patlas 9:aebb88e6e653 134 uart.printf("Initializing Ethernet ...\r\n");
patlas 8:a27748c1fc33 135 if(!eth.init("192.168.5.10", "255.255.255.0", "192.168.5.50")){//Init interface with static IP
patlas 9:aebb88e6e653 136 uart.printf("Ethernet interface configured properly\r\n");
gsteiert 3:4f71a37a1ed2 137 }
patlas 9:aebb88e6e653 138 uart.printf("Setting interface UP ...\r\n");
gsteiert 0:28bc7ce77e20 139 eth.connect();
patlas 9:aebb88e6e653 140 uart.printf("IP: %s\r\n", eth.getIPAddress());
patlas 9:aebb88e6e653 141 uart.printf("Mask: %s\r\n", eth.getNetworkMask());
gsteiert 0:28bc7ce77e20 142
gsteiert 0:28bc7ce77e20 143 // TCPSocketServer server;
patlas 9:aebb88e6e653 144 uart.printf("Starting TCP server at port: %d\r\n", HTTPD_SERVER_PORT);
gsteiert 0:28bc7ce77e20 145 server.bind(HTTPD_SERVER_PORT);
gsteiert 0:28bc7ce77e20 146 server.listen();
patlas 9:aebb88e6e653 147 uart.printf("Server starts listening at port: %d\r\n", HTTPD_SERVER_PORT);
gsteiert 0:28bc7ce77e20 148
gsteiert 0:28bc7ce77e20 149 while (true) {
patlas 8:a27748c1fc33 150 uart.printf("\nWaiting for new connection...\r\n");
gsteiert 0:28bc7ce77e20 151 server.accept(client);
gsteiert 0:28bc7ce77e20 152 client.set_blocking(false, 1500); // Timeout after (1.5)s
gsteiert 0:28bc7ce77e20 153
patlas 8:a27748c1fc33 154 uart.printf("Client with IP %s has connected with server.\r\n", client.get_address());
gsteiert 0:28bc7ce77e20 155 while (true) {
gsteiert 0:28bc7ce77e20 156 int n = client.receive(buffer, sizeof(buffer));
gsteiert 0:28bc7ce77e20 157 if (n <= 0) break;
gsteiert 3:4f71a37a1ed2 158 uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
gsteiert 0:28bc7ce77e20 159 if (n >= 1024) {
gsteiert 0:28bc7ce77e20 160 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
gsteiert 0:28bc7ce77e20 161 client.send(httpHeader,strlen(httpHeader));
gsteiert 0:28bc7ce77e20 162 client.send(buffer,n);
gsteiert 0:28bc7ce77e20 163 break;
gsteiert 0:28bc7ce77e20 164 } else {
gsteiert 0:28bc7ce77e20 165 buffer[n]=0;
gsteiert 0:28bc7ce77e20 166 }
gsteiert 0:28bc7ce77e20 167 if (!strncmp(buffer, "GET ", 4)) {
gsteiert 0:28bc7ce77e20 168 uristr = buffer + 4;
gsteiert 0:28bc7ce77e20 169 eou = strstr(uristr, " ");
gsteiert 0:28bc7ce77e20 170 if (eou == NULL) {
gsteiert 0:28bc7ce77e20 171 sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
gsteiert 0:28bc7ce77e20 172 client.send(httpHeader,strlen(httpHeader));
gsteiert 0:28bc7ce77e20 173 client.send(buffer,n);
gsteiert 0:28bc7ce77e20 174 } else {
gsteiert 0:28bc7ce77e20 175 *eou = 0;
patlas 8:a27748c1fc33 176 show_page(uristr);
gsteiert 0:28bc7ce77e20 177 }
gsteiert 0:28bc7ce77e20 178 }
gsteiert 0:28bc7ce77e20 179 }
gsteiert 0:28bc7ce77e20 180
gsteiert 0:28bc7ce77e20 181 client.close();
gsteiert 0:28bc7ce77e20 182 }
gsteiert 0:28bc7ce77e20 183 }