net: if: add default selection of first up interface

Add the default selection of the first interface which is up.

Signed-off-by: Benedikt Schmidt <benedikt.schmidt@embedded-solutions.at>
This commit is contained in:
Benedikt Schmidt 2022-03-08 11:43:59 +01:00 committed by Anas Nashif
commit ccd7ae2445
3 changed files with 25 additions and 0 deletions

View file

@ -874,6 +874,14 @@ struct net_if *net_if_get_default(void);
*/ */
struct net_if *net_if_get_first_by_type(const struct net_l2 *l2); struct net_if *net_if_get_first_by_type(const struct net_l2 *l2);
/**
* @brief Get the first network interface which is up.
*
* @return First network interface which is up or NULL if all
* interfaces are down.
*/
struct net_if *net_if_get_first_up(void);
#if defined(CONFIG_NET_L2_IEEE802154) #if defined(CONFIG_NET_L2_IEEE802154)
/** /**
* @brief Get the first IEEE 802.15.4 network interface. * @brief Get the first IEEE 802.15.4 network interface.

View file

@ -717,6 +717,9 @@ choice
config NET_DEFAULT_IF_FIRST config NET_DEFAULT_IF_FIRST
bool "First available interface" bool "First available interface"
config NET_DEFAULT_IF_UP
bool "First interface which is up"
config NET_DEFAULT_IF_ETHERNET config NET_DEFAULT_IF_ETHERNET
bool "Ethernet" bool "Ethernet"
depends on NET_L2_ETHERNET depends on NET_L2_ETHERNET

View file

@ -581,6 +581,9 @@ struct net_if *net_if_get_default(void)
#if defined(CONFIG_NET_DEFAULT_IF_PPP) #if defined(CONFIG_NET_DEFAULT_IF_PPP)
iface = net_if_get_first_by_type(&NET_L2_GET_NAME(PPP)); iface = net_if_get_first_by_type(&NET_L2_GET_NAME(PPP));
#endif #endif
#if defined(CONFIG_NET_DEFAULT_IF_UP)
iface = net_if_get_first_up();
#endif
return iface ? iface : _net_if_list_start; return iface ? iface : _net_if_list_start;
} }
@ -601,6 +604,17 @@ struct net_if *net_if_get_first_by_type(const struct net_l2 *l2)
return NULL; return NULL;
} }
struct net_if *net_if_get_first_up(void)
{
STRUCT_SECTION_FOREACH(net_if, iface) {
if (net_if_flag_is_set(iface, NET_IF_UP)) {
return iface;
}
}
return NULL;
}
static enum net_l2_flags l2_flags_get(struct net_if *iface) static enum net_l2_flags l2_flags_get(struct net_if *iface)
{ {
enum net_l2_flags flags = 0; enum net_l2_flags flags = 0;