Changed sdcard library driver, now using the one for the esp8266 which is mush better, also note that the WEBServerManager will be removed in a near future

This commit is contained in:
anschrammh 2019-10-18 01:06:59 +02:00
parent 0c152a7dc5
commit b272ab0f22
5 changed files with 207 additions and 90 deletions

View File

@ -5,7 +5,7 @@
#include "SDCardManager.h" #include "SDCardManager.h"
#include "definition.h" #include "definition.h"
#define DEBUG_FTPS #define DEBUG_FTPS
#define READ_WRITE_BUFFER_SIZE 2048 #define READ_WRITE_BUFFER_SIZE 2500
template <typename T> template <typename T>
class FTPServer : public TCPServer<T> class FTPServer : public TCPServer<T>
@ -98,9 +98,15 @@ class FTPServer : public TCPServer<T>
if (client->_dataClient.connected()) if (client->_dataClient.connected())
{ {
client->_client.println("150 File status okay;"); client->_client.println("150 File status okay;");
sendFSTree(client); if(sendFSTree(client))
{
client->_client.println("226 Closing data connection."); client->_client.println("226 Closing data connection.");
}
else
{
client->_client.println("451 Requested action aborted.");
}
client->_dataClient.stop(); client->_dataClient.stop();
client->_dataTransferPending = NONE; client->_dataTransferPending = NONE;
} }
@ -144,6 +150,9 @@ class FTPServer : public TCPServer<T>
case STOR_DF : case STOR_DF :
if (client->_dataClient.connected() || client->_dataClient.available()) if (client->_dataClient.connected() || client->_dataClient.available())
{ {
if(client->_dataClient.connected())
client->_fileIsBeeingReceived = true;
if(client->_dataClient.available()) if(client->_dataClient.available())
{ {
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
@ -165,15 +174,28 @@ class FTPServer : public TCPServer<T>
client->_fileIsBeeingReceived = false; client->_fileIsBeeingReceived = false;
client->_dataTransferPending = NONE; client->_dataTransferPending = NONE;
} }
else
client->_fileIsBeeingReceived = true;
} }
} }
else if(client->_fileIsBeeingReceived) else if(client->_fileIsBeeingReceived)
{
if(client->_fileRecvBytes == 0) //File was just created empty
{
if(_sdCardManager->exists(client->_currentFile))
_sdCardManager->remove(client->_currentFile);
File file2create = _sdCardManager->open(client->_currentFile, FILE_WRITE);
file2create.close();
#ifdef DEBUG_FTPS
Serial.println("File just created");
#endif
}
else
{ {
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
Serial.println("Whole file received"); Serial.println("Whole file received");
#endif #endif
}
client->_client.println("226 Closing data connection."); client->_client.println("226 Closing data connection.");
@ -344,12 +366,32 @@ class FTPServer : public TCPServer<T>
} }
else else
{ {
char *temp = (char *)malloc((sizeof(char) * strlen(client->_currentDirectory)) + (sizeof(char) * strlen(client->_cmdParameters->getAt(0)->getString())) + 2); //Directories with spaces are now working:
uint16_t dirNameSize(0), paramCount(client->_cmdParameters->count());
for(int i(0); i < paramCount; i++)
{
dirNameSize += strlen(client->_cmdParameters->getAt(i)->getString()) + 1;
}
dirNameSize += strlen(client->_currentDirectory) + 1;
#ifdef DEBUG_FTPS
Serial.printf("Param size : %d\n", dirNameSize);
#endif
char *temp = (char *)malloc(sizeof(char) * dirNameSize + 1);// /!\ test for malloc fail
strcpy(temp,client->_currentDirectory); strcpy(temp,client->_currentDirectory);
if(strcmp(temp, "/") != 0)strcat(temp,"/"); if(strcmp(temp, "/") != 0)strcat(temp,"/");
strcat(temp,client->_cmdParameters->getAt(0)->getString());
for(int i(0); i < paramCount; i++)
{
strcat(temp,client->_cmdParameters->getAt(i)->getString());
if(i != paramCount-1)
strcat(temp," ");
}
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
Serial.printf("Final dir : %s\n",temp); Serial.printf("Final dir : %s, size : %d --> %d\n",temp, dirNameSize, strlen(temp));
#endif #endif
client->setCurrentDirectory(temp); client->setCurrentDirectory(temp);
free(temp); free(temp);
@ -367,11 +409,32 @@ class FTPServer : public TCPServer<T>
if (client->_cmdParameters->count() > 0) if (client->_cmdParameters->count() > 0)
{ {
//We save the file path to be sent //We save the file path to be sent
char *temp = (char *)malloc((sizeof(char) * strlen(client->_currentDirectory)) + (sizeof(char) * strlen(client->_cmdParameters->getAt(0)->getString())) + (strcmp(client->_currentDirectory,"/") == 0 ? 0 : 1) + 1); uint16_t filePathSize(0), paramCount(client->_cmdParameters->count());
strcpy(temp, client->_currentDirectory); for(int i(0); i < paramCount; i++)
if(strcmp(client->_currentDirectory,"/") != 0 )strcat(temp, "/"); {
strcat(temp, client->_cmdParameters->getAt(0)->getString()); filePathSize += strlen(client->_cmdParameters->getAt(i)->getString()) + 1;
}
filePathSize += strlen(client->_currentDirectory) + 1;
#ifdef DEBUG_FTPS
Serial.printf("filePathSize : %d\n", filePathSize);
#endif
char *temp = (char *)malloc(sizeof(char) * filePathSize + 1);// /!\ test for malloc fail
strcpy(temp,client->_currentDirectory);
if(strcmp(temp, "/") != 0)strcat(temp,"/");
for(int i(0); i < paramCount; i++)
{
strcat(temp,client->_cmdParameters->getAt(i)->getString());
if(i != paramCount-1)
strcat(temp," ");
}
#ifdef DEBUG_FTPS
Serial.printf("Final file path : %s, size : %d --> %d\n",temp, filePathSize, strlen(temp));
#endif
client->setCurrentFile(temp); client->setCurrentFile(temp);
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
Serial.printf("File to donwload : %s\n", temp); Serial.printf("File to donwload : %s\n", temp);
@ -384,54 +447,42 @@ class FTPServer : public TCPServer<T>
{ {
if (client->_cmdParameters->count() > 0) if (client->_cmdParameters->count() > 0)
{ {
uint16_t dirNameSize(0), paramCount(client->_cmdParameters->count()); uint16_t dirPathSize(0) /*dir path plus dirname*/, paramCount(client->_cmdParameters->count());
char *dirName(NULL); char *dirNameWithPath(NULL);
for(int i(0); i < paramCount; i++) for(int i(0); i < paramCount; i++)
{ {
dirNameSize += strlen(client->_cmdParameters->getAt(i)->getString()); dirPathSize += strlen(client->_cmdParameters->getAt(i)->getString()) + 1;
if(i != paramCount-1)
dirNameSize++;
} }
dirName = (char *)malloc((sizeof(char)*dirNameSize) + 1); dirPathSize += strlen(client->_currentDirectory) + 1;
if(dirName != NULL)
dirNameWithPath = (char *)malloc( sizeof(char) * dirPathSize );
if(dirNameWithPath != NULL)
{ {
dirName[0] = '\0'; strcpy(dirNameWithPath,client->_currentDirectory);
if(strcmp(dirNameWithPath, "/") != 0)strcat(dirNameWithPath,"/");
for(int i(0); i < paramCount; i++) for(int i(0); i < paramCount; i++)
{ {
strcat(dirName,client->_cmdParameters->getAt(i)->getString()); strcat(dirNameWithPath,client->_cmdParameters->getAt(i)->getString());
if(i != paramCount-1) if(i != paramCount-1)
strcat(dirName," "); strcat(dirNameWithPath," ");
} }
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
Serial.printf("dirName size : %d, dirName #%s#\n",dirNameSize,dirName); Serial.printf("Final dirName : #%s#, size : %d\n",dirNameWithPath, dirPathSize);
#endif #endif
if(strlen(dirName) > 8) if(_sdCardManager->mkdir(dirNameWithPath))
dirName[8] = '\0';
//We have the dir name, we need to append the current directory...
uint16_t pathDirNameLength = strlen(dirName) + strlen(client->_currentDirectory) + /*If we need to add a /*/ (strcmp(client->_currentDirectory,"/") == 0 ? 0 : 1) + 1; //for \0
char *pathWithDirName = (char *)malloc(sizeof(char) * pathDirNameLength);
sprintf(pathWithDirName,"%s%s%s",client->_currentDirectory, strcmp(client->_currentDirectory,"/") == 0 ? "" : "/", dirName);
#ifdef DEBUG_FTPS
Serial.printf("Final dirName and path #%s# #%s# size %d\n",dirName, pathWithDirName, pathDirNameLength);
#endif
if(_sdCardManager->mkdir(strupr(pathWithDirName)))
{ {
client->_client.printf("257 \"%s\"\r\n", pathWithDirName); client->_client.printf("257 \"%s\"\r\n", dirNameWithPath);
} }
else else
client->_client.println("550 Failed to mkdir (no spaces allowed in dir name)."); client->_client.println("550 Failed to mkdir (no spaces allowed in dir name).");
free(pathWithDirName);
free(dirName); free(dirNameWithPath);
} }
else else
{ {
@ -448,15 +499,33 @@ class FTPServer : public TCPServer<T>
if (client->_cmdParameters->count() > 0) if (client->_cmdParameters->count() > 0)
{ {
//We have the dir name, we need to append the current directory... //We have the dir name, we need to append the current directory...
uint16_t pathDirNameLength = strlen(client->_cmdParameters->getAt(0)->getString()) + strlen(client->_currentDirectory) + /*If we need to add a /*/ (strcmp(client->_currentDirectory,"/") == 0 ? 0 : 1) + 1; //for \0 uint16_t dirPathSize(0) /*dir path plus dirname*/, paramCount(client->_cmdParameters->count());
char *pathWithDirName = (char *)malloc(sizeof(char) * pathDirNameLength); char *dirNameWithPath(NULL);
sprintf(pathWithDirName,"%s%s%s",client->_currentDirectory, strcmp(client->_currentDirectory,"/") == 0 ? "" : "/", client->_cmdParameters->getAt(0)->getString()); for(int i(0); i < paramCount; i++)
{
dirPathSize += strlen(client->_cmdParameters->getAt(i)->getString()) + 1;
}
dirPathSize += strlen(client->_currentDirectory) + 1;
dirNameWithPath = (char *)malloc( sizeof(char) * dirPathSize );
strcpy(dirNameWithPath,client->_currentDirectory);
if(strcmp(dirNameWithPath, "/") != 0)strcat(dirNameWithPath,"/");
for(int i(0); i < paramCount; i++)
{
strcat(dirNameWithPath,client->_cmdParameters->getAt(i)->getString());
if(i != paramCount-1)
strcat(dirNameWithPath," ");
}
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
Serial.printf("pathDirName to delete : #%s#\n",pathWithDirName); Serial.printf("pathDirName to delete : #%s#, size : %d\n",dirNameWithPath, dirPathSize);
#endif #endif
if(_sdCardManager->rmdir(pathWithDirName)) if(_sdCardManager->rmdir(dirNameWithPath))
{ {
client->_client.println("250 Requested file action okay."); client->_client.println("250 Requested file action okay.");
} }
@ -465,7 +534,7 @@ class FTPServer : public TCPServer<T>
client->_client.println("550 Requested action not taken."); client->_client.println("550 Requested action not taken.");
} }
free(pathWithDirName); free(dirNameWithPath);
} }
else else
{ {
@ -477,18 +546,35 @@ class FTPServer : public TCPServer<T>
if (client->_cmdParameters->count() > 0) if (client->_cmdParameters->count() > 0)
{ {
//We save the file path to be sent //We save the file path to be sent
uint16_t filePathSize(0) /*dir path plus dirname*/, paramCount(client->_cmdParameters->count());
char *fileNameWithPath(NULL);
for(int i(0); i < paramCount; i++)
{
filePathSize += strlen(client->_cmdParameters->getAt(i)->getString()) + 1;
}
filePathSize += strlen(client->_currentDirectory) + 1;
fileNameWithPath = (char *)malloc( sizeof(char) * filePathSize );
strcpy(fileNameWithPath,client->_currentDirectory);
if(strcmp(fileNameWithPath, "/") != 0)strcat(fileNameWithPath,"/");
for(int i(0); i < paramCount; i++)
{
strcat(fileNameWithPath,client->_cmdParameters->getAt(i)->getString());
if(i != paramCount-1)
strcat(fileNameWithPath," ");
}
client->_client.println("150 File status okay; about to open data connection."); client->_client.println("150 File status okay; about to open data connection.");
char *temp = (char *)malloc((sizeof(char) * strlen(client->_currentDirectory)) + (sizeof(char) * strlen(_83FileNameFormat(client->_cmdParameters->getAt(0)->getString()))) + (strcmp(client->_currentDirectory,"/") == 0 ? 0 : 1) + 1); client->setCurrentFile(fileNameWithPath);
strcpy(temp, client->_currentDirectory);
if(strcmp(client->_currentDirectory,"/") != 0 )strcat(temp, "/");
strcat(temp, client->_cmdParameters->getAt(0)->getString());
client->setCurrentFile(temp);
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
Serial.printf("File to store : %s\n", client->_currentFile); Serial.printf("File to store : #%s#, size : %d\n", client->_currentFile, filePathSize);
#endif #endif
free(temp); free(fileNameWithPath);
client->_dataTransferPending = STOR_DF; client->_dataTransferPending = STOR_DF;
} }
} }
@ -500,16 +586,34 @@ class FTPServer : public TCPServer<T>
{ {
if (client->_cmdParameters->count() > 0) if (client->_cmdParameters->count() > 0)
{ {
//We have the dir name, we need to append the current directory... //We have the file name, we need to append the current directory...
uint16_t pathDirNameLength = strlen(client->_cmdParameters->getAt(0)->getString()) + strlen(client->_currentDirectory) + /*If we need to add a /*/ (strcmp(client->_currentDirectory,"/") == 0 ? 0 : 1) + 1; //for \0 uint16_t filePathSize(0) /*dir path plus dirname*/, paramCount(client->_cmdParameters->count());
char *pathWithDirName = (char *)malloc(sizeof(char) * pathDirNameLength); char *fileNameWithPath(NULL);
sprintf(pathWithDirName,"%s%s%s",client->_currentDirectory, strcmp(client->_currentDirectory,"/") == 0 ? "" : "/", client->_cmdParameters->getAt(0)->getString()); for(int i(0); i < paramCount; i++)
{
filePathSize += strlen(client->_cmdParameters->getAt(i)->getString()) + 1;
}
filePathSize += strlen(client->_currentDirectory) + 1;
fileNameWithPath = (char *)malloc( sizeof(char) * filePathSize );
strcpy(fileNameWithPath,client->_currentDirectory);
if(strcmp(fileNameWithPath, "/") != 0)strcat(fileNameWithPath,"/");
for(int i(0); i < paramCount; i++)
{
strcat(fileNameWithPath,client->_cmdParameters->getAt(i)->getString());
if(i != paramCount-1)
strcat(fileNameWithPath," ");
}
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
Serial.printf("file to delete : #%s#\n",pathWithDirName); Serial.printf("file to delete : #%s#, size : %d\n",fileNameWithPath,filePathSize);
#endif #endif
if(_sdCardManager->remove(pathWithDirName)) if(_sdCardManager->remove(fileNameWithPath))
{ {
client->_client.println("250 Requested file action okay."); client->_client.println("250 Requested file action okay.");
} }
@ -518,7 +622,7 @@ class FTPServer : public TCPServer<T>
client->_client.println("550 Requested action not taken."); client->_client.println("550 Requested action not taken.");
} }
free(pathWithDirName); free(fileNameWithPath);
} }
else else
{ {
@ -576,9 +680,9 @@ class FTPServer : public TCPServer<T>
Serial.printf("Directory : %s\n",client->_currentDirectory); Serial.printf("Directory : %s\n",client->_currentDirectory);
#endif #endif
File currentDirectory = _sdCardManager->open(client->_currentDirectory); File currentDirectory = _sdCardManager->open(client->_currentDirectory);
currentDirectory.rewindDirectory();
if (currentDirectory) if (currentDirectory)
{ {
currentDirectory.rewindDirectory();
while (true) //Maybe be remove in the future to improve responsiveness while (true) //Maybe be remove in the future to improve responsiveness
{ {
File fileOrDir = currentDirectory.openNextFile(); File fileOrDir = currentDirectory.openNextFile();
@ -616,7 +720,7 @@ class FTPServer : public TCPServer<T>
{ {
if (client->_currentFile != NULL) if (client->_currentFile != NULL)
{ {
char sendBuffer[READ_WRITE_BUFFER_SIZE]; uint8_t sendBuffer[READ_WRITE_BUFFER_SIZE];
File fileToSend = _sdCardManager->open(client->_currentFile); File fileToSend = _sdCardManager->open(client->_currentFile);
if (fileToSend) if (fileToSend)

View File

@ -6,32 +6,31 @@ SDCardManager::SDCardManager()
double SDCardManager::getSize(const SizeUnit sizeUnit) double SDCardManager::getSize(const SizeUnit sizeUnit)
{ {
long numberOf512ByteChunks = cardSize(); uint64_t numberOf512BytesChunks = blocksPerCluster() * totalClusters();//cardSize();
long unit = 0;
double result = 0; double result = 0;
switch(sizeUnit) switch(sizeUnit)
{ {
case KBIT: case KBIT:
result = (double)numberOf512ByteChunks/2.0*8; result = (double)numberOf512BytesChunks/2.0*8;
break; break;
case KBYTE: case KBYTE:
result = (double)numberOf512ByteChunks/2.0; result = (double)numberOf512BytesChunks/2.0;
break; break;
case MBIT: case MBIT:
result = (double)numberOf512ByteChunks/2.0/1024.0*8; result = (double)numberOf512BytesChunks/2.0/1024.0*8;
break; break;
case MBYTE: case MBYTE:
result = (double)numberOf512ByteChunks/2.0/1024.0; result = (double)numberOf512BytesChunks/2.0/1024.0;
break; break;
case GBIT: case GBIT:
result = (double)numberOf512ByteChunks/2.0/1024.0/1024.0*8; result = (double)numberOf512BytesChunks/2.0/1024.0/1024.0*8;
break; break;
case GBYTE: case GBYTE:
result = (double)numberOf512ByteChunks/2.0/1024.0/1024.0; result = (double)numberOf512BytesChunks/2.0/1024.0/1024.0;
break; break;
default: default:
result = (double)numberOf512ByteChunks/2.0/1024.0/1024.0; result = (double)numberOf512BytesChunks/2.0/1024.0/1024.0;
} }
return result; return result;

View File

@ -6,7 +6,7 @@
#include "TCPClient.h" #include "TCPClient.h"
#define MAX_CLIENT -1 #define MAX_CLIENT -1
#define DEBUG_TCPS //#define DEBUG_TCPS
template <typename T> template <typename T>

View File

@ -4,7 +4,8 @@
#include "TCPServer.h" #include "TCPServer.h"
#include "Dictionary.h" #include "Dictionary.h"
#include "SDCardManager.h" #include "SDCardManager.h"
//#define DEBUG_WEBS #define DEBUG_WEBS
#define READ_WRITE_BUFFER_SIZE 2500
template <typename T> template <typename T>
class WEBServer : public TCPServer<T> class WEBServer : public TCPServer<T>
@ -330,7 +331,8 @@ class WEBServer : public TCPServer<T>
if(_sdCardManager != NULL) if(_sdCardManager != NULL)
{ {
File pageToSend; File pageToSend;
char *filePath(NULL), *header(NULL), sendBuffer[2048]; char *filePath(NULL), *header(NULL);
uint8_t sendBuffer[READ_WRITE_BUFFER_SIZE];
int readBytes(0); int readBytes(0);
//We check what kind of http verb it is //We check what kind of http verb it is
switch(client->_httpRequestData.HRM) switch(client->_httpRequestData.HRM)
@ -375,8 +377,6 @@ class WEBServer : public TCPServer<T>
Serial.println(pageToSend.size()); Serial.println(pageToSend.size());
Serial.print("FILE NAME : "); Serial.print("FILE NAME : ");
Serial.println(pageToSend.name()); Serial.println(pageToSend.name());
Serial.print("FILE EXTENSION : ");
Serial.println(getFileExtension(pageToSend.name()));
#endif #endif
if(pageToSend.isDirectory()) //To DO : List the files present in the directory if(pageToSend.isDirectory()) //To DO : List the files present in the directory
@ -389,7 +389,18 @@ class WEBServer : public TCPServer<T>
if(client->_fileSentBytes == 0) if(client->_fileSentBytes == 0)
{ {
header = getHTTPHeader(getMIMETypeByExtension(getFileExtension(pageToSend.name())), pageToSend.size()); char *fileName = (char *) malloc(sizeof(char) * strlen(pageToSend.name()) + 1);
if(fileName != NULL)strcpy(fileName, pageToSend.name());
header = getHTTPHeader(getMIMETypeByExtension(strlwr(getFileExtension(fileName))), pageToSend.size());
#ifdef DEBUG_WEBS
Serial.print("FILE EXTENSION : ");
Serial.println(getFileExtension(fileName));
#endif
free(fileName);
if(header == NULL) if(header == NULL)
{ {
sendInfoResponse(HTTP_CODE::_500, client, "Failed to allocate memory for the header"); sendInfoResponse(HTTP_CODE::_500, client, "Failed to allocate memory for the header");
@ -407,9 +418,14 @@ class WEBServer : public TCPServer<T>
if(pageToSend.available()) if(pageToSend.available())
{ {
readBytes = pageToSend.read(sendBuffer,2048); readBytes = pageToSend.read(sendBuffer,READ_WRITE_BUFFER_SIZE);
client->_client.write(sendBuffer, readBytes); client->_client.write(sendBuffer, readBytes);
#ifdef DEBUG_WEBS
Serial.print("BYTES SENT : ");
Serial.println(readBytes);
#endif
client->_fileSentBytes += readBytes; //We save the number of bytes sent so that we can reopen the file to this position later on. client->_fileSentBytes += readBytes; //We save the number of bytes sent so that we can reopen the file to this position later on.
} }
else else
@ -547,9 +563,7 @@ class WEBServer : public TCPServer<T>
static char *getFileExtension(char *name) static char *getFileExtension(char *name)
{ {
if(name == NULL)return NULL; char *p(lastIndexOf(name, '.'));
char *p(strchr(name, '.'));
return p != NULL ? p+1 : NULL; return p != NULL ? p+1 : NULL;
} }

View File

@ -356,10 +356,10 @@ boolean WEBServerManager::sendPageToClientFromSdCard(WiFiClient *wifiClient)
Serial.print("FILE NAME : "); Serial.print("FILE NAME : ");
Serial.println(pageToSend.name()); Serial.println(pageToSend.name());
Serial.print("FILE EXTENSION : "); Serial.print("FILE EXTENSION : ");
Serial.println(getFileExtension(pageToSend.name())); //Serial.println(getFileExtension(pageToSend.name()));
#endif #endif
header = getHTTPHeader(getMIMETypeByExtension(getFileExtension(pageToSend.name())), pageToSend.size()); header = getHTTPHeader(getMIMETypeByExtension(getFileExtension(/*pageToSend.name()*/ "dummy")), pageToSend.size());
if(header == NULL) if(header == NULL)
{ {
wifiClient->print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Failed to malloc header</h1>\r\n</html>")); wifiClient->print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Failed to malloc header</h1>\r\n</html>"));
@ -372,7 +372,7 @@ boolean WEBServerManager::sendPageToClientFromSdCard(WiFiClient *wifiClient)
while(pageToSend.available()) while(pageToSend.available())
{ {
if(wifiClient->write(sendBuffer, pageToSend.read(sendBuffer,2048)) == 0) //if(wifiClient->write(sendBuffer, pageToSend.read(sendBuffer,2048)) == 0)
break; break;
} }