From 6ec3f54b3cb0da17b7a60c26342866c1b03b5e5f Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 26 Oct 2023 15:55:06 +0200 Subject: [PATCH 1/3] drivers/slipdev: improve default UART selection --- drivers/slipdev/include/slipdev_params.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/slipdev/include/slipdev_params.h b/drivers/slipdev/include/slipdev_params.h index d9e44cc2f672..c1b8ab155d59 100644 --- a/drivers/slipdev/include/slipdev_params.h +++ b/drivers/slipdev/include/slipdev_params.h @@ -34,9 +34,13 @@ extern "C" { */ #ifndef SLIPDEV_PARAM_UART # ifndef MODULE_SLIPDEV_STDIO -# define SLIPDEV_PARAM_UART (UART_DEV(0)) +# ifdef MODULE_USBUS_CDC_ACM +# define SLIPDEV_PARAM_UART UART_DEV(0) +# else +# define SLIPDEV_PARAM_UART UART_DEV(1) +# endif # else /* MODULE_SLIPDEV_STDIO */ -# define SLIPDEV_PARAM_UART (STDIO_UART_DEV) +# define SLIPDEV_PARAM_UART STDIO_UART_DEV # endif /* MODULE_SLIPDEV_STDIO */ #endif /* SLIPDEV_PARAM_UART */ #ifndef SLIPDEV_PARAM_BAUDRATE From 32d17f5313c0e9df04e9aa71e0df637bb7b2fb91 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 26 Oct 2023 15:12:12 +0200 Subject: [PATCH 2/3] pkg/lwip: add support for slipdev --- pkg/lwip/contrib/netdev/lwip_netdev.c | 82 ++++++++++++++++++++++++++ pkg/lwip/init_devs/auto_init_slipdev.c | 44 ++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 pkg/lwip/init_devs/auto_init_slipdev.c diff --git a/pkg/lwip/contrib/netdev/lwip_netdev.c b/pkg/lwip/contrib/netdev/lwip_netdev.c index ebdce8b72fc2..3f3510a891f0 100644 --- a/pkg/lwip/contrib/netdev/lwip_netdev.c +++ b/pkg/lwip/contrib/netdev/lwip_netdev.c @@ -62,6 +62,15 @@ static err_t _eth_link_output(struct netif *netif, struct pbuf *p); #ifdef MODULE_LWIP_SIXLOWPAN static err_t _ieee802154_link_output(struct netif *netif, struct pbuf *p); #endif +#ifdef MODULE_SLIPDEV +static err_t _slip_link_output(struct netif *netif, struct pbuf *p); +#if LWIP_IPV4 +static err_t slip_output4(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr); +#endif +#if LWIP_IPV6 +static err_t slip_output6(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr); +#endif +#endif static void _event_cb(netdev_t *dev, netdev_event_t event); static void *_event_loop(void *arg); @@ -183,6 +192,47 @@ err_t lwip_netdev_init(struct netif *netif) #endif /* LWIP_IPV6_AUTOCONFIG */ break; } +#endif +#ifdef MODULE_SLIPDEV + case NETDEV_TYPE_SLIP: + netif->name[0] = 'S'; + netif->name[1] = 'L'; + + /* TODO: get from driver (currently not in netdev_eth) */ + netif->mtu = ETHERNET_DATA_LEN; + netif->linkoutput = _slip_link_output; +#if LWIP_IPV4 + netif->output = slip_output4; +#endif +#if LWIP_IPV6 + netif->output_ip6 = slip_output6; + + if (IS_USED(MODULE_SLIPDEV_L2ADDR)) { + netif->hwaddr_len = (u8_t)netdev->driver->get(netdev, NETOPT_ADDRESS_LONG, + netif->hwaddr, + sizeof(netif->hwaddr)); + if (netif->hwaddr_len > sizeof(netif->hwaddr)) { + res = ERR_IF; + goto free; + } + + /* netif_create_ip6_linklocal_address() does weird byte-swapping + * with full IIDs, so let's do it ourselves */ + ip6_addr_t *addr = ip_2_ip6(&(netif->ip6_addr[0])); + /* addr->addr is a uint32_t array */ + if (l2util_ipv6_iid_from_addr(dev_type, + netif->hwaddr, netif->hwaddr_len, + (eui64_t *)&addr->addr[2]) < 0) { + res = ERR_IF; + goto free; + } + ipv6_addr_set_link_local_prefix((ipv6_addr_t *)&addr->addr[0]); + ip6_addr_assign_zone(addr, IP6_UNICAST, netif); + /* Consider address valid. */ + netif->ip6_addr_state[0] = IP6_ADDR_PREFERRED; + } +#endif /* LWIP_IPV6 */ + break; #endif default: res = ERR_IF; @@ -257,6 +307,38 @@ static err_t _ieee802154_link_output(struct netif *netif, struct pbuf *p) } #endif +#ifdef MODULE_SLIPDEV +#if LWIP_IPV4 +static err_t slip_output4(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr) +{ + (void)ipaddr; + return netif->linkoutput(netif, q); +} +#endif +#if LWIP_IPV6 +static err_t slip_output6(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr) +{ + (void)ip6addr; + return netif->linkoutput(netif, q); +} +#endif + +static err_t _slip_link_output(struct netif *netif, struct pbuf *p) +{ + LWIP_ASSERT("p->next == NULL", p->next == NULL); + netdev_t *netdev = netif->state; + iolist_t pkt = { + .iol_base = p->payload, + .iol_len = p->len, + }; + + lwip_netif_dev_acquire(netif); + err_t res = (netdev->driver->send(netdev, &pkt) >= 0) ? ERR_OK : ERR_BUF; + lwip_netif_dev_release(netif); + return res; +} +#endif + static struct pbuf *_get_recv_pkt(netdev_t *dev) { lwip_netif_t *compat_netif = dev->context; diff --git a/pkg/lwip/init_devs/auto_init_slipdev.c b/pkg/lwip/init_devs/auto_init_slipdev.c new file mode 100644 index 000000000000..6eb65e64b33d --- /dev/null +++ b/pkg/lwip/init_devs/auto_init_slipdev.c @@ -0,0 +1,44 @@ +/* + * 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_auto_init_lwip_netif + * @{ + * + * @file + * @brief Auto initialization for the SLIP module + * + * @author Benjamin Valentin + */ + +#include "slipdev.h" +#include "slipdev_params.h" + +#include "lwip_init_devs.h" + +#define ENABLE_DEBUG 0 +#include "debug.h" + +#define NETIF_SLIPDEV_NUMOF ARRAY_SIZE(slipdev_params) + +static lwip_netif_t netif[NETIF_SLIPDEV_NUMOF]; +static slipdev_t slipdev_devs[NETIF_SLIPDEV_NUMOF]; + +static void auto_init_slipdev(void) +{ + for (unsigned i = 0; i < NETIF_SLIPDEV_NUMOF; i++) { + slipdev_setup(&slipdev_devs[i], &slipdev_params[i], i); + if (lwip_add_ethernet(&netif[i], &slipdev_devs[i].netdev) == NULL) { + DEBUG("Could not add slipdev device\n"); + return; + } + } +} + +LWIP_INIT_ETH_NETIF(auto_init_slipdev); +/** @} */ From a32dbbfe0d6366c059916dbbb59c38f95fd623e4 Mon Sep 17 00:00:00 2001 From: krzysztof-cabaj Date: Thu, 26 Oct 2023 18:08:33 +0200 Subject: [PATCH 3/3] tests/drivers/at: fix device table overflow --- tests/drivers/at/main.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/drivers/at/main.c b/tests/drivers/at/main.c index f524c256dc46..4313700423b6 100644 --- a/tests/drivers/at/main.c +++ b/tests/drivers/at/main.c @@ -47,6 +47,11 @@ static int init(int argc, char **argv) uint8_t uart = atoi(argv[1]); uint32_t baudrate = atoi(argv[2]); + if (uart >= UART_NUMOF) { + printf("Wrong UART device number - should be in range 0-%d.\n", UART_NUMOF - 1); + return 1; + } + int res = at_dev_init(&at_dev, UART_DEV(uart), baudrate, buf, sizeof(buf)); /* check the UART initialization return value and respond as needed */