dfs_net.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * File : dfs_net.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, 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. * 2018-05-24 ChenYong Add socket abstraction layer
  26. */
  27. #include <rtthread.h>
  28. #include <dfs.h>
  29. #include <dfs_net.h>
  30. #include <sys/socket.h>
  31. int dfs_net_getsocket(int fd)
  32. {
  33. int socket;
  34. struct dfs_fd *_dfs_fd;
  35. _dfs_fd = fd_get(fd);
  36. if (_dfs_fd == NULL) return -1;
  37. if (_dfs_fd->type != FT_SOCKET) socket = -1;
  38. else socket = (int)_dfs_fd->data;
  39. fd_put(_dfs_fd); /* put this dfs fd */
  40. return socket;
  41. }
  42. static int dfs_net_ioctl(struct dfs_fd* file, int cmd, void* args)
  43. {
  44. return -EIO;
  45. }
  46. static int dfs_net_read(struct dfs_fd* file, void *buf, size_t count)
  47. {
  48. int socket = (int) file->data;
  49. return sal_recvfrom(socket, buf, count, 0, NULL, NULL);
  50. }
  51. static int dfs_net_write(struct dfs_fd *file, const void *buf, size_t count)
  52. {
  53. int socket = (int) file->data;
  54. return sal_sendto(socket, buf, count, 0, NULL, 0);
  55. }
  56. static int dfs_net_close(struct dfs_fd* file)
  57. {
  58. int socket = (int) file->data;
  59. return sal_closesocket(socket);
  60. }
  61. static int dfs_net_poll(struct dfs_fd *file, struct rt_pollreq *req)
  62. {
  63. extern int sal_poll(struct dfs_fd *file, struct rt_pollreq *req);
  64. return sal_poll(file, req);
  65. }
  66. const struct dfs_file_ops _net_fops =
  67. {
  68. NULL, /* open */
  69. dfs_net_close,
  70. dfs_net_ioctl,
  71. dfs_net_read,
  72. dfs_net_write,
  73. NULL,
  74. NULL, /* lseek */
  75. NULL, /* getdents */
  76. dfs_net_poll,
  77. };
  78. const struct dfs_file_ops *dfs_net_get_fops(void)
  79. {
  80. return &_net_fops;
  81. }