dfs_net.c 3.0 KB

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