Compare commits

...

No commits in common. "gd32f405ret6" and "master" have entirely different histories.

17060 changed files with 2 additions and 6798960 deletions

View File

@ -1,11 +0,0 @@
BasedOnStyle: LLVM
IndentWidth: 4
IndentAccessModifiers: false
AccessModifierOffset: -4
DerivePointerAlignment: false
PointerAlignment: Left
SortIncludes: CaseSensitive
IndentPPDirectives: BeforeHash
AlignAfterOpenBracket: BlockIndent
BinPackArguments: false
BinPackParameters: false

15
.gitignore vendored
View File

@ -1,15 +0,0 @@
*.vscode
*.o
*libmusl.a
*liblwip.a
.DS_Store
*.pyc
.cache/
compile_commands.json
.clangd
Ubiquitous/XiZi_AIoT/services/app/bin/*
Ubiquitous/XiZi_AIoT/build/*
Ubiquitous/XiZi_AIoT/services/app/fs.img
Ubiquitous/XiZi_AIoT/services/tools/mkfs/mkfs
APP_Framework/Framework/knowing/micropython/build/*

31
.gitmodules vendored
View File

@ -1,31 +0,0 @@
[submodule "Ubiquitous/RT-Thread_Fusion_XiUOS/rt-thread"]
path = Ubiquitous/RT-Thread_Fusion_XiUOS/rt-thread
url = https://www.gitlink.org.cn/chunyexixiaoyu/rt-thread.git
[submodule "Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/k210/kendryte-sdk/kendryte-sdk-source"]
path = Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/k210/kendryte-sdk/kendryte-sdk-source
url = https://www.gitlink.org.cn/chunyexixiaoyu/kendryte-sdk-source.git
[submodule "Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/aiit-riscv64-board/kendryte-sdk/kendryte-sdk-source"]
path = Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/aiit-riscv64-board/kendryte-sdk/kendryte-sdk-source
url = https://www.gitlink.org.cn/chunyexixiaoyu/kendryte-sdk-source.git
[submodule "Ubiquitous/XiZi/fs/lwext4/lwext4_submodule"]
path = Ubiquitous/XiZi/fs/lwext4/lwext4_submodule
url = https://gitlink.org.cn/xuos/lwext4_filesystem_support_XiUOS.git
[submodule "Ubiquitous/Nuttx_Fusion_XiUOS/nuttx"]
path = Ubiquitous/Nuttx_Fusion_XiUOS/nuttx
url = https://www.gitlink.org.cn/wgzAIIT/incubator-nuttx.git
[submodule "Ubiquitous/Nuttx_Fusion_XiUOS/apps"]
path = Ubiquitous/Nuttx_Fusion_XiUOS/apps
url = https://www.gitlink.org.cn/wgzAIIT/incubator-nuttx-apps.git
[submodule "APP_Framework/Applications/webnet/WebNet_XiUOS"]
path = APP_Framework/Applications/webnet/WebNet_XiUOS
url = https://gitlink.org.cn/xuos/WebNet_XiUOS.git
[submodule "Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/xidatong-riscv64/kendryte-sdk/kendryte-sdk-source"]
path = Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/xidatong-riscv64/kendryte-sdk/kendryte-sdk-source
url = https://www.gitlink.org.cn/chunyexixiaoyu/kendryte-sdk-source.git
[submodule "APP_Framework/lib/lorawan/lorawan_devicenode"]
path = APP_Framework/lib/lorawan/lorawan_devicenode
url = https://gitlink.org.cn/xuos/lorawan_devicenode.git
branch = master
[submodule "APP_Framework/lib/lorawan/lorawan_gateway_single_channel"]
path = APP_Framework/lib/lorawan/lorawan_gateway_single_channel
url = https://gitlink.org.cn/xuos/lorawan_gateway_single_channel.git

View File

@ -1,24 +0,0 @@
menu "Applications"
menu "config stack size and priority of main task"
config MAIN_KTASK_STACK_SIZE
int "Set main task stack size"
default 1024
config MAIN_KTASK_PRIORITY
int
default 4 if KTASK_PRIORITY_8
default 16 if KTASK_PRIORITY_32
default 85 if KTASK_PRIORITY_256
endmenu
source "$APP_DIR/Applications/ota/Kconfig"
source "$APP_DIR/Applications/app_test/Kconfig"
source "$APP_DIR/Applications/connection_app/Kconfig"
source "$APP_DIR/Applications/control_app/Kconfig"
source "$APP_DIR/Applications/knowing_app/Kconfig"
source "$APP_DIR/Applications/sensor_app/Kconfig"
source "$APP_DIR/Applications/embedded_database_app/Kconfig"
source "$APP_DIR/Applications/webnet/Kconfig"
source "$APP_DIR/Applications/webserver/Kconfig"
endmenu

View File

@ -1,7 +0,0 @@
############################################################################
# APP_Framework/Applications/Make.defs
############################################################################
CONFIGURED_APPS += $(APPDIR)/../../../APP_Framework/Applications
CONFIGURED_APPS += $(APPDIR)/../../../APP_Framework/Applications/general_functions/list
include $(wildcard $(APPDIR)/../../../APP_Framework/Applications/*/Make.defs)

View File

@ -1,48 +0,0 @@
include $(KERNEL_ROOT)/.config
ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
include $(APPDIR)/Make.defs
CSRCS +=
include $(APPDIR)/Application.mk
endif
ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
SRC_DIR := general_functions app_test
SRC_FILES := main.c
ifeq ($(CONFIG_LIB_LV),y)
SRC_DIR += lv_app
SRC_DIR += tinyttf_app
endif
ifeq ($(CONFIG_APPLICATION_OTA),y)
SRC_DIR += ota
endif
ifeq ($(CONFIG_APPLICATION_SENSOR),y)
SRC_DIR += sensor_app
endif
ifeq ($(CONFIG_APPLICATION_CONNECTION),y)
SRC_DIR += connection_app
endif
ifeq ($(CONFIG_APPLICATION_KNOWING),y)
SRC_DIR += knowing_app
endif
ifeq ($(CONFIG_APPLICATION_CONTROL),y)
SRC_DIR += control_app
endif
ifeq ($(CONFIG_APP_USING_WEBNET),y)
SRC_DIR += webnet
endif
ifeq ($(CONFIG_APPLICATION_WEBSERVER),y)
SRC_DIR += webserver
endif
include $(KERNEL_ROOT)/compiler.mk
endif

View File

@ -1,20 +0,0 @@
import os
Import('RTT_ROOT')
Import('rtconfig')
from building import *
cwd = GetCurrentDir()
SOURCES = ['framework_init.c']
path = [cwd]
objs = []
group = DefineGroup('sensor', SOURCES, depend = [], CPPPATH = [cwd])
objs = objs + group
list = os.listdir(cwd)
for d in list:
path = os.path.join(cwd, d)
if os.path.isfile(os.path.join(path, 'SConscript')):
objs = objs + SConscript(os.path.join(path, 'SConscript'))
Return('objs')

View File

