netif: introduce descriptor based netif

This commit is contained in:
Jose Alamos 2019-07-22 13:32:39 +02:00
parent 9946e24bc9
commit 77a7aed6e6
5 changed files with 108 additions and 21 deletions

View File

@ -53,7 +53,6 @@ PSEUDOMODULES += lora
PSEUDOMODULES += mpu_stack_guard PSEUDOMODULES += mpu_stack_guard
PSEUDOMODULES += nanocoap_% PSEUDOMODULES += nanocoap_%
PSEUDOMODULES += netdev_default PSEUDOMODULES += netdev_default
PSEUDOMODULES += netif
PSEUDOMODULES += netstats PSEUDOMODULES += netstats
PSEUDOMODULES += netstats_l2 PSEUDOMODULES += netstats_l2
PSEUDOMODULES += netstats_ipv6 PSEUDOMODULES += netstats_ipv6

View File

@ -157,6 +157,9 @@ endif
ifneq (,$(filter suit%,$(USEMODULE))) ifneq (,$(filter suit%,$(USEMODULE)))
DIRS += suit DIRS += suit
endif endif
ifneq (,$(filter netif,$(USEMODULE)))
DIRS += net/netif
endif
DIRS += $(dir $(wildcard $(addsuffix /Makefile, $(USEMODULE)))) DIRS += $(dir $(wildcard $(addsuffix /Makefile, $(USEMODULE))))

View File

@ -13,11 +13,13 @@
* @brief Common network interface API * @brief Common network interface API
* *
* This allows access to network interfaces regardless of the network stack * 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 * - A definition for @p netif_get_name
* value `NETIF_INVALID` of type `netif_t` in a file `netif_types.h` and * - A definition for @p netif_get_opt
* - implementation of all the functions defined in @ref net/netif.h * - 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 <m.lenders@fu-berlin.de> * @author Martine Lenders <m.lenders@fu-berlin.de>
* @author Kaspar Schleiser <kaspar@schleiser.de> * @author Kaspar Schleiser <kaspar@schleiser.de>
* @author José Ignacio Alamos <jose.alamos@haw-hamburg.de>
*/ */
#ifndef NET_NETIF_H #ifndef NET_NETIF_H
#define NET_NETIF_H #define NET_NETIF_H
#include "net/netopt.h" #include <stdint.h>
#include "netif_types.h" #include "list.h"
#include "net/netopt.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -45,20 +49,27 @@ extern "C" {
#define NETIF_NAMELENMAX (8U) #define NETIF_NAMELENMAX (8U)
#endif #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 * @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. * iteration.
* *
* @note Supposed to be implemented by the networking module
*
* @return next network interface. * @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 * @brief Gets name of an interface
@ -74,25 +85,23 @@ netif_t netif_iter(netif_t last);
* hold @ref NETIF_NAMELENMAX bytes. * hold @ref NETIF_NAMELENMAX bytes.
* *
* @return length of @p name on success * @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 * @brief Gets interface by name
* *
* @pre `name != NULL` * @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 * @param[in] name The name of an interface as a zero-terminated. Must not be
* `NULL`. * `NULL`.
* *
* @return The identifier of the interface on success. * @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 * @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 Number of bytes written to @p value.
* @return `< 0` on error, 0 on success. * @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); 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 Number of bytes used from @p value.
* @return `< 0` on error, 0 on success. * @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); 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 #ifdef __cplusplus
} }
#endif #endif

3
sys/net/netif/Makefile Normal file
View File

@ -0,0 +1,3 @@
MODULE = netif
include $(RIOTBASE)/Makefile.base

60
sys/net/netif/netif.c Normal file
View File

@ -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 <jose.alamos@haw-hamburg.de>
*/
#include <string.h>
#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;
}
/** @} */