Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed
udp.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_UDP_H__ 00033 #define __LWIP_UDP_H__ 00034 00035 #include "lwip/opt.h" 00036 00037 #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */ 00038 00039 #include "lwip/pbuf.h" 00040 #include "lwip/netif.h" 00041 #include "lwip/ip_addr.h" 00042 #include "lwip/ip.h" 00043 00044 #ifdef __cplusplus 00045 extern "C" { 00046 #endif 00047 00048 #define UDP_HLEN 8 00049 00050 /* Fields are (of course) in network byte order. */ 00051 #ifdef PACK_STRUCT_USE_INCLUDES 00052 # include "arch/bpstruct.h" 00053 #endif 00054 PACK_STRUCT_BEGIN 00055 struct udp_hdr { 00056 PACK_STRUCT_FIELD(u16_t src); 00057 PACK_STRUCT_FIELD(u16_t dest); /* src/dest UDP ports */ 00058 PACK_STRUCT_FIELD(u16_t len); 00059 PACK_STRUCT_FIELD(u16_t chksum); 00060 } PACK_STRUCT_STRUCT; 00061 PACK_STRUCT_END 00062 #ifdef PACK_STRUCT_USE_INCLUDES 00063 # include "arch/epstruct.h" 00064 #endif 00065 00066 #define UDP_FLAGS_NOCHKSUM 0x01U 00067 #define UDP_FLAGS_UDPLITE 0x02U 00068 #define UDP_FLAGS_CONNECTED 0x04U 00069 00070 struct udp_pcb { 00071 /* Common members of all PCB types */ 00072 IP_PCB; 00073 00074 /* Protocol specific PCB members */ 00075 00076 struct udp_pcb *next; 00077 00078 u8_t flags; 00079 /* ports are in host byte order */ 00080 u16_t local_port, remote_port; 00081 00082 #if LWIP_IGMP 00083 /* outgoing network interface for multicast packets */ 00084 struct ip_addr multicast_ip; 00085 #endif /* LWIP_IGMP */ 00086 00087 #if LWIP_UDPLITE 00088 /* used for UDP_LITE only */ 00089 u16_t chksum_len_rx, chksum_len_tx; 00090 #endif /* LWIP_UDPLITE */ 00091 00092 /* receive callback function 00093 * addr and port are in same byte order as in the pcb 00094 * The callback is responsible for freeing the pbuf 00095 * if it's not used any more. 00096 * 00097 * ATTENTION: Be aware that 'addr' points into the pbuf 'p' so freeing this pbuf 00098 * makes 'addr' invalid, too. 00099 * 00100 * @param arg user supplied argument (udp_pcb.recv_arg) 00101 * @param pcb the udp_pcb which received data 00102 * @param p the packet buffer that was received 00103 * @param addr the remote IP address from which the packet was received 00104 * @param port the remote port from which the packet was received 00105 */ 00106 void (* recv)(void *arg, struct udp_pcb *pcb, struct pbuf *p, 00107 struct ip_addr *addr, u16_t port); 00108 /* user-supplied argument for the recv callback */ 00109 void *recv_arg; 00110 }; 00111 /* udp_pcbs export for exernal reference (e.g. SNMP agent) */ 00112 extern struct udp_pcb *udp_pcbs; 00113 00114 /* The following functions is the application layer interface to the 00115 UDP code. */ 00116 struct udp_pcb * udp_new (void); 00117 void udp_remove (struct udp_pcb *pcb); 00118 err_t udp_bind (struct udp_pcb *pcb, struct ip_addr *ipaddr, 00119 u16_t port); 00120 err_t udp_connect (struct udp_pcb *pcb, struct ip_addr *ipaddr, 00121 u16_t port); 00122 void udp_disconnect (struct udp_pcb *pcb); 00123 void udp_recv (struct udp_pcb *pcb, 00124 void (* recv)(void *arg, struct udp_pcb *upcb, 00125 struct pbuf *p, 00126 struct ip_addr *addr, 00127 u16_t port), 00128 void *recv_arg); 00129 err_t udp_sendto_if (struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *dst_ip, u16_t dst_port, struct netif *netif); 00130 err_t udp_sendto (struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *dst_ip, u16_t dst_port); 00131 err_t udp_send (struct udp_pcb *pcb, struct pbuf *p); 00132 00133 #define udp_flags(pcb) ((pcb)->flags) 00134 #define udp_setflags(pcb, f) ((pcb)->flags = (f)) 00135 00136 /* The following functions are the lower layer interface to UDP. */ 00137 void udp_input (struct pbuf *p, struct netif *inp); 00138 00139 #define udp_init() /* Compatibility define, not init needed. */ 00140 00141 #if UDP_DEBUG 00142 void udp_debug_print(struct udp_hdr *udphdr); 00143 #else 00144 #define udp_debug_print(udphdr) 00145 #endif 00146 00147 #ifdef __cplusplus 00148 } 00149 #endif 00150 00151 #endif /* LWIP_UDP */ 00152 00153 #endif /* __LWIP_UDP_H__ */
Generated on Tue Jul 12 2022 19:17:24 by 1.7.2