-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include $(RIOTBASE)/Makefile.base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |