diff --git a/components/dfs/KConfig b/components/dfs/KConfig index 5be74c7874f605deea8fbfed776c7d98c885b135..59adb46c711a7c65d8b2253fc690491718208918 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 5c7c8bf6a15b39b9adb181ba1b1187206e21ed1a..3ccaeb10803b3e056a478644f6ea0d863b10d8aa 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 e61da021792c6acc36cd8d8ae977cf42a781919a..6bfeccb578436989d7096f50ef55289f1cec073a 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 2a91434ef04579912cab389bbafe9d0b3a99c91c..5e84c4054083ce4162531b24c8e3b52bf039e622 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 0161cb0be5d00cc5ee22699573d1ee665da651d0..a22fbdc9add3aec6a33ed1d2a4560aea364d6d17 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 0000000000000000000000000000000000000000..cfdf74fbe42a0f9257f08094b2af7b319cb1cceb --- /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 e775474f0ab6f59dbf5665233996cdea6cf470bf..b0fe2041ab42cc00a0e225341ba5909dfd715e39 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 0000000000000000000000000000000000000000..0b5d6cef17fdb660a3aae9f646756643a3976de7 --- /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 b0bd234647fdac3dd5a9aea5646367f43b9e7b83..f95a9b1845edd0cd6772b64831c88addc385fa5a 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 44ba9f68a24ad54cb2ce7d352aa86bc24813aba3..36a8942331f201019b099760507934ecbf3ad9d5 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 c279490dec63cb22f82c0409bb62bfee53bbaf7d..52dd65e17b9c199ca67621a39ecbecd3d6553c32 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 bc9ed5f9a2bfecc487d2db7784785f1fc85adf7d..0553c500ee600a2d66277b537137ad5423d2ccc9 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 8e1f814560c5dee6485cf465db8b82026de75721..4c20b3c640b0f551d6510f2f7d3cd6227c8d7437 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 c245069e304ab6d17276894ec436a893233066d0..79bc78d2059657be6dc9d5b3efb32b10b03bc60f 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 719665b78e4ffb1d01c60bbf5825feaa298a88e0..74ca5fd1f62033b17bebd5b0dbce4892af4f06ea 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 9dcaa9df654d43f6a1b97c6ce5675566b4ac04ed..e7e99629d7ceb9eb30c9b0f27551b36958521207 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 e2cb04000b7a5f22a317efabd7bf46fe36b7d47c..79d702339a42229f70f400127a9a3b3189d5df01 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 c06ded705ecac76d86cb0d8a59a2229d87f07606..f8fbbb5e8f93b226aa3ba0aec51c816c46dbb177 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 69a33b391f7fe4dd15f5771d7a3efaf27f976a0f..9cd6c5e0e6292935fc821f627260585d916c3555 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 5c3f774226f2d1ace28c65b81bd8b367337331f3..43920ea3259c8863c85bda0cefa4b83a5646ac23 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 ac4f5b0ffd539e20bacd1c2118efdadc109bb02a..3b707d338e0f94755df868bb94c997e6f4fe27eb 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