Skip to content

Commit 74aadf5

Browse files
committed
Soft AP IP management, more disconnect event #30 and SD_MCC
Invalid #28, tested with this version
1 parent 80794be commit 74aadf5

File tree

6 files changed

+126
-22
lines changed

6 files changed

+126
-22
lines changed

FtpServer.cpp

Lines changed: 97 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ void FtpServer::end()
117117

118118
DEBUG_PRINTLN(F("Stop server!"));
119119

120+
if (FtpServer::_callback) {
121+
FtpServer::_callback(FTP_DISCONNECT, free(), capacity());
122+
}
123+
120124
cmdStage = FTP_Init;
121125
transferStage = FTP_Close;
122126
dataConn = FTP_NoConn;
@@ -224,6 +228,10 @@ uint8_t FtpServer::handleFTP() {
224228
else
225229
millisEndConnection = millis() + 1000L * FTP_TIME_OUT;
226230
} else if (!client.connected()) {
231+
if (FtpServer::_callback) {
232+
FtpServer::_callback(FTP_DISCONNECT, free(), capacity());
233+
}
234+
227235
cmdStage = FTP_Init;
228236
}
229237
if (transferStage == FTP_Retrieve) // Retrieve data
@@ -498,6 +506,22 @@ bool FtpServer::processCommand()
498506
} else {
499507
dataIp = localIp;
500508
}
509+
DEBUG_PRINT( F(" IP: ") );
510+
DEBUG_PRINT( int( dataIp[0]) ); DEBUG_PRINT( F(".") ); DEBUG_PRINT( int( dataIp[1]) ); DEBUG_PRINT( F(".") );
511+
DEBUG_PRINT( int( dataIp[2]) ); DEBUG_PRINT( F(".") ); DEBUG_PRINTLN( int( dataIp[3]) );
512+
513+
DEBUG_PRINT( F(" IP 0.0.0.0: ") );
514+
DEBUG_PRINT(dataIp.toString());
515+
516+
#if (FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_ESP8266_ASYNC) || (FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_ESP8266) || (FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_ESP8266) || (FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_ESP32) || (FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_WiFiNINA) || (FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_SEEED_RTL8720DN)
517+
if (dataIp.toString() == F("0.0.0.0")) {
518+
dataIp = NET_CLASS.softAPIP();
519+
}
520+
#endif
521+
DEBUG_PRINT( F(" Soft IP: ") );
522+
DEBUG_PRINT( int( dataIp[0]) ); DEBUG_PRINT( F(".") ); DEBUG_PRINT( int( dataIp[1]) ); DEBUG_PRINT( F(".") );
523+
DEBUG_PRINT( int( dataIp[2]) ); DEBUG_PRINT( F(".") ); DEBUG_PRINTLN( int( dataIp[3]) );
524+
501525
dataPort = pasvPort;
502526
DEBUG_PRINTLN( F(" Connection management set to passive") );
503527
DEBUG_PRINT( F(" Listening at ") );
@@ -1031,7 +1055,7 @@ bool FtpServer::openDir( FTP_DIR * pdir )
10311055
if( ! openD ) {
10321056
client.print( F("550 Can't open directory ") ); client.println( cwdName );
10331057
}
1034-
#elif STORAGE_TYPE == STORAGE_SD
1058+
#elif STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SD_MMC
10351059
if( cwdName == 0 ) {
10361060
dir = STORAGE_MANAGER.open( "/" );
10371061
} else {
@@ -1398,7 +1422,7 @@ bool FtpServer::doList()
13981422
nbMatch ++;
13991423
return true;
14001424
}
1401-
#elif STORAGE_TYPE == STORAGE_SD
1425+
#elif STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SD_MMC
14021426
FTP_FILE fileDir = dir.openNextFile();
14031427
if( fileDir )
14041428
{
@@ -1604,7 +1628,7 @@ bool FtpServer::doMlsd()
16041628
return true;
16051629
}
16061630

1607-
#elif STORAGE_TYPE == STORAGE_SD
1631+
#elif STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SD_MMC
16081632
DEBUG_PRINTLN("DIR MLSD ");
16091633
File fileDir = dir.openNextFile();
16101634
if( fileDir )
@@ -1840,6 +1864,62 @@ int utf8_strlen(const String& str)
18401864
return q;
18411865
}
18421866

