11 Commits

Author SHA1 Message Date
8508482bc4 Merge branch 'hotfix/opimization_FTP_client' 2015-04-15 17:29:54 +09:00
c2b6716e43 optimization FTP client 2015-04-15 17:29:42 +09:00
580241080d Merge branch 'hotfix/fix_compile_error' 2015-04-15 16:40:19 +09:00
c29dc21040 fix compile error 2015-04-15 16:39:46 +09:00
b840aa13a2 Merge pull request #6 from Wiznet/feature/FTP_Client_optimization
FTP Client optimization
2015-04-14 16:55:41 +09:00
b6b808b306 FTP Client optimization 2015-04-14 15:26:20 +09:00
924ceea741 Merge pull request #5 from Wiznet/develop
FTP Client optimization
2015-04-14 11:21:19 +09:00
86726229f0 FTP Client optimization 2015-04-14 11:05:02 +09:00
2857b805d9 Sync the code & doxygen document! 2015-04-13 09:42:44 +09:00
065b096c92 Modify setIMR() & getIMR() and setSIMR() & getSIMR()
In order to integrate W5200 with ioLibrary
setIMR() & getIMR(): Set/Get a value to IMR2 intead of _IMR_.
setIMR2() & getIMR2() (setSIMR() & getSIMR()) : Set/Get a value to _IMR_
instead of IMR2
2015-04-13 09:28:29 +09:00
f36dd4352a Fixed the Type casting Error in send() ,socket.c 2015-04-09 14:56:48 +09:00
7 changed files with 273 additions and 232 deletions

Binary file not shown.

View File

@ -252,7 +252,9 @@
* @brief Socket Interrupt Mask Register(R/W)
* @details Each bit of \ref _IMR_ corresponds to each bit of \ref IR2.
* When a bit of \ref _IMR_ is and the corresponding bit of \ref IR2 is Interrupt will be issued.
* In other words, if a bit of \ref SIMR is an interrupt will be not issued even if the corresponding bit of \ref IR is
* In other words, if a bit of \ref _IMR_, an interrupt will be not issued even if the corresponding bit of \ref IR2 is set
* @note This Register is same operated as <b>SMIR<b> of W5100, W5300 and W5550.\n
* So, \ref setSIMR() set a value to _IMR_ for integrating with ioLibrary
*/
#define _IMR_ (_W5200_IO_BASE_ + (0x0016)) // Socket Interrupt Mask
@ -375,6 +377,8 @@
* </table>
* - \ref IM_IR7 : IP Conflict Interrupt Mask
* - \ref IM_IR5 : PPPoE Close Interrupt Mask
* @note This Register is same operated as <b>_IMR_<b> of W5100, W5300 and W5550.\n
* So, \ref setIMR() set a value to IMR2 for integrating with ioLibrary
*/
#define IMR2 (_W5200_IO_BASE_ + (0x0036)) // Interrupt Mask
@ -1328,21 +1332,31 @@ void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len);
/**
* @ingroup Common_register_access_function_W5200
* @brief Set \ref _IMR_ register
* @param (uint8_t)imr Value to set @ref _IMR_ register.
* @brief Set \ref IMR2 register
* @param (uint8_t)imr Value to set @ref IMR2 register.
* @sa getIMR()
*/
//M20150410 : Replace _IMR_ with IMR2 for integrating with ioLibrary
/*
#define setIMR(imr) \
WIZCHIP_WRITE(_IMR_, imr)
*/
#define setIMR(imr) \
WIZCHIP_WRITE(IMR2, imr)
/**
* @ingroup Common_register_access_function_W5200
* @brief Get \ref _IMR_ register
* @return uint8_t. Value of @ref _IMR_ register.
* @brief Get \ref IMR2 register
* @return uint8_t. Value of @ref IMR2 register.
* @sa setIMR()
*/
//M20150410 : Replace _IMR_ with IMR2 for integrating with ioLibrary
/*
#define getIMR() \
WIZCHIP_READ(_IMR_)
*/
#define getIMR() \
WIZCHIP_READ(IMR2)
/**
* @ingroup Common_register_access_function_W5200
@ -1492,23 +1506,34 @@ void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len);
/**
* @ingroup Common_register_access_function_W5200
* @brief Set \ref IMR2 register
* @brief Set \ref _IMR_ register
* @param (uint8_t)imr2 Value to set \ref IMR2 register.
* @sa getIMR2()
* @note If possible, Don't use this function. Instead, Use setSIMR() for compatible with ioLibrary.
*/
//M20150410 : Replace IMR2 with _IMR_ for integrating with ioLibrary
/*
#define setIMR2(imr2) \
WIZCHIP_WRITE(IMR2, (imr2 & 0xA0))
*/
#define setIMR2(imr2) \
WIZCHIP_WRITE(_IMR_, (imr2 & 0xA0))
#define setSIMR(imr2) setIMR2(imr2)
/**
* @ingroup Common_register_access_function_W5200
* @brief Get \ref IMR2 register
* @brief Get \ref _IMR_ register
* @return uint8_t. Value of \ref IMR2 register.
* @sa setIMR2()
*/
//M20150410 : Replace IMR2 with _IMR_ for integrating with ioLibrary
/*
#define getIMR2() \
(WIZCHIP_READ(IMR2) & 0xA0)
#define getSIMR() getIMR2()
*/
#define getIMR2() \
(WIZCHIP_READ(_IMR_) & 0xA0)
#define getSIMR() getIMR2()
///////////////////////////////////
// Socket N register I/O function //
///////////////////////////////////

