Removed the dependency to definition.h as well as replacing SDCardManager class parameter to SDClass parameter

This commit is contained in:
anschrammh 2020-12-19 21:04:11 +01:00
parent 573e33ccf4
commit 8be389a08d

View File

@ -1,9 +1,9 @@
#ifndef FTPSERVER_H #ifndef FTPSERVER_H
#define FTPSERVER_H #define FTPSERVER_H
#include <SD.h>
#include "TCPServer.h" #include "TCPServer.h"
#include "SDCardManager.h" #include "utilities.h"
#include "definition.h"
#include "Dictionary.h" #include "Dictionary.h"
//#define DEBUG_FTPS //#define DEBUG_FTPS
#define READ_BUFFER_SIZE 2048 //2048 is max to read sd card, more will crash #define READ_BUFFER_SIZE 2048 //2048 is max to read sd card, more will crash
@ -18,12 +18,12 @@ class FTPServer : public TCPServer<T>
enum BinaryFlag {OFF = 0, ON}; enum BinaryFlag {OFF = 0, ON};
enum FtpMsgCode {_150, _200, _215, _220, _221, _230, _226, _227, _250, _257, _331, _350, _451, _5_502, _504, _530, _550 }; enum FtpMsgCode {_150, _200, _215, _220, _221, _230, _226, _227, _250, _257, _331, _350, _451, _5_502, _504, _530, _550 };
FTPServer(uint16_t port = 21, SDCardManager *sdCardManager = NULL, const char *login = NULL, const char *password = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientCommandDataBufferSize = 255) : TCPServer<T>(port, maxClient, clientCommandDataBufferSize), FTPServer(uint16_t port = 21, SDClass *sdClass = NULL, const char *login = NULL, const char *password = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientCommandDataBufferSize = 255) : TCPServer<T>(port, maxClient, clientCommandDataBufferSize),
_login(NULL), _login(NULL),
_password(NULL), _password(NULL),
_dataPort(1024), _dataPort(1024),
_dataServer(_dataPort), _dataServer(_dataPort),
_sdCardManager(sdCardManager) _sdClass(sdClass)
{ {
if (login != NULL) if (login != NULL)
{ {
@ -65,6 +65,11 @@ class FTPServer : public TCPServer<T>
} }
} }
virtual void setFTPDir(const char *FTPDir)
{
_FTPDir = FTPDir;
}
protected: protected:
virtual T* createNewClient(WiFiClient wc) virtual T* createNewClient(WiFiClient wc)
{ {
@ -191,10 +196,10 @@ class FTPServer : public TCPServer<T>
{ {
if(client->_fileRecvBytes == 0) //File was just created empty if(client->_fileRecvBytes == 0) //File was just created empty
{ {
if(_sdCardManager->exists(client->_currentFile)) if(_sdClass->exists(client->_currentFile))
_sdCardManager->remove(client->_currentFile); _sdClass->remove(client->_currentFile);
File file2create = _sdCardManager->open(client->_currentFile, FILE_WRITE); File file2create = _sdClass->open(client->_currentFile, FILE_WRITE);
file2create.close(); file2create.close();
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
@ -275,7 +280,7 @@ class FTPServer : public TCPServer<T>
processCommands(client); processCommands(client);
break; break;
case INIT: case INIT:
client->setCurrentDirectory(FTP_DIR); _FTPDir ? client->setCurrentDirectory(_FTPDir) : client->setCurrentDirectory("");
client->_ftpClientState = WAITING_FOR_COMMANDS; client->_ftpClientState = WAITING_FOR_COMMANDS;
break; break;
} }
@ -555,7 +560,7 @@ class FTPServer : public TCPServer<T>
Serial.printf("Final dirName : #%s#\n",dirNameWithPath); Serial.printf("Final dirName : #%s#\n",dirNameWithPath);
#endif #endif
if(_sdCardManager->mkdir(dirNameWithPath)) if(_sdClass->mkdir(dirNameWithPath))
{ {
client->_client.printf("257 \"%s\"\r\n", dirNameWithPath); client->_client.printf("257 \"%s\"\r\n", dirNameWithPath);
} }
@ -591,7 +596,7 @@ class FTPServer : public TCPServer<T>
Serial.printf("pathDirName to delete : #%s#\n",dirNameWithPath); Serial.printf("pathDirName to delete : #%s#\n",dirNameWithPath);
#endif #endif
if(_sdCardManager->rmdir(dirNameWithPath)) if(_sdClass->rmdir(dirNameWithPath))
{ {
client->_client.println("250 Requested file action okay."); client->_client.println("250 Requested file action okay.");
} }
@ -677,7 +682,7 @@ class FTPServer : public TCPServer<T>
Serial.printf("file to delete : #%s#\n",file2deleteNameWithPath); Serial.printf("file to delete : #%s#\n",file2deleteNameWithPath);
#endif #endif
if(_sdCardManager->remove(file2deleteNameWithPath)) if(_sdClass->remove(file2deleteNameWithPath))
{ {
client->_client.println("250 Requested file action okay."); client->_client.println("250 Requested file action okay.");
} }
@ -747,7 +752,7 @@ class FTPServer : public TCPServer<T>
Serial.printf("Old name : %s --> %s\n",client->_currentFile,file2RenameNameWithPath); Serial.printf("Old name : %s --> %s\n",client->_currentFile,file2RenameNameWithPath);
#endif #endif
if(_sdCardManager->rename(client->_currentFile,file2RenameNameWithPath)) if(_sdClass->rename(client->_currentFile,file2RenameNameWithPath))
{ {
client->_client.println("250 Requested file action okay."); client->_client.println("250 Requested file action okay.");
}else }else
@ -789,10 +794,10 @@ class FTPServer : public TCPServer<T>
{ {
if (client->_currentFile != NULL) if (client->_currentFile != NULL)
{ {
if(_sdCardManager->exists(client->_currentFile) && client->_fileRecvBytes == 0 && !append) if(_sdClass->exists(client->_currentFile) && client->_fileRecvBytes == 0 && !append)
_sdCardManager->remove(client->_currentFile); _sdClass->remove(client->_currentFile);
File fileBeeingReceived = _sdCardManager->open(client->_currentFile, FILE_WRITE); File fileBeeingReceived = _sdClass->open(client->_currentFile, FILE_WRITE);
if(fileBeeingReceived) if(fileBeeingReceived)
{ {
@ -830,7 +835,7 @@ class FTPServer : public TCPServer<T>
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
Serial.printf("Directory : %s\n",client->_currentDirectory); Serial.printf("Directory : %s\n",client->_currentDirectory);
#endif #endif
File currentDirectory = _sdCardManager->open(client->_currentDirectory); File currentDirectory = _sdClass->open(client->_currentDirectory);
if (currentDirectory) if (currentDirectory)
{ {
currentDirectory.rewindDirectory(); currentDirectory.rewindDirectory();
@ -886,7 +891,7 @@ class FTPServer : public TCPServer<T>
if (client->_currentFile != NULL) if (client->_currentFile != NULL)
{ {
uint8_t sendBuffer[READ_BUFFER_SIZE]; uint8_t sendBuffer[READ_BUFFER_SIZE];
File fileToSend = _sdCardManager->open(client->_currentFile); File fileToSend = _sdClass->open(client->_currentFile);
if (fileToSend) if (fileToSend)
{ {
@ -992,10 +997,11 @@ class FTPServer : public TCPServer<T>
char *_login; char *_login;
char *_password; char *_password;
const char *_FTPDir = NULL; //Pointer to constant string and char * cont is a constant pointer to string
uint16_t _dataPort; uint16_t _dataPort;
WiFiServer _dataServer; //In passive mode, the FTP server opens two different ports (one for the commands and the other for the data stream) WiFiServer _dataServer; //In passive mode, the FTP server opens two different ports (one for the commands and the other for the data stream)
SDCardManager *_sdCardManager; SDClass *_sdClass;
}; };
#endif //FTPSERVER_H #endif //FTPSERVER_H