This is an example code for using the TFTPServer library.

Dependencies:   TFTPServer

Fork of TFTPServerTest by Jaap Vermaas

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

/media/uploads/hudakz/tftpserver01.png

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
  • 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

A similar built-in TFTP client is available also on MAC and Linux operating systems.

References:
Committer:
hudakz
Date:
Sat Mar 17 08:13:27 2018 +0000
Revision:
3:77497b352c3f
Parent:
2:228ce0f5c9c8
Child:
4:895c0ae49294
Updated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tuxic 0:5c9e0971532d 1 /*
hudakz 1:698437bcd48d 2 * example program for the TFTPServer library
tuxic 0:5c9e0971532d 3 *
tuxic 0:5c9e0971532d 4 * Copyright (c) 2011 Jaap Vermaas
hudakz 1:698437bcd48d 5 * Modified for MBED-OS5 by Zoltan Hudak 2018
tuxic 0:5c9e0971532d 6 *
hudakz 3:77497b352c3f 7 * For more detaisl see https://os.mbed.com/users/hudakz/code/TFTPServerTest/
hudakz 3:77497b352c3f 8 *
hudakz 3:77497b352c3f 9 * The TFTPServer is part of the LaOS project (see: http://wiki.laoslaser.org)
tuxic 0:5c9e0971532d 10 *
tuxic 0:5c9e0971532d 11 * LaOS is free software: you can redistribute it and/or modify
tuxic 0:5c9e0971532d 12 * it under the terms of the GNU General Public License as published by
tuxic 0:5c9e0971532d 13 * the Free Software Foundation, either version 3 of the License, or
tuxic 0:5c9e0971532d 14 * (at your option) any later version.
tuxic 0:5c9e0971532d 15 *
tuxic 0:5c9e0971532d 16 * LaOS is distributed in the hope that it will be useful,
tuxic 0:5c9e0971532d 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
tuxic 0:5c9e0971532d 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tuxic 0:5c9e0971532d 19 * GNU General Public License for more details.
tuxic 0:5c9e0971532d 20 *
tuxic 0:5c9e0971532d 21 * You should have received a copy of the GNU General Public License
tuxic 0:5c9e0971532d 22 * along with LaOS. If not, see <http://www.gnu.org/licenses/>.
hudakz 1:698437bcd48d 23 *
hudakz 1:698437bcd48d 24 *
tuxic 0:5c9e0971532d 25 */
tuxic 0:5c9e0971532d 26 #include "mbed.h"
hudakz 1:698437bcd48d 27 #include "SDBlockDevice.h"
hudakz 1:698437bcd48d 28 #include "FATFileSystem.h"
hudakz 1:698437bcd48d 29 #include "EthernetInterface.h"
tuxic 0:5c9e0971532d 30 #include "TFTPServer.h"
hudakz 1:698437bcd48d 31 #include "errno.h"
hudakz 1:698437bcd48d 32
hudakz 1:698437bcd48d 33 Serial pc(USBTX, USBRX);
hudakz 1:698437bcd48d 34 SDBlockDevice bd(p5, p6, p7, p8); // SD card SPI driver pins: mosi, miso, sclk, cs
hudakz 1:698437bcd48d 35 FATFileSystem fs("fs"); // FAT file sytem
hudakz 1:698437bcd48d 36 EthernetInterface net; // Ethernet interface
hudakz 1:698437bcd48d 37 TFTPServer* server; // Pointer to a TFTPServer
hudakz 1:698437bcd48d 38 int myPort = 69; // TFPTServer port
hudakz 1:698437bcd48d 39 Timer timer; // For sending debug messages to the remote client every two seconds
hudakz 1:698437bcd48d 40
hudakz 1:698437bcd48d 41 #define USE_DHCP
tuxic 0:5c9e0971532d 42
hudakz 1:698437bcd48d 43 /**
hudakz 1:698437bcd48d 44 * @brief
hudakz 1:698437bcd48d 45 * @note
hudakz 1:698437bcd48d 46 * @param
hudakz 1:698437bcd48d 47 * @retval
hudakz 1:698437bcd48d 48 */
hudakz 1:698437bcd48d 49 int main()
hudakz 1:698437bcd48d 50 {
hudakz 1:698437bcd48d 51 // Try to mount the filesystem. This should work without the Ethernet.
hudakz 1:698437bcd48d 52 pc.printf("Mounting the filesystem... \r\n");
hudakz 1:698437bcd48d 53
hudakz 1:698437bcd48d 54 int err = fs.mount(&bd);
hudakz 1:698437bcd48d 55 pc.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));
tuxic 0:5c9e0971532d 56
hudakz 1:698437bcd48d 57 if (err)
hudakz 1:698437bcd48d 58 return 1;
hudakz 1:698437bcd48d 59
hudakz 1:698437bcd48d 60 // Open a file. This should work without the Ethernet, too.
hudakz 1:698437bcd48d 61 pc.printf("Opening \"/fs/test.txt\"... \r\n");
hudakz 1:698437bcd48d 62
hudakz 1:698437bcd48d 63 FILE* fp = fopen("/fs/test.txt", "r+");
hudakz 1:698437bcd48d 64 pc.printf("%s\r\n", (!fp ? "Failed :(\r\n" : "OK\r\n"));
hudakz 1:698437bcd48d 65
hudakz 1:698437bcd48d 66 if (!fp)
hudakz 1:698437bcd48d 67 {
hudakz 3:77497b352c3f 68 // Create the test file, if it doesn't exist yet.
hudakz 1:698437bcd48d 69 pc.printf("No file found, creating a new file ...\r\n");
hudakz 3:77497b352c3f 70 fp = fopen("/fs/test.txt", "w+");
hudakz 3:77497b352c3f 71 pc.printf("%s\r\n", (!fp ? "Failed :(" : "OK"));
hudakz 1:698437bcd48d 72 if (!fp)
hudakz 1:698437bcd48d 73 error("error: %s (%d)\r\n", strerror(errno), -errno);
tuxic 0:5c9e0971532d 74
hudakz 1:698437bcd48d 75 for (int i = 0; i < 10; i++)
hudakz 1:698437bcd48d 76 {
hudakz 1:698437bcd48d 77 pc.printf("\rWriting numbers (%d/%d)... ", i, 10);
hudakz 1:698437bcd48d 78 err = fprintf(fp, " %d\n", i);
hudakz 1:698437bcd48d 79 if (err < 0)
hudakz 1:698437bcd48d 80 {
hudakz 1:698437bcd48d 81 pc.printf("Fail :(\r\n");
hudakz 1:698437bcd48d 82 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 1:698437bcd48d 83 }
hudakz 1:698437bcd48d 84 }
hudakz 1:698437bcd48d 85
hudakz 1:698437bcd48d 86 pc.printf("\rWriting numbers (%d/%d)... OK\r\n", 10, 10);
hudakz 1:698437bcd48d 87
hudakz 1:698437bcd48d 88 pc.printf("Seeking file ...\r\n");
hudakz 1:698437bcd48d 89 err = fseek(fp, 0, SEEK_SET);
hudakz 3:77497b352c3f 90 pc.printf("%s\r\n", (err < 0 ? "Failed :(" : "OK"));
hudakz 1:698437bcd48d 91 if (err < 0)
hudakz 1:698437bcd48d 92 error("error: %s (%d)\r\n", strerror(errno), -errno);
tuxic 0:5c9e0971532d 93 }
tuxic 0:5c9e0971532d 94
hudakz 1:698437bcd48d 95 // Connect to the Ethernet
hudakz 1:698437bcd48d 96 pc.printf("Connecting to the Ethernet using ");
hudakz 1:698437bcd48d 97 #ifdef USE_DHCP
hudakz 1:698437bcd48d 98 pc.printf("DHCP server ...\r\n");
hudakz 1:698437bcd48d 99 #else
hudakz 1:698437bcd48d 100 pc.printf("fixed IP ...\r\n");
hudakz 1:698437bcd48d 101 net.set_network("192.168.1.185", "255.255.255.0", "192.168.1.1");
hudakz 1:698437bcd48d 102 #endif
hudakz 1:698437bcd48d 103 net.connect();
hudakz 1:698437bcd48d 104 pc.printf("IP: %s\r\n", net.get_ip_address());
hudakz 1:698437bcd48d 105 pc.printf("NetMask: %s\r\n\r\n", net.get_netmask());
hudakz 1:698437bcd48d 106
hudakz 1:698437bcd48d 107 // Create a TFTP server.
hudakz 1:698437bcd48d 108 pc.printf("Creating a TFTP server ...\r\n\r\n");
hudakz 1:698437bcd48d 109 server = new TFTPServer(&net, myPort);
tuxic 0:5c9e0971532d 110
hudakz 1:698437bcd48d 111 int fileCount = 0; // incoming files
hudakz 1:698437bcd48d 112 char fileName[256]; // to display filenames
hudakz 1:698437bcd48d 113
hudakz 1:698437bcd48d 114 timer.start();
hudakz 1:698437bcd48d 115
hudakz 1:698437bcd48d 116 while (1)
hudakz 1:698437bcd48d 117 {
hudakz 1:698437bcd48d 118 server->poll();
hudakz 1:698437bcd48d 119
hudakz 1:698437bcd48d 120 if (pc.readable())
hudakz 1:698437bcd48d 121 {
hudakz 1:698437bcd48d 122 int c = pc.getc();
tuxic 0:5c9e0971532d 123 switch (c) {
hudakz 1:698437bcd48d 124 case 's':
hudakz 1:698437bcd48d 125 server->suspend();
tuxic 0:5c9e0971532d 126 break;
hudakz 1:698437bcd48d 127
tuxic 0:5c9e0971532d 128 case 'r':
hudakz 1:698437bcd48d 129 server->resume();
tuxic 0:5c9e0971532d 130 break;
tuxic 0:5c9e0971532d 131 }
tuxic 0:5c9e0971532d 132 }
hudakz 1:698437bcd48d 133
hudakz 1:698437bcd48d 134 if (server->fileCount() > fileCount)
hudakz 1:698437bcd48d 135 {
hudakz 1:698437bcd48d 136 fileCount = server->fileCount();
hudakz 1:698437bcd48d 137 server->getFileName(fileName);
hudakz 1:698437bcd48d 138 pc.printf("new file: %s\r\n", fileName);
tuxic 0:5c9e0971532d 139 }
hudakz 1:698437bcd48d 140
hudakz 1:698437bcd48d 141 if (timer.read() > 2)
hudakz 1:698437bcd48d 142 {
hudakz 1:698437bcd48d 143 timer.reset();
hudakz 1:698437bcd48d 144
hudakz 1:698437bcd48d 145 switch (server->getState()) {
hudakz 1:698437bcd48d 146 case TFTPServer::LISTENING:
hudakz 1:698437bcd48d 147 pc.printf("main: TFTP server is listening\r\n");
tuxic 0:5c9e0971532d 148 break;
hudakz 1:698437bcd48d 149
hudakz 1:698437bcd48d 150 case TFTPServer::READING:
hudakz 1:698437bcd48d 151 server->getFileName(fileName);
hudakz 1:698437bcd48d 152 pc.printf("main: TFTP server is reading file: %s\r\n", fileName);
tuxic 0:5c9e0971532d 153 break;
hudakz 1:698437bcd48d 154
hudakz 1:698437bcd48d 155 case TFTPServer::WRITING:
hudakz 1:698437bcd48d 156 server->getFileName(fileName);
hudakz 1:698437bcd48d 157 pc.printf("main: TFTP server is writing file: %s\r\n", fileName);
tuxic 0:5c9e0971532d 158 break;
hudakz 1:698437bcd48d 159
hudakz 1:698437bcd48d 160 case TFTPServer::ERROR:
hudakz 1:698437bcd48d 161 pc.printf("main: TFTP error\r\n");
tuxic 0:5c9e0971532d 162 break;
hudakz 1:698437bcd48d 163
hudakz 1:698437bcd48d 164 case TFTPServer::SUSPENDED:
hudakz 1:698437bcd48d 165 pc.printf("main: TFTP suspended\r\n");
tuxic 0:5c9e0971532d 166 break;
hudakz 1:698437bcd48d 167
tuxic 0:5c9e0971532d 168 default:
hudakz 1:698437bcd48d 169 pc.printf("main: Unknown TFTP status\r\n");
tuxic 0:5c9e0971532d 170 break;
tuxic 0:5c9e0971532d 171 }
tuxic 0:5c9e0971532d 172 }
tuxic 0:5c9e0971532d 173 }
tuxic 0:5c9e0971532d 174 }