TCP/IP based digital io controller for operating DigitalsOuts and reading DigitalIns.

Dependencies:   EthernetInterface NetworkAPI mbed-rtos mbed

Fork of NetRelais by Roy van Dam

Revision:
11:e5375ae5c8c3
Parent:
10:22d49341340c
--- a/controller.cpp	Thu Jul 19 11:13:50 2012 +0000
+++ b/controller.cpp	Thu Sep 27 10:00:27 2012 +0000
@@ -56,6 +56,7 @@
 {
     this->_network.server.close();
     this->_network.client.close();
+    return 0;
 }
 
 int
@@ -87,15 +88,15 @@
                     break;
                 
                 default:
-                    printf("Received %d bytes.\n\r%s\r", buffer.length(), (char *)buffer.pointer());
+                    printf("Received %d bytes.\n\r%s\r", buffer.length(), (char *)buffer.data());
                     
                     // Parse command
                     result = this->_parseCommand(buffer);
     
                     // Format reply code
                     buffer.flush();
-                    buffer.setLength(std::snprintf(
-                        (char *)buffer.pointer(), buffer.size(),
+                    buffer.length(std::snprintf(
+                        (char *)buffer.data(), buffer.size(),
                         "e;%i;", result));
                    
                     if (buffer.length() < 4) {
@@ -112,7 +113,9 @@
             this->_network.client.shutdown();
             this->_network.client.close();
         }
-    } 
+    }
+    
+    return 0;
 }
 
 int
@@ -120,7 +123,7 @@
 {
     int index = 0;
     network::Buffer response(32);
-    char *cursor = (char *)buffer.pointer();
+    char *cursor = (char *)buffer.data();
     
     enum Controller::ParseState state = Controller::S_Init;
     enum Controller::Command command = Controller::C_None;
@@ -128,14 +131,14 @@
     while (cursor != NULL) {
         switch (state) {
             case Controller::S_Init: {
-                if (((*cursor) == 'r') && ((*(cursor + 1)) == ';')) {
+                if (((*cursor) == 'r') && ((*(cursor + 1)) == ':')) {
                     command = Controller::C_Read;
                     state = Controller::S_Index;
                     cursor += 2;
                     continue;
                 }
                 
-                if (((*cursor) == 'w') && ((*(cursor + 1)) == ';')) {
+                if (((*cursor) == 'w') && ((*(cursor + 1)) == ':')) {
                     command = Controller::C_Write;
                     state = Controller::S_Index;
                     cursor += 2;
@@ -150,7 +153,19 @@
                     return Controller::E_InvalidFormat;
                 }
                 
-                char *offset = std::strchr(cursor, ';');
+                char *offset;
+                switch (command) {
+                    case Controller::C_Read: {
+                        offset = std::strchr(cursor, ';');
+                        break;
+                    }
+                    
+                    case Controller::C_Write: {
+                        offset = std::strchr(cursor, ':');
+                        break;
+                    }
+                }
+                
                 if (offset == NULL) {
                     return Controller::E_InvalidFormat;
                 }
@@ -168,9 +183,9 @@
                             return Controller::E_UnknownIndex;
                         }
                         
-                        response.setLength(std::snprintf(
-                            (char *)response.pointer(), response.size(),
-                            "r;%d;%d;", index, input->read()));
+                        response.length(std::snprintf(
+                            (char *)response.data(), response.size(),
+                            "r:%d:%d;", index, input->read()));
                        
                         if (response.length() < 6) {
                             printf("Warning: failed to format reply.\n\r");