-

Dependencies:   EthernetInterface TCPSocket_HelloWorld mbed-rtos mbed

Fork of TCPSocket_HelloWorld by mbed official

Files at this revision

API Documentation at this revision

Comitter:
offroad
Date:
Sat Nov 07 15:46:54 2015 +0000
Parent:
14:72be2b8b7f24
Child:
16:22169975b5d8
Commit message:
1v0

Changed in this revision

TCPSocket_rel1v0.lib 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TCPSocket_rel1v0.lib	Sat Nov 07 15:46:54 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/TCPSocket_HelloWorld/#72be2b8b7f24
--- a/main.cpp	Wed May 14 15:07:26 2014 +0000
+++ b/main.cpp	Sat Nov 07 15:46:54 2015 +0000
@@ -1,31 +1,182 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
+LocalFileSystem local("local");    
+
+#define NPORTS 30
+static char FSM_buf[16];
+#define FSM_STATE_IDLE      0
+#define FSM_STATE_READNUM   1
+static int FSM_state1 = FSM_STATE_IDLE;
+static int FSM_state2 = 0;
+static char FSM_cmd = 'x';
+
+static void FSM_handleChar(DigitalOut* gpo, char inputChar, char* bufOut, int* nOut){
+    // === reset reply ===
+    *nOut = 0;
+
+    // === handle incoming char ===
+    switch (FSM_state1){
+        case FSM_STATE_IDLE:
+            // === handle new command char ===
+            switch(inputChar){
+                case '+':
+                case '-':
+                case '?': 
+                    FSM_cmd = inputChar;
+                    FSM_state1 = FSM_STATE_READNUM;
+                    goto doneCont;
+                default: 
+                    goto reset;
+            } // switch inputChar
+            goto lockup;
+        case FSM_STATE_READNUM:
+            if (FSM_state2 > 8)
+                goto reset; // too many digits
+            
+            if ((inputChar == 32 || inputChar == 9 || inputChar == 13 || inputChar == 10) && (FSM_state2 > 0)){
+                // === got command terminated by whitespace, and at least one digit
+                FSM_buf[FSM_state2] = 0;
+                int ixGpio = atoi(FSM_buf);
+                if (ixGpio < 0 || ixGpio >= NPORTS)
+                    goto reset; // invalid port number
+                switch(FSM_cmd){
+                    case '+':
+                        // === set GPO ===
+                        *(gpo+ixGpio) = 1; 
+                        goto reset; // success
+                    case '-':
+                        // === clear GPO ===
+                        *(gpo+ixGpio) = 0;
+                        goto reset; // success
+                    case '?':
+                        // === query GPO state (verify) ===
+                        bufOut[(*nOut)++] = *(gpo+ixGpio) ? '1' : '0';
+                        bufOut[(*nOut)++] = 13;
+                        bufOut[(*nOut)++] = 10;
+                        goto reset; // success   
+                    default:
+                        goto reset; // invalid command byte
+                }
+            }
+            
+            if (inputChar >= '0' && inputChar <= '9'){
+                // === append digit ===
+                FSM_buf[FSM_state2++] = inputChar;
+                goto doneCont;
+            }
+            
+            // invalid character (not digit)
+            goto reset;
+        default:
+            // === invalid FSM state ===
+            goto lockup;
+    } // switch FSM_state1
+
+    // === internal error. May never be reached ===
+lockup: 
+    goto lockup; 
+
+    // === reset state machine (on completion and any invalid input ===
+reset:
+    FSM_state1 = FSM_STATE_IDLE;
+    FSM_state2 = 0;
+    FSM_cmd = 'x';
+
+doneCont:
+    return;
+}
 
 int main() {
+    printf("\r\nGPIO server v2 20151106 mn\r\n");
+    fflush(stdout);
+    static DigitalOut myGpo[NPORTS] = {
+        DigitalOut(LED1),
+        DigitalOut(LED2),
+        DigitalOut(LED3),
+        DigitalOut(LED4),
+        DigitalOut(p5),
+        DigitalOut(p6),
+        DigitalOut(p7),
+        DigitalOut(p8),
+        DigitalOut(p9),
+        DigitalOut(p10),
+        DigitalOut(p11),
+        DigitalOut(p12),
+        DigitalOut(p13),
+        DigitalOut(p14),
+        DigitalOut(p15),
+        DigitalOut(p16),
+        DigitalOut(p17),
+        DigitalOut(p18),
+        DigitalOut(p19),
+        DigitalOut(p20),
+        DigitalOut(p21),
+        DigitalOut(p22),
+        DigitalOut(p23),
+        DigitalOut(p24),
+        DigitalOut(p25),
+        DigitalOut(p26),
+        DigitalOut(p27),
+        DigitalOut(p28),
+        DigitalOut(p29),
+        DigitalOut(p30)};
+
+    char buffer[256];
+    char ipAddress[256];
+    char subnetMask[256];
+    sprintf(ipAddress, "%s", "192.168.1.10");
+    sprintf(subnetMask, "%s", "255.255.255.0");
+    int port = 7;
+
+    FILE* f = fopen("/local/config.txt", "r");
+    if (f != NULL){
+        int n = fscanf(f, "%s", ipAddress); if (n <= 0) goto endOfFile;
+        n = fscanf(f, "%s", subnetMask); if (n <= 0) goto endOfFile;
+        n = fscanf(f, "%s", buffer); if (n <= 0) goto endOfFile;
+        port = atoi(buffer);
+    endOfFile:
+        fclose(f);
+    }
+
     EthernetInterface eth;
-    eth.init(); //Use DHCP
+    eth.init(ipAddress, subnetMask, "");
     eth.connect();
-    printf("IP Address is %s\n", eth.getIPAddress());
+    printf("IP address: %s (configured: %s) port %i\r\n", eth.getIPAddress(), ipAddress, port);
     
-    TCPSocketConnection sock;
-    sock.connect("mbed.org", 80);
-    
-    char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
-    sock.send_all(http_cmd, sizeof(http_cmd)-1);
-    
-    char buffer[300];
-    int ret;
+    TCPSocketServer server;
+    server.bind(port);
+    server.listen();
+
     while (true) {
-        ret = sock.receive(buffer, sizeof(buffer)-1);
-        if (ret <= 0)
-            break;
-        buffer[ret] = '\0';
-        printf("Received %d chars from server:\n%s\n", ret, buffer);
-    }
-      
-    sock.close();
-    
-    eth.disconnect();
-    
-    while(1) {}
-}
+        printf("\nWait for new connection...\n");
+        TCPSocketConnection client;
+        server.accept(client);
+        client.set_blocking(true, 0);
+        
+        printf("Connection from: %s\n", client.get_address());
+        client.send_all(">\r\n", 3);
+
+        // === main loop ===
+        while (true) {
+
+            // === read data from socket ===
+            int n = client.receive(buffer, sizeof(buffer));
+
+            // === detect connection break ===
+            if (n < 0)
+                break;
+            
+            // === handle incoming data ===
+            int ix;
+            char bufOut[255];
+            int nOut = 0;
+            for (ix = 0; ix < n; ++ix)
+                FSM_handleChar(&myGpo[0], buffer[ix], bufOut, &nOut);
+
+            // === send reply ===
+            if (nOut > 0)
+                client.send_all(bufOut, nOut);
+        } // while connection
+        client.close();
+    } // eternal main loop
+} // void main