View File

@ -171,7 +171,7 @@ int8_t close(uint8_t sn)
while( getSn_CR(sn) );
/* clear all interrupt of the socket. */
setSn_IR(sn, 0xFF);
//A20130325 : Release the sock_io_mode of socket n.
//A20150401 : Release the sock_io_mode of socket n.
sock_io_mode &= ~(1<<sn);
//
sock_is_sending &= ~(1<<sn);
@ -317,7 +317,9 @@ int32_t send(uint8_t sn, uint8_t * buf, uint16_t len)
/* wait to process the command... */
while(getSn_CR(sn));
sock_is_sending |= (1 << sn);
return len;
//M20150409 : Explicit Type Casting
//return len;
return (int32_t)len;
}
@ -359,7 +361,9 @@ int32_t recv(uint8_t sn, uint8_t * buf, uint16_t len)
wiz_recv_data(sn, buf, len);
setSn_CR(sn,Sn_CR_RECV);
while(getSn_CR(sn));
return len;
//M20150409 : Explicit Type Casting
//return len;
return (int32_t)len;
}
int32_t sendto(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port)
@ -433,16 +437,22 @@ int32_t sendto(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t
else if(tmp & Sn_IR_TIMEOUT)
{
setSn_IR(sn, Sn_IR_TIMEOUT);
len = SOCKERR_TIMEOUT;
break;
//M20150409 : Fixed the lost of sign bits by type casting.
//len = (uint16_t)SOCKERR_TIMEOUT;
//break;
#if _WIZCHIP_ < 5500 //M20150401 : for WIZCHIP Errata #4, #5 (ARP errata)
if(taddr) setSUBR((uint8_t*)&taddr);
#endif
return SOCKERR_TIMEOUT;
}
////////////
}
#if _WIZCHIP_ < 5500 //M20150401 : for WIZCHIP Errata #4, #5 (ARP errata)
if(taddr) setSUBR((uint8_t*)&taddr);
#endif
return len;
//M20150409 : Explicit Type Casting
//return len;
return (int32_t)len;
}
@ -489,7 +499,7 @@ int32_t recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16
setSn_CR(sn,Sn_CR_RECV);
while(getSn_CR(sn));
// read peer's IP address, port number & packet length
addr[0] = head[0];
addr[0] = head[0];
addr[1] = head[1];
addr[2] = head[2];
addr[3] = head[3];
@ -564,7 +574,9 @@ int32_t recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16
//if(sock_remained_size[sn] != 0) sock_pack_info[sn] |= 0x01;
if(sock_remained_size[sn] != 0) sock_pack_info[sn] |= PACK_REMAINED;
//
return pack_len;
//M20150409 : Explicit Type Casting
//return pack_len;
return (int32_t)pack_len;
}

View File

