af_inet_lwip.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * File : af_inet_lwip.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-05-17 ChenYong First version
  23. */
  24. #include <rtthread.h>
  25. #include <lwip/sockets.h>
  26. #include <lwip/netdb.h>
  27. #include <lwip/api.h>
  28. #include <lwip/init.h>
  29. #ifdef SAL_USING_POSIX
  30. #include <dfs_poll.h>
  31. #endif
  32. #include <sal.h>
  33. #if LWIP_VERSION < 0x2000000
  34. #define SELWAIT_T int
  35. #else
  36. #ifndef SELWAIT_T
  37. #define SELWAIT_T u8_t
  38. #endif
  39. #endif
  40. /*
  41. * Re-define lwip socket
  42. *
  43. * NOTE: please make sure the definitions same in lwip::net_socket.c
  44. */
  45. struct lwip_sock {
  46. /** sockets currently are built on netconns, each socket has one netconn */
  47. struct netconn *conn;
  48. /** data that was left from the previous read */
  49. void *lastdata;
  50. /** offset in the data that was left from the previous read */
  51. u16_t lastoffset;
  52. /** number of times data was received, set by event_callback(),
  53. tested by the receive and select functions */
  54. s16_t rcvevent;
  55. /** number of times data was ACKed (free send buffer), set by event_callback(),
  56. tested by select */
  57. u16_t sendevent;
  58. /** error happened for this socket, set by event_callback(), tested by select */
  59. u16_t errevent;
  60. /** last error that occurred on this socket */
  61. #if LWIP_VERSION < 0x2000000
  62. int err;
  63. #else
  64. u8_t err;
  65. #endif
  66. /** counter of how many threads are waiting for this socket using select */
  67. SELWAIT_T select_waiting;
  68. #ifdef SAL_USING_POSIX
  69. rt_wqueue_t wait_head;
  70. #endif
  71. };
  72. extern struct lwip_sock *lwip_tryget_socket(int s);
  73. static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
  74. {
  75. int s;
  76. struct lwip_sock *sock;
  77. uint32_t event = 0;
  78. SYS_ARCH_DECL_PROTECT(lev);
  79. LWIP_UNUSED_ARG(len);
  80. /* Get socket */
  81. if (conn)
  82. {
  83. s = conn->socket;
  84. if (s < 0)
  85. {
  86. /* Data comes in right away after an accept, even though
  87. * the server task might not have created a new socket yet.
  88. * Just count down (or up) if that's the case and we
  89. * will use the data later. Note that only receive events
  90. * can happen before the new socket is set up. */
  91. SYS_ARCH_PROTECT(lev);
  92. if (conn->socket < 0)
  93. {
  94. if (evt == NETCONN_EVT_RCVPLUS)
  95. {
  96. conn->socket--;
  97. }
  98. SYS_ARCH_UNPROTECT(lev);
  99. return;
  100. }
  101. s = conn->socket;
  102. SYS_ARCH_UNPROTECT(lev);
  103. }
  104. sock = lwip_tryget_socket(s);
  105. if (!sock)
  106. {
  107. return;
  108. }
  109. }
  110. else
  111. {
  112. return;
  113. }
  114. SYS_ARCH_PROTECT(lev);
  115. /* Set event as required */
  116. switch (evt)
  117. {
  118. case NETCONN_EVT_RCVPLUS:
  119. sock->rcvevent++;
  120. break;
  121. case NETCONN_EVT_RCVMINUS:
  122. sock->rcvevent--;
  123. break;
  124. case NETCONN_EVT_SENDPLUS:
  125. sock->sendevent = 1;
  126. break;
  127. case NETCONN_EVT_SENDMINUS:
  128. sock->sendevent = 0;
  129. break;
  130. case NETCONN_EVT_ERROR:
  131. sock->errevent = 1;
  132. break;
  133. default:
  134. LWIP_ASSERT("unknown event", 0);
  135. break;
  136. }
  137. if (sock->lastdata || sock->rcvevent > 0)
  138. event |= POLLIN;
  139. if (sock->sendevent)
  140. event |= POLLOUT;
  141. if (sock->errevent)
  142. event |= POLLERR;
  143. SYS_ARCH_UNPROTECT(lev);
  144. if (event)
  145. {
  146. #ifdef SAL_USING_POSIX
  147. rt_wqueue_wakeup(&sock->wait_head, (void*) event);
  148. #endif
  149. }
  150. }
  151. static int inet_socket(int domain, int type, int protocol)
  152. {
  153. int socket;
  154. socket = lwip_socket(domain, type, protocol);
  155. if (socket >= 0)
  156. {
  157. struct lwip_sock *lwsock;
  158. lwsock = lwip_tryget_socket(socket);
  159. lwsock->conn->callback = event_callback;
  160. #ifdef SAL_USING_POSIX
  161. rt_wqueue_init(&lwsock->wait_head);
  162. #endif
  163. }
  164. return socket;
  165. }
  166. static int inet_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
  167. {
  168. int new_socket;
  169. new_socket = lwip_accept(socket, addr, addrlen);
  170. if (new_socket >= 0)
  171. {
  172. struct lwip_sock *lwsock;
  173. lwsock = lwip_tryget_socket(new_socket);
  174. #ifdef SAL_USING_POSIX
  175. rt_wqueue_init(&lwsock->wait_head);
  176. #endif
  177. }
  178. return new_socket;
  179. }
  180. static int inet_getsockname(int socket, struct sockaddr *name, socklen_t *namelen)
  181. {
  182. #if LWIP_VERSION_MAJOR < 2U
  183. rt_kprintf("ERROR: Your lwIP version is not supported. Please using lwIP 2.0.0+.\n");
  184. RT_ASSERT(LWIP_VERSION_MAJOR >= 2U);
  185. #endif
  186. return lwip_getsockname(socket, name, namelen);
  187. }
  188. static int inet_poll(struct dfs_fd *file, struct rt_pollreq *req)
  189. {
  190. int mask = 0;
  191. struct lwip_sock *sock;
  192. struct sal_socket *sal_sock;
  193. sal_sock = sal_get_socket((int) file->data);
  194. if(!sal_sock)
  195. {
  196. return -1;
  197. }
  198. sock = lwip_tryget_socket((int)sal_sock->user_data);
  199. if (sock != NULL)
  200. {
  201. rt_base_t level;
  202. rt_poll_add(&sock->wait_head, req);
  203. level = rt_hw_interrupt_disable();
  204. if (sock->lastdata || sock->rcvevent)
  205. {
  206. mask |= POLLIN;
  207. }
  208. if (sock->sendevent)
  209. {
  210. mask |= POLLOUT;
  211. }
  212. if (sock->errevent)
  213. {
  214. mask |= POLLERR;
  215. }
  216. rt_hw_interrupt_enable(level);
  217. }
  218. return mask;
  219. }
  220. static const struct proto_ops lwip_inet_stream_ops = {
  221. inet_socket,
  222. lwip_close,
  223. lwip_bind,
  224. lwip_listen,
  225. lwip_connect,
  226. inet_accept,
  227. lwip_sendto,
  228. lwip_recvfrom,
  229. lwip_getsockopt,
  230. //TODO fix on 1.4.1
  231. lwip_setsockopt,
  232. lwip_shutdown,
  233. lwip_getpeername,
  234. inet_getsockname,
  235. lwip_ioctl,
  236. inet_poll,
  237. };
  238. static int inet_create(struct sal_socket *socket, int type, int protocol)
  239. {
  240. RT_ASSERT(socket);
  241. //TODO Check type & protocol
  242. socket->ops = &lwip_inet_stream_ops;
  243. return 0;
  244. }
  245. static const struct proto_family lwip_inet_family_ops = {
  246. AF_INET,
  247. AF_INET,
  248. inet_create,
  249. lwip_gethostbyname,
  250. lwip_gethostbyname_r,
  251. lwip_freeaddrinfo,
  252. lwip_getaddrinfo,
  253. };
  254. int lwip_inet_init(void)
  255. {
  256. sal_proto_family_register(&lwip_inet_family_ops);
  257. return 0;
  258. }
  259. INIT_COMPONENT_EXPORT(lwip_inet_init);