[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;
This commit is contained in:
bernard 2017-10-17 22:27:06 +08:00
parent 947d8aa4d2
commit 8a38307e2c
21 changed files with 167 additions and 129 deletions

View File

@ -87,7 +87,8 @@ if RT_USING_DFS
config RT_USING_DFS_NET config RT_USING_DFS_NET
bool "Enable BSD socket operated by file system API" bool "Enable BSD socket operated by file system API"
depends on RT_USING_LWIP select RT_USING_LWIP
select RT_USING_POSIX
default n default n
help help
Let BSD socket operated by file system API, such as read/write and involveed in select/poll POSIX APIs. Let BSD socket operated by file system API, such as read/write and involveed in select/poll POSIX APIs.

View File

@ -10,7 +10,7 @@ src/dfs_posix.c
cwd = GetCurrentDir() cwd = GetCurrentDir()
CPPPATH = [cwd + "/include"] CPPPATH = [cwd + "/include"]
if GetDepend('RT_USING_DFS_NET'): if GetDepend('RT_USING_POSIX'):
src += ['src/poll.c', 'src/select.c'] src += ['src/poll.c', 'src/select.c']
group = DefineGroup('Filesystem', src, depend = ['RT_USING_DFS'], CPPPATH = CPPPATH) group = DefineGroup('Filesystem', src, depend = ['RT_USING_DFS'], CPPPATH = CPPPATH)

View File

@ -190,6 +190,7 @@ int dfs_device_fs_open(struct dfs_fd *file)
if (device == RT_NULL) if (device == RT_NULL)
return -ENODEV; return -ENODEV;
#ifdef RT_USING_POSIX
if (device->fops) if (device->fops)
{ {
/* use device fops */ /* use device fops */
@ -207,6 +208,7 @@ int dfs_device_fs_open(struct dfs_fd *file)
} }
} }
else else
#endif
{ {
result = rt_device_open(device, RT_DEVICE_OFLAG_RDWR); result = rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
if (result == RT_EOK || result == -RT_ENOSYS) if (result == RT_EOK || result == -RT_ENOSYS)

View File

@ -32,6 +32,7 @@
#include <rtdevice.h> #include <rtdevice.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <dfs_poll.h>
#include "dfs_net.h" #include "dfs_net.h"
int dfs_net_getsocket(int fd) int dfs_net_getsocket(int fd)

View File

@ -24,6 +24,7 @@
#include <dfs.h> #include <dfs.h>
#include <dfs_posix.h> #include <dfs_posix.h>
#include <dfs_poll.h>
#include <sys/socket.h> #include <sys/socket.h>
#include "dfs_net.h" #include "dfs_net.h"

View File

@ -0,0 +1,37 @@
#ifndef DFS_POLL_H__
#define DFS_POLL_H__
#include <rtthread.h>
#ifdef RT_USING_POSIX
#include <sys/time.h> /* 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

View File

@ -28,7 +28,6 @@
#define __DFS_POSIX_H__ #define __DFS_POSIX_H__
#include <dfs_file.h> #include <dfs_file.h>
#include <sys/time.h> /* for struct timeval */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -81,96 +80,6 @@ int access(const char *path, int amode);
int pipe(int fildes[2]); int pipe(int fildes[2]);
int mkfifo(const char *path, mode_t mode); 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 <lwip/sockets.h>
#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 #ifdef __cplusplus
} }
#endif #endif

View File

@ -0,0 +1,65 @@
#ifndef DFS_SELECT_H__
#define DFS_SELECT_H__
#ifdef RT_USING_LWIP
/* we use lwIP's structure definitions. */
#include <lwip/sockets.h>
#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

View File

@ -23,11 +23,14 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include <rthw.h>
#include <rtdevice.h> #include <rtdevice.h>
#include <rtthread.h>
#include <dfs.h> #include <dfs.h>
#include <dfs_file.h> #include <dfs_file.h>
#include <dfs_posix.h> #include <dfs_posix.h>
#include <dfs_poll.h>
struct rt_poll_node; struct rt_poll_node;

View File

