From aac5e9da4cc14f88771b3cf34d24e44d7e545ea5 Mon Sep 17 00:00:00 2001 From: longtengmcu Date: Sun, 22 Nov 2020 12:36:07 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1=E3=80=81web=20socket=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=BB=8E=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=9B=9E?= =?UTF-8?q?=E6=9D=A5=E6=97=B6=EF=BC=8C=E6=9C=8D=E5=8A=A1=E5=99=A8=E4=BC=9A?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E8=BF=9E=E6=8E=A5=EF=BC=8C=E6=AD=A4=E6=97=B6?= =?UTF-8?q?AT=20socket=E5=9C=A8=E8=AF=BB=E5=8F=96=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E6=8D=AE=E6=97=B6=E4=B8=8D=E5=BA=94=E8=AF=A5?= =?UTF-8?q?=E5=88=A4=E6=96=ADsocket=E4=B8=BA=E8=BF=9E=E6=8E=A5=E7=9A=84?= =?UTF-8?q?=E7=8A=B6=E6=80=81=202=E3=80=81sal=20socket=E5=9C=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=9A=E7=BD=91=E5=8D=A1=E6=97=B6=EF=BC=8Csal=5Fget?= =?UTF-8?q?addrinfo=E4=BB=8EA=E7=BD=91=E5=8D=A1=E8=8E=B7=E5=8F=96=E7=9A=84?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E4=BF=A1=E6=81=AF=EF=BC=8C=E5=9C=A8sal=5Ffre?= =?UTF-8?q?eaddrinfo=E9=87=8A=E6=94=BE=E6=97=B6=E5=BF=85=E9=A1=BB=E7=94=B1?= =?UTF-8?q?A=E7=BD=91=E5=8D=A1=E8=BF=9B=E8=A1=8C=E9=87=8A=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/net/at/at_socket/at_socket.c | 25 +++----- components/net/sal_socket/src/sal_socket.c | 74 +++++++++++++++++----- 2 files changed, 68 insertions(+), 31 deletions(-) diff --git a/components/net/at/at_socket/at_socket.c b/components/net/at/at_socket/at_socket.c index 666b0f509e..ce8559f129 100644 --- a/components/net/at/at_socket/at_socket.c +++ b/components/net/at/at_socket/at_socket.c @@ -821,24 +821,17 @@ int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *f goto __exit; } else - { - if (sock->state == AT_SOCKET_CONNECT) + { + + /* get receive buffer to receiver ring buffer */ + rt_mutex_take(sock->recv_lock, RT_WAITING_FOREVER); + recv_len = at_recvpkt_get(&(sock->recvpkt_list), (char *) mem, len); + rt_mutex_release(sock->recv_lock); + if (recv_len > 0) { - /* get receive buffer to receiver ring buffer */ - rt_mutex_take(sock->recv_lock, RT_WAITING_FOREVER); - recv_len = at_recvpkt_get(&(sock->recvpkt_list), (char *) mem, len); - rt_mutex_release(sock->recv_lock); - if (recv_len > 0) - { - break; - } - } - else - { - LOG_D("received data exit, current socket (%d) is closed by remote.", socket); - result = 0; - goto __exit; + break; } + } } diff --git a/components/net/sal_socket/src/sal_socket.c b/components/net/sal_socket/src/sal_socket.c index ec0519df02..bf88cfa751 100644 --- a/components/net/sal_socket/src/sal_socket.c +++ b/components/net/sal_socket/src/sal_socket.c @@ -41,6 +41,13 @@ struct sal_socket_table struct sal_socket **sockets; }; +/* record the netdev and res table*/ +struct sal_netdev_res_table +{ + struct addrinfo *res; + struct netdev *netdev; +}; + #ifdef SAL_USING_TLS /* The global TLS protocol options */ static struct sal_proto_tls *proto_tls; @@ -50,6 +57,7 @@ static struct sal_proto_tls *proto_tls; static struct sal_socket_table socket_table; static struct rt_mutex sal_core_lock; static rt_bool_t init_ok = RT_FALSE; +static struct sal_netdev_res_table sal_dev_res_tbl[SAL_SOCKETS_NUM]; #define IS_SOCKET_PROTO_TLS(sock) (((sock)->protocol == PROTOCOL_TLS) || \ ((sock)->protocol == PROTOCOL_DTLS)) @@ -90,6 +98,11 @@ do { ((pf) = (struct sal_proto_family *) (netdev)->sal_user_data) != RT_NULL && \ (pf)->netdb_ops->ops) \ +#define SAL_NETDBOPS_VALID(netdev, pf, ops) \ + ((netdev) && \ + ((pf) = (struct sal_proto_family *) (netdev)->sal_user_data) != RT_NULL && \ + (pf)->netdb_ops->ops) \ + /** * SAL (Socket Abstraction Layer) initialize. * @@ -116,6 +129,9 @@ int sal_init(void) return -1; } + /*init the dev_res table */ + rt_memset(sal_dev_res_tbl, 0, sizeof(sal_dev_res_tbl)); + /* create sal socket lock */ rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_FIFO); @@ -1086,10 +1102,12 @@ int sal_getaddrinfo(const char *nodename, { struct netdev *netdev = netdev_default; struct sal_proto_family *pf; + int ret = 0; + rt_uint32_t i = 0; if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo)) { - return pf->netdb_ops->getaddrinfo(nodename, servname, hints, res); + ret = pf->netdb_ops->getaddrinfo(nodename, servname, hints, res); } else { @@ -1097,30 +1115,56 @@ int sal_getaddrinfo(const char *nodename, netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP); if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo)) { - return pf->netdb_ops->getaddrinfo(nodename, servname, hints, res); + ret = pf->netdb_ops->getaddrinfo(nodename, servname, hints, res); + } + else + { + ret = -1; } } - return -1; + if(ret == RT_EOK) + { + /*record the netdev and res*/ + for(i = 0; i < SAL_SOCKETS_NUM; i++) + { + if(sal_dev_res_tbl[i].res == RT_NULL) + { + sal_dev_res_tbl[i].res = *res; + sal_dev_res_tbl[i].netdev = netdev; + break; + } + } + + RT_ASSERT((i < SAL_SOCKETS_NUM)); + + } + + return ret; } void sal_freeaddrinfo(struct addrinfo *ai) { - struct netdev *netdev = netdev_default; - struct sal_proto_family *pf; + struct netdev *netdev = RT_NULL; + struct sal_proto_family *pf = RT_NULL; + rt_uint32_t i = 0; - if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, freeaddrinfo)) + /*when use the multi netdev, it must free the ai use the getaddrinfo netdev */ + for(i = 0; i < SAL_SOCKETS_NUM; i++) + { + if(sal_dev_res_tbl[i].res == ai) + { + netdev = sal_dev_res_tbl[i].netdev; + sal_dev_res_tbl[i].res = RT_NULL; + sal_dev_res_tbl[i].netdev = RT_NULL; + break; + } + } + RT_ASSERT((i < SAL_SOCKETS_NUM)); + + if (SAL_NETDBOPS_VALID(netdev, pf, freeaddrinfo)) { pf->netdb_ops->freeaddrinfo(ai); } - else - { - /* get the first network interface device with up status */ - netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP); - if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, freeaddrinfo)) - { - pf->netdb_ops->freeaddrinfo(ai); - } - } } From 962acc3313d611a886b764135db01572e0206c4d Mon Sep 17 00:00:00 2001 From: longtengmcu Date: Sun, 22 Nov 2020 20:05:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E6=94=B9=E5=AE=8F?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E7=9A=84=E6=8B=BC=E5=86=99=E9=94=99=E8=AF=AF?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9=E6=89=93=E5=8D=B0IMEI=E5=8F=B7?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E6=89=93=E5=8D=B0=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/net/netdev/src/netdev.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/components/net/netdev/src/netdev.c b/components/net/netdev/src/netdev.c index aa43d8d3db..646d2dfdd1 100644 --- a/components/net/netdev/src/netdev.c +++ b/components/net/netdev/src/netdev.c @@ -856,7 +856,7 @@ void netdev_low_level_set_dhcp_status(struct netdev *netdev, rt_bool_t is_enable static void netdev_list_if(void) { #define NETDEV_IFCONFIG_MAC_MAX_LEN 6 -#define NETDEV_IFCONFIG_IEMI_MAX_LEN 8 +#define NETDEV_IFCONFIG_IMEI_MAX_LEN 8 rt_ubase_t index; rt_slist_t *node = RT_NULL; @@ -894,9 +894,15 @@ static void netdev_list_if(void) { /* two numbers are displayed at one time*/ if (netdev->hwaddr[index] < 10 && index != netdev->hwaddr_len - 1) - rt_kprintf("0"); - - rt_kprintf("%d", netdev->hwaddr[index]); + { + rt_kprintf("%02d", netdev->hwaddr[index]); + } + else + { + rt_kprintf("%d", netdev->hwaddr[index]); + } + + } }