Zoltan Hudak / TFTPServer

Dependents:   TFTPServerTest

Fork of TFTPServer by Jaap Vermaas

Revision:
1:9c973065a97e
Parent:
0:3b0027b76acf
Child:
2:f7c0fbc8c5aa
--- a/TFTPServer.h	Mon Dec 26 18:18:32 2011 +0000
+++ b/TFTPServer.h	Fri Mar 16 19:22:10 2018 +0000
@@ -1,8 +1,9 @@
-/**
+/*
  * TFTPServer.h
  * Simple TFTP server 
  *
  * 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
  *
@@ -36,78 +37,94 @@
  * @endcode
  *
  */
-
 #ifndef _TFTPSERVER_H_
 #define _TFTPSERVER_H_
 
 #include <stdio.h>
 #include <ctype.h>
 #include "mbed.h"
-#include "SDFileSystem.h"   // http://mbed.org/users/NeoBelerophon/libraries/ChaNFSSD
-#include "UDPSocket.h"      // http://mbed.org/users/donatien/programs/EthernetNetIf
+
+class EthernetInterface;
 
-#define TFTP_PORT 69
-//#define TFTP_DEBUG(x) printf("%s\n\r", x);
+#define TFTP_PORT   69
 
-enum TFTPServerState { listen, reading, writing, error, suspended, deleted }; 
-
-class TFTPServer {
-
+class TFTPServer
+{
 public:
-    // create a new tftp server, with file directory dir and 
-    // listening on port
-    TFTPServer(char* dir);
-    // destroy this instance of the tftp server
-    void reset();
-    // reset socket
+    enum State
+    {
+        LISTENING,
+        READING,
+        WRITING,
+        ERROR,
+        SUSPENDED,
+        DELETED
+    };
+
+    // Creates a new TFTP server listening on myPort.
+    TFTPServer(EthernetInterface* net, uint16_t myPort = TFTP_PORT);
+    
+    // Destroys this instance of the TFTP server.
     ~TFTPServer();
-    // get current tftp status
-    TFTPServerState State();
-    // Temporarily disable incoming TFTP connections
-    void suspend();
-    // Resume after suspension
-    void resume();
-    // During read and write, this gives you the filename
-    void getFilename(char* name);
-    // Return number of received files
-    int fileCnt();
-
+    
+    // Resets the TFTP server.
+    void            reset();
+    
+    // Gets current TFTP status
+    State           getState();
+    
+    // Temporarily disables incoming TFTP connections.
+    void            suspend();
+    
+    // Resumes incoming TFTP connections after suspension.
+    void            resume();
+    
+    // Polls for data or new connection.
+    void            poll();
+    
+    // Gets the filename during read and write. 
+    void            getFileName(char* name);
+    
+    // Returns number of received files.
+    int             fileCount();
+    
 private:
-    // create a new connection reading a file from server
-    void ConnectRead(char* infile, Host* client);
-    // create a new connection writing a file to the server
-    void ConnectWrite(char* infile, Host* client);
-    // get DATA block from file on disk into memory
-    void getBlock();
-    // send DATA block to the client
-    void sendBlock();
-    // handle special files
-    // anything called *.bin is written to /local/FIRMWARE.BIN
-    // anything called config.txt is written to /local/config.txt
-    // even if workdir is not /local
-    int cmpHost(Host* client);
-    // send ACK to remote
-    void Ack(int val);
-    // send ERR message to named client
-    void Err(char* msg, Host* client);
-    // check if connection mode of client is octet/binary
-    int modeOctet(char* buff);
-    // timed routine to avoid hanging after interrupted transfers
-    void cleanUp();
-    // event driven routines to handle incoming packets
-    void onListenUDPSocketEvent(UDPSocketEvent e);
+    // Creates a new connection reading a file from server.
+    void            connectRead(char* buff);
+    
+    // Creates a new connection writing a file to the server.
+    void            connectWrite(char* buff);
+    
+    // Gets DATA block from file on disk into memory.
+    void            getBlock();
+    
+    // Sends DATA block to remote client.
+    void            sendBlock();
+    
+    // Compares host's IP and Port with connected remote machine.
+    int             cmpHost();
+    
+    // Sends ACK to remote client.
+    void            ack(int val);
     
-    UDPSocket* ListenSock;      // main listening socket (dflt: UDP port 69)
-    char workdir[256];          // file working directory
-    TFTPServerState state;      // current TFTP server state
-    Host* remote;               // connected remote Host IP and Port
-    int blockcnt, dupcnt;       // block counter, and DUP counter
-    FILE* fp;                   // current file to read or write
-    char sendbuff[516];         // current DATA block;
-    int blocksize;              // last DATA block size while sending
-    char filename[256];         // current (or most recent) filename
-    Ticker TFTPServerTimer;     // timeout timer
-    int filecnt;                // received file counter
+    // Sends ERROR message to remote client.
+    void            sendError(const char* msg);
+    
+    // Checks if connection mode of client is octet/binary.
+    int             modeOctet(char* buff);
+    
+    uint16_t        port;                       // TFTP port
+    UDPSocket*      socket;                     // Main listening socket (dflt: UDP port 69)
+    State           state;                      // Current TFTP server state
+    char*           remoteIP;                   // Connected remote Host IP
+    int             remotePort;                 // Connected remote Host Port
+    uint16_t        blockCounter, dupCounter;   // Block counter, and DUP counter
+    FILE*           file;                       // Current file to read or write
+    char            blockBuff[516];             // Current DATA block;
+    int             blockSize;                  // Last DATA block size while sending
+    char            fileName[256];              // Current (or most recent) filename
+    int             fileCounter;                // Received file counter
+    char            errorBuff[128];             // Error message buffer
+    SocketAddress   socketAddr;                 // Socket's addres (used to get remote host's address)
 };
-
 #endif