Contiki 2.5
rime-udp.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, 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  * $Id: rime-udp.c,v 1.7 2010/10/19 18:29:04 adamdunkels Exp $
32  */
33 
34 /**
35  * \file
36  * A MAC protocol using UDP over IPv6.
37  * \author
38  * Nicolas Tsiftes <nvt@sics.se>
39  */
40 
41 #include <string.h>
42 
43 #include "net/uip.h"
44 #include "net/uip-udp-packet.h"
45 #include "net/uip-netif.h"
46 #include "net/rime/rime-udp.h"
47 #include "net/packetbuf.h"
48 
49 #define DEBUG 0
50 #if DEBUG
51 #include <stdio.h>
52 #define PRINTF(...) printf(__VA_ARGS__)
53 #define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
54 #define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x ",(lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3],(lladdr)->addr[4], (lladdr)->addr[5])
55 #else
56 #define PRINTF(...)
57 #define PRINT6ADDR(addr)
58 #define PRINTLLADDR(addr)
59 #endif
60 
61 #ifndef RIME_CONF_UDP_PORT
62 #define RIME_UDP_PORT 9508
63 #else
64 #define RIME_UDP_PORT RIME_CONF_UDP_PORT
65 #endif /* RIME_CONF_UDP_PORT */
66 
67 static struct uip_udp_conn *broadcast_conn;
68 static struct uip_udp_conn *unicast_conn;
69 
70 static void (* receiver_callback)(const struct mac_driver *);
71 
72 PROCESS(rime_udp_process, "Rime over UDP process");
73 
74 PROCESS_THREAD(rime_udp_process, ev, data)
75 {
76  static uip_ipaddr_t ipaddr;
77 
78  PROCESS_BEGIN();
79 
80  broadcast_conn = udp_broadcast_new(UIP_HTONS(RIME_UDP_PORT), NULL);
81  if(broadcast_conn == NULL) {
82  PRINTF("rime-udp: Failed to allocate a broadcast connection!\n");
83  }
84 
85  uip_create_unspecified(&ipaddr);
86  unicast_conn = udp_new(&ipaddr, UIP_HTONS(RIME_UDP_PORT), NULL);
87  if(unicast_conn == NULL) {
88  PRINTF("rime-udp: Failed to allocate a unicast connection!\n");
89  }
90 
91  udp_bind(unicast_conn, UIP_HTONS(RIME_UDP_PORT));
92 
93  while(1) {
95  if(uip_newdata()) {
98  PRINTF("rime-udp: received %d bytes\n", uip_datalen());
99  receiver_callback(&rime_udp_driver);
100  }
101  }
102 
103  PROCESS_END();
104 }
105 /*---------------------------------------------------------------------------*/
106 static void
107 send_packet(mac_callback_t sent_callback, void *ptr)
108 {
109  const rimeaddr_t *addr;
110 
111  addr = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
112  PRINTF("rime-udp: Sending %d bytes to %d.%d\n", packetbuf_totlen(),
113  addr->u8[0], addr->u8[1]);
114 
115  if(rimeaddr_cmp(&rimeaddr_null, addr)) {
116  uip_udp_packet_send(broadcast_conn,
118  mac_call_sent_callback(sent_callback, ptr, MAC_TX_OK, 1);
119  } else {
120  uip_ip6addr(&unicast_conn->ripaddr, 0xfe80, 0, 0, 0, 0, 0, 0, 0);
121  uip_netif_addr_autoconf_set(&unicast_conn->ripaddr, (uip_lladdr_t *)addr);
122  uip_udp_packet_send(unicast_conn,
124  uip_create_unspecified(&unicast_conn->ripaddr);
125  }
126  return;
127 }
128 /*---------------------------------------------------------------------------*/
129 static int
130 input_packet(void)
131 {
133  return uip_datalen();
134 }
135 /*---------------------------------------------------------------------------*/
136 static void
137 set_receive_function(void (* recv)(const struct mac_driver *))
138 {
139  receiver_callback = recv;
140 }
141 /*---------------------------------------------------------------------------*/
142 static int
143 on(void)
144 {
145  return 1;
146 }
147 /*---------------------------------------------------------------------------*/
148 static int
149 off(int keep_radio_on)
150 {
151  return 0;
152 }
153 /*---------------------------------------------------------------------------*/
154 static unsigned short
155 check_interval(void)
156 {
157  return 0;
158 }
159 /*---------------------------------------------------------------------------*/
160 static int
161 init(void)
162 {
163  process_start(&rime_udp_process, NULL);
164  return 1;
165 }
166 /*---------------------------------------------------------------------------*/
167 const struct mac_driver rime_udp_driver = {
168  "rime-udp",
169  init,
170  send_packet,
171  input_packet,
172  on,
173  off,
174  check_interval,
175 };
176 /*---------------------------------------------------------------------------*/