dfs_net.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2015-02-17 Bernard First version
  9. * 2016-05-07 Bernard Rename dfs_lwip to dfs_net
  10. * 2018-03-09 Bernard Fix the last data issue in poll.
  11. * 2018-05-24 ChenYong Add socket abstraction layer
  12. */
  13. #include <rtthread.h>
  14. #include <dfs.h>
  15. #include <dfs_net.h>
  16. #include <sys/socket.h>
  17. int dfs_net_getsocket(int fd)
  18. {
  19. int socket;
  20. struct dfs_fd *_dfs_fd;
  21. _dfs_fd = fd_get(fd);
  22. if (_dfs_fd == NULL) return -1;
  23. if (_dfs_fd->type != FT_SOCKET) socket = -1;
  24. else socket = (int)_dfs_fd->data;
  25. fd_put(_dfs_fd); /* put this dfs fd */
  26. return socket;
  27. }
  28. static int dfs_net_ioctl(struct dfs_fd* file, int cmd, void* args)
  29. {
  30. return -EIO;
  31. }
  32. static int dfs_net_read(struct dfs_fd* file, void *buf, size_t count)
  33. {
  34. int socket = (int) file->data;
  35. return sal_recvfrom(socket, buf, count, 0, NULL, NULL);
  36. }
  37. static int dfs_net_write(struct dfs_fd *file, const void *buf, size_t count)
  38. {
  39. int socket = (int) file->data;
  40. return sal_sendto(socket, buf, count, 0, NULL, 0);
  41. }
  42. static int dfs_net_close(struct dfs_fd* file)
  43. {
  44. int socket = (int) file->data;
  45. return sal_closesocket(socket);
  46. }
  47. static int dfs_net_poll(struct dfs_fd *file, struct rt_pollreq *req)
  48. {
  49. extern int sal_poll(struct dfs_fd *file, struct rt_pollreq *req);
  50. return sal_poll(file, req);
  51. }
  52. const struct dfs_file_ops _net_fops =
  53. {
  54. NULL, /* open */
  55. dfs_net_close,
  56. dfs_net_ioctl,
  57. dfs_net_read,
  58. dfs_net_write,
  59. NULL,
  60. NULL, /* lseek */
  61. NULL, /* getdents */
  62. dfs_net_poll,
  63. };
  64. const struct dfs_file_ops *dfs_net_get_fops(void)
  65. {
  66. return &_net_fops;
  67. }