Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed

Dependencies:   mbed

Committer:
pehrhovey
Date:
Sun Mar 14 00:54:12 2010 +0000
Revision:
0:a548a085de55

        

Who changed what in which revision?

UserRevisionLine numberNew 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_UDP_H__
pehrhovey 0:a548a085de55 33 #define __LWIP_UDP_H__
pehrhovey 0:a548a085de55 34
pehrhovey 0:a548a085de55 35 #include "lwip/opt.h"
pehrhovey 0:a548a085de55 36
pehrhovey 0:a548a085de55 37 #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */
pehrhovey 0:a548a085de55 38
pehrhovey 0:a548a085de55 39 #include "lwip/pbuf.h"
pehrhovey 0:a548a085de55 40 #include "lwip/netif.h"
pehrhovey 0:a548a085de55 41 #include "lwip/ip_addr.h"
pehrhovey 0:a548a085de55 42 #include "lwip/ip.h"
pehrhovey 0:a548a085de55 43
pehrhovey 0:a548a085de55 44 #ifdef __cplusplus
pehrhovey 0:a548a085de55 45 extern "C" {
pehrhovey 0:a548a085de55 46 #endif
pehrhovey 0:a548a085de55 47
pehrhovey 0:a548a085de55 48 #define UDP_HLEN 8
pehrhovey 0:a548a085de55 49
pehrhovey 0:a548a085de55 50 /* Fields are (of course) in network byte order. */
pehrhovey 0:a548a085de55 51 #ifdef PACK_STRUCT_USE_INCLUDES
pehrhovey 0:a548a085de55 52 # include "arch/bpstruct.h"
pehrhovey 0:a548a085de55 53 #endif
pehrhovey 0:a548a085de55 54 PACK_STRUCT_BEGIN
pehrhovey 0:a548a085de55 55 struct udp_hdr {
pehrhovey 0:a548a085de55 56 PACK_STRUCT_FIELD(u16_t src);
pehrhovey 0:a548a085de55 57 PACK_STRUCT_FIELD(u16_t dest); /* src/dest UDP ports */
pehrhovey 0:a548a085de55 58 PACK_STRUCT_FIELD(u16_t len);
pehrhovey 0:a548a085de55 59 PACK_STRUCT_FIELD(u16_t chksum);
pehrhovey 0:a548a085de55 60 } PACK_STRUCT_STRUCT;
pehrhovey 0:a548a085de55 61 PACK_STRUCT_END
pehrhovey 0:a548a085de55 62 #ifdef PACK_STRUCT_USE_INCLUDES
pehrhovey 0:a548a085de55 63 # include "arch/epstruct.h"
pehrhovey 0:a548a085de55 64 #endif
pehrhovey 0:a548a085de55 65
pehrhovey 0:a548a085de55 66 #define UDP_FLAGS_NOCHKSUM 0x01U
pehrhovey 0:a548a085de55 67 #define UDP_FLAGS_UDPLITE 0x02U
pehrhovey 0:a548a085de55 68 #define UDP_FLAGS_CONNECTED 0x04U
pehrhovey 0:a548a085de55 69
pehrhovey 0:a548a085de55 70 struct udp_pcb {
pehrhovey 0:a548a085de55 71 /* Common members of all PCB types */
pehrhovey 0:a548a085de55 72 IP_PCB;
pehrhovey 0:a548a085de55 73
pehrhovey 0:a548a085de55 74 /* Protocol specific PCB members */
pehrhovey 0:a548a085de55 75
pehrhovey 0:a548a085de55 76 struct udp_pcb *next;
pehrhovey 0:a548a085de55 77
pehrhovey 0:a548a085de55 78 u8_t flags;
pehrhovey 0:a548a085de55 79 /* ports are in host byte order */
pehrhovey 0:a548a085de55 80 u16_t local_port, remote_port;
pehrhovey 0:a548a085de55 81
pehrhovey 0:a548a085de55 82 #if LWIP_IGMP
pehrhovey 0:a548a085de55 83 /* outgoing network interface for multicast packets */
pehrhovey 0:a548a085de55 84 struct ip_addr multicast_ip;
pehrhovey 0:a548a085de55 85 #endif /* LWIP_IGMP */
pehrhovey 0:a548a085de55 86
pehrhovey 0:a548a085de55 87 #if LWIP_UDPLITE
pehrhovey 0:a548a085de55 88 /* used for UDP_LITE only */
pehrhovey 0:a548a085de55 89 u16_t chksum_len_rx, chksum_len_tx;
pehrhovey 0:a548a085de55 90 #endif /* LWIP_UDPLITE */
pehrhovey 0:a548a085de55 91
pehrhovey 0:a548a085de55 92 /* receive callback function
pehrhovey 0:a548a085de55 93 * addr and port are in same byte order as in the pcb
pehrhovey 0:a548a085de55 94 * The callback is responsible for freeing the pbuf
pehrhovey 0:a548a085de55 95 * if it's not used any more.
pehrhovey 0:a548a085de55 96 *
pehrhovey 0:a548a085de55 97 * ATTENTION: Be aware that 'addr' points into the pbuf 'p' so freeing this pbuf
pehrhovey 0:a548a085de55 98 * makes 'addr' invalid, too.
pehrhovey 0:a548a085de55 99 *
pehrhovey 0:a548a085de55 100 * @param arg user supplied argument (udp_pcb.recv_arg)
pehrhovey 0:a548a085de55 101 * @param pcb the udp_pcb which received data
pehrhovey 0:a548a085de55 102 * @param p the packet buffer that was received
pehrhovey 0:a548a085de55 103 * @param addr the remote IP address from which the packet was received
pehrhovey 0:a548a085de55 104 * @param port the remote port from which the packet was received
pehrhovey 0:a548a085de55 105 */
pehrhovey 0:a548a085de55 106 void (* recv)(void *arg, struct udp_pcb *pcb, struct pbuf *p,
pehrhovey 0:a548a085de55 107 struct ip_addr *addr, u16_t port);
pehrhovey 0:a548a085de55 108 /* user-supplied argument for the recv callback */
pehrhovey 0:a548a085de55 109 void *recv_arg;
pehrhovey 0:a548a085de55 110 };
pehrhovey 0:a548a085de55 111 /* udp_pcbs export for exernal reference (e.g. SNMP agent) */
pehrhovey 0:a548a085de55 112 extern struct udp_pcb *udp_pcbs;
pehrhovey 0:a548a085de55 113
pehrhovey 0:a548a085de55 114 /* The following functions is the application layer interface to the
pehrhovey 0:a548a085de55 115 UDP code. */
pehrhovey 0:a548a085de55 116 struct udp_pcb * udp_new (void);
pehrhovey 0:a548a085de55 117 void udp_remove (struct udp_pcb *pcb);
pehrhovey 0:a548a085de55 118 err_t udp_bind (struct udp_pcb *pcb, struct ip_addr *ipaddr,
pehrhovey 0:a548a085de55 119 u16_t port);
pehrhovey 0:a548a085de55 120 err_t udp_connect (struct udp_pcb *pcb, struct ip_addr *ipaddr,
pehrhovey 0:a548a085de55 121 u16_t port);
pehrhovey 0:a548a085de55 122 void udp_disconnect (struct udp_pcb *pcb);
pehrhovey 0:a548a085de55 123 void udp_recv (struct udp_pcb *pcb,
pehrhovey 0:a548a085de55 124 void (* recv)(void *arg, struct udp_pcb *upcb,
pehrhovey 0:a548a085de55 125 struct pbuf *p,
pehrhovey 0:a548a085de55 126 struct ip_addr *addr,
pehrhovey 0:a548a085de55 127 u16_t port),
pehrhovey 0:a548a085de55 128 void *recv_arg);
pehrhovey 0:a548a085de55 129 err_t udp_sendto_if (struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *dst_ip, u16_t dst_port, struct netif *netif);
pehrhovey 0:a548a085de55 130 err_t udp_sendto (struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *dst_ip, u16_t dst_port);
pehrhovey 0:a548a085de55 131 err_t udp_send (struct udp_pcb *pcb, struct pbuf *p);
pehrhovey 0:a548a085de55 132
pehrhovey 0:a548a085de55 133 #define udp_flags(pcb) ((pcb)->flags)
pehrhovey 0:a548a085de55 134 #define udp_setflags(pcb, f) ((pcb)->flags = (f))
pehrhovey 0:a548a085de55 135
pehrhovey 0:a548a085de55 136 /* The following functions are the lower layer interface to UDP. */
pehrhovey 0:a548a085de55 137 void udp_input (struct pbuf *p, struct netif *inp);
pehrhovey 0:a548a085de55 138
pehrhovey 0:a548a085de55 139 #define udp_init() /* Compatibility define, not init needed. */
pehrhovey 0:a548a085de55 140
pehrhovey 0:a548a085de55 141 #if UDP_DEBUG
pehrhovey 0:a548a085de55 142 void udp_debug_print(struct udp_hdr *udphdr);
pehrhovey 0:a548a085de55 143 #else
pehrhovey 0:a548a085de55 144 #define udp_debug_print(udphdr)
pehrhovey 0:a548a085de55 145 #endif
pehrhovey 0:a548a085de55 146
pehrhovey 0:a548a085de55 147 #ifdef __cplusplus
pehrhovey 0:a548a085de55 148 }
pehrhovey 0:a548a085de55 149 #endif
pehrhovey 0:a548a085de55 150
pehrhovey 0:a548a085de55 151 #endif /* LWIP_UDP */
pehrhovey 0:a548a085de55 152
pehrhovey 0:a548a085de55 153 #endif /* __LWIP_UDP_H__ */