diff --git a/components/net/Kconfig b/components/net/Kconfig index a719f7be6f..30472e57b1 100644 --- a/components/net/Kconfig +++ b/components/net/Kconfig @@ -73,6 +73,9 @@ config RT_USING_NETDEV bool "Enable netstat features" default y + config NETDEV_USING_AUTO_DEFAULT + bool "Enable default netdev automatic change features" + default y endif endmenu diff --git a/components/net/netdev/src/netdev.c b/components/net/netdev/src/netdev.c index bfefc2d1de..fbc08325b4 100644 --- a/components/net/netdev/src/netdev.c +++ b/components/net/netdev/src/netdev.c @@ -269,7 +269,7 @@ struct netdev *netdev_get_by_family(int family) { netdev = rt_slist_entry(node, struct netdev, list); pf = (struct sal_proto_family *) netdev->sal_user_data; - if (pf && pf->skt_ops && pf->family == family && netdev_is_up(netdev) && netdev_is_link_up(netdev)) + if (pf && pf->skt_ops && pf->family == family && netdev_is_up(netdev)) { rt_hw_interrupt_enable(level); return netdev; @@ -671,6 +671,23 @@ void netdev_low_level_set_dns_server(struct netdev *netdev, uint8_t dns_num, con } } +#ifdef NETDEV_USING_AUTO_DEFAULT +/* Change to the first link_up network interface device automatically */ +static void netdev_auto_change_default(struct netdev *netdev) +{ + struct netdev *new_netdev = RT_NULL; + + if (rt_memcmp(netdev, netdev_default, sizeof(struct netdev)) == 0) + { + new_netdev = netdev_get_first_link_up(); + if (new_netdev) + { + netdev_set_default(new_netdev); + } + } +} +#endif /* NETDEV_USING_AUTO_DEFAULT */ + /** * This function will set network interface device status. * @NOTE it can only be called in the network interface device driver. @@ -689,6 +706,11 @@ void netdev_low_level_set_status(struct netdev *netdev, rt_bool_t is_up) else { netdev->flags &= ~NETDEV_FLAG_UP; + +#ifdef NETDEV_USING_AUTO_DEFAULT + /* change to the first link_up network interface device automatically */ + netdev_auto_change_default(netdev); +#endif /* NETDEV_USING_AUTO_DEFAULT */ } /* execute network interface device status change callback function */ @@ -728,6 +750,11 @@ void netdev_low_level_set_link_status(struct netdev *netdev, rt_bool_t is_up) /* set network interface device flags to internet down */ netdev->flags &= ~NETDEV_FLAG_INTERNET_UP; + +#ifdef NETDEV_USING_AUTO_DEFAULT + /* change to the first link_up network interface device automatically */ + netdev_auto_change_default(netdev); +#endif /* NETDEV_USING_AUTO_DEFAULT */ } /* execute link status change callback function */ diff --git a/components/net/sal_socket/src/sal_socket.c b/components/net/sal_socket/src/sal_socket.c index f6fdeb61f7..a63f5cafc5 100644 --- a/components/net/sal_socket/src/sal_socket.c +++ b/components/net/sal_socket/src/sal_socket.c @@ -387,7 +387,7 @@ static int socket_init(int family, int type, int protocol, struct sal_socket **r sock->type = type; sock->protocol = protocol; - if (netdv_def && netdev_is_up(netdv_def) && netdev_is_link_up(netdv_def)) + if (netdv_def && netdev_is_up(netdv_def)) { /* check default network interface device protocol family */ pf = (struct sal_proto_family *) netdv_def->sal_user_data;