at_socket.h 5.3 KB

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