Skip to content

Commit

Permalink
Merge #20022 #20025
Browse files Browse the repository at this point in the history
20022: pkg/lwip: add support for slipdev r=benpicco a=benpicco



20025: tests/drivers/at: fix device table overflow r=benpicco a=krzysztof-cabaj

### Contribution description

This PR fix device table overflow in `tests/driver/at`, which could lead to device crash.

### Testing procedure

PR was tested on two nucleo boards with 2 and 3 UARTs (nucleo-l476rg and nucleo-l496zg).
Flash `tests/driver/at` with and without this PR.

Output with this PR:

```
> main(): This is RIOT! (Version: 2022.07-devel-5083-g2b9e8-tests-drivers-at)
AT command test app
> init 5 9600

Wrong UART device number - should by in range 0-2.
>
```

Output without this PR:

```
> main(): This is RIOT! (Version: 2022.07-devel-5083-g2b9e8)
AT command test app
> init 5 9600

8001afd
*** RIOT kernel panic:
FAILED ASSERTION.

*** halted.


Context before hardfault:
   r0: 0x0000000a
   r1: 0x00000000
   . . . 
```

### Issues/PRs references

None

Co-authored-by: Benjamin Valentin <[email protected]>
Co-authored-by: krzysztof-cabaj <[email protected]>
  • Loading branch information
3 people authored Oct 27, 2023
3 parents 47f0446 + 32d17f5 + a32dbbf commit 60d6dec
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 2 deletions.
8 changes: 6 additions & 2 deletions drivers/slipdev/include/slipdev_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 82 additions & 0 deletions pkg/lwip/contrib/netdev/lwip_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
44 changes: 44 additions & 0 deletions pkg/lwip/init_devs/auto_init_slipdev.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/

#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);
/** @} */
5 changes: 5 additions & 0 deletions tests/drivers/at/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 60d6dec

Please sign in to comment.