lwip_select.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * File : lwip_select.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2015-05-05 Bernard First version
  23. */
  24. #include <rtthread.h>
  25. #include <sys/select.h>
  26. #ifdef RT_USING_LWIP
  27. int
  28. select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
  29. struct timeval *timeout)
  30. {
  31. int index, result;
  32. int sock, maxfd;
  33. fd_set sock_readset;
  34. fd_set sock_writeset;
  35. fd_set sock_exceptset;
  36. FD_ZERO(&sock_readset);
  37. FD_ZERO(&sock_writeset);
  38. FD_ZERO(&sock_exceptset);
  39. maxfd = 0;
  40. for (index = 0; index < maxfdp1; index ++)
  41. {
  42. /* convert fd to sock */
  43. sock = dfs_lwip_getsocket(index);
  44. if (sock == -1) continue;
  45. if (sock > maxfd) maxfd = sock;
  46. /* if FD is set, set the socket set */
  47. if (readset && FD_ISSET(index, readset))
  48. {
  49. FD_SET(sock, &sock_readset);
  50. }
  51. if (writeset && FD_ISSET(index, writeset))
  52. {
  53. FD_SET(sock, &sock_writeset);
  54. }
  55. if (exceptset && FD_ISSET(index, exceptset))
  56. {
  57. FD_SET(sock, &sock_exceptset);
  58. }
  59. }
  60. /* no socket found, return bad file descriptor */
  61. if (maxfd == 0) return -EBADF;
  62. maxfd += 1;
  63. result = lwip_selscan(maxfd, &sock_readset, &sock_writeset, &sock_exceptset, timeout);
  64. if (readset) FD_ZERO(readset);
  65. if (writeset) FD_ZERO(writeset);
  66. if (exceptset) FD_ZERO(exceptset);
  67. if (result != -1)
  68. {
  69. for (index = 0; index < maxfd; index ++)
  70. {
  71. /* check each socket */
  72. if ((FD_ISSET(index, &sock_readset)) ||
  73. (FD_ISSET(index, &sock_writeset)) ||
  74. (FD_ISSET(index, &sock_exceptset)))
  75. {
  76. int fd_index;
  77. /* Because we can not get the corresponding fd, we have to search it one by one */
  78. for (fd_index = 0; fd_index < maxfdp1; fd_index ++)
  79. {
  80. sock = dfs_lwip_getsocket(fd_index);
  81. if (sock == index) /* found it */
  82. {
  83. if (readset && FD_ISSET(index, &sock_readset))
  84. {
  85. FD_SET(sock, readset);
  86. }
  87. if (writeset && FD_ISSET(index, &sock_writeset))
  88. {
  89. FD_SET(sock, writeset);
  90. }
  91. if (exceptset && FD_ISSET(index, &sock_exceptset))
  92. {
  93. FD_SET(sock, exceptset);
  94. }
  95. /* end of search */
  96. break;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. return result;
  103. }
  104. #endif