at_socket.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * File : at_socket.h
  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-06-06 chenYong first version
  23. */
  24. #ifndef __AT_SOCKET_H__
  25. #define __AT_SOCKET_H__
  26. #include <rtthread.h>
  27. #include <rtdevice.h>
  28. #include <rthw.h>
  29. #include <netdb.h>
  30. #include <sys/socket.h>
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #ifndef AT_SOCKET_RECV_BFSZ
  35. #define AT_SOCKET_RECV_BFSZ 512
  36. #endif
  37. #define AT_DEFAULT_RECVMBOX_SIZE 10
  38. #define AT_DEFAULT_ACCEPTMBOX_SIZE 10
  39. /* sal socket magic word */
  40. #define AT_SOCKET_MAGIC 0xA100
  41. /* Current state of the AT socket. */
  42. enum at_socket_state
  43. {
  44. AT_SOCKET_NONE,
  45. AT_SOCKET_LISTEN,
  46. AT_SOCKET_CONNECT,
  47. AT_SOCKET_CLOSED
  48. };
  49. enum at_socket_type
  50. {
  51. AT_SOCKET_INVALID = 0,
  52. AT_SOCKET_TCP = 0x10, /* TCP IPv4 */
  53. AT_SOCKET_UDP = 0x20, /* UDP IPv4 */
  54. };
  55. typedef enum
  56. {
  57. AT_SOCKET_EVT_RECV,
  58. AT_SOCKET_EVT_CLOSED,
  59. } at_socket_evt_t;
  60. typedef void (*at_evt_cb_t)(int socket, at_socket_evt_t event, const char *buff, size_t bfsz);
  61. struct at_socket;
  62. /* A callback prototype to inform about events for AT socket */
  63. typedef void (* at_socket_callback)(struct at_socket *conn, int event, uint16_t len);
  64. /* AT device socket options function */
  65. struct at_device_ops
  66. {
  67. int (*at_connect)(int socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client);
  68. int (*at_closesocket)(int socket);
  69. int (*at_send)(int socket, const char *buff, size_t bfsz, enum at_socket_type type);
  70. int (*at_domain_resolve)(const char *name, char ip[16]);
  71. void (*at_set_event_cb)(at_socket_evt_t event, at_evt_cb_t cb);
  72. };
  73. /* AT receive package list structure */
  74. struct at_recv_pkt
  75. {
  76. rt_slist_t list;
  77. size_t bfsz_totle;
  78. size_t bfsz_index;
  79. char *buff;
  80. };
  81. typedef struct at_recv_pkt *at_recv_pkt_t;
  82. struct at_socket
  83. {
  84. /* AT socket magic word */
  85. uint32_t magic;
  86. int socket;
  87. /* type of the AT socket (TCP, UDP or RAW) */
  88. enum at_socket_type type;
  89. /* current state of the AT socket */
  90. enum at_socket_state state;
  91. /* receive semaphore, received data release semaphore */
  92. rt_sem_t recv_notice;
  93. rt_mutex_t recv_lock;
  94. rt_slist_t recvpkt_list;
  95. /* timeout to wait for send or received data in milliseconds */
  96. int32_t recv_timeout;
  97. int32_t send_timeout;
  98. /* A callback function that is informed about events for this AT socket */
  99. at_socket_callback callback;
  100. /* number of times data was received, set by event_callback() */
  101. uint16_t rcvevent;
  102. /* number of times data was ACKed (free send buffer), set by event_callback() */
  103. uint16_t sendevent;
  104. /* error happened for this socket, set by event_callback() */
  105. uint16_t errevent;
  106. #ifdef SAL_USING_POSIX
  107. rt_wqueue_t wait_head;
  108. #endif
  109. };
  110. int at_socket(int domain, int type, int protocol);
  111. int at_closesocket(int socket);
  112. int at_shutdown(int socket, int how);
  113. int at_bind(int socket, const struct sockaddr *name, socklen_t namelen);
  114. int at_connect(int socket, const struct sockaddr *name, socklen_t namelen);
  115. int at_sendto(int socket, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
  116. int at_send(int socket, const void *data, size_t size, int flags);
  117. int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
  118. int at_recv(int socket, void *mem, size_t len, int flags);
  119. int at_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlen);
  120. int at_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen);
  121. struct hostent *at_gethostbyname(const char *name);
  122. int at_getaddrinfo(const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
  123. void at_freeaddrinfo(struct addrinfo *ai);
  124. struct at_socket *at_get_socket(int socket);
  125. void at_scoket_device_register(const struct at_device_ops *ops);
  126. #ifndef RT_USING_SAL
  127. #define socket(domain, type, protocol) at_socket(domain, type, protocol)
  128. #define closesocket(socket) at_closesocket(socket)
  129. #define shutdown(socket, how) at_shutdown(socket, how)
  130. #define bind(socket, name, namelen) at_bind(socket, name, namelen)
  131. #define connect(socket, name, namelen) at_connect(socket, name, namelen)
  132. #define sendto(socket, data, size, flags, to, tolen) at_sendto(socket, data, size, flags, to, tolen)
  133. #define send(socket, data, size, flags) at_send(socket, data, size, flags)
  134. #define recvfrom(socket, mem, len, flags, from, fromlen) at_recvfrom(socket, mem, len, flags, from, fromlen)
  135. #define getsockopt(socket, level, optname, optval, optlen) at_getsockopt(socket, level, optname, optval, optlen)
  136. #define setsockopt(socket, level, optname, optval, optlen) at_setsockopt(socket, level, optname, optval, optlen)
  137. #define gethostbyname(name) at_gethostbyname(name)
  138. #define getaddrinfo(nodename, servname, hints, res) at_getaddrinfo(nodename, servname, hints, res)
  139. #define freeaddrinfo(ai) at_freeaddrinfo(ai)
  140. #endif /* RT_USING_SAL */
  141. #ifdef __cplusplus
  142. }
  143. #endif
  144. #endif /* AT_SOCKET_H__ */