![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
WIZwikiREST ver1.0 (Memory problem)
Dependencies: MbedJSONValue WIZnetInterface mbed
Revision 0:5886f525a4ad, committed 2016-03-02
- Comitter:
- joon874
- Date:
- Wed Mar 02 03:28:08 2016 +0000
- Commit message:
- WIZwikiREST ver1 (Memory problem)
Changed in this revision
diff -r 000000000000 -r 5886f525a4ad HTTPServer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPServer.cpp Wed Mar 02 03:28:08 2016 +0000 @@ -0,0 +1,140 @@ +#include <string.h> +#include "HTTPServer.h" +#include "MbedJSONValue.h" + +extern MbedJSONValue WIZwikiREST; + +bool cmp(char* a, char* b) +{ + return strcmp(a,b) < 0; +} + +HTTPServer::HTTPServer(): +socket(), +handlers(&cmp) +{ +} + +HTTPServer::~HTTPServer() +{ + for(std::map<char*, RequestHandler*>::iterator itor = handlers.begin(); + itor != handlers.end(); + ++itor) + delete itor->second; +} + +bool HTTPServer::init(int port) +{ + socket.set_blocking(true); + if(socket.bind(port)) + { + printf("Could not bind on port %d.\n", port); + return false; + } + + if(socket.listen()) + { + printf("Could not listen %d\n", port); + return false; + } + + return true; +} + +void HTTPServer::run() +{ + TCPSocketConnection c; + while(true) + { + while(socket.accept(c)); + c.set_blocking(false, 1000); + while(c.is_connected()) + { + + int n = c.receive_all(HTTPBUF, sizeof(HTTPBUF)-1); + + if(n == 0) + { + c.close(); + break; + } + else if(n != -1) + { + printf("Received data : %d\r\n",n); + + HTTPBUF[n] = '\0'; + if(handle_request(HTTPBUF) == HTTP_SUCCESS) + { + c.send(rest_result, strlen(rest_result)); + //c.send((char*)rest_result.c_str(), 159); + } + else + { + //printf("send fail : %s\r\n",(char*)rest_result.c_str()); + c.send(rest_result, strlen(rest_result)); + } + } + else + printf("Error while receiving data\n"); + + } + } +} + + +HTTP_RESULT HTTPServer::handle_request(char *buffer) +{ + char *request_type; + char *request; + +//{"Name":"WIZwiki-REST-01","Network":{"IP":"192.168.100.100","SN":"255.255.255.0","GW":"192.168.100.1"},"User":{"Name":"Lawrence","ID":"law","PSWD":"law1234"}} 159 + if(buffer) + { + // buffer check + printf("*******************************************************\r\n"); + printf("buffer=%s\r\n",buffer); + printf("*******************************************************\r\n"); + + // type parsing + request_type = strtok(buffer," \r\n"); + printf("Type = %s\r\n", request_type); + + if(request_type) + { + request = strtok(NULL, " \r\n"); // corrested " " -> " /" : /Name -> Name + if(request) + { + printf("URI = %s\r\n", request); + } + else + { + strcpy(rest_result, "Invaild URI"); + printf("%s\r\n",rest_result); + return HTTP_INVALID_URI; + } + } + } + + std::map<char*, RequestHandler*>::iterator itor = handlers.find(request_type); + if(itor == handlers.end()) + { + strcpy(rest_result, "No request handler found for this type of request."); + return HTTP_INVALID_HANDLE; + } + //if(itor != NULL) + //itor->handle(request, rest_result.c_str()); + if(itor->second != NULL) + itor->second->handle(request, rest_result); + else + { + strcpy(rest_result, "Invalid request handler"); + return HTTP_INVALID_HANDLE; + } + return HTTP_SUCCESS; +} + +void HTTPServer::add_request_handler(char *name, RequestHandler* handler) +{ + handlers[name] = handler; +} +
diff -r 000000000000 -r 5886f525a4ad HTTPServer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPServer.h Wed Mar 02 03:28:08 2016 +0000 @@ -0,0 +1,47 @@ +#ifndef HTTP_SERVER +#define HTTP_SERVER + +#include <map> + +#include "mbed.h" +#include <string> +#include "RequestHandler.h" +#include "EthernetInterface.h" + +typedef enum _HTTP_RESULT +{ + HTTP_INVALID_URI = -3, + HTTP_INVALID_DATA = -2, + HTTP_INVALID_HANDLE = -1, + HTTP_SUCCESS = 1 +}HTTP_RESULT; + + +static char HTTPBUF[1024] ={0,}; +static char rest_result[1024] = {0,}; + +class HTTPServer +{ + public : + + HTTPServer(); + virtual ~HTTPServer(); + + bool init(int port); + + void run(); + + void add_request_handler(char *name, RequestHandler* handler); + + private : + + HTTP_RESULT handle_request(char *buffer); + + TCPSocketServer socket; + std::map<char*, RequestHandler*, bool(*)(char*, char*)> handlers; + //char rest_result[2048]; + +}; + +#endif +
diff -r 000000000000 -r 5886f525a4ad MbedJSONValue.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedJSONValue.lib Wed Mar 02 03:28:08 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/joon874/code/MbedJSONValue/#e15fd7be9e74
diff -r 000000000000 -r 5886f525a4ad RequestHandler.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RequestHandler.cpp Wed Mar 02 03:28:08 2016 +0000 @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "RequestHandler.h" +#include "MbedJSONValue.h" + +extern MbedJSONValue WIZwikiREST; + +void GetRequestHandler::handle(char* rest_uri, char *reply) +{ + MbedJSONValue* tmpJson; + + char* tok; + char* last; + + printf("GetRequestHandler():%s\r\n",rest_uri); + + if(!strcmp(rest_uri, "/")) + { + strcpy(reply, WIZwikiREST.serialize().c_str()); + return; + } + tok = strtok_r(rest_uri+1, "/", &last); // 20160226 + tmpJson = &WIZwikiREST; + + while(tok) + { + printf("tok = %s \r\n", tok); // Name + if(tmpJson->size() > 0) tmpJson = &((*tmpJson)[tok]); + else + { + tmpJson = 0; + break; + } + tok = strtok_r(0, "/", &last); // 20160226 + } + if(tmpJson && tmpJson->size() > 0) + { + strcpy(reply, (*tmpJson).serialize().c_str()); + } + else + { + strcpy(reply, "{\"Result\" : \"No defined Resource\"}"); + } + return; +} + +void PutRequestHandler::handle(char* rest_uri, char *reply) +{ +} + +void DeleteRequestHandler::handle(char* rest_uri, char *reply) +{ +} + +
diff -r 000000000000 -r 5886f525a4ad RequestHandler.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RequestHandler.h Wed Mar 02 03:28:08 2016 +0000 @@ -0,0 +1,34 @@ +#ifndef __REQUESTHANDLER_H_ +#define __REQUESTHANDLER_H_ + +class RequestHandler +{ + public : + + virtual void handle(char* rest_uri, char* reply) = 0; +}; + +class GetRequestHandler : public RequestHandler +{ + public : + + virtual void handle(char* rest_uri, char* reply); +}; + +class PutRequestHandler : public RequestHandler +{ + public : + + virtual void handle(char* rest_uri, char* reply); + +}; + + +class DeleteRequestHandler : public RequestHandler +{ + public : + + virtual void handle(char* rest_uri, char* reply); + +}; +#endif
diff -r 000000000000 -r 5886f525a4ad WIZnetInterface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WIZnetInterface.lib Wed Mar 02 03:28:08 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#c91884bd2713
diff -r 000000000000 -r 5886f525a4ad main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Mar 02 03:28:08 2016 +0000 @@ -0,0 +1,153 @@ +#include "mbed.h" +#include "HTTPServer.h" +#include "RequestHandler.h" +#include "EthernetInterface.h" +#include "MbedJSONValue.h" + +#define SERVER_PORT 80 + +EthernetInterface eth; +HTTPServer WIZwikiWebSvr; +MbedJSONValue WIZwikiREST; + +// Enter a MAC address for your controller below. +uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0xFE}; +char mac_str[20]; +char ip_addr[] = "192.168.100.100"; +char subnet_mask[] = "255.255.255.0"; +char gateway_addr[] = "192.168.100.1"; + +DigitalOut LED_1(PA_1); +DigitalOut LED_2(PA_2); + +DigitalInOut P05(P5); +DigitalInOut P06(P6); + +void WIZwiki_REST_init(); + +int main(void) +{ + sprintf(mac_str, "%02X:%02X:%02X:%02X:%02X:%02X",mac_addr[0],mac_addr[1], + mac_addr[2],mac_addr[3], + mac_addr[4],mac_addr[5]); + //GPIO Set + P05.output(); + P05.write(1); + P06.input(); + + //LED Setbit + LED_1.write(0); // LED On + LED_2.write(1); // LED Off + + WIZwiki_REST_init(); + + // Serialize it into a JSON string + printf("\r\n"); + printf("-------------------------WIZwikiREST--------------------------- \r\n"); + printf("%s\r\n", WIZwikiREST.serialize().c_str()); + printf("--------------------------------------------------------------- \r\n"); + + WIZwikiWebSvr.add_request_handler("GET", new GetRequestHandler()); + //WIZwikiWebSvr.add_request_handler("DELETE", new DeleteRequestHandler()); + //WIZwikiWebSvr.add_request_handler("PUT", new PutRequestHandler()); + + #ifdef DHCP + eth.init(mac_addr); //Use DHCP + #else + eth.init(mac_addr, ip_addr, subnet_mask, gateway_addr); //Not Use DHCP + #endif + + printf("Check Ethernet Link\r\n"); + + while(1) //Wait link up + { + if(eth.link() == true) + break; + } + + printf("Link up\r\n"); + printf("IP Address is %s\r\n", eth.getIPAddress()); + + if(!WIZwikiWebSvr.init(SERVER_PORT)) + { + eth.disconnect(); + return -1; + } + + while(1) + { + WIZwikiWebSvr.run(); + } +} + +void WIZwiki_REST_init(void) +{ + //Fill the object + WIZwikiREST["Name"] = "WIZwiki-REST-01"; + //Network + WIZwikiREST["Network"]["MAC"] = mac_str; + WIZwikiREST["Network"]["IP"] = ip_addr; + WIZwikiREST["Network"]["SN"] = subnet_mask; + WIZwikiREST["Network"]["GW"] = gateway_addr; + //LEDs + WIZwikiREST["LEDs"]["LED_1"]["Value"] = (LED_1.read() ? "Off" : "On"); + WIZwikiREST["LEDs"]["LED_2"]["Value"] = (LED_2.read() ? "Off" : "On"); + + // GPIOs + WIZwikiREST["GPIOs"]["P05"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_5) ? "Output" : "Input"); + WIZwikiREST["GPIOs"]["P05"]["Value"] = (P05.read() ? "1" : "0"); + WIZwikiREST["GPIOs"]["P06"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_6) ? "Output" : "Input"); + WIZwikiREST["GPIOs"]["P06"]["Value"] = (P06.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P07"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_7) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P07"]["Value"] = (P07.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P08"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_8) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P08"]["Value"] = (P08.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P09"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_9) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P09"]["Value"] = (P09.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P10"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_10) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P10"]["Value"] = (P10.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P11"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_11) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P11"]["Value"] = (P11.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P12"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_12) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P12"]["Value"] = (P12.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P13"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_13) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P13"]["Value"] = (P13.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P14"]["Mode"] = ((GPIOA->OUTENSET&GPIO_Pin_14) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P14"]["Value"] = (P14.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P15"]["Mode"] = ((GPIOB->OUTENSET&GPIO_Pin_0) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P15"]["Value"] = (P15.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P16"]["Mode"] = ((GPIOB->OUTENSET&GPIO_Pin_1) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P16"]["Value"] = (P16.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P17"]["Mode"] = ((GPIOB->OUTENSET&GPIO_Pin_2) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P17"]["Value"] = (P17.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P18"]["Mode"] = ((GPIOB->OUTENSET&GPIO_Pin_3) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P18"]["Value"] = (P18.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P19"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_8) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P19"]["Value"] = (P19.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P20"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_4) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P20"]["Value"] = (P20.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P21"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_0) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P21"]["Value"] = (P21.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P22"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_1) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P22"]["Value"] = (P22.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P23"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_2) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P23"]["Value"] = (P23.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P24"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_3) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P24"]["Value"] = (P24.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P25"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_9) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P25"]["Value"] = (P25.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P26"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_5) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P26"]["Value"] = (P26.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P27"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_12) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P27"]["Value"] = (P27.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P28"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_13) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P28"]["Value"] = (P28.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P29"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_14) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P29"]["Value"] = (P29.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P30"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_15) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P30"]["Value"] = (P30.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P31"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_6) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P31"]["Value"] = (P31.read() ? "1" : "0"); +// WIZwikiREST["GPIOs"]["P32"]["Mode"] = ((GPIOC->OUTENSET&GPIO_Pin_7) ? "Output" : "Input"); +// WIZwikiREST["GPIOs"]["P32"]["Value"] = (P32.read() ? "1" : "0"); +}
diff -r 000000000000 -r 5886f525a4ad mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Mar 02 03:28:08 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/252557024ec3 \ No newline at end of file