lpc1768-picotcp-eth to revision 8, PicoTCP to revision 41 (mbed-rtos to revision 12, mbed to revision 63)

Dependencies:   PicoTCP lpc1768-picotcp-eth mbed-rtos mbed

Fork of TCPSocket_HelloWorldTest by Daniel Peter

Files at this revision

API Documentation at this revision

Comitter:
mbed714
Date:
Tue Feb 26 21:39:22 2013 +0000
Parent:
11:59dcefdda506
Child:
13:52706c2a28f4
Commit message:
Locks up

Changed in this revision

EthernetInterface.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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/EthernetInterface.lib	Fri Aug 10 09:41:51 2012 +0000
+++ b/EthernetInterface.lib	Tue Feb 26 21:39:22 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/EthernetInterface/#c9bb345dcc65
+http://mbed.org/users/mbed_official/code/EthernetInterface/#dd9794ce1d64
--- a/main.cpp	Fri Aug 10 09:41:51 2012 +0000
+++ b/main.cpp	Tue Feb 26 21:39:22 2013 +0000
@@ -1,31 +1,90 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
 
-int main() {
-    EthernetInterface eth;
-    eth.init(); //Use DHCP
-    eth.connect();
+#define RETRIES_ALLOWED 5
+
+Serial pc(USBTX, USBRX);
+EthernetInterface eth;
+DigitalOut led1(LED1);
+
+int main()
+{
+
+    pc.baud(115200);
+    printf("Initialising...\n");
+    set_time(0);
+
+    // use DHCP
+    eth.init();
+
+    // attempt DHCP and if timing out then try again
+    while (eth.connect()) {
+        printf("DHCP timeout\n");
+    };
+
+    // successful DHCP
     printf("IP Address is %s\n", eth.getIPAddress());
-    
-    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;
+
+    // loop forever
     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);
+
+        // new non blocking socket
+        TCPSocketConnection sock;
+        sock.set_blocking(false);
+
+        // toggle led to indicate activity
+        led1 = !led1;
+
+        // attempt socket connection and if failure then loop again
+        if (sock.connect("mbed.org", 80)) {
+            printf("Socket connect timeout\n");
+            continue;
+        }
+
+        // send command
+        printf("Sending at %d seconds\n", time(NULL));
+        char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
+        sock.send_all(http_cmd, sizeof(http_cmd) - 1);
+
+        // receive response
+        int received = 0;
+        int retries = 0;
+        while (true) {
+
+            int result;
+            char buffer[256];
+
+            result = sock.receive(buffer, sizeof(buffer) - 1);
+            printf("Received: %d %d %d\n", result, received, retries);
+
+            // if timeout or no data
+            if (result <= 0) {
+
+                // if data previously received then stop receiving
+                if (received > 0)
+                    break;
+
+                // if incremented retries exceeded then no response and stop receiving
+                if (++retries > RETRIES_ALLOWED) {
+                    break;
+                }
+
+            } else {
+
+                // zero terminate the buffer
+                buffer[result] = '\0';
+
+                // total size of data received
+                received += result;
+            }
+
+        }
+
+        // close socket at end of send and receive
+        sock.close();
+        
+        // wait before repeating
+        Thread::wait(1000);
+
     }
-      
-    sock.close();
-    
-    eth.disconnect();
-    
-    while(1) {}
-}
+}
\ No newline at end of file
--- a/mbed.bld	Fri Aug 10 09:41:51 2012 +0000
+++ b/mbed.bld	Tue Feb 26 21:39:22 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/976df7c37ad5
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/3d0ef94e36ec
\ No newline at end of file