Skip to content

Commit 0159561

Browse files
committed
Add non blocking functions for FTP download/upload
non blocking function can be useful for large file transfer
1 parent 810e98c commit 0159561

File tree

6 files changed

+584
-308
lines changed

6 files changed

+584
-308
lines changed

.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#exclude everything except some directory
2+
*.suo
3+
*.d
4+
*.o
5+
**/Release/**
6+
**/__vm/**
7+
project/**/Debug/**
8+
project/**/Release/**
9+
project/**/__vm/**
10+
packages/arduino/tools
11+
staging

examples/FTP/FTP.ino

+87-16
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ const char GPRS_APN[] = SECRET_GPRS_APN;
2626
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
2727
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
2828

29+
//this file must be present in the remote directory SECRET_FTP_REMOTE_DIR
30+
const String c_downloadFileName = "downloadFile";
31+
2932
// initialize the library instance
30-
GSMFileSytem fileSystem;
3133
GSMFTP ftp;
3234
GPRS gprs;
3335
GSM gsmAccess;
@@ -57,19 +59,22 @@ void setup() {
5759
}
5860

5961
void loop() {
62+
GSMFileSystemElem localFile;
63+
GSMFTPElem remoteFile;
6064

6165
Serial.println("Connect to FTP server.");
6266
if (ftp.connect(SECRET_FTP_HOST, SECRET_FTP_USER, SECRET_FTP_PASSWORD, SECRET_FTP_PORT) == false) {
6367
Serial.println("Failed to Connect to FTP server.");
68+
ftp.printError();
6469
}
65-
70+
6671
Serial.println("Change of directory");
6772
if (ftp.cd(SECRET_FTP_REMOTE_DIR) == false) {
6873
Serial.println("Failed to change of directory.");
6974
}
70-
75+
7176
Serial.print("Free space ");
72-
Serial.println(fileSystem.freeSpace());
77+
Serial.println(FILESYSTEM.freeSpace());
7378

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

@@ -95,39 +100,39 @@ void loop() {
95100
}
96101

97102
Serial.println("Retreive the file from the server to local memory");
98-
if (ftp.download("myFileToServer", "myFileToLocalMemory") == false) {
103+
if (ftp.download("myFileToLocalMemory", "myFileToServer") == false) {
99104
Serial.println("Failed to download the file.");
100105
ftp.printError();
101106
}
102107

103108
Serial.println("Check that the original file is identical to the one that was received");
104-
if (fileSystem.read("myFileToLocalMemory", &valueRD, sizeof(valueRD)) == false) {
109+
if (FILESYSTEM.read("myFileToLocalMemory", &valueRD, sizeof(valueRD)) == false) {
105110
Serial.println("Failed to read file");
106111
}
107112
else if (valueWR != valueRD) {
108113
Serial.println("Failed to read file, value is corrupted");
109114
}
110115

111116
Serial.print("Free space ");
112-
Serial.println(fileSystem.freeSpace());
117+
Serial.println(FILESYSTEM.freeSpace());
113118

114119
Serial.println("Display local files");
115-
if (fileSystem.ls(true) == false) {
120+
if (FILESYSTEM.ls(localFile, true) == false) {
116121
Serial.println("Failed to display local files");
117122
}
118123

119124
Serial.println("Remove local files");
120-
for (int i = 0; i < fileSystem.fileCount(); ++i) {
121-
fileSystem.remove(fileSystem.file(i).name);
125+
if (FILESYSTEM.remove(localFile) == false) {
126+
Serial.println("Failed to remove file");
122127
}
123-
128+
124129
Serial.println("Display local files");
125-
if (fileSystem.ls(true) == false) {
130+
if (FILESYSTEM.ls(localFile, true) == false) {
126131
Serial.println("Failed to display local files");
127132
}
128133

129134
Serial.println("Display remote files");
130-
if (ftp.ls(true) == false) {
135+
if (ftp.ls(remoteFile, true) == false) {
131136
Serial.println("Failed to display files.");
132137
}
133138

@@ -140,16 +145,82 @@ void loop() {
140145
}
141146

142147
Serial.println("Display remote files");
143-
if (ftp.ls(true) == false) {
148+
if (ftp.ls(remoteFile, true) == false) {
149+
Serial.println("Failed to display files.");
150+
}
151+
152+
//--- Test download/upload a large file with non blocking function ---
153+
154+
Serial.println();
155+
Serial.println("Download a file with non blocking function");
156+
downloadFileNonBlocking("downloadedFile", c_downloadFileName);
157+
158+
Serial.println("Display local files");
159+
if (FILESYSTEM.ls(localFile, true) == false) {
160+
Serial.println("Failed to display local files");
161+
}
162+
163+
Serial.println("Upload a file with non blocking function");
164+
uploadFileNonBlocking("downloadedFile", "uploadFile");
165+
166+
Serial.println("Display remote files");
167+
if (ftp.ls(remoteFile, true) == false) {
144168
Serial.println("Failed to display files.");
145169
}
146170

171+
Serial.println("Remove local and remote files");
172+
if (FILESYSTEM.remove("downloadedFile") == false) {
173+
Serial.println("Failed to remove file");
174+
}
175+
if (ftp.removeFile("uploadFile") == false) {
176+
Serial.println("Failed to remove files : myFileToServer.");
177+
}
178+
147179
Serial.println("Disconnect to FTP server");
148180
if (ftp.disconnect() == false) {
149181
Serial.println("Failed to disconnect.");
150182
}
151-
183+
152184
for (;;)
153185
;
154186
}
155187

188+
//Example of non blocking download functions
189+
void downloadFileNonBlocking(const String localFileName, const String remoteFileName) {
190+
191+
Serial.println("Retreive the file from the server to local memory");
192+
//Start download
193+
if (ftp.downloadStart(localFileName, remoteFileName) == false) {
194+
Serial.println("Failed to start download.");
195+
ftp.printError();
196+
}
197+
198+
//update download
199+
while (ftp.downloadReady(localFileName, true) == 0)
200+
{
201+
//do some job
202+
}
203+
}
204+
205+
//Example of non blocking upload functions
206+
void uploadFileNonBlocking(const String localFileName, const String remoteFileName) {
207+
208+
Serial.println("Send the file to the server from local memory");
209+
if (ftp.uploadStart(localFileName, remoteFileName) == false) {
210+
Serial.println("Failed to start upload.");
211+
ftp.printError();
212+
}
213+
214+
int res = 0;
215+
while (res == 0){
216+
res = ftp.uploadReady();
217+
if (res == 1) {
218+
Serial.println("Upload finished.");
219+
}
220+
else if (res < 0) {
221+
Serial.println("Upload error.");
222+
ftp.printError();
223+
}
224+
//do some job
225+
}
226+
}

0 commit comments

Comments
 (0)