f6b1f74343efe6a473f79b50c19e3ca9da58e23e
[bertos.git] / api.h
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_API_H__
33 #define __LWIP_API_H__
34
35 #include "lwip/opt.h"
36
37 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
38
39 #include <stddef.h> /* for size_t */
40
41 #include "lwip/netbuf.h"
42 #include "lwip/sys.h"
43 #include "lwip/ip_addr.h"
44 #include "lwip/err.h"
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 /* Throughout this file, IP addresses and port numbers are expected to be in
51  * the same byte order as in the corresponding pcb.
52  */
53
54 /* Flags for netconn_write */
55 #define NETCONN_NOFLAG 0x00
56 #define NETCONN_NOCOPY 0x00 /* Only for source code compatibility */
57 #define NETCONN_COPY   0x01
58 #define NETCONN_MORE   0x02
59
60 /* Helpers to process several netconn_types by the same code */
61 #define NETCONNTYPE_GROUP(t)    (t&0xF0)
62 #define NETCONNTYPE_DATAGRAM(t) (t&0xE0)
63
64 enum netconn_type {
65   NETCONN_INVALID    = 0,
66   /* NETCONN_TCP Group */
67   NETCONN_TCP        = 0x10,
68   /* NETCONN_UDP Group */
69   NETCONN_UDP        = 0x20,
70   NETCONN_UDPLITE    = 0x21,
71   NETCONN_UDPNOCHKSUM= 0x22,
72   /* NETCONN_RAW Group */
73   NETCONN_RAW        = 0x40
74 };
75
76 enum netconn_state {
77   NETCONN_NONE,
78   NETCONN_WRITE,
79   NETCONN_LISTEN,
80   NETCONN_CONNECT,
81   NETCONN_CLOSE
82 };
83
84 enum netconn_evt {
85   NETCONN_EVT_RCVPLUS,
86   NETCONN_EVT_RCVMINUS,
87   NETCONN_EVT_SENDPLUS,
88   NETCONN_EVT_SENDMINUS
89 };
90
91 #if LWIP_IGMP
92 enum netconn_igmp {
93   NETCONN_JOIN,
94   NETCONN_LEAVE
95 };
96 #endif /* LWIP_IGMP */
97
98 /* forward-declare some structs to avoid to include their headers */
99 struct ip_pcb;
100 struct tcp_pcb;
101 struct udp_pcb;
102 struct raw_pcb;
103 struct netconn;
104
105 /** A callback prototype to inform about events for a netconn */
106 typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len);
107
108 /** A netconn descriptor */
109 struct netconn {
110   /** type of the netconn (TCP, UDP or RAW) */
111   enum netconn_type type;
112   /** current state of the netconn */
113   enum netconn_state state;
114   /** the lwIP internal protocol control block */
115   union {
116     struct ip_pcb  *ip;
117     struct tcp_pcb *tcp;
118     struct udp_pcb *udp;
119     struct raw_pcb *raw;
120   } pcb;
121   /** the last error this netconn had */
122   err_t err;
123   /** sem that is used to synchroneously execute functions in the core context */
124   sys_sem_t op_completed;
125   /** mbox where received packets are stored until they are fetched
126       by the netconn application thread (can grow quite big) */
127   sys_mbox_t recvmbox;
128   /** mbox where new connections are stored until processed
129       by the application thread */
130   sys_mbox_t acceptmbox;
131   /** only used for socket layer */
132   int socket;
133 #if LWIP_SO_RCVTIMEO
134   /** timeout to wait for new data to be received
135       (or connections to arrive for listening netconns) */
136   int recv_timeout;
137 #endif /* LWIP_SO_RCVTIMEO */
138 #if LWIP_SO_RCVBUF
139   /** maximum amount of bytes queued in recvmbox */
140   int recv_bufsize;
141 #endif /* LWIP_SO_RCVBUF */
142   s16_t recv_avail;
143 #if LWIP_TCP
144   /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
145       this temporarily stores the message. */
146   struct api_msg_msg *write_msg;
147   /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
148       this temporarily stores how much is already sent. */
149   size_t write_offset;
150 #if LWIP_TCPIP_CORE_LOCKING
151   /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
152       this temporarily stores whether to wake up the original application task
153       if data couldn't be sent in the first try. */
154   u8_t write_delayed;
155 #endif /* LWIP_TCPIP_CORE_LOCKING */
156 #endif /* LWIP_TCP */
157   /** A callback function that is informed about events for this netconn */
158   netconn_callback callback;
159 };
160
161 /* Register an Network connection event */
162 #define API_EVENT(c,e,l) if (c->callback) {         \
163                            (*c->callback)(c, e, l); \
164                          }
165
166 /* Network connection functions: */
167 #define netconn_new(t)                  netconn_new_with_proto_and_callback(t, 0, NULL)
168 #define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c)
169 struct
170 netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto,
171                                    netconn_callback callback);
172 err_t             netconn_delete  (struct netconn *conn);
173 /** Get the type of a netconn (as enum netconn_type). */
174 #define netconn_type(conn) (conn->type)
175
176 err_t             netconn_getaddr (struct netconn *conn,
177                                    struct ip_addr *addr,
178                                    u16_t *port,
179                                    u8_t local);
180 #define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0)
181 #define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1)
182
183 err_t             netconn_bind    (struct netconn *conn,
184                                    struct ip_addr *addr,
185                                    u16_t port);
186 err_t             netconn_connect (struct netconn *conn,
187                                    struct ip_addr *addr,
188                                    u16_t port);
189 err_t             netconn_disconnect (struct netconn *conn);
190 err_t             netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
191 #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
192 struct netconn *  netconn_accept  (struct netconn *conn);
193 struct netbuf *   netconn_recv    (struct netconn *conn);
194 err_t             netconn_sendto  (struct netconn *conn,
195                                    struct netbuf *buf, struct ip_addr *addr, u16_t port);
196 err_t             netconn_send    (struct netconn *conn,
197                                    struct netbuf *buf);
198 err_t             netconn_write   (struct netconn *conn,
199                                    const void *dataptr, size_t size,
200                                    u8_t apiflags);
201 err_t             netconn_close   (struct netconn *conn);
202
203 #if LWIP_IGMP
204 err_t             netconn_join_leave_group (struct netconn *conn,
205                                             struct ip_addr *multiaddr,
206                                             struct ip_addr *interface,
207                                             enum netconn_igmp join_or_leave);
208 #endif /* LWIP_IGMP */
209 #if LWIP_DNS
210 err_t             netconn_gethostbyname(const char *name, struct ip_addr *addr);
211 #endif /* LWIP_DNS */
212
213 #define netconn_err(conn)          ((conn)->err)
214 #define netconn_recv_bufsize(conn) ((conn)->recv_bufsize)
215
216 #ifdef __cplusplus
217 }
218 #endif
219
220 #endif /* LWIP_NETCONN */
221
222 #endif /* __LWIP_API_H__ */