Skip to content

Commit

Permalink
Add non blocking functions for FTP download/upload
Browse files Browse the repository at this point in the history
non blocking function can be useful for large file transfer
  • Loading branch information
tryhus committed Jan 10, 2019
1 parent 810e98c commit a461e16
Show file tree
Hide file tree
Showing 6 changed files with 565 additions and 256 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#exclude everything except some directory
*.suo
*.d
*.o
**/Release/**
**/__vm/**
project/**/Debug/**
project/**/Release/**
project/**/__vm/**
packages/arduino/tools
staging
103 changes: 87 additions & 16 deletions examples/FTP/FTP.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ const char GPRS_APN[] = SECRET_GPRS_APN;
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;

//this file must be present in the remote directory SECRET_FTP_REMOTE_DIR
const String c_downloadFileName = "downloadFile";

// initialize the library instance
GSMFileSytem fileSystem;
GSMFTP ftp;
GPRS gprs;
GSM gsmAccess;
Expand Down Expand Up @@ -57,19 +59,22 @@ void setup() {
}

void loop() {
GSMFileSystemElem localFile;
GSMFTPElem remoteFile;

Serial.println("Connect to FTP server.");
if (ftp.connect(SECRET_FTP_HOST, SECRET_FTP_USER, SECRET_FTP_PASSWORD, SECRET_FTP_PORT) == false) {
Serial.println("Failed to Connect to FTP server.");
ftp.printError();
}

Serial.println("Change of directory");
if (ftp.cd(SECRET_FTP_REMOTE_DIR) == false) {
Serial.println("Failed to change of directory.");
}

Serial.print("Free space ");
Serial.println(fileSystem.freeSpace());
Serial.println(FILESYSTEM.freeSpace());

Serial.println("Create remote directory : test");
if (ftp.mkdir("test") == false) {
Expand All @@ -84,7 +89,7 @@ void loop() {
Serial.println("Write a binary file in local memory");
double valueWR = -12.5789876;
double valueRD = 0;
if (fileSystem.write("myFile", &valueWR, sizeof(valueWR)) == false) {
if (FILESYSTEM.write("myFile", &valueWR, sizeof(valueWR)) == false) {
Serial.println("Failed to write file");
}

Expand All @@ -95,39 +100,39 @@ void loop() {
}

Serial.println("Retreive the file from the server to local memory");
if (ftp.download("myFileToServer", "myFileToLocalMemory") == false) {
if (ftp.download("myFileToLocalMemory", "myFileToServer") == false) {
Serial.println("Failed to download the file.");
ftp.printError();
}

Serial.println("Check that the original file is identical to the one that was received");
if (fileSystem.read("myFileToLocalMemory", &valueRD, sizeof(valueRD)) == false) {
if (FILESYSTEM.read("myFileToLocalMemory", &valueRD, sizeof(valueRD)) == false) {
Serial.println("Failed to read file");
}
else if (valueWR != valueRD) {
Serial.println("Failed to read file, value is corrupted");
}

Serial.print("Free space ");
Serial.println(fileSystem.freeSpace());
Serial.println(FILESYSTEM.freeSpace());

Serial.println("Display local files");
if (fileSystem.ls(true) == false) {
if (FILESYSTEM.ls(localFile, true) == false) {
Serial.println("Failed to display local files");
}

Serial.println("Remove local files");
for (int i = 0; i < fileSystem.fileCount(); ++i) {
fileSystem.remove(fileSystem.file(i).name);
if (FILESYSTEM.remove(localFile) == false) {
Serial.println("Failed to remove file");
}

Serial.println("Display local files");
if (fileSystem.ls(true) == false) {
if (FILESYSTEM.ls(localFile, true) == false) {
Serial.println("Failed to display local files");
}

Serial.println("Display remote files");
if (ftp.ls(true) == false) {
if (ftp.ls(remoteFile, true) == false) {
Serial.println("Failed to display files.");
}

Expand All @@ -140,16 +145,82 @@ void loop() {
}

Serial.println("Display remote files");
if (ftp.ls(true) == false) {
if (ftp.ls(remoteFile, true) == false) {
Serial.println("Failed to display files.");
}

//--- Test download/upload a large file with non blocking function ---

Serial.println();
Serial.println("Download a file with non blocking function");
downloadFileNonBlocking("downloadedFile", c_downloadFileName);

Serial.println("Display local files");
if (FILESYSTEM.ls(localFile, true) == false) {
Serial.println("Failed to display local files");
}

Serial.println("Upload a file with non blocking function");
uploadFileNonBlocking("downloadedFile", "uploadFile");

Serial.println("Display remote files");
if (ftp.ls(remoteFile, true) == false) {
Serial.println("Failed to display files.");
}

Serial.println("Remove local and remote files");
if (FILESYSTEM.remove("downloadedFile") == false) {
Serial.println("Failed to remove file");
}
if (ftp.removeFile("uploadFile") == false) {
Serial.println("Failed to remove files : myFileToServer.");
}

Serial.println("Disconnect to FTP server");
if (ftp.disconnect() == false) {
Serial.println("Failed to disconnect.");
}

for (;;)
;
}

//Example of non blocking download functions
void downloadFileNonBlocking(const String localFileName, const String remoteFileName) {

Serial.println("Retreive the file from the server to local memory");
//Start download
if (ftp.downloadStart(localFileName, remoteFileName) == false) {
Serial.println("Failed to start download.");
ftp.printError();
}

//update download
while (ftp.downloadReady(localFileName, true) == 0)
{
//do some job
}
}

//Example of non blocking upload functions
void uploadFileNonBlocking(const String localFileName, const String remoteFileName) {

Serial.println("Send the file to the server from local memory");
if (ftp.uploadStart(localFileName, remoteFileName) == false) {
Serial.println("Failed to start upload.");
ftp.printError();
}

int res = 0;
while (res == 0){
res = ftp.uploadReady();
if (res == 1) {
Serial.println("Upload finished.");
}
else if (res < 0) {
Serial.println("Upload error.");
ftp.printError();
}
//do some job
}
}
Loading

0 comments on commit a461e16

Please sign in to comment.