diff --git a/components/lwip/include/lwip/lwip/sockets.h b/components/lwip/include/lwip/lwip/sockets.h index 5978b38c4..e9ebff656 100755 --- a/components/lwip/include/lwip/lwip/sockets.h +++ b/components/lwip/include/lwip/lwip/sockets.h @@ -38,7 +38,6 @@ #if LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */ -#include #include /* for size_t */ #include /* for FD_ZERO */ @@ -590,10 +589,10 @@ static inline int sendto(int s,const void *dataptr,size_t size,int flags,const s { return lwip_sendto_r(s,dataptr,size,flags,to,tolen); } static inline int socket(int domain,int type,int protocol) { return lwip_socket(domain,type,protocol); } -#ifndef ESP_PLATFORM +#ifndef ESP_HAS_SELECT static inline int select(int maxfdp1,fd_set *readset,fd_set *writeset,fd_set *exceptset,struct timeval *timeout) { return lwip_select(maxfdp1,readset,writeset,exceptset,timeout); } -#endif /* ESP_PLATFORM */ +#endif /* ESP_HAS_SELECT */ static inline int ioctlsocket(int s,long cmd,void *argp) { return lwip_ioctl_r(s,cmd,argp); } @@ -646,10 +645,10 @@ static inline int sendto(int s,const void *dataptr,size_t size,int flags,const s { return lwip_sendto(s,dataptr,size,flags,to,tolen); } static inline int socket(int domain,int type,int protocol) { return lwip_socket(domain,type,protocol); } -#ifndef ESP_PLATFORM +#ifndef ESP_HAS_SELECT static inline int select(int maxfdp1,fd_set t*readset,fd_set *writeset,fd_set *exceptset,struct timeval *timeout) { return lwip_select(maxfdp1,readset,writeset,exceptset,timeout); } -#endif /* ESP_PLATFORM */ +#endif /* ESP_HAS_SELECT */ static inline int ioctlsocket(int s,long cmd,void *argp) { return lwip_ioctl(s,cmd,argp); } diff --git a/components/lwip/include/lwip/port/lwipopts.h b/components/lwip/include/lwip/port/lwipopts.h index 4f43ed990..4d4ceb564 100644 --- a/components/lwip/include/lwip/port/lwipopts.h +++ b/components/lwip/include/lwip/port/lwipopts.h @@ -39,6 +39,7 @@ #include #include #include +#include #include "esp_task.h" #include "esp_system.h" #include "sdkconfig.h" @@ -730,6 +731,7 @@ #define ESP_DHCP_TIMER 1 #define ESP_LWIP_LOGI(...) ESP_LOGI("lwip", __VA_ARGS__) #define ESP_PING 1 +#define ESP_HAS_SELECT 1 #if CONFIG_LWIP_IRAM_OPTIMIZATION #define ESP_IRAM_ATTR IRAM_ATTR diff --git a/components/newlib/include/sys/select.h b/components/newlib/platform_include/sys/select.h similarity index 100% rename from components/newlib/include/sys/select.h rename to components/newlib/platform_include/sys/select.h diff --git a/components/vfs/test/test_vfs_select.c b/components/vfs/test/test_vfs_select.c index 5febf39a4..be74ebe91 100644 --- a/components/vfs/test/test_vfs_select.c +++ b/components/vfs/test/test_vfs_select.c @@ -33,6 +33,24 @@ typedef struct { static const char message[] = "Hello world!"; +static int open_dummy_socket() +{ + const struct addrinfo hints = { + .ai_family = AF_INET, + .ai_socktype = SOCK_DGRAM, + }; + struct addrinfo *res = NULL; + + const int err = getaddrinfo("localhost", "80", &hints, &res); + TEST_ASSERT_EQUAL(0, err); + TEST_ASSERT_NOT_NULL(res); + + const int dummy_socket_fd = socket(res->ai_family, res->ai_socktype, 0); + TEST_ASSERT(dummy_socket_fd >= 0); + + return dummy_socket_fd; +} + static int socket_init() { const struct addrinfo hints = { @@ -185,11 +203,13 @@ TEST_CASE("socket can do select()", "[vfs]") char recv_message[sizeof(message)]; init(&uart_fd, &socket_fd); + const int dummy_socket_fd = open_dummy_socket(); fd_set rfds; FD_ZERO(&rfds); FD_SET(uart_fd, &rfds); FD_SET(socket_fd, &rfds); + FD_SET(dummy_socket_fd, &rfds); const test_task_param_t test_task_param = { .fd = socket_fd, @@ -199,9 +219,10 @@ TEST_CASE("socket can do select()", "[vfs]") TEST_ASSERT_NOT_NULL(test_task_param.sem); start_task(&test_task_param); - const int s = select(MAX(uart_fd, socket_fd) + 1, &rfds, NULL, NULL, &tv); - TEST_ASSERT_EQUAL(s, 1); + const int s = select(MAX(MAX(uart_fd, socket_fd), dummy_socket_fd) + 1, &rfds, NULL, NULL, &tv); + TEST_ASSERT_EQUAL(1, s); TEST_ASSERT_UNLESS(FD_ISSET(uart_fd, &rfds)); + TEST_ASSERT_UNLESS(FD_ISSET(dummy_socket_fd, &rfds)); TEST_ASSERT(FD_ISSET(socket_fd, &rfds)); int read_bytes = read(socket_fd, recv_message, sizeof(message)); @@ -212,6 +233,7 @@ TEST_CASE("socket can do select()", "[vfs]") vSemaphoreDelete(test_task_param.sem); deinit(uart_fd, socket_fd); + close(dummy_socket_fd); } TEST_CASE("select() timeout", "[vfs]") diff --git a/components/vfs/vfs.c b/components/vfs/vfs.c index 36944ddda..a5f3c74c7 100644 --- a/components/vfs/vfs.c +++ b/components/vfs/vfs.c @@ -782,13 +782,15 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds continue; } - if (!socket_select && is_socket_fd) { - // no socket_select found yet and the fd is for a socket so take a look - if ((readfds && FD_ISSET(fd, readfds)) || - (writefds && FD_ISSET(fd, writefds)) || - (errorfds && FD_ISSET(fd, errorfds))) { - const vfs_entry_t *vfs = s_vfs[vfs_index]; - socket_select = vfs->vfs.socket_select; + if (is_socket_fd) { + if (!socket_select) { + // no socket_select found yet so take a look + if ((readfds && FD_ISSET(fd, readfds)) || + (writefds && FD_ISSET(fd, writefds)) || + (errorfds && FD_ISSET(fd, errorfds))) { + const vfs_entry_t *vfs = s_vfs[vfs_index]; + socket_select = vfs->vfs.socket_select; + } } continue; }