mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-18 12:52:44 +01:00
native: remove uart0
This commit is contained in:
parent
e17b664e3d
commit
0e4386a46f
@ -1,325 +0,0 @@
|
||||
/**
|
||||
* Native uart0 implementation
|
||||
*
|
||||
* Copyright (C) 2014 Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
|
||||
*
|
||||
* 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 native_board
|
||||
* @{
|
||||
* @file
|
||||
* @author Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <err.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/un.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <sys/select.h>
|
||||
|
||||
#include "cpu.h"
|
||||
#include "board_uart0.h"
|
||||
#include "thread.h"
|
||||
|
||||
#include "native_internal.h"
|
||||
#include "board_internal.h"
|
||||
|
||||
#define ENABLE_DEBUG (0)
|
||||
#include "debug.h"
|
||||
|
||||
int _native_uart_sock;
|
||||
int _native_uart_conn;
|
||||
|
||||
int _native_replay_enabled;
|
||||
FILE *_native_replay_buffer;
|
||||
|
||||
fd_set _native_uart_rfds;
|
||||
|
||||
/* uart API */
|
||||
|
||||
int uart0_puts(char *astring, int length)
|
||||
{
|
||||
int nwritten, offset;
|
||||
|
||||
nwritten = 0;
|
||||
offset = 0;
|
||||
|
||||
while (
|
||||
(length - offset > 0) && (
|
||||
(nwritten = _native_write(
|
||||
STDOUT_FILENO,
|
||||
astring+offset,
|
||||
length-offset)
|
||||
) > 0)
|
||||
) {
|
||||
offset += nwritten;
|
||||
}
|
||||
if (nwritten == -1) {
|
||||
err(EXIT_FAILURE, "uart0_puts: write");
|
||||
}
|
||||
else if ((length > 0) && (nwritten == 0)) {
|
||||
/* XXX: handle properly */
|
||||
errx(EXIT_FAILURE, "uart0_puts: Could not write to stdout. I don't know what to do now.");
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
/* internal */
|
||||
|
||||
void *get_in_addr(struct sockaddr *sa)
|
||||
{
|
||||
if (sa->sa_family == AF_INET) {
|
||||
return &(((struct sockaddr_in*)sa)->sin_addr);
|
||||
}
|
||||
return &(((struct sockaddr_in6*)sa)->sin6_addr);
|
||||
}
|
||||
|
||||
#ifndef UART_TCPPORT
|
||||
#define UART_TCPPORT "4711"
|
||||
#endif
|
||||
int init_tcp_socket(char *tcpport)
|
||||
{
|
||||
struct addrinfo hints, *info, *p;
|
||||
int i, s = -1;
|
||||
if (tcpport == NULL) {
|
||||
tcpport = UART_TCPPORT;
|
||||
}
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
|
||||
if ((i = real_getaddrinfo(NULL, tcpport, &hints, &info)) != 0) {
|
||||
errx(EXIT_FAILURE,
|
||||
"init_uart_socket: getaddrinfo: %s", real_gai_strerror(i));
|
||||
}
|
||||
|
||||
for (p = info; p != NULL; p = p->ai_next) {
|
||||
if ((s = real_socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
|
||||
warn("init_uart_socket: socket");
|
||||
continue;
|
||||
}
|
||||
|
||||
i = 1;
|
||||
if (real_setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(int)) == -1) {
|
||||
err(EXIT_FAILURE, "init_uart_socket: setsockopt");
|
||||
}
|
||||
|
||||
if (real_bind(s, p->ai_addr, p->ai_addrlen) == -1) {
|
||||
real_close(s);
|
||||
warn("init_uart_socket: bind");
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
if (p == NULL) {
|
||||
errx(EXIT_FAILURE, "init_uart_socket: failed to bind\n");
|
||||
}
|
||||
real_freeaddrinfo(info);
|
||||
|
||||
if (real_listen(s, 1) == -1) {
|
||||
err(EXIT_FAILURE, "init_uart_socket: listen");
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
int init_unix_socket(void)
|
||||
{
|
||||
int s;
|
||||
struct sockaddr_un sa;
|
||||
|
||||
if ((s = real_socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
||||
err(EXIT_FAILURE, "init_unix_socket: socket");
|
||||
}
|
||||
|
||||
sa.sun_family = AF_UNIX;
|
||||
|
||||
if (_native_unix_socket_path != NULL) {
|
||||
snprintf(sa.sun_path, sizeof(sa.sun_path), "%s", _native_unix_socket_path);
|
||||
}
|
||||
else {
|
||||
snprintf(sa.sun_path, sizeof(sa.sun_path), "/tmp/riot.tty.%d", _native_pid);
|
||||
}
|
||||
real_unlink(sa.sun_path); /* remove stale socket */
|
||||
if (real_bind(s, (struct sockaddr *)&sa, SUN_LEN(&sa)) == -1) {
|
||||
err(EXIT_FAILURE, "init_unix_socket: bind");
|
||||
}
|
||||
|
||||
if (real_listen(s, 5) == -1) {
|
||||
err(EXIT_FAILURE, "init_unix_socket: listen");
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void handle_uart_in(void)
|
||||
{
|
||||
char buf[42];
|
||||
int nread;
|
||||
|
||||
DEBUG("handle_uart_in\n");
|
||||
|
||||
nread = _native_read(STDIN_FILENO, buf, sizeof(buf));
|
||||
if (nread == -1) {
|
||||
err(EXIT_FAILURE, "handle_uart_in(): read()");
|
||||
}
|
||||
else if (nread == 0) {
|
||||
/* end of file / socket closed */
|
||||
if (_native_uart_conn != 0) {
|
||||
if (_native_null_out_file != -1) {
|
||||
if (real_dup2(_native_null_out_file, STDOUT_FILENO) == -1) {
|
||||
err(EXIT_FAILURE, "handle_uart_in: dup2(STDOUT_FILENO)");
|
||||
}
|
||||
}
|
||||
if (real_dup2(_native_null_in_pipe[0], STDIN_FILENO) == -1) {
|
||||
err(EXIT_FAILURE, "handle_uart_in: dup2(STDIN_FILENO)");
|
||||
}
|
||||
_native_uart_conn = 0;
|
||||
warnx("closed stdio");
|
||||
}
|
||||
else {
|
||||
errx(EXIT_FAILURE, "handle_uart_in: unhandled situation!");
|
||||
}
|
||||
}
|
||||
for (int pos = 0; pos < nread; pos++) {
|
||||
uart0_handle_incoming(buf[pos]);
|
||||
}
|
||||
uart0_notify_thread();
|
||||
|
||||
thread_yield();
|
||||
}
|
||||
|
||||
void handle_uart_sock(void)
|
||||
{
|
||||
int s;
|
||||
socklen_t t;
|
||||
struct sockaddr remote;
|
||||
|
||||
t = sizeof(remote);
|
||||
|
||||
_native_syscall_enter();
|
||||
if ((s = real_accept(_native_uart_sock, &remote, &t)) == -1) {
|
||||
err(EXIT_FAILURE, "handle_uart_sock: accept");
|
||||
}
|
||||
else {
|
||||
warnx("handle_uart_sock: successfully accepted socket");
|
||||
}
|
||||
|
||||
if (real_dup2(s, STDOUT_FILENO) == -1) {
|
||||
err(EXIT_FAILURE, "handle_uart_sock: dup2()");
|
||||
}
|
||||
if (real_dup2(s, STDIN_FILENO) == -1) {
|
||||
err(EXIT_FAILURE, "handle_uart_sock: dup2()");
|
||||
}
|
||||
|
||||
/* play back log from last position */
|
||||
if (_native_replay_enabled) {
|
||||
warnx("handle_uart_sock: replaying buffer");
|
||||
size_t nread;
|
||||
char buf[200];
|
||||
while ((nread = real_fread(buf, 1, sizeof(buf), _native_replay_buffer)) != 0) {
|
||||
int nwritten;
|
||||
int pos = 0;
|
||||
while ((nwritten = real_write(STDOUT_FILENO, &buf[pos], nread)) != -1) {
|
||||
nread -= nwritten;
|
||||
pos += nwritten;
|
||||
if (nread == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (nwritten == -1) {
|
||||
err(EXIT_FAILURE, "handle_uart_sock: write");
|
||||
}
|
||||
}
|
||||
if (real_feof(_native_replay_buffer) != 0) {
|
||||
real_clearerr(_native_replay_buffer);
|
||||
}
|
||||
else if (real_ferror(_native_replay_buffer) != 0) {
|
||||
err(EXIT_FAILURE, "handle_uart_sock(): fread()");
|
||||
}
|
||||
}
|
||||
|
||||
_native_syscall_leave();
|
||||
|
||||
_native_uart_conn = s;
|
||||
}
|
||||
|
||||
#ifdef MODULE_UART0
|
||||
void _native_handle_uart0_input(void)
|
||||
{
|
||||
if (FD_ISSET(STDIN_FILENO, &_native_rfds)) {
|
||||
handle_uart_in();
|
||||
}
|
||||
else if ((_native_uart_sock != -1) && (FD_ISSET(_native_uart_sock, &_native_rfds))) {
|
||||
handle_uart_sock();
|
||||
}
|
||||
else {
|
||||
DEBUG("_native_handle_uart0_input - nothing to do\n");
|
||||
}
|
||||
}
|
||||
|
||||
int _native_set_uart_fds(void)
|
||||
{
|
||||
DEBUG("_native_set_uart_fds\n");
|
||||
FD_SET(STDIN_FILENO, &_native_rfds);
|
||||
if (_native_uart_sock == -1) {
|
||||
return (STDIN_FILENO);
|
||||
}
|
||||
else {
|
||||
FD_SET(_native_uart_sock, &_native_rfds);
|
||||
return ((STDIN_FILENO > _native_uart_sock) ? STDIN_FILENO : _native_uart_sock);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void _native_init_uart0(char *stdiotype, char *ioparam, int replay)
|
||||
{
|
||||
_native_replay_enabled = replay;
|
||||
|
||||
if (_native_replay_enabled) {
|
||||
char stdout_logname[255];
|
||||
snprintf(stdout_logname, sizeof(stdout_logname), "/tmp/riot.stdout.%d", _native_pid);
|
||||
if ((_native_replay_buffer = real_fopen(stdout_logname, "r+")) == NULL) {
|
||||
err(EXIT_FAILURE, "_native_init_uart0: fdopen(_native_null_out_file)");
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp(stdiotype, "tcp") == 0) {
|
||||
_native_uart_sock = init_tcp_socket(ioparam);
|
||||
}
|
||||
else if (strcmp(stdiotype, "unix") == 0) {
|
||||
_native_uart_sock = init_unix_socket();
|
||||
}
|
||||
else if (strcmp(stdiotype, "stdio") == 0) {
|
||||
_native_uart_sock = -1;
|
||||
_native_uart_conn = 1;
|
||||
}
|
||||
else if (strcmp(stdiotype, "null") == 0) {
|
||||
_native_uart_sock = -1;
|
||||
_native_uart_conn = 0;
|
||||
}
|
||||
else {
|
||||
errx(EXIT_FAILURE, "_native_init_uart0: unknown stdio type");
|
||||
}
|
||||
|
||||
puts("RIOT native uart0 initialized.");
|
||||
}
|
@ -13,19 +13,6 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef MODULE_UART0
|
||||
#include <sys/select.h>
|
||||
void _native_handle_uart0_input(void);
|
||||
/**
|
||||
* @brief: initialize uart0
|
||||
*
|
||||
* @param stdiotype: "stdio", "tcp", "udp" io redirection
|
||||
* @param ioparam: a string containing a port number if stdiotype is tcp
|
||||
*/
|
||||
void _native_init_uart0(char *stdiotype, char *ioparam, int replay);
|
||||
int _native_set_uart_fds(void);
|
||||
#endif /* MODULE_UART0 */
|
||||
|
||||
extern int _native_null_out_file;
|
||||
extern int _native_null_in_pipe[2];
|
||||
void board_init(void);
|
||||
|
@ -75,75 +75,6 @@ Usage:
|
||||
|
||||
./bin/native/default.elf -d
|
||||
|
||||
Use UART redirection if you want to use a shell or get stderr/stdout
|
||||
output with/from a daemonized process.
|
||||
|
||||
|
||||
UART Redirection
|
||||
================
|
||||
|
||||
You can redirect the processes' stdin/stdout/stderr by specifying
|
||||
one or more options from below.
|
||||
|
||||
UNIX socket
|
||||
-----------
|
||||
|
||||
To redirect stdio to a UNIX socket run:
|
||||
|
||||
./bin/native/default.elf -u -d
|
||||
RIOT pid: 18663
|
||||
|
||||
Attach this UNIX socket:
|
||||
|
||||
nc -U /tmp/riot.tty.18663
|
||||
|
||||
TCP socket
|
||||
----------
|
||||
To redirect stdio to a TCP socket:
|
||||
|
||||
./bin/native/default.elf -t 4711 -d
|
||||
RIOT pid: 18663
|
||||
|
||||
Attach this TCP socket:
|
||||
|
||||
nc localhost 4711
|
||||
|
||||
Stop the process:
|
||||
|
||||
kill 18663
|
||||
|
||||
File for stderr
|
||||
---------------
|
||||
To redirect stderr to a file:
|
||||
|
||||
./bin/native/default.elf -d -e
|
||||
RIOT pid: 18663
|
||||
|
||||
Read from it:
|
||||
|
||||
tail -f /tmp/riot.stderr.18663
|
||||
|
||||
File for stdout
|
||||
---------------
|
||||
To redirect stdout to a file:
|
||||
|
||||
./bin/native/default.elf -d -o
|
||||
RIOT pid: 18663
|
||||
|
||||
Read from it:
|
||||
|
||||
tail -f /tmp/riot.stdout.18663
|
||||
|
||||
|
||||
Notes
|
||||
-----
|
||||
The stdout redirection only writes to file while no socket connection
|
||||
is established.
|
||||
|
||||
Socket redirection is only available when the UART module has been
|
||||
compiled in.
|
||||
|
||||
|
||||
Compile Time Options
|
||||
====================
|
||||
|
||||
|
@ -50,18 +50,6 @@ extern "C" {
|
||||
#endif /* OS */
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @brief UART0 buffer size definition for compatibility reasons
|
||||
*
|
||||
* TODO: remove once the remodeling of the uart0 driver is done
|
||||
* @{
|
||||
*/
|
||||
#ifdef UART0_BUFSIZE
|
||||
#undef UART0_BUFSIZE
|
||||
#endif
|
||||
#define UART0_BUFSIZE (128)
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @brief Native internal Ethernet protocol number
|
||||
*/
|
||||
|
@ -153,11 +153,6 @@ extern unsigned _native_rng_seed;
|
||||
extern int _native_rng_mode; /**< 0 = /dev/random, 1 = random(3) */
|
||||
extern const char *_native_unix_socket_path;
|
||||
|
||||
#ifdef MODULE_UART0
|
||||
#include <sys/select.h>
|
||||
extern fd_set _native_rfds;
|
||||
#endif
|
||||
|
||||
ssize_t _native_read(int fd, void *buf, size_t count);
|
||||
ssize_t _native_write(int fd, const void *buf, size_t count);
|
||||
|
||||
|
@ -19,10 +19,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#ifdef MODULE_UART0
|
||||
#include <sys/select.h>
|
||||
#include <errno.h>
|
||||
#endif
|
||||
#include <err.h>
|
||||
|
||||
#include "lpm.h"
|
||||
@ -30,9 +26,6 @@
|
||||
#include "cpu.h"
|
||||
|
||||
#include "native_internal.h"
|
||||
#ifdef MODULE_UART0
|
||||
#include "board_internal.h"
|
||||
#endif
|
||||
|
||||
static enum lpm_mode native_lpm;
|
||||
|
||||
@ -45,45 +38,9 @@ void lpm_init(void)
|
||||
|
||||
void _native_lpm_sleep(void)
|
||||
{
|
||||
#ifdef MODULE_UART0
|
||||
int nfds;
|
||||
|
||||
/* set fds */
|
||||
FD_ZERO(&_native_rfds);
|
||||
nfds = _native_set_uart_fds();
|
||||
nfds++;
|
||||
|
||||
_native_in_syscall++; // no switching here
|
||||
nfds = real_select(nfds, &_native_rfds, NULL, NULL, NULL);
|
||||
int _errno = errno;
|
||||
_native_in_syscall--;
|
||||
|
||||
DEBUG("_native_lpm_sleep: returned: %i\n", nfds);
|
||||
|
||||
if (nfds != -1) {
|
||||
/* uart ready, handle input */
|
||||
/* TODO: switch to ISR context */
|
||||
_native_handle_uart0_input();
|
||||
}
|
||||
else if ((_errno == EAGAIN) || (_errno == EWOULDBLOCK)) {
|
||||
/* would block / resource unavailable .. it appears a
|
||||
* contended socket can show this behavior sometimes */
|
||||
_native_in_syscall++;
|
||||
warn("_native_lpm_sleep: select()");
|
||||
_native_in_syscall--;
|
||||
return;
|
||||
}
|
||||
else if (_errno != EINTR) {
|
||||
/* select failed for reason other than signal */
|
||||
err(EXIT_FAILURE, "lpm_set(): select()");
|
||||
}
|
||||
|
||||
/* otherwise select was interrupted because of a signal, continue below */
|
||||
#else
|
||||
_native_in_syscall++; // no switching here
|
||||
real_pause();
|
||||
_native_in_syscall--;
|
||||
#endif
|
||||
|
||||
if (_native_sigpend > 0) {
|
||||
DEBUG("\n\n\t\treturn from syscall, calling native_irq_handler\n\n");
|
||||
|
@ -62,18 +62,11 @@ extern dev_eth_tap_t dev_eth_tap;
|
||||
ucontext_t end_context;
|
||||
char __end_stack[SIGSTKSZ];
|
||||
|
||||
#ifdef MODULE_UART0
|
||||
fd_set _native_rfds;
|
||||
#endif
|
||||
|
||||
int reboot_arch(int mode)
|
||||
{
|
||||
(void) mode;
|
||||
|
||||
printf("\n\n\t\t!! REBOOT !!\n\n");
|
||||
#ifdef MODULE_UART0
|
||||
/* TODO: close stdio fds */
|
||||
#endif
|
||||
|
||||
#ifdef MODULE_DEV_ETH_TAP
|
||||
dev_eth_tap_cleanup(&dev_eth_tap);
|
||||
|
@ -200,10 +200,6 @@ void usage_exit(void)
|
||||
real_printf(" <tap interface>");
|
||||
#endif
|
||||
|
||||
#ifdef MODULE_UART0
|
||||
real_printf(" [-t <port>|-u [path]] [-r]");
|
||||
#endif
|
||||
|
||||
real_printf(" [-i <id>] [-d] [-e|-E] [-o]\n");
|
||||
|
||||
real_printf(" help: %s -h\n", _progname);
|
||||
@ -211,14 +207,6 @@ void usage_exit(void)
|
||||
real_printf("\nOptions:\n\
|
||||
-h help\n");
|
||||
|
||||
#ifdef MODULE_UART0
|
||||
real_printf("\
|
||||
-t <port> redirect stdio to TCP socket listening on <port>\n\
|
||||
-u <path> redirect stdio to UNIX socket (<path> if given,\n\
|
||||
/tmp/riot.tty.PID otherwise)\n\
|
||||
-r replay missed output when (re-)attaching to socket\n\
|
||||
(implies -o)\n");
|
||||
#endif
|
||||
real_printf("\
|
||||
-i <id> specify instance id (set by config module)\n\
|
||||
-s <seed> specify srandom(3) seed (/dev/random is used instead of\n\
|
||||
@ -251,10 +239,6 @@ __attribute__((constructor)) static void startup(int argc, char **argv)
|
||||
char *stderrtype = "stdio";
|
||||
char *stdouttype = "stdio";
|
||||
char *stdiotype = "stdio";
|
||||
#ifdef MODULE_UART0
|
||||
char *ioparam = NULL;
|
||||
int replay = 0;
|
||||
#endif
|
||||
|
||||
#if defined(MODULE_DEV_ETH_TAP)
|
||||
if (
|
||||
@ -313,41 +297,6 @@ __attribute__((constructor)) static void startup(int argc, char **argv)
|
||||
else if (strcmp("-o", arg) == 0) {
|
||||
stdouttype = "file";
|
||||
}
|
||||
#ifdef MODULE_UART0
|
||||
else if (strcmp("-r", arg) == 0) {
|
||||
stdouttype = "file";
|
||||
replay = 1;
|
||||
}
|
||||
else if (strcmp("-t", arg) == 0) {
|
||||
stdiotype = "tcp";
|
||||
if (argp + 1 < argc) {
|
||||
ioparam = argv[++argp];
|
||||
}
|
||||
else {
|
||||
usage_exit();
|
||||
}
|
||||
if (strcmp(stdouttype, "stdio") == 0) {
|
||||
stdouttype = "null";
|
||||
}
|
||||
if (strcmp(stderrtype, "stdio") == 0) {
|
||||
stderrtype = "null";
|
||||
}
|
||||
}
|
||||
else if (strcmp("-u", arg) == 0) {
|
||||
stdiotype = "unix";
|
||||
if (strcmp(stdouttype, "stdio") == 0) {
|
||||
stdouttype = "null";
|
||||
}
|
||||
if (strcmp(stderrtype, "stdio") == 0) {
|
||||
stderrtype = "null";
|
||||
}
|
||||
|
||||
/* parse optional path */
|
||||
if ((argp + 1 < argc) && (argv[argp + 1][0] != '-')) {
|
||||
_native_unix_socket_path = argv[++argp];
|
||||
}
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
usage_exit();
|
||||
}
|
||||
@ -361,10 +310,6 @@ __attribute__((constructor)) static void startup(int argc, char **argv)
|
||||
_native_log_stdout(stdouttype);
|
||||
_native_null_in(stdiotype);
|
||||
|
||||
#ifdef MODULE_UART0
|
||||
_native_init_uart0(stdiotype, ioparam, replay);
|
||||
#endif
|
||||
|
||||
native_cpu_init();
|
||||
native_interrupt_init();
|
||||
#ifdef MODULE_DEV_ETH_TAP
|
||||
|
Loading…
Reference in New Issue
Block a user