Webserver for peach-board

Committer:
thudt90
Date:
Thu Mar 12 08:26:27 2015 +0000
Revision:
1:f1f734dd23ee
Parent:
0:6ec14f880a00
Just test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thudt90 0:6ec14f880a00 1 #include "HTTPServer.h"
thudt90 0:6ec14f880a00 2
thudt90 1:f1f734dd23ee 3 #include "webpage.h"
thudt90 1:f1f734dd23ee 4 #include "mbed.h"
thudt90 1:f1f734dd23ee 5
thudt90 0:6ec14f880a00 6 #define INVALID_FORMATTER "No valid formatter specified"
thudt90 0:6ec14f880a00 7
thudt90 1:f1f734dd23ee 8 #define HTPPRECEIVERGETOK 8
thudt90 1:f1f734dd23ee 9
thudt90 1:f1f734dd23ee 10 #define HTTPCTRREDON 1
thudt90 1:f1f734dd23ee 11 #define HTTPCTRREDOFF 2
thudt90 1:f1f734dd23ee 12 #define HTTPCTRGREENON 3
thudt90 1:f1f734dd23ee 13 #define HTTPCTRGREENOFF 4
thudt90 1:f1f734dd23ee 14 #define HTTPCTRBLUEON 5
thudt90 1:f1f734dd23ee 15 #define HTTPCTRBLUEOFF 6
thudt90 1:f1f734dd23ee 16 #define HTTPCTRLEADSTATE 7
thudt90 1:f1f734dd23ee 17 #define HTTPBUTTONPRESS 10
thudt90 1:f1f734dd23ee 18
thudt90 1:f1f734dd23ee 19 #define STRING_LED_LENGTH 8
thudt90 1:f1f734dd23ee 20 #define LED_STT(x) x?"ON":"OF"
thudt90 1:f1f734dd23ee 21 #define BUTTON_STT(x) x?"ON":"OF"
thudt90 1:f1f734dd23ee 22
thudt90 1:f1f734dd23ee 23 static unsigned char LedRed = 0, LedGreen = 0, LedBlue = 0;
thudt90 1:f1f734dd23ee 24 extern unsigned char button;
thudt90 1:f1f734dd23ee 25
thudt90 1:f1f734dd23ee 26 const char RespondOnRed[] = {"HTTP/1.1 200 OK\r\n"
thudt90 1:f1f734dd23ee 27 "Server: AVR_Small_Webserver\r\n"
thudt90 1:f1f734dd23ee 28 "WWW-Authenticate: Basic realm=\"NeedPassword\""
thudt90 1:f1f734dd23ee 29 "\r\nContent-Type: text/html\r\n\r\n"
thudt90 1:f1f734dd23ee 30 "ONRED"};
thudt90 1:f1f734dd23ee 31 const char RespondOnGreen[] = {"HTTP/1.1 200 OK\r\n"
thudt90 1:f1f734dd23ee 32 "Server: AVR_Small_Webserver\r\n"
thudt90 1:f1f734dd23ee 33 "WWW-Authenticate: Basic realm=\"NeedPassword\""
thudt90 1:f1f734dd23ee 34 "\r\nContent-Type: text/html\r\n\r\n"
thudt90 1:f1f734dd23ee 35 "ONGREEN"};
thudt90 1:f1f734dd23ee 36 const char RespondOnBlue[] = {"HTTP/1.1 200 OK\r\n"
thudt90 1:f1f734dd23ee 37 "Server: AVR_Small_Webserver\r\n"
thudt90 1:f1f734dd23ee 38 "WWW-Authenticate: Basic realm=\"NeedPassword\""
thudt90 1:f1f734dd23ee 39 "\r\nContent-Type: text/html\r\n\r\n"
thudt90 1:f1f734dd23ee 40 "ONBLUE"};
thudt90 1:f1f734dd23ee 41 const char RespondOff[] = {"HTTP/1.1 200 OK\r\n"
thudt90 1:f1f734dd23ee 42 "Server: AVR_Small_Webserver\r\n"
thudt90 1:f1f734dd23ee 43 "WWW-Authenticate: Basic realm=\"NeedPassword\""
thudt90 1:f1f734dd23ee 44 "\r\nContent-Type: text/html\r\n\r\n"
thudt90 1:f1f734dd23ee 45 "OFF"};
thudt90 1:f1f734dd23ee 46 const char ResponseHdr[] = {"HTTP/1.1 200 OK\r\n"
thudt90 1:f1f734dd23ee 47 "Server: AVR_Small_Webserver\r\n"
thudt90 1:f1f734dd23ee 48 "WWW-Authenticate: Basic realm=\"NeedPassword\""
thudt90 1:f1f734dd23ee 49 "\r\nContent-Type: text/html\r\n\r\n"};
thudt90 1:f1f734dd23ee 50
thudt90 1:f1f734dd23ee 51 DigitalOut ledRed(LED1);
thudt90 1:f1f734dd23ee 52 DigitalOut ledGreen(LED2);
thudt90 1:f1f734dd23ee 53 DigitalOut ledBlue(LED3);
thudt90 1:f1f734dd23ee 54
thudt90 0:6ec14f880a00 55 bool cmp(char* a, char* b)
thudt90 0:6ec14f880a00 56 {
thudt90 0:6ec14f880a00 57 return strcmp(a,b) < 0;
thudt90 0:6ec14f880a00 58 }
thudt90 0:6ec14f880a00 59
thudt90 1:f1f734dd23ee 60 HTTPServer::HTTPServer():
thudt90 0:6ec14f880a00 61 socket(),
thudt90 0:6ec14f880a00 62 handlers(&cmp),
thudt90 0:6ec14f880a00 63 reply(),
thudt90 1:f1f734dd23ee 64 flagHTTP(),
thudt90 0:6ec14f880a00 65 command()
thudt90 0:6ec14f880a00 66 {
thudt90 0:6ec14f880a00 67 }
thudt90 0:6ec14f880a00 68
thudt90 0:6ec14f880a00 69 HTTPServer::~HTTPServer()
thudt90 0:6ec14f880a00 70 {
thudt90 0:6ec14f880a00 71 for(std::map<char*, RequestHandler*>::iterator itor = handlers.begin();
thudt90 0:6ec14f880a00 72 itor != handlers.end();
thudt90 0:6ec14f880a00 73 ++itor)
thudt90 0:6ec14f880a00 74 delete itor->second;
thudt90 1:f1f734dd23ee 75
thudt90 1:f1f734dd23ee 76 // if(formatter)
thudt90 1:f1f734dd23ee 77 // delete formatter;
thudt90 1:f1f734dd23ee 78 }
thudt90 1:f1f734dd23ee 79
thudt90 1:f1f734dd23ee 80 void HTTPServer::ledinit(void)
thudt90 1:f1f734dd23ee 81 {
thudt90 1:f1f734dd23ee 82 ledRed = 1 ;
thudt90 1:f1f734dd23ee 83 ledGreen = 1;
thudt90 1:f1f734dd23ee 84 ledBlue = 1;
thudt90 1:f1f734dd23ee 85 }
thudt90 1:f1f734dd23ee 86
thudt90 1:f1f734dd23ee 87 int HTTPServer::findstr(char *progstr,char* str,int len)
thudt90 1:f1f734dd23ee 88 {
thudt90 1:f1f734dd23ee 89 uint16_t i,j;
thudt90 1:f1f734dd23ee 90
thudt90 1:f1f734dd23ee 91 i=0;
thudt90 1:f1f734dd23ee 92 j=0;
thudt90 1:f1f734dd23ee 93 while(i<len){
thudt90 1:f1f734dd23ee 94 if(str[i++] != *(progstr + j++)){
thudt90 1:f1f734dd23ee 95 j = 0;
thudt90 1:f1f734dd23ee 96 }
thudt90 1:f1f734dd23ee 97 if(*(progstr + j) == 0){
thudt90 1:f1f734dd23ee 98 return(i-j);
thudt90 1:f1f734dd23ee 99 }
thudt90 1:f1f734dd23ee 100 }
thudt90 1:f1f734dd23ee 101 return(0xFFFF);
thudt90 0:6ec14f880a00 102 }
thudt90 0:6ec14f880a00 103
thudt90 0:6ec14f880a00 104 bool HTTPServer::init(int port)
thudt90 0:6ec14f880a00 105 {
thudt90 0:6ec14f880a00 106 socket.set_blocking(true);
thudt90 0:6ec14f880a00 107 if(socket.bind(port))
thudt90 0:6ec14f880a00 108 {
thudt90 0:6ec14f880a00 109 printf("Could not bind on port %d.\n", port);
thudt90 1:f1f734dd23ee 110 return false;
thudt90 0:6ec14f880a00 111 }
thudt90 1:f1f734dd23ee 112
thudt90 0:6ec14f880a00 113 if(socket.listen())
thudt90 0:6ec14f880a00 114 {
thudt90 0:6ec14f880a00 115 printf("Could not listen %d\n", port);
thudt90 0:6ec14f880a00 116 return false;
thudt90 0:6ec14f880a00 117 }
thudt90 1:f1f734dd23ee 118
thudt90 0:6ec14f880a00 119 return true;
thudt90 0:6ec14f880a00 120 }
thudt90 0:6ec14f880a00 121
thudt90 1:f1f734dd23ee 122 int HTTPServer::LedState_str(char* str_Led, unsigned char Red, unsigned char Green, unsigned char Blue)
thudt90 1:f1f734dd23ee 123 {
thudt90 1:f1f734dd23ee 124 char str_mp[15];
thudt90 1:f1f734dd23ee 125 int str_len;
thudt90 1:f1f734dd23ee 126 sprintf(str_mp, "%s %s %s", LED_STT(Red), LED_STT(Green), LED_STT(Blue));
thudt90 1:f1f734dd23ee 127 sprintf(str_Led,"%s%s", ResponseHdr,str_mp);
thudt90 1:f1f734dd23ee 128 str_len = sizeof(ResponseHdr) + STRING_LED_LENGTH;//STRING_LED_LENGTH: String response length with 2 space
thudt90 1:f1f734dd23ee 129 return str_len;
thudt90 1:f1f734dd23ee 130 }
thudt90 1:f1f734dd23ee 131
thudt90 1:f1f734dd23ee 132 int HTTPServer::ButtonState_str(char* str_Led, unsigned char button)
thudt90 1:f1f734dd23ee 133 {
thudt90 1:f1f734dd23ee 134 int str_len;
thudt90 1:f1f734dd23ee 135 sprintf(str_Led,"%s%s", ResponseHdr,BUTTON_STT(button));
thudt90 1:f1f734dd23ee 136 str_len = sizeof(ResponseHdr) + 2;
thudt90 1:f1f734dd23ee 137 return str_len;
thudt90 1:f1f734dd23ee 138 }
thudt90 1:f1f734dd23ee 139
thudt90 0:6ec14f880a00 140 void HTTPServer::run()
thudt90 0:6ec14f880a00 141 {
thudt90 1:f1f734dd23ee 142 char str_LED_State[150];
thudt90 1:f1f734dd23ee 143 char str_State1[150];
thudt90 1:f1f734dd23ee 144
thudt90 0:6ec14f880a00 145 TCPSocketConnection c;
thudt90 0:6ec14f880a00 146 while(true)
thudt90 0:6ec14f880a00 147 {
thudt90 0:6ec14f880a00 148 while(socket.accept(c));
thudt90 1:f1f734dd23ee 149 c.set_blocking(false, 10);
thudt90 0:6ec14f880a00 150 while(c.is_connected())
thudt90 0:6ec14f880a00 151 {
thudt90 0:6ec14f880a00 152 char buffer[512];
thudt90 0:6ec14f880a00 153 int n = c.receive_all(buffer, sizeof(buffer)-1);
thudt90 0:6ec14f880a00 154 if(n == 0)
thudt90 0:6ec14f880a00 155 {
thudt90 0:6ec14f880a00 156 c.close();
thudt90 0:6ec14f880a00 157 break;
thudt90 0:6ec14f880a00 158 }
thudt90 0:6ec14f880a00 159 else if(n != -1)
thudt90 0:6ec14f880a00 160 {
thudt90 0:6ec14f880a00 161 buffer[n] = '\0';
thudt90 0:6ec14f880a00 162 handle_request(buffer);
thudt90 1:f1f734dd23ee 163 if(!strcmp(reply,"GET"))
thudt90 1:f1f734dd23ee 164 {
thudt90 1:f1f734dd23ee 165 c.send(RespondGetOK, strlen(RespondGetOK)+1);
thudt90 1:f1f734dd23ee 166 c.send(Page3, strlen(Page3)+1);
thudt90 1:f1f734dd23ee 167 }
thudt90 1:f1f734dd23ee 168 else if(!strcmp(reply,"load"))
thudt90 1:f1f734dd23ee 169 c.send(str_LED_State, LedState_str(str_LED_State, LedRed,LedGreen,LedBlue));
thudt90 1:f1f734dd23ee 170 else if(!strcmp(reply,"LEDRED=ON"))
thudt90 1:f1f734dd23ee 171 { ledRed = 0; //led on board
thudt90 1:f1f734dd23ee 172 c.send(RespondOnRed,sizeof(RespondOnRed));
thudt90 1:f1f734dd23ee 173 LedRed = 1;
thudt90 1:f1f734dd23ee 174 }
thudt90 1:f1f734dd23ee 175 else if(!strcmp(reply,"LEDRED=OFF"))
thudt90 1:f1f734dd23ee 176 {
thudt90 1:f1f734dd23ee 177 ledRed = 1;
thudt90 1:f1f734dd23ee 178 c.send(RespondOff,sizeof(RespondOff));
thudt90 1:f1f734dd23ee 179 LedRed = 0;
thudt90 1:f1f734dd23ee 180 }
thudt90 1:f1f734dd23ee 181 else if(!strcmp(reply,"LEDGR=ON")){
thudt90 1:f1f734dd23ee 182 ledGreen = 0;
thudt90 1:f1f734dd23ee 183 c.send(RespondOnGreen,sizeof(RespondOnGreen));
thudt90 1:f1f734dd23ee 184 LedGreen = 1;
thudt90 1:f1f734dd23ee 185 }
thudt90 1:f1f734dd23ee 186 else if(!strcmp(reply,"LEDGR=OFF")){
thudt90 1:f1f734dd23ee 187 ledGreen = 1;
thudt90 1:f1f734dd23ee 188 c.send(RespondOff,sizeof(RespondOff));
thudt90 1:f1f734dd23ee 189 LedGreen = 0;
thudt90 1:f1f734dd23ee 190 }
thudt90 1:f1f734dd23ee 191 else if(!strcmp(reply,"LEDBLUE=ON")){
thudt90 1:f1f734dd23ee 192 ledBlue = 0;
thudt90 1:f1f734dd23ee 193 c.send(RespondOnBlue,sizeof(RespondOnBlue));
thudt90 1:f1f734dd23ee 194 LedBlue = 1;
thudt90 1:f1f734dd23ee 195 }
thudt90 1:f1f734dd23ee 196 else if(!strcmp(reply,"LEDBLUE=OFF")){
thudt90 1:f1f734dd23ee 197 ledBlue = 1;
thudt90 1:f1f734dd23ee 198 c.send(RespondOff,sizeof(RespondOff));
thudt90 1:f1f734dd23ee 199 LedBlue = 0;
thudt90 1:f1f734dd23ee 200 }
thudt90 1:f1f734dd23ee 201 else if(!strcmp(reply,"isbuttonpress")){
thudt90 1:f1f734dd23ee 202 c.send(str_State1,ButtonState_str(str_State1, button));
thudt90 1:f1f734dd23ee 203 }
thudt90 1:f1f734dd23ee 204 else{
thudt90 1:f1f734dd23ee 205 printf("%s\r\n",reply);
thudt90 1:f1f734dd23ee 206 }
thudt90 1:f1f734dd23ee 207 /* if(formatter != NULL)
thudt90 0:6ec14f880a00 208 {
thudt90 0:6ec14f880a00 209 printf("Sending data...");
thudt90 0:6ec14f880a00 210 char *page = formatter->get_page(reply);
thudt90 0:6ec14f880a00 211 do
thudt90 0:6ec14f880a00 212 {
thudt90 0:6ec14f880a00 213 c.send(page, strlen(page)+1);
thudt90 0:6ec14f880a00 214 page = formatter->get_page(reply);
thudt90 0:6ec14f880a00 215 }while(strlen(page)>0);
thudt90 1:f1f734dd23ee 216 c.send(Page3, strlen(Page3)+1);
thudt90 0:6ec14f880a00 217 printf("done\n");
thudt90 0:6ec14f880a00 218 }
thudt90 0:6ec14f880a00 219 else{
thudt90 0:6ec14f880a00 220 printf("Format NULL");
thudt90 0:6ec14f880a00 221 c.send(INVALID_FORMATTER, strlen(INVALID_FORMATTER)+1);
thudt90 1:f1f734dd23ee 222 }*/
thudt90 0:6ec14f880a00 223 }
thudt90 1:f1f734dd23ee 224 else{}
thudt90 1:f1f734dd23ee 225 //printf("Error while receiving data\n");
thudt90 0:6ec14f880a00 226 }
thudt90 0:6ec14f880a00 227 }
thudt90 0:6ec14f880a00 228 }
thudt90 0:6ec14f880a00 229
thudt90 0:6ec14f880a00 230 void HTTPServer::handle_request(char *buffer)
thudt90 0:6ec14f880a00 231 {
thudt90 0:6ec14f880a00 232 char *request_type = strtok(buffer, " ");
thudt90 0:6ec14f880a00 233 char *request = strtok(NULL, " ");
thudt90 1:f1f734dd23ee 234 char *statusrequest = strtok(request, "?");
thudt90 0:6ec14f880a00 235
thudt90 1:f1f734dd23ee 236 //printf("%s\r\n",request_type);
thudt90 1:f1f734dd23ee 237 //printf("%s\r\n",request);
thudt90 1:f1f734dd23ee 238 //printf("%s\r\n",statusrequest);
thudt90 0:6ec14f880a00 239 reply[0] = '\0';
thudt90 1:f1f734dd23ee 240 //flagHTTP = 0;
thudt90 1:f1f734dd23ee 241 // GET paket
thudt90 1:f1f734dd23ee 242 if(!strcmp(request_type, "GET")){
thudt90 1:f1f734dd23ee 243 if(!strcmp(request, "/")){
thudt90 1:f1f734dd23ee 244 strcat(reply,"GET");
thudt90 1:f1f734dd23ee 245 return;
thudt90 1:f1f734dd23ee 246 }
thudt90 1:f1f734dd23ee 247 else if(!strcmp(statusrequest, "/acloadstate")){
thudt90 1:f1f734dd23ee 248 strcat(reply,"load");
thudt90 1:f1f734dd23ee 249 return;
thudt90 1:f1f734dd23ee 250 }
thudt90 1:f1f734dd23ee 251 else{}
thudt90 0:6ec14f880a00 252 }
thudt90 1:f1f734dd23ee 253 // POST packet
thudt90 1:f1f734dd23ee 254 else if(!strcmp(request_type, "POST")){
thudt90 1:f1f734dd23ee 255 ++statusrequest;
thudt90 1:f1f734dd23ee 256 strcat(reply,statusrequest);
thudt90 0:6ec14f880a00 257 return;
thudt90 0:6ec14f880a00 258 }
thudt90 1:f1f734dd23ee 259 else
thudt90 1:f1f734dd23ee 260 strcat(reply,"receive not useful data");
thudt90 0:6ec14f880a00 261 }
thudt90 0:6ec14f880a00 262
thudt90 0:6ec14f880a00 263 void HTTPServer::add_request_handler(char *name, RequestHandler* handler)
thudt90 0:6ec14f880a00 264 {
thudt90 0:6ec14f880a00 265 handlers[name] = handler;
thudt90 0:6ec14f880a00 266 }
thudt90 0:6ec14f880a00 267