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:
- 1:698437bcd48d
- Parent:
- 0:5c9e0971532d
- Child:
- 2:228ce0f5c9c8
diff -r 5c9e0971532d -r 698437bcd48d main.cpp --- a/main.cpp Tue Dec 27 10:41:28 2011 +0000 +++ b/main.cpp Fri Mar 16 19:28:34 2018 +0000 @@ -1,16 +1,16 @@ /* - * example program for TFTPServer class + * example program for the TFTPServer library * * How to use: - * -> change the IP to something that works for you + * -> use DHCP or change the IP to something that works for you * -> connect to the mbed with a TFTP client in binary(octet) mode * -> use (s)uspend and (r)esume keys to turn TFTP server on/off - * -> upload any file, but .bin files are always put on the internal - * memory, as are config.txt files + * -> upload any file * * Copyright (c) 2011 Jaap Vermaas + * Modified for MBED-OS5 by Zoltan Hudak 2018 * - * This file is part of the LaOS project (see: http://wiki.laoslaser.org + * This file is part of the LaOS project (see: http://wiki.laoslaser.org) * * LaOS is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,97 +24,162 @@ * * 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 "SDFileSystem.h" +#include "SDBlockDevice.h" +#include "FATFileSystem.h" +#include "EthernetInterface.h" #include "TFTPServer.h" -#include "EthernetNetIf.h" -LocalFileSystem local("local"); // defining this makes MBED internal mem accessible +#include "errno.h" + +Serial pc(USBTX, USBRX); +SDBlockDevice bd(p5, p6, p7, p8); // SD card SPI driver pins: mosi, miso, sclk, cs +FATFileSystem fs("fs"); // FAT file sytem +EthernetInterface net; // Ethernet interface +TFTPServer* server; // Pointer to a TFTPServer +int myPort = 69; // TFPTServer port +Timer timer; // For sending debug messages to the remote client every two seconds + +#define USE_DHCP -TFTPServer *srv; // define the TFTP Server -Timer t; // used in main() to send debug output every 2 seconds -Serial *serial; // serial just for debugging -SDFileSystem sd(p11, p12, p13, p14, "sd"); // only needed if you save to SD card -EthernetNetIf *eth; // network device +/** + * @brief + * @note + * @param + * @retval + */ +int main() +{ + // Try to mount the filesystem. This should work without the Ethernet. + pc.printf("Mounting the filesystem... \r\n"); +// fflush(stdout); + + int err = fs.mount(&bd); + pc.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n")); -int main() { - - // configure serial terminal - serial = new Serial(USBTX, USBRX); - serial->baud(115200); + if (err) + return 1; + + // Open a file. This should work without the Ethernet, too. + pc.printf("Opening \"/fs/test.txt\"... \r\n"); +// fflush(stdout); + + FILE* fp = fopen("/fs/test.txt", "r+"); + pc.printf("%s\r\n", (!fp ? "Failed :(\r\n" : "OK\r\n")); + + if (!fp) + { + // Create the test file if it doesn't exist + pc.printf("No file found, creating a new file ...\r\n"); +// fflush(stdout); + fp = fopen("/fs/test.txt", "w+"); + pc.printf("%s\r\n", (!fp ? "Fail :(" : "OK")); + if (!fp) + error("error: %s (%d)\r\n", strerror(errno), -errno); - // set up networking - eth = new EthernetNetIf( - IpAddr(192,168,1,111), //IP Address - IpAddr(255,255,255,0), //Network Mask - IpAddr(192,168,1,1), //Gateway - IpAddr(192,168,1,1) //DNS - ); - eth->setup(); - - // test SD card (TFTP server can work without it) - printf("TEST SD...\n\r"); - FILE *fp = fopen("/sd/test.txt", "wb"); - if ( fp == NULL ) - printf("SD: NOT READY\n\r"); - else { - printf("SD: READY...\n\r"); - fclose(fp); + for (int i = 0; i < 10; i++) + { + pc.printf("\rWriting numbers (%d/%d)... ", i, 10); +// fflush(stdout); + err = fprintf(fp, " %d\n", i); + if (err < 0) + { + pc.printf("Fail :(\r\n"); + error("error: %s (%d)\n", strerror(errno), -errno); + } + } + + pc.printf("\rWriting numbers (%d/%d)... OK\r\n", 10, 10); + + pc.printf("Seeking file ...\r\n"); +// fflush(stdout); + err = fseek(fp, 0, SEEK_SET); + pc.printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); + if (err < 0) + { + error("error: %s (%d)\r\n", strerror(errno), -errno); + } } - remove("/sd/test.txt"); - // start tftp server with the work dir as a comment - // use "/local/" for the internal mbed device - srv = new TFTPServer("/sd/"); + // Connect to the Ethernet + pc.printf("Connecting to the Ethernet using "); +#ifdef USE_DHCP + pc.printf("DHCP server ...\r\n"); +#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()); + 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"); + server = new TFTPServer(&net, myPort); - t.start(); - int filecounter = 0; // incoming files - char filename[256]; // to display filenames - while (1) { - Net::poll(); - if (serial->readable()) { - int c = serial->getc(); + int fileCount = 0; // incoming files + char fileName[256]; // to display filenames + + timer.start(); + + while (1) + { + server->poll(); + + if (pc.readable()) + { + int c = pc.getc(); switch (c) { - case 's': - srv->suspend(); + case 's': + server->suspend(); break; + case 'r': - srv->resume(); + server->resume(); break; } } - - if (srv->fileCnt() > filecounter) { - filecounter = srv->fileCnt(); - srv->getFilename(filename); - printf("new file: %s\n\r", filename); + + if (server->fileCount() > fileCount) + { + fileCount = server->fileCount(); + server->getFileName(fileName); + pc.printf("new file: %s\r\n", fileName); } - if (t.read() > 2) { - t.reset(); - TFTPServerState state = srv->State(); - switch(state) { - case listen: - printf("MAIN: TFTP listen\n\r"); + + if (timer.read() > 2) + { + timer.reset(); + + switch (server->getState()) { + case TFTPServer::LISTENING: + pc.printf("main: TFTP server is listening\r\n"); break; - case reading: - srv->getFilename(filename); - printf("MAIN: TFTP reading file: %s\n\r", filename); + + case TFTPServer::READING: + server->getFileName(fileName); + pc.printf("main: TFTP server is reading file: %s\r\n", fileName); break; - case writing: - srv->getFilename(filename); - printf("MAIN: TFTP writing file: %s\n\r", filename); + + case TFTPServer::WRITING: + server->getFileName(fileName); + pc.printf("main: TFTP server is writing file: %s\r\n", fileName); break; - case error: - printf("MAIN: TFTP error\n\r"); + + case TFTPServer::ERROR: + pc.printf("main: TFTP error\r\n"); break; - case suspended: - printf("MAIN: TFTP suspended\n\r"); + + case TFTPServer::SUSPENDED: + pc.printf("main: TFTP suspended\r\n"); break; + default: - printf("MAIN: Unknown TFTP status\n\r"); + pc.printf("main: Unknown TFTP status\r\n"); break; } } } } -