STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
api.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32 #ifndef LWIP_HDR_API_H
33 #define LWIP_HDR_API_H
34 
35 #include "lwip/opt.h"
36 
37 #if LWIP_NETCONN || LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
38 /* Note: Netconn API is always available when sockets are enabled -
39  * sockets are implemented on top of them */
40 
41 #include <stddef.h> /* for size_t */
42 
43 #include "lwip/netbuf.h"
44 #include "lwip/sys.h"
45 #include "lwip/ip_addr.h"
46 #include "lwip/err.h"
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /* Throughout this file, IP addresses and port numbers are expected to be in
53  * the same byte order as in the corresponding pcb.
54  */
55 
56 /* Flags for netconn_write (u8_t) */
57 #define NETCONN_NOFLAG 0x00
58 #define NETCONN_NOCOPY 0x00 /* Only for source code compatibility */
59 #define NETCONN_COPY 0x01
60 #define NETCONN_MORE 0x02
61 #define NETCONN_DONTBLOCK 0x04
62 
63 /* Flags for struct netconn.flags (u8_t) */
65 #define NETCONN_FLAG_NON_BLOCKING 0x02
66 
67 #define NETCONN_FLAG_IN_NONBLOCKING_CONNECT 0x04
68 
70 #define NETCONN_FLAG_NO_AUTO_RECVED 0x08
71 
73 #define NETCONN_FLAG_CHECK_WRITESPACE 0x10
74 #if LWIP_IPV6
75 
78 #define NETCONN_FLAG_IPV6_V6ONLY 0x20
79 #endif /* LWIP_IPV6 */
80 
81 
82 /* Helpers to process several netconn_types by the same code */
83 #define NETCONNTYPE_GROUP(t) ((t)&0xF0)
84 #define NETCONNTYPE_DATAGRAM(t) ((t)&0xE0)
85 #if LWIP_IPV6
86 #define NETCONN_TYPE_IPV6 0x08
87 #define NETCONNTYPE_ISIPV6(t) ((t)&0x08)
88 #define NETCONNTYPE_ISUDPLITE(t) (((t)&0xF7) == NETCONN_UDPLITE)
89 #define NETCONNTYPE_ISUDPNOCHKSUM(t) (((t)&0xF7) == NETCONN_UDPNOCHKSUM)
90 #else /* LWIP_IPV6 */
91 #define NETCONNTYPE_ISUDPLITE(t) ((t) == NETCONN_UDPLITE)
92 #define NETCONNTYPE_ISUDPNOCHKSUM(t) ((t) == NETCONN_UDPNOCHKSUM)
93 #endif /* LWIP_IPV6 */
94 
96 enum netconn_type {
97  NETCONN_INVALID = 0,
98  /* NETCONN_TCP Group */
99  NETCONN_TCP = 0x10,
100 #if LWIP_IPV6
101  NETCONN_TCP_IPV6 = NETCONN_TCP | NETCONN_TYPE_IPV6 /* 0x18 */,
102 #endif /* LWIP_IPV6 */
103  /* NETCONN_UDP Group */
104  NETCONN_UDP = 0x20,
105  NETCONN_UDPLITE = 0x21,
106  NETCONN_UDPNOCHKSUM = 0x22,
107 #if LWIP_IPV6
108  NETCONN_UDP_IPV6 = NETCONN_UDP | NETCONN_TYPE_IPV6 /* 0x28 */,
109  NETCONN_UDPLITE_IPV6 = NETCONN_UDPLITE | NETCONN_TYPE_IPV6 /* 0x29 */,
110  NETCONN_UDPNOCHKSUM_IPV6 = NETCONN_UDPNOCHKSUM | NETCONN_TYPE_IPV6 /* 0x2a */,
111 #endif /* LWIP_IPV6 */
112  /* NETCONN_RAW Group */
113  NETCONN_RAW = 0x40
114 #if LWIP_IPV6
115  ,
116  NETCONN_RAW_IPV6 = NETCONN_RAW | NETCONN_TYPE_IPV6 /* 0x48 */
117 #endif /* LWIP_IPV6 */
118 };
119 
122 enum netconn_state {
123  NETCONN_NONE,
124  NETCONN_WRITE,
125  NETCONN_LISTEN,
126  NETCONN_CONNECT,
127  NETCONN_CLOSE
128 };
129 
131 enum netconn_evt {
132  NETCONN_EVT_RCVPLUS,
133  NETCONN_EVT_RCVMINUS,
134  NETCONN_EVT_SENDPLUS,
135  NETCONN_EVT_SENDMINUS,
136  NETCONN_EVT_ERROR
137 };
138 
139 #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
140 
141 enum netconn_igmp {
142  NETCONN_JOIN,
143  NETCONN_LEAVE
144 };
145 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
146 
147 #if LWIP_DNS
148 /* Used for netconn_gethostbyname_addrtype(), these should match the DNS_ADDRTYPE defines in dns.h */
149 #define NETCONN_DNS_DEFAULT NETCONN_DNS_IPV4_IPV6
150 #define NETCONN_DNS_IPV4 0
151 #define NETCONN_DNS_IPV6 1
152 #define NETCONN_DNS_IPV4_IPV6 2 /* try to resolve IPv4 first, try IPv6 if IPv4 fails only */
153 #define NETCONN_DNS_IPV6_IPV4 3 /* try to resolve IPv6 first, try IPv4 if IPv6 fails only */
154 #endif /* LWIP_DNS */
155 
156 /* forward-declare some structs to avoid to include their headers */
157 struct ip_pcb;
158 struct tcp_pcb;
159 struct udp_pcb;
160 struct raw_pcb;
161 struct netconn;
162 struct api_msg_msg;
163 
165 typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len);
166 
168 struct netconn {
170  enum netconn_type type;
172  enum netconn_state state;
174  union {
175  struct ip_pcb *ip;
176  struct tcp_pcb *tcp;
177  struct udp_pcb *udp;
178  struct raw_pcb *raw;
179  } pcb;
181  err_t last_err;
182 #if !LWIP_NETCONN_SEM_PER_THREAD
183 
184  sys_sem_t op_completed;
185 #endif
186 
188  sys_mbox_t recvmbox;
189 #if LWIP_TCP
190 
192  sys_mbox_t acceptmbox;
193 #endif /* LWIP_TCP */
194 
195 #if LWIP_SOCKET
196  int socket;
197 #endif /* LWIP_SOCKET */
198 #if LWIP_SO_SNDTIMEO
199 
201  s32_t send_timeout;
202 #endif /* LWIP_SO_RCVTIMEO */
203 #if LWIP_SO_RCVTIMEO
204 
206  int recv_timeout;
207 #endif /* LWIP_SO_RCVTIMEO */
208 #if LWIP_SO_RCVBUF
209 
211  int recv_bufsize;
215  int recv_avail;
216 #endif /* LWIP_SO_RCVBUF */
217 #if LWIP_SO_LINGER
218 
219  s16_t linger;
220 #endif /* LWIP_SO_LINGER */
221 
222  u8_t flags;
223 #if LWIP_TCP
224 
226  size_t write_offset;
230  struct api_msg_msg *current_msg;
231 #endif /* LWIP_TCP */
232 
233  netconn_callback callback;
234 };
235 
237 #define API_EVENT(c,e,l) if (c->callback) { \
238  (*c->callback)(c, e, l); \
239  }
240 
242 #define NETCONN_SET_SAFE_ERR(conn, err) do { if ((conn) != NULL) { \
243  SYS_ARCH_DECL_PROTECT(netconn_set_safe_err_lev); \
244  SYS_ARCH_PROTECT(netconn_set_safe_err_lev); \
245  if (!ERR_IS_FATAL((conn)->last_err)) { \
246  (conn)->last_err = err; \
247  } \
248  SYS_ARCH_UNPROTECT(netconn_set_safe_err_lev); \
249 }} while(0);
250 
251 /* Network connection functions: */
252 #define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL)
253 #define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c)
254 struct netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto,
255  netconn_callback callback);
256 err_t netconn_delete(struct netconn *conn);
258 #define netconn_type(conn) (conn->type)
259 
260 err_t netconn_getaddr(struct netconn *conn, ip_addr_t *addr,
261  u16_t *port, u8_t local);
262 #define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0)
263 #define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1)
264 
265 err_t netconn_bind(struct netconn *conn, const ip_addr_t *addr, u16_t port);
266 err_t netconn_connect(struct netconn *conn, const ip_addr_t *addr, u16_t port);
267 err_t netconn_disconnect (struct netconn *conn);
268 err_t netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
269 #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
270 err_t netconn_accept(struct netconn *conn, struct netconn **new_conn);
271 err_t netconn_recv(struct netconn *conn, struct netbuf **new_buf);
272 err_t netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf);
273 void netconn_recved(struct netconn *conn, u32_t length);
274 err_t netconn_sendto(struct netconn *conn, struct netbuf *buf,
275  const ip_addr_t *addr, u16_t port);
276 err_t netconn_send(struct netconn *conn, struct netbuf *buf);
277 err_t netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size,
278  u8_t apiflags, size_t *bytes_written);
279 #define netconn_write(conn, dataptr, size, apiflags) \
280  netconn_write_partly(conn, dataptr, size, apiflags, NULL)
281 err_t netconn_close(struct netconn *conn);
282 err_t netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx);
283 
284 #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
285 err_t netconn_join_leave_group(struct netconn *conn, const ip_addr_t *multiaddr,
286  const ip_addr_t *netif_addr, enum netconn_igmp join_or_leave);
287 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
288 #if LWIP_DNS
289 #if LWIP_IPV4 && LWIP_IPV6
290 err_t netconn_gethostbyname_addrtype(const char *name, ip_addr_t *addr, u8_t dns_addrtype);
291 #define netconn_gethostbyname(name, addr) netconn_gethostbyname_addrtype(name, addr, NETCONN_DNS_DEFAULT)
292 #else /* LWIP_IPV4 && LWIP_IPV6 */
293 err_t netconn_gethostbyname(const char *name, ip_addr_t *addr);
294 #define netconn_gethostbyname_addrtype(name, addr, dns_addrtype) netconn_gethostbyname(name, addr)
295 #endif /* LWIP_IPV4 && LWIP_IPV6 */
296 #endif /* LWIP_DNS */
297 
298 #define netconn_err(conn) ((conn)->last_err)
299 #define netconn_recv_bufsize(conn) ((conn)->recv_bufsize)
300 
302 #define netconn_set_nonblocking(conn, val) do { if(val) { \
303  (conn)->flags |= NETCONN_FLAG_NON_BLOCKING; \
304 } else { \
305  (conn)->flags &= ~ NETCONN_FLAG_NON_BLOCKING; }} while(0)
306 
307 #define netconn_is_nonblocking(conn) (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)
308 
310 #define netconn_set_noautorecved(conn, val) do { if(val) { \
311  (conn)->flags |= NETCONN_FLAG_NO_AUTO_RECVED; \
312 } else { \
313  (conn)->flags &= ~ NETCONN_FLAG_NO_AUTO_RECVED; }} while(0)
314 
315 #define netconn_get_noautorecved(conn) (((conn)->flags & NETCONN_FLAG_NO_AUTO_RECVED) != 0)
316 
317 #if LWIP_SO_SNDTIMEO
318 
319 #define netconn_set_sendtimeout(conn, timeout) ((conn)->send_timeout = (timeout))
320 
321 #define netconn_get_sendtimeout(conn) ((conn)->send_timeout)
322 #endif /* LWIP_SO_SNDTIMEO */
323 #if LWIP_SO_RCVTIMEO
324 
325 #define netconn_set_recvtimeout(conn, timeout) ((conn)->recv_timeout = (timeout))
326 
327 #define netconn_get_recvtimeout(conn) ((conn)->recv_timeout)
328 #endif /* LWIP_SO_RCVTIMEO */
329 #if LWIP_SO_RCVBUF
330 
331 #define netconn_set_recvbufsize(conn, recvbufsize) ((conn)->recv_bufsize = (recvbufsize))
332 
333 #define netconn_get_recvbufsize(conn) ((conn)->recv_bufsize)
334 #endif /* LWIP_SO_RCVBUF*/
335 
336 #if LWIP_NETCONN_SEM_PER_THREAD
337 void netconn_thread_init(void);
338 void netconn_thread_cleanup(void);
339 #else /* LWIP_NETCONN_SEM_PER_THREAD */
340 #define netconn_thread_init()
341 #define netconn_thread_cleanup()
342 #endif /* LWIP_NETCONN_SEM_PER_THREAD */
343 
344 #ifdef __cplusplus
345 }
346 #endif
347 
348 #endif /* LWIP_NETCONN || LWIP_SOCKET */
349 
350 #endif /* LWIP_HDR_API_H */
351 
352 
signed short s16_t
Definition: cc.h:41
osMessageQId sys_mbox_t
Definition: sys_arch.h:46
unsigned long u32_t
Definition: cc.h:42
Definition: pbuf.h:108
s8_t err_t
Definition: err.h:47
osSemaphoreId sys_sem_t
Definition: sys_arch.h:44
unsigned char u8_t
Definition: cc.h:38
ip6_addr_t ip_addr_t
Definition: ip_addr.h:194
signed long s32_t
Definition: cc.h:43
Definition: ip.h:110
unsigned short u16_t
Definition: cc.h:40