Contiki-Inga 3.x
simple-udp.c
1 /**
2  * \addtogroup simple-udp
3  * @{
4  */
5 
6 
7 /*
8  * Copyright (c) 2011, Swedish Institute of Computer Science.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the Institute nor the names of its contributors
20  * may be used to endorse or promote products derived from this software
21  * without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * This file is part of the Contiki operating system.
36  *
37  * \file
38  * Header file for the simple-udp module.
39  * \author
40  * Adam Dunkels <adam@sics.se>
41  *
42  */
43 
44 #include "contiki-net.h"
45 #include "net/ip/simple-udp.h"
46 
47 #include <string.h>
48 
49 
50 PROCESS(simple_udp_process, "Simple UDP process");
51 static uint8_t started = 0;
52 static uint8_t databuffer[UIP_BUFSIZE];
53 
54 #define UIP_IP_BUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
55 
56 /*---------------------------------------------------------------------------*/
57 static void
58 init_simple_udp(void)
59 {
60  if(started == 0) {
61  process_start(&simple_udp_process, NULL);
62  started = 1;
63  }
64 }
65 /*---------------------------------------------------------------------------*/
66 /**
67  * \brief Send a UDP packet
68  * \param c A pointer to a struct simple_udp_connection
69  * \param data A pointer to the data to be sent
70  * \param datalen The length of the data
71  *
72  * This function sends a UDP packet. The packet will be
73  * sent to the IP address and with the UDP ports that were
74  * specified when the connection wa registered with
75  * simple_udp_register().
76  *
77  * \sa simple_udp_sendto()
78  */
79 int
80 simple_udp_send(struct simple_udp_connection *c,
81  const void *data, uint16_t datalen)
82 {
83  if(c->udp_conn != NULL) {
84  uip_udp_packet_sendto(c->udp_conn, data, datalen,
85  &c->remote_addr, UIP_HTONS(c->remote_port));
86  }
87  return 0;
88 }
89 /*---------------------------------------------------------------------------*/
90 /**
91  * \brief Send a UDP packet to a specified IP address
92  * \param c A pointer to a struct simple_udp_connection
93  * \param data A pointer to the data to be sent
94  * \param datalen The length of the data
95  * \param to The IP address of the receiver
96  *
97  * This function sends a UDP packet to a specified IP
98  * address. The packet will be sent with the UDP ports
99  * that were specified when the connection wa registered
100  * with simple_udp_register().
101  *
102  * \sa simple_udp_send()
103  */
104 int
105 simple_udp_sendto(struct simple_udp_connection *c,
106  const void *data, uint16_t datalen,
107  const uip_ipaddr_t *to)
108 {
109  if(c->udp_conn != NULL) {
110  uip_udp_packet_sendto(c->udp_conn, data, datalen,
111  to, UIP_HTONS(c->remote_port));
112  }
113  return 0;
114 }
115 /*---------------------------------------------------------------------------*/
116 /**
117  * \brief Send a UDP packet to a specified IP address and UDP port
118  * \param c A pointer to a struct simple_udp_connection
119  * \param data A pointer to the data to be sent
120  * \param datalen The length of the data
121  * \param to The IP address of the receiver
122  * \param port The UDP port of the receiver, in host byte order
123  *
124  * This function sends a UDP packet to a specified IP
125  * address and UDP port. The packet will be sent with the
126  * UDP ports that were specified when the connection wa
127  * registered with simple_udp_register().
128  *
129  * \sa simple_udp_sendto()
130  */
131 int
132 simple_udp_sendto_port(struct simple_udp_connection *c,
133  const void *data, uint16_t datalen,
134  const uip_ipaddr_t *to,
135  uint16_t port)
136 {
137  if(c->udp_conn != NULL) {
138  uip_udp_packet_sendto(c->udp_conn, data, datalen,
139  to, UIP_HTONS(port));
140  }
141  return 0;
142 }
143 /*---------------------------------------------------------------------------*/
144 /**
145  * \brief Register a UDP connection
146  * \param c A pointer to a struct simple_udp_connection
147  * \param local_port The local UDP port in host byte order
148  * \param remote_addr The remote IP address
149  * \param remote_port The remote UDP port in host byte order
150  * \param receive_callback A pointer to a function to be called for incoming packets
151  * \retval 0 If no UDP connection could be allocated
152  * \retval 1 If the connection was successfully allocated
153  *
154  * This function registers a UDP connection and attaches a
155  * callback function to it. The callback function will be
156  * called for incoming packets. The local UDP port can be
157  * set to 0 to indicate that an ephemeral UDP port should
158  * be allocated. The remote IP address can be NULL, to
159  * indicate that packets from any IP address should be
160  * accepted.
161  *
162  */
163 int
164 simple_udp_register(struct simple_udp_connection *c,
165  uint16_t local_port,
166  uip_ipaddr_t *remote_addr,
167  uint16_t remote_port,
168  simple_udp_callback receive_callback)
169 {
170 
171  init_simple_udp();
172 
173  c->local_port = local_port;
174  c->remote_port = remote_port;
175  if(remote_addr != NULL) {
176  uip_ipaddr_copy(&c->remote_addr, remote_addr);
177  }
178  c->receive_callback = receive_callback;
179 
180  PROCESS_CONTEXT_BEGIN(&simple_udp_process);
181  c->udp_conn = udp_new(remote_addr, UIP_HTONS(remote_port), c);
182  if(c->udp_conn != NULL) {
183  udp_bind(c->udp_conn, UIP_HTONS(local_port));
184  }
186 
187  if(c->udp_conn == NULL) {
188  return 0;
189  }
190  return 1;
191 }
192 /*---------------------------------------------------------------------------*/
193 PROCESS_THREAD(simple_udp_process, ev, data)
194 {
195  struct simple_udp_connection *c;
196  PROCESS_BEGIN();
197 
198  while(1) {
200  if(ev == tcpip_event) {
201 
202  /* An appstate pointer is passed to use from the IP stack
203  through the 'data' pointer. We registered this appstate when
204  we did the udp_new() call in simple_udp_register() as the
205  struct simple_udp_connection pointer. So we extract this
206  pointer and use it when calling the reception callback. */
207  c = (struct simple_udp_connection *)data;
208 
209  /* Defensive coding: although the appstate *should* be non-null
210  here, we make sure to avoid the program crashing on us. */
211  if(c != NULL) {
212 
213  /* If we were called because of incoming data, we should call
214  the reception callback. */
215  if(uip_newdata()) {
216  /* Copy the data from the uIP data buffer into our own
217  buffer to avoid the uIP buffer being messed with by the
218  callee. */
219  memcpy(databuffer, uip_appdata, uip_datalen());
220 
221  /* Call the client process. We use the PROCESS_CONTEXT
222  mechanism to temporarily switch process context to the
223  client process. */
224  if(c->receive_callback != NULL) {
225  PROCESS_CONTEXT_BEGIN(c->client_process);
226  c->receive_callback(c,
227  &(UIP_IP_BUF->srcipaddr),
228  UIP_HTONS(UIP_IP_BUF->srcport),
229  &(UIP_IP_BUF->destipaddr),
230  UIP_HTONS(UIP_IP_BUF->destport),
231  databuffer, uip_datalen());
233  }
234  }
235  }
236  }
237 
238  }
239 
240  PROCESS_END();
241 }
242 /*---------------------------------------------------------------------------*/
243 /** @} */