Webserver for peach-board
Revision 1:f1f734dd23ee, committed 2015-03-12
- Comitter:
- thudt90
- Date:
- Thu Mar 12 08:26:27 2015 +0000
- Parent:
- 0:6ec14f880a00
- Commit message:
- Just test
Changed in this revision
diff -r 6ec14f880a00 -r f1f734dd23ee Formatter.cpp --- a/Formatter.cpp Wed Mar 04 02:16:31 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,184 +0,0 @@ -#include "Formatter.h" -#include "mbed.h" -#include "RPCObjectManager.h" -#include "EthernetInterface.h" - -const char *SIMPLE_HTML_CODE = "\ -<!DOCTYPE html>\ -<html>\ -<head>\ -<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\ -<title>TCP Server</title>\ -</head>\ - <body>"; - - -const char* INTERACTIVE_HTML_CODE_1 = "\ -<!DOCTYPE html> \ -<html>\ -<head>\ -<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\ -<title>TCP Server</title>\ -<script type=\"text/javascript\">\ -var ip = \"%s\";\ -function submitCreateForm()\ -{\ -var list = document.getElementById(\"type\");\ -var type = list.options[list.selectedIndex].value;\ -var name = document.getElementById(\"name\").value;\ -if(name === \"\") \ -return;\ -var arg = document.getElementById(\"arg\").value;\ -var url;\ -if(arg === \"\") url = \"http://\" + ip + type + \"new?name=\" + name;\ -else url = \"http://\" + ip + type + \"new?arg=\" + arg + \"&name=\" + name;\ -location.href= url;\ -}\ -function submitCallFuncForm()\ -{\ -var command = document.getElementById(\"command\").value;\ -if(command === \"\") \ -return; \ -var tmp = command.split(\' \');\ -var url = tmp[0];\ -if(tmp.length > 1)\ -url += \"?\";\ -for(var i = 1; i < tmp.length; ++i)\ -{\ -url += \"arg\" + i + \"=\" + tmp[i];\ -if(i+1 < tmp.length)\ -url += \"&\";\ -}\ -location.href = url;\ -}\ -</script>\ -</head> \ -<body>"; - -const char* INTERACTIVE_HTML_CODE_2 = "<h3>Create Object :</h3>\ -<form>\ -Type: <select id=\"type\">\ -<option value=\"/DigitalOut/\">DigitalOut</option>\ -<option value=\"/DigitalIn/\">DigitalIn</option>\ -<option value=\"/DigitalInOut/\">DigitalInOut</option>\ -<option value=\"/PwmOut/\">PwmOut</option>\ -<option value=\"/Timer/\">Timer</option>\ -</select><br>\ -name: <input type=\"text\" id=\"name\"><br>\ -arg(optional): <input type=\"text\" id=\"arg\">\ -<p><input type=\"button\" value=\"Create\" onclick=\"javascript:submitCreateForm();\"></p>\ -</form> \ - \ -<h3>Call a function :</h3>\ -<p>Enter an RPC command.</p>\ -<form>\ -Command: <input type= \"text\" id=\"command\" maxlength=127><br>\ -<p><input type=\"button\" value=\"Send\" onclick=\"javascript:submitCallFuncForm();\"></p><br>\ -</form>\ -</body> \ -</html>"; - -static char chunk[1024]; - -Formatter::Formatter(int nb): -currentChunk(0), -nbChunk(nb) -{ -} - -char* Formatter::get_page(char *reply) -{ - chunk[0] = '\0'; - printf("get_page %d %d\n",currentChunk,nbChunk); - if(currentChunk < nbChunk) - { - get_chunk(currentChunk, reply); - currentChunk++; - } - else - currentChunk = 0; - - return chunk; -} - -void Formatter::get_chunk(const int c, char *reply) -{ - strcat(chunk, reply); -} - -SimpleHTMLFormatter::SimpleHTMLFormatter(): -Formatter() -{ -} - -void SimpleHTMLFormatter::get_chunk(const int c, char* reply) -{ - strcat(chunk, SIMPLE_HTML_CODE); - - if(reply != NULL && strlen(reply) != 0) - { - strcat(chunk, "RPC reply : "); - strcat(chunk, reply); - } - - if(!RPCObjectManager::instance().is_empty()) - { - strcat(chunk, "<ul>"); - for(std::list<char*>::iterator itor = RPCObjectManager::instance().begin(); - itor != RPCObjectManager::instance().end(); - ++itor) - { - strcat(chunk, "<li>"); - strcat(chunk, *itor); - strcat(chunk, "</li>"); - } - strcat(chunk, "</ul>"); - } - - strcat(chunk, "</body></html>"); -} - -InteractiveHTMLFormatter::InteractiveHTMLFormatter(): -Formatter(3) -{ -} - -void InteractiveHTMLFormatter::get_chunk(const int c, char *reply) -{ - if(c == 0) - sprintf(chunk, INTERACTIVE_HTML_CODE_1, EthernetInterface::getIPAddress()); - - else if(c == 1) - { - if(reply != NULL && strlen(reply) != 0) - { - strcat(chunk, "RPC reply : "); - strcat(chunk, reply); - } - if(!RPCObjectManager::instance().is_empty()) - { - strcat(chunk, "<p>Objects created :</p>"); - - strcat(chunk, "<ul>"); - for(std::list<char*>::iterator itor = RPCObjectManager::instance().begin(); - itor != RPCObjectManager::instance().end(); - ++itor) - { - strcat(chunk, "<li>"); - strcat(chunk, *itor); - strcat(chunk, " (<a href=\"http://"); - strcat(chunk, EthernetInterface::getIPAddress()); - strcat(chunk, "/"); - strcat(chunk, *itor); - strcat(chunk, "/delete\">delete</a>)"); - strcat(chunk, "</li>"); - } - strcat(chunk, "</ul>"); - } - strcat(chunk, " "); - } - else if(c == 2) - strcat(chunk, INTERACTIVE_HTML_CODE_2); -} - -
diff -r 6ec14f880a00 -r f1f734dd23ee Formatter.h --- a/Formatter.h Wed Mar 04 02:16:31 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -#ifndef FORMATTER -#define FORMATTER - - -class Formatter -{ - public : - - Formatter(int nbChunk = 1); - - char* get_page(char *reply); - - protected : - - virtual void get_chunk(const int c, char *reply); - - private : - - int currentChunk; - int nbChunk; -}; - -class SimpleHTMLFormatter : public Formatter -{ - public : - - SimpleHTMLFormatter(); - - protected : - - virtual void get_chunk(const int c, char *reply); - -}; - -class InteractiveHTMLFormatter : public Formatter -{ - public : - - InteractiveHTMLFormatter(); - - protected : - - virtual void get_chunk(const int c, char *reply); -}; - - -#endif -
diff -r 6ec14f880a00 -r f1f734dd23ee HTTPServer.cpp --- a/HTTPServer.cpp Wed Mar 04 02:16:31 2015 +0000 +++ b/HTTPServer.cpp Thu Mar 12 08:26:27 2015 +0000 @@ -1,17 +1,67 @@ #include "HTTPServer.h" +#include "webpage.h" +#include "mbed.h" + #define INVALID_FORMATTER "No valid formatter specified" +#define HTPPRECEIVERGETOK 8 + +#define HTTPCTRREDON 1 +#define HTTPCTRREDOFF 2 +#define HTTPCTRGREENON 3 +#define HTTPCTRGREENOFF 4 +#define HTTPCTRBLUEON 5 +#define HTTPCTRBLUEOFF 6 +#define HTTPCTRLEADSTATE 7 +#define HTTPBUTTONPRESS 10 + +#define STRING_LED_LENGTH 8 +#define LED_STT(x) x?"ON":"OF" +#define BUTTON_STT(x) x?"ON":"OF" + +static unsigned char LedRed = 0, LedGreen = 0, LedBlue = 0; +extern unsigned char button; + +const char RespondOnRed[] = {"HTTP/1.1 200 OK\r\n" + "Server: AVR_Small_Webserver\r\n" + "WWW-Authenticate: Basic realm=\"NeedPassword\"" + "\r\nContent-Type: text/html\r\n\r\n" + "ONRED"}; +const char RespondOnGreen[] = {"HTTP/1.1 200 OK\r\n" + "Server: AVR_Small_Webserver\r\n" + "WWW-Authenticate: Basic realm=\"NeedPassword\"" + "\r\nContent-Type: text/html\r\n\r\n" + "ONGREEN"}; +const char RespondOnBlue[] = {"HTTP/1.1 200 OK\r\n" + "Server: AVR_Small_Webserver\r\n" + "WWW-Authenticate: Basic realm=\"NeedPassword\"" + "\r\nContent-Type: text/html\r\n\r\n" + "ONBLUE"}; +const char RespondOff[] = {"HTTP/1.1 200 OK\r\n" + "Server: AVR_Small_Webserver\r\n" + "WWW-Authenticate: Basic realm=\"NeedPassword\"" + "\r\nContent-Type: text/html\r\n\r\n" + "OFF"}; +const char ResponseHdr[] = {"HTTP/1.1 200 OK\r\n" + "Server: AVR_Small_Webserver\r\n" + "WWW-Authenticate: Basic realm=\"NeedPassword\"" + "\r\nContent-Type: text/html\r\n\r\n"}; + +DigitalOut ledRed(LED1); +DigitalOut ledGreen(LED2); +DigitalOut ledBlue(LED3); + bool cmp(char* a, char* b) { return strcmp(a,b) < 0; } -HTTPServer::HTTPServer(Formatter *f): +HTTPServer::HTTPServer(): socket(), handlers(&cmp), -formatter(f), reply(), +flagHTTP(), command() { } @@ -22,9 +72,33 @@ itor != handlers.end(); ++itor) delete itor->second; - - if(formatter) - delete formatter; + + // if(formatter) +// delete formatter; +} + +void HTTPServer::ledinit(void) +{ + ledRed = 1 ; + ledGreen = 1; + ledBlue = 1; +} + +int HTTPServer::findstr(char *progstr,char* str,int len) +{ + uint16_t i,j; + + i=0; + j=0; + while(i<len){ + if(str[i++] != *(progstr + j++)){ + j = 0; + } + if(*(progstr + j) == 0){ + return(i-j); + } + } + return(0xFFFF); } bool HTTPServer::init(int port) @@ -33,41 +107,104 @@ if(socket.bind(port)) { printf("Could not bind on port %d.\n", port); - return false; + return false; } - + if(socket.listen()) { printf("Could not listen %d\n", port); return false; } - + return true; } +int HTTPServer::LedState_str(char* str_Led, unsigned char Red, unsigned char Green, unsigned char Blue) +{ + char str_mp[15]; + int str_len; + sprintf(str_mp, "%s %s %s", LED_STT(Red), LED_STT(Green), LED_STT(Blue)); + sprintf(str_Led,"%s%s", ResponseHdr,str_mp); + str_len = sizeof(ResponseHdr) + STRING_LED_LENGTH;//STRING_LED_LENGTH: String response length with 2 space + return str_len; +} + +int HTTPServer::ButtonState_str(char* str_Led, unsigned char button) +{ + int str_len; + sprintf(str_Led,"%s%s", ResponseHdr,BUTTON_STT(button)); + str_len = sizeof(ResponseHdr) + 2; + return str_len; +} + void HTTPServer::run() { + char str_LED_State[150]; + char str_State1[150]; + TCPSocketConnection c; while(true) { while(socket.accept(c)); - c.set_blocking(false, 1000); + c.set_blocking(false, 10); while(c.is_connected()) { char buffer[512]; int n = c.receive_all(buffer, sizeof(buffer)-1); if(n == 0) { - printf("Close "); c.close(); break; } else if(n != -1) { - printf("Received data\n"); buffer[n] = '\0'; handle_request(buffer); - if(formatter != NULL) + if(!strcmp(reply,"GET")) + { + c.send(RespondGetOK, strlen(RespondGetOK)+1); + c.send(Page3, strlen(Page3)+1); + } + else if(!strcmp(reply,"load")) + c.send(str_LED_State, LedState_str(str_LED_State, LedRed,LedGreen,LedBlue)); + else if(!strcmp(reply,"LEDRED=ON")) + { ledRed = 0; //led on board + c.send(RespondOnRed,sizeof(RespondOnRed)); + LedRed = 1; + } + else if(!strcmp(reply,"LEDRED=OFF")) + { + ledRed = 1; + c.send(RespondOff,sizeof(RespondOff)); + LedRed = 0; + } + else if(!strcmp(reply,"LEDGR=ON")){ + ledGreen = 0; + c.send(RespondOnGreen,sizeof(RespondOnGreen)); + LedGreen = 1; + } + else if(!strcmp(reply,"LEDGR=OFF")){ + ledGreen = 1; + c.send(RespondOff,sizeof(RespondOff)); + LedGreen = 0; + } + else if(!strcmp(reply,"LEDBLUE=ON")){ + ledBlue = 0; + c.send(RespondOnBlue,sizeof(RespondOnBlue)); + LedBlue = 1; + } + else if(!strcmp(reply,"LEDBLUE=OFF")){ + ledBlue = 1; + c.send(RespondOff,sizeof(RespondOff)); + LedBlue = 0; + } + else if(!strcmp(reply,"isbuttonpress")){ + c.send(str_State1,ButtonState_str(str_State1, button)); + } + else{ + printf("%s\r\n",reply); + } +/* if(formatter != NULL) { printf("Sending data..."); char *page = formatter->get_page(reply); @@ -76,15 +213,16 @@ c.send(page, strlen(page)+1); page = formatter->get_page(reply); }while(strlen(page)>0); + c.send(Page3, strlen(Page3)+1); printf("done\n"); } else{ printf("Format NULL"); c.send(INVALID_FORMATTER, strlen(INVALID_FORMATTER)+1); - } + }*/ } - else - printf("Error while receiving data\n"); + else{} + //printf("Error while receiving data\n"); } } } @@ -93,35 +231,33 @@ { char *request_type = strtok(buffer, " "); char *request = strtok(NULL, " "); + char *statusrequest = strtok(request, "?"); - printf("%s\n",request_type); - + //printf("%s\r\n",request_type); + //printf("%s\r\n",request); + //printf("%s\r\n",statusrequest); reply[0] = '\0'; - if(!strcmp(request, "/")){ - printf("in /\n"); - return; + //flagHTTP = 0; + // GET paket + if(!strcmp(request_type, "GET")){ + if(!strcmp(request, "/")){ + strcat(reply,"GET"); + return; + } + else if(!strcmp(statusrequest, "/acloadstate")){ + strcat(reply,"load"); + return; + } + else{} } - - if(!command.decode(request)) - { - strcat(reply, "Malformed request"); - printf("%s\n",reply); + // POST packet + else if(!strcmp(request_type, "POST")){ + ++statusrequest; + strcat(reply,statusrequest); return; } - - std::map<char*, RequestHandler*>::iterator itor = handlers.find(request_type); - if(itor == handlers.end()) - { - strcat(reply, "No request handler found for this type of request."); - printf("%s\n",reply); - return; - } - if(itor->second != NULL) - itor->second->handle(command, reply); - else{ - strcat(reply, "Invalid request handler"); - printf("%s\n",reply); - } + else + strcat(reply,"receive not useful data"); } void HTTPServer::add_request_handler(char *name, RequestHandler* handler)
diff -r 6ec14f880a00 -r f1f734dd23ee HTTPServer.h --- a/HTTPServer.h Wed Mar 04 02:16:31 2015 +0000 +++ b/HTTPServer.h Thu Mar 12 08:26:27 2015 +0000 @@ -6,7 +6,7 @@ #include "mbed.h" //#include "mbed_rpc.h" #include "RequestHandler.h" -#include "Formatter.h" + #include "EthernetInterface.h" #include "RPCCommand.h" @@ -15,11 +15,16 @@ { public : - HTTPServer(Formatter *f = new Formatter()); + HTTPServer(); virtual ~HTTPServer(); bool init(int port); - + void ledinit(void); + int findstr(char *progstr,char* str,int len); + int ButtonState_str(char* str_Led, unsigned char button); + int LedState_str(char* str_Led, unsigned char Red, unsigned char Green, unsigned char Blue); + //void PressButton0(void); + //void PressButton1(void); void run(); void add_request_handler(char *name, RequestHandler* handler); @@ -30,8 +35,8 @@ TCPSocketServer socket; std::map<char*, RequestHandler*, bool(*)(char*, char*)> handlers; - Formatter *formatter; char reply[RPC_MAX_STRING]; + int flagHTTP; RPCCommand command; };
diff -r 6ec14f880a00 -r f1f734dd23ee RPCCommand.cpp --- a/RPCCommand.cpp Wed Mar 04 02:16:31 2015 +0000 +++ b/RPCCommand.cpp Thu Mar 12 08:26:27 2015 +0000 @@ -4,84 +4,101 @@ RPCCommand::RPCCommand(): -cmd(), -obj_name(NULL), -func_name(NULL) + cmd(), + obj_name(NULL), + func_name(NULL) { } bool RPCCommand::decode(char *buffer) { + // buffer = /DigitalOut/new?arg=ytgf&name=thu if(buffer == NULL) return false; if(buffer[0] != '/') return false; - + ++buffer; + // buffer = DigitalOut/new?arg=ytgf&name=thu char *tmp = strchr(buffer ,'/'); + // tmp = /new?arg=ytgf&name=thu if(tmp == NULL) return false; if(tmp == buffer) return false; - + tmp[0] = '\0'; + // tmp = new?arg=ytgf&name=thu obj_name = buffer; - + // obj_name = DigitalOut/new?arg=ytgf&name=thu + buffer = tmp+1; - + //buffer = new?arg=ytgf&name=thu + if(buffer[0] == '\0' || buffer[0] == '?') return false; - + func_name = buffer; - + // func_name = new?arg=ytgf&name=thu + tmp = strchr(buffer, '?'); - if(tmp != NULL) - { + //tmp = ?arg=ytgf&name=thu + if(tmp != NULL) { if(tmp[1] == '\0') return false; tmp[0] = '\0'; } - + cmd[0] = '\0'; + //cmd[0] = null strcat(cmd, "/"); strcat(cmd, obj_name); strcat(cmd, "/"); strcat(cmd, func_name); + //cmd = '\0'/DigitalOut/new?arg=ytgf&name=thu/new?arg=ytgf&name=thu if(tmp == NULL) return true; - + buffer = tmp+1; - do - { + //buffer = arg=ytgf&name=thu + do { tmp = strchr(buffer, '&'); - - if(tmp != NULL) - { + //tmp = &name=thu + // lan 2: tmp = null + + if(tmp != NULL) { if(tmp[1] == '\0' || buffer == tmp) return false; tmp[0] = '\0'; + // tmp = '\0'name=thu } char *sep = strchr(buffer, '='); + //sep = =ytgf&name=thu + // lan 2: sep = =thu if(sep == NULL) return false; if(sep == buffer) return false; if(sep[1] == '\0' || sep[1] == '&') return false; - + strcat(cmd, " "); + strcat(cmd, sep+1); - + //cmd = '\0'/DigitalOut/new?arg=ytgf&name=thu/new?arg=ytgf&name=thu ytgf&name=thu + // lan 2: cmd = '\0'/DigitalOut/new?arg=ytgf&name=thu/new?arg=ytgf&name=thu ytgf&name=thu thu + if(tmp != NULL) buffer = tmp+1; + // buffer = name=thu else buffer = NULL; - }while(buffer); - + } while(buffer); + return true; } @@ -96,25 +113,23 @@ { if(!strcmp(func_name, "new") && RPCType::instance().is_supported_type(obj_name)) return CREATE; - + RPC* r = RPC::lookup(obj_name); if(r == NULL) return INVALID; - + if(!strcmp(func_name, "delete")) return DELETE; - + const struct rpc_method *methods = r->get_rpc_methods(); int i = 0; - while(methods[i].name != NULL) - { - if(!strcmp(func_name, methods[i].name)) - { + while(methods[i].name != NULL) { + if(!strcmp(func_name, methods[i].name)) { return FUNCTION_CALL; } ++i; } - + return INVALID; }