at_socket.h 5.5 KB

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