Contiki-Inga 3.x
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  */
37 
38 /**
39  * \file
40  * Rime initialization and common code
41  * \author
42  * Adam Dunkels <adam@sics.se>
43  */
44 
45 #define DEBUG 0
46 #if DEBUG
47 #include <stdio.h>
48 #define PRINTF(...) printf(__VA_ARGS__)
49 #else
50 #define PRINTF(...)
51 #endif
52 
53 #include "net/netstack.h"
54 #include "net/rime/rime.h"
55 #include "net/rime/chameleon.h"
56 #include "net/rime/route.h"
57 #include "net/rime/announcement.h"
59 #include "net/mac/mac.h"
60 
61 #include "lib/list.h"
62 
63 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL
64 #define BROADCAST_ANNOUNCEMENT_CHANNEL RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL
65 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL */
66 #define BROADCAST_ANNOUNCEMENT_CHANNEL 2
67 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL */
68 
69 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME
70 #define BROADCAST_ANNOUNCEMENT_BUMP_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME
71 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME */
72 #define BROADCAST_ANNOUNCEMENT_BUMP_TIME CLOCK_SECOND * 32 / NETSTACK_RDC_CHANNEL_CHECK_RATE
73 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME */
74 
75 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME
76 #define BROADCAST_ANNOUNCEMENT_MIN_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME
77 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME */
78 #define BROADCAST_ANNOUNCEMENT_MIN_TIME CLOCK_SECOND * 60
79 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME */
80 
81 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME
82 #define BROADCAST_ANNOUNCEMENT_MAX_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME
83 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME */
84 #define BROADCAST_ANNOUNCEMENT_MAX_TIME CLOCK_SECOND * 3600UL
85 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME */
86 
87 
88 LIST(sniffers);
89 
90 /*---------------------------------------------------------------------------*/
91 void
92 rime_sniffer_add(struct rime_sniffer *s)
93 {
94  list_add(sniffers, s);
95 }
96 /*---------------------------------------------------------------------------*/
97 void
98 rime_sniffer_remove(struct rime_sniffer *s)
99 {
100  list_remove(sniffers, s);
101 }
102 /*---------------------------------------------------------------------------*/
103 static void
104 input(void)
105 {
106  struct rime_sniffer *s;
107  struct channel *c;
108 
109  RIMESTATS_ADD(rx);
110  c = chameleon_parse();
111 
112  for(s = list_head(sniffers); s != NULL; s = list_item_next(s)) {
113  if(s->input_callback != NULL) {
114  s->input_callback();
115  }
116  }
117 
118  if(c != NULL) {
119  abc_input(c);
120  }
121 }
122 /*---------------------------------------------------------------------------*/
123 static void
124 init(void)
125 {
126  queuebuf_init();
127  packetbuf_clear();
129 
130  chameleon_init();
131 
132  /* XXX This is initializes the transmission of announcements but it
133  * is not currently certain where this initialization is supposed to
134  * be. Also, the times are arbitrarily set for now. They should
135  * either be configurable, or derived from some MAC layer property
136  * (duty cycle, sleep time, or something similar). But this is OK
137  * for now, and should at least get us started with experimenting
138  * with announcements.
139  */
140  broadcast_announcement_init(BROADCAST_ANNOUNCEMENT_CHANNEL,
141  BROADCAST_ANNOUNCEMENT_BUMP_TIME,
142  BROADCAST_ANNOUNCEMENT_MIN_TIME,
143  BROADCAST_ANNOUNCEMENT_MAX_TIME);
144 }
145 /*---------------------------------------------------------------------------*/
146 static void
147 packet_sent(void *ptr, int status, int num_tx)
148 {
149  struct channel *c = ptr;
150  struct rime_sniffer *s;
151 
152  switch(status) {
153  case MAC_TX_COLLISION:
154  PRINTF("rime: collision after %d tx\n", num_tx);
155  break;
156  case MAC_TX_NOACK:
157  PRINTF("rime: noack after %d tx\n", num_tx);
158  break;
159  case MAC_TX_OK:
160  PRINTF("rime: sent after %d tx\n", num_tx);
161  break;
162  default:
163  PRINTF("rime: error %d after %d tx\n", status, num_tx);
164  }
165 
166  /* Call sniffers, pass along the MAC status code. */
167  for(s = list_head(sniffers); s != NULL; s = list_item_next(s)) {
168  if(s->output_callback != NULL) {
169  s->output_callback(status);
170  }
171  }
172 
173  abc_sent(c, status, num_tx);
174 }
175 /*---------------------------------------------------------------------------*/
176 int
177 rime_output(struct channel *c)
178 {
179  RIMESTATS_ADD(tx);
180  if(chameleon_create(c)) {
182 
183  NETSTACK_MAC.send(packet_sent, c);
184  return 1;
185  }
186  return 0;
187 }
188 /*---------------------------------------------------------------------------*/
189 const struct network_driver rime_driver = {
190  "Rime",
191  init,
192  input
193 };
194 /** @} */