@ -1,320 +0,0 @@
menu "test app"
menuconfig USER_TEST
bool "Enable application test function "
default n
if USER_TEST
menuconfig USER_TEST_ADC
bool "Config test adc"
default n
if USER_TEST_ADC
if ADD_XIZI_FEATURES
config ADC_DEV_DRIVER
string "Set ADC dev path"
default "/dev/adc1_dev"
endif
endif
menuconfig USER_TEST_DAC
bool "Config test dac"
default n
if USER_TEST_DAC
if ADD_XIZI_FEATURES
config DAC_DEV_DRIVER
string "Set DAC dev path"
default "/dev/dac_dev"
endif
endif
menuconfig USER_TEST_FS
bool "Config test fs with sd or usb"
default n
if USER_TEST_FS
if ADD_XIZI_FEATURES
config FPATH
string "Set test file path"
default "/test_file"
endif
endif
menuconfig USER_TEST_GPIO
select BSP_USING_GPIO
select RESOURCES_PIN
select BSP_USING_LED
select BSP_USING_KEY
bool "Config test gpio with led and key"
default n
if USER_TEST_GPIO
if ADD_XIZI_FEATURES
config GPIO_DEV_DRIVER
string "Set gpio dev path"
default "/dev/pin_dev"
endif
endif
menuconfig USER_TEST_LORA
select BSP_USING_UART
select BSP_USING_GPIO
select RESOURCES_PIN
select BSP_USING_UART2
select BSP_USING_LORA
bool "Config test uart(loraE220)"
default n
if USER_TEST_LORA
if ADD_XIZI_FEATURES
config LORA_UART_DEV_DRIVER
string "Set uart dev path"
default "/dev/uart2_dev2"
config LORA_PIN_DEV_DRIVER
string "Set pin dev path"
default "/dev/pin_dev"
endif
endif
menuconfig USER_TEST_SOCKET
select BSP_USING_LWIP
bool "Config test socket(lwip)"
default n
menuconfig USER_TEST_UART
select BSP_USING_UART
select BSP_USING_UART6
bool "Config test uart"
default n
if USER_TEST_UART
if ADD_XIZI_FEATURES
config UART_DEV_DRIVER
string "Set uart dev path"
default "/dev/usart6_dev6"
endif
endif
menuconfig USER_TEST_RS485
select BSP_USING_UART
select BSP_USING_GPIO
select RESOURCES_PIN
select BSP_USING_UART1
bool "Config test uart(RS485)"
default n
if USER_TEST_RS485
if ADD_XIZI_FEATURES
config RS485_UART_DEV_DRIVER
string "Set uart dev path"
default "/dev/uart1_dev1"
config RS485_PIN_DEV_DRIVER
string "Set pin dev path"
default "/dev/pin_dev"
endif
endif
menuconfig USER_TEST_RTC
select BSP_USING_RTC
bool "Config test rtc"
default n
if USER_TEST_RTC
if ADD_XIZI_FEATURES
config RTC_DEV_DRIVER
string "Set rtc dev path"
default "/dev/rtc_dev"
endif
endif
menuconfig USER_TEST_HWTIMER
select BSP_USING_HWTIMER
select BSP_USING_GPIO
select RESOURCES_PIN
select BSP_USING_LED
bool "Config test hwtimer"
default n
if USER_TEST_HWTIMER
if ADD_XIZI_FEATURES
config HWTIMER_TIMER_DEV_DRIVER
string "Set pin dev path"
default "/dev/timer0_dev0"
config HWTIMER_PIN_DEV_DRIVER
string "Set pin dev path"
default "/dev/pin_dev"
endif
endif
menuconfig USER_TEST_WDT
select BSP_USING_WDT0
bool "Config test watchdog"
default n
if USER_TEST_WDT
if ADD_XIZI_FEATURES
config WDT0_DEV_DRIVER
string "Set wdt dev path"
default "/dev/wdt0_dev0"
endif
endif
menuconfig USER_TEST_LCD_EDU
select BSP_USING_LCD
bool "Config test lcd in PrivOpen"
default n
if USER_TEST_LCD_EDU
if ADD_XIZI_FEATURES
config EDU_LCD_DEV_DRIVER
string "Set lcd dev path"
default "/dev/lcd_dev"
endif
endif
menuconfig USER_TEST_TOUCH
select BSP_USING_TOUCH
bool "Config test touch"
default n
if USER_TEST_TOUCH
if ADD_XIZI_FEATURES
config TOUCH_DEV_DRIVER
string "Set touch dev path"
default "/dev/touch_dev"
config TOUCH_LCD_DEV_DRIVER
string "Set lcd dev path"
default "/dev/lcd_dev"
endif
endif
menuconfig USER_TEST_I2C
select BSP_USING_I2C
bool "Config test i2c"
default n
if USER_TEST_I2C
if ADD_XIZI_FEATURES
config I2C_DEV_DRIVER
string "Set i2c dev path"
default "/dev/i2c1_dev0"
endif
endif
menuconfig USER_TEST_CAN
select BSP_USING_CAN
bool "Config test can"
default n
if USER_TEST_CAN
if ADD_XIZI_FEATURES
config CAN_DEV_DRIVER
string "Set can dev path"
default "/dev/can2_dev1"
endif
endif
menuconfig USER_TEST_CAMERA
select BSP_USING_CAMERA
select BSP_USING_LCD
bool "Config test camera with lcd"
default n
if USER_TEST_CAMERA
if ADD_XIZI_FEATURES
config CAMERA_DEV_DRIVER
string "Set camera dev path"
default "/dev/camera_dev"
config CAMERA_LCD_DEV_DRIVER
string "Set lcd dev path"
default "/dev/lcd_dev"
endif
endif
config USER_TEST_SEMC
bool "Config test semc sdram"
default n
config USER_TEST_LCD
bool "Config test lcd device"
default n
menuconfig USER_TEST_ETHERNET
bool "Config test ethernet only for edu-riscv64"
default n
if USER_TEST_ETHERNET
if ADD_XIZI_FEATURES
choice
prompt "set ethernet role as client or server"
default ETHERNET_AS_SERVER
config ETHERNET_AS_SERVER
bool "test as server"
config ETHERNET_AS_CLIENT
bool "test as client"
endchoice
endif
endif
menuconfig USER_TEST_FLASH
bool "Config test w25q128 device"
default n
if USER_TEST_FLASH
if ADD_XIZI_FEATURES
config FLASH_DEV_DRIVER
string "Set flash dev path"
default "/dev/qspi_W25Q128"
endif
endif
menuconfig USER_TEST_TIMER
bool "Config test soft timer"
default n
menuconfig USER_TEST_HASH
bool "Config test hash"
default n
menuconfig USER_TEST_RADIX
bool "Config test radix tree"
default n
menuconfig USER_TEST_RBTREE
bool "Config test red black tree"
default n
menuconfig USER_TEST_MODBUS_TCP
bool "Config test modbus_tcp"
default n
menuconfig USER_TEST_WEBSERVER
bool "Config test webserver"
default n
menuconfig USER_TEST_MQTTCLIENT
bool "Config test mqtt client"
default n
select LIB_USING_CJSON
menuconfig USER_TEST_FTPCLIENT
bool "Config test ftp client"
default n
menuconfig USER_TEST_FTPCLIENT_RISCV
bool "Config test ftp client on riscv"
default n
menuconfig USER_TEST_LORA_P2P
bool "Config test lora p2p"
default n
menuconfig USER_TEST_LORAWAN_SINGLEGW
bool "Config test lorawan single channel gateway"
default n
menuconfig USER_TEST_CANOPEN
bool "Config test CanOpen"
default n
menuconfig USER_TEST_USB_CAMERA
bool "Config test usb camera"
default n
menuconfig USER_TEST_MPU
bool "Config test MPU fault (Task Isolation)"
default n
depends on TASK_ISOLATION
help
Enable MPU fault testing for task isolation feature.
This test creates user tasks that trigger controlled MPU violations
to verify memory protection is working correctly.
endif
endmenu

View File

