Upper Version Add PUT method Delete POST method

Dependencies:   Adafruit_GFX MbedJSONValue_v102 WIZnetInterface mbed

Fork of WIZwiki-REST-io_v102 by Lawrence Lee

Files at this revision

API Documentation at this revision

Comitter:
joon874
Date:
Mon Sep 05 06:29:55 2016 +0000
Parent:
7:64db444b21f4
Commit message:
Add PUT method

Changed in this revision

HTTPServer.cpp Show annotated file Show diff for this revision Revisions of this file
HTTPServer.h Show annotated file Show diff for this revision Revisions of this file
RequestHandler.cpp Show annotated file Show diff for this revision Revisions of this file
RequestHandler.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 64db444b21f4 -r 60d99da6eeb2 HTTPServer.cpp
--- a/HTTPServer.cpp	Fri Jul 29 08:15:34 2016 +0000
+++ b/HTTPServer.cpp	Mon Sep 05 06:29:55 2016 +0000
@@ -93,33 +93,33 @@
     if(buffer)
     {
 #ifdef DEBUG_HTTP                
-                // buffer check
-                printf("*******************************************************\r\n");
-        printf("buffer=%s\r\n",buffer);
-                printf("*******************************************************\r\n");
+        // buffer check
+        printf("***********************\r\n");
+        printf("  buffer=%s  \r\n",buffer);
+        printf("***********************\r\n");
 #endif                                
-              // type parsing
-              request_type = strtok(buffer," \r\n");
+        // type parsing
+        request_type = strtok(buffer," \r\n");
 #ifdef DEBUG_HTTP                              
         printf("Type = %s\r\n", request_type);
 #endif        
             
         if(request_type)
         {
-            request = strtok(NULL, "  \r\n");                                                 // corrested  " " -> " /"     : /Name -> Name
+            request = strtok(NULL, "  \r\n");  // corrested  " " -> " /"     : /Name -> Name
             if(request)
             {
 #ifdef DEBUG_HTTP                                    
-                    printf("URI = %s\r\n", request);
+                printf("URI = %s\r\n", request);
 #endif                    
              }
              else
              {
-                    strcpy(rest_result, "Invaild URI");
+                strcpy(rest_result, "Invaild URI");
 #ifdef DEBUG_HTTP                              
-                    printf("%s\r\n",rest_result);
+                printf("%s\r\n",rest_result);
 #endif                            
-                    return HTTP_INVALID_URI;
+                return HTTP_INVALID_URI;
             }
         }
     }
@@ -135,6 +135,7 @@
     if(itor->second != NULL)
     {
         char* request_data = 0;
+#ifdef HTTP_POST        
         if(!strcmp(request_type,"POST"))
         {
             request_data = strstr(request+strlen(request)+1, "\r\n\r\n");
@@ -142,6 +143,14 @@
             printf("POST:request_data=%s\r\n",request_data+4);
 #endif
         }
+#endif        
+        if(!strcmp(request_type,"PUT"))
+        {
+            request_data = strstr(request+strlen(request)+1, "\r\n\r\n");
+#ifdef DEBUG_HTTP
+            printf("PUT:request_data=%s\r\n",request_data+4);
+#endif
+        }
         itor->second->handle(request, request_data, rest_result);
     }
     else
diff -r 64db444b21f4 -r 60d99da6eeb2 HTTPServer.h
--- a/HTTPServer.h	Fri Jul 29 08:15:34 2016 +0000
+++ b/HTTPServer.h	Mon Sep 05 06:29:55 2016 +0000
@@ -8,7 +8,7 @@
 #include "RequestHandler.h"
 #include "EthernetInterface.h"
 
