7 * Copyright (c) 2006, Swedish Institute of Computer Science.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
34 * This file is part of the Contiki operating system.
36 * $Id: rime.c,v 1.31 2010/10/03 20:10:22 adamdunkels Exp $
41 * Rime initialization and common code
43 * Adam Dunkels <adam@sics.se>
49 #define PRINTF(...) printf(__VA_ARGS__)
54 #include "net/netstack.h"
56 #include "net/rime/chameleon.h"
57 #include "net/rime/route.h"
58 #include "net/rime/announcement.h"
59 #include "net/rime/broadcast-announcement.h"
60 #include "net/mac/mac.h"
64 const struct mac_driver
*rime_mac
;
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 */
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 */
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 */
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 */
93 /*---------------------------------------------------------------------------*/
95 rime_sniffer_add(struct rime_sniffer
*s
)
97 list_add(sniffers
, s
);
99 /*---------------------------------------------------------------------------*/
101 rime_sniffer_remove(struct rime_sniffer
*s
)
103 list_remove(sniffers
, s
);
105 /*---------------------------------------------------------------------------*/
109 struct rime_sniffer
*s
;
113 c
= chameleon_parse();
115 for(s
= list_head(sniffers
); s
!= NULL
; s
= list_item_next(s
)) {
116 if(s
->input_callback
!= NULL
) {
125 /*---------------------------------------------------------------------------*/
133 rime_mac
= &NETSTACK_MAC
;
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.
144 broadcast_announcement_init(BROADCAST_ANNOUNCEMENT_CHANNEL
,
145 BROADCAST_ANNOUNCEMENT_BUMP_TIME
,
146 BROADCAST_ANNOUNCEMENT_MIN_TIME
,
147 BROADCAST_ANNOUNCEMENT_MAX_TIME
);
149 /*---------------------------------------------------------------------------*/
151 packet_sent(void *ptr
, int status
, int num_tx
)
153 struct channel
*c
= ptr
;
157 case MAC_TX_COLLISION
:
158 PRINTF("rime: collision after %d tx\n", num_tx
);
161 PRINTF("rime: noack after %d tx\n", num_tx
);
164 PRINTF("rime: sent after %d tx\n", num_tx
);
167 PRINTF("rime: error %d after %d tx\n", status
, num_tx
);
170 if(status
== MAC_TX_OK
) {
171 struct rime_sniffer
*s
;
172 /* Call sniffers, but only if the packet was sent. */
173 for(s
= list_head(sniffers
); s
!= NULL
; s
= list_item_next(s
)) {
174 if(s
->output_callback
!= NULL
) {
175 s
->output_callback();
180 abc_sent(c
, status
, num_tx
);
182 /*---------------------------------------------------------------------------*/
184 rime_output(struct channel
*c
)
187 if(chameleon_create(c
)) {
190 NETSTACK_MAC
.send(packet_sent
, c
);
195 /*---------------------------------------------------------------------------*/
196 const struct network_driver rime_driver
= {