diff --git a/components/net/Kconfig b/components/net/Kconfig index 97e97ca4b7..30f25cda94 100644 --- a/components/net/Kconfig +++ b/components/net/Kconfig @@ -5,7 +5,6 @@ menu "Socket abstraction layer" config RT_USING_SAL bool "Enable socket abstraction layer" default n - select RT_USING_DFS if RT_USING_SAL @@ -28,14 +27,24 @@ config RT_USING_SAL config SAL_USING_POSIX bool "Enable BSD socket operated by file system API" + default y if RT_USING_POSIX + default n + select RT_USING_DFS select RT_USING_LIBC select RT_USING_POSIX - default n help Let BSD socket operated by file system API, such as read/write and involveed in select/poll POSIX APIs. + + if !SAL_USING_POSIX + + config SAL_SOCKETS_NUM + int "the maximum number of sockets" + default 16 + + endif config SAL_PROTO_FAMILIES_NUM - int "the number of protocol family" + int "the maximum number of protocol families" default 4 endif diff --git a/components/net/at/at_socket/at_socket.c b/components/net/at/at_socket/at_socket.c index a95c7aec53..f754ad52a7 100644 --- a/components/net/at/at_socket/at_socket.c +++ b/components/net/at/at_socket/at_socket.c @@ -46,7 +46,7 @@ ((unsigned char *)&addr)[2], \ ((unsigned char *)&addr)[3] -#ifdef AT_DEVICE_NOT_SELECTED +#if !defined(AT_DEVICE_SOCKETS_NUM) || defined(AT_DEVICE_NOT_SELECTED) #error The AT socket device is not selected, please select it through the env menuconfig. #endif @@ -398,7 +398,7 @@ int at_closesocket(int socket) if (last_state == AT_SOCKET_CONNECT) { - if (at_dev_ops->close(socket) != 0) + if (at_dev_ops->at_closesocket(socket) != 0) { LOG_E("AT socket (%d) closesocket failed!", socket); } @@ -424,7 +424,7 @@ int at_shutdown(int socket, int how) if (sock->state == AT_SOCKET_CONNECT) { - if (at_dev_ops->close(socket) != 0) + if (at_dev_ops->at_closesocket(socket) != 0) { LOG_E("AT socket (%d) shutdown failed!", socket); } @@ -535,7 +535,7 @@ int at_connect(int socket, const struct sockaddr *name, socklen_t namelen) socketaddr_to_ipaddr_port(name, &remote_addr, &remote_port); ipaddr_to_ipstr(name, ipstr); - if (at_dev_ops->connect(socket, ipstr, remote_port, sock->type, RT_TRUE) < 0) + if (at_dev_ops->at_connect(socket, ipstr, remote_port, sock->type, RT_TRUE) < 0) { LOG_E("AT socket(%d) connect failed!", socket); result = -1; @@ -545,8 +545,8 @@ int at_connect(int socket, const struct sockaddr *name, socklen_t namelen) sock->state = AT_SOCKET_CONNECT; /* set AT socket receive data callback function */ - at_dev_ops->set_event_cb(AT_SOCKET_EVT_RECV, at_recv_notice_cb); - at_dev_ops->set_event_cb(AT_SOCKET_EVT_CLOSED, at_closed_notice_cb); + at_dev_ops->at_set_event_cb(AT_SOCKET_EVT_RECV, at_recv_notice_cb); + at_dev_ops->at_set_event_cb(AT_SOCKET_EVT_CLOSED, at_closed_notice_cb); __exit: @@ -593,7 +593,7 @@ int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *f socketaddr_to_ipaddr_port(from, &remote_addr, &remote_port); ipaddr_to_ipstr(from, ipstr); - if (at_dev_ops->connect(socket, ipstr, remote_port, sock->type, RT_TRUE) < 0) + if (at_dev_ops->at_connect(socket, ipstr, remote_port, sock->type, RT_TRUE) < 0) { LOG_E("AT socket UDP connect failed!"); result = -1; @@ -719,7 +719,7 @@ int at_sendto(int socket, const void *data, size_t size, int flags, const struct goto __exit; } - if ((len = at_dev_ops->send(sock->socket, (const char *) data, size, sock->type)) < 0) + if ((len = at_dev_ops->at_send(sock->socket, (const char *) data, size, sock->type)) < 0) { result = -1; goto __exit; @@ -736,7 +736,7 @@ int at_sendto(int socket, const void *data, size_t size, int flags, const struct socketaddr_to_ipaddr_port(to, &remote_addr, &remote_port); ipaddr_to_ipstr(to, ipstr); - if (at_dev_ops->connect(socket, ipstr, remote_port, sock->type, RT_TRUE) < 0) + if (at_dev_ops->at_connect(socket, ipstr, remote_port, sock->type, RT_TRUE) < 0) { LOG_E("AT socket (%d) UDP connect failed!", socket); result = -1; @@ -745,7 +745,7 @@ int at_sendto(int socket, const void *data, size_t size, int flags, const struct sock->state = AT_SOCKET_CONNECT; } - if ((len = at_dev_ops->send(sock->socket, (char *) data, size, sock->type)) < 0) + if ((len = at_dev_ops->at_send(sock->socket, (char *) data, size, sock->type)) < 0) { result = -1; goto __exit; @@ -940,7 +940,7 @@ struct hostent *at_gethostbyname(const char *name) if (idx < strlen(name)) { - if (at_dev_ops->domain_resolve(name, ipstr) < 0) + if (at_dev_ops->at_domain_resolve(name, ipstr) < 0) { LOG_E("AT domain (%s) resolve error!", name); return RT_NULL; @@ -1040,7 +1040,7 @@ int at_getaddrinfo(const char *nodename, const char *servname, if(idx < strlen(nodename)) { - if (at_dev_ops->domain_resolve((char *) nodename, ip_str) != 0) + if (at_dev_ops->at_domain_resolve((char *) nodename, ip_str) != 0) { return EAI_FAIL; } @@ -1129,10 +1129,10 @@ void at_freeaddrinfo(struct addrinfo *ai) void at_scoket_device_register(const struct at_device_ops *ops) { RT_ASSERT(ops); - RT_ASSERT(ops->connect); - RT_ASSERT(ops->close); - RT_ASSERT(ops->send); - RT_ASSERT(ops->domain_resolve); - RT_ASSERT(ops->set_event_cb); + RT_ASSERT(ops->at_connect); + RT_ASSERT(ops->at_closesocket); + RT_ASSERT(ops->at_send); + RT_ASSERT(ops->at_domain_resolve); + RT_ASSERT(ops->at_set_event_cb); at_dev_ops = (struct at_device_ops *) ops; } diff --git a/components/net/at/at_socket/at_socket.h b/components/net/at/at_socket/at_socket.h index 7d9ce91f39..f6e2a69d5b 100644 --- a/components/net/at/at_socket/at_socket.h +++ b/components/net/at/at_socket/at_socket.h @@ -77,11 +77,11 @@ typedef void (* at_socket_callback)(struct at_socket *conn, int event, uint16_t /* AT device socket options function */ struct at_device_ops { - int (*connect)(int socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client); - int (*close)(int socket); - int (*send)(int socket, const char *buff, size_t bfsz, enum at_socket_type type); - int (*domain_resolve)(const char *name, char ip[16]); - void (*set_event_cb)(at_socket_evt_t event, at_evt_cb_t cb); + int (*at_connect)(int socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client); + int (*at_closesocket)(int socket); + int (*at_send)(int socket, const char *buff, size_t bfsz, enum at_socket_type type); + int (*at_domain_resolve)(const char *name, char ip[16]); + void (*at_set_event_cb)(at_socket_evt_t event, at_evt_cb_t cb); }; /* AT receive package list structure */ diff --git a/components/net/lwip-2.0.2/SConscript b/components/net/lwip-2.0.2/SConscript index 0fc63f4b26..d99556af90 100644 --- a/components/net/lwip-2.0.2/SConscript +++ b/components/net/lwip-2.0.2/SConscript @@ -67,7 +67,7 @@ path = [GetCurrentDir() + '/src', GetCurrentDir() + '/src/arch/include', GetCurrentDir() + '/src/include/netif'] -if not GetDepend('RT_USING_POSIX') or not GetDepend('SAL_USING_POSIX'): +if not GetDepend('RT_USING_SAL'): path += [GetCurrentDir() + '/src/include/posix'] if GetDepend(['RT_LWIP_SNMP']): diff --git a/components/net/sal_socket/SConscript b/components/net/sal_socket/SConscript index 3dcb5c3fe4..e962193359 100644 --- a/components/net/sal_socket/SConscript +++ b/components/net/sal_socket/SConscript @@ -5,10 +5,9 @@ from building import * cwd = GetCurrentDir() src = Glob('src/*.c') -src += Glob('socket/*.c') +src += Glob('socket/net_netdb.c') CPPPATH = [cwd + '/include'] -CPPPATH += [cwd + '/include/dfs_net'] CPPPATH += [cwd + '/include/socket'] if GetDepend('SAL_USING_LWIP'): @@ -21,6 +20,8 @@ if GetDepend('SAL_USING_LWIP') or GetDepend('SAL_USING_AT'): CPPPATH += [cwd + '/impl'] if GetDepend('SAL_USING_POSIX'): + CPPPATH += [cwd + '/include/dfs_net'] + src += Glob('socket/net_sockets.c') src += Glob('dfs_net/*.c') if not GetDepend('HAVE_SYS_SELECT_H'): diff --git a/components/net/sal_socket/impl/af_inet_at.c b/components/net/sal_socket/impl/af_inet_at.c index 3337af4194..f921e721eb 100644 --- a/components/net/sal_socket/impl/af_inet_at.c +++ b/components/net/sal_socket/impl/af_inet_at.c @@ -22,6 +22,8 @@ * 2018-06-06 ChenYong First version */ +#include + #include #include @@ -91,8 +93,6 @@ static const struct proto_ops at_inet_stream_ops = #ifdef SAL_USING_POSIX at_poll, -#else - NULL, #endif /* SAL_USING_POSIX */ }; diff --git a/components/net/sal_socket/impl/af_inet_lwip.c b/components/net/sal_socket/impl/af_inet_lwip.c index c45e39e39e..563f8947d0 100644 --- a/components/net/sal_socket/impl/af_inet_lwip.c +++ b/components/net/sal_socket/impl/af_inet_lwip.c @@ -44,6 +44,7 @@ #endif #endif +#ifdef SAL_USING_POSIX /* * Re-define lwip socket * @@ -73,9 +74,7 @@ struct lwip_sock { /** counter of how many threads are waiting for this socket using select */ SELWAIT_T select_waiting; -#ifdef SAL_USING_POSIX rt_wqueue_t wait_head; -#endif }; extern struct lwip_sock *lwip_tryget_socket(int s); @@ -160,14 +159,14 @@ static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len if (event) { -#ifdef SAL_USING_POSIX rt_wqueue_wakeup(&sock->wait_head, (void*) event); -#endif } } +#endif /* SAL_USING_POSIX */ static int inet_socket(int domain, int type, int protocol) { +#ifdef SAL_USING_POSIX int socket; socket = lwip_socket(domain, type, protocol); @@ -178,17 +177,18 @@ static int inet_socket(int domain, int type, int protocol) lwsock = lwip_tryget_socket(socket); lwsock->conn->callback = event_callback; -#ifdef SAL_USING_POSIX rt_wqueue_init(&lwsock->wait_head); -#endif - } return socket; +#else + return lwip_socket(domain, type, protocol); +#endif /* SAL_USING_POSIX */ } static int inet_accept(int socket, struct sockaddr *addr, socklen_t *addrlen) { +#ifdef SAL_USING_POSIX int new_socket; new_socket = lwip_accept(socket, addr, addrlen); @@ -198,12 +198,13 @@ static int inet_accept(int socket, struct sockaddr *addr, socklen_t *addrlen) lwsock = lwip_tryget_socket(new_socket); -#ifdef SAL_USING_POSIX rt_wqueue_init(&lwsock->wait_head); -#endif } return new_socket; +#else + return lwip_accept(socket, addr, addrlen); +#endif /* SAL_USING_POSIX */ } static int inet_getsockname(int socket, struct sockaddr *name, socklen_t *namelen) @@ -216,6 +217,7 @@ static int inet_getsockname(int socket, struct sockaddr *name, socklen_t *namele return lwip_getsockname(socket, name, namelen); } +#ifdef SAL_USING_POSIX static int inet_poll(struct dfs_fd *file, struct rt_pollreq *req) { int mask = 0; @@ -253,6 +255,7 @@ static int inet_poll(struct dfs_fd *file, struct rt_pollreq *req) return mask; } +#endif static const struct proto_ops lwip_inet_stream_ops = { inet_socket, @@ -270,7 +273,9 @@ static const struct proto_ops lwip_inet_stream_ops = { lwip_getpeername, inet_getsockname, lwip_ioctl, +#ifdef SAL_USING_POSIX inet_poll, +#endif }; static int inet_create(struct sal_socket *socket, int type, int protocol) diff --git a/components/net/sal_socket/include/sal.h b/components/net/sal_socket/include/sal.h index 62ffaa303e..480167c91d 100644 --- a/components/net/sal_socket/include/sal.h +++ b/components/net/sal_socket/include/sal.h @@ -25,9 +25,12 @@ #ifndef SAL_H__ #define SAL_H__ -#include #include +#ifdef SAL_USING_POSIX +#include +#endif + #ifdef __cplusplus extern "C" { #endif @@ -40,7 +43,9 @@ typedef uint32_t socklen_t; #define SAL_SOCKET_MAGIC 0x5A10 /* The maximum number of sockets structure */ +#ifndef SAL_SOCKETS_NUM #define SAL_SOCKETS_NUM DFS_FD_MAX +#endif /* The maximum number of protocol families */ #ifndef SAL_PROTO_FAMILIES_NUM @@ -68,7 +73,9 @@ struct proto_ops int (*getpeername)(int s, struct sockaddr *name, socklen_t *namelen); int (*getsockname)(int s, struct sockaddr *name, socklen_t *namelen); int (*ioctlsocket)(int s, long cmd, void *arg); +#ifdef SAL_USING_POSIX int (*poll) (struct dfs_fd *file, struct rt_pollreq *req); +#endif }; struct sal_socket diff --git a/components/net/sal_socket/include/socket/sys_socket/sys/socket.h b/components/net/sal_socket/include/socket/sys_socket/sys/socket.h index 2b0357ff81..ec7f7b87ea 100644 --- a/components/net/sal_socket/include/socket/sys_socket/sys/socket.h +++ b/components/net/sal_socket/include/socket/sys_socket/sys/socket.h @@ -26,19 +26,21 @@ #ifndef SYS_SOCKET_H_ #define SYS_SOCKET_H_ +#include #include #ifdef __cplusplus extern "C" { #endif +#ifdef SAL_USING_POSIX int accept(int s, struct sockaddr *addr, socklen_t *addrlen); int bind(int s, const struct sockaddr *name, socklen_t namelen); int shutdown(int s, int how); -int getpeername (int s, struct sockaddr *name, socklen_t *namelen); -int getsockname (int s, struct sockaddr *name, socklen_t *namelen); -int getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen); -int setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen); +int getpeername(int s, struct sockaddr *name, socklen_t *namelen); +int getsockname(int s, struct sockaddr *name, socklen_t *namelen); +int getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen); +int setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen); int connect(int s, const struct sockaddr *name, socklen_t namelen); int listen(int s, int backlog); int recv(int s, void *mem, size_t len, int flags); @@ -50,6 +52,24 @@ int sendto(int s, const void *dataptr, size_t size, int flags, int socket(int domain, int type, int protocol); int closesocket(int s); int ioctlsocket(int s, long cmd, void *arg); +#else +#define accept(s, addr, addrlen) sal_accept(s, addr, addrlen) +#define bind(s, name, namelen) sal_bind(s, name, namelen) +#define shutdown(s, how) sal_shutdown(s, how) +#define getpeername(s, name, namelen) sal_getpeername(s, name, namelen) +#define getsockname(s, name, namelen) sal_getsockname(s, name, namelen) +#define getsockopt(s, level, optname, optval, optlen) sal_getsockopt(s, level, optname, optval, optlen) +#define setsockopt(s, level, optname, optval, optlen) sal_setsockopt(s, level, optname, optval, optlen) +#define connect(s, name, namelen) sal_connect(s, name, namelen) +#define listen(s, backlog) sal_listen(s, backlog) +#define recv(s, mem, len, flags) sal_recvfrom(s, mem, len, flags, NULL, NULL) +#define recvfrom(s, mem, len, flags, from, fromlen) sal_recvfrom(s, mem, len, flags, from, fromlen) +#define send(s, dataptr, size, flags) sal_sendto(s, dataptr, size, flags, NULL, NULL) +#define sendto(s, dataptr, size, flags, to, tolen) sal_sendto(s, dataptr, size, flags, to, tolen) +#define socket(domain, type, protocol) sal_socket(domain, type, protocol) +#define closesocket(s) sal_closesocket(s) +#define ioctlsocket(s, cmd, arg) sal_ioctlsocket(s, cmd, arg) +#endif /* SAL_USING_POSIX */ #ifdef __cplusplus } diff --git a/components/net/sal_socket/src/sal_socket.c b/components/net/sal_socket/src/sal_socket.c index 7b6683a69c..f43492069e 100644 --- a/components/net/sal_socket/src/sal_socket.c +++ b/components/net/sal_socket/src/sal_socket.c @@ -705,6 +705,7 @@ int sal_ioctlsocket(int socket, long cmd, void *arg) return sock->ops->ioctlsocket((int) sock->user_data, cmd, arg); } +#ifdef SAL_USING_POSIX int sal_poll(struct dfs_fd *file, struct rt_pollreq *req) { struct sal_socket *sock; @@ -723,6 +724,7 @@ int sal_poll(struct dfs_fd *file, struct rt_pollreq *req) return sock->ops->poll(file, req); } +#endif struct hostent *sal_gethostbyname(const char *name) {