Webserver only w/o any other functions, single thread. Running on STM32F013+W5500

Dependencies:   NTPClient W5500Interface Watchdog device_configuration eeprom_flash mbed-rpc-nucleo mbed-rtos mbed

Fork of F103-Serial-to-Ethernet by Chau Vo

Revision:
28:00c0c20d03c1
Parent:
27:22f289beceb8
Child:
29:bc052f283ada
--- a/main.cpp	Mon Dec 29 21:40:55 2014 +0000
+++ b/main.cpp	Mon Dec 29 23:38:26 2014 +0000
@@ -169,6 +169,7 @@
 char tcp_receiving_buffer[256];
 char tcp_sending_buffer[256]; // socket buffer
 char udp_receiving_buffer[256];
+char rpc_outbuf[256]; // rpc output buffer
 
 
 /*
@@ -240,7 +241,7 @@
 
 // Prototypes
 int ethernet_init(void);
-void process_control_command(char* received_buffer, int len);
+int process_control_command(char* received_buffer, int len);
 void process_config_command(char* received_buffer, int len);
 void update_digital_outputs(char* buf);
 void update_sending_frame(char* buf);
@@ -388,10 +389,14 @@
                     n = tcp_client.receive(tcp_receiving_buffer, sizeof(tcp_receiving_buffer));
                     if (n <= 0) break;
                     
-                    // got some data, test it
+                    // got some data, process it
                     tcp_receiving_buffer[n] = '\0'; // for debugging purpose
                     DBG("TCP server received: %s", tcp_receiving_buffer);
-                    process_control_command(tcp_receiving_buffer, n);
+                    n = process_control_command(tcp_receiving_buffer, n);
+                    // send rpc reply back to client, NNIO protocol always returns 0
+                    if (n > 0) {
+                        tcp_client.send_all(rpc_outbuf, strlen(rpc_outbuf));
+                    }
                 } // end loop if no data received within timeout
             } // if client connected
             tcp_client.close();
@@ -433,13 +438,20 @@
             } // while (n > 0), config loop
         } // if (config_mode_flag)
         else if ((n > 0) && (!discovery_mode_flag)) { // process control packages sent using UDP
-            process_control_command(udp_receiving_buffer, n);
+            n = process_control_command(udp_receiving_buffer, n);
+            // send rpc reply back to client, NNIO protocol always returns 0
+            if (n > 0) {
+                udp_server.sendTo(ep_udp_client, rpc_outbuf, strlen(rpc_outbuf));
+            }
         }
 #endif
     } // network processor
 }
 
 
+/*
+ * Process NNCF commands
+ */
 void process_config_command(char* received_buffer, int len)
 {
     DBG("Processing configuration command");
@@ -542,13 +554,14 @@
 }
 
 /*
-* Procedure to process receiving protocol, which includes command to control outputs
-*/
-void process_control_command(char* received_buffer, int len)
+ * Procedure to process receiving protocol, which includes command to control outputs
+ * Return: 0 if NNIO protocol; length of rpc output buffer if rpc; -1 if rpc failed
+ */
+int process_control_command(char* received_buffer, int len)
 {
     char* received_frame;
     int pos;
-    char inbuf[256], outbuf[256];
+    char inbuf[256];
     
     DBG("Processing control command");
     
@@ -557,19 +570,25 @@
     inbuf[len] = '\r'; // use inbuf for RPC protocol
     inbuf[len+1] = '\n';
     inbuf[len+2] = '\0'; // add CR-LF
-    DBG("inbuf = %s", inbuf);
     if ((len > 0) && (inbuf[0] == '/')) {
         bool result;
-        result = RPC::call(inbuf, outbuf);
+        DBG("Rpc command = %s", inbuf);
+        result = RPC::call(inbuf, rpc_outbuf);
         if (result) {
-            DBG("I: %s O: %s\n", inbuf, outbuf);
+            // calculate length of rpc_outbuf
+            int i = strlen(rpc_outbuf);
+            DBG("Rpc reply (%d bytes): %s", i, rpc_outbuf);
+            return i; // return length of rpc_outbuf
         }
         else {
             ERR("Failed: %s", inbuf);
+            return -1;
         }
     }
     
-    // NNIO protocol
+    /*
+     * This section below is  for NNIO protocol
+     */
     while (len >= RECEIVING_PROTOCOL_LENGTH) {
         // find device ID
         DBG("Checking device ID...");
@@ -625,7 +644,9 @@
             DBG("Sent");
         }
     }
+    
     DBG("Successfully processed.");
+    return 0;
 }