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
Diff: HTTPServer.cpp
- Revision:
- 0:9e4bcb10b3e3
- Child:
- 4:624527ebc0fa
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPServer.cpp Wed Jul 17 10:15:05 2013 +0000 @@ -0,0 +1,110 @@ +#include "HTTPServer.h" + +#define INVALID_FORMATTER "No valid formatter specified" + +bool cmp(char* a, char* b) +{ + return strcmp(a,b) < 0; +} +const char *TEST = "\ +<!DOCTYPE html>\ +<html>\ + <head>\ + <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\ + <title>TCP Server</title>\ + </head>\ + <body>Hello World !</body></html>"; +HTTPServer::HTTPServer(Formatter *f): +socket(), +handlers(&cmp), +formatter(f), +reply(), +command() +{ +} + +HTTPServer::~HTTPServer() +{ + for(std::map<char*, RequestHandler*>::iterator itor = handlers.begin(); + itor != handlers.end(); + ++itor) + delete itor->second; + + if(formatter) + delete formatter; +} + +bool HTTPServer::init(int port) +{ + socket.set_blocking(true); + return !socket.bind(port) && !socket.listen(); +} + +void HTTPServer::run() +{ + TCPSocketConnection c; + while(true) + { + while(socket.accept(c)); + c.set_blocking(false, 1000); + while(c.is_connected()) + { + char buffer[512]; + int n = c.receive_all(buffer, sizeof(buffer)-1); + if(n == 0) + { + c.close(); + break; + } + else if(n != -1) + { + buffer[n] = '\0'; + handle_request(buffer); + if(formatter != NULL) + { + char *page = formatter->get_page(reply); + do + { + c.send(page, strlen(page)+1); + page = formatter->get_page(reply); + }while(strlen(page)>0); + } + else + c.send(INVALID_FORMATTER, strlen(INVALID_FORMATTER)+1); + } + } + } +} + +void HTTPServer::handle_request(char *buffer) +{ + char *request_type = strtok(buffer, " "); + char *request = strtok(NULL, " "); + + reply[0] = '\0'; + if(!strcmp(request, "/")) + return; + + if(!command.decode(request)) + { + strcat(reply, "Malformed request"); + return; + } + + std::map<char*, RequestHandler*>::iterator itor = handlers.find(request_type); + if(itor == handlers.end()) + { + strcat(reply, "Only PUT, DELETE and GET requests are accepted."); + return; + } + if(itor->second != NULL) + itor->second->handle(command, reply); + else + strcat(reply, "Invalid request handler"); +} + +void HTTPServer::add_request_handler(char *name, RequestHandler* handler) +{ + handlers[name] = handler; +} +