Fix compiler warnings
[contiki-2.x.git] / core / net / rime / rime.c
blob5ea9e30ab46432e8a71a93cf2993a721bb07b8de
1 /**
2 * \addtogroup rime
3 * @{
4 */
6 /*
7 * Copyright (c) 2006, Swedish Institute of Computer Science.
8 * All rights reserved.
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.
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.
34 * This file is part of the Contiki operating system.
36 * $Id: rime.c,v 1.30 2010/08/01 21:18:07 dak664 Exp $
39 /**
40 * \file
41 * Rime initialization and common code
42 * \author
43 * Adam Dunkels <adam@sics.se>
46 #define DEBUG 0
47 #if DEBUG
48 #include <stdio.h>
49 #define PRINTF(...) printf(__VA_ARGS__)
50 #else
51 #define PRINTF(...)
52 #endif
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"
59 #include "net/rime/polite-announcement.h"
60 #include "net/rime/broadcast-announcement.h"
61 #include "net/mac/mac.h"
63 #include "lib/list.h"
65 const struct mac_driver *rime_mac;
67 #ifdef RIME_CONF_POLITE_ANNOUNCEMENT_CHANNEL
68 #define POLITE_ANNOUNCEMENT_CHANNEL RIME_CONF_POLITE_ANNOUNCEMENT_CHANNEL
69 #else /* RIME_CONF_POLITE_ANNOUNCEMENT_CHANNEL */
70 #define POLITE_ANNOUNCEMENT_CHANNEL 1
71 #endif /* RIME_CONF_POLITE_ANNOUNCEMENT_CHANNEL */
73 #ifdef RIME_CONF_POLITE_ANNOUNCEMENT_START_TIME
74 #define POLITE_ANNOUNCEMENT_START_TIME RIME_CONF_POLITE_ANNOUNCEMENT_START_TIME
75 #else /* RIME_CONF_POLITE_ANNOUNCEMENT_START_TIME */
76 #define POLITE_ANNOUNCEMENT_START_TIME CLOCK_SECOND * 8
77 #endif /* RIME_CONF_POLITE_ANNOUNCEMENT_START_TIME */
79 #ifdef RIME_CONF_POLITE_ANNOUNCEMENT_MAX_TIME
80 #define POLITE_ANNOUNCEMENT_MAX_TIME RIME_CONF_POLITE_ANNOUNCEMENT_MAX_TIME
81 #else /* RIME_CONF_POLITE_ANNOUNCEMENT_MAX_TIME */
82 #define POLITE_ANNOUNCEMENT_MAX_TIME CLOCK_SECOND * 128
83 #endif /* RIME_CONF_POLITE_ANNOUNCEMENT_MAX_TIME */
87 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL
88 #define BROADCAST_ANNOUNCEMENT_CHANNEL RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL
89 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL */
90 #define BROADCAST_ANNOUNCEMENT_CHANNEL 2
91 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL */
93 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME
94 #define BROADCAST_ANNOUNCEMENT_BUMP_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME
95 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME */
96 #define BROADCAST_ANNOUNCEMENT_BUMP_TIME CLOCK_SECOND * 8
97 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME */
99 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME
100 #define BROADCAST_ANNOUNCEMENT_MIN_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME
101 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME */
102 #define BROADCAST_ANNOUNCEMENT_MIN_TIME CLOCK_SECOND * 30
103 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME */
105 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME
106 #define BROADCAST_ANNOUNCEMENT_MAX_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME
107 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME */
108 #define BROADCAST_ANNOUNCEMENT_MAX_TIME CLOCK_SECOND * 600UL
109 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME */
112 LIST(sniffers);
114 /*---------------------------------------------------------------------------*/
115 void
116 rime_sniffer_add(struct rime_sniffer *s)
118 list_add(sniffers, s);
120 /*---------------------------------------------------------------------------*/
121 void
122 rime_sniffer_remove(struct rime_sniffer *s)
124 list_remove(sniffers, s);
126 /*---------------------------------------------------------------------------*/
127 static void
128 input(void)
130 struct rime_sniffer *s;
131 struct channel *c;
133 RIMESTATS_ADD(rx);
134 c = chameleon_parse();
136 for(s = list_head(sniffers); s != NULL; s = s->next) {
137 if(s->input_callback != NULL) {
138 s->input_callback();
142 if(c != NULL) {
143 abc_input(c);
146 /*---------------------------------------------------------------------------*/
147 static void
148 init(void)
150 queuebuf_init();
151 packetbuf_clear();
152 announcement_init();
154 rime_mac = &NETSTACK_MAC;
155 chameleon_init();
156 #if ! RIME_CONF_NO_POLITE_ANNOUCEMENTS
157 /* XXX This is initializes the transmission of announcements but it
158 * is not currently certain where this initialization is supposed to
159 * be. Also, the times are arbitrarily set for now. They should
160 * either be configurable, or derived from some MAC layer property
161 * (duty cycle, sleep time, or something similar). But this is OK
162 * for now, and should at least get us started with experimenting
163 * with announcements.
165 polite_announcement_init(POLITE_ANNOUNCEMENT_CHANNEL,
166 POLITE_ANNOUNCEMENT_START_TIME,
167 POLITE_ANNOUNCEMENT_MAX_TIME);
168 #endif /* ! RIME_CONF_NO_POLITE_ANNOUCEMENTS */
171 #if ! RIME_CONF_NO_BROADCAST_ANNOUCEMENTS
172 /* XXX This is initializes the transmission of announcements but it
173 * is not currently certain where this initialization is supposed to
174 * be. Also, the times are arbitrarily set for now. They should
175 * either be configurable, or derived from some MAC layer property
176 * (duty cycle, sleep time, or something similar). But this is OK
177 * for now, and should at least get us started with experimenting
178 * with announcements.
180 broadcast_announcement_init(BROADCAST_ANNOUNCEMENT_CHANNEL,
181 BROADCAST_ANNOUNCEMENT_BUMP_TIME,
182 BROADCAST_ANNOUNCEMENT_MIN_TIME,
183 BROADCAST_ANNOUNCEMENT_MAX_TIME);
184 #endif /* ! RIME_CONF_NO_BROADCAST_ANNOUCEMENTS */
186 /*---------------------------------------------------------------------------*/
187 static void
188 packet_sent(void *ptr, int status, int num_tx)
190 struct channel *c = ptr;
193 switch(status) {
194 case MAC_TX_COLLISION:
195 PRINTF("rime: collision after %d tx\n", num_tx);
196 break;
197 case MAC_TX_NOACK:
198 PRINTF("rime: noack after %d tx\n", num_tx);
199 break;
200 case MAC_TX_OK:
201 PRINTF("rime: sent after %d tx\n", num_tx);
202 break;
203 default:
204 PRINTF("rime: error %d after %d tx\n", status, num_tx);
207 if(status == MAC_TX_OK) {
208 struct rime_sniffer *s;
209 /* Call sniffers, but only if the packet was sent. */
210 for(s = list_head(sniffers); s != NULL; s = s->next) {
211 if(s->output_callback != NULL) {
212 s->output_callback();
217 abc_sent(c, status, num_tx);
219 /*---------------------------------------------------------------------------*/
221 rime_output(struct channel *c)
223 RIMESTATS_ADD(tx);
224 if(chameleon_create(c)) {
225 packetbuf_compact();
227 NETSTACK_MAC.send(packet_sent, c);
228 return 1;
230 return 0;
232 /*---------------------------------------------------------------------------*/
233 const struct network_driver rime_driver = {
234 "Rime",
235 init,
236 input
238 /** @} */