Contiki 2.5
rime.c
Go to the documentation of this file.
1 /**
2  * \addtogroup rime
3  * @{
4  */
5 
6 /*
7  * Copyright (c) 2006, Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the Institute nor the names of its contributors
19  * may be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * This file is part of the Contiki operating system.
35  *
36  * $Id: rime.c,v 1.31 2010/10/03 20:10:22 adamdunkels Exp $
37  */
38 
39 /**
40  * \file
41  * Rime initialization and common code
42  * \author
43  * Adam Dunkels <adam@sics.se>
44  */
45 
46 #define DEBUG 0
47 #if DEBUG
48 #include <stdio.h>
49 #define PRINTF(...) printf(__VA_ARGS__)
50 #else
51 #define PRINTF(...)
52 #endif
53 
54 #include "net/netstack.h"
55 #include "net/rime.h"
56 #include "net/rime/chameleon.h"
57 #include "net/rime/route.h"
58 #include "net/rime/announcement.h"
60 #include "net/mac/mac.h"
61 
62 #include "lib/list.h"
63 
64 const struct mac_driver *rime_mac;
65 
66 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL
67 #define BROADCAST_ANNOUNCEMENT_CHANNEL RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL
68 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL */
69 #define BROADCAST_ANNOUNCEMENT_CHANNEL 2
70 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL */
71 
72 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME
73 #define BROADCAST_ANNOUNCEMENT_BUMP_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME
74 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME */
75 #define BROADCAST_ANNOUNCEMENT_BUMP_TIME CLOCK_SECOND * 8
76 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME */
77 
78 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME
79 #define BROADCAST_ANNOUNCEMENT_MIN_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME
80 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME */
81 #define BROADCAST_ANNOUNCEMENT_MIN_TIME CLOCK_SECOND * 60
82 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME */
83 
84 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME
85 #define BROADCAST_ANNOUNCEMENT_MAX_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME
86 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME */
87 #define BROADCAST_ANNOUNCEMENT_MAX_TIME CLOCK_SECOND * 3600UL
88 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME */
89 
90 
91 LIST(sniffers);
92 
93 /*---------------------------------------------------------------------------*/
94 void
95 rime_sniffer_add(struct rime_sniffer *s)
96 {
97  list_add(sniffers, s);
98 }
99 /*---------------------------------------------------------------------------*/
100 void
101 rime_sniffer_remove(struct rime_sniffer *s)
102 {
103  list_remove(sniffers, s);
104 }
105 /*---------------------------------------------------------------------------*/
106 static void
107 input(void)
108 {
109  struct rime_sniffer *s;
110  struct channel *c;
111 
112  RIMESTATS_ADD(rx);
113  c = chameleon_parse();
114 
115  for(s = list_head(sniffers); s != NULL; s = list_item_next(s)) {
116  if(s->input_callback != NULL) {
117  s->input_callback();
118  }
119  }
120 
121  if(c != NULL) {
122  abc_input(c);
123  }
124 }
125 /*---------------------------------------------------------------------------*/
126 static void
127 init(void)
128 {
129  queuebuf_init();
130  packetbuf_clear();
132 
133  rime_mac = &NETSTACK_MAC;
134  chameleon_init();
135 
136  /* XXX This is initializes the transmission of announcements but it
137  * is not currently certain where this initialization is supposed to
138  * be. Also, the times are arbitrarily set for now. They should
139  * either be configurable, or derived from some MAC layer property
140  * (duty cycle, sleep time, or something similar). But this is OK
141  * for now, and should at least get us started with experimenting
142  * with announcements.
143  */
144  broadcast_announcement_init(BROADCAST_ANNOUNCEMENT_CHANNEL,
145  BROADCAST_ANNOUNCEMENT_BUMP_TIME,
146  BROADCAST_ANNOUNCEMENT_MIN_TIME,
147  BROADCAST_ANNOUNCEMENT_MAX_TIME);
148 }
149 /*---------------------------------------------------------------------------*/
150 static void
151 packet_sent(void *ptr, int status, int num_tx)
152 {
153  struct channel *c = ptr;
154  struct rime_sniffer *s;
155 
156  switch(status) {
157  case MAC_TX_COLLISION:
158  PRINTF("rime: collision after %d tx\n", num_tx);
159  break;
160  case MAC_TX_NOACK:
161  PRINTF("rime: noack after %d tx\n", num_tx);
162  break;
163  case MAC_TX_OK:
164  PRINTF("rime: sent after %d tx\n", num_tx);
165  break;
166  default:
167  PRINTF("rime: error %d after %d tx\n", status, num_tx);
168  }
169 
170  /* Call sniffers, pass along the MAC status code. */
171  for(s = list_head(sniffers); s != NULL; s = list_item_next(s)) {
172  if(s->output_callback != NULL) {
173  s->output_callback(status);
174  }
175  }
176 
177  abc_sent(c, status, num_tx);
178 }
179 /*---------------------------------------------------------------------------*/
180 int
181 rime_output(struct channel *c)
182 {
183  RIMESTATS_ADD(tx);
184  if(chameleon_create(c)) {
186 
187  NETSTACK_MAC.send(packet_sent, c);
188  return 1;
189  }
190  return 0;
191 }
192 /*---------------------------------------------------------------------------*/
193 const struct network_driver rime_driver = {
194  "Rime",
195  init,
196  input
197 };
198 /** @} */