FTP用户接口文件的实现
KEIL官网有提供FTP的接口文件,名为FTPC_uif.c文件。我们就是在这个文件上修改。具体修改后的代码如下:
#include
#include
#define FTPC_USERNAME "armfly" /* 远程FTP服务器的账号 */
#define FTPC_PASSWORD "123456" /* 远程FTP服务器的密码 */
#define FTPC_PATH "" /* 要访问的子文件夹,FTP电脑端设置后是没有路径的,填空 */
#define FTPC_FILENAME "server.pdf" /* 要访问的文件 */
#define FTPC_NEWNAME "renamed.pdf" /* 使用这里定义的文件字重命名宏定义FTPC_FILENAME的文件名 */
#define FTPC_DIRNAME "New_Folder" /* 创建名字为New_Folder的文件夹或者删除名字为New_Folder的文件夹 */
#define FTPC_LISTNAME "*" /* 浏览宏定义FTPC_PATH路径下所有文件 */
#define LOCAL_FILE "client.pdf" /* 开发板SD卡里面要上传的文件,或者从FTP服务器下载文件后,
文件会被设置成此名字 */
/*----------------------------------------------------------------------------
* FTP Client File Access and Data CallBack Functions
*---------------------------------------------------------------------------*/
/*--------------------------- ftpc_fopen ------------------------------------*/
void *ftpc_fopen (U8 *mode) {
/* Open local file for reading or writing. If the return value is NULL, */
/* processing of FTP Client commands PUT, APPEND or GET is cancelled. */
return (fopen (LOCAL_FILE, (char *)mode));
}
/*--------------------------- ftpc_fclose -----------------------------------*/
void ftpc_fclose (void *file) {
/* Close a local file. */
fclose (file);
}
/*--------------------------- ftpc_fread ------------------------------------*/
U16 ftpc_fread (void *file, U8 *buf, U16 len) {
/* Read 'len' bytes from file to buffer 'buf'. Return number of bytes */
/* copied. The file will be closed, when the return value is 0. */
/* For optimal performance the return value should be 'len' */
return (fread (buf, 1, len, file));
}
/*--------------------------- ftpc_fwrite -----------------------------------*/
U16 ftpc_fwrite (void *file, U8 *buf, U16 len) {
/* Write 'len' bytes from buffer 'buf' to a file. Data transfer will be */
/* aborted, if the return value is not equal 'len'. */
return (fwrite (buf, 1, len, file));
}
/*--------------------------- ftpc_cbfunc -----------------------------------*/
U16 ftpc_cbfunc (U8 code, U8 *buf, U16 buflen) {
/* This function is called by the FTP client to get parameters. It returns*/
/* the number of bytes written to buffer 'buf'. This function should NEVER*/
/* write more than 'buflen' bytes to this buffer. */
/* Parameters: */
/* code - function code with following values: */
/* 0 = Username for FTP authentication */
/* 1 = Password for FTP authentication */
/* 2 = Working directory path for all commands */
/* 3 = Filename for PUT, GET, APPEND, DELETE, RENAME command */
/* 4 - New filename for RENAME command */
/* 5 - Directory name for MKDIR, RMDIR command */
/* 6 - File filter/mask for LIST command (wildcards allowed) */
/* 7 = Received directory listing on LIST command */
/* buf - transmit/receive buffer */
/* buflen - length of this buffer */
/* on transmit, it specifies size of 'buf' */
/* on receive, specifies the number of bytes received */
U32 i,len = 0;
switch (code) {
case 0:
/* Enter Username for login. */
len = str_copy (buf, FTPC_USERNAME);
break;
case 1:
/* Enter Password for login. */
len = str_copy (buf, FTPC_PASSWORD);
break;
case 2:
/* Enter a working directory path. */
len = str_copy (buf, FTPC_PATH);
break;
case 3:
/* Enter a filename for file operations. */
len = str_copy (buf, FTPC_FILENAME);
break;
case 4:
/* Enter a new name for rename command. */
len = str_copy (buf, FTPC_NEWNAME);
break;
case 5:
/* Enter a directory name to create or remove. */
len = str_copy (buf, FTPC_DIRNAME);
break;
case 6:
/* Enter a file filter for list command. */
len = str_copy (buf, FTPC_LISTNAME);
break;
case 7:
/* Process received directory listing in raw format. */
/* Function return value is don't care. */
for (i = 0; i < buflen; i++) {
putchar (buf[i]);
}
break;
}
return ((U16)len);
}
/*----------------------------------------------------------------------------
* end of file
*---------------------------------------------------------------------------*/ |