Zoltan Hudak / Mbed OS TFTPServerTest

Dependencies:   TFTPServer

Fork of TFTPServerTest by Jaap Vermaas

Files at this revision

API Documentation at this revision

Comitter:
hudakz
Date:
Tue Mar 20 17:32:28 2018 +0000
Parent:
3:77497b352c3f
Child:
5:063211afa95c
Commit message:
Updated.

Changed in this revision

TFTPServer.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
sd-driver.lib Show annotated file Show diff for this revision Revisions of this file
--- a/TFTPServer.lib	Sat Mar 17 08:13:27 2018 +0000
+++ b/TFTPServer.lib	Tue Mar 20 17:32:28 2018 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/hudakz/code/TFTPServer/#9c973065a97e
+https://os.mbed.com/users/hudakz/code/TFTPServer/#f7c0fbc8c5aa
--- a/main.cpp	Sat Mar 17 08:13:27 2018 +0000
+++ b/main.cpp	Tue Mar 20 17:32:28 2018 +0000
@@ -2,7 +2,7 @@
  * example program for the TFTPServer library
  *
  * Copyright (c) 2011 Jaap Vermaas
- * Modified for MBED-OS5 by Zoltan Hudak 2018
+ * Modified by Zoltan Hudak 2018 for MBED-OS5
  *
  *   For more detaisl see https://os.mbed.com/users/hudakz/code/TFTPServerTest/
  *
@@ -21,7 +21,6 @@
  *   You should have received a copy of the GNU General Public License
  *   along with LaOS.  If not, see <http://www.gnu.org/licenses/>.
  * 
- * 
  */
 #include "mbed.h"
 #include "SDBlockDevice.h"
@@ -49,64 +48,84 @@
 int main()
 {
     // Try to mount the filesystem. This should work without the Ethernet.
-    pc.printf("Mounting the filesystem... \r\n");
+    pc.printf("Mounting the filesystem... ");
 
     int err = fs.mount(&bd);
     pc.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));
-
     if (err)
-        return 1;
+        return err;
 
     // Open a file. This should work without the Ethernet, too.
-    pc.printf("Opening \"/fs/test.txt\"... \r\n");
+    pc.printf("Opening file '/fs/test.txt'... ");
 
-    FILE*   fp = fopen("/fs/test.txt", "r+");
+    FILE*   fp = fopen("/fs/test.txt", "w+");
     pc.printf("%s\r\n", (!fp ? "Failed :(\r\n" : "OK\r\n"));
 
     if (!fp)
     {
-        // Create the test file, if it doesn't exist yet.
+        // Create the test file if it doesn't exist
         pc.printf("No file found, creating a new file ...\r\n");
         fp = fopen("/fs/test.txt", "w+");
         pc.printf("%s\r\n", (!fp ? "Failed :(" : "OK"));
         if (!fp)
+        {
             error("error: %s (%d)\r\n", strerror(errno), -errno);
-
-        for (int i = 0; i < 10; i++)
+            return errno;
+        }
+    }
+    
+    for (int i = 0; i < 10; i++)
+    {
+        pc.printf("Writing numbers (%d/%d)... ", i, 10);
+        err = fprintf(fp, "    %d\r\n", i);
+        if (err < 0)
         {
-            pc.printf("\rWriting numbers (%d/%d)... ", i, 10);
-            err = fprintf(fp, "    %d\n", i);
-            if (err < 0)
-            {
-                pc.printf("Fail :(\r\n");
-                error("error: %s (%d)\n", strerror(errno), -errno);
-            }
+            pc.printf("Fail :(\r\n");
+            error("error: %s (%d)\r\n", strerror(errno), -errno);
         }
+        else
+            pc.printf("OK\r\n");
+    }
 
-        pc.printf("\rWriting numbers (%d/%d)... OK\r\n", 10, 10);
-
-        pc.printf("Seeking file ...\r\n");
-        err = fseek(fp, 0, SEEK_SET);
-        pc.printf("%s\r\n", (err < 0 ? "Failed :(" : "OK"));
-        if (err < 0)
-            error("error: %s (%d)\r\n", strerror(errno), -errno);
-    }
+    pc.printf("Writing numbers (%d/%d)... OK\r\n\r\n", 10, 10);
+    
+    err = fclose(fp);
+    pc.printf("Closing file '/fs/test.txt'... ");
+    pc.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));
+    if (err)
+      return err;
 
     // Connect to the Ethernet
     pc.printf("Connecting to the Ethernet using ");
 #ifdef USE_DHCP
-    pc.printf("DHCP server ...\r\n");
+    pc.printf("DHCP server... ");
 #else
     pc.printf("fixed IP ...\r\n");
     net.set_network("192.168.1.185", "255.255.255.0", "192.168.1.1");
 #endif
-    net.connect();
-    pc.printf("IP: %s\r\n", net.get_ip_address());
+    err = net.connect();
+    pc.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));
+    if (err)
+        return err;
+
+    pc.printf("IP:      %s\r\n", net.get_ip_address());
     pc.printf("NetMask: %s\r\n\r\n", net.get_netmask());
 
     // Create a TFTP server.
-    pc.printf("Creating a TFTP server ...\r\n\r\n");
+    pc.printf("Creating a TFTP server... ");
     server = new TFTPServer(&net, myPort);
+    pc.printf("%s\r\n", (server ? "OK" : "Failed :("));
+    if (!server)
+        return 1;
+    
+    if (server->getState() == TFTPServer::LISTENING)
+        pc.printf("\r\nThe TFTP server is listening at port %d.\r\n", myPort);
+    else
+    {
+        pc.printf("\r\nSomething went wrong.\r\nThe TFTP server is not listening.\r\n");
+        return 1;
+    }
+    
 
     int     fileCount = 0;  // incoming files
     char    fileName[256];  // to display filenames
@@ -117,58 +136,11 @@
     {
         server->poll();
 
-        if (pc.readable())
-        {
-            int c = pc.getc();
-            switch (c) {
-                case 's':
-                    server->suspend();
-                    break;
-
-                case 'r':
-                    server->resume();
-                    break;
-            }
-        }
-
         if (server->fileCount() > fileCount)
         {
             fileCount = server->fileCount();
             server->getFileName(fileName);
             pc.printf("new file: %s\r\n", fileName);
         }
-
-        if (timer.read() > 2)
-        {
-            timer.reset();
-
-            switch (server->getState()) {
-                case TFTPServer::LISTENING:
-                    pc.printf("main: TFTP server is listening\r\n");
-                    break;
-
-                case TFTPServer::READING:
-                    server->getFileName(fileName);
-                    pc.printf("main: TFTP server is reading file: %s\r\n", fileName);
-                    break;
-
-                case TFTPServer::WRITING:
-                    server->getFileName(fileName);
-                    pc.printf("main: TFTP server is writing file: %s\r\n", fileName);
-                    break;
-
-                case TFTPServer::ERROR:
-                    pc.printf("main: TFTP error\r\n");
-                    break;
-
-                case TFTPServer::SUSPENDED:
-                    pc.printf("main: TFTP suspended\r\n");
-                    break;
-
-                default:
-                    pc.printf("main: Unknown TFTP status\r\n");
-                    break;
-            }
-        }
     }
 }
--- a/sd-driver.lib	Sat Mar 17 08:13:27 2018 +0000
+++ b/sd-driver.lib	Tue Mar 20 17:32:28 2018 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/sd-driver/#c16fa2bf36b87e862f81e0b30dbddca1460e1084
+https://github.com/ARMmbed/sd-driver/#10aac9f16207e81d0f231dfc9eef7ac88ad32c27