diff --git a/components/newlib/CMakeLists.txt b/components/newlib/CMakeLists.txt index fd3d5a5d9..03101d79b 100644 --- a/components/newlib/CMakeLists.txt +++ b/components/newlib/CMakeLists.txt @@ -3,17 +3,13 @@ set(srcs "heap.c" "locks.c" "poll.c" - "pread.c" - "pwrite.c" "pthread.c" "random.c" "reent_init.c" - "select.c" "syscall_table.c" "syscalls.c" "termios.c" - "time.c" - "utime.c") + "time.c") set(include_dirs platform_include) if(CONFIG_SPIRAM_CACHE_WORKAROUND) diff --git a/components/newlib/poll.c b/components/newlib/poll.c index 481b13f00..c9f072fd5 100644 --- a/components/newlib/poll.c +++ b/components/newlib/poll.c @@ -1,4 +1,4 @@ -// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// Copyright 2019-2020 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. @@ -12,10 +12,84 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include -#include "esp_vfs.h" +#include +#include +#include int poll(struct pollfd *fds, nfds_t nfds, int timeout) { - return esp_vfs_poll(fds, nfds, timeout); + struct timeval tv = { + // timeout is in milliseconds + .tv_sec = timeout / 1000, + .tv_usec = (timeout % 1000) * 1000, + }; + int max_fd = -1; + fd_set readfds; + fd_set writefds; + fd_set errorfds; + struct _reent* r = __getreent(); + int ret = 0; + + if (fds == NULL) { + __errno_r(r) = ENOENT; + return -1; + } + + FD_ZERO(&readfds); + FD_ZERO(&writefds); + FD_ZERO(&errorfds); + + for (int i = 0; i < nfds; ++i) { + fds[i].revents = 0; + + if (fds[i].fd < 0) { + // revents should remain 0 and events ignored (according to the documentation of poll()). + continue; + } + + if (fds[i].fd >= FD_SETSIZE) { + fds[i].revents |= POLLNVAL; + ++ret; + continue; + } + + if (fds[i].events & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) { + FD_SET(fds[i].fd, &readfds); + FD_SET(fds[i].fd, &errorfds); + max_fd = MAX(max_fd, fds[i].fd); + } + + if (fds[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) { + FD_SET(fds[i].fd, &writefds); + FD_SET(fds[i].fd, &errorfds); + max_fd = MAX(max_fd, fds[i].fd); + } + } + + const int select_ret = select(max_fd + 1, &readfds, &writefds, &errorfds, timeout < 0 ? NULL: &tv); + + if (select_ret > 0) { + ret += select_ret; + + for (int i = 0; i < nfds; ++i) { + if (FD_ISSET(fds[i].fd, &readfds)) { + fds[i].revents |= POLLIN; + } + + if (FD_ISSET(fds[i].fd, &writefds)) { + fds[i].revents |= POLLOUT; + } + + if (FD_ISSET(fds[i].fd, &errorfds)) { + fds[i].revents |= POLLERR; + } + } + } else { + ret = select_ret; + // keeping the errno from select() + } + + return ret; } diff --git a/components/newlib/pread.c b/components/newlib/pread.c deleted file mode 100644 index d14bd7257..000000000 --- a/components/newlib/pread.c +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 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 -#include "esp_vfs.h" - -ssize_t pread(int fd, void *dst, size_t size, off_t offset) -{ - return esp_vfs_pread(fd, dst, size, offset); -} diff --git a/components/newlib/pwrite.c b/components/newlib/pwrite.c deleted file mode 100644 index 78fd0b85c..000000000 --- a/components/newlib/pwrite.c +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 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 -#include "esp_vfs.h" - -ssize_t pwrite(int fd, const void *src, size_t size, off_t offset) -{ - return esp_vfs_pwrite(fd, src, size, offset); -} diff --git a/components/newlib/select.c b/components/newlib/select.c deleted file mode 100644 index ac9de99da..000000000 --- a/components/newlib/select.c +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2018 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 -#include "esp_vfs.h" -#include "sdkconfig.h" - -#ifdef CONFIG_LWIP_USE_ONLY_LWIP_SELECT -#include "lwip/sockets.h" - -#ifdef CONFIG_VFS_SUPPRESS_SELECT_DEBUG_OUTPUT -#define LOG_LOCAL_LEVEL ESP_LOG_NONE -#endif //CONFIG_VFS_SUPPRESS_SELECT_DEBUG_OUTPUT -#include "esp_log.h" - -static const char *TAG = "newlib_select"; - -static void log_fd_set(const char *fds_name, const fd_set *fds) -{ - if (fds_name && fds) { - ESP_LOGD(TAG, "FDs in %s =", fds_name); - for (int i = 0; i < MAX_FDS; ++i) { - if (FD_ISSET(i, fds)) { - ESP_LOGD(TAG, "%d", i); - } - } - } -} -#endif //CONFIG_LWIP_USE_ONLY_LWIP_SELECT - -int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout) -{ -#ifdef CONFIG_LWIP_USE_ONLY_LWIP_SELECT - ESP_LOGD(TAG, "lwip_select starts with nfds = %d", nfds); - if (timeout) { - ESP_LOGD(TAG, "timeout is %lds + %ldus", (long)timeout->tv_sec, timeout->tv_usec); - } - log_fd_set("readfds", readfds); - log_fd_set("writefds", writefds); - log_fd_set("errorfds", errorfds); - - int ret = lwip_select(nfds, readfds, writefds, errorfds, timeout); - - ESP_LOGD(TAG, "lwip_select returns %d", ret); - log_fd_set("readfds", readfds); - log_fd_set("writefds", writefds); - log_fd_set("errorfds", errorfds); - - return ret; -#else - return esp_vfs_select(nfds, readfds, writefds, errorfds, timeout); -#endif -} diff --git a/components/newlib/utime.c b/components/newlib/utime.c deleted file mode 100644 index c838fd45a..000000000 --- a/components/newlib/utime.c +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2018 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 -#include "esp_vfs.h" - -int utime(const char *path, const struct utimbuf *times) -{ - return esp_vfs_utime(path, times); -}