@ -1,7 +0,0 @@
############################################################################
# APP_Framework/Applications/app_test/Make.defs
############################################################################
CONFIGURED_APPS += $(APPDIR)/../../../APP_Framework/Applications/app_test
include $(wildcard $(APPDIR)/../../../APP_Framework/Applications/app_test/*/Make.defs)

View File

@ -1,183 +0,0 @@
include $(KERNEL_ROOT)/.config
ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
include $(APPDIR)/Make.defs
ifeq ($(CONFIG_USER_TEST_SEMC),y)
CSRCS += test_extsram.c
endif
ifeq ($(CONFIG_USER_TEST_LCD),y)
CSRCS += test_lcd.c
endif
ifeq ($(CONFIG_BSP_USING_TOUCH),y)
CSRCS += test_touch.c
endif
ifeq ($(CONFIG_MUSL_LIBC),y)
CSRCS += test_musl.c
endif
include $(APPDIR)/Application.mk
endif
ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
SRC_FILES := test_shell.c
ifeq ($(CONFIG_USER_TEST_ADC),y)
SRC_FILES += test_adc.c
endif
ifeq ($(CONFIG_USER_TEST_DAC),y)
SRC_FILES += test_dac.c
endif
ifeq ($(CONFIG_USER_TEST_FS),y)
SRC_FILES += test_fs.c
endif
ifeq ($(CONFIG_USER_TEST_SEMC),y)
SRC_FILES += test_extsram.c
endif
ifeq ($(CONFIG_USER_TEST_LCD),y)
SRC_FILES +=
endif
ifeq ($(CONFIG_USER_TEST_I2C),y)
ifeq ($(CONFIG_BOARD_EDU_RISCV64_EVB),y)
SRC_FILES += test_i2c_riscv.c
endif
ifeq ($(CONFIG_BOARD_EDU_ARM32_EVB),y)
SRC_FILES += test_i2c_arm.c
endif
endif
ifeq ($(CONFIG_USER_TEST_UART),y)
SRC_FILES += test_uart.c
endif
ifeq ($(CONFIG_USER_TEST_GPIO),y)
SRC_FILES += test_gpio.c
endif
ifeq ($(CONFIG_USER_TEST_LORA),y)
SRC_FILES += test_loraE220.c
endif
ifeq ($(CONFIG_USER_TEST_RTC),y)
SRC_FILES += test_rtc.c
endif
ifeq ($(CONFIG_USER_TEST_RS485),y)
ifeq ($(CONFIG_BOARD_EDU_RISCV64_EVB),y)
SRC_FILES += test_rs485_riscv.c
endif
ifeq ($(CONFIG_BOARD_EDU_ARM32_EVB),y)
SRC_FILES += test_rs485_arm.c
endif
endif
ifeq ($(CONFIG_USER_TEST_HWTIMER),y)
SRC_FILES += test_hwtimer.c
endif
ifeq ($(CONFIG_USER_TEST_LCD_EDU),y)
SRC_FILES += test_lcd_edu.c
endif
ifeq ($(CONFIG_USER_TEST_WDT),y)
SRC_FILES += test_wdt.c
endif
ifeq ($(CONFIG_USER_TEST_TOUCH),y)
SRC_FILES += test_touch.c
endif
ifeq ($(CONFIG_USER_TEST_CAMERA),y)
SRC_FILES += test_camera.c
endif
ifeq ($(CONFIG_USER_TEST_ETHERNET),y)
SRC_FILES += test_ethernet.c
endif
ifeq ($(CONFIG_USER_TEST_FLASH),y)
SRC_FILES += test_flash.c
endif
ifeq ($(CONFIG_USER_TEST_CAN),y)
SRC_FILES += test_can.c
endif
ifeq ($(CONFIG_USER_TEST_TIMER),y)
SRC_FILES += test_timer.c
endif
ifeq ($(CONFIG_USER_TEST_HASH),y)
SRC_FILES += test_hash/test_hash.c
endif
ifeq ($(CONFIG_USER_TEST_RADIX),y)
SRC_FILES += test_radix_tree/test_radix_tree.c
endif
ifeq ($(CONFIG_USER_TEST_RBTREE),y)
SRC_FILES += test_rbtree/test_rbtree.c
endif
ifeq ($(CONFIG_USER_TEST_SOCKET),y)
SRC_FILES += test_socket.c
endif
ifeq ($(CONFIG_USER_TEST_MODBUS_TCP),y)
SRC_DIR += test_modbus_tcp
endif
ifeq ($(CONFIG_USER_TEST_WEBSERVER),y)
SRC_FILES += test_webserver/test_webserver.c
endif
ifeq ($(CONFIG_USER_TEST_MQTTCLIENT),y)
SRC_DIR:= test_mqttclient
SRC_FILES +=
endif
ifeq ($(CONFIG_USER_TEST_FTPCLIENT),y)
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_FTPCLIENT_RISCV),y)
ifeq ($(CONFIG_BSP_USING_W5500),y)
SRC_FILES += test_ftpclient_riscv/test_ftpclient_riscv.c
endif
endif
ifeq ($(CONFIG_USER_TEST_LORA_P2P),y)
SRC_FILES +=
endif
ifeq ($(CONFIG_USER_TEST_LORAWAN_SINGLEGW),y)
SRC_FILES +=
endif
ifeq ($(CONFIG_USER_TEST_CANOPEN),y)
SRC_FILES +=
endif
ifeq ($(CONFIG_USER_TEST_USB_CAMERA),y)
SRC_FILES +=
endif
ifeq ($(CONFIG_USER_TEST_FTPCLIENT_FINAL),y)
SRC_FILES += test_ftpclient_final/test_ftpclient_final.c test_ftpclient_final/ftp_client/ftp_client.c test_ftpclient_final/ftp_client/my_socket.c
endif
ifeq ($(CONFIG_USER_TEST_MPU),y)
SRC_FILES += test_mpu_fault.c
endif
include $(KERNEL_ROOT)/compiler.mk
endif

View File

@ -1,11 +0,0 @@
import os
from building import *
Import('RTT_ROOT')
Import('rtconfig')
cwd = GetCurrentDir()
DEPENDS = [""]
SOURCES = ['test_shell.c']
path = [cwd]
objs = DefineGroup('app_test', src = SOURCES, depend = DEPENDS,CPPPATH = path)
Return("objs")

View File

@ -1,162 +0,0 @@
/*
* 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_4g_ec200a.c
* @brief Implement the connection 4G function, using QUECTEL EC200A device
* @version 1.1
* @author AIIT XUOS Lab
* @date 2023.10.15
*/
#include <stdio.h>
#include <string.h>
#include <transform.h>
#ifdef ADD_XIZI_FEATURES
char *trans_data = "{\"saleid\": \"52330000MJ87322565\", \"gateid\": \"10100001011\", \"type\": \"report\", \"time\": \"2023-06-15 17:22:00\", \"sequence\": \"268\", \"source\": \"da\", \"meter\": [{\"id\": \"T100106\", \"status\": \"1\", \"name\": \"T100106\", \"values\": {}}]}\r\n";
// const char chk_buad[] = {"AT+IPR?\r\n"};
const char set_mode[] = {"AT+QMTCFG=\"recv/mode\",0,0,1\r\n"};
// const char set_cfg[] = {"AT+QMTCFG=\"aliauth\",0,\"k0celEMx5DK\",\"test_device\",\"411aa14c0dc671f7ee869adced442d13\"\r\n"};
const char set_server[] = {"AT+QMTOPEN=0,\"xyheqmx.e3.luyouxia.net\",13333\r\n"};
// const char set_server[] = {"AT+QMTOPEN=0,\"iot-060a7p7c.mqtt.iothub.aliyuncs.com\",1883\r\n"};
const char set_conn[] = {"AT+QMTCONN=0,\"quectel\",\"test\",\"test123456\"\r\n"};
// const char set_conn[] = {"AT+QMTCONN=0,\"sysoul\"\r\n"};
// const char set_get[] = {"AT+QMTSUB=0,1,\"/get\",0\r\n"};
// const char set_reply[] = {"AT+QMTPUBEX=0,0,0,0,\"/reply\",1000\r\n"};
char recv_buf[100] = {0};
int buf_len = 0;
void Test4G(void)
{
int quectel_fd = PrivOpen(ADAPTER_QUECTEL_DRIVER, O_RDWR);
if (quectel_fd < 0) {
printf("open quectel fd error:%d\n", quectel_fd);
return;
}
printf("quectel fopen success\n");
struct SerialDataCfg quectel_cfg;
memset(&quectel_cfg, 0, sizeof(struct SerialDataCfg));
quectel_cfg.serial_baud_rate = BAUD_RATE_115200;
quectel_cfg.serial_data_bits = DATA_BITS_8;
quectel_cfg.serial_stop_bits = STOP_BITS_1;
quectel_cfg.serial_parity_mode = PARITY_NONE;
quectel_cfg.serial_bit_order = BIT_ORDER_LSB;
quectel_cfg.serial_invert_mode = NRZ_NORMAL;
// quectel_cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
quectel_cfg.serial_buffer_size = 1024;
quectel_cfg.serial_timeout = 1000;
quectel_cfg.is_ext_uart = 0;
// quectel_cfg.ext_uart_no = ADAPTER_QUECTEL_DRIVER_EXT_PORT;
// quectel_cfg.port_configure = PORT_CFG_INIT;
struct PrivIoctlCfg ioctl_cfg;
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
ioctl_cfg.args = &quectel_cfg;
if (0 != PrivIoctl(quectel_fd, OPE_INT, &ioctl_cfg)) {
printf("ioctl quectel fd error %d\n", quectel_fd);
PrivClose(quectel_fd);
return;
}
printf("4G module set\n");
// memset(recv_buf, 0, sizeof(recv_buf));
// printf("chk_buad: %s\n", chk_buad);
// printf("chk_buad_len: %d\n", strlen(chk_buad));
// PrivWrite(quectel_fd, chk_buad, strlen(chk_buad));
// PrivTaskDelay(30);
// buf_len = PrivRead(quectel_fd, recv_buf, sizeof(recv_buf));
// printf("buf_len: %d\n", buf_len);
// printf("[%s] Info: Recv from uart: %s\n", __func__, recv_buf);
// PrivTaskDelay(1000);
memset(recv_buf, 0, sizeof(recv_buf));
printf("set_mode: %s\n", set_mode);
printf("set_mode_len: %d\n", strlen(set_mode));
PrivWrite(quectel_fd, set_mode, strlen(set_mode));
PrivTaskDelay(30);
buf_len = PrivRead(quectel_fd, recv_buf, sizeof(recv_buf));
printf("buf_len: %d\n", buf_len);
printf("[%s] Info: Recv from uart: %s\n", __func__, recv_buf);
PrivTaskDelay(1000);
// memset(recv_buf, 0, sizeof(recv_buf));
// printf("set_cfg: %s\n", set_cfg);
// printf("set_cfg_len: %d\n", strlen(set_cfg));
// PrivWrite(quectel_fd, set_cfg, strlen(set_cfg));
// PrivTaskDelay(30);
// buf_len = PrivRead(quectel_fd, recv_buf, sizeof(recv_buf));
// printf("buf_len: %d\n", buf_len);
// printf("[%s] Info: Recv from uart: %s\n", __func__, recv_buf);
// PrivTaskDelay(1000);
memset(recv_buf, 0, sizeof(recv_buf));
printf("set_server: %s\n", set_server);
printf("set_server_len: %d\n", strlen(set_server));
PrivWrite(quectel_fd, set_server, strlen(set_server));
PrivTaskDelay(30);
buf_len = PrivRead(quectel_fd, recv_buf, sizeof(recv_buf));
printf("buf_len: %d\n", buf_len);
printf("[%s] Info: Recv from uart: %s\n", __func__, recv_buf);
PrivTaskDelay(1000);
memset(recv_buf, 0, sizeof(recv_buf));
printf("set_conn: %s\n", set_conn);
printf("set_conn_len: %d\n", strlen(set_conn));
PrivWrite(quectel_fd, set_conn, strlen(set_conn));
PrivTaskDelay(30);
buf_len = PrivRead(quectel_fd, recv_buf, sizeof(recv_buf));
printf("buf_len: %d\n", buf_len);
printf("[%s] Info: Recv from uart: %s\n", __func__, recv_buf);
PrivTaskDelay(1000);
while (1) {
printf("start send\n");
char set_reply[50] = {"AT+QMTPUBEX=0,0,0,0,\"/reply\","};
char data_len_str[20];
sprintf(data_len_str, "%d", strlen(trans_data));
printf("data_len_str: %s\n", data_len_str);
strcat(set_reply, data_len_str);
strcat(set_reply, "\r\n");
memset(recv_buf, 0, sizeof(recv_buf));
printf("set_reply: %s\n", set_reply);
printf("set_reply_len: %d\n", strlen(set_reply));
PrivWrite(quectel_fd, set_reply, strlen(set_reply));
PrivTaskDelay(30);
buf_len = PrivRead(quectel_fd, recv_buf, sizeof(recv_buf));
printf("buf_len: %d\n", buf_len);
printf("[%s] Info: Recv from uart: %s\n", __func__, recv_buf);
PrivTaskDelay(500);
memset(recv_buf, 0, sizeof(recv_buf));
printf("send_data: %s\n", trans_data);
printf("send_data_len: %d\n", strlen(trans_data));
PrivWrite(quectel_fd, trans_data, strlen(trans_data));
PrivTaskDelay(30);
buf_len = PrivRead(quectel_fd, recv_buf, sizeof(recv_buf));
printf("buf_len: %d\n", buf_len);
printf("[%s] Info: Recv from uart: %s\n", __func__, recv_buf);
PrivTaskDelay(5000);
}
PrivClose(quectel_fd);
return;
}
PRIV_SHELL_CMD_FUNCTION(Test4G, a quectel test sample, PRIV_SHELL_CMD_MAIN_ATTR);
#endif

View File

@ -1,58 +0,0 @@
/*
* 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_adc.c
* @brief: a application of adc function
* @version: 1.1
* @author: AIIT XUOS Lab
* @date: 2022/1/7
*/
#include <stdio.h>
#include <string.h>
#include <transform.h>
#ifdef ADD_XIZI_FEATURES
void TestAdc(void)
{
int adc_fd;
uint8 adc_channel = 0x1;
uint16 adc_sample = 0;
adc_fd = PrivOpen(ADC_DEV_DRIVER, O_RDWR);
if (adc_fd < 0) {
printf("open adc fd error %d\n", adc_fd);
return;
}
struct PrivIoctlCfg ioctl_cfg;
ioctl_cfg.ioctl_driver_type = ADC_TYPE;
ioctl_cfg.args = &adc_channel;
if (0 != PrivIoctl(adc_fd, OPE_CFG, &ioctl_cfg)) {
printf("ioctl adc fd error %d\n", adc_fd);
PrivClose(adc_fd);
return;
}
for (int i = 0; i < 10; i ++) {
PrivRead(adc_fd, &adc_sample, 2);
printf("adc sample %u mv\n", adc_sample);
PrivTaskDelay(500);
}
PrivClose(adc_fd);
return;
}
PRIV_SHELL_CMD_FUNCTION(TestAdc, a adc test sample, PRIV_SHELL_CMD_MAIN_ATTR);
#endif

View File

@ -1,120 +0,0 @@
/*
* 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_camera.c
* @brief: a application of camera function
* @version: 1.1
* @author: AIIT XUOS Lab
* @date: 2022/12/7
*/
#include <stdio.h>
#include <string.h>
#include <transform.h>
#ifdef ADD_XIZI_FEATURES
static uint16_t image_buff[384000];
void TestCamera(int argc, char *argv[])
{
int frame_counter = 10000;
if (argc > 1)
{
frame_counter = atoi(argv[1]);
}
printf("This test will refresh %d frames\n", frame_counter);
int camera_fd = PrivOpen(CAMERA_DEV_DRIVER, O_RDWR);
if (camera_fd < 0)
{
printf("open camera fd error:%d\n", camera_fd);
return;
}
int lcd_fd = PrivOpen(CAMERA_LCD_DEV_DRIVER, O_RDWR);
if (lcd_fd < 0)
{
printf("open lcd fd error:%d\n", lcd_fd);
return;
}
//configure the camera's output address
struct PrivIoctlCfg ioctl_cfg;
ioctl_cfg.ioctl_driver_type = CAMERA_TYPE;
struct CameraCfg camera_cfg ={
.gain_manu_enable = 0,
.gain = 0xFF,
.window_w = 800,
.window_h = 600,
.output_w = IMAGE_WIDTH,
.output_h = IMAGE_HEIGHT,
.window_xoffset = 0,
.window_yoffset = 0
};
ioctl_cfg.args = &camera_cfg;
if (0 != PrivIoctl(camera_fd, OPE_CFG, &ioctl_cfg))
{
printf("camera pin fd error %d\n", camera_fd);
PrivClose(camera_fd);
return;
}
ioctl_cfg.args = (void *)image_buff;
if (0 != PrivRead(camera_fd, image_buff, NULL_PARAMETER))
{
printf("camera pin fd error %d\n", camera_fd);
PrivClose(camera_fd);
return;
}
printf("address buff is %x\n", image_buff);
LcdWriteParam graph_param;
graph_param.type = LCD_DOT_TYPE;
//clear the LCD
uint16_t back_color[LCD_SIZE];
memset(back_color,0,sizeof(back_color));
for (int i = 0; i < LCD_SIZE; i++)
{
graph_param.pixel_info.pixel_color = &back_color;
graph_param.pixel_info.x_startpos = 0;
graph_param.pixel_info.y_startpos = i;
graph_param.pixel_info.x_endpos = LCD_SIZE -1;
graph_param.pixel_info.y_endpos = i;
PrivWrite(lcd_fd, &graph_param, NULL_PARAMETER);
}
//refresh the LCD using photo of camera
while (frame_counter--)
{
for (int i = 0; i < IMAGE_HEIGHT; i++)
{
graph_param.pixel_info.pixel_color = image_buff + i * IMAGE_WIDTH;
graph_param.pixel_info.x_startpos = 0;
graph_param.pixel_info.y_startpos = i + (LCD_SIZE - IMAGE_HEIGHT) / 2;
graph_param.pixel_info.x_endpos = IMAGE_WIDTH - 1;
graph_param.pixel_info.y_endpos = i + (LCD_SIZE - IMAGE_HEIGHT) / 2;
PrivWrite(lcd_fd, &graph_param, NULL_PARAMETER);
}
}
// close test
PrivClose(lcd_fd);
PrivClose(camera_fd);
printf("The camera test is finished successfully\n");
}
PRIV_SHELL_CMD_FUNCTION(TestCamera, a camera test sample, PRIV_SHELL_CMD_MAIN_ATTR);
#endif

View File

@ -1,94 +0,0 @@
/*
* 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_can.c
* @brief: a application of can function
* @version: 1.1
* @author: AIIT XUOS Lab
* @date: 2023/2/17
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <transform.h>
#ifdef ADD_XIZI_FEATURES
void TestCAN(void)
{
// config CAN pin(SCL:34.SDA:35) in menuconfig
int can_fd = PrivOpen(CAN_DEV_DRIVER, O_RDWR);
if (can_fd < 0)
{
printf("open can_fd fd error:%d\n", can_fd);
return;
}
printf("CAN open successful!\n");
struct PrivIoctlCfg ioctl_cfg;
ioctl_cfg.ioctl_driver_type = CAN_TYPE;
struct CanDriverConfigure can_config;
can_config.brp = 8U;
can_config.tbs1 = 1U + 8U;
can_config.tbs2 = 4U;
can_config.tsjw = 4U;
can_config.mode = 0U;
ioctl_cfg.args = (void *)&can_config;
if (0 != PrivIoctl(can_fd, OPE_INT, &ioctl_cfg))
{
printf("init can fd error %d\n", can_fd);
PrivClose(can_fd);
return;
}
printf("CAN configure successful!\n");
uint8_t data_buff[64u] = "12344321";
struct CanSendConfigure frame_send;
frame_send.ide=0;
frame_send.stdid = 0x55;
frame_send.rtr=0;
frame_send.data_lenth=8;
struct CanSendConfigure frame_recv;
uint8_t recv_buff[64u] = {};
frame_recv.data = recv_buff;
// CAN write
while (1)
{
// PrivTaskDelay(500);
// PrivWrite(can_fd, &frame_send, NONE);
// PrivTaskDelay(500);
PrivRead(can_fd, &frame_recv, NONE);
// if any data has received,Then printf message
if(frame_recv.data_lenth > 0){
printf("ID %08x : \n",frame_recv.exdid);
for(int i = 0; i < frame_recv.data_lenth; i ++) {
printf("0x%x ", frame_recv.data[i]);
}
printf("\n");
frame_send.data = recv_buff;
PrivWrite(can_fd, &frame_send, NONE);
}
}
PrivClose(can_fd);
return;
}
PRIV_SHELL_CMD_FUNCTION(TestCAN, a can test sample, PRIV_SHELL_CMD_MAIN_ATTR);
#endif

View File

@ -1,63 +0,0 @@
/*
* 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_dac.c
* @brief: a application of dac function
* @version: 2.0
* @author: AIIT XUOS Lab
* @date: 2022/1/11
*/
#include <stdio.h>
#include <string.h>
#include <transform.h>
#ifdef ADD_XIZI_FEATURES
static pthread_t test_dac_task;
static void *TestDacTask(void *parameter)
{
int dac_fd;
uint16 dac_set_value = 4096 * 10;//sin length
dac_fd = PrivOpen(DAC_DEV_DRIVER, O_RDWR);
if (dac_fd < 0) {
KPrintf("open dac fd error %d\n", dac_fd);
}
struct PrivIoctlCfg ioctl_cfg;
ioctl_cfg.ioctl_driver_type = DAC_TYPE;
ioctl_cfg.args = &dac_set_value;
if (0 != PrivIoctl(dac_fd, OPE_CFG, &ioctl_cfg)) {
KPrintf("ioctl dac fd error %d\n", dac_fd);
PrivClose(dac_fd);
}
while (1) {
//start dac output sin
PrivWrite(dac_fd, NULL, 0);
}
PrivClose(dac_fd);
}
void TestDac(void)
{
pthread_attr_t tid;
tid.schedparam.sched_priority = 20;
tid.stacksize = 4096;
PrivTaskCreate(&test_dac_task, &tid, &TestDacTask, NULL);
PrivTaskStartup(&test_dac_task);
}
PRIV_SHELL_CMD_FUNCTION(TestDac, a dac test sample, PRIV_SHELL_CMD_MAIN_ATTR);
#endif

View File

@ -1,195 +0,0 @@
/*
* 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_ethernet.c
* @brief: a application of ethernet function
* @version: 1.1
* @author: AIIT XUOS Lab
* @date: 2022/12/17
*/
#include <stdio.h>
#include <string.h>
#include <transform.h>
#ifdef ADD_XIZI_FEATURES
#ifdef BSP_USING_W5500
#include <socket.h>
#define BUFF_SIZE 128
#define RECV_SIZE 16
#define TCP_PORT 12345
const static uint32_t sn = 0;
#ifdef ETHERNET_AS_SERVER
static int32_t wiz_server_op(uint8_t sn, uint8_t *buf, uint32_t buf_size,
uint16_t port, enum TCP_OPTION opt) {
int32_t ret = 0;
uint16_t size = 0, sentsize = 0;
switch (getSn_SR(sn)) {
case SOCK_ESTABLISHED:
if (getSn_IR(sn) & Sn_IR_CON) {
printf("%d:Connected\r\n", sn);
setSn_IR(sn, Sn_IR_CON);
}
if (opt == SEND_DATA) {
uint32_t sent_size = 0;
memset(buf,0,buf_size);
strcpy(buf,"The message has been recved");
ret = wiz_sock_send(sn, buf, buf_size);
if (ret < 0) {
wiz_sock_close(sn);
return ret;
}
} else if (opt == RECV_DATA) {
uint32_t size = 0;
if ((size = getSn_RX_RSR(sn)) > 0) {
if (size > buf_size) size = buf_size;
memset(buf,0,buf_size);
ret = wiz_sock_recv(sn, buf, size);
printf("Recv message: %s\n",buf);
return ret;
}
}
break;
case SOCK_CLOSE_WAIT:
printf("%d:CloseWait\r\n", sn);
if ((ret = wiz_sock_disconnect(sn)) != SOCK_OK) return ret;
printf("%d:Closed\r\n", sn);
break;
case SOCK_INIT:
printf("%d:Listen, port [%d]\r\n", sn, port);
if ((ret = wiz_sock_listen(sn)) != SOCK_OK) return ret;
break;
case SOCK_CLOSED:
printf("%d:LBTStart\r\n", sn);
if ((ret = wiz_socket(sn, Sn_MR_TCP, port, 0x00)) != sn) return ret;
printf("%d:Opened\r\n", sn);
break;
default:
break;
}
return 0;
}
void TestSocketAsServer(int argc, char *argv[])
{
x_err_t ret;
uint8_t buf[BUFF_SIZE] = {0};
while (1) {
ret = wiz_server_op(0, buf, BUFF_SIZE, TCP_PORT, RECV_DATA);
if (ret > 0) {
wiz_server_op(0, buf, BUFF_SIZE, TCP_PORT, SEND_DATA);
};
}
return ;
}
PRIV_SHELL_CMD_FUNCTION(TestSocketAsServer, a w5500 server test sample, PRIV_SHELL_CMD_MAIN_ATTR);
#elif defined ETHERNET_AS_CLIENT
static uint32_t wiz_client_op(uint8_t sn, uint8_t *buf, uint32_t buf_size,
uint8_t dst_ip[4], uint16_t dst_port,
enum TCP_OPTION opt) {
// assert(buf_size <= g_wiznet_buf_size);
int32_t ret;
switch (getSn_SR(sn)) {
case SOCK_CLOSE_WAIT:
wiz_sock_disconnect(sn);
break;
case SOCK_CLOSED:
wiz_socket(sn, Sn_MR_TCP, 5000, 0x00);
break;
case SOCK_INIT:
KPrintf("[SOCKET CLIENT] sock init.\n");
wiz_sock_connect(sn, dst_ip, dst_port);
break;
case SOCK_ESTABLISHED:
if (getSn_IR(sn) & Sn_IR_CON) {
printf("[SOCKET CLIENT] %d:Connected\r\n", sn);
setSn_IR(sn, Sn_IR_CON);
}
if (opt == SEND_DATA) {
uint32_t sent_size = 0;
ret = wiz_sock_send(sn, buf, buf_size);
if (ret < 0) {
wiz_sock_close(sn);
return ret;
}
} else if (opt == RECV_DATA) {
uint32_t size = 0;
if ((size = getSn_RX_RSR(sn)) > 0) {
if (size > buf_size) size = buf_size;
ret = wiz_sock_recv(sn, buf, size);
if (ret <= 0) return ret;
}
}
break;
default:
break;
}
}
void TestSocketAsClient(int argc, char *argv[])
{
x_err_t ret;
uint8_t buf[BUFF_SIZE] = {0};
uint32_t tmp_ip[4];
uint32_t port;
const uint8_t client_sock = 2;
if(argc<3){
printf("Please enter command like TestSocketAsClient ip:port msg\n");
}
sscanf(argv[1],"%d.%d.%d.%d:%d",tmp_ip,tmp_ip+1,tmp_ip+2,tmp_ip+3,&port);
printf("Client to %d.%d.%d.%d:%d\n",tmp_ip[0],tmp_ip[1],tmp_ip[2],tmp_ip[3],port);
uint8_t destination_ip[4]={0};
for(int i=0;i<4;i++){
destination_ip[i]=tmp_ip[i];
}
while(1){
ret = wiz_client_op(client_sock, argv[2], strlen(argv[2]), destination_ip, port,SEND_DATA);
printf("sizeof:%d\n",strlen(argv[2]));
PrivTaskDelay(1000);
if (ret > 0) {
ret=wiz_client_op(client_sock, buf, BUFF_SIZE, destination_ip, port, RECV_DATA);
printf("read ret is %d\n",ret);
if(ret>0){
printf("client recv msg successfully!\n");
printf("%s\n",buf);
}
};
PrivTaskDelay(1000);
}
return ;
}
PRIV_SHELL_CMD_FUNCTION(TestSocketAsClient, a w5500 client-ip-port-msg test sample, PRIV_SHELL_CMD_MAIN_ATTR);
#endif
#endif
#endif

View File

@ -1,107 +0,0 @@
/*
* 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_extsram.c
* @brief: a extsram test application
* @version: 2.0
* @author: AIIT XUOS Lab
* @date: 2022/9/26
*
*/
#include <transform.h>
#include "stdio.h"
/* parameters for sram peripheral */
// /* stm32f4 Bank3:0X68000000 */
// #define SRAM_BANK_ADDR ((uint32_t)0X68000000)
/* OK-1052 semc:0X80000000 */
#define SRAM_BANK_ADDR ((uint32_t)0X80000000)
/* data width: 8, 16, 32 */
#define SRAM_DATA_WIDTH 16
/* sram size */
#define SRAM_SIZE ((uint32_t)0x2000000)
#define TICK_PER_SECOND 100
int ExtsramTest(void)
{
int i = 0;
uint32_t start_time = 0, time_cast = 0;
#if SRAM_DATA_WIDTH == 8
char data_width = 1;
uint8_t data = 0;
#elif SRAM_DATA_WIDTH == 16
char data_width = 2;
uint16_t data = 0;
#else
char data_width = 4;
uint32_t data = 0;
#endif
/* write data */
printf("Writing the %ld bytes data, waiting....\n", SRAM_SIZE);
start_time = PrivGetTickTime();
for (i = 0; i < SRAM_SIZE / data_width; i++)
{
#if SRAM_DATA_WIDTH == 8
*(volatile uint8_t *)(SRAM_BANK_ADDR + i * data_width) = (uint8_t)0x55;
#elif SRAM_DATA_WIDTH == 16
*(volatile uint16_t *)(SRAM_BANK_ADDR + i * data_width) = (uint16_t)0x5555;
#else
*(volatile uint32_t *)(SRAM_BANK_ADDR + i * data_width) = (uint32_t)0x55555555;
#endif
}
time_cast = PrivGetTickTime() - start_time;
printf("Write data success, total time: %ld.%03ldS.\n", time_cast / TICK_PER_SECOND,
time_cast % TICK_PER_SECOND / ((TICK_PER_SECOND * 1 + 999) / 1000));
/* read data */
printf("start Reading and verifying data, waiting....\n");
for (i = 0; i < SRAM_SIZE / data_width; i++)
{
#if SRAM_DATA_WIDTH == 8
data = *(volatile uint8_t *)(SRAM_BANK_ADDR + i * data_width);
if (data != 0x55)
{
printf("SRAM test failed!");
break;
}
#elif SRAM_DATA_WIDTH == 16
data = *(volatile uint16_t *)(SRAM_BANK_ADDR + i * data_width);
if (data != 0x5555)
{
printf("SRAM test failed! data = 0x%x\n",data);
break;
}
#else
data = *(volatile uint32_t *)(SRAM_BANK_ADDR + i * data_width);
if (data != 0x55555555)
{
printf("SRAM test failed!\n");
break;
}
#endif
}
if (i >= SRAM_SIZE / data_width)
{
printf("SRAM test success!\n");
}
return 0;
}
PRIV_SHELL_CMD_FUNCTION(ExtsramTest, a extsram test sample, PRIV_SHELL_CMD_MAIN_ATTR);

View File

@ -1,56 +0,0 @@
/*
* 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_flash.c
* @brief: a application of flash function
* @version: 1.1
* @author: AIIT XUOS Lab
* @date: 2022/12/17
*/
#include <stdio.h>
#include <string.h>
#include <transform.h>
#ifdef ADD_XIZI_FEATURES
void TestFlash(void)
{
int fd = open(FLASH_DEV_DRIVER,O_RDWR);
if(fd<0){
printf("fs fd open error:%d\n",fd);
return;
}
struct BusBlockWriteParam flash_writer;
uint8_t read_buff[8] = {1,2,3,4,5,6,7,8};
flash_writer.pos = 0x000000L;
flash_writer.size = 8;
flash_writer.buffer = read_buff;
struct BusBlockReadParam flash_reader;
flash_reader.pos = 0x000000L;
flash_reader.size = 8;
flash_reader.buffer = read_buff;
PrivRead(fd,&flash_reader,NONE);
printf("Read data is:");
for(int i=0;i<flash_writer.size;i++){
printf("%02x ",read_buff[i]);
read_buff[i]++;
}
printf("\n");
PrivWrite(fd,&flash_writer,NONE);
return;
}
PRIV_SHELL_CMD_FUNCTION(TestFlash, a flash test sample, PRIV_SHELL_CMD_MAIN_ATTR);
#endif

View File

@ -1,83 +0,0 @@
/*
* 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_fs.c
* @brief: a application of file_system function
* @version: 1.1
* @author: AIIT XUOS Lab
* @date: 2022/11/17
*/
#include <stdio.h>
#include <string.h>
#include <transform.h>
#ifdef ADD_XIZI_FEATURES
#define MAX_READ_LENGTH 1000
// sd card here is loaded as "/"
void TestFs(void)
{
//open the file in sdcard
int fd = open(FPATH,O_RDWR|O_CREAT);
if(fd<0){
printf("fs fd open error:%d\n",fd);
return;
}
char filewords[MAX_READ_LENGTH];
memset(filewords,0,MAX_READ_LENGTH);
const char *input_words = "these words are going to write in fs\n";
//read and write then close file
int err_flag = read(fd,filewords,MAX_READ_LENGTH);
if(err_flag<0){
printf("read failed,error:%d\n",err_flag);
return;
}
printf("read data is \n%s\n",filewords);
err_flag = write(fd,input_words,strlen(input_words));
if(err_flag<0){
printf("write failed,error:%d\n",err_flag);
return;
}
err_flag = close(fd);
if(err_flag<0){
printf("close failed,error %d\n",err_flag);
return ;
}
//re-open the file and re-read the file
fd = open(FPATH,O_RDWR);
if(fd<0){
printf("fs fd open error:%d\n",fd);
return;
}
err_flag = read(fd,filewords,MAX_READ_LENGTH);
if(err_flag<0){
printf("read failed,error:%d\n",err_flag);
return;
}
printf("read data is \n%s\n",filewords);
err_flag = close(fd);
if(err_flag<0){
printf("close failed,error:%d\n",err_flag);
return;
}
return;
}
PRIV_SHELL_CMD_FUNCTION(TestFs, a sd or usb filesystem test sample, PRIV_SHELL_CMD_MAIN_ATTR);
#endif

View File

@ -1,60 +0,0 @@
# 初赛一级赛题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)

View File

@ -1,256 +0,0 @@
/*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#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<len; off+=ret)
{
ret = SocketRecv(m_socket_cmd, &resp[off], 1);
if(ret < 0)
{
printf("recv respond error(ret=%d)!\r\n", ret);
return 0;
}
if(resp[off] == '\n')
{
break;
}
}
resp[off+1] = 0;
printf("respond:%s\r\n", resp);
return atoi(resp);
}
static int FtpEnterPasv(char *ipaddr, int *port)
{
int ret;
char *find;
int a,b,c,d;
int pa,pb;
ret = FtpSendCommand("PASV\r\n");
if(ret != 1)
{
return 0;
}
ret = FtpRecvRespond(m_recv_buffer, 1024);
if(ret != 227)
{
return 0;
}
find = strrchr(m_recv_buffer, '(');
sscanf(find, "(%d,%d,%d,%d,%d,%d)", &a, &b, &c, &d, &pa, &pb);
sprintf(ipaddr, "%d.%d.%d.%d", a, b, c, d);
*port = pa * 256 + pb;
return 1;
}
int FtpDownload(char *name, char *buf, int len)
{
int i;
int ret;
char ipaddr[32];
int port;
//查询数据地址
ret = FtpEnterPasv(ipaddr, &port);
if(ret != 1)
{
return 0;
}
//连接数据端口
ret = SocketConnect(m_socket_data, ipaddr, port);
if(ret != 1)
{
printf("failed to connect data port\r\n");
return 0;
}
//准备下载
sprintf(m_send_buffer, "RETR %s\r\n", name);
ret = FtpSendCommand(m_send_buffer);
if(ret != 1)
{
return 0;
}
ret = FtpRecvRespond(m_recv_buffer, 1024);
if(ret != 150)
{
SocketClose(m_socket_data);
return 0;
}
//开始下载,读取完数据后,服务器会自动关闭连接
for(i=0; i<len; i+=ret)
{
ret = SocketRecv(m_socket_data, ((char *)buf) + i, len);
printf("download %d/%d.\r\n", i + ret, len);
if(ret < 0)
{
printf("download was interrupted.\r\n");
break;
}
}
//下载完成
printf("download %d/%d bytes complete.\r\n", i, len);
printf("====file content====\n");
buf[100] = '\0';
printf("%s\n",buf);
printf("====================\n");
// FILE *fp = NULL;
// fp = fopen(name+1, "wb");
// fwrite(buf,len,1,fp);
// fclose(fp);
SocketClose(m_socket_data);
ret = FtpRecvRespond(m_recv_buffer, 1024);
return (ret==226);
}
int FtpFileSize(char *name)
{
int ret;
int size;
sprintf(m_send_buffer,"SIZE %s\r\n",name);
ret = FtpSendCommand(m_send_buffer);
if(ret != 1)
{
return 0;
}
ret = FtpRecvRespond(m_recv_buffer, 1024);
if(ret != 213)
{
return 0;
}
size = atoi(m_recv_buffer + 4);
return size;
}
int FtpLogin(char *addr, int port, char *username, char *password)
{
int ret;
printf("connect...\r\n");
ret = SocketConnect(m_socket_cmd, addr, port);
if(ret != 1)
{
printf("connect server failed!\r\n");
return 0;
}
printf("connect ok.\r\n");
//等待欢迎信息
ret = FtpRecvRespond(m_recv_buffer, 1024);
if(ret != 220)
{
printf("bad server, ret=%d!\r\n", ret);
SocketClose(m_socket_cmd);
return 0;
}
printf("login...\r\n");
//发送USER
sprintf(m_send_buffer, "USER %s\r\n", username);
ret = FtpSendCommand(m_send_buffer);
if(ret != 1)
{
SocketClose(m_socket_cmd);
return 0;
}
ret = FtpRecvRespond(m_recv_buffer, 1024);
if(ret != 331)
{
SocketClose(m_socket_cmd);
return 0;
}
//发送PASS
sprintf(m_send_buffer, "PASS %s\r\n", password);
ret = FtpSendCommand(m_send_buffer);
if(ret != 1)
{
SocketClose(m_socket_cmd);
return 0;
}
ret = FtpRecvRespond(m_recv_buffer, 1024);
if(ret != 230)
{
SocketClose(m_socket_cmd);
return 0;
}
printf("login success.\r\n");
//设置为二进制模式
ret = FtpSendCommand("TYPE I\r\n");
if(ret != 1)
{
SocketClose(m_socket_cmd);
return 0;
}
ret = FtpRecvRespond(m_recv_buffer, 1024);
if(ret != 200)
{
SocketClose(m_socket_cmd);
return 0;
}
return 1;
}
void FtpQuit(void)
{
FtpSendCommand("QUIT\r\n");
SocketClose(m_socket_cmd);
}
void FtpInitCmd(void)
{
m_socket_cmd = SocketCreate();
}
void FtpInitData(void){
m_socket_data= SocketCreate();
}

View File

@ -1,42 +0,0 @@
/*
* 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.h
* @brief: ftp client tool
* @version: 1.0
* @author: bdislab_final
* @date: 2023/7/25
*/
#ifndef FTP_CLIENT_H
#define FTP_CLIENT_H
/* init ftp cmd socket */
void FtpInitCmd(void);
/* init ftp data socket */
void FtpInitData(void);
/* quit ftp */
void FtpQuit(void);
/* fpt login */
int FtpLogin(char *addr, int port, char *username, char *password);
/* get file size */
int FtpFileSize(char *name);
/* ftp download file*/
int FtpDownload(char *name, char *buf, int len);
#endif

View File

@ -1,57 +0,0 @@
/*
* 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.c
* @brief: a abstract socket api
* @version: 1.0
* @author: bdislab_final
* @date: 2023/7/25
* @reference https://kerndev.blog.csdn.net/article/details/89383888
*/
#include "my_socket.h"
#include <sockets.h>
#include <def.h>
#include <errno.h>
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);
}

