diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index c7beabb713..b1b2d046d6 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -53,7 +53,6 @@ PSEUDOMODULES += lora PSEUDOMODULES += mpu_stack_guard PSEUDOMODULES += nanocoap_% PSEUDOMODULES += netdev_default -PSEUDOMODULES += netif PSEUDOMODULES += netstats PSEUDOMODULES += netstats_l2 PSEUDOMODULES += netstats_ipv6 diff --git a/sys/Makefile b/sys/Makefile index 3cb3056212..91a09e7b1f 100644 --- a/sys/Makefile +++ b/sys/Makefile @@ -157,6 +157,9 @@ endif ifneq (,$(filter suit%,$(USEMODULE))) DIRS += suit endif +ifneq (,$(filter netif,$(USEMODULE))) + DIRS += net/netif +endif DIRS += $(dir $(wildcard $(addsuffix /Makefile, $(USEMODULE)))) diff --git a/sys/include/net/netif.h b/sys/include/net/netif.h index e0fedf0a65..f4593548df 100644 --- a/sys/include/net/netif.h +++ b/sys/include/net/netif.h @@ -13,11 +13,13 @@ * @brief Common network interface API * * This allows access to network interfaces regardless of the network stack - * implementation. @anchor NETIF_INVALID The network stack must provide + * implementation. The network stack must provide * - * - Both a definition for the type `netif_t` and the - * value `NETIF_INVALID` of type `netif_t` in a file `netif_types.h` and - * - implementation of all the functions defined in @ref net/netif.h + * - A definition for @p netif_get_name + * - A definition for @p netif_get_opt + * - A definition for @p netif_set_opt + * + * The network stack should also register each interface via @p netif_register. * * @{ * @@ -26,13 +28,15 @@ * * @author Martine Lenders * @author Kaspar Schleiser + * @author José Ignacio Alamos */ #ifndef NET_NETIF_H #define NET_NETIF_H -#include "net/netopt.h" +#include -#include "netif_types.h" +#include "list.h" +#include "net/netopt.h" #ifdef __cplusplus extern "C" { @@ -45,20 +49,27 @@ extern "C" { #define NETIF_NAMELENMAX (8U) #endif +/** + * @brief Network interface descriptor. + * + * @note All network interfaces should inherit from this structure. + */ +typedef struct { + list_node_t node; /**< Pointer to the next interface */ +} netif_t; + /** * @brief Iterator for the interfaces * - * Returns interface after @p last. To start use `last == NETIF_INVALID`. + * Returns interface after @p last. To start use `last == NULL`. * - * @param[in] last The previous interface. Usen `NETIF_INVALID` to start + * @param[in] last The previous interface. Use `NULL` to start * iteration. * - * @note Supposed to be implemented by the networking module - * * @return next network interface. - * @return @ref NETIF_INVALID, if there is no interface after @p last + * @return NULL, if there is no interface after @p last */ -netif_t netif_iter(netif_t last); +netif_t *netif_iter(netif_t *last); /** * @brief Gets name of an interface @@ -74,25 +85,23 @@ netif_t netif_iter(netif_t last); * hold @ref NETIF_NAMELENMAX bytes. * * @return length of @p name on success - * @return 0, if @p netif was not a valid interface. */ -int netif_get_name(netif_t netif, char *name); + +int netif_get_name(netif_t *netif, char *name); /** * @brief Gets interface by name * * @pre `name != NULL` * - * @note Supposed to be implemented by the networking module. - * * @param[in] name The name of an interface as a zero-terminated. Must not be * `NULL`. * * @return The identifier of the interface on success. - * @return @ref NETIF_INVALID if no interface is named @p name. + * @return NULL if no interface is named @p name. */ -netif_t netif_get_by_name(const char *name); +netif_t *netif_get_by_name(const char *name); /** * @brief Gets option from an interface @@ -108,7 +117,7 @@ netif_t netif_get_by_name(const char *name); * @return Number of bytes written to @p value. * @return `< 0` on error, 0 on success. */ -int netif_get_opt(netif_t netif, netopt_t opt, uint16_t context, +int netif_get_opt(netif_t *netif, netopt_t opt, uint16_t context, void *value, size_t max_len); /** @@ -125,9 +134,22 @@ int netif_get_opt(netif_t netif, netopt_t opt, uint16_t context, * @return Number of bytes used from @p value. * @return `< 0` on error, 0 on success. */ -int netif_set_opt(netif_t netif, netopt_t opt, uint16_t context, +int netif_set_opt(netif_t *netif, netopt_t opt, uint16_t context, void *value, size_t value_len); + +/** + * @brief Registers a network interface in the global interface list. + * + * @note This functions should be called when initializing an interface. + * + * @param[in] netif Interface to be registered + * + * @return 0 on success + * @return -EINVAL if @p netif is NULL. + */ +int netif_register(netif_t *netif); + #ifdef __cplusplus } #endif diff --git a/sys/net/netif/Makefile b/sys/net/netif/Makefile new file mode 100644 index 0000000000..805279bbe8 --- /dev/null +++ b/sys/net/netif/Makefile @@ -0,0 +1,3 @@ +MODULE = netif + +include $(RIOTBASE)/Makefile.base diff --git a/sys/net/netif/netif.c b/sys/net/netif/netif.c new file mode 100644 index 0000000000..d7062d81ac --- /dev/null +++ b/sys/net/netif/netif.c @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2019 HAW Hamburg + * + * 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. + */ + +/** + * @{ + * + * @file + * @author Jose I. Alamos + */ + +#include + +#include "errno.h" +#include "net/netif.h" +#include "utlist.h" + +static list_node_t netif_list; + +int netif_register(netif_t *netif) +{ + if(netif == NULL) { + return -EINVAL; + } + + list_add(&netif_list, &netif->node); + return 0; +} + +netif_t *netif_iter(netif_t *last) +{ + if (last == NULL) { + return (netif_t *)netif_list.next; + } + + return (netif_t *)last->node.next; +} + +netif_t *netif_get_by_name(const char *name) +{ + assert(name); + list_node_t *node = netif_list.next; + + char tmp[NETIF_NAMELENMAX]; + + while(node) { + netif_get_name((netif_t *)node, tmp); + if(strncmp(name, tmp, NETIF_NAMELENMAX) == 0) { + return (netif_t *)node; + } + node = node->next; + } + + return NULL; +} +/** @} */