ADD FTP server to filesystem (#20402)

* add ftp lib

* add ftp server
This commit is contained in:
gemu 2024-01-08 16:44:49 +01:00 committed by GitHub
parent 22d2377442
commit 92214ac633
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1641 additions and 3 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,125 @@
/*
* FTP SERVER FOR ESP8266/ESP32
* based on FTP Server for Arduino Due and Ethernet shield (W5100) or WIZ820io (W5200)
* based on Jean-Michel Gallego's work
* modified to work with esp8266 SPIFFS by David Paiva (david@nailbuster.com)
* 2017: modified by @robo8080 (ported to ESP32 and SD)
* 2019: modified by @fa1ke5 (use SD card in SD_MMC mode (No SD lib, SD_MMC lib), and added fully fuctional passive mode ftp server)
* 2020: modified by @jmwislez (support generic FS, and re-introduced ESP8266)
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*******************************************************************************
** **
** DEFINITIONS FOR FTP SERVER **
** **
*******************************************************************************/
// Uncomment to print debugging info to console
//#define FTP_DEBUG
#ifndef FTP_SERVERESP_H
#define FTP_SERVERESP_H
#include <FS.h>
#include <WiFiClient.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#define FTP_SERVER_VERSION "jmwislez/ESP32FtpServer 0.1.0"
#define FTP_CTRL_PORT 21 // Command port on wich server is listening
#define FTP_DATA_PORT_PASV 50009 // Data port in passive mode
#define FTP_TIME_OUT 5 // Disconnect client after 5 minutes of inactivity
#ifdef ESP32
#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 4096 //512 // 700 KByte/s download in AP mode, direct connection.
#endif
#ifdef ESP8266
#define FTP_CMD_SIZE 128 // max size of a command
#define FTP_CWD_SIZE 128 // max size of a directory name
#define FTP_FIL_SIZE 64 // max size of a file name
#define FTP_BUF_SIZE 256 // 700 KByte/s download in AP mode, direct connection.
#endif
class FtpServer {
public:
void begin(String uname, String pword, FS *ufp);
void handleFTP (void);
~FtpServer(void);
bool is_up = false;
private:
bool haveParameter ();
bool makeExistsPath (char * path, char * param = NULL);
void iniVariables ();
void clientConnected ();
void disconnectClient ();
boolean userIdentity ();
boolean userPassword ();
boolean processCommand ();
boolean dataConnect ();
boolean doRetrieve ();
boolean doStore ();
void closeTransfer ();
void abortTransfer ();
boolean makePath (char * fullname);
boolean makePath (char * fullName, char * param);
uint8_t getDateTime (uint16_t * pyear, uint8_t * pmonth, uint8_t * pday,
uint8_t * phour, uint8_t * pminute, uint8_t * second);
char * makeDateTimeStr (char * tstr, uint16_t date, uint16_t time);
int8_t readChar ();
String fillSpaces (uint8_t len, String input_str);
IPAddress dataIp; // IP address of client for data
WiFiClient client;
WiFiClient data;
WiFiServer *ftpServer;
WiFiServer *dataServer;
FS *cfsp;
File file;
boolean dataPassiveConn;
uint16_t dataPort;
char buf[FTP_BUF_SIZE]; // data buffer for transfers
char cmdLine[FTP_CMD_SIZE]; // where to store incoming char from client
char cwdName[FTP_CWD_SIZE]; // name of current directory
char command[5]; // command sent by client
boolean rnfrCmd; // previous command was RNFR
char * parameters; // point to begin of parameters sent by client
uint16_t iCL; // pointer to cmdLine next incoming char
int8_t cmdStatus, // status of ftp command connexion
transferStatus; // status of ftp data transfer
uint32_t millisTimeOut, // disconnect after 5 min of inactivity
millisDelay,
millisEndConnection, //
millisBeginTrans, // store time of beginning of a transaction
bytesTransferred; //
String _FTP_USER;
String _FTP_PASS;
};
#endif // FTP_SERVERESP_H

View File

@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

View File

@ -0,0 +1,14 @@
# ESPFtpServer
The history of this code is as follows:
* https://github.com/gallegojm/Arduino-Ftp-Server/tree/master/FtpServer: FTP server for Arduino Mega2560 and Due with ethernet module W5100, W5200 or W5500
* https://github.com/nailbuster/esp8266FTPServer: ported to ESP8266 and SPIFFS file system
* https://github.com/robo8080/ESP32_FTPServer_SD: ported to ESP32 and SD card file system
* https://github.com/fa1ke5/ESP32_FTPServer_SD_MMC: use SD card in SD_MMC mode (No SD lib, SD_MMC lib). Added fully fuctional passive mode ftp server, browse dir, change dir, rename dir/file, delete dir/file, upload and download files, dirs.
The current repository is forked from fa1ke5, and has the following changes:
* use of any file system file system (like SPIFFS/LittleFS/SD_MMC)
* codebase to work for both ESP8266 and ESP32
* clean-up of code layout and English
* addition of library description files

View File

@ -0,0 +1,104 @@
// Uncomment the file system to use for FTP server
#define FS_LITTLEFS
//#define FS_SPIFFS
//#define FS_SD_MMC
#ifdef ESP32
#include <WiFi.h>
#endif
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#include <WiFiClient.h>
#include <time.h>
#include "ESPFtpServer.h"
#if defined(FS_LITTLEFS)
#ifdef ESP32
#include "LITTLEFS.h"
#define FS_ID LITTLEFS
#endif
#ifdef ESP8266
#include "LittleFS.h"
#define FS_ID LittleFS
#endif
#define FS_NAME "LittleFS"
#elif defined(FS_SPIFFS)
#ifdef ESP32
#include "SPIFFS.h"
#endif
#define FS_ID SPIFFS
#define FS_NAME "SPIFFS"
#elif defined(FS_SD_MMC)
#include "SD_MMC.h"
#define FS_ID SD_MMC
#define FS_NAME "SD_MMC"
#else
#define FS_ID SD
#define FS_NAME "UNDEF"
#endif
const char* ssid = "*********************";
const char* password = "*********************";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
struct tm timeinfo;
FtpServer ftpSrv; //set #define FTP_DEBUG in ESP32FtpServer.h to see ftp verbose on serial
#ifdef ESP8266
bool getLocalTime (struct tm * info) {
time_t now;
time(&now);
localtime_r (&now, info);
if (info->tm_year > (2016 - 1900)) {
return true;
}
else {
return false;
}
}
#endif
void setup (void) {
Serial.begin (115200);
WiFi.begin (ssid, password);
Serial.println ("");
// Wait for connection
while (WiFi.status () != WL_CONNECTED) {
delay (500);
Serial.print (".");
}
Serial.println ("");
Serial.print ("Connected to ");
Serial.println (ssid);
Serial.print ("IP address: ");
Serial.println (WiFi.localIP ());
configTime (gmtOffset_sec, daylightOffset_sec, ntpServer);
while (!getLocalTime (&timeinfo)) {
delay (500);
Serial.print (".");
}
Serial.printf ("\nNow is : %d-%02d-%02d %02d:%02d:%02d\n", (timeinfo.tm_year) + 1900, (timeinfo.tm_mon) + 1, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
//FS_ID.format ();
if (FS_ID.begin ()) {
Serial.println ("File system opened (" + String (FS_NAME) + ")");
ftpSrv.begin ("esp32", "esp32"); //username, password for ftp. set ports in ESPFtpServer.h (default 21, 50009 for PASV)
}
else {
Serial.println ("File system could not be opened; ftp server will not work");
}
}
void loop (void){
ftpSrv.handleFTP (FS_ID); //make sure in loop you call handleFTP()!
}

View File

@ -0,0 +1,21 @@
{
"name": "ESPFtpServer",
"keywords": "ftp, littlefs, spiffs, esp32, esp8266",
"description": "ESPFtpServer implements a simple FTP server (passive mode, only one connection at a time) on ESP8266 and ESP32, for any file system (SPIFFS/LittleFS/SD_MMC).",
"homepage": "https://github.com/jmwislez/ESPFtpServer/",
"repository": {
"type": "git",
"url": "https://github.com/jmwislez/ESPFtpServer.git"
},
"version": "0.1.0",
"authors": {
"name": "Jean-Marc Wislez",
"url": "https://github.com/jmwislez/"
},
"exclude": [
".github",
"extras"
],
"frameworks": "arduino",
"platforms": ["espressif8266", "espressif32"]
}

View File

@ -0,0 +1,12 @@
name=ESPFtpServer
version=0.1.0
author=Jean-Marc Wislez <jmwislez@gmail.com>
maintainer=Jean-Marc Wislez <jmwislez@gmail.com>
sentence=A simple FTP server for LittleFS/SPIFFS on ESP8266/ESP32
paragraph=ESPFtpServer implements a simple FTP server (passive mode, only one connection at a time) on ESP8266 and ESP32, for any file system (SPIFFS/LittleFS/SD_MMC).
category=Network
url=https://github.com/jmwislez/ESPFtpServer/
architectures=esp8266,esp32
repository=https://github.com/jmwislez/ESPFtpServer.git
license=GPL
architectures=*

View File

@ -264,8 +264,7 @@ typedef union {
uint32_t spare21 : 1; // bit 21
uint32_t spare22 : 1; // bit 22
uint32_t spare23 : 1; // bit 23
uint32_t spare24 : 1; // bit 24
uint32_t spare25 : 1; // bit 25
uint32_t FTP_Mode : 2; // bit 24, 25
uint32_t tariff_forced : 2; // bit 26..27 (v12.4.0.2) - Energy forced tariff : 0=tariff change on time, 1|2=tariff forced
uint32_t sunrise_dawn_angle : 2; // bits 28/29 (v12.1.1.4) -
uint32_t temperature_set_res : 2; // bits 30/31 (v9.3.1.4) - (Tuya)

View File

@ -48,6 +48,13 @@ ufs fs info
ufstype get filesytem type 0=none 1=SD 2=Flashfile
ufssize total size in kB
ufsfree free size in kB
ufsdelete
ufsrename
ufsrun
ufsServe
ftp start stop ftp server: 0 = OFF, 1 = SDC, 2 = FlashFile
\*********************************************************************************************/
#define XDRV_50 50
@ -547,6 +554,9 @@ const char kUFSCommands[] PROGMEM = "Ufs|" // Prefix
"|Type|Size|Free|Delete|Rename|Run"
#ifdef UFILESYS_STATIC_SERVING
"|Serve"
#endif
#ifdef USE_FTP
"|FTP"
#endif
;
@ -554,7 +564,10 @@ void (* const kUFSCommand[])(void) PROGMEM = {
&UFSInfo, &UFSType, &UFSSize, &UFSFree, &UFSDelete, &UFSRename, &UFSRun
#ifdef UFILESYS_STATIC_SERVING
,&UFSServe
#endif
#endif
#ifdef USE_FTP
,&Switch_FTP
#endif
};
void UFSInfo(void) {
@ -1348,17 +1361,70 @@ void UfsEditorUpload(void) {
#endif // USE_WEBSERVER
#ifdef USE_FTP
#include <ESPFtpServer.h>
FtpServer *ftpSrv;
void FTP_Server(uint32_t mode) {
if (mode > 0) {
if (ftpSrv) {
delete ftpSrv;
}
ftpSrv = new FtpServer;
if (mode == 1) {
ftpSrv->begin(USER_FTP,PW_FTP, ufsp);
} else {
ftpSrv->begin(USER_FTP,PW_FTP, ffsp);
}
AddLog(LOG_LEVEL_INFO, PSTR("UFS: FTP Server started in mode: '%d'"), mode);
} else {
if (ftpSrv) {
delete ftpSrv;
ftpSrv = nullptr;
}
}
}
void Switch_FTP(void) {
if (XdrvMailbox.data_len > 0) {
if (XdrvMailbox.payload >= 0 && XdrvMailbox.payload <= 2) {
FTP_Server(XdrvMailbox.payload);
Settings->mbflag2.FTP_Mode = XdrvMailbox.payload;
}
}
ResponseCmndNumber(Settings->mbflag2.FTP_Mode);
}
#endif // USE_FTP
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
bool Xdrv50(uint32_t function) {
bool result = false;
switch (function) {
case FUNC_LOOP:
UfsExecuteCommandFileLoop();
#ifdef USE_FTP
if (ftpSrv) {
ftpSrv->handleFTP();
}
#endif
break;
case FUNC_NETWORK_UP:
#ifdef USE_FTP
if (Settings->mbflag2.FTP_Mode && !ftpSrv) {
FTP_Server(Settings->mbflag2.FTP_Mode);
}
#endif
break;
/*
// Moved to support_tasmota.ino for earlier init to be used by scripter
#ifdef USE_SDCARD