1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #9790 from haukepetersen/opt_stdio_vfs

vfs/stdio: cleanup STDIO mapping for vfs
This commit is contained in:
Joakim Nohlgård 2018-08-31 08:04:38 +02:00 committed by GitHub
commit 31c63210c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 102 deletions

View File

@ -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
*

View File

@ -75,12 +75,6 @@
* @}
*/
#include <stdio.h>
#if MODULE_VFS
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#endif
#include <string.h>
#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 */

View File

@ -27,12 +27,6 @@
* @}
*/
#include <stdio.h>
#if MODULE_VFS
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#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
}

71
sys/vfs/vfs_stdio.c Normal file
View File

@ -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 <joakim.nohlgard@eistec.se>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#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;
}