forked from xuos/xiuos
Merge branch 'prepare_for_master' of https://gitlink.org.cn/xuos/xiuos into jerryscript
This commit is contained in:
@@ -72,6 +72,24 @@ menu "test app"
|
||||
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
|
||||
|
||||
@@ -49,6 +49,10 @@ ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
|
||||
SRC_FILES += test_i2c.c
|
||||
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
|
||||
@@ -113,6 +117,10 @@ ifeq ($(CONFIG_ADD_XIZI_FEATURES),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_WEBSERVER),y)
|
||||
SRC_FILES +=
|
||||
endif
|
||||
|
||||
@@ -26,9 +26,8 @@
|
||||
void TestAdc(void)
|
||||
{
|
||||
int adc_fd;
|
||||
uint8 adc_channel = 0x0;
|
||||
uint16 adc_sample, adc_value_decimal = 0;
|
||||
float adc_value;
|
||||
uint8 adc_channel = 0x1;
|
||||
uint16 adc_sample = 0;
|
||||
|
||||
adc_fd = PrivOpen(ADC_DEV_DRIVER, O_RDWR);
|
||||
if (adc_fd < 0) {
|
||||
@@ -45,13 +44,11 @@ void TestAdc(void)
|
||||
return;
|
||||
}
|
||||
|
||||
PrivRead(adc_fd, &adc_sample, 2);
|
||||
|
||||
adc_value = (float)adc_sample * (3.3 / 4096);
|
||||
|
||||
adc_value_decimal = (adc_value - (uint16)adc_value) * 1000;
|
||||
|
||||
printf("adc sample %u value integer %u decimal %u\n", adc_sample, (uint16)adc_value, adc_value_decimal);
|
||||
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);
|
||||
|
||||
|
||||
@@ -22,17 +22,16 @@
|
||||
#include <transform.h>
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
|
||||
void TestDac(void)
|
||||
static pthread_t test_dac_task;
|
||||
|
||||
static void *TestDacTask(void *parameter)
|
||||
{
|
||||
int dac_fd;
|
||||
uint16 dac_set_value = 800;
|
||||
uint16 dac_sample, dac_value_decimal = 0;
|
||||
float dac_value;
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
@@ -41,20 +40,24 @@ void TestDac(void)
|
||||
if (0 != PrivIoctl(dac_fd, OPE_CFG, &ioctl_cfg)) {
|
||||
KPrintf("ioctl dac fd error %d\n", dac_fd);
|
||||
PrivClose(dac_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
PrivRead(dac_fd, &dac_sample, 2);
|
||||
|
||||
dac_value = (float)dac_sample * (3.3 / 4096);//Vref+ need to be 3.3V
|
||||
|
||||
dac_value_decimal = (dac_value - (uint16)dac_value) * 1000;
|
||||
|
||||
printf("dac sample %u value integer %u decimal %u\n", dac_sample, (uint16)dac_value, dac_value_decimal);
|
||||
while (1) {
|
||||
//start dac output sin
|
||||
PrivWrite(dac_fd, NULL, 0);
|
||||
}
|
||||
|
||||
PrivClose(dac_fd);
|
||||
}
|
||||
|
||||
return;
|
||||
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
|
||||
@@ -28,11 +28,11 @@
|
||||
static uint16_t pin_fd=0;
|
||||
static struct PinStat pin_led;
|
||||
|
||||
void ledflip(void *parameter)
|
||||
void LedFlip(void *parameter)
|
||||
{
|
||||
pin_led.pin = BSP_LED_PIN;
|
||||
pin_led.val = !pin_led.val;
|
||||
PrivWrite(pin_fd,&pin_led,NULL_PARAMETER);
|
||||
PrivWrite(pin_fd, &pin_led, NULL_PARAMETER);
|
||||
}
|
||||
|
||||
void TestHwTimer(void)
|
||||
@@ -40,22 +40,22 @@ void TestHwTimer(void)
|
||||
x_ticks_t period = 100000;
|
||||
|
||||
pin_fd = PrivOpen(HWTIMER_PIN_DEV_DRIVER, O_RDWR);
|
||||
if(pin_fd<0){
|
||||
if(pin_fd<0) {
|
||||
printf("open pin fd error:%d\n",pin_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
int timer_fd = PrivOpen(HWTIMER_TIMER_DEV_DRIVER, O_RDWR);
|
||||
if(timer_fd<0){
|
||||
if(timer_fd<0) {
|
||||
printf("open timer fd error:%d\n",timer_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
//config led pin in board
|
||||
struct PinParam parameter;
|
||||
parameter.cmd = GPIO_CONFIG_MODE;
|
||||
parameter.pin = BSP_LED_PIN;
|
||||
parameter.mode = GPIO_CFG_OUTPUT;
|
||||
struct PinParam parameter;
|
||||
parameter.cmd = GPIO_CONFIG_MODE;
|
||||
parameter.pin = BSP_LED_PIN;
|
||||
parameter.mode = GPIO_CFG_OUTPUT;
|
||||
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
ioctl_cfg.ioctl_driver_type = PIN_TYPE;
|
||||
@@ -68,7 +68,7 @@ void TestHwTimer(void)
|
||||
}
|
||||
|
||||
ioctl_cfg.ioctl_driver_type = TIME_TYPE;
|
||||
ioctl_cfg.args = (void *)&ledflip;
|
||||
ioctl_cfg.args = (void *)&LedFlip;
|
||||
if (0 != PrivIoctl(timer_fd, OPE_INT, &ioctl_cfg)) {
|
||||
printf("timer pin fd error %d\n", pin_fd);
|
||||
PrivClose(pin_fd);
|
||||
@@ -82,13 +82,10 @@ void TestHwTimer(void)
|
||||
return;
|
||||
}
|
||||
|
||||
while(1){
|
||||
while(1) {
|
||||
|
||||
}
|
||||
|
||||
// int32 timer_handle = KCreateTimer("LED on and off by 1s",&ledflip,&pin_fd,period,TIMER_TRIGGER_PERIODIC);
|
||||
|
||||
// KTimerStartRun(timer_handle);
|
||||
PrivClose(pin_fd);
|
||||
PrivClose(timer_fd);
|
||||
}
|
||||
|
||||
@@ -24,18 +24,16 @@
|
||||
|
||||
#define I2C_SLAVE_ADDRESS 0x0012U
|
||||
|
||||
void TestI2C(void)
|
||||
int OpenIic(void)
|
||||
{
|
||||
// config IIC pin(SCL:34.SDA:35) in menuconfig
|
||||
int iic_fd = PrivOpen(I2C_DEV_DRIVER, O_RDWR);
|
||||
if (iic_fd < 0)
|
||||
{
|
||||
printf("open iic_fd fd error:%d\n", iic_fd);
|
||||
return;
|
||||
printf("[TestI2C] Open iic_fd fd error: %d\n", iic_fd);
|
||||
return -ERROR;
|
||||
}
|
||||
printf("IIC open successful!\n");
|
||||
printf("[TestI2C] IIC open successful!\n");
|
||||
|
||||
// init iic
|
||||
uint16 iic_address = I2C_SLAVE_ADDRESS;
|
||||
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
@@ -44,28 +42,55 @@ void TestI2C(void)
|
||||
|
||||
if (0 != PrivIoctl(iic_fd, OPE_INT, &ioctl_cfg))
|
||||
{
|
||||
printf("ioctl iic fd error %d\n", iic_fd);
|
||||
printf("[TestI2C] Ioctl iic fd error %d\n", iic_fd);
|
||||
PrivClose(iic_fd);
|
||||
return;
|
||||
return -ERROR;
|
||||
}
|
||||
printf("IIC configure successful!\n");
|
||||
|
||||
// I2C read and write
|
||||
char tmp_buff[100];
|
||||
while (1)
|
||||
{
|
||||
PrivTaskDelay(1000);
|
||||
PrivWrite(iic_fd, "Hello World!\n", sizeof("Hello World!\n"));
|
||||
printf("msg send:%s\n", "Hello World!\n");
|
||||
PrivTaskDelay(1000);
|
||||
memset(tmp_buff, 0, sizeof(tmp_buff));
|
||||
PrivRead(iic_fd, tmp_buff, sizeof(tmp_buff));
|
||||
printf("msg recv:%s\n", tmp_buff);
|
||||
return iic_fd;
|
||||
}
|
||||
|
||||
static const int nr_transmit = 15;
|
||||
|
||||
void TestMasterI2c(void)
|
||||
{
|
||||
char recv_buff[13] = { 0 };
|
||||
|
||||
int iic_fd = OpenIic();
|
||||
if (iic_fd < 0) {
|
||||
printf("[%s] Error open iic\n", __func__);
|
||||
return;
|
||||
}
|
||||
|
||||
for (int transmit_cnt = 0; transmit_cnt < nr_transmit; transmit_cnt++) {
|
||||
// wait if you like.
|
||||
PrivTaskDelay(500);
|
||||
memset(recv_buff, 0, sizeof(recv_buff));
|
||||
PrivRead(iic_fd, recv_buff, sizeof(recv_buff));
|
||||
printf("[%s] Msg recv: %s\n", __func__, recv_buff);
|
||||
}
|
||||
|
||||
PrivClose(iic_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
PRIV_SHELL_CMD_FUNCTION(TestI2C, a iic test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||
void TestSlaveI2c(void)
|
||||
{
|
||||
char send_buff[] = "Hello, World";
|
||||
|
||||
int iic_fd = OpenIic();
|
||||
|
||||
for (int transmit_cnt = 0; transmit_cnt < nr_transmit; transmit_cnt++) {
|
||||
// wait if you like.
|
||||
PrivTaskDelay(500);
|
||||
PrivWrite(iic_fd, send_buff, sizeof(send_buff));
|
||||
printf("[%s] Msg send: %s\n", __func__, send_buff);
|
||||
}
|
||||
|
||||
PrivClose(iic_fd);
|
||||
}
|
||||
|
||||
PRIV_SHELL_CMD_FUNCTION(TestMasterI2c, a iic test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||
PRIV_SHELL_CMD_FUNCTION(TestSlaveI2c, a iic test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||
|
||||
#endif
|
||||
@@ -22,26 +22,94 @@
|
||||
#include <transform.h>
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
|
||||
#define BSP_485_DIR_PIN 24
|
||||
//edu-arm board dir pin PG01----no.67 in XiZi_IIoT/board/edu_arm32/third_party_driver/gpio/connect_gpio.c
|
||||
#define BSP_485_DIR_PIN 67
|
||||
|
||||
static int pin_fd;
|
||||
static int uart_fd;
|
||||
static char write_485_data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
|
||||
static char read_485_data[8] = {0};
|
||||
|
||||
/**
|
||||
* @description: Set Uart 485 Input
|
||||
* @return
|
||||
*/
|
||||
static void Set485Input(void)
|
||||
{
|
||||
struct PinStat pin_stat;
|
||||
pin_stat.pin = BSP_485_DIR_PIN;
|
||||
pin_stat.val = GPIO_LOW;
|
||||
PrivWrite(pin_fd, &pin_stat, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Set Uart 485 Output
|
||||
* @return
|
||||
*/
|
||||
static void Set485Output(void)
|
||||
{
|
||||
struct PinStat pin_stat;
|
||||
pin_stat.pin = BSP_485_DIR_PIN;
|
||||
pin_stat.val = GPIO_HIGH;
|
||||
PrivWrite(pin_fd, &pin_stat, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Control Framework Serial Write
|
||||
* @param write_data - write data
|
||||
* @param length - length
|
||||
* @return
|
||||
*/
|
||||
void Rs485Write(uint8_t *write_data, int length)
|
||||
{
|
||||
Set485Output();
|
||||
PrivTaskDelay(20);
|
||||
|
||||
PrivWrite(uart_fd, write_data, length);
|
||||
|
||||
PrivTaskDelay(15);
|
||||
Set485Input();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Control Framework Serial Read
|
||||
* @param read_data - read data
|
||||
* @param length - length
|
||||
* @return read data size
|
||||
*/
|
||||
int Rs485Read(uint8_t *read_data, int length)
|
||||
{
|
||||
int data_size = 0;
|
||||
int data_recv_size = 0;
|
||||
|
||||
while (data_size < length) {
|
||||
data_recv_size = PrivRead(uart_fd, read_data + data_size, length - data_size);
|
||||
data_size += data_recv_size;
|
||||
}
|
||||
|
||||
//need to wait 30ms , make sure write cmd again and receive data successfully
|
||||
PrivTaskDelay(30);
|
||||
|
||||
return data_size;
|
||||
}
|
||||
|
||||
void Test485(void)
|
||||
{
|
||||
int pin_fd = PrivOpen(RS485_PIN_DEV_DRIVER, O_RDWR);
|
||||
if (pin_fd < 0)
|
||||
{
|
||||
int read_data_length = 0;
|
||||
pin_fd = PrivOpen(RS485_PIN_DEV_DRIVER, O_RDWR);
|
||||
if (pin_fd < 0) {
|
||||
printf("open pin fd error:%d\n", pin_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
int uart_fd = PrivOpen(RS485_UART_DEV_DRIVER, O_RDWR);
|
||||
if (uart_fd < 0)
|
||||
{
|
||||
uart_fd = PrivOpen(RS485_UART_DEV_DRIVER, O_RDWR);
|
||||
if (uart_fd < 0) {
|
||||
printf("open pin fd error:%d\n", uart_fd);
|
||||
return;
|
||||
}
|
||||
printf("uart and pin fopen success\n");
|
||||
|
||||
//config led pin in board
|
||||
//config dir pin in board
|
||||
struct PinParam pin_parameter;
|
||||
memset(&pin_parameter, 0, sizeof(struct PinParam));
|
||||
pin_parameter.cmd = GPIO_CONFIG_MODE;
|
||||
@@ -68,36 +136,34 @@ void Test485(void)
|
||||
uart_cfg.serial_bit_order = BIT_ORDER_LSB;
|
||||
uart_cfg.serial_invert_mode = NRZ_NORMAL;
|
||||
uart_cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
|
||||
uart_cfg.serial_timeout = 1000;
|
||||
uart_cfg.serial_timeout = -1;
|
||||
uart_cfg.is_ext_uart = 0;
|
||||
|
||||
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
|
||||
ioctl_cfg.args = (void *)&uart_cfg;
|
||||
|
||||
if (0 != PrivIoctl(uart_fd, OPE_INT, &ioctl_cfg))
|
||||
{
|
||||
if (0 != PrivIoctl(uart_fd, OPE_INT, &ioctl_cfg)) {
|
||||
printf("ioctl uart fd error %d\n", uart_fd);
|
||||
PrivClose(uart_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
struct PinStat pin_dir;
|
||||
pin_dir.pin = BSP_485_DIR_PIN;
|
||||
while (1)
|
||||
{
|
||||
pin_dir.val = GPIO_HIGH;
|
||||
PrivWrite(pin_fd,&pin_dir,0);
|
||||
PrivWrite(uart_fd,"Hello world!\n",sizeof("Hello world!\n"));
|
||||
PrivTaskDelay(100);
|
||||
Rs485Write(write_485_data, sizeof(write_485_data));
|
||||
|
||||
pin_dir.val = GPIO_LOW;
|
||||
PrivWrite(pin_fd,&pin_dir,0);
|
||||
char recv_buff[100];
|
||||
memset(recv_buff,0,sizeof(recv_buff));
|
||||
PrivRead(uart_fd,recv_buff,20);
|
||||
printf("%s",recv_buff);
|
||||
PrivTaskDelay(100);
|
||||
while(1) {
|
||||
printf("ready to read data\n");
|
||||
|
||||
read_data_length = Rs485Read(read_485_data, sizeof(read_485_data));
|
||||
printf("%s read data length %d\n", __func__, read_data_length);
|
||||
for (int i = 0; i < read_data_length; i ++) {
|
||||
printf("i %d read data 0x%x\n", i, read_485_data[i]);
|
||||
}
|
||||
Rs485Write(read_485_data, read_data_length);
|
||||
memset(read_485_data, 0, sizeof(read_485_data));
|
||||
|
||||
printf("read data done\n");
|
||||
}
|
||||
|
||||
PrivClose(pin_fd);
|
||||
PrivClose(uart_fd);
|
||||
return;
|
||||
|
||||
347
APP_Framework/Applications/app_test/test_socket.c
Normal file
347
APP_Framework/Applications/app_test/test_socket.c
Normal file
@@ -0,0 +1,347 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <argparse.h>
|
||||
#include <stdbool.h>
|
||||
#include <transform.h>
|
||||
|
||||
#include "lwip/sockets.h"
|
||||
#include "sys_arch.h"
|
||||
|
||||
#define IPERF_PORT 5001
|
||||
#define IPERF_BUFSZ (4 * 1024)
|
||||
|
||||
enum IperfMode {
|
||||
IPERF_MODE_STOP = (1 << 0),
|
||||
IPERF_MODE_SERVER = (1 << 1),
|
||||
IPERF_MODE_CLIENT = (1 << 2),
|
||||
};
|
||||
|
||||
struct AtomicIperfMode {
|
||||
/* pthread_mutex_t here is a int */
|
||||
pthread_mutex_t mtx;
|
||||
enum IperfMode mode;
|
||||
};
|
||||
|
||||
static struct AtomicIperfMode* GetGlobalIperfMode()
|
||||
{
|
||||
/* init when used */
|
||||
static struct AtomicIperfMode g_iperf_mode = {
|
||||
-1,
|
||||
IPERF_MODE_STOP,
|
||||
};
|
||||
if (g_iperf_mode.mtx < 0) {
|
||||
/* mtx is a static obj, so there is only creation but not destruction */
|
||||
PrivMutexCreate(&g_iperf_mode.mtx, NULL);
|
||||
/* init lwip if necessary */
|
||||
lwip_config_tcp(0, lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
}
|
||||
return &g_iperf_mode;
|
||||
}
|
||||
|
||||
static enum IperfMode GetGlobalMode()
|
||||
{
|
||||
enum IperfMode mode = IPERF_MODE_STOP;
|
||||
struct AtomicIperfMode* g_mode = GetGlobalIperfMode();
|
||||
|
||||
PrivMutexObtain(&g_mode->mtx);
|
||||
mode = g_mode->mode;
|
||||
PrivMutexAbandon(&g_mode->mtx);
|
||||
|
||||
return mode;
|
||||
}
|
||||
|
||||
static void SetGlobalMode(enum IperfMode mode)
|
||||
{
|
||||
struct AtomicIperfMode* g_mode = GetGlobalIperfMode();
|
||||
PrivMutexObtain(&g_mode->mtx);
|
||||
g_mode->mode = mode;
|
||||
PrivMutexAbandon(&g_mode->mtx);
|
||||
}
|
||||
|
||||
struct IperfParam {
|
||||
char host[16];
|
||||
int port;
|
||||
};
|
||||
|
||||
static void* TestIperfServer(void* param)
|
||||
{
|
||||
struct IperfParam* iperf_param = (struct IperfParam*)param;
|
||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock < 0) {
|
||||
printf("[%s] Err: Can't create socker.\n", __func__);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint8_t* recv_data = (uint8_t*)malloc(IPERF_BUFSZ);
|
||||
if (recv_data == NULL) {
|
||||
KPrintf("[%s] No memory to alloc buffer!\n", __func__);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
struct sockaddr_in server_addr, client_addr;
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = htons(iperf_param->port);
|
||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
memset(&(server_addr.sin_zero), 0x0, sizeof(server_addr.sin_zero));
|
||||
|
||||
if (bind(sock, (struct sockaddr*)&server_addr, sizeof(struct sockaddr)) == -1) {
|
||||
KPrintf("[%s] Err: Unable to bind socket: %d!\n", __func__, sock);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (listen(sock, 5) == -1) {
|
||||
KPrintf("[%s] Err: Listen error!\n", __func__);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
struct timeval timeout = {
|
||||
.tv_sec = 3,
|
||||
.tv_usec = 0,
|
||||
};
|
||||
|
||||
fd_set readset;
|
||||
while (GetGlobalMode() == IPERF_MODE_SERVER) {
|
||||
FD_ZERO(&readset);
|
||||
FD_SET(sock, &readset);
|
||||
|
||||
if (select(sock + 1, &readset, NULL, NULL, &timeout) == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
socklen_t sin_size = sizeof(struct sockaddr_in);
|
||||
struct sockaddr_in client_addr;
|
||||
int connection = accept(sock, (struct sockaddr*)&client_addr, &sin_size);
|
||||
printf("[%s] Info: New client connected from (%s, %d)\n", __func__,
|
||||
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
|
||||
|
||||
int flag = 1;
|
||||
setsockopt(connection,
|
||||
IPPROTO_TCP, /* set option at TCP level */
|
||||
TCP_NODELAY, /* name of option */
|
||||
(void*)&flag, /* the cast is historical cruft */
|
||||
sizeof(int)); /* length of option value */
|
||||
|
||||
int recvlen = 0;
|
||||
int tick_beg = PrivGetTickTime();
|
||||
int tick_end = tick_beg;
|
||||
while (GetGlobalMode() == IPERF_MODE_SERVER) {
|
||||
int bytes_received = recv(connection, recv_data, IPERF_BUFSZ, 0);
|
||||
if (bytes_received == 0) {
|
||||
KPrintf("client disconnected (%s, %d)\n",
|
||||
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
|
||||
break;
|
||||
} else if (bytes_received < 0) {
|
||||
KPrintf("recv error, client: (%s, %d)\n",
|
||||
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
|
||||
break;
|
||||
}
|
||||
|
||||
recvlen += bytes_received;
|
||||
|
||||
tick_end = PrivGetTickTime();
|
||||
if (tick_end - tick_beg >= 5000) {
|
||||
double speed;
|
||||
// int integer, decimal;
|
||||
|
||||
speed = (double)(recvlen / (tick_end - tick_beg));
|
||||
speed = speed / 1000.0f;
|
||||
printf("[%s]: %2.4f MBps!\n", __func__, speed);
|
||||
tick_beg = tick_end;
|
||||
recvlen = 0;
|
||||
}
|
||||
}
|
||||
if (connection >= 0)
|
||||
closesocket(connection);
|
||||
connection = -1;
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (sock >= 0)
|
||||
closesocket(sock);
|
||||
if (recv_data)
|
||||
free(recv_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void* TestIperfClient(void* param)
|
||||
{
|
||||
struct IperfParam* iperf_param = (struct IperfParam*)param;
|
||||
|
||||
uint8_t* send_buf
|
||||
= (uint8_t*)malloc(IPERF_BUFSZ);
|
||||
if (NONE == send_buf) {
|
||||
printf("[%s] Err: Unable to alloc buffer\n", __func__);
|
||||
return NULL;
|
||||
}
|
||||
for (int i = 0; i < IPERF_BUFSZ; i++) {
|
||||
send_buf[i] = i & 0xff;
|
||||
}
|
||||
|
||||
struct sockaddr_in addr;
|
||||
while (GetGlobalMode() == IPERF_MODE_CLIENT) {
|
||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock < 0) {
|
||||
printf("[%s] Warning: Can't create socker.\n", __func__);
|
||||
PrivTaskDelay(1000);
|
||||
continue;
|
||||
}
|
||||
|
||||
addr.sin_family = PF_INET;
|
||||
addr.sin_port = htons(iperf_param->port);
|
||||
addr.sin_addr.s_addr = inet_addr((char*)iperf_param->host);
|
||||
|
||||
int ret = connect(sock, (const struct sockaddr*)&addr, sizeof(addr));
|
||||
if (ret == -1) {
|
||||
printf("[%s] Warning: Connect to iperf server faile, Waiting for the server to open!\n", __func__);
|
||||
closesocket(sock);
|
||||
DelayKTask(TICK_PER_SECOND);
|
||||
continue;
|
||||
}
|
||||
printf("[%s] Connect to iperf server successful!\n", __func__);
|
||||
|
||||
int flag = 1;
|
||||
setsockopt(sock,
|
||||
IPPROTO_TCP, /* set option at TCP level */
|
||||
TCP_NODELAY, /* name of option */
|
||||
(void*)&flag, /* the cast is historical cruft */
|
||||
sizeof(int)); /* length of option value */
|
||||
|
||||
int tick_beg = PrivGetTickTime();
|
||||
int tick_end = tick_beg;
|
||||
int sentlen = 0;
|
||||
while (GetGlobalMode() == IPERF_MODE_CLIENT) {
|
||||
tick_end = PrivGetTickTime();
|
||||
/* Print every 5 second */
|
||||
if (tick_end - tick_beg >= 5000) {
|
||||
double speed;
|
||||
|
||||
speed = (double)(sentlen / (tick_end - tick_beg));
|
||||
speed = speed / 1000.0f;
|
||||
printf("[%s]: %2.4f MBps!\n", __func__, speed);
|
||||
tick_beg = tick_end;
|
||||
sentlen = 0;
|
||||
}
|
||||
|
||||
ret = send(sock, send_buf, IPERF_BUFSZ, 0);
|
||||
if (ret > 0) {
|
||||
sentlen += ret;
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
break;
|
||||
}
|
||||
|
||||
closesocket(sock);
|
||||
printf("[%s] Info: Disconnected, iperf server shut down!\n", __func__);
|
||||
}
|
||||
free(send_buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
enum IperfParamEnum {
|
||||
IPERF_PARAM_SERVER = 's',
|
||||
IPERF_PARAM_CLIENT = 'c',
|
||||
IPERF_PARAM_STOP = 0,
|
||||
IPERF_PARAM_IPADDR = 0,
|
||||
IPERF_PARAM_PORT = 'p',
|
||||
};
|
||||
|
||||
void TestSocket(int argc, char* argv[])
|
||||
{
|
||||
lwip_config_tcp(0, lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
|
||||
static char usage_info[] = "Run either a iperf server or iperf client.";
|
||||
static char program_info[] = "Lwip socket test task, a simple iperf.";
|
||||
static const char* const usages[] = {
|
||||
"TestIperf -c [--ip arg] [-p arg]",
|
||||
"TestIperf -s [-p arg]",
|
||||
NULL,
|
||||
};
|
||||
|
||||
static struct IperfParam iperf_param = {
|
||||
.host = "255.255.255.255",
|
||||
.port = 5001,
|
||||
};
|
||||
|
||||
enum IperfMode mode = 0;
|
||||
char* ip_ptr = NULL;
|
||||
bool is_help = false;
|
||||
struct argparse_option options[] = {
|
||||
OPT_HELP(&is_help),
|
||||
OPT_GROUP("Bit Options"),
|
||||
OPT_BIT(IPERF_PARAM_SERVER, "server", &mode, "start a iperf server", NULL, IPERF_MODE_SERVER, 0),
|
||||
OPT_BIT(IPERF_PARAM_CLIENT, "client", &mode, "start a iperf client", NULL, IPERF_MODE_CLIENT, 0),
|
||||
OPT_BIT(IPERF_PARAM_STOP, "stop", &mode, "stop iperf", NULL, IPERF_MODE_STOP, OPT_NONEG),
|
||||
OPT_GROUP("Param Options"),
|
||||
OPT_STRING(IPERF_PARAM_IPADDR, "ip", &ip_ptr, "server IP if iperf is a client", NULL, 0, 0),
|
||||
OPT_INTEGER(IPERF_PARAM_PORT, "port", &iperf_param.port, "server PORT needed for iperf", NULL, 0, 0),
|
||||
OPT_END(),
|
||||
};
|
||||
|
||||
struct argparse argparse;
|
||||
argparse_init(&argparse, options, usages, 0);
|
||||
argparse_describe(&argparse, usage_info, program_info);
|
||||
argc = argparse_parse(&argparse, argc, (const char**)argv);
|
||||
/* help task */
|
||||
if (is_help) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* stop iperf task */
|
||||
if (mode & IPERF_MODE_STOP) {
|
||||
SetGlobalMode(IPERF_MODE_STOP);
|
||||
return;
|
||||
}
|
||||
if (mode & IPERF_MODE_SERVER && mode & IPERF_MODE_CLIENT) {
|
||||
printf("[%s] Err: Can't run iperf server and client at one time.\n", __func__);
|
||||
}
|
||||
|
||||
/* iperf server or iperf client*/
|
||||
struct AtomicIperfMode* iperf_mode = GetGlobalIperfMode();
|
||||
PrivMutexObtain(&iperf_mode->mtx);
|
||||
if (iperf_mode->mode != IPERF_MODE_STOP) {
|
||||
PrivMutexAbandon(&iperf_mode->mtx);
|
||||
printf("[%s] Err: There is already a iperf running, please stop it before running a new one\n", __func__);
|
||||
return;
|
||||
}
|
||||
|
||||
if (mode & IPERF_MODE_SERVER) {
|
||||
iperf_mode->mode = IPERF_MODE_SERVER;
|
||||
} else if (mode & IPERF_MODE_CLIENT) {
|
||||
if (ip_ptr == NONE) {
|
||||
PrivMutexAbandon(&iperf_mode->mtx);
|
||||
printf("[%s] Err: Iperf client must assign a server ip.\n", __func__);
|
||||
return;
|
||||
} else {
|
||||
memset(iperf_param.host, 0, sizeof(iperf_param.host));
|
||||
strncpy(iperf_param.host, ip_ptr, strlen(ip_ptr));
|
||||
}
|
||||
iperf_mode->mode = IPERF_MODE_CLIENT;
|
||||
}
|
||||
PrivMutexAbandon(&iperf_mode->mtx);
|
||||
|
||||
pthread_t thd;
|
||||
mode = GetGlobalMode();
|
||||
if (mode == IPERF_MODE_SERVER) {
|
||||
printf("[%s] Running iperf server at port %d.\n", __func__, iperf_param.port);
|
||||
|
||||
PrivTaskCreate(&thd, NULL, TestIperfServer, (void*)&iperf_param);
|
||||
} else if (mode == IPERF_MODE_CLIENT) {
|
||||
printf("[%s] Running iperf client to server at %s:%d.\n", __func__, iperf_param.host, iperf_param.port);
|
||||
PrivTaskCreate(&thd, NULL, TestIperfClient, (void*)&iperf_param);
|
||||
}
|
||||
|
||||
PrivTaskStartup(&thd);
|
||||
}
|
||||
|
||||
PRIV_SHELL_CMD_FUNCTION(TestSocket, Test socket using iperf, PRIV_SHELL_CMD_MAIN_ATTR | SHELL_CMD_PARAM_NUM(8));
|
||||
95
APP_Framework/Applications/app_test/test_uart.c
Normal file
95
APP_Framework/Applications/app_test/test_uart.c
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* 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_uart.c
|
||||
* @brief: a application of uart function, uart6 for edu-arm32
|
||||
* @version: 3.0
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2023/8/11
|
||||
*/
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <transform.h>
|
||||
|
||||
#include <argparse.h>
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
|
||||
void TestUart(int argc, char* argv[])
|
||||
{
|
||||
static char program_info[] = "App Test uart, sending a message through uart and receive messages from uart.";
|
||||
static const char* const usages[] = {
|
||||
"TestUart -m arg",
|
||||
NULL,
|
||||
};
|
||||
|
||||
bool is_help = false;
|
||||
char* msg = NULL;
|
||||
struct argparse_option options[] = {
|
||||
OPT_HELP(&is_help),
|
||||
OPT_STRING('m', "message", &msg, "MESSAGE to send through uart.", NULL, 0, 0),
|
||||
OPT_END(),
|
||||
};
|
||||
struct argparse argparse;
|
||||
argparse_init(&argparse, options, usages, 0);
|
||||
argparse_describe(&argparse, NULL, program_info);
|
||||
argc = argparse_parse(&argparse, argc, (const char**)argv);
|
||||
if (is_help) {
|
||||
return;
|
||||
}
|
||||
|
||||
int uart_fd = PrivOpen(UART_DEV_DRIVER, O_RDWR);
|
||||
if (uart_fd < 0) {
|
||||
printf("open pin fd error:%d\n", uart_fd);
|
||||
return;
|
||||
}
|
||||
printf("[%s] Info: Uart and pin fopen success\n", __func__);
|
||||
|
||||
struct SerialDataCfg uart_cfg;
|
||||
memset(&uart_cfg, 0, sizeof(struct SerialDataCfg));
|
||||
|
||||
uart_cfg.serial_baud_rate = BAUD_RATE_115200;
|
||||
uart_cfg.serial_data_bits = DATA_BITS_8;
|
||||
uart_cfg.serial_stop_bits = STOP_BITS_1;
|
||||
uart_cfg.serial_parity_mode = PARITY_NONE;
|
||||
uart_cfg.serial_bit_order = BIT_ORDER_LSB;
|
||||
uart_cfg.serial_invert_mode = NRZ_NORMAL;
|
||||
uart_cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
|
||||
uart_cfg.serial_timeout = -1;
|
||||
uart_cfg.is_ext_uart = 0;
|
||||
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
|
||||
ioctl_cfg.args = (void*)&uart_cfg;
|
||||
|
||||
if (0 != PrivIoctl(uart_fd, OPE_INT, &ioctl_cfg)) {
|
||||
printf("[%s] Err: ioctl uart fd error %d\n", __func__, uart_fd);
|
||||
PrivClose(uart_fd);
|
||||
return;
|
||||
}
|
||||
PrivWrite(uart_fd, msg, strlen(msg));
|
||||
|
||||
char recv_buf[100];
|
||||
while (1) {
|
||||
memset(recv_buf, 0, sizeof(recv_buf));
|
||||
PrivRead(uart_fd, recv_buf, sizeof(recv_buf));
|
||||
printf("[%s] Info: Recv from uart: %s\n", __func__, recv_buf);
|
||||
}
|
||||
|
||||
PrivClose(uart_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
PRIV_SHELL_CMD_FUNCTION(TestUart, a uart test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||
#endif
|
||||
@@ -180,7 +180,7 @@ int SerialRead(uint8_t *read_data, int length)
|
||||
int data_recv_size = 0;
|
||||
|
||||
while (data_size < length) {
|
||||
data_recv_size = PrivRead(uart_fd, read_data + data_recv_size, length);
|
||||
data_recv_size = PrivRead(uart_fd, read_data + data_size, length - data_size);
|
||||
data_size += data_recv_size;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user