NNN40 run a HTTP server with RPC using Soft AP mode
Dependencies: WIFI_API_32kRAM mbed
Fork of HTTP-Server by
The sample code will run as a WiFi Soft AP mode with given AP configuration setting including SSID name and password. IP address (fixed to 192.168.2.1 for the current version of WIFI_API)of AP router will be print out once Soft AP mode is operating.
User can open their web browser and go to http://192.168.2.1/. and have a try on switch on a led. Firstly, we need to create an object to control a led
Then, led can be switch on using RPC command
More information cab be found from the links below
https://developer.mbed.org/users/feb11/code/HTTP-Server/
https://developer.mbed.org/cookbook/Interfacing-Using-RPC
HTTPServer.cpp@0:9e4bcb10b3e3, 2013-07-17 (annotated)
- Committer:
- feb11
- Date:
- Wed Jul 17 10:15:05 2013 +0000
- Revision:
- 0:9e4bcb10b3e3
- Child:
- 4:624527ebc0fa
Initial import
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
feb11 | 0:9e4bcb10b3e3 | 1 | #include "HTTPServer.h" |
feb11 | 0:9e4bcb10b3e3 | 2 | |
feb11 | 0:9e4bcb10b3e3 | 3 | #define INVALID_FORMATTER "No valid formatter specified" |
feb11 | 0:9e4bcb10b3e3 | 4 | |
feb11 | 0:9e4bcb10b3e3 | 5 | bool cmp(char* a, char* b) |
feb11 | 0:9e4bcb10b3e3 | 6 | { |
feb11 | 0:9e4bcb10b3e3 | 7 | return strcmp(a,b) < 0; |
feb11 | 0:9e4bcb10b3e3 | 8 | } |
feb11 | 0:9e4bcb10b3e3 | 9 | const char *TEST = "\ |
feb11 | 0:9e4bcb10b3e3 | 10 | <!DOCTYPE html>\ |
feb11 | 0:9e4bcb10b3e3 | 11 | <html>\ |
feb11 | 0:9e4bcb10b3e3 | 12 | <head>\ |
feb11 | 0:9e4bcb10b3e3 | 13 | <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\ |
feb11 | 0:9e4bcb10b3e3 | 14 | <title>TCP Server</title>\ |
feb11 | 0:9e4bcb10b3e3 | 15 | </head>\ |
feb11 | 0:9e4bcb10b3e3 | 16 | <body>Hello World !</body></html>"; |
feb11 | 0:9e4bcb10b3e3 | 17 | HTTPServer::HTTPServer(Formatter *f): |
feb11 | 0:9e4bcb10b3e3 | 18 | socket(), |
feb11 | 0:9e4bcb10b3e3 | 19 | handlers(&cmp), |
feb11 | 0:9e4bcb10b3e3 | 20 | formatter(f), |
feb11 | 0:9e4bcb10b3e3 | 21 | reply(), |
feb11 | 0:9e4bcb10b3e3 | 22 | command() |
feb11 | 0:9e4bcb10b3e3 | 23 | { |
feb11 | 0:9e4bcb10b3e3 | 24 | } |
feb11 | 0:9e4bcb10b3e3 | 25 | |
feb11 | 0:9e4bcb10b3e3 | 26 | HTTPServer::~HTTPServer() |
feb11 | 0:9e4bcb10b3e3 | 27 | { |
feb11 | 0:9e4bcb10b3e3 | 28 | for(std::map<char*, RequestHandler*>::iterator itor = handlers.begin(); |
feb11 | 0:9e4bcb10b3e3 | 29 | itor != handlers.end(); |
feb11 | 0:9e4bcb10b3e3 | 30 | ++itor) |
feb11 | 0:9e4bcb10b3e3 | 31 | delete itor->second; |
feb11 | 0:9e4bcb10b3e3 | 32 | |
feb11 | 0:9e4bcb10b3e3 | 33 | if(formatter) |
feb11 | 0:9e4bcb10b3e3 | 34 | delete formatter; |
feb11 | 0:9e4bcb10b3e3 | 35 | } |
feb11 | 0:9e4bcb10b3e3 | 36 | |
feb11 | 0:9e4bcb10b3e3 | 37 | bool HTTPServer::init(int port) |
feb11 | 0:9e4bcb10b3e3 | 38 | { |
feb11 | 0:9e4bcb10b3e3 | 39 | socket.set_blocking(true); |
feb11 | 0:9e4bcb10b3e3 | 40 | return !socket.bind(port) && !socket.listen(); |
feb11 | 0:9e4bcb10b3e3 | 41 | } |
feb11 | 0:9e4bcb10b3e3 | 42 | |
feb11 | 0:9e4bcb10b3e3 | 43 | void HTTPServer::run() |
feb11 | 0:9e4bcb10b3e3 | 44 | { |
feb11 | 0:9e4bcb10b3e3 | 45 | TCPSocketConnection c; |
feb11 | 0:9e4bcb10b3e3 | 46 | while(true) |
feb11 | 0:9e4bcb10b3e3 | 47 | { |
feb11 | 0:9e4bcb10b3e3 | 48 | while(socket.accept(c)); |
feb11 | 0:9e4bcb10b3e3 | 49 | c.set_blocking(false, 1000); |
feb11 | 0:9e4bcb10b3e3 | 50 | while(c.is_connected()) |
feb11 | 0:9e4bcb10b3e3 | 51 | { |
feb11 | 0:9e4bcb10b3e3 | 52 | char buffer[512]; |
feb11 | 0:9e4bcb10b3e3 | 53 | int n = c.receive_all(buffer, sizeof(buffer)-1); |
feb11 | 0:9e4bcb10b3e3 | 54 | if(n == 0) |
feb11 | 0:9e4bcb10b3e3 | 55 | { |
feb11 | 0:9e4bcb10b3e3 | 56 | c.close(); |
feb11 | 0:9e4bcb10b3e3 | 57 | break; |
feb11 | 0:9e4bcb10b3e3 | 58 | } |
feb11 | 0:9e4bcb10b3e3 | 59 | else if(n != -1) |
feb11 | 0:9e4bcb10b3e3 | 60 | { |
feb11 | 0:9e4bcb10b3e3 | 61 | buffer[n] = '\0'; |
feb11 | 0:9e4bcb10b3e3 | 62 | handle_request(buffer); |
feb11 | 0:9e4bcb10b3e3 | 63 | if(formatter != NULL) |
feb11 | 0:9e4bcb10b3e3 | 64 | { |
feb11 | 0:9e4bcb10b3e3 | 65 | char *page = formatter->get_page(reply); |
feb11 | 0:9e4bcb10b3e3 | 66 | do |
feb11 | 0:9e4bcb10b3e3 | 67 | { |
feb11 | 0:9e4bcb10b3e3 | 68 | c.send(page, strlen(page)+1); |
feb11 | 0:9e4bcb10b3e3 | 69 | page = formatter->get_page(reply); |
feb11 | 0:9e4bcb10b3e3 | 70 | }while(strlen(page)>0); |
feb11 | 0:9e4bcb10b3e3 | 71 | } |
feb11 | 0:9e4bcb10b3e3 | 72 | else |
feb11 | 0:9e4bcb10b3e3 | 73 | c.send(INVALID_FORMATTER, strlen(INVALID_FORMATTER)+1); |
feb11 | 0:9e4bcb10b3e3 | 74 | } |
feb11 | 0:9e4bcb10b3e3 | 75 | } |
feb11 | 0:9e4bcb10b3e3 | 76 | } |
feb11 | 0:9e4bcb10b3e3 | 77 | } |
feb11 | 0:9e4bcb10b3e3 | 78 | |
feb11 | 0:9e4bcb10b3e3 | 79 | void HTTPServer::handle_request(char *buffer) |
feb11 | 0:9e4bcb10b3e3 | 80 | { |
feb11 | 0:9e4bcb10b3e3 | 81 | char *request_type = strtok(buffer, " "); |
feb11 | 0:9e4bcb10b3e3 | 82 | char *request = strtok(NULL, " "); |
feb11 | 0:9e4bcb10b3e3 | 83 | |
feb11 | 0:9e4bcb10b3e3 | 84 | reply[0] = '\0'; |
feb11 | 0:9e4bcb10b3e3 | 85 | if(!strcmp(request, "/")) |
feb11 | 0:9e4bcb10b3e3 | 86 | return; |
feb11 | 0:9e4bcb10b3e3 | 87 | |
feb11 | 0:9e4bcb10b3e3 | 88 | if(!command.decode(request)) |
feb11 | 0:9e4bcb10b3e3 | 89 | { |
feb11 | 0:9e4bcb10b3e3 | 90 | strcat(reply, "Malformed request"); |
feb11 | 0:9e4bcb10b3e3 | 91 | return; |
feb11 | 0:9e4bcb10b3e3 | 92 | } |
feb11 | 0:9e4bcb10b3e3 | 93 | |
feb11 | 0:9e4bcb10b3e3 | 94 | std::map<char*, RequestHandler*>::iterator itor = handlers.find(request_type); |
feb11 | 0:9e4bcb10b3e3 | 95 | if(itor == handlers.end()) |
feb11 | 0:9e4bcb10b3e3 | 96 | { |
feb11 | 0:9e4bcb10b3e3 | 97 | strcat(reply, "Only PUT, DELETE and GET requests are accepted."); |
feb11 | 0:9e4bcb10b3e3 | 98 | return; |
feb11 | 0:9e4bcb10b3e3 | 99 | } |
feb11 | 0:9e4bcb10b3e3 | 100 | if(itor->second != NULL) |
feb11 | 0:9e4bcb10b3e3 | 101 | itor->second->handle(command, reply); |
feb11 | 0:9e4bcb10b3e3 | 102 | else |
feb11 | 0:9e4bcb10b3e3 | 103 | strcat(reply, "Invalid request handler"); |
feb11 | 0:9e4bcb10b3e3 | 104 | } |
feb11 | 0:9e4bcb10b3e3 | 105 | |
feb11 | 0:9e4bcb10b3e3 | 106 | void HTTPServer::add_request_handler(char *name, RequestHandler* handler) |
feb11 | 0:9e4bcb10b3e3 | 107 | { |
feb11 | 0:9e4bcb10b3e3 | 108 | handlers[name] = handler; |
feb11 | 0:9e4bcb10b3e3 | 109 | } |
feb11 | 0:9e4bcb10b3e3 | 110 |