@ -25,6 +25,9 @@
#include <dfs_fs.h> #include <dfs_fs.h>
#include <dfs_posix.h> #include <dfs_posix.h>
#include <dfs_poll.h>
#include <dfs_select.h>
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{ {
int fd; int fd;

View File

@ -44,14 +44,14 @@
#define DEBUG_COLOR #define DEBUG_COLOR
#include <rtdbg.h> #include <rtdbg.h>
#ifdef RT_USING_POSIX
#include <dfs_posix.h>
#include <dfs_poll.h>
#ifdef RT_USING_POSIX_TERMIOS #ifdef RT_USING_POSIX_TERMIOS
#include <posix_termios.h> #include <posix_termios.h>
#endif #endif
#ifdef RT_USING_DFS
#ifdef RT_USING_DFS_DEVFS
#include <dfs_posix.h>
/* it's possible the 'getc/putc' is defined by stdio.h in gcc/newlib. */ /* it's possible the 'getc/putc' is defined by stdio.h in gcc/newlib. */
#ifdef getc #ifdef getc
#undef getc #undef getc
@ -97,6 +97,7 @@ static int serial_fops_open(struct dfs_fd *fd)
break; break;
} }
if ((fd->flags & O_ACCMODE) != O_WRONLY)
rt_device_set_rx_indicate(device, serial_fops_rx_ind); rt_device_set_rx_indicate(device, serial_fops_rx_ind);
ret = rt_device_open(device, flags); ret = rt_device_open(device, flags);
if (ret == RT_EOK) return 0; if (ret == RT_EOK) return 0;
@ -210,7 +211,7 @@ const static struct dfs_file_ops _serial_fops =
serial_fops_poll, serial_fops_poll,
}; };
#endif #endif
#endif
/* /*
* Serial poll routines * Serial poll routines
*/ */
@ -993,7 +994,7 @@ rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
/* register a character device */ /* register a character device */
ret = rt_device_register(device, name, flag); ret = rt_device_register(device, name, flag);
#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS) #if defined(RT_USING_POSIX)
/* set fops */ /* set fops */
device->fops = &_serial_fops; device->fops = &_serial_fops;
#endif #endif

View File

