1 /* This header file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */
2 
3 /*
4  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  *    this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
21  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
23  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27  * OF SUCH DAMAGE.
28  *
29  * This file is part of the lwIP TCP/IP stack.
30  *
31  * Author: Adam Dunkels <adam@sics.se>
32  *
33  */
34 #ifndef __LWIP_API_H__
35 #define __LWIP_API_H__
36 
37 #include "lwip/opt.h"
38 
39 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
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 */
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 
62 /* Helpers to process several netconn_types by the same code */
63 #define NETCONNTYPE_GROUP(t)    (t&0xF0)
64 #define NETCONNTYPE_DATAGRAM(t) (t&0xE0)
65 
66 enum netconn_type {
67   NETCONN_INVALID    = 0,
68   /* NETCONN_TCP Group */
69   NETCONN_TCP        = 0x10,
70   /* NETCONN_UDP Group */
71   NETCONN_UDP        = 0x20,
72   NETCONN_UDPLITE    = 0x21,
73   NETCONN_UDPNOCHKSUM= 0x22,
74   /* NETCONN_RAW Group */
75   NETCONN_RAW        = 0x40
76 };
77 
78 enum netconn_state {
79   NETCONN_NONE,
80   NETCONN_WRITE,
81   NETCONN_LISTEN,
82   NETCONN_CONNECT,
83   NETCONN_CLOSE
84 };
85 
86 enum netconn_evt {
87   NETCONN_EVT_RCVPLUS,
88   NETCONN_EVT_RCVMINUS,
89   NETCONN_EVT_SENDPLUS,
90   NETCONN_EVT_SENDMINUS
91 };
92 
93 #if LWIP_IGMP
94 enum netconn_igmp {
95   NETCONN_JOIN,
96   NETCONN_LEAVE
97 };
98 #endif /* LWIP_IGMP */
99 
100 /* forward-declare some structs to avoid to include their headers */
101 struct ip_pcb;
102 struct tcp_pcb;
103 struct udp_pcb;
104 struct raw_pcb;
105 struct netconn;
106 
107 /** A callback prototype to inform about events for a netconn */
108 typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len);
109 
110 /** A netconn descriptor */
111 struct netconn {
112   /** type of the netconn (TCP, UDP or RAW) */
113   enum netconn_type type;
114   /** current state of the netconn */
115   enum netconn_state state;
116   /** the lwIP internal protocol control block */
117   union {
118     struct ip_pcb  *ip;
119     struct tcp_pcb *tcp;
120     struct udp_pcb *udp;
121     struct raw_pcb *raw;
122   } pcb;
123   /** the last error this netconn had */
124   err_t err;
125   /** sem that is used to synchroneously execute functions in the core context */
126   sys_sem_t op_completed;
127   /** mbox where received packets are stored until they are fetched
128       by the netconn application thread (can grow quite big) */
129   sys_mbox_t recvmbox;
130   /** mbox where new connections are stored until processed
131       by the application thread */
132   sys_mbox_t acceptmbox;
133   /** only used for socket layer */
134   int socket;
135 #if LWIP_SO_RCVTIMEO
136   /** timeout to wait for new data to be received
137       (or connections to arrive for listening netconns) */
138   int recv_timeout;
139 #endif /* LWIP_SO_RCVTIMEO */
140 #if LWIP_SO_RCVBUF
141   /** maximum amount of bytes queued in recvmbox */
142   int recv_bufsize;
143 #endif /* LWIP_SO_RCVBUF */
144   s16_t recv_avail;
145 #if LWIP_TCP
146   /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
147       this temporarily stores the message. */
148   struct api_msg_msg *write_msg;
149   /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
150       this temporarily stores how much is already sent. */
151   size_t write_offset;
152 #if LWIP_TCPIP_CORE_LOCKING
153   /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
154       this temporarily stores whether to wake up the original application task
155       if data couldn't be sent in the first try. */
156   u8_t write_delayed;
157 #endif /* LWIP_TCPIP_CORE_LOCKING */
158 #endif /* LWIP_TCP */
159   /** A callback function that is informed about events for this netconn */
160   netconn_callback callback;
161 };
162 
163 /* Register an Network connection event */
164 #define API_EVENT(c,e,l) if (c->callback) {         \
165                            (*c->callback)(c, e, l); \
166                          }
167 
168 /* Network connection functions: */
169 #define netconn_new(t)                  netconn_new_with_proto_and_callback(t, 0, NULL)
170 #define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c)
171 struct
172 netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto,
173                                    netconn_callback callback);
174 err_t             netconn_delete  (struct netconn *conn);
175 /** Get the type of a netconn (as enum netconn_type). */
176 #define netconn_type(conn) (conn->type)
177 
178 err_t             netconn_getaddr (struct netconn *conn,
179                                    struct ip_addr *addr,
180                                    u16_t *port,
181                                    u8_t local);
182 #define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0)
183 #define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1)
184 
185 err_t             netconn_bind    (struct netconn *conn,
186                                    struct ip_addr *addr,
187                                    u16_t port);
188 err_t             netconn_connect (struct netconn *conn,
189                                    struct ip_addr *addr,
190                                    u16_t port);
191 err_t             netconn_disconnect (struct netconn *conn);
192 err_t             netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
193 #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
194 struct netconn *  netconn_accept  (struct netconn *conn);
195 struct netbuf *   netconn_recv    (struct netconn *conn);
196 err_t             netconn_sendto  (struct netconn *conn,
197                                    struct netbuf *buf, struct ip_addr *addr, u16_t port);
198 err_t             netconn_send    (struct netconn *conn,
199                                    struct netbuf *buf);
200 err_t             netconn_write   (struct netconn *conn,
201                                    const void *dataptr, size_t size,
202                                    u8_t apiflags);
203 err_t             netconn_close   (struct netconn *conn);
204 
205 #if LWIP_IGMP
206 err_t             netconn_join_leave_group (struct netconn *conn,
207                                             struct ip_addr *multiaddr,
208                                             struct ip_addr *interface,
209                                             enum netconn_igmp join_or_leave);
210 #endif /* LWIP_IGMP */
211 #if LWIP_DNS
212 err_t             netconn_gethostbyname(const char *name, struct ip_addr *addr);
213 #endif /* LWIP_DNS */
214 
215 #define netconn_err(conn)          ((conn)->err)
216 #define netconn_recv_bufsize(conn) ((conn)->recv_bufsize)
217 
218 #ifdef __cplusplus
219 }
220 #endif
221 
222 #endif /* LWIP_NETCONN */
223 
224 #endif /* __LWIP_API_H__ */
225