FTP client library for mbed-os

Revision:
1:e069c405c934
Parent:
0:c2da359279a6
Child:
2:88b7399c8260
--- a/FTPClient.cpp	Wed Feb 07 07:01:38 2018 +0000
+++ b/FTPClient.cpp	Tue Sep 18 08:01:30 2018 +0000
@@ -19,6 +19,14 @@
 
 #define FTP_BUF_SIZE     (1460)
 
+//#define ftp_debug_print  printf
+
+#ifndef ftp_debug_print
+static int ftp_debug_print(const char *format, ...) {
+    return 0;
+}
+#endif
+
 FTPClient::FTPClient(NetworkInterface *net, const char* root) {
     _ctr_open = false;
     _login = false;
@@ -38,14 +46,17 @@
 
     FTPClientControlSock.open(p_network);
     if (FTPClientControlSock.connect(ip_addr, port) < 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     _ctr_open = true;
 
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if (strncmp(p_ftp_buf, "220", 3) != 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
@@ -53,18 +64,22 @@
     sprintf(p_ftp_buf, "user %s\r\n", user);
     FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if (strncmp(p_ftp_buf, "331", 3) != 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
     sprintf(p_ftp_buf, "pass %s\r\n", pass);
     FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if (strncmp(p_ftp_buf, "230", 3) != 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     _login = true;
@@ -73,15 +88,18 @@
 
 bool FTPClient::quit() {
     if (!_login) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
     sprintf(p_ftp_buf, "quit\r\n");
     FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if (strncmp(p_ftp_buf, "250", 3) != 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     _login = false;
@@ -96,10 +114,12 @@
     int size;
 
     if (!_login) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
     if (!open_data_sock()) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
@@ -107,9 +127,11 @@
     sprintf(p_ftp_buf, "retr %s\r\n", file_name);
     FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if ((strncmp(p_ftp_buf, "150", 3) != 0) && (strncmp(p_ftp_buf, "125", 3) != 0)) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
@@ -127,9 +149,11 @@
     FTPClientDataSock.close();
 
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if (strncmp(p_ftp_buf, "226", 3) != 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     return true;
@@ -141,10 +165,12 @@
     int32_t send_size;
 
     if (!_login) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
     if (!open_data_sock()) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
@@ -152,9 +178,11 @@
     sprintf(p_ftp_buf, "stor %s\r\n", file_name);
     FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if ((strncmp(p_ftp_buf, "150", 3) != 0) && (strncmp(p_ftp_buf, "125", 3) != 0)) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
@@ -178,9 +206,11 @@
     FTPClientDataSock.close();
 
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if (strncmp(p_ftp_buf, "226", 3) != 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     return true;
@@ -188,15 +218,18 @@
 
 bool FTPClient::del(const char* file_name) {
     if (!_login) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
     sprintf(p_ftp_buf, "dele %s\r\n", file_name);
     FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if (strncmp(p_ftp_buf, "250", 3) != 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
@@ -205,15 +238,18 @@
 
 bool FTPClient::mkdir(const char* dir_name) {
     if (!_login) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
     sprintf(p_ftp_buf, "xmkd %s\r\n", dir_name);
     FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if (strncmp(p_ftp_buf, "257", 3) != 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
@@ -222,15 +258,18 @@
 
 bool FTPClient::cd(const char* dir_name) {
     if (!_login) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
     sprintf(p_ftp_buf, "cwd %s\r\n", dir_name);
     FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if (strncmp(p_ftp_buf, "250", 3) != 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
@@ -243,15 +282,18 @@
     int remain_size = buf_size - 1;
 
     if (list_buf == NULL) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     list_buf[0] = '\0';
 
     if (!_login) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
     if (!open_data_sock()) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
@@ -259,9 +301,11 @@
     sprintf(p_ftp_buf, "list\r\n");
     FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if ((strncmp(p_ftp_buf, "150", 3) != 0) && (strncmp(p_ftp_buf, "125", 3) != 0)) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
@@ -282,9 +326,11 @@
     FTPClientDataSock.close();
 
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if (strncmp(p_ftp_buf, "226", 3) != 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     return true;
@@ -296,15 +342,18 @@
     int remain_size = buf_size - 1;
 
     if (list_buf == NULL) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     list_buf[0] = '\0';
 
     if (!_login) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
     if (!open_data_sock()) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
@@ -312,9 +361,11 @@
     sprintf(p_ftp_buf, "nlst\r\n");
     FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if ((strncmp(p_ftp_buf, "150", 3) != 0) && (strncmp(p_ftp_buf, "125", 3) != 0)) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
@@ -335,9 +386,11 @@
     FTPClientDataSock.close();
 
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if (strncmp(p_ftp_buf, "226", 3) != 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     return true;
@@ -353,9 +406,11 @@
     sprintf(p_ftp_buf, "pasv\r\n");
     FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
     if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
     if (strncmp(p_ftp_buf, "227", 3) != 0) {
+        ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
         return false;
     }
 
@@ -365,6 +420,7 @@
         ip_addr[i] = (uint8_t)atoi(token);
         token = savept;
         if (token == NULL) {
+            ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
             return false;
         }
     }
@@ -375,6 +431,7 @@
         remote_port += atoi(token);
         token = savept;
         if (token == NULL) {
+            ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
             return false;
         }
     }
@@ -387,6 +444,7 @@
         }
         wait(1);
     }
+    ftp_debug_print("ERROR: %s(%d)\r\n", __FILE__, __LINE__);
     return false;
 }