1
0

at_socket.h 5.6 KB

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