select.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <sys/select.h>
  15. #include "esp_vfs.h"
  16. #include "sdkconfig.h"
  17. #ifdef CONFIG_USE_ONLY_LWIP_SELECT
  18. #include "lwip/sockets.h"
  19. #ifdef CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT
  20. #define LOG_LOCAL_LEVEL ESP_LOG_NONE
  21. #endif //CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT
  22. #include "esp_log.h"
  23. static const char *TAG = "newlib_select";
  24. static void log_fd_set(const char *fds_name, const fd_set *fds)
  25. {
  26. if (fds_name && fds) {
  27. ESP_LOGD(TAG, "FDs in %s =", fds_name);
  28. for (int i = 0; i < MAX_FDS; ++i) {
  29. if (FD_ISSET(i, fds)) {
  30. ESP_LOGD(TAG, "%d", i);
  31. }
  32. }
  33. }
  34. }
  35. #endif //CONFIG_USE_ONLY_LWIP_SELECT
  36. int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout)
  37. {
  38. #ifdef CONFIG_USE_ONLY_LWIP_SELECT
  39. ESP_LOGD(TAG, "lwip_select starts with nfds = %d", nfds);
  40. if (timeout) {
  41. ESP_LOGD(TAG, "timeout is %lds + %ldus", timeout->tv_sec, timeout->tv_usec);
  42. }
  43. log_fd_set("readfds", readfds);
  44. log_fd_set("writefds", writefds);
  45. log_fd_set("errorfds", errorfds);
  46. int ret = lwip_select(nfds, readfds, writefds, errorfds, timeout);
  47. ESP_LOGD(TAG, "lwip_select returns %d", ret);
  48. log_fd_set("readfds", readfds);
  49. log_fd_set("writefds", writefds);
  50. log_fd_set("errorfds", errorfds);
  51. return ret;
  52. #else
  53. return esp_vfs_select(nfds, readfds, writefds, errorfds, timeout);
  54. #endif
  55. }