Upper Version Add PUT method Delete POST method

Dependencies:   Adafruit_GFX MbedJSONValue_v102 WIZnetInterface mbed

Fork of WIZwiki-REST-io_v102 by Lawrence Lee

Revision:
4:0b5a199e91f3
Parent:
2:ca36ecca24c2
Child:
5:473a66ae9596
diff -r 31ffbba7e8e9 -r 0b5a199e91f3 main.cpp
--- a/main.cpp	Tue May 03 02:21:56 2016 +0000
+++ b/main.cpp	Tue May 31 10:31:30 2016 +0000
@@ -8,6 +8,8 @@
 
 #define DHCP
 
+//#define DEBUG
+
 EthernetInterface eth;
 HTTPServer WIZwikiWebSvr;
 MbedJSONValue WIZwikiREST;
@@ -15,10 +17,13 @@
 // 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 ip_addr[]      = "192.168.100.100";
+char subnet_mask[]  = "255.255.255.0";
 char gateway_addr[] = "192.168.100.1";
 
+GetRequestHandler myGetReq;
+PostRequestHandler myPostReq;
+
 DigitalOut LED_1(PA_1);
 DigitalOut LED_2(PA_2);
 
@@ -51,9 +56,42 @@
 DigitalInOut GP31(P31);
 DigitalInOut GP32(P32);
 
+
+bool gpio_all(void* param)
+{
+    if(!param) return false;
+    GP05.write(*(int*)param);
+    GP06.write(*(int*)param);
+    GP07.write(*(int*)param);
+    GP08.write(*(int*)param);
+    GP09.write(*(int*)param);
+    GP10.write(*(int*)param);                    
+    GP11.write(*(int*)param);
+    GP12.write(*(int*)param);
+    GP13.write(*(int*)param);
+    GP14.write(*(int*)param);
+    GP15.write(*(int*)param);
+    GP16.write(*(int*)param);                    
+    GP17.write(*(int*)param);
+    GP18.write(*(int*)param);
+    GP19.write(*(int*)param);
+    GP20.write(*(int*)param);                    
+    GP21.write(*(int*)param);
+    GP22.write(*(int*)param);
+    GP23.write(*(int*)param);
+    GP24.write(*(int*)param);
+    GP25.write(*(int*)param);
+    GP26.write(*(int*)param);                    
+    GP27.write(*(int*)param);
+    GP28.write(*(int*)param);
+    GP29.write(*(int*)param);
+    return true;
+}
+
+
 bool p5_set(void* param)
 {
-    if(!param) return false;
+     if(!param) return false;
     GP05.write(*(int*)param);
     return true;
 }
@@ -224,16 +262,21 @@
 {
     printf("SP:0x%X\r\n",__current_sp());
     __heapstats((__heapprt)fprintf,stderr);
+#ifdef DEBUG
+    __heapvalid((__heapprt)fprintf,stderr, 1);
+#endif
     printf("\r\n");
 }    
+
 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]);
+#ifdef DEBUG
+    debug_info();
+#endif
+    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
     GP05.output();
     GP05.write(1);
@@ -286,28 +329,31 @@
     GP29.output();
     GP29.write(1);
 
-
     //LED Setbit
-    LED_1.write(0); // LED On
-    LED_2.write(1); // LED Off
+    //LED_1.write(0); // LED On
+    //LED_2.write(1); // LED Off
     printf("START \r\n");    
     printf("sizeof(MbedJSONValue)=%d\r\n",sizeof(MbedJSONValue));
     printf("sizeof(vector)=%d\r\n",sizeof(std::vector<string*>));
     printf("sizeof(string)=%d\r\n",sizeof(std::string));
+#ifdef DEBUG
     debug_info();
+#endif
 
     WIZwiki_REST_init();
 
+#ifdef DEBUG
     debug_info();
+#endif
                     
     // Serialize it into a JSON string
-    printf("\r\n");
     printf("-------------------------WIZwikiREST--------------------------- \r\n");
