From 7ceb3f0a68923aaef102d7c08a1a4fedbb300e5b Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 14 Feb 2020 00:15:52 +0100 Subject: [PATCH] netdev: add netdev_register() to keep track if netdev IDs It is desireable to have a way to identify network devices. This should be independent from the type of netdev, so a common identifier is needed. Base this on the driver ID and the index in the configuration struct. This way we achive unique IDs that stay consistent for any firmware flashed on a board. --- Makefile.dep | 5 +++++ drivers/include/net/netdev.h | 37 ++++++++++++++++++++++++++++++++++ makefiles/pseudomodules.inc.mk | 1 + 3 files changed, 43 insertions(+) diff --git a/Makefile.dep b/Makefile.dep index 873f3ab98b..c2dcc9f659 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -69,6 +69,11 @@ ifneq (,$(filter gnrc_mac,$(USEMODULE))) endif endif +# Don't register netdevs if there is only a single one of them +ifeq (,$(filter gnrc_netif_single,$(USEMODULE))) + USEMODULE += netdev_register +endif + ifneq (,$(filter gnrc_gomach,$(USEMODULE))) USEMODULE += gnrc_netif USEMODULE += gnrc_nettype_gomach diff --git a/drivers/include/net/netdev.h b/drivers/include/net/netdev.h index 8ea89c5274..97d68196f5 100644 --- a/drivers/include/net/netdev.h +++ b/drivers/include/net/netdev.h @@ -199,6 +199,7 @@ extern "C" { #include "iolist.h" #include "net/netopt.h" +#include "kernel_defines.h" #ifdef MODULE_L2FILTER #include "net/l2filter.h" @@ -271,6 +272,18 @@ typedef struct netdev netdev_t; */ typedef void (*netdev_event_cb_t)(netdev_t *dev, netdev_event_t event); +/** + * @brief Driver types for netdev. + * @{ + */ +typedef enum { + NETDEV_ANY = 0, /**< Will match any device type */ + NETDEV_AT86RF215, + NETDEV_AT86RF2XX, + /* add more if needed */ +} netdev_type_t; +/** @} */ + /** * @brief Structure to hold driver state * @@ -290,8 +303,32 @@ struct netdev { #ifdef MODULE_L2FILTER l2filter_t filter[CONFIG_L2FILTER_LISTSIZE]; /**< link layer address filters */ #endif +#ifdef MODULE_NETDEV_REGISTER + netdev_type_t type; /**< driver type used for netdev */ + uint8_t index; /**< instance number of the device */ +#endif }; +/** + * @brief Register a device with netdev. + * Must by called by the driver's setup function. + * + * @param[out] dev the new netdev + * @param[in] type the driver used for the netdev + * @param[in] index the index in the config struct + */ +static inline void netdev_register(struct netdev *dev, netdev_type_t type, uint8_t index) +{ +#ifdef MODULE_NETDEV_REGISTER + dev->type = type; + dev->index = index; +#else + (void) dev; + (void) type; + (void) index; +#endif +} + /** * @brief Structure to hold driver interface -> function mapping * diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index 003f06c3b3..d2b3405bae 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -73,6 +73,7 @@ PSEUDOMODULES += mpu_noexec_ram PSEUDOMODULES += nanocoap_% PSEUDOMODULES += netdev_default PSEUDOMODULES += netdev_ieee802154_% +PSEUDOMODULES += netdev_register PSEUDOMODULES += netstats PSEUDOMODULES += netstats_l2 PSEUDOMODULES += netstats_ipv6