Update FTPClient

Dependents:   Digital_Photo_Frame_with_FTP_SD_WIZwiki-W7500 FTP_Streaming_Music_Player_WIZwiki-W7500 GIF2015 MP3Decoding_VS1002_WIZwiki-W7500

Fork of FTPClient by Midnight Cow

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FTPClient.cpp Source File

FTPClient.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "FTPClient.h"
00004 
00005 //#define DEBUG
00006 
00007 FTPClient::FTPClient(PinName mosi, PinName miso, PinName sclk, PinName ssel, const char* root) {
00008     _SDFileSystem = new SDFileSystem(mosi, miso, sclk, ssel, root);
00009     blogin = false;
00010     strcpy(this->root,root);
00011 }
00012 
00013 FTPClient::FTPClient(const char* root)
00014 {
00015     _SDFileSystem = NULL;
00016     blogin = false;
00017     strcpy(this->root,root);
00018 }
00019 
00020 FTPClient::~FTPClient()
00021 {
00022     if(_SDFileSystem == NULL) delete _SDFileSystem;
00023 }
00024 
00025 bool FTPClient::open(char* ip, int port, char* id, char* pass){
00026     
00027     int size;
00028     blogin = false;
00029     if (FTPClientControlSock.connect(ip, port) < 0) {
00030     #ifdef DEBUG
00031         printf("Unable to connect to (%s) on port (%d)\r\n", ip, port);
00032     #endif
00033         return false;
00034     }
00035     
00036     while(!blogin){
00037         size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf));
00038         if(size > 0){
00039         #ifdef DEBUG
00040             printf("Received message from server: %s\r\n", ftpbuf);
00041         #endif
00042             if (!strncmp(ftpbuf, "220", 3)){
00043                 printf("%s\r\n", ftpbuf);
00044                 sprintf(ftpbuf, "user %s\r\n", id);             
00045                 FTPClientControlSock.send(ftpbuf, strlen(ftpbuf));
00046             }
00047             else if (!strncmp(ftpbuf, "331", 3)){
00048                 sprintf(ftpbuf, "pass %s\r\n", pass);             
00049                 FTPClientControlSock.send(ftpbuf, strlen(ftpbuf));
00050             }
00051             else if (!strncmp(ftpbuf, "230", 3)){
00052                 blogin = true;
00053             }
00054             else{
00055                 break;
00056             }
00057         }  
00058     }
00059     return blogin;
00060 }
00061 
00062 bool FTPClient::getfile(char* filename){
00063     FILE* fp;
00064     int size;
00065 
00066     if(blogin){
00067         printf("%08X\r\n",ftpbuf);
00068         sprintf(ftpbuf, "pasv\r\n");             
00069         FTPClientControlSock.send(ftpbuf, strlen(ftpbuf));
00070         
00071         while(1){
00072             size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf));
00073             if(size > 0){
00074             #ifdef DEBUG
00075                 printf("Received message from server: %s\r\n", ftpbuf);
00076             #endif
00077                 if (!strncmp(ftpbuf, "150", 3)){
00078                     sprintf(ftpbuf,"%s/%s\0",root,filename);
00079                 #ifdef DEBUG
00080                     printf("myfilename : %s\r\n", ftpbuf); 
00081                 #endif
00082                     fp = fopen(ftpbuf, "w");
00083                     while(1){
00084                         size = FTPClientDataSock.receive(ftpbuf, sizeof(ftpbuf));
00085                     #ifdef DEBUG
00086                         printf("remain_datasize : %d\r\n", size);
00087                     #endif
00088                         if(size>0){
00089                             fwrite(ftpbuf,size,sizeof(char),fp);
00090                         #ifdef DEBUG
00091                             printf("#");
00092                         #endif
00093                         }
00094                         if(size < 0 || size < MAX_SS) 
00095                         {
00096                             fclose(fp);
00097                             FTPClientDataSock.close();
00098                             break;
00099                         }
00100                     }
00101                 }
00102                 else if (!strncmp(ftpbuf, "227", 3)){
00103                     pportc(ftpbuf); 
00104                     
00105                 #if 1
00106                     while (FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port) < 0) {
00107                     #ifdef DEBUG
00108                         printf("Unable to connect to (%s) on port (%d)\r\n", ftpServer_data_ip_addr_str, remote_port);
00109                     #endif
00110                         wait(1);
00111                     }
00112                 #endif
00113                     
00114                 #if 0
00115                     do{
00116                         FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port);
00117                     }while(!FTPClientDataSock.is_connected());    
00118                 #endif
00119                     sprintf(ftpbuf, "retr %s\r\n", filename);             
00120                     FTPClientControlSock.send(ftpbuf, strlen(ftpbuf));  
00121                 }
00122                 else if(!strncmp(ftpbuf,"226",3)) break;
00123             }  
00124         }
00125         return true;
00126     }
00127     return false;
00128 }
00129 
00130 bool FTPClient::putfile(char* filename){
00131     FILE* fp;
00132     int size;
00133     long int remain_filesize;
00134     long int send_byte;
00135     
00136     if(blogin){
00137         
00138         sprintf(ftpbuf, "pasv\r\n");             
00139         FTPClientControlSock.send(ftpbuf, strlen(ftpbuf));
00140         
00141         while(1){
00142             size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf));
00143             if(size > 0){
00144             #ifdef DEBUG
00145                 printf("Received message from server: %s\r\n", ftpbuf);
00146             #endif
00147                 if (!strncmp(ftpbuf, "150", 3)){
00148                     sprintf(ftpbuf,"%s/%s",root,filename);
00149                     fp = fopen(ftpbuf, "r"); 
00150                     fseek(fp, 0, SEEK_END);            // seek to end of file
00151                     remain_filesize = ftell(fp);       // get current file pointer
00152                     fseek(fp, 0, SEEK_SET);            // seek back to beginning of file  
00153                     do{
00154                         if(remain_filesize > MAX_SS)
00155                             send_byte = MAX_SS;
00156                         else
00157                             send_byte = remain_filesize;
00158                         fread (ftpbuf, 1, send_byte, fp);
00159                         FTPClientDataSock.send(ftpbuf, send_byte);
00160                         remain_filesize -= send_byte;
00161                     #ifdef DEBUG
00162                         printf("#");
00163                     #endif
00164                     }while(remain_filesize!=0);
00165                     fclose(fp); 
00166                     FTPClientDataSock.close();
00167                     break;
00168                 }
00169                 else if (!strncmp(ftpbuf, "227", 3)){
00170                     pportc(ftpbuf); 
00171                 #if 0
00172                     do{
00173                         FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port);
00174                     }while(!FTPClientDataSock.is_connected());  
00175                 #endif
00176                     
00177                 #if 1
00178                     while (FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port) < 0) {
00179                     #ifdef DEBUG
00180                         printf("Unable to connect to (%s) on port (%d)\r\n", ftpServer_data_ip_addr_str, remote_port);
00181                     #endif
00182                         wait(1);
00183                     }
00184                 #endif  
00185                     sprintf(ftpbuf, "stor %s\r\n", filename);             
00186                     FTPClientControlSock.send(ftpbuf, strlen(ftpbuf));  
00187                 }
00188                 else if(!strncmp(ftpbuf,"226",3)) break;
00189             }  
00190         }
00191         return true;
00192     }
00193     return false;
00194 }
00195 
00196 bool FTPClient::dir(char* liststr){
00197     int size;
00198     if(blogin){
00199     
00200         sprintf(ftpbuf, "pasv\r\n");             
00201         FTPClientControlSock.send(ftpbuf, strlen(ftpbuf));
00202         
00203         while(1){
00204             size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf));
00205             if(size > 0){
00206             #ifdef DEBUG
00207                 printf("Received message from server: %s\r\n", ftpbuf);
00208             #endif
00209                 if (!strncmp(ftpbuf, "150", 3)){
00210                     while(1){
00211                         size = FTPClientDataSock.receive(ftpbuf, sizeof(ftpbuf));
00212                         if(size>0){
00213                             ftpbuf[size] = '\0';
00214                             strcpy(liststr,ftpbuf);
00215                             printf("%s", ftpbuf);
00216                         }
00217                         else{
00218                             FTPClientDataSock.close();
00219                             break;
00220                         }
00221                     }
00222                 }
00223                 else if (!strncmp(ftpbuf, "227", 3)){
00224                     pportc(ftpbuf); 
00225                 #if 0
00226                     do{
00227                         FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port);
00228                     }while(!FTPClientDataSock.is_connected());   
00229                 #endif
00230                 #if 1
00231                     while (FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port) < 0) {
00232                     #ifdef DEBUG
00233                         printf("Unable to connect to (%s) on port (%d)\r\n", ftpServer_data_ip_addr_str, remote_port);
00234                     #endif
00235                         wait(1);
00236                     }
00237                 #endif   
00238                     sprintf(ftpbuf, "list\r\n");             
00239                     FTPClientControlSock.send(ftpbuf, strlen(ftpbuf));  
00240                 }
00241                 else if(!strncmp(ftpbuf, "226",3)) break;
00242             }  
00243         }
00244         return true;
00245     }
00246     *liststr = 0;
00247     return false;
00248 }
00249 
00250 bool FTPClient::ls(char* liststr){
00251     
00252     int size ;
00253     if(blogin){
00254     
00255         sprintf(ftpbuf, "pasv\r\n");             
00256         FTPClientControlSock.send(ftpbuf, strlen(ftpbuf));
00257         
00258         while(1){
00259             size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf));
00260             if(size > 0){
00261             #ifdef DEBUG
00262                 printf("Received message from server: %s\r\n", ftpbuf);
00263             #endif
00264                 if (!strncmp(ftpbuf, "150", 3)){
00265                     while(1){
00266                         size = FTPClientDataSock.receive(ftpbuf, sizeof(ftpbuf));
00267                         if(size>0){
00268                             ftpbuf[size] = '\0';
00269                             strcpy(liststr,ftpbuf);
00270                         #ifdef DEBUG
00271                             printf("%s", ftpbuf);
00272                         #endif
00273                         }
00274                         else{
00275                             FTPClientDataSock.close();
00276                             break;
00277                         }
00278                     }  
00279                 }
00280                 else if (!strncmp(ftpbuf, "227", 3)){
00281                     pportc(ftpbuf); 
00282                 #if 0
00283                     do{
00284                         FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port);
00285                     }while(!FTPClientDataSock.is_connected());   
00286                 #endif
00287                 #if 1
00288                     while (FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port) < 0) {
00289                     #ifdef DEBUG
00290                         printf("Unable to connect to (%s) on port (%d)\r\n", ftpServer_data_ip_addr_str, remote_port);
00291                     #endif
00292                         wait(1);
00293                     }
00294                 #endif   
00295                     sprintf(ftpbuf, "nlst\r\n");             
00296                     FTPClientControlSock.send(ftpbuf, strlen(ftpbuf));  
00297                 }
00298                 else if(!strncmp(ftpbuf,"226",3)) break;
00299             }  
00300         }
00301         return true;
00302     }
00303     *liststr = 0;
00304     return false;
00305 }
00306 
00307 bool FTPClient::fdelete(char* filename){
00308     int size;
00309     if(blogin){
00310     
00311         sprintf(ftpbuf, "dele %s\r\n", filename);             
00312         FTPClientControlSock.send(ftpbuf, strlen(ftpbuf));
00313         
00314         size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf));
00315         if(size > 0){
00316         #ifdef DEBUG
00317             printf("Received message from server: %s\r\n", ftpbuf);
00318         #endif
00319             if (!strncmp(ftpbuf, "250", 3))  return true;
00320         }
00321     }
00322     return false;
00323 }
00324 
00325 bool FTPClient::mkdir(char* dirname){
00326     
00327     int size;
00328     if(blogin){
00329     
00330         sprintf(ftpbuf, "xmkd %s\r\n", dirname);             
00331         FTPClientControlSock.send(ftpbuf, strlen(ftpbuf));
00332         
00333         size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf));
00334         if(size > 0){
00335         #ifdef DEBUG
00336             printf("Received message from server: %s\r\n", ftpbuf);
00337         #endif
00338             if (!strncmp(ftpbuf, "257", 3)) return true;
00339         }
00340     }
00341     return false;
00342 }
00343 
00344 bool FTPClient::cd(char* dirname){
00345     int size;
00346     
00347     if(blogin){
00348     
00349         sprintf(ftpbuf, "cwd %s\r\n", dirname);             
00350         FTPClientControlSock.send(ftpbuf, strlen(ftpbuf));
00351         
00352         size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf));
00353         if(size > 0){
00354         #ifdef DEBUG
00355             printf("Received message from server: %s\r\n", ftpbuf);
00356         #endif
00357             if (!strncmp(ftpbuf, "250", 3)) return true;
00358         }
00359     }
00360     return false;
00361 }
00362 
00363 bool FTPClient::quit(){
00364     int size;
00365     if(blogin){
00366     
00367         sprintf(ftpbuf, "quit \r\n");             
00368         FTPClientControlSock.send(ftpbuf, strlen(ftpbuf));
00369         
00370         size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf));
00371         if(size > 0){
00372         #ifdef DEBUG
00373             printf("Received message from server: %s\r\n", ftpbuf);
00374         #endif
00375             if (!strncmp(ftpbuf, "250", 3)){
00376                 printf("%s\r\n", ftpbuf);
00377                 FTPClientControlSock.close();
00378                 blogin = false;
00379                 return true;
00380             }
00381         }  
00382     }
00383     blogin = false;
00384     return false;
00385 }
00386 
00387 int FTPClient::pportc(char * arg)
00388 {
00389     uint8_t ip[4];
00390     char* tok=0;
00391     char* lasts = 0;
00392     int i;
00393    
00394     tok = strchr(arg, '(') + 1  ;
00395     for (i = 0; i < 4; i++)
00396     {
00397         tok = strtok_r(tok,",", &lasts);
00398         ip[i] = (uint8_t)atoi(tok);
00399         tok = lasts;
00400         if (!tok){
00401         #ifndef DEBUG
00402             printf("bad pport : %s\r\n", arg);
00403         #endif
00404             return -1;
00405         }
00406     }
00407     remote_port = 0;
00408     for (i = 0; i < 2; i++){
00409         tok = strtok_r(tok,",)",&lasts);
00410         remote_port <<= 8;
00411         remote_port += atoi(tok);
00412         tok = lasts;
00413         if (!tok){
00414         #ifdef DEBUG
00415             printf("bad pport : %s\r\n", arg);
00416         #endif
00417             return -1;
00418         }
00419     }
00420     sprintf(ftpServer_data_ip_addr_str, "%d.%d.%d.%d", ip[0],ip[1],ip[2],ip[3]);
00421     return 0; 
00422 }
00423 
00424 
00425 
00426