af_inet_lwip.c 7.0 KB

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