diff --git a/APP_Framework/Applications/app_test/Makefile b/APP_Framework/Applications/app_test/Makefile index 6b0c1ea96..ee75fe950 100644 --- a/APP_Framework/Applications/app_test/Makefile +++ b/APP_Framework/Applications/app_test/Makefile @@ -134,7 +134,8 @@ ifeq ($(CONFIG_ADD_XIZI_FEATURES),y) endif ifeq ($(CONFIG_USER_TEST_FTPCLIENT),y) - SRC_FILES += + SRC_FILES += test_ftpclient/test_ftpclient.c test_ftpclient/ftp_client/ftp_client.c\ + test_ftpclient/ftp_client/my_socket.c endif ifeq ($(CONFIG_USER_TEST_LORA_P2P),y) diff --git a/APP_Framework/Applications/app_test/test_ftpclient/README.md b/APP_Framework/Applications/app_test/test_ftpclient/README.md new file mode 100644 index 000000000..3905e33aa --- /dev/null +++ b/APP_Framework/Applications/app_test/test_ftpclient/README.md @@ -0,0 +1,60 @@ +# 初赛一级赛题3:基于矽璓已实现的Lwip,在ARM上实现FTP协议的Client功能 + +## 1. 简介 +本项目是基于矽璓已实现的Lwip,在ARM上实现FTP协议的Client功能 +test_ftpclient.h声明了下载10个文件的测试函数 +test_ftpclient.c实现了下载10个文件的测试函数 +ftp_client文件夹定义了ftp_client的相关类库其中my_socket.h,my_socket.c定义了socket抽象层,并基于 +Lwip实现了该抽象层,ftp_client.h,ftp_client.c实现了ftp登录,获取文件大小,下载文件等功能 + +## 2. 数据结构设计说明 +- ftp_client.c 的设计 +分别定义了发送命令和接收数据的socket和相应的缓冲区并且实现了登录、发送命令、接收响应数据、查找文件大小、进入被动模式、下载文件、关闭ftp客户端等操作 +```c +static int m_socket_cmd; // 发送命令的socket文件描述符 +static int m_socket_data; // 接收ftp服务器文件的socket文件描述符 +static char m_send_buffer[1024]; // 发送缓冲区 +static char m_recv_buffer[1024]; // 接收缓冲区 +``` +## 3. 测试程序说明 +- test_ftpclient.c用于测试下载10个文件 +连接电脑上的ftp服务器下载10个4KB文件,通过终端日志打印确定文件是否下载成功,以及追踪通信流程和下载进度 +```c +void TestFtpClient(int argc, char* argv[]) +{ + FtpInitCmd(); + int ret = FtpLogin("192.168.0.248", 21, "anonymous", "anonymous"); + int size; + char *buf; + for(int i = 1;i <= 10;i++){ + char fileName[20] = "/file"; + char temp[5] = ""; + sprintf(temp,"%d",i); + strcat(fileName,temp); + size = FtpFileSize(fileName); + buf = malloc(size); + FtpInitData(); // data socket 每次下载都要重新创建,下载完都要关闭 + ret = FtpDownload(fileName, buf, size); + free(buf); + } + FtpQuit(); + return; +} +``` + +## 4. 运行结果(##需结合运行测试截图按步骤说明##) +1. 配置开启BSP_USING_LWIP、USER_TEST_FTPCLIENT +![](./img/image.png) +![](./img/image-1.png) +2. 编译 +![](./img/image-2.png) +3. 烧写 +![](./img/image-3.png) +4. xshell连接串口终端 +![](./img/image-4.png) +6. 配置ip +![](./img/image-5.png) +7. 运行TestFtpClient,开始下载文件 +![](./img/image-6.png) +![](./img/image-7.png) +![](./img/image-8.png) \ No newline at end of file diff --git a/APP_Framework/Applications/app_test/test_ftpclient/ftp_client/ftp_client.c b/APP_Framework/Applications/app_test/test_ftpclient/ftp_client/ftp_client.c new file mode 100644 index 000000000..e1ac107ec --- /dev/null +++ b/APP_Framework/Applications/app_test/test_ftpclient/ftp_client/ftp_client.c @@ -0,0 +1,256 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** +* @file: ftp_client.c +* @brief: ftp client tool +* @version: 1.0 +* @author: bdislab_final +* @date: 2023/7/25 +* @reference https://kerndev.blog.csdn.net/article/details/89383888 +*/ + +#include +#include +#include +#include +#include "my_socket.h" +#include "ftp_client.h" + +static int m_socket_cmd; +static int m_socket_data; +static char m_send_buffer[1024]; +static char m_recv_buffer[1024]; + +static int FtpSendCommand(char *cmd) +{ + int ret; + printf("send command: %s\r\n", cmd); + ret = SocketSend(m_socket_cmd, cmd, (int)strlen(cmd)); + if(ret < 0) + { + printf("failed to send command: %s\r\n",cmd); + return 0; + } + return 1; +} + +static int FtpRecvRespond(char *resp, int len) +{ + int ret; + int off; + len -= 1; + for(off=0; off +#include +#include + +int SocketCreate(void){ + return socket(AF_INET,SOCK_STREAM,0); +} + +int SocketConnect(int sock, const char *addr, int port){ + // unsigned int iRemoteAddr = 0; + struct sockaddr_in stRemoteAddr = {0}; //对端,即目标地址信息 + + stRemoteAddr.sin_family = AF_INET; + stRemoteAddr.sin_port = htons(port); + stRemoteAddr.sin_addr.s_addr = inet_addr(addr); + // inet_pton(AF_INET, addr, &iRemoteAddr); + // stRemoteAddr.sin_addr.s_addr=iRemoteAddr; + int res = connect(sock,(struct sockaddr *)&stRemoteAddr,sizeof(stRemoteAddr)); + if(res == -1){ + printf("error:%d\n",errno); + } + return res == 0 ? 1 : 0; +} + +int SocketSend(int sock, void *data, int len){ + return send(sock,data,len,0); +} + +int SocketRecv(int sock, void *data, int len){ + return recv(sock,data,len,0); +} + +void SocketClose(int sock){ + close(sock); +} diff --git a/APP_Framework/Applications/app_test/test_ftpclient/ftp_client/my_socket.h b/APP_Framework/Applications/app_test/test_ftpclient/ftp_client/my_socket.h new file mode 100644 index 000000000..2b3aee813 --- /dev/null +++ b/APP_Framework/Applications/app_test/test_ftpclient/ftp_client/my_socket.h @@ -0,0 +1,46 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** + * @file my_socket.h + * @brief a abstract socket api + * @version 1.0 + * @author bdislab_final + * @date 2023/7/25 + */ + +#ifndef MYSOCKET_H +#define MYSOCKET_H + +#ifdef __cplusplus +extern "C" { +#endif +/* create a socket */ +int SocketCreate(void); + +/* connect a socket */ +int SocketConnect(int sock, const char *addr, int port); + +/* send data through socket*/ +int SocketSend(int sock, void *data, int len); + +/* receive data from socket*/ +int SocketRecv(int sock, void *data, int len); + +/* close socket*/ +void SocketClose(int sock); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/APP_Framework/Applications/app_test/test_ftpclient/img/image-1.png b/APP_Framework/Applications/app_test/test_ftpclient/img/image-1.png new file mode 100644 index 000000000..0d050f2ca Binary files /dev/null and b/APP_Framework/Applications/app_test/test_ftpclient/img/image-1.png differ diff --git a/APP_Framework/Applications/app_test/test_ftpclient/img/image-2.png b/APP_Framework/Applications/app_test/test_ftpclient/img/image-2.png new file mode 100644 index 000000000..39b7fdc20 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_ftpclient/img/image-2.png differ diff --git a/APP_Framework/Applications/app_test/test_ftpclient/img/image-3.png b/APP_Framework/Applications/app_test/test_ftpclient/img/image-3.png new file mode 100644 index 000000000..a22bb75d2 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_ftpclient/img/image-3.png differ diff --git a/APP_Framework/Applications/app_test/test_ftpclient/img/image-4.png b/APP_Framework/Applications/app_test/test_ftpclient/img/image-4.png new file mode 100644 index 000000000..30de94357 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_ftpclient/img/image-4.png differ diff --git a/APP_Framework/Applications/app_test/test_ftpclient/img/image-5.png b/APP_Framework/Applications/app_test/test_ftpclient/img/image-5.png new file mode 100644 index 000000000..729f01a77 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_ftpclient/img/image-5.png differ diff --git a/APP_Framework/Applications/app_test/test_ftpclient/img/image-6.png b/APP_Framework/Applications/app_test/test_ftpclient/img/image-6.png new file mode 100644 index 000000000..dc3672a47 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_ftpclient/img/image-6.png differ diff --git a/APP_Framework/Applications/app_test/test_ftpclient/img/image-7.png b/APP_Framework/Applications/app_test/test_ftpclient/img/image-7.png new file mode 100644 index 000000000..97bb36a30 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_ftpclient/img/image-7.png differ diff --git a/APP_Framework/Applications/app_test/test_ftpclient/img/image-8.png b/APP_Framework/Applications/app_test/test_ftpclient/img/image-8.png new file mode 100644 index 000000000..25a5fc307 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_ftpclient/img/image-8.png differ diff --git a/APP_Framework/Applications/app_test/test_ftpclient/img/image.png b/APP_Framework/Applications/app_test/test_ftpclient/img/image.png new file mode 100644 index 000000000..9775c39a5 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_ftpclient/img/image.png differ diff --git a/APP_Framework/Applications/app_test/test_ftpclient/test_ftpclient.c b/APP_Framework/Applications/app_test/test_ftpclient/test_ftpclient.c new file mode 100644 index 000000000..a97858799 --- /dev/null +++ b/APP_Framework/Applications/app_test/test_ftpclient/test_ftpclient.c @@ -0,0 +1,50 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** +* @file: test_ftpclient.c +* @brief: a ftpClient test sample +* @version: 1.0 +* @author: bdislab_final +* @date: 2023/7/25 +*/ + +#include "test_ftpclient.h" +#include "ftp_client/ftp_client.h" +#include +#include +#include +#include + + +/* test for ftp client */ +void TestFtpClient(int argc, char* argv[]) +{ + FtpInitCmd(); + int ret = FtpLogin("192.168.1.248", 21, "anonymous", "anonymous"); + int size; + char *buf; + for(int i = 1;i <= 10;i++){ + char fileName[20] = "/file"; + char temp[5] = ""; + sprintf(temp,"%d",i-1); + strcat(fileName,temp); + size = FtpFileSize(fileName); + buf = malloc(size); + FtpInitData(); // data socket 每次下载都要重新创建,下载完都要关闭 + ret = FtpDownload(fileName, buf, size); + free(buf); + } + FtpQuit(); + return; +} +PRIV_SHELL_CMD_FUNCTION(TestFtpClient, a ftpClient test sample, PRIV_SHELL_CMD_MAIN_ATTR); diff --git a/APP_Framework/Applications/app_test/test_ftpclient/test_ftpclient.h b/APP_Framework/Applications/app_test/test_ftpclient/test_ftpclient.h new file mode 100644 index 000000000..d35e65cab --- /dev/null +++ b/APP_Framework/Applications/app_test/test_ftpclient/test_ftpclient.h @@ -0,0 +1,35 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** + * @file test_ftpclient.h + * @brief a ftpClient test sample + * @version 1.0 + * @author bdislab_final + * @date 2023/7/25 + */ + +#ifndef TEST_FTPCLIENT_H +#define TEST_FTPCLIENT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* test for ftp client */ +void TestFtpClient(int argc, char* argv[]); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file