wiz_socket.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-09-26 chenyong first version
  9. */
  10. #ifndef __WIZ_SOCKET_H__
  11. #define __WIZ_SOCKET_H__
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include <rthw.h>
  15. #include <netdb.h>
  16. #include <sys/socket.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /* WIZnet socket magic word */
  21. #define WIZ_SOCKET_MAGIC 0x3120
  22. /* WIZnet Socket address family */
  23. #ifndef AF_WIZ
  24. #define AF_WIZ 46
  25. #endif
  26. struct wiz_socket;
  27. /* A callback prototype to inform about events for wiznet socket */
  28. typedef void (* wiz_socket_callback)(struct wiz_socket *conn, int event, uint16_t len);
  29. struct wiz_clnt_info
  30. {
  31. int socket;
  32. int state;
  33. rt_slist_t list;
  34. };
  35. struct wiz_svr_info
  36. {
  37. int backlog;
  38. rt_timer_t conn_tmr;
  39. rt_mailbox_t conn_mbox;
  40. rt_slist_t clnt_list;
  41. };
  42. struct wiz_socket
  43. {
  44. /* WIZnet socket magic word */
  45. uint32_t magic;
  46. int socket;
  47. uint16_t port;
  48. /* type of the WIZnet socket (TCP, UDP or RAW) */
  49. uint8_t type;
  50. /* current state of the WIZnet socket */
  51. uint8_t state;
  52. /* receive semaphore, received data release semaphore */
  53. rt_sem_t recv_notice;
  54. rt_mutex_t recv_lock;
  55. /* timeout to wait for send or receive data in milliseconds */
  56. int32_t recv_timeout;
  57. int32_t send_timeout;
  58. /* A callback function that is informed about events for this AT socket */
  59. wiz_socket_callback callback;
  60. struct sockaddr *remote_addr;
  61. /* number of times data was received, set by event_callback() */
  62. uint16_t rcvevent;
  63. /* number of times data was ACKed (free send buffer), set by event_callback() */
  64. uint16_t sendevent;
  65. /* error happened for this socket, set by event_callback() */
  66. uint16_t errevent;
  67. /* server socket information */
  68. struct wiz_svr_info *svr_info;
  69. #ifdef SAL_USING_POSIX
  70. rt_wqueue_t wait_head;
  71. #endif
  72. };
  73. int wiz_socket(int domain, int type, int protocol);
  74. int wiz_closesocket(int socket);
  75. int wiz_shutdown(int socket, int how);
  76. int wiz_listen(int socket, int backlog);
  77. int wiz_bind(int socket, const struct sockaddr *name, socklen_t namelen);
  78. int wiz_connect(int socket, const struct sockaddr *name, socklen_t namelen);
  79. int wiz_accept(int socket, struct sockaddr *addr, socklen_t *addrlen);
  80. int wiz_sendto(int socket, const void *dwiza, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
  81. int wiz_send(int socket, const void *dwiza, size_t size, int flags);
  82. int wiz_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
  83. int wiz_recv(int socket, void *mem, size_t len, int flags);
  84. int wiz_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlen);
  85. int wiz_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen);
  86. struct hostent *wiz_gethostbyname(const char *name);
  87. int wiz_getaddrinfo(const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
  88. void wiz_freeaddrinfo(struct addrinfo *ai);
  89. /* get WIZnet socket object */
  90. struct wiz_socket *wiz_get_socket(int socket);
  91. /* WIZnet chip TCP/IP protocol register */
  92. int wiz_inet_init(void);
  93. #ifdef __cplusplus
  94. }
  95. #endif
  96. #endif /* __WIZ_SOCKET_H__ */