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
main.cpp
- Committer:
- tuxic
- Date:
- 2011-12-27
- Revision:
- 0:5c9e0971532d
- Child:
- 1:698437bcd48d
File content as of revision 0:5c9e0971532d:
/* * example program for TFTPServer class * * How to use: * -> 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 * * Copyright (c) 2011 Jaap Vermaas * * 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 * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * LaOS is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * 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 "TFTPServer.h" #include "EthernetNetIf.h" LocalFileSystem local("local"); // defining this makes MBED internal mem accessible 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 int main() { // configure serial terminal serial = new Serial(USBTX, USBRX); serial->baud(115200); // 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); } 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/"); t.start(); int filecounter = 0; // incoming files char filename[256]; // to display filenames while (1) { Net::poll(); if (serial->readable()) { int c = serial->getc(); switch (c) { case 's': srv->suspend(); break; case 'r': srv->resume(); break; } } if (srv->fileCnt() > filecounter) { filecounter = srv->fileCnt(); srv->getFilename(filename); printf("new file: %s\n\r", filename); } if (t.read() > 2) { t.reset(); TFTPServerState state = srv->State(); switch(state) { case listen: printf("MAIN: TFTP listen\n\r"); break; case reading: srv->getFilename(filename); printf("MAIN: TFTP reading file: %s\n\r", filename); break; case writing: srv->getFilename(filename); printf("MAIN: TFTP writing file: %s\n\r", filename); break; case error: printf("MAIN: TFTP error\n\r"); break; case suspended: printf("MAIN: TFTP suspended\n\r"); break; default: printf("MAIN: Unknown TFTP status\n\r"); break; } } } }