socket.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * File : socket.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, 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. * 2015-02-17 Bernard First version
  23. */
  24. #ifndef SOCKET_H__
  25. #define SOCKET_H__
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include <inttypes.h>
  30. #include <lwip/sockets.h>
  31. #include <lwip/api.h>
  32. #include <lwip/init.h>
  33. #include <rtdevice.h>
  34. #if LWIP_VERSION < 0x2000000
  35. typedef uint16_t sa_family_t;
  36. typedef uint16_t in_port_t;
  37. struct sockaddr_storage
  38. {
  39. sa_family_t ss_family; /* Address family */
  40. char ss_data[14]; /* 14-bytes of address data */
  41. };
  42. #endif
  43. #if LWIP_VERSION < 0x2000000
  44. #define SELWAIT_T int
  45. #else
  46. #ifndef SELWAIT_T
  47. #define SELWAIT_T u8_t
  48. #endif
  49. #endif
  50. /*
  51. * Re-define lwip socket
  52. *
  53. * NOTE: please make sure the definitions same in lwip::net_socket.c
  54. */
  55. struct lwip_sock {
  56. /** sockets currently are built on netconns, each socket has one netconn */
  57. struct netconn *conn;
  58. /** data that was left from the previous read */
  59. void *lastdata;
  60. /** offset in the data that was left from the previous read */
  61. u16_t lastoffset;
  62. /** number of times data was received, set by event_callback(),
  63. tested by the receive and select functions */
  64. s16_t rcvevent;
  65. /** number of times data was ACKed (free send buffer), set by event_callback(),
  66. tested by select */
  67. u16_t sendevent;
  68. /** error happened for this socket, set by event_callback(), tested by select */
  69. u16_t errevent;
  70. /** last error that occurred on this socket */
  71. int err;
  72. /** counter of how many threads are waiting for this socket using select */
  73. SELWAIT_T select_waiting;
  74. rt_wqueue_t wait_head;
  75. };
  76. struct lwip_sock *lwip_tryget_socket(int s);
  77. int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
  78. int bind(int s, const struct sockaddr *name, socklen_t namelen);
  79. int shutdown(int s, int how);
  80. int getpeername (int s, struct sockaddr *name, socklen_t *namelen);
  81. int getsockname (int s, struct sockaddr *name, socklen_t *namelen);
  82. int getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen);
  83. int setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen);
  84. int connect(int s, const struct sockaddr *name, socklen_t namelen);
  85. int listen(int s, int backlog);
  86. int recv(int s, void *mem, size_t len, int flags);
  87. int recvfrom(int s, void *mem, size_t len, int flags,
  88. struct sockaddr *from, socklen_t *fromlen);
  89. int send(int s, const void *dataptr, size_t size, int flags);
  90. int sendto(int s, const void *dataptr, size_t size, int flags,
  91. const struct sockaddr *to, socklen_t tolen);
  92. int socket(int domain, int type, int protocol);
  93. int closesocket(int s);
  94. int ioctlsocket(int s, long cmd, void *arg);
  95. #ifdef __cplusplus
  96. }
  97. #endif
  98. #endif