1
0

at_socket.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-06-06 chenYong first version
  9. * 2022-06-02 xianxistu add implement about "AT server"
  10. */
  11. #ifndef __AT_SOCKET_H__
  12. #define __AT_SOCKET_H__
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. #include <rthw.h>
  16. #include <netdb.h>
  17. #include <sys/socket.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #ifndef AT_SOCKET_RECV_BFSZ
  22. #define AT_SOCKET_RECV_BFSZ 512
  23. #endif
  24. #define AT_DEFAULT_RECVMBOX_SIZE 10
  25. #define AT_DEFAULT_ACCEPTMBOX_SIZE 10
  26. /* sal socket magic word */
  27. #define AT_SOCKET_MAGIC 0xA100
  28. #ifdef AT_USING_SOCKET_SERVER
  29. #define AT_SOCKET_INFO_LEN (sizeof("SOCKET:") + 4)
  30. #endif
  31. /* Current state of the AT socket. */
  32. enum at_socket_state
  33. {
  34. AT_SOCKET_NONE,
  35. AT_SOCKET_OPEN,
  36. AT_SOCKET_LISTEN,
  37. AT_SOCKET_CONNECT,
  38. AT_SOCKET_CLOSED
  39. };
  40. enum at_socket_type
  41. {
  42. AT_SOCKET_INVALID = 0,
  43. AT_SOCKET_TCP = 0x10, /* TCP IPv4 */
  44. AT_SOCKET_UDP = 0x20, /* UDP IPv4 */
  45. };
  46. typedef enum
  47. {
  48. AT_SOCKET_EVT_RECV,
  49. AT_SOCKET_EVT_CLOSED,
  50. #ifdef AT_USING_SOCKET_SERVER
  51. AT_SOCKET_EVT_CONNECTED,
  52. #endif
  53. } at_socket_evt_t;
  54. struct at_socket;
  55. struct at_device;
  56. typedef void (*at_evt_cb_t)(struct at_socket *socket, at_socket_evt_t event, const char *buff, size_t bfsz);
  57. /* A callback prototype to inform about events for AT socket */
  58. typedef void (* at_socket_callback)(struct at_socket *conn, int event, uint16_t len);
  59. /* AT socket operations function */
  60. struct at_socket_ops
  61. {
  62. int (*at_connect)(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client);
  63. int (*at_closesocket)(struct at_socket *socket);
  64. int (*at_send)(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type);
  65. int (*at_domain_resolve)(const char *name, char ip[16]);
  66. void (*at_set_event_cb)(at_socket_evt_t event, at_evt_cb_t cb);
  67. int (*at_socket)(struct at_device *device, enum at_socket_type type);
  68. #ifdef AT_USING_SOCKET_SERVER
  69. int (*at_listen)(struct at_socket *socket, int backlog);
  70. #endif
  71. };
  72. /* AT receive package list structure */
  73. struct at_recv_pkt
  74. {
  75. rt_slist_t list;
  76. size_t bfsz_totle;
  77. size_t bfsz_index;
  78. char *buff;
  79. };
  80. typedef struct at_recv_pkt *at_recv_pkt_t;
  81. #ifdef AT_USING_SOCKET_SERVER
  82. struct at_listen_state
  83. {
  84. uint16_t is_listen;
  85. uint16_t port;
  86. };
  87. #endif
  88. struct at_socket
  89. {
  90. /* AT socket magic word */
  91. uint32_t magic;
  92. int socket;
  93. #ifdef AT_USING_SOCKET_SERVER
  94. struct at_listen_state listen;
  95. #endif
  96. /* device releated information for the socket */
  97. void *device;
  98. /* type of the AT socket (TCP, UDP or RAW) */
  99. enum at_socket_type type;
  100. /* current state of the AT socket */
  101. enum at_socket_state state;
  102. /* sockets operations */
  103. const struct at_socket_ops *ops;
  104. /* receive semaphore, received data release semaphore */
  105. rt_sem_t recv_notice;
  106. rt_mutex_t recv_lock;
  107. rt_slist_t recvpkt_list;
  108. /* timeout to wait for send or received data in milliseconds */
  109. int32_t recv_timeout;
  110. int32_t send_timeout;
  111. /* A callback function that is informed about events for this AT socket */
  112. at_socket_callback callback;
  113. /* number of times data was received, set by event_callback() */
  114. uint16_t rcvevent;
  115. /* number of times data was ACKed (free send buffer), set by event_callback() */
  116. uint16_t sendevent;
  117. /* error happened for this socket, set by event_callback() */
  118. uint16_t errevent;
  119. #ifdef SAL_USING_POSIX
  120. rt_wqueue_t wait_head;
  121. #endif
  122. rt_slist_t list;
  123. /* user-specific data */
  124. void *user_data;
  125. };
  126. int at_socket(int domain, int type, int protocol);
  127. int at_closesocket(int socket);
  128. int at_shutdown(int socket, int how);
  129. int at_bind(int socket, const struct sockaddr *name, socklen_t namelen);
  130. #ifdef AT_USING_SOCKET_SERVER
  131. int at_listen(int socket, int backlog);
  132. #endif
  133. int at_connect(int socket, const struct sockaddr *name, socklen_t namelen);
  134. #ifdef AT_USING_SOCKET_SERVER
  135. int at_accept(int socket, struct sockaddr *name, socklen_t *namelen);
  136. #endif
  137. int at_sendto(int socket, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
  138. int at_send(int socket, const void *data, size_t size, int flags);
  139. int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
  140. int at_recv(int socket, void *mem, size_t len, int flags);
  141. int at_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlen);
  142. int at_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen);
  143. struct hostent *at_gethostbyname(const char *name);
  144. int at_getaddrinfo(const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
  145. void at_freeaddrinfo(struct addrinfo *ai);
  146. struct at_socket *at_get_socket(int socket);
  147. #ifdef AT_USING_SOCKET_SERVER
  148. struct at_socket *at_get_base_socket(int base_socket);
  149. #endif
  150. #ifndef RT_USING_SAL
  151. #define socket(domain, type, protocol) at_socket(domain, type, protocol)
  152. #define closesocket(socket) at_closesocket(socket)
  153. #define shutdown(socket, how) at_shutdown(socket, how)
  154. #define bind(socket, name, namelen) at_bind(socket, name, namelen)
  155. #define connect(socket, name, namelen) at_connect(socket, name, namelen)
  156. #define sendto(socket, data, size, flags, to, tolen) at_sendto(socket, data, size, flags, to, tolen)
  157. #define send(socket, data, size, flags) at_send(socket, data, size, flags)
  158. #define recvfrom(socket, mem, len, flags, from, fromlen) at_recvfrom(socket, mem, len, flags, from, fromlen)
  159. #define getsockopt(socket, level, optname, optval, optlen) at_getsockopt(socket, level, optname, optval, optlen)
  160. #define setsockopt(socket, level, optname, optval, optlen) at_setsockopt(socket, level, optname, optval, optlen)
  161. #define gethostbyname(name) at_gethostbyname(name)
  162. #define getaddrinfo(nodename, servname, hints, res) at_getaddrinfo(nodename, servname, hints, res)
  163. #define freeaddrinfo(ai) at_freeaddrinfo(ai)
  164. #endif /* RT_USING_SAL */
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif /* AT_SOCKET_H__ */