From ea545d2028a08f424753be70d9897499d90ebc04 Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Wed, 11 Oct 2023 22:50:51 +0200 Subject: [PATCH] sys/shell/gnrc_txtsnd: Move to separate module --- makefiles/pseudomodules.inc.mk | 1 + sys/shell/Makefile.dep | 3 ++ sys/shell/cmds/gnrc_netif.c | 62 ----------------------- sys/shell/cmds/gnrc_txtsnd.c | 89 ++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 62 deletions(-) create mode 100644 sys/shell/cmds/gnrc_txtsnd.c diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index 695fe7ea4e13..986a1c11ca4e 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -479,6 +479,7 @@ PSEUDOMODULES += shell_cmd_gnrc_pktbuf PSEUDOMODULES += shell_cmd_gnrc_rpl PSEUDOMODULES += shell_cmd_gnrc_sixlowpan_ctx PSEUDOMODULES += shell_cmd_gnrc_sixlowpan_frag_stats +PSEUDOMODULES += shell_cmd_gnrc_txtsnd PSEUDOMODULES += shell_cmd_gnrc_udp PSEUDOMODULES += shell_cmd_heap PSEUDOMODULES += shell_cmd_i2c_scan diff --git a/sys/shell/Makefile.dep b/sys/shell/Makefile.dep index 862ada7b9301..4c3b51854037 100644 --- a/sys/shell/Makefile.dep +++ b/sys/shell/Makefile.dep @@ -51,6 +51,9 @@ ifneq (,$(filter shell_cmds_default,$(USEMODULE))) USEMODULE += shell_cmd_gnrc_netif_lora endif endif + ifneq (,$(filter gnrc_txtsnd,$(USEMODULE))) + USEMODULE += shell_cmd_gnrc_txtsnd + endif ifneq (,$(filter gnrc_netif_lorawan,$(USEMODULE))) USEMODULE += shell_cmd_gnrc_netif_lorawan endif diff --git a/sys/shell/cmds/gnrc_netif.c b/sys/shell/cmds/gnrc_netif.c index 938480e89f7d..c2af1315ef07 100644 --- a/sys/shell/cmds/gnrc_netif.c +++ b/sys/shell/cmds/gnrc_netif.c @@ -33,7 +33,6 @@ #include "net/loramac.h" #include "net/netif.h" #include "shell.h" -#include "container.h" #ifdef MODULE_NETSTATS #include "net/netstats.h" @@ -1793,67 +1792,6 @@ static int _netif_del(netif_t *iface, char *addr_str) } /* shell commands */ -#ifdef MODULE_GNRC_TXTSND -static int _gnrc_netif_send(int argc, char **argv) -{ - netif_t *iface; - uint8_t addr[GNRC_NETIF_L2ADDR_MAXLEN]; - size_t addr_len; - gnrc_pktsnip_t *pkt, *hdr; - gnrc_netif_hdr_t *nethdr; - uint8_t flags = 0x00; - - if (argc < 4) { - printf("usage: %s [|bcast] \n", argv[0]); - return 1; - } - - iface = netif_get_by_name(argv[1]); - if (!iface) { - printf("error: invalid interface given\n"); - return 1; - } - - /* parse address */ - addr_len = gnrc_netif_addr_from_str(argv[2], addr); - - if (addr_len == 0) { - if (strcmp(argv[2], "bcast") == 0) { - flags |= GNRC_NETIF_HDR_FLAGS_BROADCAST; - } - else { - printf("error: invalid address given\n"); - return 1; - } - } - - /* put packet together */ - pkt = gnrc_pktbuf_add(NULL, argv[3], strlen(argv[3]), GNRC_NETTYPE_UNDEF); - if (pkt == NULL) { - printf("error: packet buffer full\n"); - return 1; - } - hdr = gnrc_netif_hdr_build(NULL, 0, addr, addr_len); - if (hdr == NULL) { - printf("error: packet buffer full\n"); - gnrc_pktbuf_release(pkt); - return 1; - } - pkt = gnrc_pkt_prepend(pkt, hdr); - nethdr = (gnrc_netif_hdr_t *)hdr->data; - nethdr->flags = flags; - /* and send it */ - if (gnrc_netif_send(container_of(iface, gnrc_netif_t, netif), pkt) < 1) { - printf("error: unable to send\n"); - gnrc_pktbuf_release(pkt); - return 1; - } - - return 0; -} - -SHELL_COMMAND(txtsnd, "Sends a custom string as is over the link layer", _gnrc_netif_send); -#endif /* TODO: updated tests/net/gnrc_dhcpv6_client to no longer abuse this shell command * and add static qualifier */ diff --git a/sys/shell/cmds/gnrc_txtsnd.c b/sys/shell/cmds/gnrc_txtsnd.c new file mode 100644 index 000000000000..b5014f6a8aac --- /dev/null +++ b/sys/shell/cmds/gnrc_txtsnd.c @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2017 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_shell_commands + * @{ + * + * @file + * @brief Shell command for sending raw text on network + * + * @author Martine Lenders + * @author Hauke Petersen + * @author Oliver Hahm + */ + +#include +#include + +#include "net/gnrc.h" +#include "net/gnrc.h" +#include "net/gnrc/netif/hdr.h" +#include "net/ipv6/addr.h" +#include "shell.h" +#include "container.h" + +static int _gnrc_netif_send(int argc, char **argv) +{ + netif_t *iface; + uint8_t addr[GNRC_NETIF_L2ADDR_MAXLEN]; + size_t addr_len; + gnrc_pktsnip_t *pkt, *hdr; + gnrc_netif_hdr_t *nethdr; + uint8_t flags = 0x00; + + if (argc < 4) { + printf("usage: %s [|bcast] \n", argv[0]); + return 1; + } + + iface = netif_get_by_name(argv[1]); + if (!iface) { + printf("error: invalid interface given\n"); + return 1; + } + + /* parse address */ + addr_len = gnrc_netif_addr_from_str(argv[2], addr); + + if (addr_len == 0) { + if (strcmp(argv[2], "bcast") == 0) { + flags |= GNRC_NETIF_HDR_FLAGS_BROADCAST; + } + else { + printf("error: invalid address given\n"); + return 1; + } + } + + /* put packet together */ + pkt = gnrc_pktbuf_add(NULL, argv[3], strlen(argv[3]), GNRC_NETTYPE_UNDEF); + if (pkt == NULL) { + printf("error: packet buffer full\n"); + return 1; + } + hdr = gnrc_netif_hdr_build(NULL, 0, addr, addr_len); + if (hdr == NULL) { + printf("error: packet buffer full\n"); + gnrc_pktbuf_release(pkt); + return 1; + } + pkt = gnrc_pkt_prepend(pkt, hdr); + nethdr = (gnrc_netif_hdr_t *)hdr->data; + nethdr->flags = flags; + /* and send it */ + if (gnrc_netif_send(container_of(iface, gnrc_netif_t, netif), pkt) < 1) { + printf("error: unable to send\n"); + gnrc_pktbuf_release(pkt); + return 1; + } + + return 0; +} + +SHELL_COMMAND(txtsnd, "Sends a custom string as is over the link layer", _gnrc_netif_send);