af_inet_at.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * File : af_inet_at.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. * 2018-06-06 ChenYong First version
  23. */
  24. #include <rtthread.h>
  25. #include <netdb.h>
  26. #include <sal.h>
  27. #include <at_socket.h>
  28. #include <af_inet.h>
  29. #ifdef SAL_USING_POSIX
  30. #include <dfs_poll.h>
  31. #endif
  32. #ifdef SAL_USING_POSIX
  33. static int at_poll(struct dfs_fd *file, struct rt_pollreq *req)
  34. {
  35. int mask = 0;
  36. struct at_socket *sock;
  37. struct sal_socket *sal_sock;
  38. sal_sock = sal_get_socket((int) file->data);
  39. if(!sal_sock)
  40. {
  41. return -1;
  42. }
  43. sock = at_get_socket((int)sal_sock->user_data);
  44. if (sock != NULL)
  45. {
  46. rt_base_t level;
  47. rt_poll_add(&sock->wait_head, req);
  48. level = rt_hw_interrupt_disable();
  49. if (sock->rcvevent)
  50. {
  51. mask |= POLLIN;
  52. }
  53. if (sock->sendevent)
  54. {
  55. mask |= POLLOUT;
  56. }
  57. if (sock->errevent)
  58. {
  59. mask |= POLLERR;
  60. }
  61. rt_hw_interrupt_enable(level);
  62. }
  63. return mask;
  64. }
  65. #endif
  66. static const struct proto_ops at_inet_stream_ops =
  67. {
  68. at_socket,
  69. at_closesocket,
  70. at_bind,
  71. NULL,
  72. at_connect,
  73. NULL,
  74. at_sendto,
  75. at_recvfrom,
  76. at_getsockopt,
  77. at_setsockopt,
  78. at_shutdown,
  79. NULL,
  80. NULL,
  81. NULL,
  82. #ifdef SAL_USING_POSIX
  83. at_poll,
  84. #endif /* SAL_USING_POSIX */
  85. };
  86. static int at_create(struct sal_socket *socket, int type, int protocol)
  87. {
  88. RT_ASSERT(socket);
  89. //TODO Check type & protocol
  90. socket->ops = &at_inet_stream_ops;
  91. return 0;
  92. }
  93. static const struct proto_family at_inet_family_ops = {
  94. "at",
  95. AF_AT,
  96. AF_INET,
  97. at_create,
  98. at_gethostbyname,
  99. NULL,
  100. at_freeaddrinfo,
  101. at_getaddrinfo,
  102. };
  103. int at_inet_init(void)
  104. {
  105. sal_proto_family_register(&at_inet_family_ops);
  106. return 0;
  107. }
  108. INIT_COMPONENT_EXPORT(at_inet_init);