Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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