@ -23,13 +23,13 @@
*/ */
#include <rthw.h> #include <rthw.h>
#include <rtdevice.h> #include <rtdevice.h>
#if defined(RT_USING_DFS)
#if defined(RT_USING_POSIX)
#include <dfs_file.h> #include <dfs_file.h>
#include <dfs_posix.h> #include <dfs_posix.h>
#endif #include <dfs_poll.h>
#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS) static int pipe_fops_open(struct dfs_fd *fd)
static int pipe_open(struct dfs_fd *fd)
{ {
rt_device_t device; rt_device_t device;
rt_pipe_t *pipe; rt_pipe_t *pipe;
@ -65,7 +65,7 @@ static int pipe_open(struct dfs_fd *fd)
return 0; return 0;
} }
static int pipe_close(struct dfs_fd *fd) static int pipe_fops_close(struct dfs_fd *fd)
{ {
rt_device_t device; rt_device_t device;
rt_pipe_t *pipe; rt_pipe_t *pipe;
@ -112,7 +112,7 @@ static int pipe_close(struct dfs_fd *fd)
return 0; 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; rt_pipe_t *pipe;
int ret = 0; int ret = 0;
@ -135,7 +135,7 @@ static int pipe_ioctl(struct dfs_fd *fd, int cmd, void *args)
return ret; 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; int len = 0;
rt_pipe_t *pipe; rt_pipe_t *pipe;
@ -185,7 +185,7 @@ out:
return len; 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; int len;
rt_pipe_t *pipe; rt_pipe_t *pipe;
@ -256,7 +256,7 @@ out:
return ret; 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; int mask = 0;
rt_pipe_t *pipe; 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 = static const struct dfs_file_ops pipe_fops =
{ {
pipe_open, pipe_fops_open,
pipe_close, pipe_fops_close,
pipe_ioctl, pipe_fops_ioctl,
pipe_read, pipe_fops_read,
pipe_write, pipe_fops_write,
RT_NULL, RT_NULL,
RT_NULL, RT_NULL,
RT_NULL, RT_NULL,
pipe_poll, pipe_fops_poll,
}; };
rt_pipe_t *rt_pipe_create(const char *name) rt_pipe_t *rt_pipe_create(const char *name)

View File

@ -84,7 +84,7 @@ const char *finsh_get_prompt()
static char finsh_getchar(void) static char finsh_getchar(void)
{ {
#ifdef RT_USING_POSIX_STDIN #ifdef RT_USING_POSIX
return getchar(); return getchar();
#else #else
char ch; char ch;
@ -97,7 +97,7 @@ static char finsh_getchar(void)
#endif #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) static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size)
{ {
RT_ASSERT(shell != RT_NULL); RT_ASSERT(shell != RT_NULL);
@ -420,7 +420,7 @@ void finsh_thread_entry(void *parameter)
finsh_init(&shell->parser); finsh_init(&shell->parser);
#endif #endif
#ifndef RT_USING_POSIX_STDIN #ifndef RT_USING_POSIX
/* set console device as shell device */ /* set console device as shell device */
if (shell->device == RT_NULL) if (shell->device == RT_NULL)
{ {

View File

@ -1,4 +1,4 @@
menu "libc" menu "POSIX layer and C standard library"
config RT_USING_LIBC config RT_USING_LIBC
bool "Enable libc APIs from toolchain" bool "Enable libc APIs from toolchain"
@ -9,12 +9,13 @@ config RT_USING_PTHREADS
default n default n
if RT_USING_LIBC if RT_USING_LIBC
config RT_USING_POSIX_STDIN config RT_USING_POSIX
bool "Enable stdin" bool "Enable POSIX layer for poll/select, stdin etc"
select RT_USING_DFS select RT_USING_DFS
select RT_USING_DFS_DEVFS select RT_USING_DFS_DEVFS
default y default y
if RT_USING_POSIX
config RT_USING_POSIX_MMAP config RT_USING_POSIX_MMAP
bool "Enable mmap() api" bool "Enable mmap() api"
default n default n
@ -23,5 +24,6 @@ if RT_USING_LIBC
bool "Enable termios feature" bool "Enable termios feature"
default n default n
endif endif
endif
endmenu endmenu

View File

@ -153,7 +153,7 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
if (fh == STDIN) if (fh == STDIN)
{ {
#ifdef RT_USING_POSIX_STDIN #ifdef RT_USING_POSIX
size = libc_stdio_read(buf, len); size = libc_stdio_read(buf, len);
return len - size; return len - size;
#else #else
@ -192,7 +192,7 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
#ifndef RT_USING_CONSOLE #ifndef RT_USING_CONSOLE
return 0; return 0;
#else #else
#ifdef RT_USING_POSIX_STDIN #ifdef RT_USING_POSIX
size = libc_stdio_write(buf, len); size = libc_stdio_write(buf, len);
return len - size; return len - size;
#else #else
@ -319,7 +319,7 @@ int fgetc(FILE *f)
{ {
char ch; char ch;
#ifdef RT_USING_POSIX_STDIN #ifdef RT_USING_POSIX
if (libc_stdio_read(&ch, 1) == 1) if (libc_stdio_read(&ch, 1) == 1)
return ch; return ch;
#endif #endif

View File

@ -38,7 +38,7 @@ size_t __read(int handle, unsigned char *buf, size_t len)
if (handle == _LLIO_STDIN) if (handle == _LLIO_STDIN)
{ {
#ifdef RT_USING_POSIX_STDIN #ifdef RT_USING_POSIX
return libc_stdio_read(buf, len); return libc_stdio_read(buf, len);
#else #else
return _LLIO_ERROR; return _LLIO_ERROR;

View File

@ -43,7 +43,7 @@ size_t __write(int handle, const unsigned char *buf, size_t len)
return _LLIO_ERROR; return _LLIO_ERROR;
#else #else
#ifdef RT_USING_POSIX_STDIN #ifdef RT_USING_POSIX
return libc_stdio_write((void*)buf, len); return libc_stdio_write((void*)buf, len);
#else #else
rt_device_t console_device; rt_device_t console_device;

View File

@ -43,7 +43,7 @@ int libc_system_init(void)
dev_console = rt_console_get_device(); dev_console = rt_console_get_device();
if (dev_console) 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); libc_stdio_set_console(dev_console->parent.name, O_RDWR);
#else #else
libc_stdio_set_console(dev_console->parent.name, O_WRONLY); libc_stdio_set_console(dev_console->parent.name, O_WRONLY);

View File

@ -23,6 +23,19 @@
#include <rtconfig.h> #include <rtconfig.h>
/* 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. */ /* Using this macro to control all kernel debug features. */
#ifdef RT_DEBUG #ifdef RT_DEBUG

View File

@ -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_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); 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; const struct dfs_file_ops *fops;
rt_list_t wait_queue; rt_list_t wait_queue;
#endif #endif

View File

@ -56,7 +56,7 @@ rt_err_t rt_device_register(rt_device_t dev,
dev->ref_count = 0; dev->ref_count = 0;
dev->open_flag = 0; dev->open_flag = 0;
#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS) #if defined(RT_USING_POSIX)
dev->fops = RT_NULL; dev->fops = RT_NULL;
rt_list_init(&(dev->wait_queue)); rt_list_init(&(dev->wait_queue));
#endif #endif