at_socket.h 5.9 KB

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