From 8a38307e2cce628017e9eb7153ae7a2430a3d9ef Mon Sep 17 00:00:00 2001 From: bernard Date: Tue, 17 Oct 2017 22:27:06 +0800 Subject: [PATCH] [libc] Add RT_USING_POSIX macro. 1. Add macro check in rtdebug.h; 2. Use RT_USING_POSIX for poll/select, stdin etc. 3. Split dfs_posix.h to dfs_posix.h, dfs_poll.h and dfs_select.h; --- components/dfs/KConfig | 3 +- components/dfs/SConscript | 2 +- components/dfs/filesystems/devfs/devfs.c | 2 + components/dfs/filesystems/net/dfs_net.c | 1 + components/dfs/filesystems/net/net_sockets.c | 1 + components/dfs/include/dfs_poll.h | 37 ++++++++ components/dfs/include/dfs_posix.h | 91 ------------------- components/dfs/include/dfs_select.h | 65 +++++++++++++ components/dfs/src/poll.c | 3 + components/dfs/src/select.c | 3 + components/drivers/serial/serial.c | 15 +-- components/drivers/src/pipe.c | 30 +++--- components/finsh/shell.c | 6 +- components/libc/KConfig | 8 +- components/libc/compilers/armlibc/stubs.c | 6 +- components/libc/compilers/dlib/syscall_read.c | 2 +- .../libc/compilers/dlib/syscall_write.c | 2 +- components/libc/compilers/newlib/libc.c | 2 +- include/rtdebug.h | 13 +++ include/rtdef.h | 2 +- src/device.c | 2 +- 21 files changed, 167 insertions(+), 129 deletions(-) create mode 100644 components/dfs/include/dfs_poll.h create mode 100644 components/dfs/include/dfs_select.h diff --git a/components/dfs/KConfig b/components/dfs/KConfig index 5be74c7874..59adb46c71 100644 --- a/components/dfs/KConfig +++ b/components/dfs/KConfig @@ -87,7 +87,8 @@ if RT_USING_DFS config RT_USING_DFS_NET bool "Enable BSD socket operated by file system API" - depends on RT_USING_LWIP + select RT_USING_LWIP + select RT_USING_POSIX default n help Let BSD socket operated by file system API, such as read/write and involveed in select/poll POSIX APIs. diff --git a/components/dfs/SConscript b/components/dfs/SConscript index 5c7c8bf6a1..3ccaeb1080 100644 --- a/components/dfs/SConscript +++ b/components/dfs/SConscript @@ -10,7 +10,7 @@ src/dfs_posix.c cwd = GetCurrentDir() CPPPATH = [cwd + "/include"] -if GetDepend('RT_USING_DFS_NET'): +if GetDepend('RT_USING_POSIX'): src += ['src/poll.c', 'src/select.c'] group = DefineGroup('Filesystem', src, depend = ['RT_USING_DFS'], CPPPATH = CPPPATH) diff --git a/components/dfs/filesystems/devfs/devfs.c b/components/dfs/filesystems/devfs/devfs.c index e61da02179..6bfeccb578 100644 --- a/components/dfs/filesystems/devfs/devfs.c +++ b/components/dfs/filesystems/devfs/devfs.c @@ -190,6 +190,7 @@ int dfs_device_fs_open(struct dfs_fd *file) if (device == RT_NULL) return -ENODEV; +#ifdef RT_USING_POSIX if (device->fops) { /* use device fops */ @@ -207,6 +208,7 @@ int dfs_device_fs_open(struct dfs_fd *file) } } else +#endif { result = rt_device_open(device, RT_DEVICE_OFLAG_RDWR); if (result == RT_EOK || result == -RT_ENOSYS) diff --git a/components/dfs/filesystems/net/dfs_net.c b/components/dfs/filesystems/net/dfs_net.c index 2a91434ef0..5e84c40540 100644 --- a/components/dfs/filesystems/net/dfs_net.c +++ b/components/dfs/filesystems/net/dfs_net.c @@ -32,6 +32,7 @@ #include #include +#include #include "dfs_net.h" int dfs_net_getsocket(int fd) diff --git a/components/dfs/filesystems/net/net_sockets.c b/components/dfs/filesystems/net/net_sockets.c index 0161cb0be5..a22fbdc9ad 100644 --- a/components/dfs/filesystems/net/net_sockets.c +++ b/components/dfs/filesystems/net/net_sockets.c @@ -24,6 +24,7 @@ #include #include +#include #include #include "dfs_net.h" diff --git a/components/dfs/include/dfs_poll.h b/components/dfs/include/dfs_poll.h new file mode 100644 index 0000000000..cfdf74fbe4 --- /dev/null +++ b/components/dfs/include/dfs_poll.h @@ -0,0 +1,37 @@ +#ifndef DFS_POLL_H__ +#define DFS_POLL_H__ + +#include + +#ifdef RT_USING_POSIX +#include /* for struct timeval */ + +#define POLLIN (0x01) +#define POLLRDNORM (0x01) +#define POLLRDBAND (0x01) +#define POLLPRI (0x01) + +#define POLLOUT (0x02) +#define POLLWRNORM (0x02) +#define POLLWRBAND (0x02) + +#define POLLERR (0x04) +#define POLLHUP (0x08) +#define POLLNVAL (0x10) + +#define POLLMASK_DEFAULT (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM) + +typedef unsigned int nfds_t; + +struct pollfd +{ + int fd; + short events; + short revents; +}; + +int poll(struct pollfd *fds, nfds_t nfds, int timeout); +#endif + +#endif + diff --git a/components/dfs/include/dfs_posix.h b/components/dfs/include/dfs_posix.h index e775474f0a..b0fe2041ab 100644 --- a/components/dfs/include/dfs_posix.h +++ b/components/dfs/include/dfs_posix.h @@ -28,7 +28,6 @@ #define __DFS_POSIX_H__ #include -#include /* for struct timeval */ #ifdef __cplusplus extern "C" { @@ -81,96 +80,6 @@ int access(const char *path, int amode); int pipe(int fildes[2]); int mkfifo(const char *path, mode_t mode); -/* poll and select */ - -#define POLLIN (0x01) -#define POLLRDNORM (0x01) -#define POLLRDBAND (0x01) -#define POLLPRI (0x01) - -#define POLLOUT (0x02) -#define POLLWRNORM (0x02) -#define POLLWRBAND (0x02) - -#define POLLERR (0x04) -#define POLLHUP (0x08) -#define POLLNVAL (0x10) - -#define POLLMASK_DEFAULT (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM) - -typedef unsigned int nfds_t; - -struct pollfd -{ - int fd; - short events; - short revents; -}; - -int poll(struct pollfd *fds, nfds_t nfds, int timeout); - - -#ifdef RT_USING_LWIP -/* we use lwIP's structure definitions. */ -#include -#else - -#ifndef FD_SET - -/* Get the total number of descriptors that we will have to support */ - -#define FD_SETSIZE (12) - -/* We will use a 32-bit bitsets to represent the set of descriptors. How - * many uint32_t's do we need to span all descriptors? - */ - -#if FD_SETSIZE <= 32 -# define __SELECT_NUINT32 1 -#elif FD_SETSIZE <= 64 -# define __SELECT_NUINT32 2 -#elif FD_SETSIZE <= 96 -# define __SELECT_NUINT32 3 -#elif FD_SETSIZE <= 128 -# define __SELECT_NUINT32 4 -#elif FD_SETSIZE <= 160 -# define __SELECT_NUINT32 5 -#elif FD_SETSIZE <= 192 -# define __SELECT_NUINT32 6 -#elif FD_SETSIZE <= 224 -# define __SELECT_NUINT32 7 -#elif FD_SETSIZE <= 256 -# define __SELECT_NUINT32 8 -#else -# warning "Larger fd_set needed" -#endif - -/* These macros map a file descriptor to an index and bit number */ - -#define _FD_NDX(fd) ((fd) >> 5) -#define _FD_BIT(fd) ((fd) & 0x1f) - -/* Standard helper macros */ - -#define FD_CLR(fd,set) \ - ((((fd_set*)(set))->arr)[_FD_NDX(fd)] &= ~(1 << _FD_BIT(fd))) -#define FD_SET(fd,set) \ - ((((fd_set*)(set))->arr)[_FD_NDX(fd)] |= (1 << _FD_BIT(fd))) -#define FD_ISSET(fd,set) \ - (((((fd_set*)(set))->arr)[_FD_NDX(fd)] & (1 << _FD_BIT(fd))) != 0) -#define FD_ZERO(set) \ - memset((set), 0, sizeof(fd_set)) - -typedef struct -{ - uint32_t arr[__SELECT_NUINT32]; -}fd_set; -#endif - -#endif - -int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); - #ifdef __cplusplus } #endif diff --git a/components/dfs/include/dfs_select.h b/components/dfs/include/dfs_select.h new file mode 100644 index 0000000000..0b5d6cef17 --- /dev/null +++ b/components/dfs/include/dfs_select.h @@ -0,0 +1,65 @@ +#ifndef DFS_SELECT_H__ +#define DFS_SELECT_H__ + +#ifdef RT_USING_LWIP +/* we use lwIP's structure definitions. */ +#include +#elif defined(RT_USING_POSIX) + +#ifndef FD_SET + +/* Get the total number of descriptors that we will have to support */ + +#define FD_SETSIZE (12) + +/* We will use a 32-bit bitsets to represent the set of descriptors. How + * many uint32_t's do we need to span all descriptors? + */ + +#if FD_SETSIZE <= 32 +# define __SELECT_NUINT32 1 +#elif FD_SETSIZE <= 64 +# define __SELECT_NUINT32 2 +#elif FD_SETSIZE <= 96 +# define __SELECT_NUINT32 3 +#elif FD_SETSIZE <= 128 +# define __SELECT_NUINT32 4 +#elif FD_SETSIZE <= 160 +# define __SELECT_NUINT32 5 +#elif FD_SETSIZE <= 192 +# define __SELECT_NUINT32 6 +#elif FD_SETSIZE <= 224 +# define __SELECT_NUINT32 7 +#elif FD_SETSIZE <= 256 +# define __SELECT_NUINT32 8 +#else +# warning "Larger fd_set needed" +#endif + +/* These macros map a file descriptor to an index and bit number */ + +#define _FD_NDX(fd) ((fd) >> 5) +#define _FD_BIT(fd) ((fd) & 0x1f) + +/* Standard helper macros */ + +#define FD_CLR(fd,set) \ + ((((fd_set*)(set))->arr)[_FD_NDX(fd)] &= ~(1 << _FD_BIT(fd))) +#define FD_SET(fd,set) \ + ((((fd_set*)(set))->arr)[_FD_NDX(fd)] |= (1 << _FD_BIT(fd))) +#define FD_ISSET(fd,set) \ + (((((fd_set*)(set))->arr)[_FD_NDX(fd)] & (1 << _FD_BIT(fd))) != 0) +#define FD_ZERO(set) \ + memset((set), 0, sizeof(fd_set)) + +typedef struct +{ + uint32_t arr[__SELECT_NUINT32]; +}fd_set; +#endif + +int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); + +#endif /* end of RT_USING_LWIP */ + +#endif diff --git a/components/dfs/src/poll.c b/components/dfs/src/poll.c index b0bd234647..f95a9b1845 100644 --- a/components/dfs/src/poll.c +++ b/components/dfs/src/poll.c @@ -23,11 +23,14 @@ */ #include +#include #include +#include #include #include #include +#include struct rt_poll_node; diff --git a/components/dfs/src/select.c b/components/dfs/src/select.c index 44ba9f68a2..36a8942331 100644 --- a/components/dfs/src/select.c +++ b/components/dfs/src/select.c @@ -25,6 +25,9 @@ #include #include +#include +#include + int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { int fd; diff --git a/components/drivers/serial/serial.c b/components/drivers/serial/serial.c index c279490dec..52dd65e17b 100644 --- a/components/drivers/serial/serial.c +++ b/components/drivers/serial/serial.c @@ -44,14 +44,14 @@ #define DEBUG_COLOR #include +#ifdef RT_USING_POSIX +#include +#include + #ifdef RT_USING_POSIX_TERMIOS #include #endif -#ifdef RT_USING_DFS -#ifdef RT_USING_DFS_DEVFS -#include - /* it's possible the 'getc/putc' is defined by stdio.h in gcc/newlib. */ #ifdef getc #undef getc @@ -97,7 +97,8 @@ static int serial_fops_open(struct dfs_fd *fd) break; } - rt_device_set_rx_indicate(device, serial_fops_rx_ind); + if ((fd->flags & O_ACCMODE) != O_WRONLY) + rt_device_set_rx_indicate(device, serial_fops_rx_ind); ret = rt_device_open(device, flags); if (ret == RT_EOK) return 0; @@ -210,7 +211,7 @@ const static struct dfs_file_ops _serial_fops = serial_fops_poll, }; #endif -#endif + /* * Serial poll routines */ @@ -993,7 +994,7 @@ rt_err_t rt_hw_serial_register(struct rt_serial_device *serial, /* register a character device */ ret = rt_device_register(device, name, flag); -#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS) +#if defined(RT_USING_POSIX) /* set fops */ device->fops = &_serial_fops; #endif diff --git a/components/drivers/src/pipe.c b/components/drivers/src/pipe.c index bc9ed5f9a2..0553c500ee 100644 --- a/components/drivers/src/pipe.c +++ b/components/drivers/src/pipe.c @@ -23,13 +23,13 @@ */ #include #include -#if defined(RT_USING_DFS) + +#if defined(RT_USING_POSIX) #include #include -#endif +#include -#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS) -static int pipe_open(struct dfs_fd *fd) +static int pipe_fops_open(struct dfs_fd *fd) { rt_device_t device; rt_pipe_t *pipe; @@ -65,7 +65,7 @@ static int pipe_open(struct dfs_fd *fd) return 0; } -static int pipe_close(struct dfs_fd *fd) +static int pipe_fops_close(struct dfs_fd *fd) { rt_device_t device; rt_pipe_t *pipe; @@ -112,7 +112,7 @@ static int pipe_close(struct dfs_fd *fd) return 0; } -static int pipe_ioctl(struct dfs_fd *fd, int cmd, void *args) +static int pipe_fops_ioctl(struct dfs_fd *fd, int cmd, void *args) { rt_pipe_t *pipe; int ret = 0; @@ -135,7 +135,7 @@ static int pipe_ioctl(struct dfs_fd *fd, int cmd, void *args) return ret; } -static int pipe_read(struct dfs_fd *fd, void *buf, size_t count) +static int pipe_fops_read(struct dfs_fd *fd, void *buf, size_t count) { int len = 0; rt_pipe_t *pipe; @@ -185,7 +185,7 @@ out: return len; } -static int pipe_write(struct dfs_fd *fd, const void *buf, size_t count) +static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count) { int len; rt_pipe_t *pipe; @@ -256,7 +256,7 @@ out: return ret; } -static int pipe_poll(struct dfs_fd *fd, rt_pollreq_t *req) +static int pipe_fops_poll(struct dfs_fd *fd, rt_pollreq_t *req) { int mask = 0; rt_pipe_t *pipe; @@ -308,15 +308,15 @@ static int pipe_poll(struct dfs_fd *fd, rt_pollreq_t *req) static const struct dfs_file_ops pipe_fops = { - pipe_open, - pipe_close, - pipe_ioctl, - pipe_read, - pipe_write, + pipe_fops_open, + pipe_fops_close, + pipe_fops_ioctl, + pipe_fops_read, + pipe_fops_write, RT_NULL, RT_NULL, RT_NULL, - pipe_poll, + pipe_fops_poll, }; rt_pipe_t *rt_pipe_create(const char *name) diff --git a/components/finsh/shell.c b/components/finsh/shell.c index 8e1f814560..4c20b3c640 100644 --- a/components/finsh/shell.c +++ b/components/finsh/shell.c @@ -84,7 +84,7 @@ const char *finsh_get_prompt() static char finsh_getchar(void) { -#ifdef RT_USING_POSIX_STDIN +#ifdef RT_USING_POSIX return getchar(); #else char ch; @@ -97,7 +97,7 @@ static char finsh_getchar(void) #endif } -#ifndef RT_USING_POSIX_STDIN +#ifndef RT_USING_POSIX static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size) { RT_ASSERT(shell != RT_NULL); @@ -420,7 +420,7 @@ void finsh_thread_entry(void *parameter) finsh_init(&shell->parser); #endif -#ifndef RT_USING_POSIX_STDIN +#ifndef RT_USING_POSIX /* set console device as shell device */ if (shell->device == RT_NULL) { diff --git a/components/libc/KConfig b/components/libc/KConfig index c245069e30..79bc78d205 100644 --- a/components/libc/KConfig +++ b/components/libc/KConfig @@ -1,4 +1,4 @@ -menu "libc" +menu "POSIX layer and C standard library" config RT_USING_LIBC bool "Enable libc APIs from toolchain" @@ -9,12 +9,13 @@ config RT_USING_PTHREADS default n if RT_USING_LIBC - config RT_USING_POSIX_STDIN - bool "Enable stdin" + config RT_USING_POSIX + bool "Enable POSIX layer for poll/select, stdin etc" select RT_USING_DFS select RT_USING_DFS_DEVFS default y + if RT_USING_POSIX config RT_USING_POSIX_MMAP bool "Enable mmap() api" default n @@ -22,6 +23,7 @@ if RT_USING_LIBC config RT_USING_POSIX_TERMIOS bool "Enable termios feature" default n + endif endif endmenu diff --git a/components/libc/compilers/armlibc/stubs.c b/components/libc/compilers/armlibc/stubs.c index 719665b78e..74ca5fd1f6 100644 --- a/components/libc/compilers/armlibc/stubs.c +++ b/components/libc/compilers/armlibc/stubs.c @@ -153,7 +153,7 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) if (fh == STDIN) { -#ifdef RT_USING_POSIX_STDIN +#ifdef RT_USING_POSIX size = libc_stdio_read(buf, len); return len - size; #else @@ -192,7 +192,7 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) #ifndef RT_USING_CONSOLE return 0; #else -#ifdef RT_USING_POSIX_STDIN +#ifdef RT_USING_POSIX size = libc_stdio_write(buf, len); return len - size; #else @@ -319,7 +319,7 @@ int fgetc(FILE *f) { char ch; -#ifdef RT_USING_POSIX_STDIN +#ifdef RT_USING_POSIX if (libc_stdio_read(&ch, 1) == 1) return ch; #endif diff --git a/components/libc/compilers/dlib/syscall_read.c b/components/libc/compilers/dlib/syscall_read.c index 9dcaa9df65..e7e99629d7 100644 --- a/components/libc/compilers/dlib/syscall_read.c +++ b/components/libc/compilers/dlib/syscall_read.c @@ -38,7 +38,7 @@ size_t __read(int handle, unsigned char *buf, size_t len) if (handle == _LLIO_STDIN) { -#ifdef RT_USING_POSIX_STDIN +#ifdef RT_USING_POSIX return libc_stdio_read(buf, len); #else return _LLIO_ERROR; diff --git a/components/libc/compilers/dlib/syscall_write.c b/components/libc/compilers/dlib/syscall_write.c index e2cb04000b..79d702339a 100644 --- a/components/libc/compilers/dlib/syscall_write.c +++ b/components/libc/compilers/dlib/syscall_write.c @@ -43,7 +43,7 @@ size_t __write(int handle, const unsigned char *buf, size_t len) return _LLIO_ERROR; #else -#ifdef RT_USING_POSIX_STDIN +#ifdef RT_USING_POSIX return libc_stdio_write((void*)buf, len); #else rt_device_t console_device; diff --git a/components/libc/compilers/newlib/libc.c b/components/libc/compilers/newlib/libc.c index c06ded705e..f8fbbb5e8f 100644 --- a/components/libc/compilers/newlib/libc.c +++ b/components/libc/compilers/newlib/libc.c @@ -43,7 +43,7 @@ int libc_system_init(void) dev_console = rt_console_get_device(); if (dev_console) { - #if defined(RT_USING_DFS_DEVFS) && defined(RT_USING_POSIX_STDIN) + #if defined(RT_USING_POSIX) libc_stdio_set_console(dev_console->parent.name, O_RDWR); #else libc_stdio_set_console(dev_console->parent.name, O_WRONLY); diff --git a/include/rtdebug.h b/include/rtdebug.h index 69a33b391f..9cd6c5e0e6 100644 --- a/include/rtdebug.h +++ b/include/rtdebug.h @@ -23,6 +23,19 @@ #include +/* settings depend check */ +#ifdef RT_USING_POSIX +#if !defined(RT_USING_DFS) || !defined(RT_USING_DFS_DEVFS) +#error "POSIX poll/select, stdin need file system(RT_USING_DFS) and device file system(RT_USING_DFS_DEVFS)" +#endif +#endif + +#ifdef RT_USING_POSIX_TERMIOS +#if !defined(RT_USING_POSIX) +#error "termios need POSIX layer(RT_USING_POSIX)" +#endif +#endif + /* Using this macro to control all kernel debug features. */ #ifdef RT_DEBUG diff --git a/include/rtdef.h b/include/rtdef.h index 5c3f774226..43920ea325 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -885,7 +885,7 @@ struct rt_device rt_size_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size); rt_err_t (*control)(rt_device_t dev, int cmd, void *args); -#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS) +#if defined(RT_USING_POSIX) const struct dfs_file_ops *fops; rt_list_t wait_queue; #endif diff --git a/src/device.c b/src/device.c index ac4f5b0ffd..3b707d338e 100644 --- a/src/device.c +++ b/src/device.c @@ -56,7 +56,7 @@ rt_err_t rt_device_register(rt_device_t dev, dev->ref_count = 0; dev->open_flag = 0; -#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS) +#if defined(RT_USING_POSIX) dev->fops = RT_NULL; rt_list_init(&(dev->wait_queue)); #endif -- GitLab