ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
FtpServer.h
Go to the documentation of this file.
1 /*
2  * FTP Serveur for Arduino Due or Mega 2580
3  * and Ethernet shield W5100, W5200 or W5500
4  * or for Esp8266 with external SD card or SpiFfs
5  * Copyright (c) 2014-2018 by Jean-Michel Gallego
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 /*
21 * 2019-10-27 Modified version for ESP3D by Luc LEBOSSE @luc-github
22 * support for ESP8266 and ESP32 in ESP3D project
23 */
24 
25 /*******************************************************************************
26  ** **
27  ** DEFINITIONS FOR FTP SERVER **
28  ** **
29  *******************************************************************************/
30 
31 #ifndef FTP_SERVER_H
32 #define FTP_SERVER_H
33 
34 class WiFiServer;
35 class WiFiClient;
36 #ifndef FF_MAX_LFN
37 #define FF_MAX_LFN 255
38 #endif
39 #define FTP_TIME_OUT 5 * 60 // Disconnect client after 5 minutes of inactivity
40 #define FTP_AUTH_TIME_OUT 10 // Wait for authentication for 10 seconds
41 #define FTP_CMD_SIZE FF_MAX_LFN+8 // max size of a command
42 #define FTP_CWD_SIZE FF_MAX_LFN+8 // max size of a directory name
43 #define FTP_FIL_SIZE FF_MAX_LFN // max size of a file name
44 #define FTP_BUF_SIZE 1024 // 512 // size of file buffer for read/write
45 
46 #define FTP_SERVER WiFiServer
47 #define FTP_CLIENT WiFiClient
48 #define CommandIs( a ) (command != NULL && ! strcmp_P( command, PSTR( a )))
49 #define ParameterIs( a ) ( parameter != NULL && ! strcmp_P( parameter, PSTR( a )))
50 #include <time.h>
51 
52 enum ftpCmd { FTP_Stop = 0, // In this stage, stop any connection
53  FTP_Init, // initialize some variables
54  FTP_Client, // wait for client connection
55  FTP_User, // wait for user name
56  FTP_Pass, // wait for user password
58  }; // answers to commands
59 
60 enum ftpTransfer { FTP_Close = 0, // In this stage, close data channel
61  FTP_Retrieve, // retrieve file
62  FTP_Store, // store file
63  FTP_List, // list of files
64  FTP_Nlst, // list of name of files
66  }; // listing for machine processing
67 
68 enum ftpDataConn { FTP_NoConn = 0,// No data connexion
69  FTP_Pasive, // Pasive type
71  }; // Active type
72 
73 class FtpServer
74 {
75 public:
76  FtpServer();
77  ~FtpServer();
78  bool begin();
79  void handle();
80  void end();
81  bool started();
82  uint16_t ctrlport()
83  {
84  return ctrlPort;
85  }
86  uint16_t datapassiveport()
87  {
88  return passivePort;
89  }
90  uint16_t dataactiveport()
91  {
92  return activePort;
93  }
94  void closeClient();
95  bool isConnected();
96  const char* clientIPAddress();
97  bool isUser(const char * user);
98  bool isPassword(const char * password);
99 private:
100  void iniVariables();
101  void clientConnected();
102  void disconnectClient();
103  bool processCommand();
104  bool haveParameter();
105  int dataConnect( bool out150 = true );
106  bool dataConnected();
107  bool doRetrieve();
108  bool doStore();
109  bool doList();
110  bool doMlsd();
111  void closeTransfer();
112  void abortTransfer();
113  bool makePath( char * fullName, char * param = NULL );
114  bool makeExistsPath( char * path, char * param = NULL );
115  char * makeDateTimeStr( char * tstr, time_t timefile );
116  char * makeDateTimeString( char * tstr, time_t timefile );
117  uint8_t getDateTime( char * dt, uint16_t * pyear, uint8_t * pmonth, uint8_t * pday,
118  uint8_t * phour, uint8_t * pminute, uint8_t * second );
119 
120  bool getFileModTime(const char * path,time_t & time);
121  bool timeStamp( const char * path, uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second );
122  int8_t readChar();
123 
124  FTP_SERVER * ftpServer;
125  FTP_SERVER * dataServer;
126  uint16_t ctrlPort; // Command port on wich server is listening
127  uint16_t activePort; // Default data port in active mode
128  uint16_t passivePort; // Data port in passive mode
129  bool _started;
130  uint8_t _root;
131  IPAddress dataIp; // IP address of client for data
132  FTP_CLIENT client;
133  FTP_CLIENT data;
134 
135  ftpCmd cmdStage; // stage of ftp command connexion
136  ftpTransfer transferStage; // stage of data connexion
137  ftpDataConn dataConn; // type of data connexion
138 
139  // uint8_t __attribute__((packed, aligned(4))) // need to be aligned to 32bit for Esp8266 SPIClass::transferBytes()
140  uint8_t buf[ FTP_BUF_SIZE ]; // data buffer for transfers
141  char cmdLine[ FTP_CMD_SIZE ]; // where to store incoming char from client
142  char cwdName[ FTP_CWD_SIZE ]; // name of current directory
143  char rnfrName[ FTP_CWD_SIZE ]; // name of file for RNFR command
144  char command[ 5 ]; // command sent by client
145  bool rnfrCmd; // previous command was RNFR
146  char * parameter; // point to begin of parameters sent by client
147  uint16_t dataPort;
148  uint16_t iCL; // pointer to cmdLine next incoming char
149  uint16_t nbMatch;
150 
151  uint32_t millisDelay, //
152  millisEndConnection, //
153  millisBeginTrans, // store time of beginning of a transaction
154  bytesTransfered; //
155  String _currentUser;
156 };
157 
158 extern FtpServer ftp_server;
159 
160 #endif // FTP_SERVER_H
FTP_Pasive
@ FTP_Pasive
Definition: FtpServer.h:69
FTP_CLIENT
#define FTP_CLIENT
Definition: FtpServer.h:47
FTP_SERVER
#define FTP_SERVER
Definition: FtpServer.h:46
FtpServer::handle
void handle()
Definition: FtpServer.cpp:219
ftpCmd
ftpCmd
Definition: FtpServer.h:52
FTP_Nlst
@ FTP_Nlst
Definition: FtpServer.h:64
FtpServer::datapassiveport
uint16_t datapassiveport()
Definition: FtpServer.h:86
FtpServer::started
bool started()
Definition: FtpServer.cpp:170
FTP_Client
@ FTP_Client
Definition: FtpServer.h:54
FtpServer::closeClient
void closeClient()
Definition: FtpServer.cpp:118
FTP_Store
@ FTP_Store
Definition: FtpServer.h:62
FTP_BUF_SIZE
#define FTP_BUF_SIZE
Definition: FtpServer.h:44
FtpServer::~FtpServer
~FtpServer()
Definition: FtpServer.cpp:138
FTP_Close
@ FTP_Close
Definition: FtpServer.h:60
ftpTransfer
ftpTransfer
Definition: FtpServer.h:60
FTP_Cmd
@ FTP_Cmd
Definition: FtpServer.h:57
FtpServer::isUser
bool isUser(const char *user)
Definition: FtpServer.cpp:309
FtpServer::FtpServer
FtpServer()
Definition: FtpServer.cpp:128
FtpServer::begin
bool begin()
Definition: FtpServer.cpp:175
FTP_Stop
@ FTP_Stop
Definition: FtpServer.h:52
FtpServer::dataactiveport
uint16_t dataactiveport()
Definition: FtpServer.h:90
FtpServer::ctrlport
uint16_t ctrlport()
Definition: FtpServer.h:82
FtpServer::isConnected
bool isConnected()
Definition: FtpServer.cpp:123
FTP_Active
@ FTP_Active
Definition: FtpServer.h:70
FTP_CWD_SIZE
#define FTP_CWD_SIZE
Definition: FtpServer.h:42
FtpServer
Definition: FtpServer.h:73
FTP_CMD_SIZE
#define FTP_CMD_SIZE
Definition: FtpServer.h:41
FTP_Pass
@ FTP_Pass
Definition: FtpServer.h:56
FTP_List
@ FTP_List
Definition: FtpServer.h:63
FTP_NoConn
@ FTP_NoConn
Definition: FtpServer.h:68
ftp_server
FtpServer ftp_server
Definition: FtpServer.cpp:106
FtpServer::end
void end()
Definition: FtpServer.cpp:143
FTP_Retrieve
@ FTP_Retrieve
Definition: FtpServer.h:61
FTP_User
@ FTP_User
Definition: FtpServer.h:55
FtpServer::isPassword
bool isPassword(const char *password)
Definition: FtpServer.cpp:326
FtpServer::clientIPAddress
const char * clientIPAddress()
Definition: FtpServer.cpp:160
FTP_Init
@ FTP_Init
Definition: FtpServer.h:53
FTP_Mlsd
@ FTP_Mlsd
Definition: FtpServer.h:65
ftpDataConn
ftpDataConn
Definition: FtpServer.h:68