@ -341,7 +341,9 @@ typedef enum
SIK_RECEIVED = (1 << 2), ///< data received
SIK_TIMEOUT = (1 << 3), ///< timeout occurred
SIK_SENT = (1 << 4), ///< send ok
SIK_ALL = 0x1F, ///< all interrupt
//M20150410 : Remove the comma of last member
//SIK_ALL = 0x1F, ///< all interrupt
SIK_ALL = 0x1F ///< all interrupt
}sockint_kind;
/**

View File

@ -1,103 +1,37 @@
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include <stdarg.h>
#include "stdio_private.h"
#include "socket.h"
#include "ftpc.h"
#include "mmcHandler.h"
/* FTP Responses */
#define R_150 150 /* File status ok; opening data conn */
#define R_200 200 /* 'Generic' command ok */
#define R_220 220 /* Service ready for new user. */
#define R_226 226 /* Closing data connection. File transfer/abort successful */
#define R_227 227 /* Entering passive mode (h1,h2,h3,h4,p1,p2) */
#define R_230 230 /* User logged in, proceed */
#define R_331 331 /* User name okay, need password. */
#define TransferAscii 0x41
#define TransferBinary 0x49
#define ConnectActive 1
#define ConnectPassive 0
un_l2cval remote_ip;
uint16_t remote_port;
un_l2cval local_ip;
uint16_t local_port;
uint8_t connect_state_controlc = 0;
uint8_t connect_state_datac = 0;
uint8_t connect_state_control_ftpc = 0;
uint8_t connect_state_data_ftpc = 0;
uint8_t gModeActivePassiveflag = 0;
struct ftpd ftp;
enum CommandFirst {
f_nocmd,
f_dir,
f_put,
f_get,
};
enum CommandSecond {
s_nocmd,
s_dir,
s_put,
s_get,
};
struct Command {
enum CommandFirst First;
enum CommandSecond Second;
};
struct Command Command;
int current_yearc = 2014;
int current_monthc = 12;
int current_dayc = 31;
int current_hourc = 10;
int current_minc = 10;
int current_secc = 30;
uint8_t FTP_destip[4] = {192, 168, 10, 230}; // For FTP client examples; destination network info
uint16_t FTP_destport = 21; // For FTP client examples; destination network info
uint8_t gMenuStart = 0;
uint8_t gDataSockReady = 0;
uint8_t gDataPutGetStart = 0;
uint8_t gModeAsciiBinary = 0;
uint8_t gModeActivePassive = ConnectPassive;
static uint8_t gMsgBuf[20]={0,};
// For FTP client examples; destination network info
uint8_t FTP_destip[4] = {192, 168, 0, 230};
uint16_t FTP_destport = 21;
extern int Board_UARTGetCharBlocking(void);
uint8_t* User_Keyboard_MSG()
{
uint8_t i=0;
do{
gMsgBuf[i] = Board_UARTGetCharBlocking();
i++;
}while(gMsgBuf[i-1]!=0x0d);
gMsgBuf[i-1]=0;
return gMsgBuf;
}
struct ftpc ftpc;
struct Command Command;
void ftpc_init(uint8_t * src_ip)
{
ftp.state = FTPS_NOT_LOGIN;
ftp.dsock_mode = ACTIVE_MODE;
ftpc.dsock_mode = ACTIVE_MODE;
local_ip.cVal[0] = src_ip[0];
local_ip.cVal[1] = src_ip[1];
local_ip.cVal[2] = src_ip[2];
local_ip.cVal[3] = src_ip[3];
local_port = 35000;
strcpy(ftp.workingdir, "/");
socket(CTRL_SOCK, Sn_MR_TCP, IPPORT_FTP, 0x0);
strcpy(ftpc.workingdir, "/");
socket(CTRL_SOCK, Sn_MR_TCP, FTP_destport, 0x0);
}
uint8_t ftpc_run(uint8_t * dbuf)
{
#ifndef Need_UARTGetCharBlocking_func
uint16_t size = 0;
long ret = 0;
uint32_t send_byte, recv_byte;
@ -111,10 +45,10 @@ uint8_t ftpc_run(uint8_t * dbuf)
switch(getSn_SR(CTRL_SOCK))
{
case SOCK_ESTABLISHED :
if(!connect_state_controlc){
if(!connect_state_control_ftpc){
printf("%d:FTP Connected\r\n", CTRL_SOCK);
strcpy(ftp.workingdir, "/");
connect_state_controlc = 1;
strcpy(ftpc.workingdir, "/");
connect_state_control_ftpc = 1;
}
if(gMenuStart){
gMenuStart = 0;
@ -123,16 +57,18 @@ uint8_t ftpc_run(uint8_t * dbuf)
printf("----------------------------------------\r\n");
printf("1> View FTP Server Directory\r\n");
printf("2> View My Directory\r\n");
printf("3> Sets the type of file to be transferred. Current state : %s\r\n", (gModeAsciiBinary==TransferAscii)?"Ascii":"Binary");
printf("4> Sets Data Connection. Current state : %s\r\n", (gModeActivePassive==ConnectActive)?"Active":"Passive");
printf("3> Sets the type of file to be transferred. Current state : %s\r\n", (ftpc.type==ASCII_TYPE)?"Ascii":"Binary");
printf("4> Sets Data Connection. Current state : %s\r\n", (ftpc.dsock_mode==ACTIVE_MODE)?"Active":"Passive");
printf("5> Put File to Server\r\n");
printf("6> Get File from Server\r\n");
#if defined(F_FILESYSTEM)
printf("7> Delete My File\r\n");
#endif
printf("----------------------------------------\r\n");
while(1){
msg_c=Board_UARTGetCharBlocking();
msg_c=ftp_getc();
if(msg_c=='1'){
if(gModeActivePassive==ConnectPassive){
if(ftpc.dsock_mode==PASSIVE_MODE){
sprintf(dat,"PASV\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_dir;
@ -151,7 +87,7 @@ uint8_t ftpc_run(uint8_t * dbuf)
break;
}
else if(msg_c=='5'){
if(gModeActivePassive==ConnectPassive){
if(ftpc.dsock_mode==PASSIVE_MODE){
sprintf(dat,"PASV\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_put;
@ -169,7 +105,7 @@ uint8_t ftpc_run(uint8_t * dbuf)
}
}
else if(msg_c=='6'){
if(gModeActivePassive==ConnectPassive){
if(ftpc.dsock_mode==PASSIVE_MODE){
sprintf(dat,"PASV\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_get;
@ -187,10 +123,14 @@ uint8_t ftpc_run(uint8_t * dbuf)
}
}
else if(msg_c=='2'){
scan_files(ftp.workingdir, dbuf, (int *)&size);
#if defined(F_FILESYSTEM)
scan_files(ftpc.workingdir, dbuf, (int *)&size);
printf("\r\n%s\r\n", dbuf);
getMountedMemorySize(SPI_FLASHM, &totalSize, &availableSize);
printf(" - Available Memory Size : %ld kB / %ld kB ( %ld kB is used )\r\n", availableSize, totalSize, (totalSize - availableSize));
#else
if (strncmp(ftpc.workingdir, "/$Recycle.Bin", sizeof("/$Recycle.Bin")) != 0)
size = sprintf(dbuf, "drwxr-xr-x 1 ftp ftp 0 Dec 31 2014 $Recycle.Bin\r\n-rwxr-xr-x 1 ftp ftp 512 Dec 31 2014 test.txt\r\n");
printf("\r\n%s\r\n", dbuf);
#endif
gMenuStart = 1;
break;
}
@ -198,16 +138,16 @@ uint8_t ftpc_run(uint8_t * dbuf)
printf("1> ASCII\r\n");
printf("2> BINARY\r\n");
while(1){
msg_c=Board_UARTGetCharBlocking();
msg_c=ftp_getc();
if(msg_c=='1'){
sprintf(dat,"TYPE %c\r\n", TransferAscii);
gModeAsciiBinary = TransferAscii;
ftpc.type = ASCII_TYPE;
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
}
else if(msg_c=='2'){
sprintf(dat,"TYPE %c\r\n", TransferBinary);
gModeAsciiBinary = TransferBinary;
ftpc.type = IMAGE_TYPE;
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
}
@ -221,13 +161,13 @@ uint8_t ftpc_run(uint8_t * dbuf)
printf("1> ACTIVE\r\n");
printf("2> PASSIVE\r\n");
while(1){
msg_c=Board_UARTGetCharBlocking();
msg_c=ftp_getc();
if(msg_c=='1'){
gModeActivePassive = ConnectActive;
ftpc.dsock_mode=ACTIVE_MODE;
break;
}
else if(msg_c=='2'){
gModeActivePassive = ConnectPassive;
ftpc.dsock_mode=PASSIVE_MODE;
break;
}
else{
@ -237,10 +177,11 @@ uint8_t ftpc_run(uint8_t * dbuf)
gMenuStart = 1;
break;
}
#if defined(F_FILESYSTEM)
else if(msg_c=='7'){
printf(">del filename?");
sprintf(ftp.filename, "/%s\r\n", User_Keyboard_MSG());
if (f_unlink((const char *)ftp.filename) != 0){
sprintf(ftpc.filename, "/%s\r\n", User_Keyboard_MSG());
if (f_unlink((const char *)ftpc.filename) != 0){
printf("\r\nCould not delete.\r\n");
}
else{
@ -249,6 +190,7 @@ uint8_t ftpc_run(uint8_t * dbuf)
gMenuStart = 1;
break;
}
#endif
else{
printf("\r\nRetry...\r\n");
}
@ -262,17 +204,17 @@ uint8_t ftpc_run(uint8_t * dbuf)
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case f_put:
printf(">put filename?");
printf(">put file name?");
sprintf(dat,"STOR %s\r\n", User_Keyboard_MSG());
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case f_get:
printf(">get filename?");
printf(">get file name?");
sprintf(dat,"RETR %s\r\n", User_Keyboard_MSG());
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
default:
printf("shit!!!! Command.First = default\r\n");
printf("Command.First = default\r\n");
break;
}
}
@ -301,7 +243,7 @@ uint8_t ftpc_run(uint8_t * dbuf)
break;
case SOCK_CLOSED :
printf("%d:FTPStart\r\n",CTRL_SOCK);
if((ret=socket(CTRL_SOCK, Sn_MR_TCP, IPPORT_FTP, 0x0)) != CTRL_SOCK){
if((ret=socket(CTRL_SOCK, Sn_MR_TCP, FTP_destport, 0x0)) != CTRL_SOCK){
printf("%d:socket() error:%ld\r\n", CTRL_SOCK, ret);
close(CTRL_SOCK);
return ret;
@ -313,7 +255,7 @@ uint8_t ftpc_run(uint8_t * dbuf)
printf("%d:Connect error\r\n",CTRL_SOCK);
return ret;
}
connect_state_controlc = 0;
connect_state_control_ftpc = 0;
printf("%d:Connectting...\r\n",CTRL_SOCK);
break;
default :
@ -322,15 +264,14 @@ uint8_t ftpc_run(uint8_t * dbuf)
switch(getSn_SR(DATA_SOCK)){
case SOCK_ESTABLISHED :
if(!connect_state_datac){
if(!connect_state_data_ftpc){
printf("%d:FTP Data socket Connected\r\n", DATA_SOCK);
connect_state_datac = 1;
//gDataSockReady = 1;
connect_state_data_ftpc = 1;
}
if(gDataPutGetStart){
switch(Command.Second){
case s_dir:
printf("waitng...\r\n");
printf("dir waiting...\r\n");
if((size = getSn_RX_RSR(DATA_SOCK)) > 0){ // Don't need to check SOCKERR_BUSY because it doesn't not occur.
printf("ok\r\n");
memset(dbuf, 0, _MAX_SS);
@ -347,17 +288,19 @@ uint8_t ftpc_run(uint8_t * dbuf)
}
printf("Rcvd Data:\n\r%s\n\r", dbuf);
gDataPutGetStart = 0;
Command.Second = s_nocmd;
}
break;
case s_put:
printf("waitng...\r\n");
if(strlen(ftp.workingdir) == 1)
sprintf(ftp.filename, "/%s", (uint8_t *)gMsgBuf);
printf("put waiting...\r\n");
if(strlen(ftpc.workingdir) == 1)
sprintf(ftpc.filename, "/%s", (uint8_t *)gMsgBuf);
else
sprintf(ftp.filename, "%s/%s", ftp.workingdir, (uint8_t *)gMsgBuf);
ftp.fr = f_open(&(ftp.fil), (const char *)ftp.filename, FA_READ);
if(ftp.fr == FR_OK){
remain_filesize = ftp.fil.fsize;
sprintf(ftpc.filename, "%s/%s", ftpc.workingdir, (uint8_t *)gMsgBuf);
#if defined(F_FILESYSTEM)
ftpc.fr = f_open(&(ftpc.fil), (const char *)ftpc.filename, FA_READ);
if(ftpc.fr == FR_OK){
remain_filesize = ftpc.fil.fsize;
printf("f_open return FR_OK\r\n");
do{
memset(dbuf, 0, _MAX_SS);
@ -365,8 +308,8 @@ uint8_t ftpc_run(uint8_t * dbuf)
send_byte = _MAX_SS;
else
send_byte = remain_filesize;
ftp.fr = f_read(&(ftp.fil), (void *)dbuf, send_byte , (UINT *)&blocklen);
if(ftp.fr != FR_OK){
ftpc.fr = f_read(&(ftpc.fil), (void *)dbuf, send_byte , (UINT *)&blocklen);
if(ftpc.fr != FR_OK){
break;
}
printf("#");
@ -374,25 +317,35 @@ uint8_t ftpc_run(uint8_t * dbuf)
remain_filesize -= blocklen;
}while(remain_filesize != 0);
printf("\r\nFile read finished\r\n");
ftp.fr = f_close(&(ftp.fil));
gDataPutGetStart = 0;
disconnect(DATA_SOCK);
ftpc.fr = f_close(&(ftpc.fil));
}
else{
printf("File Open Error: %d\r\n", ftp.fr);
ftp.fr = f_close(&(ftp.fil));
gDataPutGetStart = 0;
disconnect(DATA_SOCK);
printf("File Open Error: %d\r\n", ftpc.fr);
ftpc.fr = f_close(&(ftpc.fil));
}
#else
remain_filesize = strlen(ftpc.filename);
do{
memset(dbuf, 0, _MAX_SS);
blocklen = sprintf(dbuf, "%s", ftpc.filename);
printf("########## dbuf:%s\r\n", dbuf);
send(DATA_SOCK, dbuf, blocklen);
remain_filesize -= blocklen;
}while(remain_filesize != 0);
#endif
gDataPutGetStart = 0;
Command.Second = s_nocmd;
disconnect(DATA_SOCK);
break;
case s_get:
printf("waitng...\r\n");
if(strlen(ftp.workingdir) == 1)
sprintf(ftp.filename, "/%s", (uint8_t *)gMsgBuf);
printf("get waiting...\r\n");
if(strlen(ftpc.workingdir) == 1)
sprintf(ftpc.filename, "/%s", (uint8_t *)gMsgBuf);
else
sprintf(ftp.filename, "%s/%s", ftp.workingdir, (uint8_t *)gMsgBuf);
ftp.fr = f_open(&(ftp.fil), (const char *)ftp.filename, FA_CREATE_ALWAYS | FA_WRITE);
if(ftp.fr == FR_OK){
sprintf(ftpc.filename, "%s/%s", ftpc.workingdir, (uint8_t *)gMsgBuf);
#if defined(F_FILESYSTEM)
ftpc.fr = f_open(&(ftpc.fil), (const char *)ftpc.filename, FA_CREATE_ALWAYS | FA_WRITE);
if(ftpc.fr == FR_OK){
printf("f_open return FR_OK\r\n");
while(1){
if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){
@ -401,15 +354,15 @@ uint8_t ftpc_run(uint8_t * dbuf)
if(remain_datasize > _MAX_SS) recv_byte = _MAX_SS;
else recv_byte = remain_datasize;
ret = recv(DATA_SOCK, dbuf, recv_byte);
ftp.fr = f_write(&(ftp.fil), (const void *)dbuf, (UINT)ret, (UINT *)&blocklen);
ftpc.fr = f_write(&(ftpc.fil), (const void *)dbuf, (UINT)ret, (UINT *)&blocklen);
remain_datasize -= blocklen;
if(ftp.fr != FR_OK){
if(ftpc.fr != FR_OK){
printf("f_write failed\r\n");
break;
}
if(remain_datasize <= 0) break;
}
if(ftp.fr != FR_OK){
if(ftpc.fr != FR_OK){
printf("f_write failed\r\n");
break;
}
@ -420,26 +373,49 @@ uint8_t ftpc_run(uint8_t * dbuf)
}
}
printf("\r\nFile write finished\r\n");
ftp.fr = f_close(&(ftp.fil));
ftpc.fr = f_close(&(ftpc.fil));
gDataPutGetStart = 0;
}else{
printf("File Open Error: %d\r\n", ftp.fr);
printf("File Open Error: %d\r\n", ftpc.fr);
}
#else
while(1){
if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){
while(1){
memset(dbuf, 0, _MAX_SS);
if(remain_datasize > _MAX_SS)
recv_byte = _MAX_SS;
else
recv_byte = remain_datasize;
ret = recv(DATA_SOCK, dbuf, recv_byte);
printf("########## dbuf:%s\r\n", dbuf);
remain_datasize -= ret;
if(remain_datasize <= 0)
break;
}
}else{
if(getSn_SR(DATA_SOCK) != SOCK_ESTABLISHED)
break;
}
}
gDataPutGetStart = 0;
Command.Second = s_nocmd;
#endif
break;
default:
printf("shit!!!! Command.Second = default\r\n");
printf("Command.Second = default\r\n");
break;
}
}
break;
case SOCK_CLOSE_WAIT :
printf("%d:CloseWait\r\n",DATA_SOCK);
if((ret=disconnect(DATA_SOCK)) != SOCK_OK) return ret;
printf("%d:Closed\r\n",DATA_SOCK);
if((ret=disconnect(DATA_SOCK)) != SOCK_OK) return ret;
printf("%d:Closed\r\n",DATA_SOCK);
break;
case SOCK_CLOSED :
if(ftp.dsock_state == DATASOCK_READY){
if(ftp.dsock_mode == PASSIVE_MODE){
if(ftpc.dsock_state == DATASOCK_READY){
if(ftpc.dsock_mode == PASSIVE_MODE){
printf("%d:FTPDataStart, port : %d\r\n",DATA_SOCK, local_port);
if((ret=socket(DATA_SOCK, Sn_MR_TCP, local_port, 0x0)) != DATA_SOCK){
printf("%d:socket() error:%ld\r\n", DATA_SOCK, ret);
@ -460,13 +436,13 @@ uint8_t ftpc_run(uint8_t * dbuf)
if(local_port > 50000)
local_port = 35000;
}
ftp.dsock_state = DATASOCK_START;
ftpc.dsock_state = DATASOCK_START;
}
break;
case SOCK_INIT :
printf("%d:Opened\r\n",DATA_SOCK);
if(ftp.dsock_mode == ACTIVE_MODE){
if(ftpc.dsock_mode == ACTIVE_MODE){
if( (ret = listen(DATA_SOCK)) != SOCK_OK){
printf("%d:Listen error\r\n",DATA_SOCK);
return ret;
@ -480,11 +456,12 @@ uint8_t ftpc_run(uint8_t * dbuf)
}
gDataSockReady = 1;
}
connect_state_datac = 0;
connect_state_data_ftpc = 0;
break;
default :
break;
}
#endif
return 0;
}
@ -496,55 +473,58 @@ char proc_ftpc(char * buf)
Responses =(buf[0]-'0')*100+(buf[1]-'0')*10+(buf[2]-'0');
switch(Responses){
case R_220:
case R_220: /* Service ready for new user. */
printf("\r\nInput your User ID > ");
sprintf(dat,"USER %s\r\n", User_Keyboard_MSG());
printf("\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case R_331:
case R_331: /* User name okay, need password. */
printf("\r\nInput your Password > ");
sprintf(dat,"PASS %s\r\n", User_Keyboard_MSG());
printf("\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case R_230:
case R_230: /* User logged in, proceed */
printf("\r\nUser logged in, proceed\r\n");
sprintf(dat,"TYPE %c\r\n", TransferAscii);
gModeAsciiBinary = TransferAscii;
ftpc.type = ASCII_TYPE;
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case R_200:
if((gModeActivePassive==ConnectActive)&&gModeActivePassiveflag){
ftp.dsock_mode = ACTIVE_MODE;
ftp.dsock_state = DATASOCK_READY;
if((ftpc.dsock_mode==ACTIVE_MODE)&&gModeActivePassiveflag){
ftpc.dsock_state = DATASOCK_READY;
gModeActivePassiveflag = 0;
}
else{
gMenuStart = 1;
}
break;
case R_150:
switch(Command.First){
case f_dir:
Command.First = f_nocmd;
Command.Second = s_dir;
gDataPutGetStart = 1;
break;
case f_get:
Command.First = f_nocmd;
Command.Second = s_get;
gDataPutGetStart = 1;
break;
case f_put:
Command.First = f_nocmd;
Command.Second = s_put;
gDataPutGetStart = 1;
break;
default :
printf("shit!!!! Command.First = default\r\n");
printf("Command.First = default\r\n");
break;
}
break;
case R_226: /* Closing data connection. File transfer/abort successful */
case R_226:
gMenuStart = 1;
break;
case R_227:
@ -553,8 +533,8 @@ char proc_ftpc(char * buf)
}
else{
printf("Go Open Data Sock...\r\n ");
ftp.dsock_mode = PASSIVE_MODE;
ftp.dsock_state = DATASOCK_READY;
ftpc.dsock_mode = PASSIVE_MODE;
ftpc.dsock_state = DATASOCK_READY;
}
break;
default:
@ -592,22 +572,13 @@ int pportc(char * arg)
printf("ip : %d.%d.%d.%d, port : %d\r\n", remote_ip.cVal[0], remote_ip.cVal[1], remote_ip.cVal[2], remote_ip.cVal[3], remote_port);
return 0;
}
#if defined(F_FILESYSTEM)
void print_filedsc(FIL *fil)
uint8_t* User_Keyboard_MSG()
{
#if defined(_FTP_DEBUG_)
printf("File System pointer : %08X\r\n", fil->fs);
printf("File System mount ID : %d\r\n", fil->id);
printf("File status flag : %08X\r\n", fil->flag);
printf("File System pads : %08X\r\n", fil->err);
printf("File read write pointer : %08X\r\n", fil->fptr);
printf("File size : %08X\r\n", fil->fsize);
printf("File start cluster : %08X\r\n", fil->sclust);
printf("current cluster : %08X\r\n", fil->clust);
printf("current data sector : %08X\r\n", fil->dsect);
printf("dir entry sector : %08X\r\n", fil->dir_sect);
printf("dir entry pointer : %08X\r\n", fil->dir_ptr);
#endif
uint8_t i=0;
do{
gMsgBuf[i] = ftp_getc();
i++;
}while(gMsgBuf[i-1]!=0x0d);
gMsgBuf[i-1]=0;
return gMsgBuf;
}
#endif

View File

@ -1,77 +1,106 @@
#ifndef _FTPC_H_
#define _FTPC_H_
#include <stdint.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include <stdarg.h>
#include "stdio_private.h"
#include "socket.h"
#define F_FILESYSTEM // If your target support a file system, you have to activate this feature and implement.
#define F_APP_FTPC
#if defined(F_FILESYSTEM)
/* If your target support a file system, you have to activate this feature and implement. */
//#define F_FILESYSTEM
/* Change to your Chipset Uart function, you have to activate this feature and implement.
* Change!! -> Board_UARTGetCharBlocking()
* Below is an example of a function of lpc_chip library. */
//#define ftp_getc() Board_UARTGetCharBlocking()
#ifdef F_FILESYSTEM
#include "ff.h"
#endif
#define F_APP_FTP
#define _FTP_DEBUG_
#define F_APP_FTP_CLIENT
#ifndef ftp_getc()
#define Need_UARTGetCharBlocking_func
#else
/* Change library
* Change!! -> board_api.h,
* Below is an example of a function of lpc_chip library. */
#include "board_api.h"
#endif
#define LINELEN 100
//#define DATA_BUF_SIZE 100
#if !defined(F_FILESYSTEM)
#ifndef F_FILESYSTEM
#define _MAX_SS 512
#endif
#define CTRL_SOCK 2
#define DATA_SOCK 3
#define IPPORT_FTPD 20 /* FTP Data port */
#define IPPORT_FTP 21 /* FTP Control port */
/* FTP Responses */
#define R_150 150 /* File status ok; opening data conn */
#define R_200 200 /* 'Generic' command ok */
#define R_220 220 /* Service ready for new user. */
#define R_226 226 /* Closing data connection. File transfer/abort successful */
#define R_227 227 /* Entering passive mode (h1,h2,h3,h4,p1,p2) */
#define R_230 230 /* User logged in, proceed */
#define R_331 331 /* User name okay, need password. */
#define HOSTNAME "iinChip"
#define VERSION "1.0"
#define TransferAscii 'A'
#define TransferBinary 'I'
#define FILENAME "a.txt"
enum ftp_type {
enum ftpc_type {
ASCII_TYPE,
IMAGE_TYPE,
LOGICAL_TYPE
};
enum ftp_state {
FTPS_NOT_LOGIN,
FTPS_LOGIN
};
enum datasock_state{
enum ftpc_datasock_state{
DATASOCK_IDLE,
DATASOCK_READY,
DATASOCK_START
};
enum datasock_mode{
enum ftpc_datasock_mode{
PASSIVE_MODE,
ACTIVE_MODE
};
struct ftpd {
enum CommandFirst {
f_nocmd,
f_dir,
f_put,
f_get,
};
enum CommandSecond {
s_nocmd,
s_dir,
s_put,
s_get,
};
struct Command {
enum CommandFirst First;
enum CommandSecond Second;
};
struct ftpc {
uint8_t control; /* Control stream */
uint8_t data; /* Data stream */
uint8_t data; /* Data stream */
enum ftp_type type; /* Transfer type */
enum ftp_state state;
enum ftpc_type type; /* Transfer type */
enum datasock_state dsock_state;
enum datasock_mode dsock_mode;
enum ftpc_datasock_state dsock_state;
enum ftpc_datasock_mode dsock_mode;
char username[LINELEN]; /* Arg to USER command */
char workingdir[LINELEN];
char filename[LINELEN];
#if defined(F_FILESYSTEM)
#ifdef F_FILESYSTEM
FIL fil; // FatFs File objects
FRESULT fr; // FatFs function common result code
#endif
};
#ifndef un_I2cval
@ -87,13 +116,4 @@ char proc_ftpc(char * buf);
int pportc(char * arg);
uint8_t* User_Keyboard_MSG();
int sendit(char * command);
int recvit(char * command);
long sendfile(uint8_t s, char * command);
long recvfile(uint8_t s);
#if defined(F_FILESYSTEM)
void print_filedsc(FIL *fil);
#endif
#endif // _FTPC_H_

View File

@ -1,6 +1,6 @@
#ioLibrary Driver
The ioLibrary means “Internet Offload Library” for WIZnet chip. It includes drivers and application protocols.
The driver (ioLibrary) can be used for [W5500](http://wizwiki.net/wiki/doku.php?id=products:w5500:start) application designs. These will be updated continuously.
The driver (ioLibrary) can be used for the application design of WIZnet TCP/IP chips as [W5500](http://wizwiki.net/wiki/doku.php?id=products:w5500:start), W5200, W5100 and etc.
##ioLibrary
This driver provides the Berkeley Socket type APIs.
@ -16,6 +16,17 @@ This driver provides the Berkeley Socket type APIs.
## How to add an ioLibrary in project through github site.
- Example, refer to https://www.youtube.com/watch?v=mt815RBGdsA
- [ioLibrary Doxygen doument](https://github.com/Wiznet/ioLibrary_Driver/blob/master/Ethernet/SOCKET_APIs_V2.0.chm) : Refer to **TODO** in this document
- Define what chip is used in **wizchip_conf.h**
- Define what Host I/F mode is used in **wizchip_conf.h**
## Revision History
Last release : Nov. 2014
* Type casting error Fixed : 09, April. 2015
In socket.c, send() : Refer to M20150409
* ioLibrary V2.0 released : April. 2015
* Added to W5100, W5200
* Correct to some typing error
* Fixed the warning of type casting.
* Last release : Nov. 2014