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