dfs_net.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * File : dfs_net.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015-2016, 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-02-17 Bernard First version
  23. * 2016-05-07 Bernard Rename dfs_lwip to dfs_net
  24. * 2018-03-09 Bernard Fix the last data issue in poll.
  25. */
  26. #include <rtthread.h>
  27. #include <dfs.h>
  28. #include <dfs_fs.h>
  29. #include <dfs_file.h>
  30. #include <dfs_posix.h>
  31. #include <rtdevice.h>
  32. #include <sys/socket.h>
  33. #include <dfs_poll.h>
  34. #include "dfs_net.h"
  35. int dfs_net_getsocket(int fd)
  36. {
  37. int sock;
  38. struct dfs_fd *_dfs_fd;
  39. _dfs_fd = fd_get(fd);
  40. if (_dfs_fd == NULL) return -1;
  41. if (_dfs_fd->type != FT_SOCKET) sock = -1;
  42. else sock = (int)_dfs_fd->data;
  43. fd_put(_dfs_fd); /* put this dfs fd */
  44. return sock;
  45. }
  46. static int dfs_net_ioctl(struct dfs_fd* file, int cmd, void* args)
  47. {
  48. return -EIO;
  49. }
  50. static int dfs_net_read(struct dfs_fd* file, void *buf, size_t count)
  51. {
  52. int sock;
  53. sock = (int)file->data;
  54. count = lwip_read(sock, buf, count);
  55. return count;
  56. }
  57. static int dfs_net_write(struct dfs_fd *file, const void *buf, size_t count)
  58. {
  59. int sock;
  60. sock = (int)file->data;
  61. count = lwip_write(sock, buf, count);
  62. return count;
  63. }
  64. static int dfs_net_close(struct dfs_fd* file)
  65. {
  66. int sock;
  67. int result;
  68. sock = (int)file->data;
  69. result = lwip_close(sock);
  70. if (result == 0) return RT_EOK;
  71. return -result;
  72. }
  73. static int dfs_net_poll(struct dfs_fd *file, struct rt_pollreq *req)
  74. {
  75. int sfd;
  76. int mask = 0;
  77. struct lwip_sock *sock;
  78. sfd = (int)file->data;
  79. sock = lwip_tryget_socket(sfd);
  80. if (sock != NULL)
  81. {
  82. rt_base_t level;
  83. rt_poll_add(&sock->wait_head, req);
  84. level = rt_hw_interrupt_disable();
  85. if (sock->lastdata || sock->rcvevent)
  86. {
  87. mask |= POLLIN;
  88. }
  89. if (sock->sendevent)
  90. {
  91. mask |= POLLOUT;
  92. }
  93. if (sock->errevent)
  94. {
  95. mask |= POLLERR;
  96. }
  97. rt_hw_interrupt_enable(level);
  98. }
  99. return mask;
  100. }
  101. const struct dfs_file_ops _net_fops =
  102. {
  103. NULL, /* open */
  104. dfs_net_close,
  105. dfs_net_ioctl,
  106. dfs_net_read,
  107. dfs_net_write,
  108. NULL,
  109. NULL, /* lseek */
  110. NULL, /* getdents */
  111. dfs_net_poll,
  112. };
  113. const struct dfs_file_ops *dfs_net_get_fops(void)
  114. {
  115. return &_net_fops;
  116. }