From 52cf361f419b9d956ff640af3c28097023e87598 Mon Sep 17 00:00:00 2001 From: beegee-tokyo Date: Mon, 16 Oct 2017 15:06:18 +0800 Subject: Added ESP32 support Merged @bbx10 transfer speed increase and reduction of blocking (See issue https://github.com/nailbuster/esp8266FTPServer/issues/9) --- ESP8266FtpServer.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++++++++-- ESP8266FtpServer.h | 3 +- 2 files changed, 102 insertions(+), 4 deletions(-) diff --git a/ESP8266FtpServer.cpp b/ESP8266FtpServer.cpp index 34b56c1..f981682 100644 --- a/ESP8266FtpServer.cpp +++ b/ESP8266FtpServer.cpp @@ -19,10 +19,15 @@ */ #include "ESP8266FtpServer.h" - +#ifdef ESP8266 #include -#include #include +#elif defined ESP32 +#include +#include +#include "SPIFFS.h" +#endif +#include #include @@ -378,6 +383,7 @@ boolean FtpServer::processCommand() { client.println( "150 Accepted data connection"); uint16_t nm = 0; +#ifdef ESP8266 Dir dir=SPIFFS.openDir(cwdName); if( !SPIFFS.exists(cwdName)) client.println( "550 Can't open directory " + String(cwdName) ); @@ -395,6 +401,40 @@ boolean FtpServer::processCommand() } client.println( "226 " + String(nm) + " matches total"); } +#elif defined ESP32 + File root = SPIFFS.open(cwdName); + if(!root){ + client.println( "550 Can't open directory " + String(cwdName) ); + // return; + } else { + // if(!root.isDirectory()){ + // Serial.println("Not a directory"); + // return; + // } + + File file = root.openNextFile(); + while(file){ + if(file.isDirectory()){ + data.println( "+r,s " + String(file.name())); + // Serial.print(" DIR : "); + // Serial.println(file.name()); + // if(levels){ + // listDir(fs, file.name(), levels -1); + // } + } else { + String fn, fs; + fn = file.name(); + // fn.remove(0, 1); + fs = String(file.size()); + data.println( "+r,s" + fs); + data.println( ",\t" + fn ); + nm ++; + } + file = root.openNextFile(); + } + client.println( "226 " + String(nm) + " matches total"); + } +#endif data.stop(); } } @@ -409,6 +449,7 @@ boolean FtpServer::processCommand() { client.println( "150 Accepted data connection"); uint16_t nm = 0; +#ifdef ESP8266 Dir dir= SPIFFS.openDir(cwdName); char dtStr[ 15 ]; // if(!SPIFFS.exists(cwdName)) @@ -427,6 +468,40 @@ boolean FtpServer::processCommand() client.println( "226-options: -a -l"); client.println( "226 " + String(nm) + " matches total"); } +#elif defined ESP32 + File root = SPIFFS.open(cwdName); + // if(!root){ + // client.println( "550 Can't open directory " + String(cwdName) ); + // // return; + // } else { + // if(!root.isDirectory()){ + // Serial.println("Not a directory"); + // return; + // } + + File file = root.openNextFile(); + while(file){ + // if(file.isDirectory()){ + // data.println( "+r,s " + String(file.name())); + // // Serial.print(" DIR : "); + // // Serial.println(file.name()); + // // if(levels){ + // // listDir(fs, file.name(), levels -1); + // // } + // } else { + String fn, fs; + fn = file.name(); + fn.remove(0, 1); + fs = String(file.size()); + data.println( "Type=file;Size=" + fs + ";"+"modify=20000101160656;" +" " + fn); + nm ++; + // } + file = root.openNextFile(); + } + client.println( "226-options: -a -l"); + client.println( "226 " + String(nm) + " matches total"); + // } +#endif data.stop(); } } @@ -441,6 +516,7 @@ boolean FtpServer::processCommand() { client.println( "150 Accepted data connection"); uint16_t nm = 0; +#ifdef ESP8266 Dir dir=SPIFFS.openDir(cwdName); if( !SPIFFS.exists( cwdName )) client.println( "550 Can't open directory " + String(parameters)); @@ -453,6 +529,21 @@ boolean FtpServer::processCommand() } client.println( "226 " + String(nm) + " matches total"); } +#elif defined ESP32 + File root = SPIFFS.open(cwdName); + if(!root){ + client.println( "550 Can't open directory " + String(cwdName) ); + } else { + + File file = root.openNextFile(); + while(file){ + data.println( file.name()); + nm ++; + file = root.openNextFile(); + } + client.println( "226 " + String(nm) + " matches total"); + } +#endif data.stop(); } } @@ -693,7 +784,13 @@ boolean FtpServer::doStore() { if( data.connected() ) { - int16_t nb = data.readBytes((uint8_t*) buf, FTP_BUF_SIZE ); + // Avoid blocking by never reading more bytes than are available + int navail = data.available(); + if (navail <= 0) return true; + // And be sure not to overflow buf. + if (navail > FTP_BUF_SIZE) navail = FTP_BUF_SIZE; + int16_t nb = data.read((uint8_t*) buf, navail ); + // int16_t nb = data.readBytes((uint8_t*) buf, FTP_BUF_SIZE ); if( nb > 0 ) { // Serial.println( millis() << " " << nb << endl; diff --git a/ESP8266FtpServer.h b/ESP8266FtpServer.h index 87529ad..877ad24 100644 --- a/ESP8266FtpServer.h +++ b/ESP8266FtpServer.h @@ -44,7 +44,8 @@ #define FTP_CMD_SIZE 255 + 8 // max size of a command #define FTP_CWD_SIZE 255 + 8 // max size of a directory name #define FTP_FIL_SIZE 255 // max size of a file name -#define FTP_BUF_SIZE 1024 //512 // size of file buffer for read/write +// #define FTP_BUF_SIZE 1024 //512 // size of file buffer for read/write +#define FTP_BUF_SIZE 2*1460 //512 // size of file buffer for read/write class FtpServer { -- cgit v1.2.3