This is an example code for using the TFTPServer library.
Fork of TFTPServerTest by
A simple TFTP server built with the TFTPServer library
In this example a FAT formatted SD card is connected to the MBED board to provide a storage place for a TFTP server. This will allow us to upload files to or download files from the TFTP server (SD card) over an Ethernet connection as well as to use the files uploaded to the TFTP server in a program running on the MBED board.
Schematic
We can use for example the SD Formatter utility to format an SD card for the FAT file system .
NOTE: In the commands below substitute 192.168.1.185
with the actual IP address of your TFTP server. It's printed to the serial terminal of the connected PC during the server start up.
To upload or download files from a MS Windows PC we can use the TFTP client utility as follows:
- To upload for example the
test.txt
file to the TFTP server:
- Open a command prompt in the folder the file is located in.
- Type
tftp 192.168.1.185 put test.txt /fs/test.txt
and hit ENTER
- Open a command prompt in the folder the file is located in.
- To download for example the
test.txt
file from the TFTP server:
- Open a command prompt in the destination folder.
- Type
tftp 192.168.1.185 get /fs/test.txt test.txt
and hit ENTER
- Open a command prompt in the destination folder.
A similar built-in TFTP client is available also on MAC and Linux operating systems.
References:
- The Trivial File Transfer Protocol (TFTP)
- RFC 1350 - The TFT Protocol
- LaOS (The Laser Open Source) project
Diff: main.cpp
- Revision:
- 4:895c0ae49294
- Parent:
- 3:77497b352c3f
- Child:
- 5:063211afa95c
--- 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; - } - } } }