diff --git a/pkg/lwip/Makefile.dep b/pkg/lwip/Makefile.dep index 2179cfbbc6..4e2d4ad80d 100644 --- a/pkg/lwip/Makefile.dep +++ b/pkg/lwip/Makefile.dep @@ -63,6 +63,7 @@ ifneq (,$(filter lwip_%,$(USEMODULE))) USEMODULE += lwip_contrib USEMODULE += lwip_core USEMODULE += lwip_netif + USEMODULE += lwip_netif_init_devs USEMODULE += netdev ifeq (,$(filter lwip_ipv4 lwip_ipv6,$(USEMODULE))) USEMODULE += lwip_ipv4 diff --git a/pkg/lwip/Makefile.include b/pkg/lwip/Makefile.include index caf6629cb9..616c6a109c 100644 --- a/pkg/lwip/Makefile.include +++ b/pkg/lwip/Makefile.include @@ -29,6 +29,9 @@ endif ifneq (,$(filter lwip_netdev,$(USEMODULE))) DIRS += $(RIOTBASE)/pkg/lwip/contrib/netdev endif +ifneq (,$(filter lwip_netif_init_devs,$(USEMODULE))) + DIRS += $(RIOTBASE)/pkg/lwip/init_devs +endif ifneq (,$(filter lwip_sock,$(USEMODULE))) ifneq (,$(filter lwip_ipv6,$(USEMODULE))) CFLAGS += -DSOCK_HAS_IPV6 diff --git a/pkg/lwip/contrib/lwip.c b/pkg/lwip/contrib/lwip.c index 8940aaf706..1c272f1ade 100644 --- a/pkg/lwip/contrib/lwip.c +++ b/pkg/lwip/contrib/lwip.c @@ -11,6 +11,7 @@ * * @file * @author Martine Lenders + * @author Erik Ekman */ #include "kernel_defines.h" @@ -21,11 +22,6 @@ #include "lwip/netifapi.h" #include "netif/lowpan6.h" -#ifdef MODULE_NETDEV_TAP -#include "netdev_tap.h" -#include "netdev_tap_params.h" -#endif - #ifdef MODULE_AT86RF2XX #include "at86rf2xx.h" #include "at86rf2xx_params.h" @@ -76,15 +72,12 @@ #endif #include "lwip.h" +#include "lwip_init_devs.h" #define ENABLE_DEBUG 0 #include "debug.h" -#ifdef MODULE_NETDEV_TAP -#define LWIP_NETIF_NUMOF (NETDEV_TAP_MAX) -#endif - -#ifdef MODULE_AT86RF2XX /* is mutual exclusive with above ifdef */ +#ifdef MODULE_AT86RF2XX #define LWIP_NETIF_NUMOF ARRAY_SIZE(at86rf2xx_params) #endif @@ -129,10 +122,6 @@ static struct netif netif[LWIP_NETIF_NUMOF]; #endif -#ifdef MODULE_NETDEV_TAP -static netdev_tap_t netdev_taps[LWIP_NETIF_NUMOF]; -#endif - #ifdef MODULE_AT86RF2XX static at86rf2xx_t at86rf2xx_devs[LWIP_NETIF_NUMOF]; #endif @@ -179,18 +168,10 @@ static netdev_ieee802154_submac_t nrf802154_netdev; void lwip_bootstrap(void) { + lwip_netif_init_devs(); /* TODO: do for every eligible netdev */ #ifdef LWIP_NETIF_NUMOF -#ifdef MODULE_NETDEV_TAP - for (unsigned i = 0; i < LWIP_NETIF_NUMOF; i++) { - netdev_tap_setup(&netdev_taps[i], &netdev_tap_params[i]); - if (netif_add_noaddr(&netif[i], &netdev_taps[i].netdev, lwip_netdev_init, - tcpip_input) == NULL) { - DEBUG("Could not add netdev_tap device\n"); - return; - } - } -#elif defined(MODULE_MRF24J40) +#ifdef MODULE_MRF24J40 for (unsigned i = 0; i < LWIP_NETIF_NUMOF; i++) { mrf24j40_setup(&mrf24j40_devs[i], &mrf24j40_params[i], i); if (netif_add_noaddr(&netif[i], &mrf24j40_devs[i].netdev.netdev, lwip_netdev_init, diff --git a/pkg/lwip/include/lwip_init_devs.h b/pkg/lwip/include/lwip_init_devs.h new file mode 100644 index 0000000000..3a0f504c69 --- /dev/null +++ b/pkg/lwip/include/lwip_init_devs.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) Google LLC + * + * 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 Helpers for simplified network setup + * + * @author Erik Ekman + */ + +#ifndef LWIP_INIT_DEVS_H +#define LWIP_INIT_DEVS_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "lwip/netif.h" +#include "net/netdev.h" + +void lwip_netif_init_devs(void); + +/** + * @brief Adds an Ethernet netif using the supplied netdev. + * + * @param netif pointer to the interface to be added + * @param state pointer to the netdev for the interface + * + * The netif will be set up using the `lwip_netdev_init` helper. + */ +struct netif *lwip_add_ethernet(struct netif *netif, netdev_t *state); + +#ifdef __cplusplus +} +#endif + +#endif /* LWIP_INIT_DEVS_H */ +/** @} */ diff --git a/pkg/lwip/init_devs/Makefile b/pkg/lwip/init_devs/Makefile new file mode 100644 index 0000000000..38f77f1dbd --- /dev/null +++ b/pkg/lwip/init_devs/Makefile @@ -0,0 +1,3 @@ +MODULE = lwip_netif_init_devs + +include $(RIOTMAKE)/auto_init.inc.mk diff --git a/pkg/lwip/init_devs/auto_init_netdev_tap.c b/pkg/lwip/init_devs/auto_init_netdev_tap.c new file mode 100644 index 0000000000..eb50e17d2c --- /dev/null +++ b/pkg/lwip/init_devs/auto_init_netdev_tap.c @@ -0,0 +1,43 @@ +/* + * Copyright (C) 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_auto_init_lwip_netif + * @{ + * + * @file + * @brief Auto initialization for TAP network interfaces + * + * @author Martine Lenders + * @author Erik Ekman + */ + +#include "netdev_tap.h" +#include "netdev_tap_params.h" + +#include "lwip_init_devs.h" + +#define ENABLE_DEBUG 0 +#include "debug.h" + +#define NETIF_TAP_NUMOF (NETDEV_TAP_MAX) + +static struct netif netif[NETIF_TAP_NUMOF]; +static netdev_tap_t netdev_taps[NETIF_TAP_NUMOF]; + +void auto_init_netdev_tap(void) +{ + for (unsigned i = 0; i < NETIF_TAP_NUMOF; i++) { + netdev_tap_setup(&netdev_taps[i], &netdev_tap_params[i]); + if (lwip_add_ethernet(&netif[i], &netdev_taps[i].netdev) == NULL) { + DEBUG("Could not add netdev_tap device\n"); + return; + } + } +} +/** @} */ diff --git a/pkg/lwip/init_devs/init.c b/pkg/lwip/init_devs/init.c new file mode 100644 index 0000000000..68e1a65b31 --- /dev/null +++ b/pkg/lwip/init_devs/init.c @@ -0,0 +1,38 @@ +/* + * Copyright (C) Google LLC + * + * 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 Initializes the supported network interfaces for lwIP + * @author Erik Ekman + */ + +#include "kernel_defines.h" +#include "lwip_init_devs.h" +#include "lwip/netif/netdev.h" + +/** + * @brief Initializes network interfaces + */ +void lwip_netif_init_devs(void) +{ + if (IS_USED(MODULE_NETDEV_TAP)) { + extern void auto_init_netdev_tap(void); + auto_init_netdev_tap(); + } +} + +struct netif *lwip_add_ethernet(struct netif *netif, netdev_t *state) +{ + return netif_add_noaddr(netif, state, lwip_netdev_init, tcpip_input); +} + +/**@}*/ diff --git a/sys/include/auto_init.h b/sys/include/auto_init.h index 7edc6f7251..9eb437d41a 100644 --- a/sys/include/auto_init.h +++ b/sys/include/auto_init.h @@ -78,6 +78,14 @@ * @see @ref net_gnrc_netif, @ref sys_auto_init */ +/** + * @defgroup sys_auto_init_lwip_netif lwIP netif drivers auto-initialization + * @ingroup sys_auto_init + * @brief Provides auto-initialization of network device drivers for lwIP + * + * @see @ref pkg_lwip, @ref sys_auto_init + */ + /** * @defgroup sys_auto_init_multimedia Multimedia driver auto-initialization * @ingroup sys_auto_init