Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TFTPServerTest by
main.cpp@0:5c9e0971532d, 2011-12-27 (annotated)
- Committer:
- tuxic
- Date:
- Tue Dec 27 10:41:28 2011 +0000
- Revision:
- 0:5c9e0971532d
- Child:
- 1:698437bcd48d
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tuxic | 0:5c9e0971532d | 1 | /* |
| tuxic | 0:5c9e0971532d | 2 | * example program for TFTPServer class |
| tuxic | 0:5c9e0971532d | 3 | * |
| tuxic | 0:5c9e0971532d | 4 | * How to use: |
| tuxic | 0:5c9e0971532d | 5 | * -> change the IP to something that works for you |
| tuxic | 0:5c9e0971532d | 6 | * -> connect to the mbed with a TFTP client in binary(octet) mode |
| tuxic | 0:5c9e0971532d | 7 | * -> use (s)uspend and (r)esume keys to turn TFTP server on/off |
| tuxic | 0:5c9e0971532d | 8 | * -> upload any file, but .bin files are always put on the internal |
| tuxic | 0:5c9e0971532d | 9 | * memory, as are config.txt files |
| tuxic | 0:5c9e0971532d | 10 | * |
| tuxic | 0:5c9e0971532d | 11 | * Copyright (c) 2011 Jaap Vermaas |
| tuxic | 0:5c9e0971532d | 12 | * |
| tuxic | 0:5c9e0971532d | 13 | * This file is part of the LaOS project (see: http://wiki.laoslaser.org |
| tuxic | 0:5c9e0971532d | 14 | * |
| tuxic | 0:5c9e0971532d | 15 | * LaOS is free software: you can redistribute it and/or modify |
| tuxic | 0:5c9e0971532d | 16 | * it under the terms of the GNU General Public License as published by |
| tuxic | 0:5c9e0971532d | 17 | * the Free Software Foundation, either version 3 of the License, or |
| tuxic | 0:5c9e0971532d | 18 | * (at your option) any later version. |
| tuxic | 0:5c9e0971532d | 19 | * |
| tuxic | 0:5c9e0971532d | 20 | * LaOS is distributed in the hope that it will be useful, |
| tuxic | 0:5c9e0971532d | 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| tuxic | 0:5c9e0971532d | 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| tuxic | 0:5c9e0971532d | 23 | * GNU General Public License for more details. |
| tuxic | 0:5c9e0971532d | 24 | * |
| tuxic | 0:5c9e0971532d | 25 | * You should have received a copy of the GNU General Public License |
| tuxic | 0:5c9e0971532d | 26 | * along with LaOS. If not, see <http://www.gnu.org/licenses/>. |
| tuxic | 0:5c9e0971532d | 27 | */ |
| tuxic | 0:5c9e0971532d | 28 | #include "mbed.h" |
| tuxic | 0:5c9e0971532d | 29 | #include "SDFileSystem.h" |
| tuxic | 0:5c9e0971532d | 30 | #include "TFTPServer.h" |
| tuxic | 0:5c9e0971532d | 31 | #include "EthernetNetIf.h" |
| tuxic | 0:5c9e0971532d | 32 | LocalFileSystem local("local"); // defining this makes MBED internal mem accessible |
| tuxic | 0:5c9e0971532d | 33 | |
| tuxic | 0:5c9e0971532d | 34 | TFTPServer *srv; // define the TFTP Server |
| tuxic | 0:5c9e0971532d | 35 | Timer t; // used in main() to send debug output every 2 seconds |
| tuxic | 0:5c9e0971532d | 36 | Serial *serial; // serial just for debugging |
| tuxic | 0:5c9e0971532d | 37 | SDFileSystem sd(p11, p12, p13, p14, "sd"); // only needed if you save to SD card |
| tuxic | 0:5c9e0971532d | 38 | EthernetNetIf *eth; // network device |
| tuxic | 0:5c9e0971532d | 39 | |
| tuxic | 0:5c9e0971532d | 40 | int main() { |
| tuxic | 0:5c9e0971532d | 41 | |
| tuxic | 0:5c9e0971532d | 42 | // configure serial terminal |
| tuxic | 0:5c9e0971532d | 43 | serial = new Serial(USBTX, USBRX); |
| tuxic | 0:5c9e0971532d | 44 | serial->baud(115200); |
| tuxic | 0:5c9e0971532d | 45 | |
| tuxic | 0:5c9e0971532d | 46 | // set up networking |
| tuxic | 0:5c9e0971532d | 47 | eth = new EthernetNetIf( |
| tuxic | 0:5c9e0971532d | 48 | IpAddr(192,168,1,111), //IP Address |
| tuxic | 0:5c9e0971532d | 49 | IpAddr(255,255,255,0), //Network Mask |
| tuxic | 0:5c9e0971532d | 50 | IpAddr(192,168,1,1), //Gateway |
| tuxic | 0:5c9e0971532d | 51 | IpAddr(192,168,1,1) //DNS |
| tuxic | 0:5c9e0971532d | 52 | ); |
| tuxic | 0:5c9e0971532d | 53 | eth->setup(); |
| tuxic | 0:5c9e0971532d | 54 | |
| tuxic | 0:5c9e0971532d | 55 | // test SD card (TFTP server can work without it) |
| tuxic | 0:5c9e0971532d | 56 | printf("TEST SD...\n\r"); |
| tuxic | 0:5c9e0971532d | 57 | FILE *fp = fopen("/sd/test.txt", "wb"); |
| tuxic | 0:5c9e0971532d | 58 | if ( fp == NULL ) |
| tuxic | 0:5c9e0971532d | 59 | printf("SD: NOT READY\n\r"); |
| tuxic | 0:5c9e0971532d | 60 | else { |
| tuxic | 0:5c9e0971532d | 61 | printf("SD: READY...\n\r"); |
| tuxic | 0:5c9e0971532d | 62 | fclose(fp); |
| tuxic | 0:5c9e0971532d | 63 | } |
| tuxic | 0:5c9e0971532d | 64 | remove("/sd/test.txt"); |
| tuxic | 0:5c9e0971532d | 65 | |
| tuxic | 0:5c9e0971532d | 66 | // start tftp server with the work dir as a comment |
| tuxic | 0:5c9e0971532d | 67 | // use "/local/" for the internal mbed device |
| tuxic | 0:5c9e0971532d | 68 | srv = new TFTPServer("/sd/"); |
| tuxic | 0:5c9e0971532d | 69 | |
| tuxic | 0:5c9e0971532d | 70 | t.start(); |
| tuxic | 0:5c9e0971532d | 71 | int filecounter = 0; // incoming files |
| tuxic | 0:5c9e0971532d | 72 | char filename[256]; // to display filenames |
| tuxic | 0:5c9e0971532d | 73 | while (1) { |
| tuxic | 0:5c9e0971532d | 74 | Net::poll(); |
| tuxic | 0:5c9e0971532d | 75 | if (serial->readable()) { |
| tuxic | 0:5c9e0971532d | 76 | int c = serial->getc(); |
| tuxic | 0:5c9e0971532d | 77 | switch (c) { |
| tuxic | 0:5c9e0971532d | 78 | case 's': |
| tuxic | 0:5c9e0971532d | 79 | srv->suspend(); |
| tuxic | 0:5c9e0971532d | 80 | break; |
| tuxic | 0:5c9e0971532d | 81 | case 'r': |
| tuxic | 0:5c9e0971532d | 82 | srv->resume(); |
| tuxic | 0:5c9e0971532d | 83 | break; |
| tuxic | 0:5c9e0971532d | 84 | } |
| tuxic | 0:5c9e0971532d | 85 | } |
| tuxic | 0:5c9e0971532d | 86 | |
| tuxic | 0:5c9e0971532d | 87 | if (srv->fileCnt() > filecounter) { |
| tuxic | 0:5c9e0971532d | 88 | filecounter = srv->fileCnt(); |
| tuxic | 0:5c9e0971532d | 89 | srv->getFilename(filename); |
| tuxic | 0:5c9e0971532d | 90 | printf("new file: %s\n\r", filename); |
| tuxic | 0:5c9e0971532d | 91 | } |
| tuxic | 0:5c9e0971532d | 92 | if (t.read() > 2) { |
| tuxic | 0:5c9e0971532d | 93 | t.reset(); |
| tuxic | 0:5c9e0971532d | 94 | TFTPServerState state = srv->State(); |
| tuxic | 0:5c9e0971532d | 95 | switch(state) { |
| tuxic | 0:5c9e0971532d | 96 | case listen: |
| tuxic | 0:5c9e0971532d | 97 | printf("MAIN: TFTP listen\n\r"); |
| tuxic | 0:5c9e0971532d | 98 | break; |
| tuxic | 0:5c9e0971532d | 99 | case reading: |
| tuxic | 0:5c9e0971532d | 100 | srv->getFilename(filename); |
| tuxic | 0:5c9e0971532d | 101 | printf("MAIN: TFTP reading file: %s\n\r", filename); |
| tuxic | 0:5c9e0971532d | 102 | break; |
| tuxic | 0:5c9e0971532d | 103 | case writing: |
| tuxic | 0:5c9e0971532d | 104 | srv->getFilename(filename); |
| tuxic | 0:5c9e0971532d | 105 | printf("MAIN: TFTP writing file: %s\n\r", filename); |
| tuxic | 0:5c9e0971532d | 106 | break; |
| tuxic | 0:5c9e0971532d | 107 | case error: |
| tuxic | 0:5c9e0971532d | 108 | printf("MAIN: TFTP error\n\r"); |
| tuxic | 0:5c9e0971532d | 109 | break; |
| tuxic | 0:5c9e0971532d | 110 | case suspended: |
| tuxic | 0:5c9e0971532d | 111 | printf("MAIN: TFTP suspended\n\r"); |
| tuxic | 0:5c9e0971532d | 112 | break; |
| tuxic | 0:5c9e0971532d | 113 | default: |
| tuxic | 0:5c9e0971532d | 114 | printf("MAIN: Unknown TFTP status\n\r"); |
| tuxic | 0:5c9e0971532d | 115 | break; |
| tuxic | 0:5c9e0971532d | 116 | } |
| tuxic | 0:5c9e0971532d | 117 | } |
| tuxic | 0:5c9e0971532d | 118 | } |
| tuxic | 0:5c9e0971532d | 119 | } |
| tuxic | 0:5c9e0971532d | 120 |
