Skip to content

Commit

Permalink
sys/stdio_udp: add stdio over UDP
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Jan 2, 2023
1 parent 821acbe commit 8c8d42f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
5 changes: 5 additions & 0 deletions makefiles/stdio.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ STDIO_MODULES = \
stdio_rtt \
stdio_semihosting \
stdio_uart \
stdio_udp \
stdio_telnet \
stdio_tinyusb_cdc_acm \
#
Expand Down Expand Up @@ -74,6 +75,10 @@ ifneq (,$(filter stdio_telnet,$(USEMODULE)))
USEMODULE += telnet
endif

ifneq (,$(filter stdio_udp,$(USEMODULE)))
USEMODULE += sock_udp
endif

# enable stdout buffering for modules that benefit from sending out buffers in larger chunks
ifneq (,$(filter picolibc,$(USEMODULE)))
ifneq (,$(filter stdio_cdc_acm stdio_ethos slipdev_stdio stdio_semihosting,$(USEMODULE)))
Expand Down
1 change: 1 addition & 0 deletions sys/stdio_udp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
99 changes: 99 additions & 0 deletions sys/stdio_udp/stdio_udp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (C) 2023 ML!PA Consulting GmbH
*
* 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 over UDP implementation
*
* This file implements STDIO via a UDP that can be used with e.g. netcat:
*
* nc -u fe80::7837:fcff:fe7d:1aaf%tapbr0 2323
*
* @warning This is entirely unsecured, only use this for debugging in
* an isolated network!
*
* @author Benjamin Valentin <[email protected]>
*
* @}
*/

#include <errno.h>

#include "net/sock/udp.h"
#if IS_USED(MODULE_VFS)
#include "vfs.h"
#endif

#ifndef CONFIG_STDIO_UDP_PORT
#define CONFIG_STDIO_UDP_PORT 2323
#endif

#ifndef CONFIG_STDIO_UDP_RX_BUF_LEN
#define CONFIG_STDIO_UDP_RX_BUF_LEN 64
#endif

#define MIN(a, b) ((a) > (b) ? (b) : (a))

static sock_udp_t sock;
static sock_udp_ep_t remote;

void stdio_init(void)
{
const sock_udp_ep_t local = {
.family = AF_INET6,
.netif = SOCK_ADDR_ANY_NETIF,
.port = CONFIG_STDIO_UDP_PORT,
};

sock_udp_create(&sock, &local, NULL, 0);

#if IS_USED(MODULE_VFS)
vfs_bind_stdio();
#endif
}

ssize_t stdio_read(void* buffer, size_t count)
{
static uint8_t rx_buf[CONFIG_STDIO_UDP_RX_BUF_LEN];
static uint8_t *pos;
static size_t left;

/* shell will only read one byte at a time, so we buffer the input */
if (left == 0) {
int res = sock_udp_recv(&sock, rx_buf, sizeof(rx_buf),
SOCK_NO_TIMEOUT, &remote);
if (res > 0) {
left = res;
pos = rx_buf;
} else {
return res;
}
}

count = MIN(left, count);
uint8_t *dst = buffer;
for (unsigned i = 0; i < count; ++i) {
*dst++ = *pos++;
}
left -= count;
return count;
}

ssize_t stdio_write(const void* buffer, size_t len)
{
if (remote.port == 0) {
return -ENOTCONN;
}
if (len == 0) {
return 0;
}
return sock_udp_send(&sock, buffer, len, &remote);
}

0 comments on commit 8c8d42f

Please sign in to comment.