View File

@ -1,46 +0,0 @@
/*
* 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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 214 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 121 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 108 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 117 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 136 KiB

View File

@ -1,50 +0,0 @@
/*
* 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <transform.h>
/* 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);

View File

@ -1,35 +0,0 @@
/*
* 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

View File

@ -1,135 +0,0 @@
# 决赛一级赛题3基于初赛一级赛题3在云服务器上实现FTP Server功能
## 1. 简介
本项目是基于赛事提供的云服务器实现FTP协议的Server功能其功能支持至少10个Client端并发向Server端传输4KB大小的文件
支持Server端并发地向至少10个Client端传输4KB大小的文件。
test_ftpclient_final.h声明了多个客户端并发下载文件、并发上传文件的测试函数
test_ftpclient_final.c实现了多个客户端并发下载文件、并发上传文件的测试函数
ftp_client文件夹定义了ftp_client的相关类库其中my_socket.h,my_socket.c定义了socket抽象层并基于
Lwip实现了该抽象层ftp_client.h,ftp_client.c实现了ftp登录获取文件大小下载文件等功能
注意:在赛事提供的云服务器/root/yanglongFTP上有对应服务器代码 ./server运行服务器为了方便查看代码我们讲服务器代码备份存放在ftp_server目录下
## 2. 数据结构设计说明
- ftp_client.c 的设计
分别定义了发送命令和接收数据的socket和相应的缓冲区并且实现了登录、发送命令、接收响应数据、查找文件大小、进入被动模式、下载文件、关闭ftp客户端等操作
```c
static int m_socket_cmd[THREAD_NUM]; // 发送命令的socket文件描述符,THREAD_NUM表示线程数目用来模拟多个客户端并发访问
static int m_socket_data[THREAD_NUM]; // 接收ftp服务器文件的socket文件描述符
static char m_send_buffer[THREAD_NUM][1024]; // 发送缓冲区
static char m_recv_buffer[THREAD_NUM][1024]; // 接收缓冲区
```
- server.c 的设计(具体代码在赛事提供的云服务器的/root/yanglongFTP下)
```c
#define THREAD_NUM 10000 // thread num
static int isBinary = 0; // transmit binary data
static int port = 9992; // service port
static int dataPort = 9993; // the port for file download
static char order[4]; // receive order
static char param[20]; // receive order param
static char *respMessage; // respose message
static int serverFd; // the server fd for deal with order requests
static int dataServerFd; // the server fd for file download
sem_t mutex; // mutex lock
struct Data{ // 线程间通信传输的数据
char fileName[20]; // file name
int type; // 0:download 1:upload
sem_t isDone; // complete file downlaod
sem_t isReady; // 文件准备好了
} data;
```
## 3. 测试程序说明
- test_ftpclient_final.c用于测试多个客户端并发下载文件
通过多线程模拟多个客户端并发访问服务器,通过 TestFtpClient options threads进行测试
其中options=1表示下载options=2表示上传threads表示线程数/模拟的并发客户端数目
```c
/* test for 10 ftp client */
void TestFtpClient(int argc, char* argv[])
{
int options = atoi(argv[1]);
int n = atoi(argv[2]);
pthread_t threads[THREAD_NUM];
for(int i = 0;i < n;++i){
threadIDs[i] = i;
if(options == 1){ // 全部都是下载
pthread_create(&threads[i],NULL,&DownLoad,&threadIDs[i]);
}else if(options == 2){ // 全部都是上传
pthread_create(&threads[i],NULL,&UpLoad,&threadIDs[i]);
}else if(options == 3){ // 随机下载/上传
int r = rand()%2;
if(r == 0){
printf("===============download===============\n");
pthread_create(&threads[i],NULL,&DownLoad,&threadIDs[i]);
}else{
printf("===============upload===============\n");
pthread_create(&threads[i],NULL,&UpLoad,&threadIDs[i]);
}
}
}
return;
}
PRIV_SHELL_CMD_FUNCTION(TestFtpClient, a ftpClient test sample, PRIV_SHELL_CMD_MAIN_ATTR);
```
## 4. 运行结果(##需结合运行测试截图按步骤说明##
1. 配置开启BSP_USING_LWIP、USER_TEST_FTPCLIENT
![](./img/image.png)
![](./img/image-1.png)
![](./img/image-2.png)
2. 编译
![](./img/image-3.png)
3. 烧写
![](./img/image-4.png)
4. xshell连接串口终端并配置ip
![](./img/image-5.png)
5. 通过./server在云服务器运行FTP服务器
![](./img/image-6.png)
6. 运行TestFtpClient 1 10模拟10个客户端并发下载文件
- 客户端日志
![](./img/image-7.png)
![](./img/image-8.png)
![](./img/image-9.png)
![](./img/image-10.png)
![](./img/image-11.png)
![](./img/image-12.png)
![](./img/image-13.png)
![](./img/image-14.png)
![](./img/image-15.png)
![](./img/image-16.png)
- 服务器日志
![](./img/image-17.png)
![](./img/image-18.png)
![](./img/image-19.png)
![](./img/image-20.png)
![](./img/image-21.png)
7. 运行TestFtpClient 2 10模拟10个客户端并发上传文件
- 客户端日志
![](./img/image-22.png)
![](./img/image-23.png)
![](./img/image-24.png)
![](./img/image-25.png)
![](./img/image-26.png)
![](./img/image-27.png)
![](./img/image-28.png)
- 服务器日志
![](./img/image-29.png)
![](./img/image-30.png)
![](./img/image-31.png)
![](./img/image-32.png)
- 上传结果
![](./img/image-33.png)
8. 运行TestFtpClient 3 10模拟10个客户端混合并发下载和上传文件
- 客户端日志
![](./img/image-34.png)
![](./img/image-35.png)
![](./img/image-36.png)
![](./img/image-37.png)
![](./img/image-38.png)
![](./img/image-39.png)
- 服务器日志
![](./img/image-40.png)
![](./img/image-41.png)
![](./img/image-42.png)
- 下载和上传结果
![](./img/image-43.png)
![](./img/image-44.png)

View File

@ -1,299 +0,0 @@
/*
* 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/9/16
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "my_socket.h"
#include "ftp_client.h"
static int m_socket_cmd[THREAD_NUM];
static int m_socket_data[THREAD_NUM];
static char m_send_buffer[THREAD_NUM][1024];
static char m_recv_buffer[THREAD_NUM][1024];
static int FtpSendCommand(int threadID,char *cmd)
{
int ret;
printf("send command: %s,threadID=%d\r\n", cmd,threadID);
ret = SocketSend(m_socket_cmd[threadID], cmd, (int)strlen(cmd));
if(ret < 0)
{
printf("failed to send command: %s\r\n",cmd);
return 0;
}
return 1;
}
static int FtpRecvRespond(int threadID,char *resp, int len)
{
int ret;
int off;
len -= 1;
for(off=0; off<len; off+=ret)
{
ret = SocketRecv(m_socket_cmd[threadID], &resp[off], 1);
if(ret < 0)
{
printf("recv respond error(ret=%d)!\r\n", ret);
return 0;
}
if(resp[off] == '\n')
{
break;
}
}
resp[off+1] = 0;
printf("respond:%s,threadID=%d\r\n", resp,threadID);
return atoi(resp);
}
static int FtpEnterPasv(int threadID,char *ipaddr, int *port)
{
int ret;
char *find;
int a,b,c,d;
int pa,pb;
ret = FtpSendCommand(threadID,"PASV\r\n");
if(ret != 1)
{
return 0;
}
ret = FtpRecvRespond(threadID,m_recv_buffer[threadID], 1024);
if(ret != 227)
{
return 0;
}
find = strrchr(m_recv_buffer[threadID], '(');
sscanf(find, "(%d,%d,%d,%d,%d,%d)", &a, &b, &c, &d, &pa, &pb);
sprintf(ipaddr, "%d.%d.%d.%d", a, b, c, d);
*port = pa * 256 + pb;
return 1;
}
int FtpUpload(int threadID,char *name, void *buf, int len)
{
int ret;
char ipaddr[32];
int port;
//查询数据地址
ret=FtpEnterPasv(threadID,ipaddr, &port);
if(ret != 1)
{
return 0;
}
ret=SocketConnect(m_socket_data[threadID], ipaddr, port);
if(ret != 1)
{
return 0;
}
//准备上传
sprintf(m_send_buffer[threadID], "STOR %s\r\n", name);
ret = FtpSendCommand(threadID,m_send_buffer[threadID]);
if(ret != 1)
{
return 0;
}
ret = FtpRecvRespond(threadID,m_recv_buffer[threadID], 1024);
if(ret != 150)
{
SocketClose(m_socket_data[threadID]);
return 0;
}
//开始上传
ret = SocketSend(m_socket_data[threadID], buf, len);
if(ret != len)
{
printf("send data error!\r\n");
SocketClose(m_socket_data[threadID]);
return 0;
}
SocketClose(m_socket_data[threadID]);
//上传完成,等待回应
ret = FtpRecvRespond(threadID,m_recv_buffer[threadID], 1024);
return (ret==226);
}
int FtpDownload(int threadID,char *name, void *buf, int len)
{
int i;
int ret;
char ipaddr[32];
int port;
//查询数据地址
ret = FtpEnterPasv(threadID,ipaddr, &port);
if(ret != 1)
{
return 0;
}
printf("m_socket_data[threadID]=%d,threadID=%d\n",m_socket_data[threadID],threadID);
//连接数据端口
ret = SocketConnect(m_socket_data[threadID], ipaddr, port);
if(ret != 1)
{
printf("failed to connect data port\r\n");
return 0;
}
//准备下载
sprintf(m_send_buffer[threadID], "RETR %s\r\n", name);
ret = FtpSendCommand(threadID,m_send_buffer[threadID]);
if(ret != 1)
{
return 0;
}
ret = FtpRecvRespond(threadID,m_recv_buffer[threadID], 1024);
if(ret != 150)
{
SocketClose(m_socket_data[threadID]);
return 0;
}
//开始下载,读取完数据后,服务器会自动关闭连接
for(i=0; i<len; i+=ret)
{
ret = SocketRecv(m_socket_data[threadID], ((char *)buf) + i, len);
printf("download %d/%d.,threadID=%d\r\n", i + ret, len,threadID);
if(ret < 0)
{
printf("download was interrupted.\r\n");
break;
}
}
//下载完成
printf("download %d/%d bytes complete.,threadID=%d\r\n", i, len,threadID);
// FILE *fp = NULL;
// fp = fopen(name+1, "wb");
// fwrite(buf,len,1,fp);
// fclose(fp);
SocketClose(m_socket_data[threadID]);
ret = FtpRecvRespond(threadID,m_recv_buffer[threadID], 1024);
return (ret==226);
}
int FtpFileSize(int threadID,char *name)
{
int ret;
int size;
sprintf(m_send_buffer[threadID],"SIZE %s\r\n",name);
ret = FtpSendCommand(threadID,m_send_buffer[threadID]);
if(ret != 1)
{
return 0;
}
ret = FtpRecvRespond(threadID,m_recv_buffer[threadID], 1024);
if(ret != 213)
{
return 0;
}
size = atoi(m_recv_buffer[threadID] + 4);
return size;
}
int FtpLogin(int threadID,char *addr, int port, char *username, char *password)
{
int ret;
printf("connect...,threadID=%d\r\n",threadID);
ret = SocketConnect(m_socket_cmd[threadID], addr, port);
if(ret != 1)
{
printf("connect server failed!\r\n");
return 0;
}
printf("connect ok.,threadID=%d\r\n",threadID);
//等待欢迎信息
ret = FtpRecvRespond(threadID,m_recv_buffer[threadID], 1024);
if(ret != 220)
{
printf("bad server, ret=%d!\r\n", ret);
SocketClose(m_socket_cmd[threadID]);
return 0;
}
printf("login...threadID=%d\r\n",threadID);
//发送USER
sprintf(m_send_buffer[threadID], "USER %s\r\n", username);
ret = FtpSendCommand(threadID,m_send_buffer[threadID]);
if(ret != 1)
{
SocketClose(m_socket_cmd[threadID]);
return 0;
}
ret = FtpRecvRespond(threadID,m_recv_buffer[threadID], 1024);
if(ret != 331)
{
SocketClose(m_socket_cmd[threadID]);
return 0;
}
//发送PASS
sprintf(m_send_buffer[threadID], "PASS %s\r\n", password);
ret = FtpSendCommand(threadID,m_send_buffer[threadID]);
if(ret != 1)
{
SocketClose(m_socket_cmd[threadID]);
return 0;
}
ret = FtpRecvRespond(threadID,m_recv_buffer[threadID], 1024);
if(ret != 230)
{
SocketClose(m_socket_cmd[threadID]);
return 0;
}
printf("login success.threadID=%d\r\n",threadID);
//设置为二进制模式
ret = FtpSendCommand(threadID,"TYPE I\r\n");
if(ret != 1)
{
SocketClose(m_socket_cmd[threadID]);
return 0;
}
ret = FtpRecvRespond(threadID,m_recv_buffer[threadID], 1024);
if(ret != 200)
{
SocketClose(m_socket_cmd[threadID]);
return 0;
}
return 1;
}
void FtpQuit(int threadID)
{
FtpSendCommand(threadID,"QUIT\r\n");
SocketClose(m_socket_cmd[threadID]);
}
void FtpInitCmd(int threadID)
{
m_socket_cmd[threadID] = SocketCreate();
}
void FtpInitData(int threadID){
m_socket_data[threadID]= SocketCreate();
}

View File

@ -1,46 +0,0 @@
/*
* 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.h
* @brief: ftp client tool
* @version: 1.0
* @author: bdislab_final
* @date: 2023/9/16
*/
#ifndef FTP_CLIENT_H
#define FTP_CLIENT_H
#define THREAD_NUM 10
/* init ftp cmd socket */
void FtpInitCmd(int threadID);
/* init ftp data socket */
void FtpInitData(int threadID);
/* quit ftp */
void FtpQuit(int threadID);
/* fpt login */
int FtpLogin(int threadID,char *addr, int port, char *username, char *password);
/* get file size */
int FtpFileSize(int threadID,char *name);
/* ftp download file*/
int FtpDownload(int threadID,char *name, void *buf, int len);
/*ftp upload*/
int FtpUpload(int threadID,char *name, void *buf, int len);
#endif

View File

@ -1,55 +0,0 @@
/*
* 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.c
* @brief: a abstract socket api
* @version: 1.0
* @author: bdislab_final
* @date: 2023/9/16
*/
#include "my_socket.h"
#include <sockets.h>
#include <def.h>
#include <errno.h>
#include <string.h>
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);
int res = connect(sock,(struct sockaddr *)&stRemoteAddr,sizeof(stRemoteAddr));
if(res == -1){
printf("error:%d,str error:%s\n",errno,strerror(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);
}

View File

@ -1,46 +0,0 @@
/*
* 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/9/16
*/
#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

View File

@ -1,5 +0,0 @@
#!/bin/bash
for i in {1..10}
do
dd if=/dev/zero of="file$i" bs=4K count=1
done

View File

@ -1 +0,0 @@
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

View File

@ -1,251 +0,0 @@
/*
* 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: server.c
* @brief: a ftpserver
* @version: 1.0
* @author: bdislab_final
* @date: 2023/10/11
*/
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#define THREAD_NUM 10000 // thread num
static int isBinary = 0; // transmit binary data
static int port = 9992; // service port
static int dataPort = 9993; // the port for file download
static char order[4]; // receive order
static char param[20]; // receive order param
static char *respMessage; // respose message
static int serverFd; // the server fd for deal with order requests
static int dataServerFd; // the server fd for file download
sem_t mutex; // mutex lock
struct Data{ // 线程间通信传输的数据
char fileName[20]; // file name
int type; // 0:download 1:upload
sem_t isDone; // complete file downlaod
sem_t isReady; // 文件准备好了
} data;
void RecvData(int clientFd,char param[20]){
recv(clientFd,param,20,0);
for(int i = 0;i < 20;++i){
if(param[i] == '\r'){
param[i] = '\0';
return;
}
}
return;
}
void SendMessage(int clientFd, char * respMessage){
int len = 0;
while(respMessage[len] != '\n'){
len++;
}
send(clientFd,respMessage,len+1,0);
}
int CreateServer(int port,int type) {
int serverFd = 0;
struct sockaddr_in serverAddr = {0};
socklen_t socklen = 0;
serverFd = socket(AF_INET, SOCK_STREAM, 0);
if(0 > serverFd)
{
printf("创建socket失败\n");
return 0;
}
if(type == 0){
printf("in ftp server:serverFd=%d\n",serverFd);
}else{
printf("in ftp data server:serverFd=%d\n",serverFd);
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
serverAddr.sin_addr.s_addr=htonl(INADDR_ANY);
if(0 > bind(serverFd, (void *)&serverAddr, sizeof(serverAddr)))
{
printf("绑定失败!\n");
return 0;
}
if(0 > listen(serverFd, 1024))
{
printf("监听失败!\n");
return 0;
}
return serverFd;
}
// receive file's data
void* UpLoadServer(void * args){
int clientFd = *(int *) args;
char buf[4096];
int ret = recv(clientFd,buf,4096,0);
printf("ret=%d,errno=%d\n",ret,errno);
FILE * file = fopen(data.fileName,"wb");
fwrite(buf,4096,1,file);
fclose(file);
printf("upload complete for client: clientFd=%d,tid=%ld\n",clientFd,pthread_self());
// notify for file download complete
sem_post(&data.isDone);
close(clientFd);
}
// send file's data
void* DownLoadServer(void * args){
int clientFd = *(int *) args;
char buf[4096];
FILE * file = fopen(data.fileName,"rb");
fread(buf,4096,1,file);
send(clientFd,buf,sizeof(buf),0);
fclose(file);
printf("download complete for client: clientFd=%d,tid=%ld\n",clientFd,pthread_self());
// notify for file download complete
sem_post(&data.isDone);
close(clientFd);
}
// accept a client and new a thread for downlaod/upload
void* DataServer(void * args){
dataServerFd = CreateServer(dataPort,1);
while(1){
// wait for a filename
sem_wait(&data.isReady);
struct sockaddr_in clientAdrr = {0};
int socketLen = sizeof(clientAdrr);
int clientFd = accept(dataServerFd,(struct sockaddr *)&clientAdrr,&socketLen);
pthread_t thread;
if(data.type == 0){ // download
printf("accept a client for file download: clientFd=%d,tid=%ld\n",clientFd,pthread_self());
pthread_create(&thread,NULL,&DownLoadServer,&clientFd);
}else if(data.type == 1){ // upload
printf("accept a client for file upload: clientFd=%d,tid=%ld\n",clientFd,pthread_self());
pthread_create(&thread,NULL,&UpLoadServer,&clientFd);
}
// release lock
sem_post(&mutex);
}
}
// deal with order requests
void* OrderServer(void * args){
int clientFd = *(int *)args;
printf("in OrderServer thread: tid=%ld\n",pthread_self());
// order format: USER anonymous\r\n
// response code format: 213 4096\r\n
while(1){
recv(clientFd,order,4,0);
printf("order=%s,tid=%ld\n",order,pthread_self());
if(strcmp(order,"USER") == 0){
RecvData(clientFd,param);
if(strcmp(param," anonymous") == 0){
respMessage = "331 Please specify the password.\r\n";
SendMessage(clientFd,respMessage);
}
}else if(strcmp(order,"PASS") == 0){
RecvData(clientFd,param);
if(strcmp(param," anonymous") == 0){
respMessage = "230 Login successful.\r\n";
SendMessage(clientFd,respMessage);
printf("login successful in clientFd %d,tid=%ld\n",clientFd,pthread_self());
}
}else if(strcmp(order,"TYPE") == 0){
RecvData(clientFd,param);
if(strcmp(param," I") == 0){
isBinary = 1;
respMessage = "200 Switching to Binary mode.\r\n";
SendMessage(clientFd,respMessage);
}
}else if(strcmp(order,"SIZE") == 0){
RecvData(clientFd,param);
respMessage = "213 4096\r\n";
SendMessage(clientFd,respMessage);
}else if(strcmp(order,"PASV") == 0){
sem_wait(&mutex); // 锁竞争
sem_init(&data.isDone,0,0);
RecvData(clientFd,param);
char buf[50];
sprintf(buf,"227 Entering passive mode (8,140,53,225,%d,%d).\r\n",dataPort/256,dataPort%256);
respMessage = buf;
SendMessage(clientFd,respMessage);
}else if(strcmp(order,"RETR") == 0){
RecvData(clientFd,param);
sprintf(data.fileName,"./data/%s",param+2);
data.type = 0;
// notify the thread of waiting fileName
sem_post(&data.isReady);
char buf[70];
sprintf(buf,"150 Opening Binary mode data connection for %s (4096 bytes).\r\n",param+1);
respMessage = buf;
SendMessage(clientFd,respMessage);
// wait for download complete
printf("wait for download complete,tid=%ld\n",pthread_self());
sem_wait(&data.isDone);
respMessage = "226 Transfer complete.\r\n";
SendMessage(clientFd,respMessage);
}else if(strcmp(order,"STOR") == 0){
RecvData(clientFd,param);
sprintf(data.fileName,"./out/%s",param+1);
data.type = 1;
// notify the thread of waiting fileName
sem_post(&data.isReady);
char buf[70];
sprintf(buf,"150 Opening Binary mode data connection for %s (4096 bytes).\r\n",param+1);
respMessage = buf;
SendMessage(clientFd,respMessage);
// wait for upload complete
printf("wait for upload complete,tid=%ld\n",pthread_self());
sem_wait(&data.isDone);
respMessage = "226 Transfer complete.\r\n";
SendMessage(clientFd,respMessage);
}
else if(strcmp(order,"QUIT") == 0){
printf("close clientFd=%d,tid=%ld\n",clientFd,pthread_self());
close(clientFd);
break;
}
}
return NULL;
}
int main(){
serverFd = CreateServer(port,0);
sem_init(&data.isReady,0,0);
sem_init(&mutex,0,1);
pthread_t thread;
pthread_create(&thread,NULL,&DataServer,NULL);
int clientFds[THREAD_NUM];
int i = 0;
while(1){
struct sockaddr_in clientAdrr = {0};
int socketLen = sizeof(clientAdrr);
clientFds[i] = accept(serverFd,(struct sockaddr *)&clientAdrr,&socketLen);
printf("accept a client:fd=%d\n",clientFds[i]);
respMessage = "220 (myFtpServer 1.0)\r\n";
SendMessage(clientFds[i],respMessage);
printf("send welcome message successfully.\n");
pthread_t thread;
pthread_create(&thread,NULL,&OrderServer,&clientFds[i]);
i++;
}
return 0;
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 97 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 201 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 218 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 165 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 264 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 207 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 350 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 172 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 247 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 336 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 269 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 473 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 331 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 228 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 208 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 222 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 199 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 268 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 250 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 174 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 193 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 346 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 412 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 189 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 220 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 61 KiB

Some files were not shown because too many files have changed in this diff Show More