Contiki 2.5
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/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 Register a UDP connection
118  * \param c A pointer to a struct simple_udp_connection
119  * \param local_port The local UDP port in host byte order
120  * \param remote_addr The remote IP address
121  * \param remote_port The remote UDP port in host byte order
122  * \param receive_callback A pointer to a function to be called for incoming packets
123  * \retval 0 If no UDP connection could be allocated
124  * \retval 1 If the connection was successfully allocated
125  *
126  * This function registers a UDP connection and attaches a
127  * callback function to it. The callback function will be
128  * called for incoming packets. The local UDP port can be
129  * set to 0 to indicate that an ephemeral UDP port should
130  * be allocated. The remote IP address can be NULL, to
131  * indicate that packets from any IP address should be
132  * accepted.
133  *
134  */
135 int
136 simple_udp_register(struct simple_udp_connection *c,
137  uint16_t local_port,
138  uip_ipaddr_t *remote_addr,
139  uint16_t remote_port,
140  simple_udp_callback receive_callback)
141 {
142 
143  init_simple_udp();
144 
145  c->local_port = local_port;
146  c->remote_port = remote_port;
147  if(remote_addr != NULL) {
148  uip_ipaddr_copy(&c->remote_addr, remote_addr);
149  }
150  c->receive_callback = receive_callback;
151 
152  PROCESS_CONTEXT_BEGIN(&simple_udp_process);
153  c->udp_conn = udp_new(remote_addr, UIP_HTONS(remote_port), c);
154  if(c->udp_conn != NULL) {
155  udp_bind(c->udp_conn, UIP_HTONS(local_port));
156  }
158 
159  if(c->udp_conn == NULL) {
160  return 0;
161  }
162  return 1;
163 }
164 /*---------------------------------------------------------------------------*/
165 PROCESS_THREAD(simple_udp_process, ev, data)
166 {
167  struct simple_udp_connection *c;
168  PROCESS_BEGIN();
169 
170  while(1) {
172  if(ev == tcpip_event) {
173 
174  /* An appstate pointer is passed to use from the IP stack
175  through the 'data' pointer. We registered this appstate when
176  we did the udp_new() call in simple_udp_register() as the
177  struct simple_udp_connection pointer. So we extract this
178  pointer and use it when calling the reception callback. */
179  c = (struct simple_udp_connection *)data;
180 
181  /* Defensive coding: although the appstate *should* be non-null
182  here, we make sure to avoid the program crashing on us. */
183  if(c != NULL) {
184 
185  /* If we were called because of incoming data, we should call
186  the reception callback. */
187  if(uip_newdata()) {
188  /* Copy the data from the uIP data buffer into our own
189  buffer to avoid the uIP buffer being messed with by the
190  callee. */
191  memcpy(databuffer, uip_appdata, uip_datalen());
192 
193  /* Call the client process. We use the PROCESS_CONTEXT
194  mechanism to temporarily switch process context to the
195  client process. */
196  if(c->receive_callback != NULL) {
197  PROCESS_CONTEXT_BEGIN(c->client_process);
198  c->receive_callback(c,
199  &(UIP_IP_BUF->srcipaddr),
200  UIP_HTONS(UIP_IP_BUF->srcport),
201  &(UIP_IP_BUF->destipaddr),
202  UIP_HTONS(UIP_IP_BUF->destport),
203  databuffer, uip_datalen());
205  }
206  }
207  }
208  }
209 
210  }
211 
212  PROCESS_END();
213 }
214 /*---------------------------------------------------------------------------*/
215 /** @} */