Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell/cmds: add udptty command #19742

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ PSEUDOMODULES += shell_cmd_sht1x
PSEUDOMODULES += shell_cmd_sntp
PSEUDOMODULES += shell_cmd_suit
PSEUDOMODULES += shell_cmd_sys
PSEUDOMODULES += shell_cmd_udptty
PSEUDOMODULES += shell_cmd_vfs
PSEUDOMODULES += shell_cmds_default
## @addtogroup sys_shell_commands
Expand Down
4 changes: 4 additions & 0 deletions sys/shell/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ endif
ifneq (,$(filter shell_cmd_suit,$(USEMODULE)))
USEMODULE += suit_transport_worker
endif
ifneq (,$(filter shell_cmd_udptty,$(USEMODULE)))
USEMODULE += sock_async
USEMODULE += sock_udp
endif
ifneq (,$(filter shell_cmd_vfs,$(USEMODULE)))
USEMODULE += vfs
USEMODULE += tiny_strerror
Expand Down
119 changes: 119 additions & 0 deletions sys/shell/cmds/udptty.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* 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_shell_commands
* @{
*
* @file
*
* @author Benjamin Valentin <[email protected]>
*/

#include <stdio.h>
#include <stdlib.h>
#include "shell.h"
#include "stdio_base.h"
#include "net/sock/async.h"
#include "net/sock/udp.h"
#include "net/utils.h"
#include "ztimer.h"

#ifndef CONFIG_STDIO_UDP_PORT
#define CONFIG_STDIO_UDP_PORT 2323
#endif

#ifndef EOT
#define EOT 0x4
#endif

extern int readline(char *buf, size_t size);

static void _sock_cb(sock_udp_t *sock, sock_async_flags_t flags, void *arg)
{
(void)arg;
if ((flags & SOCK_ASYNC_MSG_RECV) == 0) {
return;
}

void *data, *ctx = NULL;

int res;
while ((res = sock_udp_recv_buf(sock, &data, &ctx, 0, NULL)) > 0) {
stdio_write(data, res);
}
}

static int _udptty_cmd(int argc, char **argv)
{
if (argc < 2) {
goto usage;
}

netif_t *netif = NULL;
sock_udp_ep_t remote = SOCK_IPV6_EP_ANY;
sock_udp_t sock;
int res;

const sock_udp_ep_t local = {
.family = AF_INET6,
.netif = SOCK_ADDR_ANY_NETIF,
};

if (netutils_get_ipv6((ipv6_addr_t *)&remote.addr, &netif, argv[1])) {
goto usage;
}

if (netif) {
remote.netif = netif_get_id(netif);
}

if (argc > 2) {
remote.port = atoi(argv[2]);
} else {
remote.port = CONFIG_STDIO_UDP_PORT;
}

res = sock_udp_create(&sock, &local, &remote, 0);
if (res) {
printf("can't create socket: %d\n", res);
return res;
}

printf("connected to %s, press Ctrl+D to exit\n", argv[1]);
sock_udp_set_cb(&sock, _sock_cb, NULL);

do {
char buf[64];
res = readline(buf, sizeof(buf));

if (res > 0) {
buf[res] = '\n';
sock_udp_send(&sock, buf, res + 1, NULL);
}

#ifdef CPU_NATIVE
/* readline() on native blocks all other threads - see #19002 */
ztimer_sleep(ZTIMER_MSEC, 100);
#endif
} while (res != EOF);
benpicco marked this conversation as resolved.
Show resolved Hide resolved

/* send disconnect */
const char eot = EOT;
sock_udp_send(&sock, &eot, sizeof(eot), NULL);
sock_udp_close(&sock);

return 0;

usage:
printf("usage: %s <server> <port>\n", argv[0]);
return -1;
}

SHELL_COMMAND(udptty, "UDP remote shell", _udptty_cmd);
/** @} */
Loading