-//#define DEBUG_HTTP
+#define DEBUG_HTTP
 
 typedef enum _HTTP_RESULT
 {
@@ -20,7 +20,7 @@
 
 
 static  char HTTPBUF[600] = {0,};
-static  char rest_result[532] = {0,};
+static  char rest_result[300] = {0,};
 
 class HTTPServer
 {
diff -r 64db444b21f4 -r 60d99da6eeb2 RequestHandler.cpp
--- a/RequestHandler.cpp	Fri Jul 29 08:15:34 2016 +0000
+++ b/RequestHandler.cpp	Mon Sep 05 06:29:55 2016 +0000
@@ -99,6 +99,7 @@
     return;
 }
 
+#ifdef HTTP_POST
 void PostRequestHandler::handle(char* rest_uri, char* request_data, char *reply)
 {
     MbedJSONValue* tmpJson;
@@ -203,6 +204,191 @@
                         if((*tmpJson)[tok].accessible){
                             errnum = 200;
                             if((*pDataJson)[i].size() > 0){
+                                /*
+                                (*tmpJson)[tok] = (*pDataJson)[i]._value.asString;
+                                (*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); 
+                                */
+                                (*tmpJson)[tok] = (*pDataJson)[i]._value.asString->c_str();
+                                (*tmpJson)[tok].cb_action((void*)((*pDataJson)[i]._value.asString->c_str())); 
+#ifdef DEBUG_HTTPREQ                                                            
+                                printf("String Updated: %s : %s\r\n", tok,(*tmpJson)[tok].get<std::string>().c_str());
+#endif                                
+                            }
+                            else{
+                                (*tmpJson)[tok] = (*pDataJson)[i]._value.asInt;
+                                (*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); 
+#ifdef DEBUG_HTTPREQ
+                                printf("Int Updated: %s : %d\r\n", tok,(*tmpJson)[tok].get<int>());
+#endif                                
+                            }
+                        }
+                        else{
+                            errnum = 403; break;
+                        }
+                    }
+                    else{
+                        errnum = 404; break;
+                    }
+                }
+                break;
+                
+            default:
+                errnum = 403; break;
+        }    
+    }
+        
+    switch(errnum)
+    {
+        case 403:
+                strcpy (reply, "HTTP/1.1 403 OK\r\n");
+                strcat (reply, "Sever: WIZwiki-REST\r\n");
+                strcat (reply, "content-Type: text/json\r\n");
+                sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4);
+                strcat(reply, "{\"Result : No Accessible\"}");
+                strcat (reply, "\r\n\r\n");
+                break;
+            
+        case 404:
+                strcpy (reply, "HTTP/1.1 404 OK\r\n");
+                strcat (reply, "Sever: WIZwiki-REST\r\n");
+                strcat (reply, "content-Type: text/json\r\n");
+                sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 33+4);
+                strcat(reply, "{\"Result\" : \"No defined Resource\"}");
+                strcat (reply, "\r\n\r\n");
+                break;
+            
+        case 200:
+                strcpy (reply, "HTTP/1.1 200 OK\r\n");
+                strcat (reply, "Sever: WIZwiki-REST\r\n");
+                strcat (reply, "content-Type: text/json\r\n");
+                sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 15+4);
+                strcat (reply, "{\"Result\":\"OK\"}");
+                strcat (reply, "\r\n\r\n");
+                break;
+    }
+    
+#ifdef DEBUG_HTTPREQ
+    printf("Before Delete\r\n");
+    debug_info();
+#endif
+    if(pDataJson){
+#ifdef DEBUG_HTTPREQ
+        printf("type;%d, pDataJson->index_token=%d\r\n",pDataJson->_type,pDataJson->index_token);
+#endif
+        delete pDataJson;
+        pDataJson = 0;
+    }
+#ifdef DEBUG_HTTPREQ
+    printf("After Delete\r\n");
+    debug_info();
+#endif
+}
+#endif
+
+void PutRequestHandler::handle(char* rest_uri, char* request_data, char *reply)
+{
+    MbedJSONValue* tmpJson;
+      char* tok;
+      char* last;
+      int errnum = 200;
+      
+#ifdef DEBUG_HTTPREQ
+    printf("PutRequestHandler():%s\r\n",request_data+4);
+#endif
+    if(!pDataJson){
+        pDataJson = new MbedJSONValue();  
+    }
+#ifdef DEBUG_HTTPREQ
+    printf("Before Parse\r\n");
+    debug_info();
+#endif
+    parse(*pDataJson,(const char*)request_data);
+#ifdef DEBUG_HTTPREQ
+    printf("After Parse\r\n");
+    debug_info();
+    printf("DataJson._type=%d\r\n",pDataJson->_type);
+    printf("DataJson=%s\r\n",pDataJson->serialize().c_str());
+    printf("DataJson.size()=%d\r\n",pDataJson->size());
+    printf("DataJason.type=%d\r\n",pDataJson->_type);
+#endif    
+
+    if(!strcmp(rest_uri, "/")){
+        tmpJson = &WIZwikiREST;     
+    }
+    else{
+        tok = strtok_r(rest_uri+1, "/", &last);
+        tmpJson = &WIZwikiREST;
+        
+        char* name = 0;
+        while(tok)
+        {
+#ifdef DEBUG_HTTPREQ            
+            printf("tok = %s \r\n", tok);
+#endif            
+            if(tmpJson->hasMember(tok)){
+                tmpJson = &((*tmpJson)[tok]);
+                name = tok;
+                tok = strtok_r(0, "/", &last);
+            }
+            else{
+#ifdef DEBUG_HTTPREQ                
+                printf("No Member\r\n");
+#endif                
+                break;
+            }
+        }
+        if(name){
+#ifdef DEBUG_HTTPREQ                            
+            printf("Token_name=%s\r\n",name);
+#endif
+            if(tok){
+#ifdef DEBUG_HTTPREQ                                            
+                printf("It should be no parameters : tok=%s\r\n",tok);
+#endif                
+                errnum = 403;
+            }
+        }
+        else{
+            errnum = 404;
+        }
+    }
+    if(errnum != 0){
+        switch(pDataJson->_type)
+        {
+            case MbedJSONValue::TypeInt:
+                if(tmpJson->accessible){
+                    *tmpJson = pDataJson->_value.asInt; 
+                    tmpJson->cb_action(&tmpJson->_value); 
+#ifdef DEBUG_HTTPREQ                        
+                    printf("set int:%d\r\n",atoi(tok));
+#endif                        
+                }
+                else{
+                    errnum = 403;
+                }
+                break;
+                
+            case MbedJSONValue::TypeString:
+                if(tmpJson->accessible){
+                    *tmpJson = pDataJson->_value.asString;
+                    tmpJson->cb_action((void*)tmpJson->_value.asString->c_str()); 
+                }
+                else{
+                    errnum = 403;
+                }
+                break;
+                
+            case MbedJSONValue::TypeObject:
+                for(int i = 0; i < pDataJson->index_token; i++)
+                {
+                    tok = (char*)pDataJson->token_name[i]->c_str();
+#ifdef DEBUG_HTTPREQ                                                
+                    printf("pDataJson.token_name[%d]->c_str()=%s\r\n",i,tok);
+#endif                    
+                    if(tmpJson->hasMember(tok)){
+                        if((*tmpJson)[tok].accessible){
+                            errnum = 200;
+                            if((*pDataJson)[i].size() > 0){
                                 //(*tmpJson)[tok] = (*pDataJson)[i]._value.asString;
                                 (*tmpJson)[tok] = (*pDataJson)[i]._value.asString->c_str();
                                 
@@ -283,10 +469,6 @@
 }
 
 /*
-void PutRequestHandler::handle(char* rest_uri, char* request_data, char *reply)
-{
-}
-
 void DeleteRequestHandler::handle(char* rest_uri, char* request_data, char *reply)
 {
 }
diff -r 64db444b21f4 -r 60d99da6eeb2 RequestHandler.h
--- a/RequestHandler.h	Fri Jul 29 08:15:34 2016 +0000
+++ b/RequestHandler.h	Mon Sep 05 06:29:55 2016 +0000
@@ -18,21 +18,22 @@
         
 };
 
+/*
 class PostRequestHandler : public RequestHandler
 {
     public :
         virtual void handle(char* rest_uri, char* req_data, char* reply);
         
 };
+*/
 
-/* 
 class PutRequestHandler : public RequestHandler
 {
     public :
         virtual void handle(char* rest_uri, char* req_data, char* reply);
 
 };
-*/
+
 
 /*
 class DeleteRequestHandler : public RequestHandler
diff -r 64db444b21f4 -r 60d99da6eeb2 main.cpp
--- a/main.cpp	Fri Jul 29 08:15:34 2016 +0000
+++ b/main.cpp	Mon Sep 05 06:29:55 2016 +0000
@@ -3,16 +3,14 @@
 #include "RequestHandler.h"
 #include "EthernetInterface.h"
 #include "MbedJSONValue.h"
-// 20160630
-#include "Adafruit_SSD1306.h"
 
 #define SERVER_PORT 80
-
-#define DHCP
-
+//#define DHCP
 //#define DEBUG
 
 //-- I2C OLED --
+#include "Adafruit_SSD1306.h"
+
 class I2CPreInit : public I2C
 {
 public:
@@ -33,6 +31,10 @@
 //-- GPIO LED --
 DigitalInOut GP05(D5);
 
+DigitalInOut GP04(D4);
+DigitalInOut GP03(D3);
+
+
 //-- ADC --
 AnalogIn   ain(A0);
 
@@ -48,8 +50,9 @@
 char gateway_addr[] = "192.168.100.1";
 float ain_value;
 
-GetRequestHandler myGetReq;
-PostRequestHandler myPostReq;
+GetRequestHandler  myGetReq;
+//PostRequestHandler myPostReq;
+PutRequestHandler  myPutReq;
 
 //-- I2C OLED --
 bool oled_set(void* param)
@@ -77,6 +80,19 @@
     GP05.write(*(int*)param);
     return true;
 }
+bool p4_set(void* param)
+{
+    if(!param) return false;
+    GP04.write(*(int*)param);
+    return true;
+}
+bool p3_set(void* param)
+{
+    if(!param) return false;
+    GP03.write(*(int*)param);
+    return true;
+}
+
 //-- ADC --
 bool ain_read(void* param)
 {
@@ -116,8 +132,7 @@
     GP05.write(1);
     
     //ADC init
-    //
-
+    
     printf("START \r\n");    
     printf("sizeof(MbedJSONValue)=%d\r\n",sizeof(MbedJSONValue));
     printf("sizeof(vector)=%d\r\n",sizeof(std::vector<string*>));
@@ -133,14 +148,14 @@
 #endif
                     
     // Serialize it into a JSON string
-    printf("-------------------------WIZwikiREST--------------------------- \r\n");
+    printf("---------------------WIZwikiREST-------------------- \r\n");
     printf("\r\n%s\r\n", WIZwikiREST.serialize().c_str());
-    printf("--------------------------------------------------------------- \r\n");
+    printf("---------------------------------------------------- \r\n");
 
     WIZwikiWebSvr.add_request_handler("GET", &myGetReq);
-    WIZwikiWebSvr.add_request_handler("POST", &myPostReq);
+    //WIZwikiWebSvr.add_request_handler("POST", &myPostReq);
+    WIZwikiWebSvr.add_request_handler("PUT", &myPutReq);
     //WIZwikiWebSvr.add_request_handler("DELETE", new PostRequestHandler());
-    //WIZwikiWebSvr.add_request_handler("PUT", new PutRequestHandler());
     
     #ifdef DHCP
         eth.init(mac_addr); //Use DHCP
@@ -214,6 +229,12 @@
     WIZwikiREST["PWM"]["DC"].cb_action = pwm_set;
     
     // GPIO
+    WIZwikiREST["GPIOs"]["P03"] = GP03.read();
+    WIZwikiREST["GPIOs"]["P03"].accessible = true;
+    WIZwikiREST["GPIOs"]["P03"].cb_action = p3_set;
+    WIZwikiREST["GPIOs"]["P04"] = GP04.read();
+    WIZwikiREST["GPIOs"]["P04"].accessible = true;
+    WIZwikiREST["GPIOs"]["P04"].cb_action = p4_set;\
     WIZwikiREST["GPIOs"]["P05"] = GP05.read();
     WIZwikiREST["GPIOs"]["P05"].accessible = true;
     WIZwikiREST["GPIOs"]["P05"].cb_action = p5_set;
diff -r 64db444b21f4 -r 60d99da6eeb2 mbed.bld
--- a/mbed.bld	Fri Jul 29 08:15:34 2016 +0000
+++ b/mbed.bld	Mon Sep 05 06:29:55 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/6c34061e7c34
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/2e9cc70d1897
\ No newline at end of file