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