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_API_MSG_H__
pehrhovey 0:a548a085de55 33 #define __LWIP_API_MSG_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/ip_addr.h"
pehrhovey 0:a548a085de55 42 #include "lwip/err.h"
pehrhovey 0:a548a085de55 43 #include "lwip/sys.h"
pehrhovey 0:a548a085de55 44 #include "lwip/igmp.h"
pehrhovey 0:a548a085de55 45 #include "lwip/api.h"
pehrhovey 0:a548a085de55 46
pehrhovey 0:a548a085de55 47 #ifdef __cplusplus
pehrhovey 0:a548a085de55 48 extern "C" {
pehrhovey 0:a548a085de55 49 #endif
pehrhovey 0:a548a085de55 50
pehrhovey 0:a548a085de55 51 /* IP addresses and port numbers are expected to be in
pehrhovey 0:a548a085de55 52 * the same byte order as in the corresponding pcb.
pehrhovey 0:a548a085de55 53 */
pehrhovey 0:a548a085de55 54 /** This struct includes everything that is necessary to execute a function
pehrhovey 0:a548a085de55 55 for a netconn in another thread context (mainly used to process netconns
pehrhovey 0:a548a085de55 56 in the tcpip_thread context to be thread safe). */
pehrhovey 0:a548a085de55 57 struct api_msg_msg {
pehrhovey 0:a548a085de55 58 /** The netconn which to process - always needed: it includes the semaphore
pehrhovey 0:a548a085de55 59 which is used to block the application thread until the function finished. */
pehrhovey 0:a548a085de55 60 struct netconn *conn;
pehrhovey 0:a548a085de55 61 /** Depending on the executed function, one of these union members is used */
pehrhovey 0:a548a085de55 62 union {
pehrhovey 0:a548a085de55 63 /** used for do_send */
pehrhovey 0:a548a085de55 64 struct netbuf *b;
pehrhovey 0:a548a085de55 65 /** used for do_newconn */
pehrhovey 0:a548a085de55 66 struct {
pehrhovey 0:a548a085de55 67 u8_t proto;
pehrhovey 0:a548a085de55 68 } n;
pehrhovey 0:a548a085de55 69 /** used for do_bind and do_connect */
pehrhovey 0:a548a085de55 70 struct {
pehrhovey 0:a548a085de55 71 struct ip_addr *ipaddr;
pehrhovey 0:a548a085de55 72 u16_t port;
pehrhovey 0:a548a085de55 73 } bc;
pehrhovey 0:a548a085de55 74 /** used for do_getaddr */
pehrhovey 0:a548a085de55 75 struct {
pehrhovey 0:a548a085de55 76 struct ip_addr *ipaddr;
pehrhovey 0:a548a085de55 77 u16_t *port;
pehrhovey 0:a548a085de55 78 u8_t local;
pehrhovey 0:a548a085de55 79 } ad;
pehrhovey 0:a548a085de55 80 /** used for do_write */
pehrhovey 0:a548a085de55 81 struct {
pehrhovey 0:a548a085de55 82 const void *dataptr;
pehrhovey 0:a548a085de55 83 size_t len;
pehrhovey 0:a548a085de55 84 u8_t apiflags;
pehrhovey 0:a548a085de55 85 } w;
pehrhovey 0:a548a085de55 86 /** used for do_recv */
pehrhovey 0:a548a085de55 87 struct {
pehrhovey 0:a548a085de55 88 u16_t len;
pehrhovey 0:a548a085de55 89 } r;
pehrhovey 0:a548a085de55 90 #if LWIP_IGMP
pehrhovey 0:a548a085de55 91 /** used for do_join_leave_group */
pehrhovey 0:a548a085de55 92 struct {
pehrhovey 0:a548a085de55 93 struct ip_addr *multiaddr;
pehrhovey 0:a548a085de55 94 struct ip_addr *interface;
pehrhovey 0:a548a085de55 95 enum netconn_igmp join_or_leave;
pehrhovey 0:a548a085de55 96 } jl;
pehrhovey 0:a548a085de55 97 #endif /* LWIP_IGMP */
pehrhovey 0:a548a085de55 98 #if TCP_LISTEN_BACKLOG
pehrhovey 0:a548a085de55 99 struct {
pehrhovey 0:a548a085de55 100 u8_t backlog;
pehrhovey 0:a548a085de55 101 } lb;
pehrhovey 0:a548a085de55 102 #endif /* TCP_LISTEN_BACKLOG */
pehrhovey 0:a548a085de55 103 } msg;
pehrhovey 0:a548a085de55 104 };
pehrhovey 0:a548a085de55 105
pehrhovey 0:a548a085de55 106 /** This struct contains a function to execute in another thread context and
pehrhovey 0:a548a085de55 107 a struct api_msg_msg that serves as an argument for this function.
pehrhovey 0:a548a085de55 108 This is passed to tcpip_apimsg to execute functions in tcpip_thread context. */
pehrhovey 0:a548a085de55 109 struct api_msg {
pehrhovey 0:a548a085de55 110 /** function to execute in tcpip_thread context */
pehrhovey 0:a548a085de55 111 void (* function)(struct api_msg_msg *msg);
pehrhovey 0:a548a085de55 112 /** arguments for this function */
pehrhovey 0:a548a085de55 113 struct api_msg_msg msg;
pehrhovey 0:a548a085de55 114 };
pehrhovey 0:a548a085de55 115
pehrhovey 0:a548a085de55 116 #if LWIP_DNS
pehrhovey 0:a548a085de55 117 /** As do_gethostbyname requires more arguments but doesn't require a netconn,
pehrhovey 0:a548a085de55 118 it has its own struct (to avoid struct api_msg getting bigger than necessary).
pehrhovey 0:a548a085de55 119 do_gethostbyname must be called using tcpip_callback instead of tcpip_apimsg
pehrhovey 0:a548a085de55 120 (see netconn_gethostbyname). */
pehrhovey 0:a548a085de55 121 struct dns_api_msg {
pehrhovey 0:a548a085de55 122 /** Hostname to query or dotted IP address string */
pehrhovey 0:a548a085de55 123 const char *name;
pehrhovey 0:a548a085de55 124 /** Rhe resolved address is stored here */
pehrhovey 0:a548a085de55 125 struct ip_addr *addr;
pehrhovey 0:a548a085de55 126 /** This semaphore is posted when the name is resolved, the application thread
pehrhovey 0:a548a085de55 127 should wait on it. */
pehrhovey 0:a548a085de55 128 sys_sem_t sem;
pehrhovey 0:a548a085de55 129 /** Errors are given back here */
pehrhovey 0:a548a085de55 130 err_t *err;
pehrhovey 0:a548a085de55 131 };
pehrhovey 0:a548a085de55 132 #endif /* LWIP_DNS */
pehrhovey 0:a548a085de55 133
pehrhovey 0:a548a085de55 134 void do_newconn ( struct api_msg_msg *msg);
pehrhovey 0:a548a085de55 135 void do_delconn ( struct api_msg_msg *msg);
pehrhovey 0:a548a085de55 136 void do_bind ( struct api_msg_msg *msg);
pehrhovey 0:a548a085de55 137 void do_connect ( struct api_msg_msg *msg);
pehrhovey 0:a548a085de55 138 void do_disconnect ( struct api_msg_msg *msg);
pehrhovey 0:a548a085de55 139 void do_listen ( struct api_msg_msg *msg);
pehrhovey 0:a548a085de55 140 void do_send ( struct api_msg_msg *msg);
pehrhovey 0:a548a085de55 141 void do_recv ( struct api_msg_msg *msg);
pehrhovey 0:a548a085de55 142 void do_write ( struct api_msg_msg *msg);
pehrhovey 0:a548a085de55 143 void do_getaddr ( struct api_msg_msg *msg);
pehrhovey 0:a548a085de55 144 void do_close ( struct api_msg_msg *msg);
pehrhovey 0:a548a085de55 145 #if LWIP_IGMP
pehrhovey 0:a548a085de55 146 void do_join_leave_group( struct api_msg_msg *msg);
pehrhovey 0:a548a085de55 147 #endif /* LWIP_IGMP */
pehrhovey 0:a548a085de55 148
pehrhovey 0:a548a085de55 149 #if LWIP_DNS
pehrhovey 0:a548a085de55 150 void do_gethostbyname(void *arg);
pehrhovey 0:a548a085de55 151 #endif /* LWIP_DNS */
pehrhovey 0:a548a085de55 152
pehrhovey 0:a548a085de55 153 struct netconn* netconn_alloc(enum netconn_type t, netconn_callback callback);
pehrhovey 0:a548a085de55 154 void netconn_free(struct netconn *conn);
pehrhovey 0:a548a085de55 155
pehrhovey 0:a548a085de55 156 #ifdef __cplusplus
pehrhovey 0:a548a085de55 157 }
pehrhovey 0:a548a085de55 158 #endif
pehrhovey 0:a548a085de55 159
pehrhovey 0:a548a085de55 160 #endif /* LWIP_NETCONN */
pehrhovey 0:a548a085de55 161
pehrhovey 0:a548a085de55 162 #endif /* __LWIP_API_MSG_H__ */