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  *
98  */
100  uint16_t local_port,
101  uip_ipaddr_t *remote_addr,
102  uint16_t remote_port,
103  simple_udp_callback receive_callback);
104 
105 /**
106  * \brief Send a UDP packet
107  * \param c A pointer to a struct simple_udp_connection
108  * \param data A pointer to the data to be sent
109  * \param datalen The length of the data
110  *
111  * This function sends a UDP packet. The packet will be
112  * sent to the IP address and with the UDP ports that were
113  * specified when the connection was registered with
114  * simple_udp_register().
115  *
116  * \sa simple_udp_sendto()
117  */
119  const void *data, uint16_t datalen);
120 
121 /**
122  * \brief Send a UDP packet to a specified IP address
123  * \param c A pointer to a struct simple_udp_connection
124  * \param data A pointer to the data to be sent
125  * \param datalen The length of the data
126  * \param to The IP address of the receiver
127  *
128  * This function sends a UDP packet to a specified IP
129  * address. The packet will be sent with the UDP ports
130  * that were specified when the connection was registered
131  * with simple_udp_register().
132  *
133  * \sa simple_udp_send()
134  */
136  const void *data, uint16_t datalen,
137  const uip_ipaddr_t *to);
138 
139 /**
140  * \brief Send a UDP packet to a specified IP address and UDP port
141  * \param c A pointer to a struct simple_udp_connection
142  * \param data A pointer to the data to be sent
143  * \param datalen The length of the data
144  * \param to The IP address of the receiver
145  * \param to_port The UDP port of the receiver, in host byte order
146  *
147  * This function sends a UDP packet to a specified IP
148  * address and UDP port. The packet will be sent with the
149  * UDP ports that were specified when the connection was
150  * registered with simple_udp_register().
151  *
152  * \sa simple_udp_sendto()
153  */
155  const void *data, uint16_t datalen,
156  const uip_ipaddr_t *to, uint16_t to_port);
157 
158 void simple_udp_init(void);
159 
160 #endif /* SIMPLE_UDP_H */
161 
162 /** @} */
163 /** @} */
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:1375