Contiki-NG
simple-udp.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  */
31 
32 /**
33  * \file
34  * Header file for the simple-udp module.
35  * \author
36  * Adam Dunkels <adam@sics.se>
37  *
38  */
39 
40 /**
41  * \addtogroup uip
42  * @{
43  */
44 
45 
46 /**
47  * \defgroup simple-udp A simple UDP API
48  *
49  * The default Contiki UDP API is difficult to use. The simple-udp
50  * module provides a significantly simpler API.
51  *
52  * @{
53  */
54 
55 #ifndef SIMPLE_UDP_H
56 #define SIMPLE_UDP_H
57 
58 #include "net/ipv6/uip.h"
59 
61 
62 /** Simple UDP Callback function type. */
63 typedef void (* simple_udp_callback)(struct simple_udp_connection *c,
64  const uip_ipaddr_t *source_addr,
65  uint16_t source_port,
66  const uip_ipaddr_t *dest_addr,
67  uint16_t dest_port,
68  const uint8_t *data, uint16_t datalen);
69 
70 /** Simple UDP connection */
72  struct simple_udp_connection *next;
73  uip_ipaddr_t remote_addr;
74  uint16_t remote_port, local_port;
75  simple_udp_callback receive_callback;
76  struct uip_udp_conn *udp_conn;
77  struct process *client_process;
78 };
79 
80 /**
81  * \brief Register a UDP connection
82  * \param c A pointer to a struct simple_udp_connection
83  * \param local_port The local UDP port in host byte order
84  * \param remote_addr The remote IP address
85  * \param remote_port The remote UDP port in host byte order
86  * \param receive_callback A pointer to a function of to be called for incoming packets
87  * \retval 0 If no UDP connection could be allocated
88  * \retval 1 If the connection was successfully allocated
89  *
90  * This function registers a UDP connection and attaches a
91  * callback function to it. The callback function will be
92  * called for incoming packets. The local UDP port can be
93  * set to 0 to indicate that an ephemeral UDP port should
94  * be allocated. The remote IP address can be NULL, to
95  * indicate that packets from any IP address should be
96  * accepted.
97  * Note that receive_callback is executed in the context
98  * of the process which has called this function
99  *
100  */
102  uint16_t local_port,
103  uip_ipaddr_t *remote_addr,
104  uint16_t remote_port,
105  simple_udp_callback receive_callback);
106 
107 /**
108  * \brief Send a UDP packet
109  * \param c A pointer to a struct simple_udp_connection
110  * \param data A pointer to the data to be sent
111  * \param datalen The length of the data
112  *
113  * This function sends a UDP packet. The packet will be
114  * sent to the IP address and with the UDP ports that were
115  * specified when the connection was registered with
116  * simple_udp_register().
117  *
118  * \sa simple_udp_sendto()
119  */
121  const void *data, uint16_t datalen);
122 
123 /**
124  * \brief Send a UDP packet to a specified IP address
125  * \param c A pointer to a struct simple_udp_connection
126  * \param data A pointer to the data to be sent
127  * \param datalen The length of the data
128  * \param to The IP address of the receiver
129  *
130  * This function sends a UDP packet to a specified IP
131  * address. The packet will be sent with the UDP ports
132  * that were specified when the connection was registered
133  * with simple_udp_register().
134  *
135  * \sa simple_udp_send()
136  */
138  const void *data, uint16_t datalen,
139  const uip_ipaddr_t *to);
140 
141 /**
142  * \brief Send a UDP packet to a specified IP address and UDP port
143  * \param c A pointer to a struct simple_udp_connection
144  * \param data A pointer to the data to be sent
145  * \param datalen The length of the data
146  * \param to The IP address of the receiver
147  * \param to_port The UDP port of the receiver, in host byte order
148  *
149  * This function sends a UDP packet to a specified IP
150  * address and UDP port. The packet will be sent with the
151  * UDP ports that were specified when the connection was
152  * registered with simple_udp_register().
153  *
154  * \sa simple_udp_sendto()
155  */
157  const void *data, uint16_t datalen,
158  const uip_ipaddr_t *to, uint16_t to_port);
159 
160 void simple_udp_init(void);
161 
162 #endif /* SIMPLE_UDP_H */
163 
164 /** @} */
165 /** @} */
int simple_udp_sendto_port(struct simple_udp_connection *c, const void *data, uint16_t datalen, const uip_ipaddr_t *to, uint16_t port)
Send a UDP packet to a specified IP address and UDP port.
Definition: simple-udp.c:89
void(* simple_udp_callback)(struct simple_udp_connection *c, const uip_ipaddr_t *source_addr, uint16_t source_port, const uip_ipaddr_t *dest_addr, uint16_t dest_port, const uint8_t *data, uint16_t datalen)
Simple UDP Callback function type.
Definition: simple-udp.h:63
Simple UDP connection.
Definition: simple-udp.h:71
int simple_udp_register(struct simple_udp_connection *c, uint16_t local_port, uip_ipaddr_t *remote_addr, uint16_t remote_port, simple_udp_callback receive_callback)
Register a UDP connection.
Definition: simple-udp.c:102
Header file for the uIP TCP/IP stack.
int simple_udp_sendto(struct simple_udp_connection *c, const void *data, uint16_t datalen, const uip_ipaddr_t *to)
Send a UDP packet to a specified IP address.
Definition: simple-udp.c:77
int simple_udp_send(struct simple_udp_connection *c, const void *data, uint16_t datalen)
Send a UDP packet.
Definition: simple-udp.c:66
Representation of a uIP UDP connection.
Definition: uip.h:1309