diff --git a/FtpServer.cpp b/FtpServer.cpp index b7b8b27..e3390ef 100644 --- a/FtpServer.cpp +++ b/FtpServer.cpp @@ -100,6 +100,22 @@ void FtpServer::begin( const char * _user, const char * _pass, const char * _wel iniVariables(); } +void FtpServer::end() +{ + if(client.connected()) { + disconnectClient(); + } + + ftpServer.end(); + dataServer.end(); + DEBUG_PRINTLN(F("Stop server!")); + + cmdStage = FTP_Init; +} +void FtpServer::setLocalIp(IPAddress localIp) +{ + this->localIp = localIp; +} void FtpServer::credentials( const char * _user, const char * _pass ) { if( strlen( _user ) > 0 && strlen( _user ) < FTP_CRED_SIZE ) @@ -432,8 +448,8 @@ bool FtpServer::processCommand() { data.stop(); dataServer.begin(); - if((((uint32_t) NET_CLASS.localIP()) & ((uint32_t) NET_CLASS.subnetMask())) == - (((uint32_t) client.remoteIP()) & ((uint32_t) NET_CLASS.subnetMask()))) { + if (((((uint32_t) NET_CLASS.localIP()) & ((uint32_t) NET_CLASS.subnetMask())) == + (((uint32_t) client.remoteIP()) & ((uint32_t) NET_CLASS.subnetMask()))) && (uint32_t)localIp <= 0) { dataIp = NET_CLASS.localIP(); } else { dataIp = localIp; diff --git a/FtpServer.h b/FtpServer.h index 8054bf2..f0d4da6 100644 --- a/FtpServer.h +++ b/FtpServer.h @@ -260,8 +260,14 @@ #define FTP_FILE_WRITE_APPEND "a+" #define FTP_FILE_WRITE_CREATE "w+" #else +#if ESP_ARDUINO_VERSION_MAJOR >= 2 + #include "FS.h" + #include "LittleFS.h" + #define STORAGE_MANAGER LittleFS +#else #include "LITTLEFS.h" #define STORAGE_MANAGER LITTLEFS +#endif #define FTP_FILE File #define FTP_DIR File @@ -453,6 +459,8 @@ class FtpServer FtpServer( uint16_t _cmdPort = FTP_CMD_PORT, uint16_t _pasvPort = FTP_DATA_PORT_PASV ); void begin( const char * _user = FTP_USER, const char * _pass = FTP_PASS, const char * welcomeMessage = "Welcome to Simply FTP server" ); + void end(); + void setLocalIp(IPAddress localIp); void credentials( const char * _user, const char * _pass ); uint8_t handleFTP(); diff --git a/FtpServerKey.h b/FtpServerKey.h index fef5076..660be38 100644 --- a/FtpServerKey.h +++ b/FtpServerKey.h @@ -18,7 +18,7 @@ #define FTP_SERVER_CONFIG_H // Uncomment to enable printing out nice debug messages. -//#define FTP_SERVER_DEBUG +// #define FTP_SERVER_DEBUG // Define where debug output will be printed. #define DEBUG_PRINTER Serial @@ -35,6 +35,7 @@ #define NETWORK_ESP8266_ASYNC (0) #define NETWORK_ESP8266 (1) +// && (defined(ESP8266) && ARDUINO_ESP8266_MAJOR<3) #define NETWORK_ESP8266_242 (6) #define NETWORK_W5100 (2) #define NETWORK_ENC28J60 (3) @@ -49,7 +50,7 @@ #endif #ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_ESP32 #define DEFAULT_FTP_SERVER_NETWORK_TYPE_ESP32 NETWORK_ESP32 - #define DEFAULT_STORAGE_TYPE_ESP32 STORAGE_SPIFFS + #define DEFAULT_STORAGE_TYPE_ESP32 STORAGE_LITTLEFS #endif #ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_ARDUINO #define DEFAULT_FTP_SERVER_NETWORK_TYPE_ARDUINO NETWORK_W5100 diff --git a/README.md b/README.md index 1a8130c..6e3ea5f 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,15 @@ [Instruction on FTP server on esp8266 and esp32](https://www.mischianti.org/2020/02/08/ftp-server-on-esp8266-and-esp32) [Simple FTP Server library now with support for Wio Terminal and SD](https://www.mischianti.org/2021/07/01/simple-ftp-server-library-now-with-support-for-wio-terminal-and-sd/) -Simple FTP Server for +#### Simple FTP Server for - esp8266 (Flash: SPIFFs, LittleFS. SD: SD, SdFat 2) - - esp32 (SPIFFS, LITTLEFS, FFAT, SdFat) - - Arduino (SD with 8.3 file format, SdFat 2) + - esp32 (SPIFFS, LITTLEFS, FFAT, SD: SD, SdFat) + - Arduino (SD with 8.3 file format, SD: SD, SdFat 2) - Wio Terminal (SdFat 2, and native FAT) +#### Changelog +- 2022-02-01 Add workaround to start FTP server before connection, add end and setLocalIP method. +
When I develop a new solution I'd like to divide the application in layer, and so I'd like focus my attention in only one aspect at time.
diff --git a/examples/FTPServer_esp8266_esp32_LittleFS/FTPServer_esp8266_esp32_LittleFS.ino b/examples/FTPServer_esp8266_esp32_LittleFS/FTPServer_esp8266_esp32_LittleFS.ino new file mode 100644 index 0000000..0d82e76 --- /dev/null +++ b/examples/FTPServer_esp8266_esp32_LittleFS/FTPServer_esp8266_esp32_LittleFS.ino @@ -0,0 +1,110 @@ +/* + * FtpServer esp8266 and esp32 with LittleFS + * + * AUTHOR: Renzo Mischianti + * + * https://www.mischianti.org/2020/02/08/ftp-server-on-esp8266-and-esp32 + * + */ + +#ifdef ESP8266 +#include