From b272ab0f229fdb8b814a4bec16d4cd73bb57ecba Mon Sep 17 00:00:00 2001 From: anschrammh Date: Fri, 18 Oct 2019 01:06:59 +0200 Subject: [PATCH] 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 --- src/app/FTPServer.h | 240 +++++++++++++++++++++++++---------- src/app/SDCardManager.cpp | 17 ++- src/app/TCPServer.h | 2 +- src/app/WEBServer.h | 32 +++-- src/app/WEBServerManager.cpp | 6 +- 5 files changed, 207 insertions(+), 90 deletions(-) diff --git a/src/app/FTPServer.h b/src/app/FTPServer.h index ceaa440..51f7ddb 100644 --- a/src/app/FTPServer.h +++ b/src/app/FTPServer.h @@ -5,7 +5,7 @@ #include "SDCardManager.h" #include "definition.h" #define DEBUG_FTPS -#define READ_WRITE_BUFFER_SIZE 2048 +#define READ_WRITE_BUFFER_SIZE 2500 template class FTPServer : public TCPServer @@ -98,9 +98,15 @@ class FTPServer : public TCPServer if (client->_dataClient.connected()) { client->_client.println("150 File status okay;"); - sendFSTree(client); - - client->_client.println("226 Closing data connection."); + if(sendFSTree(client)) + { + client->_client.println("226 Closing data connection."); + } + else + { + client->_client.println("451 Requested action aborted."); + } + client->_dataClient.stop(); client->_dataTransferPending = NONE; } @@ -143,7 +149,10 @@ class FTPServer : public TCPServer break; case STOR_DF : if (client->_dataClient.connected() || client->_dataClient.available()) - { + { + if(client->_dataClient.connected()) + client->_fileIsBeeingReceived = true; + if(client->_dataClient.available()) { #ifdef DEBUG_FTPS @@ -165,15 +174,28 @@ class FTPServer : public TCPServer client->_fileIsBeeingReceived = false; client->_dataTransferPending = NONE; } - else - client->_fileIsBeeingReceived = true; } } else if(client->_fileIsBeeingReceived) { - #ifdef DEBUG_FTPS - Serial.println("Whole file received"); - #endif + 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 + Serial.println("Whole file received"); + #endif + } client->_client.println("226 Closing data connection."); @@ -344,12 +366,32 @@ class FTPServer : public TCPServer } 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); 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 - Serial.printf("Final dir : %s\n",temp); + Serial.printf("Final dir : %s, size : %d --> %d\n",temp, dirNameSize, strlen(temp)); #endif client->setCurrentDirectory(temp); free(temp); @@ -367,11 +409,32 @@ class FTPServer : public TCPServer if (client->_cmdParameters->count() > 0) { //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); - strcpy(temp, client->_currentDirectory); - if(strcmp(client->_currentDirectory,"/") != 0 )strcat(temp, "/"); - strcat(temp, client->_cmdParameters->getAt(0)->getString()); + uint16_t filePathSize(0), paramCount(client->_cmdParameters->count()); + for(int i(0); i < paramCount; i++) + { + 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); #ifdef DEBUG_FTPS Serial.printf("File to donwload : %s\n", temp); @@ -384,54 +447,42 @@ class FTPServer : public TCPServer { if (client->_cmdParameters->count() > 0) { - uint16_t dirNameSize(0), paramCount(client->_cmdParameters->count()); - char *dirName(NULL); + uint16_t dirPathSize(0) /*dir path plus dirname*/, paramCount(client->_cmdParameters->count()); + char *dirNameWithPath(NULL); for(int i(0); i < paramCount; i++) { - dirNameSize += strlen(client->_cmdParameters->getAt(i)->getString()); - - if(i != paramCount-1) - dirNameSize++; + dirPathSize += strlen(client->_cmdParameters->getAt(i)->getString()) + 1; } - dirName = (char *)malloc((sizeof(char)*dirNameSize) + 1); - if(dirName != NULL) + dirPathSize += strlen(client->_currentDirectory) + 1; + + 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++) { - strcat(dirName,client->_cmdParameters->getAt(i)->getString()); + strcat(dirNameWithPath,client->_cmdParameters->getAt(i)->getString()); if(i != paramCount-1) - strcat(dirName," "); + strcat(dirNameWithPath," "); } #ifdef DEBUG_FTPS - Serial.printf("dirName size : %d, dirName #%s#\n",dirNameSize,dirName); + Serial.printf("Final dirName : #%s#, size : %d\n",dirNameWithPath, dirPathSize); #endif - if(strlen(dirName) > 8) - 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))) + if(_sdCardManager->mkdir(dirNameWithPath)) { - client->_client.printf("257 \"%s\"\r\n", pathWithDirName); + client->_client.printf("257 \"%s\"\r\n", dirNameWithPath); } else client->_client.println("550 Failed to mkdir (no spaces allowed in dir name)."); - free(pathWithDirName); - free(dirName); + + free(dirNameWithPath); } else { @@ -448,15 +499,33 @@ class FTPServer : public TCPServer if (client->_cmdParameters->count() > 0) { //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 - char *pathWithDirName = (char *)malloc(sizeof(char) * pathDirNameLength); - sprintf(pathWithDirName,"%s%s%s",client->_currentDirectory, strcmp(client->_currentDirectory,"/") == 0 ? "" : "/", client->_cmdParameters->getAt(0)->getString()); + uint16_t dirPathSize(0) /*dir path plus dirname*/, paramCount(client->_cmdParameters->count()); + char *dirNameWithPath(NULL); + 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 - Serial.printf("pathDirName to delete : #%s#\n",pathWithDirName); + Serial.printf("pathDirName to delete : #%s#, size : %d\n",dirNameWithPath, dirPathSize); #endif - if(_sdCardManager->rmdir(pathWithDirName)) + if(_sdCardManager->rmdir(dirNameWithPath)) { client->_client.println("250 Requested file action okay."); } @@ -465,7 +534,7 @@ class FTPServer : public TCPServer client->_client.println("550 Requested action not taken."); } - free(pathWithDirName); + free(dirNameWithPath); } else { @@ -477,18 +546,35 @@ class FTPServer : public TCPServer if (client->_cmdParameters->count() > 0) { //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."); - 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); - strcpy(temp, client->_currentDirectory); - if(strcmp(client->_currentDirectory,"/") != 0 )strcat(temp, "/"); - strcat(temp, client->_cmdParameters->getAt(0)->getString()); + client->setCurrentFile(fileNameWithPath); - client->setCurrentFile(temp); #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 - free(temp); + free(fileNameWithPath); client->_dataTransferPending = STOR_DF; } } @@ -500,16 +586,34 @@ class FTPServer : public TCPServer { if (client->_cmdParameters->count() > 0) { - //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 - char *pathWithDirName = (char *)malloc(sizeof(char) * pathDirNameLength); - sprintf(pathWithDirName,"%s%s%s",client->_currentDirectory, strcmp(client->_currentDirectory,"/") == 0 ? "" : "/", client->_cmdParameters->getAt(0)->getString()); + //We have the file name, we need to append the current directory... + 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," "); + } + #ifdef DEBUG_FTPS - Serial.printf("file to delete : #%s#\n",pathWithDirName); + Serial.printf("file to delete : #%s#, size : %d\n",fileNameWithPath,filePathSize); #endif - if(_sdCardManager->remove(pathWithDirName)) + if(_sdCardManager->remove(fileNameWithPath)) { client->_client.println("250 Requested file action okay."); } @@ -518,7 +622,7 @@ class FTPServer : public TCPServer client->_client.println("550 Requested action not taken."); } - free(pathWithDirName); + free(fileNameWithPath); } else { @@ -576,9 +680,9 @@ class FTPServer : public TCPServer Serial.printf("Directory : %s\n",client->_currentDirectory); #endif File currentDirectory = _sdCardManager->open(client->_currentDirectory); - currentDirectory.rewindDirectory(); if (currentDirectory) { + currentDirectory.rewindDirectory(); while (true) //Maybe be remove in the future to improve responsiveness { File fileOrDir = currentDirectory.openNextFile(); @@ -616,7 +720,7 @@ class FTPServer : public TCPServer { if (client->_currentFile != NULL) { - char sendBuffer[READ_WRITE_BUFFER_SIZE]; + uint8_t sendBuffer[READ_WRITE_BUFFER_SIZE]; File fileToSend = _sdCardManager->open(client->_currentFile); if (fileToSend) diff --git a/src/app/SDCardManager.cpp b/src/app/SDCardManager.cpp index 706fe90..184d125 100644 --- a/src/app/SDCardManager.cpp +++ b/src/app/SDCardManager.cpp @@ -6,32 +6,31 @@ SDCardManager::SDCardManager() double SDCardManager::getSize(const SizeUnit sizeUnit) { - long numberOf512ByteChunks = cardSize(); - long unit = 0; + uint64_t numberOf512BytesChunks = blocksPerCluster() * totalClusters();//cardSize(); double result = 0; switch(sizeUnit) { case KBIT: - result = (double)numberOf512ByteChunks/2.0*8; + result = (double)numberOf512BytesChunks/2.0*8; break; case KBYTE: - result = (double)numberOf512ByteChunks/2.0; + result = (double)numberOf512BytesChunks/2.0; break; case MBIT: - result = (double)numberOf512ByteChunks/2.0/1024.0*8; + result = (double)numberOf512BytesChunks/2.0/1024.0*8; break; case MBYTE: - result = (double)numberOf512ByteChunks/2.0/1024.0; + result = (double)numberOf512BytesChunks/2.0/1024.0; break; case GBIT: - result = (double)numberOf512ByteChunks/2.0/1024.0/1024.0*8; + result = (double)numberOf512BytesChunks/2.0/1024.0/1024.0*8; break; case GBYTE: - result = (double)numberOf512ByteChunks/2.0/1024.0/1024.0; + result = (double)numberOf512BytesChunks/2.0/1024.0/1024.0; break; default: - result = (double)numberOf512ByteChunks/2.0/1024.0/1024.0; + result = (double)numberOf512BytesChunks/2.0/1024.0/1024.0; } return result; diff --git a/src/app/TCPServer.h b/src/app/TCPServer.h index a77939c..180bcdc 100644 --- a/src/app/TCPServer.h +++ b/src/app/TCPServer.h @@ -6,7 +6,7 @@ #include "TCPClient.h" #define MAX_CLIENT -1 -#define DEBUG_TCPS +//#define DEBUG_TCPS template diff --git a/src/app/WEBServer.h b/src/app/WEBServer.h index f47fdc9..38f072a 100644 --- a/src/app/WEBServer.h +++ b/src/app/WEBServer.h @@ -4,7 +4,8 @@ #include "TCPServer.h" #include "Dictionary.h" #include "SDCardManager.h" -//#define DEBUG_WEBS +#define DEBUG_WEBS +#define READ_WRITE_BUFFER_SIZE 2500 template class WEBServer : public TCPServer @@ -330,7 +331,8 @@ class WEBServer : public TCPServer if(_sdCardManager != NULL) { File pageToSend; - char *filePath(NULL), *header(NULL), sendBuffer[2048]; + char *filePath(NULL), *header(NULL); + uint8_t sendBuffer[READ_WRITE_BUFFER_SIZE]; int readBytes(0); //We check what kind of http verb it is switch(client->_httpRequestData.HRM) @@ -375,8 +377,6 @@ class WEBServer : public TCPServer Serial.println(pageToSend.size()); Serial.print("FILE NAME : "); Serial.println(pageToSend.name()); - Serial.print("FILE EXTENSION : "); - Serial.println(getFileExtension(pageToSend.name())); #endif if(pageToSend.isDirectory()) //To DO : List the files present in the directory @@ -389,7 +389,18 @@ class WEBServer : public TCPServer 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) { sendInfoResponse(HTTP_CODE::_500, client, "Failed to allocate memory for the header"); @@ -407,9 +418,14 @@ class WEBServer : public TCPServer if(pageToSend.available()) { - readBytes = pageToSend.read(sendBuffer,2048); + readBytes = pageToSend.read(sendBuffer,READ_WRITE_BUFFER_SIZE); 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. } else @@ -547,9 +563,7 @@ class WEBServer : public TCPServer static char *getFileExtension(char *name) { - if(name == NULL)return NULL; - - char *p(strchr(name, '.')); + char *p(lastIndexOf(name, '.')); return p != NULL ? p+1 : NULL; } diff --git a/src/app/WEBServerManager.cpp b/src/app/WEBServerManager.cpp index 404ef9c..6ff1459 100644 --- a/src/app/WEBServerManager.cpp +++ b/src/app/WEBServerManager.cpp @@ -356,10 +356,10 @@ boolean WEBServerManager::sendPageToClientFromSdCard(WiFiClient *wifiClient) Serial.print("FILE NAME : "); Serial.println(pageToSend.name()); Serial.print("FILE EXTENSION : "); - Serial.println(getFileExtension(pageToSend.name())); + //Serial.println(getFileExtension(pageToSend.name())); #endif - header = getHTTPHeader(getMIMETypeByExtension(getFileExtension(pageToSend.name())), pageToSend.size()); + header = getHTTPHeader(getMIMETypeByExtension(getFileExtension(/*pageToSend.name()*/ "dummy")), pageToSend.size()); if(header == NULL) { wifiClient->print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n\r\n\r\n

Failed to malloc header

\r\n")); @@ -372,7 +372,7 @@ boolean WEBServerManager::sendPageToClientFromSdCard(WiFiClient *wifiClient) while(pageToSend.available()) { - if(wifiClient->write(sendBuffer, pageToSend.read(sendBuffer,2048)) == 0) + //if(wifiClient->write(sendBuffer, pageToSend.read(sendBuffer,2048)) == 0) break; }