summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Paiva <david@nailbuster.com>2017-10-18 15:33:55 +0200
committerGitHub <noreply@github.com>2017-10-18 15:33:55 +0200
commit7dfca5a1cb12dbfe5984eb34a87498f965dcdbdc (patch)
treee1426ace49bc490b5c660d8be1728a81d1e943eb
parentMerge pull request #1 from lnxbil/patch-1 (diff)
parentAdded ESP32 support (diff)
downloadFTPCLientServer-7dfca5a1cb12dbfe5984eb34a87498f965dcdbdc.tar
FTPCLientServer-7dfca5a1cb12dbfe5984eb34a87498f965dcdbdc.tar.gz
FTPCLientServer-7dfca5a1cb12dbfe5984eb34a87498f965dcdbdc.tar.bz2
FTPCLientServer-7dfca5a1cb12dbfe5984eb34a87498f965dcdbdc.tar.lz
FTPCLientServer-7dfca5a1cb12dbfe5984eb34a87498f965dcdbdc.tar.xz
FTPCLientServer-7dfca5a1cb12dbfe5984eb34a87498f965dcdbdc.tar.zst
FTPCLientServer-7dfca5a1cb12dbfe5984eb34a87498f965dcdbdc.zip
-rw-r--r--ESP8266FtpServer.cpp103
-rw-r--r--ESP8266FtpServer.h3
-rw-r--r--library.json19
3 files changed, 121 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 <ESP8266WiFi.h>
-#include <WiFiClient.h>
#include <ESP8266WebServer.h>
+#elif defined ESP32
+#include <WiFi.h>
+#include <Webserver.h>
+#include "SPIFFS.h"
+#endif
+#include <WiFiClient.h>
#include <FS.h>
@@ -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 <DIR> " + 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 <DIR> " + 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
{
diff --git a/library.json b/library.json
new file mode 100644
index 0000000..bb72342
--- /dev/null
+++ b/library.json
@@ -0,0 +1,19 @@
+{
+ "name": "esp8266FTPServer",
+ "description": "Simple FTP Server for using esp8266 SPIFFs",
+ "keywords": "esp8266, ftp, spiffs",
+ "authors":
+ {
+ "name": "David Paiva",
+ "email": "david@paivahome.com",
+ "url": "http://nailbuster.com/"
+ },
+ "repository":
+ {
+ "type": "git",
+ "url": "https://github.com/beegee-tokyo/esp8266FTPServer"
+ },
+ "url": "http://nailbuster.com/",
+ "frameworks": "Arduino",
+ "platforms": "*"
+}