This is the firmware for the LaOS - Laser Open Source project. You can use it to drive a laser cutter. For hardware and more information, look at our wiki: http://wiki.laoslaser.org

Dependencies:   EthernetNetIf mbed

Committer:
fablabtruck
Date:
Fri Jun 08 09:26:40 2012 +0000
Revision:
0:3852426a5068
svn revision 379

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fablabtruck 0:3852426a5068 1 /**
fablabtruck 0:3852426a5068 2 * TFTPServer.h
fablabtruck 0:3852426a5068 3 * Simple TFTP server
fablabtruck 0:3852426a5068 4 *
fablabtruck 0:3852426a5068 5 * Copyright (c) 2011 Jaap Vermaas
fablabtruck 0:3852426a5068 6 *
fablabtruck 0:3852426a5068 7 * This file is part of the LaOS project (see: http://wiki.laoslaser.org
fablabtruck 0:3852426a5068 8 *
fablabtruck 0:3852426a5068 9 * LaOS is free software: you can redistribute it and/or modify
fablabtruck 0:3852426a5068 10 * it under the terms of the GNU General Public License as published by
fablabtruck 0:3852426a5068 11 * the Free Software Foundation, either version 3 of the License, or
fablabtruck 0:3852426a5068 12 * (at your option) any later version.
fablabtruck 0:3852426a5068 13 *
fablabtruck 0:3852426a5068 14 * LaOS is distributed in the hope that it will be useful,
fablabtruck 0:3852426a5068 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
fablabtruck 0:3852426a5068 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fablabtruck 0:3852426a5068 17 * GNU General Public License for more details.
fablabtruck 0:3852426a5068 18 *
fablabtruck 0:3852426a5068 19 * You should have received a copy of the GNU General Public License
fablabtruck 0:3852426a5068 20 * along with LaOS. If not, see <http://www.gnu.org/licenses/>.
fablabtruck 0:3852426a5068 21 *
fablabtruck 0:3852426a5068 22 * Minimal TFTP Server
fablabtruck 0:3852426a5068 23 * * Receive and send files via TFTP
fablabtruck 0:3852426a5068 24 * * Server handles only one transfer at a time
fablabtruck 0:3852426a5068 25 * * Supports only binary mode transfers, no (net)ascii
fablabtruck 0:3852426a5068 26 * * fixed block size: 512 bytes
fablabtruck 0:3852426a5068 27 *
fablabtruck 0:3852426a5068 28 * http://spectral.mscs.mu.edu/RFC/rfc1350.html
fablabtruck 0:3852426a5068 29 *
fablabtruck 0:3852426a5068 30 * Example:
fablabtruck 0:3852426a5068 31 * @code
fablabtruck 0:3852426a5068 32 * TFTPServer *srv;
fablabtruck 0:3852426a5068 33 * ...
fablabtruck 0:3852426a5068 34 * srv = new TFTPServer();
fablabtruck 0:3852426a5068 35 * ...
fablabtruck 0:3852426a5068 36 * @endcode
fablabtruck 0:3852426a5068 37 *
fablabtruck 0:3852426a5068 38 */
fablabtruck 0:3852426a5068 39
fablabtruck 0:3852426a5068 40 #ifndef _TFTPSERVER_H_
fablabtruck 0:3852426a5068 41 #define _TFTPSERVER_H_
fablabtruck 0:3852426a5068 42
fablabtruck 0:3852426a5068 43 #include <stdio.h>
fablabtruck 0:3852426a5068 44 #include <ctype.h>
fablabtruck 0:3852426a5068 45 #include "mbed.h"
fablabtruck 0:3852426a5068 46 #include "laosfilesystem.h"
fablabtruck 0:3852426a5068 47 #include "UDPSocket.h" // http://mbed.org/users/donatien/programs/EthernetNetIf
fablabtruck 0:3852426a5068 48
fablabtruck 0:3852426a5068 49 #define TFTP_PORT 69
fablabtruck 0:3852426a5068 50 //#define TFTP_DEBUG(x) printf("%s\n\r", x);
fablabtruck 0:3852426a5068 51
fablabtruck 0:3852426a5068 52 enum TFTPServerState { listen, reading, writing, error, suspended, deleted };
fablabtruck 0:3852426a5068 53
fablabtruck 0:3852426a5068 54 class TFTPServer {
fablabtruck 0:3852426a5068 55
fablabtruck 0:3852426a5068 56 public:
fablabtruck 0:3852426a5068 57 // create a new tftp server, with file directory dir and
fablabtruck 0:3852426a5068 58 // listening on port
fablabtruck 0:3852426a5068 59 TFTPServer(char* dir, int myport = TFTP_PORT);
fablabtruck 0:3852426a5068 60 // destroy this instance of the tftp server
fablabtruck 0:3852426a5068 61 void reset();
fablabtruck 0:3852426a5068 62 // reset socket
fablabtruck 0:3852426a5068 63 ~TFTPServer();
fablabtruck 0:3852426a5068 64 // get current tftp status
fablabtruck 0:3852426a5068 65 TFTPServerState State();
fablabtruck 0:3852426a5068 66 // Temporarily disable incoming TFTP connections
fablabtruck 0:3852426a5068 67 void suspend();
fablabtruck 0:3852426a5068 68 // Resume after suspension
fablabtruck 0:3852426a5068 69 void resume();
fablabtruck 0:3852426a5068 70 // During read and write, this gives you the filename
fablabtruck 0:3852426a5068 71 void getFilename(char* name);
fablabtruck 0:3852426a5068 72 // Return number of received files
fablabtruck 0:3852426a5068 73 int fileCnt();
fablabtruck 0:3852426a5068 74
fablabtruck 0:3852426a5068 75 private:
fablabtruck 0:3852426a5068 76 // create a new connection reading a file from server
fablabtruck 0:3852426a5068 77 void ConnectRead(char* infile, Host* client);
fablabtruck 0:3852426a5068 78 // create a new connection writing a file to the server
fablabtruck 0:3852426a5068 79 void ConnectWrite(char* infile, Host* client);
fablabtruck 0:3852426a5068 80 // get DATA block from file on disk into memory
fablabtruck 0:3852426a5068 81 void getBlock();
fablabtruck 0:3852426a5068 82 // send DATA block to the client
fablabtruck 0:3852426a5068 83 void sendBlock();
fablabtruck 0:3852426a5068 84 // handle special files
fablabtruck 0:3852426a5068 85 // anything called *.bin is written to /local/FIRMWARE.BIN
fablabtruck 0:3852426a5068 86 // anything called config.txt is written to /local/config.txt
fablabtruck 0:3852426a5068 87 // even if workdir is not /local
fablabtruck 0:3852426a5068 88 int cmpHost(Host* client);
fablabtruck 0:3852426a5068 89 // send ACK to remote
fablabtruck 0:3852426a5068 90 void Ack(int val);
fablabtruck 0:3852426a5068 91 // send ERR message to named client
fablabtruck 0:3852426a5068 92 void Err(char* msg, Host* client);
fablabtruck 0:3852426a5068 93 // check if connection mode of client is octet/binary
fablabtruck 0:3852426a5068 94 int modeOctet(char* buff);
fablabtruck 0:3852426a5068 95 // timed routine to avoid hanging after interrupted transfers
fablabtruck 0:3852426a5068 96 void cleanUp();
fablabtruck 0:3852426a5068 97 // event driven routines to handle incoming packets
fablabtruck 0:3852426a5068 98 void onListenUDPSocketEvent(UDPSocketEvent e);
fablabtruck 0:3852426a5068 99 int port; // The TFTP port
fablabtruck 0:3852426a5068 100 UDPSocket* ListenSock; // main listening socket (dflt: UDP port 69)
fablabtruck 0:3852426a5068 101 char workdir[256]; // file working directory
fablabtruck 0:3852426a5068 102 TFTPServerState state; // current TFTP server state
fablabtruck 0:3852426a5068 103 Host* remote; // connected remote Host IP and Port
fablabtruck 0:3852426a5068 104 int blockcnt, dupcnt; // block counter, and DUP counter
fablabtruck 0:3852426a5068 105 FILE* fp; // current file to read or write
fablabtruck 0:3852426a5068 106 char sendbuff[516]; // current DATA block;
fablabtruck 0:3852426a5068 107 int blocksize; // last DATA block size while sending
fablabtruck 0:3852426a5068 108 char filename[256]; // current (or most recent) filename
fablabtruck 0:3852426a5068 109 //Ticker TFTPServerTimer; // timeout timer
fablabtruck 0:3852426a5068 110 int filecnt; // received file counter
fablabtruck 0:3852426a5068 111 };
fablabtruck 0:3852426a5068 112
fablabtruck 0:3852426a5068 113 #endif