EVE 1.0
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 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  const 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 /**
194  * \brief Deregister an UDP connection
195  * \param c A pointer to a struct simple_udp_connection
196  *
197  * This function deallocates an UDP port previously allocated by
198  * simple_udp_register() function.
199  *
200  */
201 void
202 simple_udp_deregister(struct simple_udp_connection *c)
203 {
204  struct uip_udp_conn *cptr;
205 
206  for(cptr = &uip_udp_conns[0]; cptr < &uip_udp_conns[UIP_UDP_CONNS]; ++cptr) {
207  if(cptr->appstate.state == c) {
208  cptr->lport = 0;
209  }
210  }
211 }
212 /*---------------------------------------------------------------------------*/
213 PROCESS_THREAD(simple_udp_process, ev, data)
214 {
215  struct simple_udp_connection *c;
216  PROCESS_BEGIN();
217 
218  while(1) {
220  if(ev == tcpip_event) {
221 
222  /* An appstate pointer is passed to use from the IP stack
223  through the 'data' pointer. We registered this appstate when
224  we did the udp_new() call in simple_udp_register() as the
225  struct simple_udp_connection pointer. So we extract this
226  pointer and use it when calling the reception callback. */
227  c = (struct simple_udp_connection *)data;
228 
229  /* Defensive coding: although the appstate *should* be non-null
230  here, we make sure to avoid the program crashing on us. */
231  if(c != NULL) {
232 
233  /* If we were called because of incoming data, we should call
234  the reception callback. */
235  if(uip_newdata() && uip_datalen() <= UIP_BUFSIZE) {
236  /* Copy the data from the uIP data buffer into our own
237  buffer to avoid the uIP buffer being messed with by the
238  callee. */
239  memcpy(databuffer, uip_appdata, uip_datalen());
240 
241  /* Call the client process. We use the PROCESS_CONTEXT
242  mechanism to temporarily switch process context to the
243  client process. */
244  if(c->receive_callback != NULL) {
245  PROCESS_CONTEXT_BEGIN(c->client_process);
246  c->receive_callback(c,
247  &(UIP_IP_BUF->srcipaddr),
248  UIP_HTONS(UIP_IP_BUF->srcport),
249  &(UIP_IP_BUF->destipaddr),
250  UIP_HTONS(UIP_IP_BUF->destport),
251  databuffer, uip_datalen());
253  }
254  }
255  }
256  }
257 
258  }
259 
260  PROCESS_END();
261 #ifdef ECLIPSE_STUB_CODE_ANALYSE
262  return PT_ENDED;
263 #endif /* ECLIPSE_STUB_CODE_ANALYSE */
264 }
265 /*---------------------------------------------------------------------------*/
266 /** @} */
#define PROCESS_END()
Definition: process.h:140
#define PROCESS(name, strname)
Definition: process.h:316
void process_start(struct process *p, const char *arg)
Definition: process.c:100
#define PROCESS_CONTEXT_END(p)
Definition: process.h:475
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:132
#define UIP_BUFSIZE
Definition: uipopt.h:175
#define UIP_UDP_CONNS
Definition: uipopt.h:370
#define PROCESS_WAIT_EVENT()
Definition: process.h:150
int simple_udp_register(struct simple_udp_connection *c, uint16_t local_port, const uip_ipaddr_t *remote_addr, uint16_t remote_port, simple_udp_callback receive_callback)
Register a UDP connection.
Definition: simple-udp.c:164
void simple_udp_deregister(struct simple_udp_connection *c)
Deregister an UDP connection.
Definition: simple-udp.c:202
process_event_t tcpip_event
Definition: tcpip.c:81
uint16_t lport
Definition: uip.h:1414
#define PROCESS_BEGIN()
Definition: process.h:129
#define uip_newdata()
Definition: uip.h:745
#define uip_ipaddr_copy(dest, src)
Definition: uip.h:1036
CCIF struct uip_udp_conn * udp_new(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
#define UIP_HTONS(n)
Definition: uip.h:1248
uint8_t data[USBNET_RX_BUF_SIZE]
Definition: usbnet.h:140
#define PROCESS_CONTEXT_BEGIN(p)
Definition: process.h:461
#define PROCESS_THREAD(name, ev, data)
Definition: process.h:282
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:105
#define udp_bind(conn, port)
Definition: tcpip.h:262
void * uip_appdata
Definition: uip.c:154
#define uip_datalen()
Definition: uip.h:653
int simple_udp_send(struct simple_udp_connection *c, const void *data, uint16_t datalen)
Send a UDP packet.
Definition: simple-udp.c:80
uip_udp_appstate_t appstate
Definition: uip.h:1420