-    printf("%s\r\n", WIZwikiREST.serialize().c_str());
+    printf("\r\n%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("GET", &myGetReq);
+    WIZwikiWebSvr.add_request_handler("POST", &myPostReq);
+    //WIZwikiWebSvr.add_request_handler("DELETE", new PostRequestHandler());
     //WIZwikiWebSvr.add_request_handler("PUT", new PutRequestHandler());
     
     #ifdef DHCP
@@ -318,7 +364,7 @@
     
     
     printf("Check Ethernet Link\r\n");
-    //Add
+    
     do{
         printf("   Link - Wait... \r\n");
         wait(1);
@@ -334,15 +380,12 @@
         printf("   MASK : %s\r\n\r\n", eth.getNetworkMask());
         printf("   GW   : %s\r\n\r\n", eth.getGateway());
     }
-        
-    //while(1) //Wait link up
-    //{
-    //    if(eth.link() == true) 
-    //    break;
-    //}
     
     printf("Link up\r\n");
     printf("IP Address is %s\r\n", eth.getIPAddress());
+#ifdef DEBUG
+    debug_info();
+#endif
 
     if(!WIZwikiWebSvr.init(SERVER_PORT))
     {
@@ -361,30 +404,28 @@
     //Fill the object
     WIZwikiREST["Name"] = "WIZwiki-RESTful-01";
     WIZwikiREST["Name"].accessible = false;
+#ifdef DEBUG
     debug_info();
+#endif
     
     //Network
-
     WIZwikiREST["Network"]["MAC"] = mac_str;
-    debug_info();
     WIZwikiREST["Network"]["IP"] = ip_addr; 
     WIZwikiREST["Network"]["IP"].accessible = true; 
-    debug_info();
     WIZwikiREST["Network"]["SN"] = subnet_mask;  
     WIZwikiREST["Network"]["SN"].accessible = true;  
-    debug_info();
     WIZwikiREST["Network"]["GW"] = gateway_addr;
     WIZwikiREST["Network"]["GW"].accessible = true;
-    debug_info();
 
     //LEDs
-    WIZwikiREST["LED1"] = (LED_1.read() ? "Off" : "On");
-    WIZwikiREST["LED1"].accessible = true;
-    debug_info();    
-    WIZwikiREST["LED2"] = (LED_2.read() ? "Off" : "On");
-    WIZwikiREST["LED2"].accessible = true;
-    debug_info();
+    //WIZwikiREST["LED1"] = (LED_1.read() ? "Off" : "On");
+    //WIZwikiREST["LED1"].accessible = true;
+    //WIZwikiREST["LED2"] = (LED_2.read() ? "Off" : "On");
+    //WIZwikiREST["LED2"].accessible = true;
+    
     // GPIOs
+    WIZwikiREST["GPIOs"]["all"].accessible = true;
+    WIZwikiREST["GPIOs"]["all"].cb_action = gpio_all; 
     WIZwikiREST["GPIOs"]["P05"] = GP05.read();
     WIZwikiREST["GPIOs"]["P05"].accessible = true;
     WIZwikiREST["GPIOs"]["P05"].cb_action = p5_set;
@@ -439,7 +480,6 @@
     WIZwikiREST["GPIOs"]["P22"] = GP22.read();
     WIZwikiREST["GPIOs"]["P22"].accessible = true;
     WIZwikiREST["GPIOs"]["P22"].cb_action = p22_set;
-    
     WIZwikiREST["GPIOs"]["P23"] = GP23.read();
     WIZwikiREST["GPIOs"]["P23"].accessible = true;
     WIZwikiREST["GPIOs"]["P23"].cb_action = p23_set;
@@ -461,16 +501,8 @@
     WIZwikiREST["GPIOs"]["P29"] = GP29.read();
     WIZwikiREST["GPIOs"]["P29"].accessible = true;
     WIZwikiREST["GPIOs"]["P29"].cb_action = p29_set;
-/*    
-    WIZwikiREST["GPIOs"]["P30"] = GP30.read();
-    WIZwikiREST["GPIOs"]["P30"].accessible = true;
-    WIZwikiREST["GPIOs"]["P30"].cb_action = p30_set;
-    WIZwikiREST["GPIOs"]["P31"] = GP31.read();
-    WIZwikiREST["GPIOs"]["P31"].accessible = true;
-    WIZwikiREST["GPIOs"]["P31"].cb_action = p31_set;
-    WIZwikiREST["GPIOs"]["P32"] = GP32.read();
-    WIZwikiREST["GPIOs"]["P32"].accessible = true;
-    WIZwikiREST["GPIOs"]["P32"].cb_action = p32_set;
-*/
+
+#ifdef DEBUG
     debug_info();
+#endif
 }