Contiki 3.x
rime.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, 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  */
32 
33 /**
34  * \file
35  * Rime initialization and common code
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rime
42  * @{
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_LLSEC.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 /** @} */
Linked list manipulation routines.
Header file for Chameleon, Rime&#39;s header processing module
The MAC layer deferred the transmission for a later time.
Definition: mac.h:86
void * list_item_next(void *item)
Get the next item following this item.
Definition: list.c:325
#define NULL
The null pointer.
The structure of a network driver in Contiki.
Definition: netstack.h:117
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Definition: list.c:240
void abc_input(struct channel *channel)
Internal Rime function: Pass a packet to the abc layer.
Definition: abc.c:88
Header file for the Rime stack
void(* input)(void)
Callback for getting notified of incoming packet.
Definition: netstack.h:124
Header file for the announcement primitive
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:83
void packetbuf_compact(void)
Compact the packetbuf.
Definition: packetbuf.c:105
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:143
void announcement_init(void)
Initialize the announcement module.
Definition: announcement.c:56
#define LIST(name)
Declare a linked list.
Definition: list.h:86
void(* init)(void)
Initialize the network driver.
Definition: netstack.h:121
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:77
The MAC layer transmission was OK.
Definition: mac.h:79
Neighbor discovery header file
The MAC layer did not get an acknowledgement for the packet.
Definition: mac.h:83
MAC driver header file
Include file for the Contiki low-layer network stack (NETSTACK)
Header file for the Rime route table