Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed
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 <stddef.h> /* for size_t */ 00040 00041 #include "lwip/netbuf.h" 00042 #include "lwip/sys.h" 00043 #include "lwip/ip_addr.h" 00044 #include "lwip/err.h" 00045 00046 #ifdef __cplusplus 00047 extern "C" { 00048 #endif 00049 00050 /* Throughout this file, IP addresses and port numbers are expected to be in 00051 * the same byte order as in the corresponding pcb. 00052 */ 00053 00054 /* Flags for netconn_write */ 00055 #define NETCONN_NOFLAG 0x00 00056 #define NETCONN_NOCOPY 0x00 /* Only for source code compatibility */ 00057 #define NETCONN_COPY 0x01 00058 #define NETCONN_MORE 0x02 00059 00060 /* Helpers to process several netconn_types by the same code */ 00061 #define NETCONNTYPE_GROUP(t) (t&0xF0) 00062 #define NETCONNTYPE_DATAGRAM(t) (t&0xE0) 00063 00064 enum netconn_type { 00065 NETCONN_INVALID = 0, 00066 /* NETCONN_TCP Group */ 00067 NETCONN_TCP = 0x10, 00068 /* NETCONN_UDP Group */ 00069 NETCONN_UDP = 0x20, 00070 NETCONN_UDPLITE = 0x21, 00071 NETCONN_UDPNOCHKSUM= 0x22, 00072 /* NETCONN_RAW Group */ 00073 NETCONN_RAW = 0x40 00074 }; 00075 00076 enum netconn_state { 00077 NETCONN_NONE, 00078 NETCONN_WRITE, 00079 NETCONN_LISTEN, 00080 NETCONN_CONNECT, 00081 NETCONN_CLOSE 00082 }; 00083 00084 enum netconn_evt { 00085 NETCONN_EVT_RCVPLUS, 00086 NETCONN_EVT_RCVMINUS, 00087 NETCONN_EVT_SENDPLUS, 00088 NETCONN_EVT_SENDMINUS 00089 }; 00090 00091 #if LWIP_IGMP 00092 enum netconn_igmp { 00093 NETCONN_JOIN, 00094 NETCONN_LEAVE 00095 }; 00096 #endif /* LWIP_IGMP */ 00097 00098 /* forward-declare some structs to avoid to include their headers */ 00099 struct ip_pcb; 00100 struct tcp_pcb; 00101 struct udp_pcb; 00102 struct raw_pcb; 00103 struct netconn; 00104 00105 /** A callback prototype to inform about events for a netconn */ 00106 typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len); 00107 00108 /** A netconn descriptor */ 00109 struct netconn { 00110 /** type of the netconn (TCP, UDP or RAW) */ 00111 enum netconn_type type; 00112 /** current state of the netconn */ 00113 enum netconn_state state; 00114 /** the lwIP internal protocol control block */ 00115 union { 00116 struct ip_pcb *ip; 00117 struct tcp_pcb *tcp; 00118 struct udp_pcb *udp; 00119 struct raw_pcb *raw; 00120 } pcb; 00121 /** the last error this netconn had */ 00122 err_t err; 00123 /** sem that is used to synchroneously execute functions in the core context */ 00124 sys_sem_t op_completed; 00125 /** mbox where received packets are stored until they are fetched 00126 by the netconn application thread (can grow quite big) */ 00127 sys_mbox_t recvmbox; 00128 /** mbox where new connections are stored until processed 00129 by the application thread */ 00130 sys_mbox_t acceptmbox; 00131 /** only used for socket layer */ 00132 int socket; 00133 #if LWIP_SO_RCVTIMEO 00134 /** timeout to wait for new data to be received 00135 (or connections to arrive for listening netconns) */ 00136 int recv_timeout; 00137 #endif /* LWIP_SO_RCVTIMEO */ 00138 #if LWIP_SO_RCVBUF 00139 /** maximum amount of bytes queued in recvmbox */ 00140 int recv_bufsize; 00141 #endif /* LWIP_SO_RCVBUF */ 00142 s16_t recv_avail; 00143 #if LWIP_TCP 00144 /** TCP: when data passed to netconn_write doesn't fit into the send buffer, 00145 this temporarily stores the message. */ 00146 struct api_msg_msg *write_msg; 00147 /** TCP: when data passed to netconn_write doesn't fit into the send buffer, 00148 this temporarily stores how much is already sent. */ 00149 size_t write_offset; 00150 #if LWIP_TCPIP_CORE_LOCKING 00151 /** TCP: when data passed to netconn_write doesn't fit into the send buffer, 00152 this temporarily stores whether to wake up the original application task 00153 if data couldn't be sent in the first try. */ 00154 u8_t write_delayed; 00155 #endif /* LWIP_TCPIP_CORE_LOCKING */ 00156 #endif /* LWIP_TCP */ 00157 /** A callback function that is informed about events for this netconn */ 00158 netconn_callback callback; 00159 }; 00160 00161 /* Register an Network connection event */ 00162 #define API_EVENT(c,e,l) if (c->callback) { \ 00163 (*c->callback)(c, e, l); \ 00164 } 00165 00166 /* Network connection functions: */ 00167 #define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL) 00168 #define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c) 00169 struct 00170 netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, 00171 netconn_callback callback); 00172 err_t netconn_delete (struct netconn *conn); 00173 /** Get the type of a netconn (as enum netconn_type). */ 00174 #define netconn_type(conn) (conn->type) 00175 00176 err_t netconn_getaddr (struct netconn *conn, 00177 struct ip_addr *addr, 00178 u16_t *port, 00179 u8_t local); 00180 #define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0) 00181 #define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1) 00182 00183 err_t netconn_bind (struct netconn *conn, 00184 struct ip_addr *addr, 00185 u16_t port); 00186 err_t netconn_connect (struct netconn *conn, 00187 struct ip_addr *addr, 00188 u16_t port); 00189 err_t netconn_disconnect (struct netconn *conn); 00190 err_t netconn_listen_with_backlog(struct netconn *conn, u8_t backlog); 00191 #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG) 00192 struct netconn * netconn_accept (struct netconn *conn); 00193 struct netbuf * netconn_recv (struct netconn *conn); 00194 err_t netconn_sendto (struct netconn *conn, 00195 struct netbuf *buf, struct ip_addr *addr, u16_t port); 00196 err_t netconn_send (struct netconn *conn, 00197 struct netbuf *buf); 00198 err_t netconn_write (struct netconn *conn, 00199 const void *dataptr, size_t size, 00200 u8_t apiflags); 00201 err_t netconn_close (struct netconn *conn); 00202 00203 #if LWIP_IGMP 00204 err_t netconn_join_leave_group (struct netconn *conn, 00205 struct ip_addr *multiaddr, 00206 struct ip_addr *interface, 00207 enum netconn_igmp join_or_leave); 00208 #endif /* LWIP_IGMP */ 00209 #if LWIP_DNS 00210 err_t netconn_gethostbyname(const char *name, struct ip_addr *addr); 00211 #endif /* LWIP_DNS */ 00212 00213 #define netconn_err(conn) ((conn)->err) 00214 #define netconn_recv_bufsize(conn) ((conn)->recv_bufsize) 00215 00216 #ifdef __cplusplus 00217 } 00218 #endif 00219 00220 #endif /* LWIP_NETCONN */ 00221 00222 #endif /* __LWIP_API_H__ */
Generated on Tue Jul 12 2022 19:17:23 by 1.7.2