[add] a new function to manage internet check.

Signed-off-by: xiangxistu <xiangxistu@foxmail.com>
This commit is contained in:
xiangxistu 2020-12-31 11:53:30 +08:00
parent 53f5d0fa89
commit 5db412b138
2 changed files with 27 additions and 4 deletions

View File

@ -10,6 +10,12 @@ config RT_USING_SAL
if RT_USING_SAL if RT_USING_SAL
config SAL_INTERNET_CHECK
bool "Enable the ability that check internet status"
default y
help
The ability that check internet status is provided by RT-Thread.
if RT_USING_LWIP || AT_USING_SOCKET if RT_USING_LWIP || AT_USING_SOCKET
menu "protocol stack implement" menu "protocol stack implement"

View File

@ -21,7 +21,9 @@
#include <sal.h> #include <sal.h>
#include <netdev.h> #include <netdev.h>
#ifdef SAL_INTERNET_CHECK
#include <ipc/workqueue.h> #include <ipc/workqueue.h>
#endif
/* check system workqueue stack size */ /* check system workqueue stack size */
#if RT_SYSTEM_WORKQUEUE_STACKSIZE < 1536 #if RT_SYSTEM_WORKQUEUE_STACKSIZE < 1536
@ -45,7 +47,7 @@ struct sal_socket_table
struct sal_netdev_res_table struct sal_netdev_res_table
{ {
struct addrinfo *res; struct addrinfo *res;
struct netdev *netdev; struct netdev *netdev;
}; };
#ifdef SAL_USING_TLS #ifdef SAL_USING_TLS
@ -131,7 +133,7 @@ int sal_init(void)
/*init the dev_res table */ /*init the dev_res table */
rt_memset(sal_dev_res_tbl, 0, sizeof(sal_dev_res_tbl)); rt_memset(sal_dev_res_tbl, 0, sizeof(sal_dev_res_tbl));
/* create sal socket lock */ /* create sal socket lock */
rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_FIFO); rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_FIFO);
@ -142,6 +144,7 @@ int sal_init(void)
} }
INIT_COMPONENT_EXPORT(sal_init); INIT_COMPONENT_EXPORT(sal_init);
#ifdef SAL_INTERNET_CHECK
/* check SAL network interface device internet status */ /* check SAL network interface device internet status */
static void check_netdev_internet_up_work(struct rt_work *work, void *work_data) static void check_netdev_internet_up_work(struct rt_work *work, void *work_data)
{ {
@ -271,6 +274,7 @@ __exit:
skt_ops->closesocket(sockfd); skt_ops->closesocket(sockfd);
} }
} }
#endif /* SAL_INTERNET_CHECK */
/** /**
* This function will check SAL network interface device internet status. * This function will check SAL network interface device internet status.
@ -279,10 +283,12 @@ __exit:
*/ */
int sal_check_netdev_internet_up(struct netdev *netdev) int sal_check_netdev_internet_up(struct netdev *netdev)
{ {
RT_ASSERT(netdev);
#ifdef SAL_INTERNET_CHECK
/* workqueue for network connect */ /* workqueue for network connect */
struct rt_delayed_work *net_work = RT_NULL; struct rt_delayed_work *net_work = RT_NULL;
RT_ASSERT(netdev);
net_work = (struct rt_delayed_work *)rt_calloc(1, sizeof(struct rt_delayed_work)); net_work = (struct rt_delayed_work *)rt_calloc(1, sizeof(struct rt_delayed_work));
if (net_work == RT_NULL) if (net_work == RT_NULL)
@ -293,7 +299,18 @@ int sal_check_netdev_internet_up(struct netdev *netdev)
rt_delayed_work_init(net_work, check_netdev_internet_up_work, (void *)netdev); rt_delayed_work_init(net_work, check_netdev_internet_up_work, (void *)netdev);
rt_work_submit(&(net_work->work), RT_TICK_PER_SECOND); rt_work_submit(&(net_work->work), RT_TICK_PER_SECOND);
#else
if(netdev_is_link_up(netdev))
{
netdev->flags |= NETDEV_FLAG_INTERNET_UP;
}
else
{
netdev->flags &= ~NETDEV_FLAG_INTERNET_UP;
}
LOG_D("You have shut down internet check function, internet status is subject to link status.");
#endif /* SAL_INTERNET_CHECK */
return 0; return 0;
} }
@ -1138,7 +1155,7 @@ int sal_getaddrinfo(const char *nodename,
} }
RT_ASSERT((i < SAL_SOCKETS_NUM)); RT_ASSERT((i < SAL_SOCKETS_NUM));
} }
return ret; return ret;