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
Diff: FTPClient.cpp
- Revision:
- 4:4bef734cc93e
- Parent:
- 3:4fd8e5cd6307
- Child:
- 5:fe95043a506e
diff -r 4fd8e5cd6307 -r 4bef734cc93e FTPClient.cpp --- a/FTPClient.cpp Wed Jul 29 00:03:21 2015 +0000 +++ b/FTPClient.cpp Sat Aug 15 08:43:02 2015 +0000 @@ -1,30 +1,38 @@ #include "mbed.h" #include "EthernetInterface.h" #include "FTPClient.h" + //#define DEBUG -FTPClient::FTPClient(PinName mosi, PinName miso, PinName sclk, PinName ssel, const char* root) : _SDFileSystem(mosi, miso, sclk, ssel, root){ + +FTPClient::FTPClient(PinName mosi, PinName miso, PinName sclk, PinName ssel, const char* root) { + _SDFileSystem = new SDFileSystem(mosi, miso, sclk, ssel, root); blogin = false; - bopenflag = false; - brfileflag = false; - bdirflag = false; - blsflag = false; - bfdeleteflag = false; - bmkdirflag = false; - bcdflag = false; - bquitflag = false; + strcpy(this->root,root); } + +FTPClient::FTPClient(const char* root) +{ + _SDFileSystem = NULL; + blogin = false; + strcpy(this->root,root); +} + +FTPClient::~FTPClient() +{ + if(_SDFileSystem == NULL) delete _SDFileSystem; +} + bool FTPClient::open(char* ip, int port, char* id, char* pass){ - FTPClientControlSock = new TCPSocketConnection; - FTPClientDataSock = new TCPSocketConnection; - + int size; + blogin = false; #if 0 do{ - FTPClientControlSock->connect(ip, port); - }while(!FTPClientControlSock->is_connected()); + FTPClientControlSock.connect(ip, port); + }while(!FTPClientControlSock.is_connected()); #endif #if 1 - while (FTPClientControlSock->connect(ip, port) < 0) { + while (FTPClientControlSock.connect(ip, port) < 0) { #ifdef DEBUG printf("Unable to connect to (%s) on port (%d)\r\n", ip, port); #endif @@ -33,77 +41,76 @@ #endif while(!blogin){ - size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); + size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf)); if(size > 0){ #ifdef DEBUG - printf("Received message from server: %s\r\n", rbuf); + printf("Received message from server: %s\r\n", ftpbuf); #endif - if (!strncmp(rbuf, "220", 3)){ - printf("%s\r\n", rbuf); - sprintf(sbuf, "user %s\r\n", id); - FTPClientControlSock->send(sbuf, strlen(sbuf)); + if (!strncmp(ftpbuf, "220", 3)){ + printf("%s\r\n", ftpbuf); + sprintf(ftpbuf, "user %s\r\n", id); + FTPClientControlSock.send(ftpbuf, strlen(ftpbuf)); } - else if (!strncmp(rbuf, "331", 3)){ - sprintf(sbuf, "pass %s\r\n", pass); - FTPClientControlSock->send(sbuf, strlen(sbuf)); + else if (!strncmp(ftpbuf, "331", 3)){ + sprintf(ftpbuf, "pass %s\r\n", pass); + FTPClientControlSock.send(ftpbuf, strlen(ftpbuf)); } - else if (!strncmp(rbuf, "230", 3)){ + else if (!strncmp(ftpbuf, "230", 3)){ blogin = true; } else{ - blogin = true; + break; } } } - return 1; + return blogin; } -bool FTPClient::getfile(char* myfilename, char* filename){ - +bool FTPClient::getfile(char* filename){ + FILE* fp; + int size; + if(blogin){ - - sprintf(sbuf, "pasv\r\n"); - FTPClientControlSock->send(sbuf, strlen(sbuf)); + printf("%08X\r\n",ftpbuf); + sprintf(ftpbuf, "pasv\r\n"); + FTPClientControlSock.send(ftpbuf, strlen(ftpbuf)); - while(!brfileflag){ - size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); + while(1){ + size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf)); if(size > 0){ #ifdef DEBUG - printf("Received message from server: %s\r\n", rbuf); + printf("Received message from server: %s\r\n", ftpbuf); #endif - if (!strncmp(rbuf, "150", 3)){ - fp = fopen(myfilename, "w"); + if (!strncmp(ftpbuf, "150", 3)){ + sprintf(ftpbuf,"%s/%s\0",root,filename); #ifdef DEBUG - printf("myfilename : %s\r\n", myfilename); + printf("myfilename : %s\r\n", ftpbuf); #endif - while(true){ - memset(rbuf, 0, sizeof(rbuf)); - remain_datasize = FTPClientDataSock->receive(rbuf, sizeof(rbuf)); + fp = fopen(ftpbuf, "w"); + while(1){ + size = FTPClientDataSock.receive(ftpbuf, sizeof(ftpbuf)); #ifdef DEBUG - printf("remain_datasize : %d\r\n", remain_datasize); + printf("remain_datasize : %d\r\n", size); #endif - if(remain_datasize>0){ - for (i = 0; i < remain_datasize; i++) { - //printf("%c", rbuf[i]); - fprintf(fp, "%c", rbuf[i]); - } + if(size>0){ + fwrite(ftpbuf,size,sizeof(char),fp); #ifdef DEBUG printf("#"); #endif } - else{ - brfileflag = true; + if(size < 0 || size < MAX_SS) + { fclose(fp); - FTPClientDataSock->close(); + FTPClientDataSock.close(); break; } } } - else if (!strncmp(rbuf, "227", 3)){ - pportc(rbuf); + else if (!strncmp(ftpbuf, "227", 3)){ + pportc(ftpbuf); #if 1 - while (FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port) < 0) { + while (FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port) < 0) { #ifdef DEBUG printf("Unable to connect to (%s) on port (%d)\r\n", ftpServer_data_ip_addr_str, remote_port); #endif @@ -113,309 +120,290 @@ #if 0 do{ - FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port); - }while(!FTPClientDataSock->is_connected()); + FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port); + }while(!FTPClientDataSock.is_connected()); #endif - sprintf(sbuf, "retr %s\r\n", filename); - FTPClientControlSock->send(sbuf, strlen(sbuf)); + sprintf(ftpbuf, "retr %s\r\n", filename); + FTPClientControlSock.send(ftpbuf, strlen(ftpbuf)); } + else if(!strncmp(ftpbuf,"226",3)) break; } } - brfileflag = false; - return 1; + return true; } - else return 0; + return false; } -bool FTPClient::putfile(char* myfilename, char* filename){ + +bool FTPClient::putfile(char* filename){ + FILE* fp; + int size; + long int remain_filesize; + long int send_byte; if(blogin){ - sprintf(sbuf, "pasv\r\n"); - FTPClientControlSock->send(sbuf, strlen(sbuf)); + sprintf(ftpbuf, "pasv\r\n"); + FTPClientControlSock.send(ftpbuf, strlen(ftpbuf)); - while(!bsfileflag){ - size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); + while(1){ + size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf)); if(size > 0){ #ifdef DEBUG - printf("Received message from server: %s\r\n", rbuf); + printf("Received message from server: %s\r\n", ftpbuf); #endif - if (!strncmp(rbuf, "150", 3)){ - fp = fopen(myfilename, "r"); + if (!strncmp(ftpbuf, "150", 3)){ + sprintf(ftpbuf,"%s/%s",root,filename); + fp = fopen(ftpbuf, "r"); fseek(fp, 0, SEEK_END); // seek to end of file remain_filesize = ftell(fp); // get current file pointer fseek(fp, 0, SEEK_SET); // seek back to beginning of file do{ - memset(sbuf, 0, sizeof(sbuf)); if(remain_filesize > MAX_SS) send_byte = MAX_SS; else send_byte = remain_filesize; - fread (sbuf, 1, send_byte, fp); - FTPClientDataSock->send(sbuf, send_byte); + fread (ftpbuf, 1, send_byte, fp); + FTPClientDataSock.send(ftpbuf, send_byte); remain_filesize -= send_byte; #ifdef DEBUG printf("#"); #endif }while(remain_filesize!=0); fclose(fp); - bsfileflag = true; - FTPClientDataSock->close(); + FTPClientDataSock.close(); break; } - else if (!strncmp(rbuf, "227", 3)){ - pportc(rbuf); + else if (!strncmp(ftpbuf, "227", 3)){ + pportc(ftpbuf); #if 0 do{ - FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port); - }while(!FTPClientDataSock->is_connected()); + FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port); + }while(!FTPClientDataSock.is_connected()); #endif #if 1 - while (FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port) < 0) { + while (FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port) < 0) { #ifdef DEBUG printf("Unable to connect to (%s) on port (%d)\r\n", ftpServer_data_ip_addr_str, remote_port); #endif wait(1); } #endif - sprintf(sbuf, "stor %s\r\n", filename); - FTPClientControlSock->send(sbuf, strlen(sbuf)); + sprintf(ftpbuf, "stor %s\r\n", filename); + FTPClientControlSock.send(ftpbuf, strlen(ftpbuf)); } + else if(!strncmp(ftpbuf,"226",3)) break; } } - bsfileflag = false; - return 1; + return true; } - else return 0; + return false; } -bool FTPClient::dir(){ - + +bool FTPClient::dir(char* liststr){ + int size; if(blogin){ - sprintf(sbuf, "pasv\r\n"); - FTPClientControlSock->send(sbuf, strlen(sbuf)); + sprintf(ftpbuf, "pasv\r\n"); + FTPClientControlSock.send(ftpbuf, strlen(ftpbuf)); - while(!bdirflag){ - size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); + while(1){ + size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf)); if(size > 0){ #ifdef DEBUG - printf("Received message from server: %s\r\n", rbuf); + printf("Received message from server: %s\r\n", ftpbuf); #endif - if (!strncmp(rbuf, "150", 3)){ - while(true){ - memset(rbuf, 0, sizeof(rbuf)); - size = FTPClientDataSock->receive(rbuf, sizeof(rbuf)); - rbuf[size] = '\0'; + if (!strncmp(ftpbuf, "150", 3)){ + while(1){ + size = FTPClientDataSock.receive(ftpbuf, sizeof(ftpbuf)); if(size>0){ - printf("%s", rbuf); + ftpbuf[size] = '\0'; + strcpy(liststr,ftpbuf); + printf("%s", ftpbuf); } else{ - bdirflag = true; - FTPClientDataSock->close(); + FTPClientDataSock.close(); break; } - } + } } - else if (!strncmp(rbuf, "227", 3)){ - pportc(rbuf); + else if (!strncmp(ftpbuf, "227", 3)){ + pportc(ftpbuf); #if 0 do{ - FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port); - }while(!FTPClientDataSock->is_connected()); + FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port); + }while(!FTPClientDataSock.is_connected()); #endif #if 1 - while (FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port) < 0) { + while (FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port) < 0) { #ifdef DEBUG printf("Unable to connect to (%s) on port (%d)\r\n", ftpServer_data_ip_addr_str, remote_port); #endif wait(1); } #endif - sprintf(sbuf, "list\r\n"); - FTPClientControlSock->send(sbuf, strlen(sbuf)); + sprintf(ftpbuf, "list\r\n"); + FTPClientControlSock.send(ftpbuf, strlen(ftpbuf)); } + else if(!strncmp(ftpbuf, "226",3)) break; } } - bdirflag = false; - return 1; + return true; } - else return 0; + *liststr = 0; + return false; } -bool FTPClient::ls(){ +bool FTPClient::ls(char* liststr){ + int size ; if(blogin){ - sprintf(sbuf, "pasv\r\n"); - FTPClientControlSock->send(sbuf, strlen(sbuf)); + sprintf(ftpbuf, "pasv\r\n"); + FTPClientControlSock.send(ftpbuf, strlen(ftpbuf)); - while(!blsflag){ - size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); + while(1){ + size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf)); if(size > 0){ #ifdef DEBUG - printf("Received message from server: %s\r\n", rbuf); + printf("Received message from server: %s\r\n", ftpbuf); #endif - if (!strncmp(rbuf, "150", 3)){ - while(true){ - memset(rbuf, 0, sizeof(rbuf)); - size = FTPClientDataSock->receive(rbuf, sizeof(rbuf)); - rbuf[size] = '\0'; + if (!strncmp(ftpbuf, "150", 3)){ + while(1){ + size = FTPClientDataSock.receive(ftpbuf, sizeof(ftpbuf)); if(size>0){ - printf("%s", rbuf); + ftpbuf[size] = '\0'; + strcpy(liststr,ftpbuf); + #ifdef DEBUG + printf("%s", ftpbuf); + #endif } else{ - blsflag = true; - FTPClientDataSock->close(); + FTPClientDataSock.close(); break; } } } - else if (!strncmp(rbuf, "227", 3)){ - pportc(rbuf); + else if (!strncmp(ftpbuf, "227", 3)){ + pportc(ftpbuf); #if 0 do{ - FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port); - }while(!FTPClientDataSock->is_connected()); + FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port); + }while(!FTPClientDataSock.is_connected()); #endif #if 1 - while (FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port) < 0) { + while (FTPClientDataSock.connect(ftpServer_data_ip_addr_str, remote_port) < 0) { #ifdef DEBUG printf("Unable to connect to (%s) on port (%d)\r\n", ftpServer_data_ip_addr_str, remote_port); #endif wait(1); } #endif - sprintf(sbuf, "nlst\r\n"); - FTPClientControlSock->send(sbuf, strlen(sbuf)); + sprintf(ftpbuf, "nlst\r\n"); + FTPClientControlSock.send(ftpbuf, strlen(ftpbuf)); } + else if(!strncmp(ftpbuf,"226",3)) break; } } - blsflag = false; - return 1; + return true; } - else return 0; + *liststr = 0; + return false; } bool FTPClient::fdelete(char* filename){ - + int size; if(blogin){ - sprintf(sbuf, "dele %s\r\n", filename); - FTPClientControlSock->send(sbuf, strlen(sbuf)); + sprintf(ftpbuf, "dele %s\r\n", filename); + FTPClientControlSock.send(ftpbuf, strlen(ftpbuf)); - while(!bfdeleteflag){ - size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); - if(size > 0){ - #ifdef DEBUG - printf("Received message from server: %s\r\n", rbuf); - #endif - if (!strncmp(rbuf, "250", 3)){ - bfdeleteflag = true; - } - else { - return 0; - } - } + size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf)); + if(size > 0){ + #ifdef DEBUG + printf("Received message from server: %s\r\n", ftpbuf); + #endif + if (!strncmp(ftpbuf, "250", 3)) return true; } - bfdeleteflag = false; - return 1; } - else return 0; + return false; } + bool FTPClient::mkdir(char* dirname){ + int size; + if(blogin){ + + sprintf(ftpbuf, "xmkd %s\r\n", dirname); + FTPClientControlSock.send(ftpbuf, strlen(ftpbuf)); + + size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf)); + if(size > 0){ + #ifdef DEBUG + printf("Received message from server: %s\r\n", ftpbuf); + #endif + if (!strncmp(ftpbuf, "257", 3)) return true; + } + } + return false; +} + +bool FTPClient::cd(char* dirname){ + int size; + if(blogin){ - sprintf(sbuf, "xmkd %s\r\n", dirname); - FTPClientControlSock->send(sbuf, strlen(sbuf)); + sprintf(ftpbuf, "cwd %s\r\n", dirname); + FTPClientControlSock.send(ftpbuf, strlen(ftpbuf)); - while(!bmkdirflag){ - size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); - if(size > 0){ - #ifdef DEBUG - printf("Received message from server: %s\r\n", rbuf); - #endif - if (!strncmp(rbuf, "257", 3)){ - bmkdirflag = true; - } - else { - return 0; - } - } + size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf)); + if(size > 0){ + #ifdef DEBUG + printf("Received message from server: %s\r\n", ftpbuf); + #endif + if (!strncmp(ftpbuf, "250", 3)) return true; } - bmkdirflag = false; - return 1; } - else return 0; + return false; } -bool FTPClient::cd(char* dirname){ - + +bool FTPClient::quit(){ + int size; if(blogin){ - sprintf(sbuf, "cwd %s\r\n", dirname); - FTPClientControlSock->send(sbuf, strlen(sbuf)); + sprintf(ftpbuf, "quit \r\n"); + FTPClientControlSock.send(ftpbuf, strlen(ftpbuf)); - while(!bcdflag){ - size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); - if(size > 0){ - #ifdef DEBUG - printf("Received message from server: %s\r\n", rbuf); - #endif - if (!strncmp(rbuf, "250", 3)){ - bcdflag = true; - } - else { - return 0; - } - } - } - bcdflag = false; - return 1; + size = FTPClientControlSock.receive(ftpbuf, sizeof(ftpbuf)); + if(size > 0){ + #ifdef DEBUG + printf("Received message from server: %s\r\n", ftpbuf); + #endif + if (!strncmp(ftpbuf, "250", 3)){ + printf("%s\r\n", ftpbuf); + FTPClientControlSock.close(); + blogin = false; + return true; + } + } } - else return 0; + blogin = false; + return false; } -bool FTPClient::quit(){ - - if(blogin){ - - sprintf(sbuf, "quit \r\n"); - FTPClientControlSock->send(sbuf, strlen(sbuf)); - - while(!bquitflag){ - size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); - if(size > 0){ - #ifdef DEBUG - printf("Received message from server: %s\r\n", rbuf); - #endif - if (!strncmp(rbuf, "250", 3)){ - printf("%s\r\n", rbuf); - bquitflag = true; - } - else { - return 0; - } - } - } - - bquitflag = false; - blogin = false; - delete FTPClientControlSock; - delete FTPClientDataSock; - - return 1; - } - else return 0; -} + int FTPClient::pportc(char * arg) { + uint8_t ip[4]; char* tok=0; - - strtok(arg,"("); + char* lasts = 0; + int i; + + tok = strchr(arg, '(') + 1 ; for (i = 0; i < 4; i++) { - if(i==0) tok = strtok(NULL,",\r\n"); - else tok = strtok(NULL,","); - ftpServer_data_ip_addr[i] = (uint8_t)atoi(tok); + tok = strtok_r(tok,",", &lasts); + ip[i] = (uint8_t)atoi(tok); + tok = lasts; if (!tok){ #ifndef DEBUG printf("bad pport : %s\r\n", arg); @@ -425,9 +413,10 @@ } remote_port = 0; for (i = 0; i < 2; i++){ - tok = strtok(NULL,",\r\n"); + tok = strtok_r(tok,",)",&lasts); remote_port <<= 8; remote_port += atoi(tok); + tok = lasts; if (!tok){ #ifdef DEBUG printf("bad pport : %s\r\n", arg); @@ -435,8 +424,8 @@ return -1; } } - sprintf(ftpServer_data_ip_addr_str, "%d.%d.%d.%d", ftpServer_data_ip_addr[0], ftpServer_data_ip_addr[1], ftpServer_data_ip_addr[2], ftpServer_data_ip_addr[3]); - return 0; + sprintf(ftpServer_data_ip_addr_str, "%d.%d.%d.%d", ip[0],ip[1],ip[2],ip[3]); + return 0; }