A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers api.h Source File

api.h

00001 /*
00002  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00003  * All rights reserved. 
00004  * 
00005  * Redistribution and use in source and binary forms, with or without modification, 
00006  * are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  *    this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright notice,
00011  *    this list of conditions and the following disclaimer in the documentation
00012  *    and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission. 
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00017  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00018  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00019  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00021  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00024  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00025  * OF SUCH DAMAGE.
00026  *
00027  * This file is part of the lwIP TCP/IP stack.
00028  * 
00029  * Author: Adam Dunkels <adam@sics.se>
00030  *
00031  */
00032 #ifndef __LWIP_API_H__
00033 #define __LWIP_API_H__
00034 
00035 #include "lwip/opt.h"
00036 
00037 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
00038 
00039 #include "lwip/netbuf.h"
00040 #include "lwip/sys.h"
00041 #include "lwip/ip_addr.h"
00042 #include "lwip/err.h"
00043 
00044 #ifdef __cplusplus
00045 extern "C" {
00046 #endif
00047 
00048 /* Throughout this file, IP addresses and port numbers are expected to be in
00049  * the same byte order as in the corresponding pcb.
00050  */
00051 
00052 /* Flags for netconn_write */
00053 #define NETCONN_NOFLAG 0x00
00054 #define NETCONN_NOCOPY 0x00 /* Only for source code compatibility */
00055 #define NETCONN_COPY   0x01
00056 #define NETCONN_MORE   0x02
00057 
00058 /* Helpers to process several netconn_types by the same code */
00059 #define NETCONNTYPE_GROUP(t)    (t&0xF0)
00060 #define NETCONNTYPE_DATAGRAM(t) (t&0xE0)
00061 
00062 enum netconn_type {
00063   NETCONN_INVALID    = 0,
00064   /* NETCONN_TCP Group */
00065   NETCONN_TCP        = 0x10,
00066   /* NETCONN_UDP Group */
00067   NETCONN_UDP        = 0x20,
00068   NETCONN_UDPLITE    = 0x21,
00069   NETCONN_UDPNOCHKSUM= 0x22,
00070   /* NETCONN_RAW Group */
00071   NETCONN_RAW        = 0x40
00072 };
00073 
00074 enum netconn_state {
00075   NETCONN_NONE,
00076   NETCONN_WRITE,
00077   NETCONN_LISTEN,
00078   NETCONN_CONNECT,
00079   NETCONN_CLOSE
00080 };
00081 
00082 enum netconn_evt {
00083   NETCONN_EVT_RCVPLUS,
00084   NETCONN_EVT_RCVMINUS,
00085   NETCONN_EVT_SENDPLUS,
00086   NETCONN_EVT_SENDMINUS
00087 };
00088 
00089 #if LWIP_IGMP
00090 enum netconn_igmp {
00091   NETCONN_JOIN,
00092   NETCONN_LEAVE
00093 };
00094 #endif /* LWIP_IGMP */
00095 
00096 /* forward-declare some structs to avoid to include their headers */
00097 struct ip_pcb;
00098 struct tcp_pcb;
00099 struct udp_pcb;
00100 struct raw_pcb;
00101 struct netconn;
00102 
00103 /** A callback prototype to inform about events for a netconn */
00104 typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len);
00105 
00106 /** A netconn descriptor */
00107 struct netconn {
00108   /** type of the netconn (TCP, UDP or RAW) */
00109   enum netconn_type type;
00110   /** current state of the netconn */
00111   enum netconn_state state;
00112   /** the lwIP internal protocol control block */
00113   union {
00114     struct ip_pcb  *ip;
00115     struct tcp_pcb *tcp;
00116     struct udp_pcb *udp;
00117     struct raw_pcb *raw;
00118   } pcb;
00119   /** the last error this netconn had */
00120   err_t err;
00121   /** sem that is used to synchroneously execute functions in the core context */
00122   sys_sem_t op_completed;
00123   /** mbox where received packets are stored until they are fetched
00124       by the netconn application thread (can grow quite big) */
00125   sys_mbox_t recvmbox;
00126   /** mbox where new connections are stored until processed
00127       by the application thread */
00128   sys_mbox_t acceptmbox;
00129   /** only used for socket layer */
00130   int socket;
00131 #if LWIP_SO_RCVTIMEO
00132   /** timeout to wait for new data to be received
00133       (or connections to arrive for listening netconns) */
00134   int recv_timeout;
00135 #endif /* LWIP_SO_RCVTIMEO */
00136 #if LWIP_SO_RCVBUF
00137   /** maximum amount of bytes queued in recvmbox */
00138   int recv_bufsize;
00139 #endif /* LWIP_SO_RCVBUF */
00140   u16_t recv_avail;
00141   /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
00142       this temporarily stores the message. */
00143   struct api_msg_msg *write_msg;
00144 #if LWIP_TCP
00145   /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
00146       this temporarily stores how much is already sent. */
00147   size_t write_offset;
00148 #endif /* LWIP_TCP */
00149 #if LWIP_TCPIP_CORE_LOCKING
00150   /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
00151       this temporarily stores whether to wake up the original application task
00152       if data couldn't be sent in the first try. */
00153   u8_t write_delayed;
00154 #endif /* LWIP_TCPIP_CORE_LOCKING */
00155   /** A callback function that is informed about events for this netconn */
00156   netconn_callback callback;
00157 };
00158 
00159 /* Register an Network connection event */
00160 #define API_EVENT(c,e,l) if (c->callback) {         \
00161                            (*c->callback)(c, e, l); \
00162                          }
00163 
00164 /* Network connection functions: */
00165 #define netconn_new(t)                  netconn_new_with_proto_and_callback(t, 0, NULL)
00166 #define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c)
00167 struct
00168 netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto,
00169                                    netconn_callback callback);
00170 err_t             netconn_delete  (struct netconn *conn);
00171 /** Get the type of a netconn (as enum netconn_type). */
00172 #define netconn_type(conn) (conn->type)
00173 
00174 err_t             netconn_getaddr (struct netconn *conn,
00175                                    struct ip_addr *addr,
00176                                    u16_t *port,
00177                                    u8_t local);
00178 #define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0)
00179 #define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1)
00180 
00181 err_t             netconn_bind    (struct netconn *conn,
00182                                    struct ip_addr *addr,
00183                                    u16_t port);
00184 err_t             netconn_connect (struct netconn *conn,
00185                                    struct ip_addr *addr,
00186                                    u16_t port);
00187 err_t             netconn_disconnect (struct netconn *conn);
00188 err_t             netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
00189 #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
00190 struct netconn *  netconn_accept  (struct netconn *conn);
00191 struct netbuf *   netconn_recv    (struct netconn *conn);
00192 err_t             netconn_sendto  (struct netconn *conn,
00193                                    struct netbuf *buf, struct ip_addr *addr, u16_t port);
00194 err_t             netconn_send    (struct netconn *conn,
00195                                    struct netbuf *buf);
00196 err_t             netconn_write   (struct netconn *conn,
00197                                    const void *dataptr, size_t size,
00198                                    u8_t apiflags);
00199 err_t             netconn_close   (struct netconn *conn);
00200 
00201 #if LWIP_IGMP
00202 err_t             netconn_join_leave_group (struct netconn *conn,
00203                                             struct ip_addr *multiaddr,
00204                                             struct ip_addr *interface,
00205                                             enum netconn_igmp join_or_leave);
00206 #endif /* LWIP_IGMP */
00207 #if LWIP_DNS
00208 err_t             netconn_gethostbyname(const char *name, struct ip_addr *addr);
00209 #endif /* LWIP_DNS */
00210 
00211 #define netconn_err(conn)          ((conn)->err)
00212 #define netconn_recv_bufsize(conn) ((conn)->recv_bufsize)
00213 
00214 #ifdef __cplusplus
00215 }
00216 #endif
00217 
00218 #endif /* LWIP_NETCONN */
00219 
00220 #endif /* __LWIP_API_H__ */