Dependents:   RTnoV3 RTnoV3_LED RTnoV3_Template RTnoV3_ADC ... more

Committer:
sherckuith
Date:
Sat Dec 31 11:25:27 2011 +0000
Revision:
0:479ce5546098
Ethernet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sherckuith 0:479ce5546098 1 /*
sherckuith 0:479ce5546098 2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
sherckuith 0:479ce5546098 3 * All rights reserved.
sherckuith 0:479ce5546098 4 *
sherckuith 0:479ce5546098 5 * Redistribution and use in source and binary forms, with or without modification,
sherckuith 0:479ce5546098 6 * are permitted provided that the following conditions are met:
sherckuith 0:479ce5546098 7 *
sherckuith 0:479ce5546098 8 * 1. Redistributions of source code must retain the above copyright notice,
sherckuith 0:479ce5546098 9 * this list of conditions and the following disclaimer.
sherckuith 0:479ce5546098 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
sherckuith 0:479ce5546098 11 * this list of conditions and the following disclaimer in the documentation
sherckuith 0:479ce5546098 12 * and/or other materials provided with the distribution.
sherckuith 0:479ce5546098 13 * 3. The name of the author may not be used to endorse or promote products
sherckuith 0:479ce5546098 14 * derived from this software without specific prior written permission.
sherckuith 0:479ce5546098 15 *
sherckuith 0:479ce5546098 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
sherckuith 0:479ce5546098 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
sherckuith 0:479ce5546098 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
sherckuith 0:479ce5546098 19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
sherckuith 0:479ce5546098 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
sherckuith 0:479ce5546098 21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
sherckuith 0:479ce5546098 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
sherckuith 0:479ce5546098 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
sherckuith 0:479ce5546098 24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
sherckuith 0:479ce5546098 25 * OF SUCH DAMAGE.
sherckuith 0:479ce5546098 26 *
sherckuith 0:479ce5546098 27 * This file is part of the lwIP TCP/IP stack.
sherckuith 0:479ce5546098 28 *
sherckuith 0:479ce5546098 29 * Author: Adam Dunkels <adam@sics.se>
sherckuith 0:479ce5546098 30 *
sherckuith 0:479ce5546098 31 */
sherckuith 0:479ce5546098 32 #ifndef __LWIP_API_H__
sherckuith 0:479ce5546098 33 #define __LWIP_API_H__
sherckuith 0:479ce5546098 34
sherckuith 0:479ce5546098 35 #include "lwip/opt.h"
sherckuith 0:479ce5546098 36
sherckuith 0:479ce5546098 37 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
sherckuith 0:479ce5546098 38
sherckuith 0:479ce5546098 39 #include <stddef.h> /* for size_t */
sherckuith 0:479ce5546098 40
sherckuith 0:479ce5546098 41 #include "lwip/netbuf.h"
sherckuith 0:479ce5546098 42 #include "lwip/sys.h"
sherckuith 0:479ce5546098 43 #include "lwip/ip_addr.h"
sherckuith 0:479ce5546098 44 #include "lwip/err.h"
sherckuith 0:479ce5546098 45
sherckuith 0:479ce5546098 46 #ifdef __cplusplus
sherckuith 0:479ce5546098 47 extern "C" {
sherckuith 0:479ce5546098 48 #endif
sherckuith 0:479ce5546098 49
sherckuith 0:479ce5546098 50 /* Throughout this file, IP addresses and port numbers are expected to be in
sherckuith 0:479ce5546098 51 * the same byte order as in the corresponding pcb.
sherckuith 0:479ce5546098 52 */
sherckuith 0:479ce5546098 53
sherckuith 0:479ce5546098 54 /* Flags for netconn_write (u8_t) */
sherckuith 0:479ce5546098 55 #define NETCONN_NOFLAG 0x00
sherckuith 0:479ce5546098 56 #define NETCONN_NOCOPY 0x00 /* Only for source code compatibility */
sherckuith 0:479ce5546098 57 #define NETCONN_COPY 0x01
sherckuith 0:479ce5546098 58 #define NETCONN_MORE 0x02
sherckuith 0:479ce5546098 59 #define NETCONN_DONTBLOCK 0x04
sherckuith 0:479ce5546098 60
sherckuith 0:479ce5546098 61 /* Flags for struct netconn.flags (u8_t) */
sherckuith 0:479ce5546098 62 /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
sherckuith 0:479ce5546098 63 this temporarily stores whether to wake up the original application task
sherckuith 0:479ce5546098 64 if data couldn't be sent in the first try. */
sherckuith 0:479ce5546098 65 #define NETCONN_FLAG_WRITE_DELAYED 0x01
sherckuith 0:479ce5546098 66 /** Should this netconn avoid blocking? */
sherckuith 0:479ce5546098 67 #define NETCONN_FLAG_NON_BLOCKING 0x02
sherckuith 0:479ce5546098 68 /** Was the last connect action a non-blocking one? */
sherckuith 0:479ce5546098 69 #define NETCONN_FLAG_IN_NONBLOCKING_CONNECT 0x04
sherckuith 0:479ce5546098 70 /** If this is set, a TCP netconn must call netconn_recved() to update
sherckuith 0:479ce5546098 71 the TCP receive window (done automatically if not set). */
sherckuith 0:479ce5546098 72 #define NETCONN_FLAG_NO_AUTO_RECVED 0x08
sherckuith 0:479ce5546098 73 /** If a nonblocking write has been rejected before, poll_tcp needs to
sherckuith 0:479ce5546098 74 check if the netconn is writable again */
sherckuith 0:479ce5546098 75 #define NETCONN_FLAG_CHECK_WRITESPACE 0x10
sherckuith 0:479ce5546098 76
sherckuith 0:479ce5546098 77
sherckuith 0:479ce5546098 78 /* Helpers to process several netconn_types by the same code */
sherckuith 0:479ce5546098 79 #define NETCONNTYPE_GROUP(t) (t&0xF0)
sherckuith 0:479ce5546098 80 #define NETCONNTYPE_DATAGRAM(t) (t&0xE0)
sherckuith 0:479ce5546098 81
sherckuith 0:479ce5546098 82 /** Protocol family and type of the netconn */
sherckuith 0:479ce5546098 83 enum netconn_type {
sherckuith 0:479ce5546098 84 NETCONN_INVALID = 0,
sherckuith 0:479ce5546098 85 /* NETCONN_TCP Group */
sherckuith 0:479ce5546098 86 NETCONN_TCP = 0x10,
sherckuith 0:479ce5546098 87 /* NETCONN_UDP Group */
sherckuith 0:479ce5546098 88 NETCONN_UDP = 0x20,
sherckuith 0:479ce5546098 89 NETCONN_UDPLITE = 0x21,
sherckuith 0:479ce5546098 90 NETCONN_UDPNOCHKSUM= 0x22,
sherckuith 0:479ce5546098 91 /* NETCONN_RAW Group */
sherckuith 0:479ce5546098 92 NETCONN_RAW = 0x40
sherckuith 0:479ce5546098 93 };
sherckuith 0:479ce5546098 94
sherckuith 0:479ce5546098 95 /** Current state of the netconn. Non-TCP netconns are always
sherckuith 0:479ce5546098 96 * in state NETCONN_NONE! */
sherckuith 0:479ce5546098 97 enum netconn_state {
sherckuith 0:479ce5546098 98 NETCONN_NONE,
sherckuith 0:479ce5546098 99 NETCONN_WRITE,
sherckuith 0:479ce5546098 100 NETCONN_LISTEN,
sherckuith 0:479ce5546098 101 NETCONN_CONNECT,
sherckuith 0:479ce5546098 102 NETCONN_CLOSE
sherckuith 0:479ce5546098 103 };
sherckuith 0:479ce5546098 104
sherckuith 0:479ce5546098 105 /** Use to inform the callback function about changes */
sherckuith 0:479ce5546098 106 enum netconn_evt {
sherckuith 0:479ce5546098 107 NETCONN_EVT_RCVPLUS,
sherckuith 0:479ce5546098 108 NETCONN_EVT_RCVMINUS,
sherckuith 0:479ce5546098 109 NETCONN_EVT_SENDPLUS,
sherckuith 0:479ce5546098 110 NETCONN_EVT_SENDMINUS,
sherckuith 0:479ce5546098 111 NETCONN_EVT_ERROR
sherckuith 0:479ce5546098 112 };
sherckuith 0:479ce5546098 113
sherckuith 0:479ce5546098 114 #if LWIP_IGMP
sherckuith 0:479ce5546098 115 /** Used for netconn_join_leave_group() */
sherckuith 0:479ce5546098 116 enum netconn_igmp {
sherckuith 0:479ce5546098 117 NETCONN_JOIN,
sherckuith 0:479ce5546098 118 NETCONN_LEAVE
sherckuith 0:479ce5546098 119 };
sherckuith 0:479ce5546098 120 #endif /* LWIP_IGMP */
sherckuith 0:479ce5546098 121
sherckuith 0:479ce5546098 122 /* forward-declare some structs to avoid to include their headers */
sherckuith 0:479ce5546098 123 struct ip_pcb;
sherckuith 0:479ce5546098 124 struct tcp_pcb;
sherckuith 0:479ce5546098 125 struct udp_pcb;
sherckuith 0:479ce5546098 126 struct raw_pcb;
sherckuith 0:479ce5546098 127 struct netconn;
sherckuith 0:479ce5546098 128 struct api_msg_msg;
sherckuith 0:479ce5546098 129
sherckuith 0:479ce5546098 130 /** A callback prototype to inform about events for a netconn */
sherckuith 0:479ce5546098 131 typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len);
sherckuith 0:479ce5546098 132
sherckuith 0:479ce5546098 133 /** A netconn descriptor */
sherckuith 0:479ce5546098 134 struct netconn {
sherckuith 0:479ce5546098 135 /** type of the netconn (TCP, UDP or RAW) */
sherckuith 0:479ce5546098 136 enum netconn_type type;
sherckuith 0:479ce5546098 137 /** current state of the netconn */
sherckuith 0:479ce5546098 138 enum netconn_state state;
sherckuith 0:479ce5546098 139 /** the lwIP internal protocol control block */
sherckuith 0:479ce5546098 140 union {
sherckuith 0:479ce5546098 141 struct ip_pcb *ip;
sherckuith 0:479ce5546098 142 struct tcp_pcb *tcp;
sherckuith 0:479ce5546098 143 struct udp_pcb *udp;
sherckuith 0:479ce5546098 144 struct raw_pcb *raw;
sherckuith 0:479ce5546098 145 } pcb;
sherckuith 0:479ce5546098 146 /** the last error this netconn had */
sherckuith 0:479ce5546098 147 err_t last_err;
sherckuith 0:479ce5546098 148 /** sem that is used to synchroneously execute functions in the core context */
sherckuith 0:479ce5546098 149 sys_sem_t op_completed;
sherckuith 0:479ce5546098 150 /** mbox where received packets are stored until they are fetched
sherckuith 0:479ce5546098 151 by the netconn application thread (can grow quite big) */
sherckuith 0:479ce5546098 152 sys_mbox_t recvmbox;
sherckuith 0:479ce5546098 153 #if LWIP_TCP
sherckuith 0:479ce5546098 154 /** mbox where new connections are stored until processed
sherckuith 0:479ce5546098 155 by the application thread */
sherckuith 0:479ce5546098 156 sys_mbox_t acceptmbox;
sherckuith 0:479ce5546098 157 #endif /* LWIP_TCP */
sherckuith 0:479ce5546098 158 /** only used for socket layer */
sherckuith 0:479ce5546098 159 #if LWIP_SOCKET
sherckuith 0:479ce5546098 160 int socket;
sherckuith 0:479ce5546098 161 #endif /* LWIP_SOCKET */
sherckuith 0:479ce5546098 162 #if LWIP_SO_RCVTIMEO
sherckuith 0:479ce5546098 163 /** timeout to wait for new data to be received
sherckuith 0:479ce5546098 164 (or connections to arrive for listening netconns) */
sherckuith 0:479ce5546098 165 int recv_timeout;
sherckuith 0:479ce5546098 166 #endif /* LWIP_SO_RCVTIMEO */
sherckuith 0:479ce5546098 167 #if LWIP_SO_RCVBUF
sherckuith 0:479ce5546098 168 /** maximum amount of bytes queued in recvmbox
sherckuith 0:479ce5546098 169 not used for TCP: adjust TCP_WND instead! */
sherckuith 0:479ce5546098 170 int recv_bufsize;
sherckuith 0:479ce5546098 171 #endif /* LWIP_SO_RCVBUF */
sherckuith 0:479ce5546098 172 /** number of bytes currently in recvmbox to be received,
sherckuith 0:479ce5546098 173 tested against recv_bufsize to limit bytes on recvmbox
sherckuith 0:479ce5546098 174 for UDP and RAW
sherckuith 0:479ce5546098 175 @todo: should only be necessary with LWIP_SO_RCVBUF==1 */
sherckuith 0:479ce5546098 176 s16_t recv_avail;
sherckuith 0:479ce5546098 177 /** flags holding more netconn-internal state, see NETCONN_FLAG_* defines */
sherckuith 0:479ce5546098 178 u8_t flags;
sherckuith 0:479ce5546098 179 #if LWIP_TCP
sherckuith 0:479ce5546098 180 /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
sherckuith 0:479ce5546098 181 this temporarily stores how much is already sent. */
sherckuith 0:479ce5546098 182 size_t write_offset;
sherckuith 0:479ce5546098 183 /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
sherckuith 0:479ce5546098 184 this temporarily stores the message.
sherckuith 0:479ce5546098 185 Also used during connect and close. */
sherckuith 0:479ce5546098 186 struct api_msg_msg *current_msg;
sherckuith 0:479ce5546098 187 #endif /* LWIP_TCP */
sherckuith 0:479ce5546098 188 /** A callback function that is informed about events for this netconn */
sherckuith 0:479ce5546098 189 netconn_callback callback;
sherckuith 0:479ce5546098 190 };
sherckuith 0:479ce5546098 191
sherckuith 0:479ce5546098 192 /** Register an Network connection event */
sherckuith 0:479ce5546098 193 #define API_EVENT(c,e,l) if (c->callback) { \
sherckuith 0:479ce5546098 194 (*c->callback)(c, e, l); \
sherckuith 0:479ce5546098 195 }
sherckuith 0:479ce5546098 196
sherckuith 0:479ce5546098 197 /** Set conn->last_err to err but don't overwrite fatal errors */
sherckuith 0:479ce5546098 198 #define NETCONN_SET_SAFE_ERR(conn, err) do { \
sherckuith 0:479ce5546098 199 SYS_ARCH_DECL_PROTECT(lev); \
sherckuith 0:479ce5546098 200 SYS_ARCH_PROTECT(lev); \
sherckuith 0:479ce5546098 201 if (!ERR_IS_FATAL((conn)->last_err)) { \
sherckuith 0:479ce5546098 202 (conn)->last_err = err; \
sherckuith 0:479ce5546098 203 } \
sherckuith 0:479ce5546098 204 SYS_ARCH_UNPROTECT(lev); \
sherckuith 0:479ce5546098 205 } while(0);
sherckuith 0:479ce5546098 206
sherckuith 0:479ce5546098 207 /* Network connection functions: */
sherckuith 0:479ce5546098 208 #define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL)
sherckuith 0:479ce5546098 209 #define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c)
sherckuith 0:479ce5546098 210 struct
sherckuith 0:479ce5546098 211 netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto,
sherckuith 0:479ce5546098 212 netconn_callback callback);
sherckuith 0:479ce5546098 213 err_t netconn_delete(struct netconn *conn);
sherckuith 0:479ce5546098 214 /** Get the type of a netconn (as enum netconn_type). */
sherckuith 0:479ce5546098 215 #define netconn_type(conn) (conn->type)
sherckuith 0:479ce5546098 216
sherckuith 0:479ce5546098 217 err_t netconn_getaddr(struct netconn *conn, ip_addr_t *addr,
sherckuith 0:479ce5546098 218 u16_t *port, u8_t local);
sherckuith 0:479ce5546098 219 #define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0)
sherckuith 0:479ce5546098 220 #define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1)
sherckuith 0:479ce5546098 221
sherckuith 0:479ce5546098 222 err_t netconn_bind(struct netconn *conn, ip_addr_t *addr, u16_t port);
sherckuith 0:479ce5546098 223 err_t netconn_connect(struct netconn *conn, ip_addr_t *addr, u16_t port);
sherckuith 0:479ce5546098 224 err_t netconn_disconnect (struct netconn *conn);
sherckuith 0:479ce5546098 225 err_t netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
sherckuith 0:479ce5546098 226 #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
sherckuith 0:479ce5546098 227 err_t netconn_accept(struct netconn *conn, struct netconn **new_conn);
sherckuith 0:479ce5546098 228 err_t netconn_recv(struct netconn *conn, struct netbuf **new_buf);
sherckuith 0:479ce5546098 229 err_t netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf);
sherckuith 0:479ce5546098 230 void netconn_recved(struct netconn *conn, u32_t length);
sherckuith 0:479ce5546098 231 err_t netconn_sendto(struct netconn *conn, struct netbuf *buf,
sherckuith 0:479ce5546098 232 ip_addr_t *addr, u16_t port);
sherckuith 0:479ce5546098 233 err_t netconn_send(struct netconn *conn, struct netbuf *buf);
sherckuith 0:479ce5546098 234 err_t netconn_write(struct netconn *conn, const void *dataptr, size_t size,
sherckuith 0:479ce5546098 235 u8_t apiflags);
sherckuith 0:479ce5546098 236 err_t netconn_close(struct netconn *conn);
sherckuith 0:479ce5546098 237 err_t netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx);
sherckuith 0:479ce5546098 238
sherckuith 0:479ce5546098 239 #if LWIP_IGMP
sherckuith 0:479ce5546098 240 err_t netconn_join_leave_group(struct netconn *conn, ip_addr_t *multiaddr,
sherckuith 0:479ce5546098 241 ip_addr_t *netif_addr, enum netconn_igmp join_or_leave);
sherckuith 0:479ce5546098 242 #endif /* LWIP_IGMP */
sherckuith 0:479ce5546098 243 #if LWIP_DNS
sherckuith 0:479ce5546098 244 err_t netconn_gethostbyname(const char *name, ip_addr_t *addr);
sherckuith 0:479ce5546098 245 #endif /* LWIP_DNS */
sherckuith 0:479ce5546098 246
sherckuith 0:479ce5546098 247 #define netconn_err(conn) ((conn)->last_err)
sherckuith 0:479ce5546098 248 #define netconn_recv_bufsize(conn) ((conn)->recv_bufsize)
sherckuith 0:479ce5546098 249
sherckuith 0:479ce5546098 250 /** Set the blocking status of netconn calls (@todo: write/send is missing) */
sherckuith 0:479ce5546098 251 #define netconn_set_nonblocking(conn, val) do { if(val) { \
sherckuith 0:479ce5546098 252 (conn)->flags |= NETCONN_FLAG_NON_BLOCKING; \
sherckuith 0:479ce5546098 253 } else { \
sherckuith 0:479ce5546098 254 (conn)->flags &= ~ NETCONN_FLAG_NON_BLOCKING; }} while(0)
sherckuith 0:479ce5546098 255 /** Get the blocking status of netconn calls (@todo: write/send is missing) */
sherckuith 0:479ce5546098 256 #define netconn_is_nonblocking(conn) (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)
sherckuith 0:479ce5546098 257
sherckuith 0:479ce5546098 258 /** TCP: Set the no-auto-recved status of netconn calls (see NETCONN_FLAG_NO_AUTO_RECVED) */
sherckuith 0:479ce5546098 259 #define netconn_set_noautorecved(conn, val) do { if(val) { \
sherckuith 0:479ce5546098 260 (conn)->flags |= NETCONN_FLAG_NO_AUTO_RECVED; \
sherckuith 0:479ce5546098 261 } else { \
sherckuith 0:479ce5546098 262 (conn)->flags &= ~ NETCONN_FLAG_NO_AUTO_RECVED; }} while(0)
sherckuith 0:479ce5546098 263 /** TCP: Get the no-auto-recved status of netconn calls (see NETCONN_FLAG_NO_AUTO_RECVED) */
sherckuith 0:479ce5546098 264 #define netconn_get_noautorecved(conn) (((conn)->flags & NETCONN_FLAG_NO_AUTO_RECVED) != 0)
sherckuith 0:479ce5546098 265
sherckuith 0:479ce5546098 266 #if LWIP_SO_RCVTIMEO
sherckuith 0:479ce5546098 267 /** Set the receive timeout in milliseconds */
sherckuith 0:479ce5546098 268 #define netconn_set_recvtimeout(conn, timeout) ((conn)->recv_timeout = (timeout))
sherckuith 0:479ce5546098 269 /** Get the receive timeout in milliseconds */
sherckuith 0:479ce5546098 270 #define netconn_get_recvtimeout(conn) ((conn)->recv_timeout)
sherckuith 0:479ce5546098 271 #endif /* LWIP_SO_RCVTIMEO */
sherckuith 0:479ce5546098 272 #if LWIP_SO_RCVBUF
sherckuith 0:479ce5546098 273 /** Set the receive buffer in bytes */
sherckuith 0:479ce5546098 274 #define netconn_set_recvbufsize(conn, recvbufsize) ((conn)->recv_bufsize = (recvbufsize))
sherckuith 0:479ce5546098 275 /** Get the receive buffer in bytes */
sherckuith 0:479ce5546098 276 #define netconn_get_recvbufsize(conn) ((conn)->recv_bufsize)
sherckuith 0:479ce5546098 277 #endif /* LWIP_SO_RCVBUF*/
sherckuith 0:479ce5546098 278
sherckuith 0:479ce5546098 279 #ifdef __cplusplus
sherckuith 0:479ce5546098 280 }
sherckuith 0:479ce5546098 281 #endif
sherckuith 0:479ce5546098 282
sherckuith 0:479ce5546098 283 #endif /* LWIP_NETCONN */
sherckuith 0:479ce5546098 284
sherckuith 0:479ce5546098 285 #endif /* __LWIP_API_H__ */