From c1d7df514c2eff335ce1d47953d1800a7cad03c2 Mon Sep 17 00:00:00 2001 From: Dong Heng Date: Mon, 9 Jul 2018 14:08:24 +0800 Subject: [PATCH] feat(lwip): Use "glue" API to replace LWIP raw socket function --- components/lwip/Kconfig | 4 - .../lwip/apps/multi-threads/sockets_mt.c | 94 ++++++++++---- components/lwip/component.mk | 2 +- .../include/lwip/apps/multi-threads/errno.h | 33 ----- .../include/lwip/apps/multi-threads/netdb.h | 33 ----- .../lwip/apps/multi-threads/sys/socket.h | 115 ------------------ .../lwip/port/esp8266/include/lwipopts.h | 3 - 7 files changed, 71 insertions(+), 213 deletions(-) delete mode 100644 components/lwip/include/lwip/apps/multi-threads/errno.h delete mode 100644 components/lwip/include/lwip/apps/multi-threads/netdb.h delete mode 100644 components/lwip/include/lwip/apps/multi-threads/sys/socket.h diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index dc8cca34..77645a7c 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -22,10 +22,6 @@ config LWIP_SOCKET_MULTITHREAD help Enable the option can enable LWIP socket multithread and all function will be thread safe. - - But all LWIP raw socket APIs will be renamed ao that user can - not call "lwip_xxx"(for example "lwip_socket"), because compiling may faile. - So if you want to use LWIP raw socket APIs, please disable the option. config LWIP_MAX_SOCKETS int "Max number of open sockets" diff --git a/components/lwip/apps/multi-threads/sockets_mt.c b/components/lwip/apps/multi-threads/sockets_mt.c index 919cd7b5..5d1a9397 100644 --- a/components/lwip/apps/multi-threads/sockets_mt.c +++ b/components/lwip/apps/multi-threads/sockets_mt.c @@ -1,3 +1,16 @@ +// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. #include "lwip/opt.h" @@ -5,7 +18,7 @@ #include "lwip/priv/api_msg.h" -/* This helps code parsers/code completion by not having the COMPAT functions as defines */ +/* disable all LWIP socket API when compiling LWIP raw socket */ #undef lwip_accept #undef lwip_bind @@ -64,6 +77,39 @@ #include "../../lwip/src/api/sockets.c" +/* disable macros to enable LWIP function */ + +#undef lwip_accept +#undef lwip_bind +#undef lwip_shutdown +#undef lwip_getpeername +#undef lwip_getsockname +#undef lwip_setsockopt +#undef lwip_getsockopt +#undef lwip_close +#undef lwip_connect +#undef lwip_listen +#undef lwip_recv +#undef lwip_recvfrom +#undef lwip_send +#undef lwip_sendmsg +#undef lwip_sendto +#undef lwip_socket +#undef lwip_select +#undef lwip_ioctlsocket + +#if LWIP_POSIX_SOCKETS_IO_NAMES +#undef lwip_read +#undef lwip_write +#undef lwip_writev +#undef lwip_close +#undef closesocket +#undef lwip_fcntl +#undef lwip_ioctl +#endif /* LWIP_POSIX_SOCKETS_IO_NAMES */ + +/********************************************************************/ + #ifndef LWIP_SYNC_MT_SLEEP_MS #define LWIP_SYNC_MT_SLEEP_MS 10 #endif @@ -653,7 +699,7 @@ static void lwip_sync_mt(int s) } } -int lwip_socket_mt(int domain, int type, int protocol) +int lwip_socket(int domain, int type, int protocol) { int s; int i; @@ -682,7 +728,7 @@ int lwip_socket_mt(int domain, int type, int protocol) return s; } -int lwip_bind_mt(int s, const struct sockaddr *name, socklen_t namelen) +int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen) { int ret; @@ -696,7 +742,7 @@ int lwip_bind_mt(int s, const struct sockaddr *name, socklen_t namelen) return ret; } -int lwip_connect_mt(int s, const struct sockaddr *name, socklen_t namelen) +int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen) { int ret; @@ -709,7 +755,7 @@ int lwip_connect_mt(int s, const struct sockaddr *name, socklen_t namelen) return ret; } -int lwip_listen_mt(int s, int backlog) +int lwip_listen(int s, int backlog) { int ret; @@ -722,7 +768,7 @@ int lwip_listen_mt(int s, int backlog) return ret; } -int lwip_accept_mt(int s, struct sockaddr *addr, socklen_t *addrlen) +int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen) { int i; int ret; @@ -756,7 +802,7 @@ int lwip_accept_mt(int s, struct sockaddr *addr, socklen_t *addrlen) return ret; } -int lwip_getpeername_mt(int s, struct sockaddr *name, socklen_t *namelen) +int lwip_getpeername(int s, struct sockaddr *name, socklen_t *namelen) { int ret; @@ -769,7 +815,7 @@ int lwip_getpeername_mt(int s, struct sockaddr *name, socklen_t *namelen) return ret; } -int lwip_getsockname_mt(int s, struct sockaddr *name, socklen_t *namelen) +int lwip_getsockname(int s, struct sockaddr *name, socklen_t *namelen) { int ret; @@ -782,7 +828,7 @@ int lwip_getsockname_mt(int s, struct sockaddr *name, socklen_t *namelen) return ret; } -int lwip_setsockopt_mt(int s, int level, int optname, const void *optval, socklen_t optlen) +int lwip_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen) { int ret; @@ -795,7 +841,7 @@ int lwip_setsockopt_mt(int s, int level, int optname, const void *optval, sockle return ret; } -int lwip_getsockopt_mt(int s, int level, int optname, void *optval, socklen_t *optlen) +int lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) { int ret; @@ -820,7 +866,7 @@ int lwip_getsockopt_mt(int s, int level, int optname, void *optval, socklen_t *o return ret; } -int lwip_ioctl_mt(int s, long cmd, void *argp) +int lwip_ioctl(int s, long cmd, void *argp) { int ret; @@ -833,7 +879,7 @@ int lwip_ioctl_mt(int s, long cmd, void *argp) return ret; } -int lwip_sendto_mt(int s, const void *data, size_t size, int flags, +int lwip_sendto(int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen) { int ret; @@ -847,7 +893,7 @@ int lwip_sendto_mt(int s, const void *data, size_t size, int flags, return ret; } -int lwip_send_mt(int s, const void *data, size_t size, int flags) +int lwip_send(int s, const void *data, size_t size, int flags) { int ret; @@ -860,7 +906,7 @@ int lwip_send_mt(int s, const void *data, size_t size, int flags) return ret; } -int lwip_recvfrom_mt(int s, void *mem, size_t len, int flags, +int lwip_recvfrom(int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen) { int ret; @@ -874,22 +920,22 @@ int lwip_recvfrom_mt(int s, void *mem, size_t len, int flags, return ret; } -int lwip_recv_mt(int s, void *mem, size_t len, int flags) +int lwip_recv(int s, void *mem, size_t len, int flags) { - return lwip_recvfrom_mt(s, mem, len, flags, NULL, NULL); + return lwip_recvfrom(s, mem, len, flags, NULL, NULL); } -int lwip_read_mt(int s, void *mem, size_t len) +int lwip_read(int s, void *mem, size_t len) { - return lwip_recvfrom_mt(s, mem, len, 0, NULL, NULL); + return lwip_recvfrom(s, mem, len, 0, NULL, NULL); } -int lwip_write_mt(int s, const void *data, size_t size) +int lwip_write(int s, const void *data, size_t size) { - return lwip_send_mt(s, data, size, 0); + return lwip_send(s, data, size, 0); } -int lwip_fcntl_mt(int s, int cmd, int val) +int lwip_fcntl(int s, int cmd, int val) { int ret; @@ -917,12 +963,12 @@ static int __lwip_shutdown_mt(int s, int how) return ret; } -int lwip_shutdown_mt(int s, int how) +int lwip_shutdown(int s, int how) { return lwip_shutdown_esp(s, how); } -int lwip_close_mt(int s) +int lwip_close(int s) { int ret; int i; @@ -951,7 +997,7 @@ int lwip_close_mt(int s) return ret; } -int lwip_select_mt(int maxfdp1, fd_set *readset, fd_set *writeset, +int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout) { int ret; diff --git a/components/lwip/component.mk b/components/lwip/component.mk index 0ac9b6b0..6d0265b3 100644 --- a/components/lwip/component.mk +++ b/components/lwip/component.mk @@ -4,7 +4,7 @@ COMPONENT_ADD_INCLUDEDIRS += include/lwip/apps \ lwip/src/include \ port/esp8266/include \ - include/lwip/apps/multi-threads + lwip/src/include/posix COMPONENT_SRCDIRS += apps/dhcpserver \ apps/multi-threads \ diff --git a/components/lwip/include/lwip/apps/multi-threads/errno.h b/components/lwip/include/lwip/apps/multi-threads/errno.h deleted file mode 100644 index 611fcf7f..00000000 --- a/components/lwip/include/lwip/apps/multi-threads/errno.h +++ /dev/null @@ -1,33 +0,0 @@ -/** - * @file - * This file is a posix wrapper for lwip/errno.h. - */ - -/* - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - */ - -#include "posix/errno.h" diff --git a/components/lwip/include/lwip/apps/multi-threads/netdb.h b/components/lwip/include/lwip/apps/multi-threads/netdb.h deleted file mode 100644 index 1f43136a..00000000 --- a/components/lwip/include/lwip/apps/multi-threads/netdb.h +++ /dev/null @@ -1,33 +0,0 @@ -/** - * @file - * This file is a posix wrapper for lwip/netdb.h. - */ - -/* - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - */ - -#include "posix/netdb.h" diff --git a/components/lwip/include/lwip/apps/multi-threads/sys/socket.h b/components/lwip/include/lwip/apps/multi-threads/sys/socket.h deleted file mode 100644 index 261c7103..00000000 --- a/components/lwip/include/lwip/apps/multi-threads/sys/socket.h +++ /dev/null @@ -1,115 +0,0 @@ -/** - * @file - * This file is a posix wrapper for lwip/sockets.h. - */ - -/* - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - */ - -#ifndef _SOCKET_MT_H_ -#define _SOCKET_MT_H_ - -#include "posix/sys/socket.h" - -#ifdef SOCKETS_MT -int lwip_mt_init(void); -int lwip_socket_mt(int domain, int type, int protocol); -int lwip_bind_mt(int s, const struct sockaddr *name, socklen_t namelen); -int lwip_connect_mt(int s, const struct sockaddr *name, socklen_t namelen); -int lwip_listen_mt(int s, int backlog); -int lwip_accept_mt(int s, struct sockaddr *addr, socklen_t *addrlen); -int lwip_getpeername_mt(int s, struct sockaddr *name, socklen_t *namelen); -int lwip_getsockname_mt(int s, struct sockaddr *name, socklen_t *namelen); -int lwip_setsockopt_mt(int s, int level, int optname, const void *optval, socklen_t optlen); -int lwip_getsockopt_mt(int s, int level, int optname, void *optval, socklen_t *optlen); -int lwip_ioctl_mt(int s, long cmd, void *argp); -int lwip_sendto_mt(int s, const void *data, size_t size, int flags, - const struct sockaddr *to, socklen_t tolen); -int lwip_send_mt(int s, const void *data, size_t size, int flags); -int lwip_recvfrom_mt(int s, void *mem, size_t len, int flags, - struct sockaddr *from, socklen_t *fromlen); -int lwip_recv_mt(int s, void *mem, size_t len, int flags); -int lwip_read_mt(int s, void *mem, size_t len); -int lwip_write_mt(int s, const void *data, size_t size); -int lwip_shutdown_mt(int s, int how); -int lwip_close_mt(int s); -int lwip_select_mt(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout); -int lwip_fcntl_mt(int s, int cmd, int val); - -#undef accept -#undef bind -#undef shutdown -#undef connect -#undef getsockname -#undef getpeername -#undef setsockopt -#undef getsockopt -#undef listen -#undef recv -#undef recvfrom -#undef send -#undef sendto -#undef socket -#undef select -#undef ioctlsocket -#undef sendmsg - -#define accept(a,b,c) lwip_accept_mt(a,b,c) -#define bind(a,b,c) lwip_bind_mt(a,b,c) -#define shutdown(a,b) lwip_shutdown_mt(a,b) -#define connect(a,b,c) lwip_connect_mt(a,b,c) -#define getsockname(a,b,c) lwip_getsockname_mt(a,b,c) -#define getpeername(a,b,c) lwip_getpeername_mt(a,b,c) -#define setsockopt(a,b,c,d,e) lwip_setsockopt_mt(a,b,c,d,e) -#define getsockopt(a,b,c,d,e) lwip_getsockopt_mt(a,b,c,d,e) -#define listen(a,b) lwip_listen_mt(a,b) -#define recv(a,b,c,d) lwip_recv_mt(a,b,c,d) -#define recvfrom(a,b,c,d,e,f) lwip_recvfrom_mt(a,b,c,d,e,f) -#define send(a,b,c,d) lwip_send_mt(a,b,c,d) -#define sendto(a,b,c,d,e,f) lwip_sendto_mt(a,b,c,d,e,f) -#define socket(a,b,c) lwip_socket_mt(a,b,c) -#define select(a,b,c,d,e) lwip_select_mt(a,b,c,d,e) -#define ioctlsocket(a,b,c) lwip_ioctl_mt(a,b,c) - -#if LWIP_POSIX_SOCKETS_IO_NAMES -#undef read -#undef write -#undef close -#undef fcntl -#undef writev -#undef closesocket -#undef ioctl - -#define read(a,b,c) lwip_read_mt(a,b,c) -#define write(a,b,c) lwip_write_mt(a,b,c) -#define close(s) lwip_close_mt(s) -#define closesocket(s) lwip_close_mt(s) -#define fcntl(a,b,c) lwip_fcntl_mt(a,b,c) -#endif /* LWIP_POSIX_SOCKETS_IO_NAMES */ -#endif /* SOCKETS_MT */ - -#endif /* _SOCKET_H_ */ diff --git a/components/lwip/port/esp8266/include/lwipopts.h b/components/lwip/port/esp8266/include/lwipopts.h index 3198d9a8..535cd6fe 100644 --- a/components/lwip/port/esp8266/include/lwipopts.h +++ b/components/lwip/port/esp8266/include/lwipopts.h @@ -52,9 +52,6 @@ #ifdef CONFIG_LWIP_SOCKET_MULTITHREAD #define SOCKETS_MT -#if defined(LWIP_HDR_SOCKETS_H) && !defined(_SOCKET_MT_H_) -#error Please use instead of "lwip/sockets.h" -#endif #endif //#define SOCKETS_TCP_TRACE