1867+
//// ****** UTF8-Decoder: convert UTF8-string to extended ASCII *******
1868+
//static byte c1; // Last character buffer
1869+
//
1870+
//// Convert a single Character from UTF8 to Extended ASCII
1871+
//// Return "0" if a byte has to be ignored
1872+
//byte utf8ascii(byte ascii) {
1873+
// if ( ascii<128 ) // Standard ASCII-set 0..0x7F handling
1874+
// { c1=0;
1875+
// return( ascii );
1876+
// }
1877+
//
1878+
// // get previous input
1879+
// byte last = c1; // get last char
1880+
// c1=ascii; // remember actual character
1881+
//
1882+
// switch (last) // conversion depending on first UTF8-character
1883+
// { case 0xC2: return (ascii); break;
1884+
// case 0xC3: return (ascii | 0xC0); break;
1885+
// case 0x82: if(ascii==0xAC) return(0x80); // special case Euro-symbol
1886+
// }
1887+
//
1888+
// return (0); // otherwise: return zero, if character has to be ignored
1889+
//}
1890+
//
1891+
//// convert String object from UTF8 String to Extended ASCII
1892+
//String utf8ascii(String s)
1893+
//{
1894+
// String r="";
1895+
// char c;
1896+
// for (int i=0; i<s.length(); i++)
1897+
// {
1898+
// c = utf8ascii(s.charAt(i));
1899+
// if (c!=0) r+=c;
1900+
// }
1901+
// return r;
1902+
//}
1903+
//
1904+
//// In Place conversion UTF8-string to Extended ASCII (ASCII is shorter!)
1905+
//void utf8ascii(char* s)
1906+
//{
1907+
// int k=0;
1908+
// char c;
1909+
// for (int i=0; i<strlen(s); i++)
1910+
// {
1911+
// c = utf8ascii(s[i]);
1912+
// if (c!=0)
1913+
// s[k++]=c;
1914+
// }
1915+
// s[k]=0;
1916+
//}
1917+
//
1918+
//int utf8_strlen(const String& str)
1919+
//{
1920+
// String ascii = utf8ascii(str);
1921+
// return ascii.length();
1922+
//}
18431923
// Make complete path/name from cwdName and param
18441924
//
18451925
// 3 possible cases: param can be absolute path, relative path or only the name
@@ -1884,6 +1964,12 @@ bool FtpServer::makePath( char * fullName, char * param )
18841964
// for( uint8_t i = 0; i < utf8_strlen( fullName ); i ++ ) {
18851965
//
18861966
// }
1967+
1968+
DEBUG_PRINT(F("utf8_strlen"));
1969+
DEBUG_PRINTLN(utf8_strlen(fullName));
1970+
// DEBUG_PRINT(F("utf8_strlen2"));
1971+
// DEBUG_PRINTLN(utf8_strlen2(fullName));
1972+
18871973
if (utf8_strlen(fullName)>=FILENAME_LENGTH) {
18881974
client.println(F("553 File name not allowed. Too long.") );
18891975
return false;
@@ -1910,7 +1996,7 @@ bool FtpServer::makeExistsPath( char * path, char * param )
19101996
return false;
19111997
// RoSchmi
19121998
//#if STORAGE_TYPE == STORAGE_SPIFFS || STORAGE_TYPE == STORAGE_SD
1913-
#if (STORAGE_TYPE == STORAGE_SPIFFS || STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SEEED_SD)
1999+
#if (STORAGE_TYPE == STORAGE_SPIFFS || STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SD_MMC || STORAGE_TYPE == STORAGE_SEEED_SD)
19142000
if (strcmp(path, "/") == 0) return true;
19152001
#endif
19162002
DEBUG_PRINT("PATH --> ")
@@ -2000,7 +2086,7 @@ char * FtpServer::makeDateTimeStr( char * tstr, uint16_t date, uint16_t time )
20002086

20012087

20022088
uint32_t FtpServer::fileSize( FTP_FILE file ) {
2003-
#if (STORAGE_TYPE == STORAGE_SPIFFS || STORAGE_TYPE == STORAGE_LITTLEFS || STORAGE_TYPE == STORAGE_FFAT || STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SEEED_SD)
2089+
#if (STORAGE_TYPE == STORAGE_SPIFFS || STORAGE_TYPE == STORAGE_LITTLEFS || STORAGE_TYPE == STORAGE_FFAT || STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SD_MMC || STORAGE_TYPE == STORAGE_SEEED_SD)
20042090
return file.size();
20052091
#else
20062092
return file.fileSize();
@@ -2029,7 +2115,7 @@ uint32_t FtpServer::fileSize( FTP_FILE file ) {
20292115
return true;
20302116
}
20312117
}
2032-
#elif (STORAGE_TYPE == STORAGE_SD && defined(ESP8266))// FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_ESP8266_242)
2118+
#elif ((STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SD_MMC) && defined(ESP8266))// FTP_SERVER_NETWORK_TYPE_SELECTED == NETWORK_ESP8266_242)
20332119
bool FtpServer::openFile( char path[ FTP_CWD_SIZE ], int readTypeInt ){
20342120
DEBUG_PRINT(F("File to open ") );
20352121
DEBUG_PRINT( path );
@@ -2066,7 +2152,7 @@ uint32_t FtpServer::fileSize( FTP_FILE file ) {
20662152
return true;
20672153
}
20682154
}
2069-
#elif STORAGE_TYPE <= STORAGE_SDFAT2 || STORAGE_TYPE == STORAGE_SPIFM || (STORAGE_TYPE == STORAGE_SD && ARDUINO_ARCH_SAMD)
2155+
#elif STORAGE_TYPE <= STORAGE_SDFAT2 || STORAGE_TYPE == STORAGE_SPIFM || ((STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SD_MMC) && ARDUINO_ARCH_SAMD)
20702156
bool FtpServer::openFile( char path[ FTP_CWD_SIZE ], int readTypeInt ){
20712157
DEBUG_PRINT(F("File to open ") );
20722158
DEBUG_PRINT( path );
@@ -2092,7 +2178,7 @@ uint32_t FtpServer::fileSize( FTP_FILE file ) {
20922178
DEBUG_PRINT( path );
20932179
DEBUG_PRINT(F(" readType ") );
20942180
DEBUG_PRINTLN(readType);
2095-
#if (STORAGE_TYPE == STORAGE_SD && !defined(ESP32))
2181+
#if ((STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SD_MMC) && !defined(ESP32))
20962182
if (readType == 0X01) {
20972183
readType = FILE_READ;
20982184
}else {
@@ -2150,7 +2236,7 @@ bool FtpServer::isDir( char * path )
21502236
if( ! openFile( path, FTP_FILE_READ )) {
21512237
return false;
21522238
}
2153-
#if STORAGE_TYPE == STORAGE_SD
2239+
#if STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SD_MMC
21542240
// if (strcmp(path, "/") == 0) return true;
21552241
// res = file.isDirectory();
21562242
// DEBUG_PRINT(path);
@@ -2170,7 +2256,7 @@ bool FtpServer::isDir( char * path )
21702256
bool FtpServer::timeStamp( char * path, uint16_t year, uint8_t month, uint8_t day,
21712257
uint8_t hour, uint8_t minute, uint8_t second )
21722258
{
2173-
#if STORAGE_TYPE == STORAGE_SPIFFS || STORAGE_TYPE == STORAGE_LITTLEFS || STORAGE_TYPE == STORAGE_FFAT || STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SEEED_SD
2259+
#if STORAGE_TYPE == STORAGE_SPIFFS || STORAGE_TYPE == STORAGE_LITTLEFS || STORAGE_TYPE == STORAGE_FFAT || STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SD_MMC || STORAGE_TYPE == STORAGE_SEEED_SD
21742260
// struct tm tmDate = { second, minute, hour, day, month, year };
21752261
// time_t rawtime = mktime(&tmDate);
21762262

@@ -2235,7 +2321,7 @@ bool FtpServer::getFileModTime( uint16_t * pdate, uint16_t * ptime )
22352321
}
22362322
#endif
22372323

2238-
#if STORAGE_TYPE == STORAGE_SD
2324+
#if STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SD_MMC
22392325
bool FtpServer::rename( const char * path, const char * newpath ){
22402326

22412327
FTP_FILE myFileIn = STORAGE_MANAGER.open(path, FILE_READ);

FtpServer.h

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#ifndef FTP_SERVER_H
2121
#define FTP_SERVER_H
2222

23-
#define FTP_SERVER_VERSION "2.1.2 (2022-07-06)"
23+
#define FTP_SERVER_VERSION "2.1.3 (2022-09-20)"
2424

2525
#if ARDUINO >= 100
2626
#include "Arduino.h"
@@ -298,13 +298,18 @@
298298
#define FTP_FILE_WRITE_APPEND "a+"
299299
#define FTP_FILE_WRITE_CREATE "w+"
300300
#else
301-
#if ESP_ARDUINO_VERSION_MAJOR >= 2
302-
#include "FS.h"
303-
#include "LittleFS.h"
304-
#define STORAGE_MANAGER LittleFS
301+
#ifdef ESP32
302+
#if ESP_ARDUINO_VERSION_MAJOR >= 2
303+
#include "FS.h"
304+
#include "LittleFS.h"
305+
#define STORAGE_MANAGER LittleFS
306+
#else
307+
#include "LITTLEFS.h"
308+
#define STORAGE_MANAGER LITTLEFS
309+
#endif
305310
#else
306-
#include "LITTLEFS.h"
307-
#define STORAGE_MANAGER LITTLEFS
311+
#include "LittleFS.h"
312+
#define STORAGE_MANAGER LittleFS
308313
#endif
309314
#define FTP_FILE File
310315
#define FTP_DIR File
@@ -324,6 +329,17 @@
324329
#define FTP_FILE File
325330
#define FTP_DIR File
326331

332+
#define FTP_FILE_READ FILE_READ
333+
#define FTP_FILE_READ_ONLY FILE_READ
334+
#define FTP_FILE_READ_WRITE FILE_WRITE
335+
#elif(STORAGE_TYPE == STORAGE_SD_MMC)
336+
#include <SPI.h>
337+
#include <SD_MMC.h>
338+
339+
#define STORAGE_MANAGER SD
340+
#define FTP_FILE File
341+
#define FTP_DIR File
342+
327343
#define FTP_FILE_READ FILE_READ
328344
#define FTP_FILE_READ_ONLY FILE_READ
329345
#define FTP_FILE_READ_WRITE FILE_WRITE
@@ -584,7 +600,7 @@ class FtpServer
584600
bool removeDir( const char * path ) { return STORAGE_MANAGER.rmdir( path ); };
585601
#endif
586602

587-
#if STORAGE_TYPE == STORAGE_SD
603+
#if STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SD_MMC
588604
bool rename( const char * path, const char * newpath );
589605
#else
590606
bool rename( const char * path, const char * newpath ) { return STORAGE_MANAGER.rename( path, newpath ); };
@@ -630,7 +646,7 @@ class FtpServer
630646
STORAGE_MANAGER.usedBytes()) >> 1;
631647
};
632648
#endif
633-
#elif STORAGE_TYPE == STORAGE_SD
649+
#elif STORAGE_TYPE == STORAGE_SD || STORAGE_TYPE == STORAGE_SD_MMC
634650
uint32_t capacity() { return true; };
635651
uint32_t free() { return true; };
636652
#elif STORAGE_TYPE == STORAGE_SEEED_SD

FtpServerKey.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#define STORAGE_LITTLEFS 7 // LITTLEFS
3333
#define STORAGE_SEEED_SD 8 // Seeed_SD library
3434
#define STORAGE_FFAT 9 // ESP32 FFAT
35+
#define STORAGE_SD_MMC 10 // SD_MMC library
3536

3637
#define NETWORK_ESP8266_ASYNC (1)
3738
#define NETWORK_ESP8266 (2) // Standard ESP8266WiFi

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Wio Terminal (SdFat 2, Seed SD, and native FAT)
1212

1313
#### Changelog
14+
- 2022-09-20 2.1.3 Soft AP IP management, more disconnect event and SD_MCC
1415
- 2022-05-21 2.1.2 Fix SD path (#19)
1516
- 2022-05-21 2.1.1 Minor fix
1617
- 2022-03-30 2.1.0 Add UTF8 support and enabled It by default (Thanks to @plaber)

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
},
1717
"url": "https://www.mischianti.org",
1818
"frameworks": "Arduino",
19-
"version": "2.1.2",
19+
"version": "2.1.3",
2020
"platforms": "*"
2121
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SimpleFTPServer
2-
version=2.1.2
2+
version=2.1.3
33
author=Renzo Mischianti <renzo.mischianti@gmail.com>
44
maintainer=Renzo Mischianti <renzo.mischianti@gmail.com>
55
sentence=Simple FTP server for esp8266, esp32 and Arduino

0 commit comments

Comments
 (0)