From b837e656f9c9563074871e5128a2d9a098543907 Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Thu, 16 Aug 2018 18:09:27 +0200 Subject: [PATCH] stdio/vfs: generalized VFS mapping for STDIO --- sys/include/vfs.h | 9 +++++ sys/stdio_rtt/stdio_rtt.c | 53 +-------------------------- sys/stdio_uart/stdio_uart.c | 52 ++------------------------- sys/vfs/vfs_stdio.c | 71 +++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 102 deletions(-) create mode 100644 sys/vfs/vfs_stdio.c diff --git a/sys/include/vfs.h b/sys/include/vfs.h index 12423aaf8b..157b25e2e5 100644 --- a/sys/include/vfs.h +++ b/sys/include/vfs.h @@ -561,6 +561,15 @@ struct vfs_file_system_ops { int (*fstatvfs) (vfs_mount_t *mountp, vfs_file_t *filp, struct statvfs *buf); }; +/** + * @brief Allocate and bind file descriptors for STDIN, STDERR, and STDOUT + * + * This function is meant to be called once during system initialization time. + * It is typically called from the initialization of the selected STDIO + * implementation. + */ +void vfs_bind_stdio(void); + /** * @brief Close an open file * diff --git a/sys/stdio_rtt/stdio_rtt.c b/sys/stdio_rtt/stdio_rtt.c index 692d3aea14..1802da4830 100644 --- a/sys/stdio_rtt/stdio_rtt.c +++ b/sys/stdio_rtt/stdio_rtt.c @@ -75,12 +75,6 @@ * @} */ -#include -#if MODULE_VFS -#include -#include -#include -#endif #include #include "stdio_rtt.h" @@ -284,39 +278,6 @@ int rtt_write(const char* buf_ptr, unsigned num_bytes) { return num_bytes_written; } -#if MODULE_VFS - -static ssize_t rtt_stdio_vfs_read(vfs_file_t *filp, void *dest, size_t nbytes); -static ssize_t rtt_stdio_vfs_write(vfs_file_t *filp, const void *src, size_t nbytes); - -/** - * @brief VFS file operation table for stdin/stdout/stderr - */ -static vfs_file_ops_t rtt_stdio_vfs_ops = { - .read = rtt_stdio_vfs_read, - .write = rtt_stdio_vfs_write, -}; - -static ssize_t rtt_stdio_vfs_read(vfs_file_t *filp, void *dest, size_t nbytes) -{ - int fd = filp->private_data.value; - if (fd != STDIN_FILENO) { - return -EBADF; - } - return rtt_read(dest, nbytes); -} - -static ssize_t rtt_stdio_vfs_write(vfs_file_t *filp, const void *src, size_t nbytes) -{ - int fd = filp->private_data.value; - if (fd == STDIN_FILENO) { - return -EBADF; - } - return rtt_write(src, nbytes); -} - -#endif - void stdio_init(void) { #ifndef STDIO_RTT_DISABLE_STDIN stdin_enabled = 1; @@ -327,19 +288,7 @@ void stdio_init(void) { #endif #if MODULE_VFS - int fd; - fd = vfs_bind(STDIN_FILENO, O_RDONLY, &rtt_stdio_vfs_ops, (void *)STDIN_FILENO); - if (fd < 0) { - /* How to handle errors on init? */ - } - fd = vfs_bind(STDOUT_FILENO, O_WRONLY, &rtt_stdio_vfs_ops, (void *)STDOUT_FILENO); - if (fd < 0) { - /* How to handle errors on init? */ - } - fd = vfs_bind(STDERR_FILENO, O_WRONLY, &rtt_stdio_vfs_ops, (void *)STDERR_FILENO); - if (fd < 0) { - /* How to handle errors on init? */ - } + vfs_bind_stdio(); #endif /* the mutex should start locked */ diff --git a/sys/stdio_uart/stdio_uart.c b/sys/stdio_uart/stdio_uart.c index e89ca2047e..cb2a32a5f6 100644 --- a/sys/stdio_uart/stdio_uart.c +++ b/sys/stdio_uart/stdio_uart.c @@ -27,12 +27,6 @@ * @} */ -#include -#if MODULE_VFS -#include -#include -#include -#endif #include "stdio_uart.h" #include "board.h" @@ -51,40 +45,10 @@ extern ethos_t ethos; #define ENABLE_DEBUG 0 #include "debug.h" + static char _rx_buf_mem[STDIO_UART_RX_BUFSIZE]; isrpipe_t stdio_uart_isrpipe = ISRPIPE_INIT(_rx_buf_mem); -#if MODULE_VFS -static ssize_t stdio_uart_vfs_read(vfs_file_t *filp, void *dest, size_t nbytes); -static ssize_t stdio_uart_vfs_write(vfs_file_t *filp, const void *src, size_t nbytes); - -/** - * @brief VFS file operation table for stdin/stdout/stderr - */ -static vfs_file_ops_t stdio_uart_vfs_ops = { - .read = stdio_uart_vfs_read, - .write = stdio_uart_vfs_write, -}; - -static ssize_t stdio_uart_vfs_read(vfs_file_t *filp, void *dest, size_t nbytes) -{ - int fd = filp->private_data.value; - if (fd != STDIN_FILENO) { - return -EBADF; - } - return stdio_read(dest, nbytes); -} - -static ssize_t stdio_uart_vfs_write(vfs_file_t *filp, const void *src, size_t nbytes) -{ - int fd = filp->private_data.value; - if (fd == STDIN_FILENO) { - return -EBADF; - } - return stdio_write(src, nbytes); -} -#endif - void stdio_init(void) { #ifndef USE_ETHOS_FOR_STDIO @@ -93,19 +57,7 @@ void stdio_init(void) uart_init(ETHOS_UART, ETHOS_BAUDRATE, (uart_rx_cb_t) isrpipe_write_one, &stdio_uart_isrpipe); #endif #if MODULE_VFS - int fd; - fd = vfs_bind(STDIN_FILENO, O_RDONLY, &stdio_uart_vfs_ops, (void *)STDIN_FILENO); - if (fd < 0) { - /* How to handle errors on init? */ - } - fd = vfs_bind(STDOUT_FILENO, O_WRONLY, &stdio_uart_vfs_ops, (void *)STDOUT_FILENO); - if (fd < 0) { - /* How to handle errors on init? */ - } - fd = vfs_bind(STDERR_FILENO, O_WRONLY, &stdio_uart_vfs_ops, (void *)STDERR_FILENO); - if (fd < 0) { - /* How to handle errors on init? */ - } + vfs_bind_stdio(); #endif } diff --git a/sys/vfs/vfs_stdio.c b/sys/vfs/vfs_stdio.c new file mode 100644 index 0000000000..93f54ee25d --- /dev/null +++ b/sys/vfs/vfs_stdio.c @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2016 Eistec AB + * 2018 Freie UniversitÀt Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys + * @{ + * + * @file + * @brief STDIO binding for the VFS module + * + * @author Joakim NohlgÄrd + * @author Hauke Petersen + * + * @} + */ + +#include +#include +#include +#include + +#include "assert.h" +#include "stdio_base.h" +#include "vfs.h" + +static ssize_t _stdio_read(vfs_file_t *filp, void *dest, size_t nbytes) +{ + int fd = filp->private_data.value; + if (fd != STDIN_FILENO) { + return -EBADF; + } + return stdio_read(dest, nbytes); +} + +static ssize_t _stdio_write(vfs_file_t *filp, const void *src, size_t nbytes) +{ + int fd = filp->private_data.value; + if (fd == STDIN_FILENO) { + return -EBADF; + } + return stdio_write(src, nbytes); +} + +/** + * @brief VFS file operation table for stdin/stdout/stderr + */ +static vfs_file_ops_t _stdio_ops = { + .read = _stdio_read, + .write = _stdio_write, +}; + +void vfs_bind_stdio(void) +{ + int fd; + fd = vfs_bind(STDIN_FILENO, O_RDONLY, &_stdio_ops, (void *)STDIN_FILENO); + /* Is there a better way to handle errors on init? */ + assert(fd >= 0); + fd = vfs_bind(STDOUT_FILENO, O_WRONLY, &_stdio_ops, (void *)STDOUT_FILENO); + assert(fd >= 0); + fd = vfs_bind(STDERR_FILENO, O_WRONLY, &_stdio_ops, (void *)STDERR_FILENO); + assert(fd >= 0); + /* we are not interested in the return value in case assert is not + * compiled in */ + (void)fd; +}