dfs_net.c 3.0 KB

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