af_inet_lwip.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #include <af_inet.h>
  34. #if LWIP_VERSION < 0x2000000
  35. #define SELWAIT_T int
  36. #else
  37. #ifndef SELWAIT_T
  38. #define SELWAIT_T u8_t
  39. #endif
  40. #endif
  41. #ifdef SAL_USING_POSIX
  42. /*
  43. * Re-define lwip socket
  44. *
  45. * NOTE: please make sure the definitions same in lwip::net_socket.c
  46. */
  47. struct lwip_sock {
  48. /** sockets currently are built on netconns, each socket has one netconn */
  49. struct netconn *conn;
  50. /** data that was left from the previous read */
  51. void *lastdata;
  52. /** offset in the data that was left from the previous read */
  53. u16_t lastoffset;
  54. /** number of times data was received, set by event_callback(),
  55. tested by the receive and select functions */
  56. s16_t rcvevent;
  57. /** number of times data was ACKed (free send buffer), set by event_callback(),
  58. tested by select */
  59. u16_t sendevent;
  60. /** error happened for this socket, set by event_callback(), tested by select */
  61. u16_t errevent;
  62. /** last error that occurred on this socket */
  63. #if LWIP_VERSION < 0x2000000
  64. int err;
  65. #else
  66. u8_t err;
  67. #endif
  68. /** counter of how many threads are waiting for this socket using select */
  69. SELWAIT_T select_waiting;
  70. rt_wqueue_t wait_head;
  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. rt_wqueue_wakeup(&sock->wait_head, (void*) event);
  147. }
  148. }
  149. #endif /* SAL_USING_POSIX */
  150. static int inet_socket(int domain, int type, int protocol)
  151. {
  152. #ifdef SAL_USING_POSIX
  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. rt_wqueue_init(&lwsock->wait_head);
  161. }
  162. return socket;
  163. #else
  164. return lwip_socket(domain, type, protocol);
  165. #endif /* SAL_USING_POSIX */
  166. }
  167. static int inet_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
  168. {
  169. #ifdef SAL_USING_POSIX
  170. int new_socket;
  171. new_socket = lwip_accept(socket, addr, addrlen);
  172. if (new_socket >= 0)
  173. {
  174. struct lwip_sock *lwsock;
  175. lwsock = lwip_tryget_socket(new_socket);
  176. rt_wqueue_init(&lwsock->wait_head);
  177. }
  178. return new_socket;
  179. #else
  180. return lwip_accept(socket, addr, addrlen);
  181. #endif /* SAL_USING_POSIX */
  182. }
  183. static int inet_getsockname(int socket, struct sockaddr *name, socklen_t *namelen)
  184. {
  185. #if LWIP_VERSION_MAJOR < 2U
  186. rt_kprintf("ERROR: Your lwIP version is not supported. Please using lwIP 2.0.0+.\n");
  187. RT_ASSERT(LWIP_VERSION_MAJOR >= 2U);
  188. #endif
  189. return lwip_getsockname(socket, name, namelen);
  190. }
  191. #ifdef SAL_USING_POSIX
  192. static int inet_poll(struct dfs_fd *file, struct rt_pollreq *req)
  193. {
  194. int mask = 0;
  195. struct lwip_sock *sock;
  196. struct sal_socket *sal_sock;
  197. sal_sock = sal_get_socket((int) file->data);
  198. if(!sal_sock)
  199. {
  200. return -1;
  201. }
  202. sock = lwip_tryget_socket((int)sal_sock->user_data);
  203. if (sock != NULL)
  204. {
  205. rt_base_t level;
  206. rt_poll_add(&sock->wait_head, req);
  207. level = rt_hw_interrupt_disable();
  208. if (sock->lastdata || sock->rcvevent)
  209. {
  210. mask |= POLLIN;
  211. }
  212. if (sock->sendevent)
  213. {
  214. mask |= POLLOUT;
  215. }
  216. if (sock->errevent)
  217. {
  218. mask |= POLLERR;
  219. }
  220. rt_hw_interrupt_enable(level);
  221. }
  222. return mask;
  223. }
  224. #endif
  225. static const struct proto_ops lwip_inet_stream_ops = {
  226. inet_socket,
  227. lwip_close,
  228. lwip_bind,
  229. lwip_listen,
  230. lwip_connect,
  231. inet_accept,
  232. lwip_sendto,
  233. 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_inet_stream_ops;
  250. return 0;
  251. }
  252. static const struct proto_family lwip_inet_family_ops = {
  253. "lwip",
  254. AF_INET,
  255. AF_INET,
  256. inet_create,
  257. lwip_gethostbyname,
  258. lwip_gethostbyname_r,
  259. lwip_freeaddrinfo,
  260. lwip_getaddrinfo,
  261. };
  262. int lwip_inet_init(void)
  263. {
  264. sal_proto_family_register(&lwip_inet_family_ops);
  265. return 0;
  266. }
  267. INIT_COMPONENT_EXPORT(lwip_inet_init);