From 491524cb9621f85c011215748d30709fc4578adf Mon Sep 17 00:00:00 2001 From: Sherman Date: Wed, 23 Jun 2021 18:35:19 +0800 Subject: [components]rt-link v0.2.0 新特性: 1. service 通道增加【连接状态】属性 2. service 通道增加发送消息质量配置,ACK、CRC 可配置开关; 3. 发送接口支持阻塞、非阻塞两种发送方式; 4. 已对接到 device 框架,支持 rt_device 接口操作 5. 已对接到 DFS 框架,支持 POSIX 接口操作 6. 移除 hw_port 目录,改为软件包方式添加 hw_port 7. 修复已知 BUG MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/rt-link/rtlink_dev_example.c | 395 ++++++++++++++++++++++++++++++++++ 1 file changed, 395 insertions(+) create mode 100644 examples/rt-link/rtlink_dev_example.c (limited to 'examples/rt-link/rtlink_dev_example.c') diff --git a/examples/rt-link/rtlink_dev_example.c b/examples/rt-link/rtlink_dev_example.c new file mode 100644 index 0000000000..0a026bcb3d --- /dev/null +++ b/examples/rt-link/rtlink_dev_example.c @@ -0,0 +1,395 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-06-19 Sherman the first version + */ + +#include +#include + +#define DBG_TAG "example" +#define DBG_LVL DBG_LOG +#include +#include + +enum +{ + NONE_TEST = 0, + SHORT_FRAME_TEST, + LONG_FRAME_TEST +}; +static rt_uint8_t speed_test_type = NONE_TEST; + +static struct rt_link_device rtlink_dev = {0}; +#define RTLINK01 "rtlink01" +#define TEST_CONTEXT "This message is sent by RT-Link" +rt_uint8_t test_buff[1024] = {0}; + +static rt_err_t rtlink_dev_rx_ind(rt_device_t dev, rt_size_t size) +{ + RT_ASSERT(dev != RT_NULL); + LOG_I("rx_ind: dev name %s, rx size %d", dev->parent.name, size); + return RT_EOK; +} + +static rt_err_t rtlink_dev_tx_done(rt_device_t dev, void *buffer) +{ + RT_ASSERT(dev != RT_NULL); + struct rt_link_device *rtlink_dev = (struct rt_link_device *)dev; + LOG_I("tx_done: dev name %s, buffer 0x%p errno %d", dev->parent.name, buffer, rtlink_dev->service.err); + rt_free(buffer); + return RT_EOK; +} + +#ifdef RT_USING_POSIX +#include +#include +#include + +#define RTLINK01_PATH "/dev/rtlink01" +int fd = -1; + +static void rtlink_fopen(int argc, char *argv[]) +{ + fd = open(RTLINK01_PATH, O_RDWR | O_NONBLOCK); + + if (fd < 0) + { + LOG_E("open rt_link failed!"); + } +} +MSH_CMD_EXPORT(rtlink_fopen, rtlink posix interface example); + +static void rtlink_fclose(int argc, char *argv[]) +{ + LOG_D("colse %d", fd); + close(fd); + fd = -1; +} +MSH_CMD_EXPORT(rtlink_fclose, rtlink posix interface example); + +static void rtlink_fread(int argc, char *argv[]) +{ + int read_len; + read_len = read(fd, test_buff, sizeof(test_buff)); + LOG_D("read len %d", read_len); + LOG_HEX("read", 8, test_buff, 32); +} +MSH_CMD_EXPORT(rtlink_fread, rtlink posix interface example); + +static void rtlink_fwrite(int argc, char *argv[]) +{ + char *data = RT_NULL; + rt_size_t length = 0; + rt_uint16_t count = 0; + rt_size_t ret = 0; + + if (argc == 1) + { + data = rt_malloc(sizeof(TEST_CONTEXT)); + if (data) + { + length = sizeof(TEST_CONTEXT) - 1; + rt_memcpy(data, TEST_CONTEXT, sizeof(TEST_CONTEXT) - 1); + ret = write(fd, data, length); + } + LOG_I("write data(0x%p) result: %d.", data, ret); + } + else if (argc >= 3) + { + if (strcmp(argv[1], "-l") == 0) + { + data = rt_malloc(atoi((const char *)argv[2])); + if (data) + { + for (count = 0; count < atoi((const char *)argv[2]); count++) + { + data[count] = (count % 93 + 33); + } + length = atoi((const char *)argv[2]); + ret = write(fd, data, length); + } + LOG_I("write data(0x%p) result: %d.", data, ret); + } + else + { + LOG_E("Invalid parameter."); + } + } +} +MSH_CMD_EXPORT(rtlink_fwrite, rtlink posix interface example); + +#define RTLINK02 "rtlink02" +#define RTLINK02_PATH "/dev/rtlink02" +static struct rt_link_device rtlink_fd = {0}; +rt_uint8_t fd_buff[1024] = {0}; + +static void listen_thread(void *param) +{ + int fd = open(RTLINK02_PATH, O_RDWR | O_NONBLOCK); + if (fd < 0) + { + LOG_E("open (%s) failed", RTLINK02); + return; + } + while (1) + { + rt_uint8_t *write_buf = RT_NULL; + int write_len = 0; + fd_set readfds, writefds; + FD_ZERO(&readfds); + FD_ZERO(&writefds); + FD_SET(fd, &readfds); + FD_SET(fd, &writefds); + + int ret = select(fd + 1, &readfds, &writefds, RT_NULL, RT_NULL); + LOG_D("select ret(%d), read (%d), write (%d)", ret, readfds, writefds); + if (FD_ISSET(fd, &readfds)) + { + LOG_I("POLLIN"); + int read_len = read(fd, fd_buff, sizeof(test_buff)); + LOG_D("read len %d", read_len); + LOG_HEX("read", 8, test_buff, 32); + } + + if (FD_ISSET(fd, &writefds)) + { + LOG_I("POLLOUT"); + write_buf = rt_malloc(1024); + if (write_buf) + { + write_len = write(fd, write_buf, 1024); + LOG_D("write %d", write_len); + } + } + + rt_thread_delay(500); + } + LOG_I("fd (%s) listen thread exit", RTLINK02); +} + +static void rtlink_fselect() +{ + /* step1: register rtlink to to the device framework */ + rt_link_dev_register(&rtlink_fd, RTLINK02, + RT_DEVICE_FLAG_RDWR | + RT_DEVICE_FLAG_REMOVABLE | + RT_DEVICE_FLAG_STANDALONE, + RT_NULL); + + /* step2: Initialize the rlink device as the default configuration, */ + rt_device_t device = rt_device_find(RTLINK02); + if (device == RT_NULL) + { + LOG_E("device not find!"); + return ; + } + rt_device_init(device); + + /* step3: config rtlink device rx/tx callback, channel, send timeout */ + rt_device_set_rx_indicate(device, rtlink_dev_rx_ind); + rt_device_set_tx_complete(device, rtlink_dev_tx_done); + struct rt_link_service service; + service.service = RT_LINK_SERVICE_MNGT; + service.timeout_tx = RT_WAITING_NO; + rt_device_control(device, RT_DEVICE_CTRL_CONFIG, &service); + + rt_thread_t tid = rt_thread_create(RTLINK02, listen_thread, RT_NULL, 1024, 21, 20); + if (tid) + { + rt_thread_startup(tid); + } +} +MSH_CMD_EXPORT(rtlink_fselect, rtlink posix interface example); +#endif /* RT_USING_POSIX */ + +static void rtlink_dread(void) +{ + rt_size_t read_len = 0; + rt_device_t dev = rt_device_find(RTLINK01); + if (dev == RT_NULL) + { + LOG_E("dev %s not find ", RTLINK01); + return; + } + + read_len = rt_device_read(dev, 0, test_buff, sizeof(test_buff)); + + LOG_D("read len %d", read_len); + LOG_HEX("read", 8, test_buff, 32); +} +MSH_CMD_EXPORT(rtlink_dread, rtlink device interface example); + +void rt_link_speed_test(void *paremeter) +{ + int ret; + rt_uint8_t *send_buf, *data; + rt_size_t bufflen = 0; + rt_size_t sentlen = 0; + rt_size_t count = 0; + rt_tick_t tick1, tick2; + rt_size_t total = 0; + rt_uint32_t integer, decimal; + rt_device_t dev = rt_device_find(RTLINK01); + if (dev == RT_NULL) + { + LOG_E("dev %s not find!", RTLINK01); + return ; + } + + if (speed_test_type == SHORT_FRAME_TEST) + { + bufflen = 988; + } + else + { + bufflen = 3036; + } + + send_buf = rt_malloc(bufflen); + if (send_buf != RT_NULL) + { + data = send_buf; + for (count = 0; count < bufflen; count++) + { + *data++ = (count % 93 + 33); + } + } + else + { + rt_kprintf("speed of send buffer malloc failed."); + return; + } + + tick1 = rt_tick_get(); + while (speed_test_type) + { + ret = rt_device_write(dev, 0, send_buf, bufflen); + + if (ret == RT_EOK) + { + sentlen += bufflen; + } + + tick2 = rt_tick_get(); + if (tick2 - tick1 >= RT_TICK_PER_SECOND) + { + total = sentlen * RT_TICK_PER_SECOND / 125 / (tick2 - tick1); + integer = total / 1000; + decimal = total % 1000; + LOG_I("%d.%03d0 Mbps!", integer, decimal); + sentlen = 0; + tick1 = tick2; + } + } + rt_free(send_buf); + LOG_W("speed test end, type %d", speed_test_type); +} + +void create_thead_to_test_speed(rt_uint8_t mutil_num) +{ + rt_uint8_t i = 0; + + LOG_D("Speed test type [%02d], mutil [%02d]", speed_test_type, mutil_num); + for (i = 0; i < mutil_num; i++) + { + rt_thread_t tid; + char tid_name[RT_NAME_MAX + 1] = {0}; + + rt_snprintf(tid_name, sizeof(tid_name), "lny_s%03d", i + 1); + tid = rt_thread_create(tid_name, rt_link_speed_test, RT_NULL, 1024, 20, 10); + rt_thread_startup(tid); + LOG_I("Speed test thread[%s] startup", tid_name); + } +} + +static void rtlink_dwrite(int argc, char *argv[]) +{ + char *data = RT_NULL; + rt_size_t length = 0; + rt_uint16_t count = 0; + rt_size_t ret = RT_ERROR; + + rt_device_t dev = rt_device_find(RTLINK01); + if (dev == RT_NULL) + { + LOG_E("device not find!"); + return ; + } + + if (argc == 1) + { + data = rt_malloc(sizeof(TEST_CONTEXT)); + length = sizeof(TEST_CONTEXT) - 1; + rt_memcpy(data, TEST_CONTEXT, sizeof(TEST_CONTEXT) - 1); + + ret = rt_device_write(dev, 0, data, length); + LOG_I("write data(0x%p) result: %d.", data, ret); + } + else if (argc >= 3) + { + if (strcmp(argv[1], "-l") == 0) + { + data = rt_malloc(atoi((const char *)argv[2])); + for (count = 0; count < atoi((const char *)argv[2]); count++) + { + data[count] = (count % 93 + 33); + } + length = atoi((const char *)argv[2]); + ret = rt_device_write(dev, 0, data, length); + LOG_I("write data(0x%p) result: %d.", data, ret); + } + else + { + LOG_E("Invalid parameter."); + } + } +} +MSH_CMD_EXPORT(rtlink_dwrite, rtlink device interface example); + +static void rtlink_dinit(void) +{ + /* step1: register rtlink to to the device framework */ + rt_link_dev_register(&rtlink_dev, RTLINK01, + RT_DEVICE_FLAG_RDWR | + RT_DEVICE_FLAG_REMOVABLE | + RT_DEVICE_FLAG_STANDALONE, + RT_NULL); + + /* step2: Initialize the rlink device as the default configuration, */ + rt_device_t device = rt_device_find(RTLINK01); + if (device == RT_NULL) + { + LOG_E("device not find!"); + return ; + } + rt_device_init(device); + + /* step3: config rtlink device rx/tx callback, channel, send timeout */ + rt_device_set_rx_indicate(device, rtlink_dev_rx_ind); + rt_device_set_tx_complete(device, rtlink_dev_tx_done); + struct rt_link_service service; + service.service = RT_LINK_SERVICE_SOCKET; + service.timeout_tx = RT_WAITING_FOREVER; + service.flag = RT_LINK_FLAG_ACK | RT_LINK_FLAG_CRC; + rt_device_control(device, RT_DEVICE_CTRL_CONFIG, &service); +} +MSH_CMD_EXPORT(rtlink_dinit, rtlink device interface example); + +static void rtlink_dopen() +{ + /* step4: open rtlink device, attach the service channel */ + rt_device_t device = rt_device_find(RTLINK01); + if (device == RT_NULL) + { + LOG_E("device not find!"); + return ; + } + rt_err_t ret = rt_device_open(device, RT_DEVICE_OFLAG_RDWR); + LOG_I("dev open ret %d", ret); +} +MSH_CMD_EXPORT(rtlink_dopen, rtlink device interface example); -- cgit v1.3.1 From c360688714d3aaf1f2114c45afaa3ee2ddc64fdd Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Sat, 11 Sep 2021 11:48:14 -0400 Subject: 使用poll.h代替dfs_poll.h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/dfs/src/dfs_poll.c | 2 +- components/dfs/src/dfs_select.c | 2 +- components/drivers/serial/serial.c | 2 +- components/drivers/serial/serial_v2.c | 2 +- components/drivers/src/pipe.c | 2 +- components/libc/compilers/common/poll.h | 9 +++++++++ components/lwp/lwp_syscall.c | 2 +- components/net/at/at_socket/at_socket.c | 2 +- components/net/sal_socket/impl/af_inet_at.c | 2 +- components/net/sal_socket/impl/af_inet_lwip.c | 2 +- components/net/sal_socket/socket/net_sockets.c | 2 +- components/utilities/rt-link/src/rtlink_dev.c | 2 +- examples/rt-link/rtlink_dev_example.c | 2 +- 13 files changed, 21 insertions(+), 12 deletions(-) (limited to 'examples/rt-link/rtlink_dev_example.c') diff --git a/components/dfs/src/dfs_poll.c b/components/dfs/src/dfs_poll.c index d1e0372494..607b3487be 100644 --- a/components/dfs/src/dfs_poll.c +++ b/components/dfs/src/dfs_poll.c @@ -17,7 +17,7 @@ #include #include #include -#include +#include #ifdef RT_USING_POSIX diff --git a/components/dfs/src/dfs_select.c b/components/dfs/src/dfs_select.c index c9286ca97f..ca9c549259 100644 --- a/components/dfs/src/dfs_select.c +++ b/components/dfs/src/dfs_select.c @@ -11,7 +11,7 @@ #include #include -#include +#include #include #ifdef RT_USING_POSIX diff --git a/components/drivers/serial/serial.c b/components/drivers/serial/serial.c index c567e34b52..289d523bd2 100644 --- a/components/drivers/serial/serial.c +++ b/components/drivers/serial/serial.c @@ -38,7 +38,7 @@ #ifdef RT_USING_POSIX #include -#include +#include #include #ifdef RT_USING_POSIX_TERMIOS diff --git a/components/drivers/serial/serial_v2.c b/components/drivers/serial/serial_v2.c index 6e65457a5d..390dd31748 100644 --- a/components/drivers/serial/serial_v2.c +++ b/components/drivers/serial/serial_v2.c @@ -18,7 +18,7 @@ #ifdef RT_USING_POSIX #include -#include +#include #include #ifdef getc diff --git a/components/drivers/src/pipe.c b/components/drivers/src/pipe.c index 7fa5eb067d..af90f13c6f 100644 --- a/components/drivers/src/pipe.c +++ b/components/drivers/src/pipe.c @@ -16,7 +16,7 @@ #ifdef RT_USING_POSIX #include #include -#include +#include #include static int pipe_fops_open(struct dfs_fd *fd) diff --git a/components/libc/compilers/common/poll.h b/components/libc/compilers/common/poll.h index 1862373559..aea73d6eaf 100644 --- a/components/libc/compilers/common/poll.h +++ b/components/libc/compilers/common/poll.h @@ -1,3 +1,12 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-09-11 Meco Man First version + */ #ifndef __POLL_H__ #define __POLL_H__ diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index 02915ab1cc..d9112eed3e 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -13,7 +13,7 @@ #include #include -#include +#include #include #if (defined(RT_USING_SAL) && defined(SAL_USING_POSIX)) diff --git a/components/net/at/at_socket/at_socket.c b/components/net/at/at_socket/at_socket.c index 8215dd4417..96ea053f9c 100644 --- a/components/net/at/at_socket/at_socket.c +++ b/components/net/at/at_socket/at_socket.c @@ -19,7 +19,7 @@ #include #ifdef SAL_USING_POSIX -#include +#include #endif #include diff --git a/components/net/sal_socket/impl/af_inet_at.c b/components/net/sal_socket/impl/af_inet_at.c index c85dfe9f06..07dff0018c 100644 --- a/components/net/sal_socket/impl/af_inet_at.c +++ b/components/net/sal_socket/impl/af_inet_at.c @@ -19,7 +19,7 @@ #include #ifdef SAL_USING_POSIX -#include +#include #endif #ifdef SAL_USING_AT diff --git a/components/net/sal_socket/impl/af_inet_lwip.c b/components/net/sal_socket/impl/af_inet_lwip.c index 2e19275b5d..2cf0958ee4 100644 --- a/components/net/sal_socket/impl/af_inet_lwip.c +++ b/components/net/sal_socket/impl/af_inet_lwip.c @@ -17,7 +17,7 @@ #include #ifdef SAL_USING_POSIX -#include +#include #endif #include diff --git a/components/net/sal_socket/socket/net_sockets.c b/components/net/sal_socket/socket/net_sockets.c index 7d4db34327..1b6db50e6d 100644 --- a/components/net/sal_socket/socket/net_sockets.c +++ b/components/net/sal_socket/socket/net_sockets.c @@ -11,7 +11,7 @@ #include #include -#include +#include #include #include #include diff --git a/components/utilities/rt-link/src/rtlink_dev.c b/components/utilities/rt-link/src/rtlink_dev.c index 5eff1f9782..efebf37565 100644 --- a/components/utilities/rt-link/src/rtlink_dev.c +++ b/components/utilities/rt-link/src/rtlink_dev.c @@ -21,7 +21,7 @@ #ifdef RT_USING_POSIX #include -#include +#include int rtlink_fops_open(struct dfs_fd *fd) { diff --git a/examples/rt-link/rtlink_dev_example.c b/examples/rt-link/rtlink_dev_example.c index 0a026bcb3d..d00f916efd 100644 --- a/examples/rt-link/rtlink_dev_example.c +++ b/examples/rt-link/rtlink_dev_example.c @@ -47,7 +47,7 @@ static rt_err_t rtlink_dev_tx_done(rt_device_t dev, void *buffer) #ifdef RT_USING_POSIX #include -#include +#include #include #define RTLINK01_PATH "/dev/rtlink01" -- cgit v1.3.1 From ee4b7916a35cff4c97103fe631a2eba7eccd69ac Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Wed, 22 Sep 2021 16:45:21 -0400 Subject: [libc标准化]脱敏dfs_select.h 由标准sys/select.h代替 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/dfs/src/dfs_select.c | 2 +- components/lwp/lwp_syscall.c | 2 +- components/net/sal_socket/SConscript | 3 --- .../sal_socket/include/dfs_net/sys_select/sys/select.h | 16 ---------------- examples/libc/termios_test.c | 2 +- examples/rt-link/rtlink_dev_example.c | 2 +- 6 files changed, 4 insertions(+), 23 deletions(-) delete mode 100644 components/net/sal_socket/include/dfs_net/sys_select/sys/select.h (limited to 'examples/rt-link/rtlink_dev_example.c') diff --git a/components/dfs/src/dfs_select.c b/components/dfs/src/dfs_select.c index ca9c549259..795b816354 100644 --- a/components/dfs/src/dfs_select.c +++ b/components/dfs/src/dfs_select.c @@ -12,7 +12,7 @@ #include #include -#include +#include #ifdef RT_USING_POSIX diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index d9112eed3e..f9bc84ea97 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -14,7 +14,7 @@ #include #include -#include +#include #if (defined(RT_USING_SAL) && defined(SAL_USING_POSIX)) #include diff --git a/components/net/sal_socket/SConscript b/components/net/sal_socket/SConscript index cc7634f7d9..3903dce89a 100644 --- a/components/net/sal_socket/SConscript +++ b/components/net/sal_socket/SConscript @@ -26,9 +26,6 @@ if GetDepend('SAL_USING_POSIX'): CPPPATH += [cwd + '/include/dfs_net'] src += Glob('socket/net_sockets.c') src += Glob('dfs_net/*.c') - - if not GetDepend('HAVE_SYS_SELECT_H'): - CPPPATH += [cwd + '/include/dfs_net/sys_select'] if not GetDepend('HAVE_SYS_SOCKET_H'): CPPPATH += [cwd + '/include/socket/sys_socket'] diff --git a/components/net/sal_socket/include/dfs_net/sys_select/sys/select.h b/components/net/sal_socket/include/dfs_net/sys_select/sys/select.h deleted file mode 100644 index f174bc6504..0000000000 --- a/components/net/sal_socket/include/dfs_net/sys_select/sys/select.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-05-02 Bernard First version - */ - -#ifndef SELECT_H__ -#define SELECT_H__ - -#include - -#endif diff --git a/examples/libc/termios_test.c b/examples/libc/termios_test.c index f3a6ce3a1c..855bc82b96 100644 --- a/examples/libc/termios_test.c +++ b/examples/libc/termios_test.c @@ -16,7 +16,7 @@ #if defined (RT_USING_POSIX) #include - #include + #include #if defined (RT_USING_POSIX_TERMIOS) #include #else diff --git a/examples/rt-link/rtlink_dev_example.c b/examples/rt-link/rtlink_dev_example.c index d00f916efd..523b67ba53 100644 --- a/examples/rt-link/rtlink_dev_example.c +++ b/examples/rt-link/rtlink_dev_example.c @@ -48,7 +48,7 @@ static rt_err_t rtlink_dev_tx_done(rt_device_t dev, void *buffer) #ifdef RT_USING_POSIX #include #include -#include +#include #define RTLINK01_PATH "/dev/rtlink01" int fd = -1; -- cgit v1.3.1