Contiki 3.x
orchestra.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, 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  */
30 
31 /**
32  * \file
33  * Orchestra: an autonomous scheduler for TSCH exploiting RPL state.
34  * See "Orchestra: Robust Mesh Networks Through Autonomously Scheduled TSCH", ACM SenSys'15
35  *
36  * \author Simon Duquennoy <simonduq@sics.se>
37  */
38 
39 #include "contiki.h"
40 #include "orchestra.h"
41 #include "net/packetbuf.h"
42 #include "net/ipv6/uip-icmp6.h"
43 #include "net/rpl/rpl-private.h"
44 #include "net/rime/rime.h" /* Needed for so-called rime-sniffer */
45 
46 #define DEBUG DEBUG_PRINT
47 #include "net/ip/uip-debug.h"
48 
49 /* A net-layer sniffer for packets sent and received */
50 static void orchestra_packet_received(void);
51 static void orchestra_packet_sent(int mac_status);
52 RIME_SNIFFER(orchestra_sniffer, orchestra_packet_received, orchestra_packet_sent);
53 
54 /* The current RPL preferred parent's link-layer address */
55 linkaddr_t orchestra_parent_linkaddr;
56 /* Set to one only after getting an ACK for a DAO sent to our preferred parent */
57 int orchestra_parent_knows_us = 0;
58 
59 /* The set of Orchestra rules in use */
60 const struct orchestra_rule *all_rules[] = ORCHESTRA_RULES;
61 #define NUM_RULES (sizeof(all_rules) / sizeof(struct orchestra_rule *))
62 
63 /*---------------------------------------------------------------------------*/
64 static void
65 orchestra_packet_received(void)
66 {
67 }
68 /*---------------------------------------------------------------------------*/
69 static void
70 orchestra_packet_sent(int mac_status)
71 {
72  /* Check if our parent just ACKed a DAO */
73  if(orchestra_parent_knows_us == 0
74  && mac_status == MAC_TX_OK
75  && packetbuf_attr(PACKETBUF_ATTR_NETWORK_ID) == UIP_PROTO_ICMP6
76  && packetbuf_attr(PACKETBUF_ATTR_CHANNEL) == (ICMP6_RPL << 8 | RPL_CODE_DAO)) {
77  if(!linkaddr_cmp(&orchestra_parent_linkaddr, &linkaddr_null)
78  && linkaddr_cmp(&orchestra_parent_linkaddr, packetbuf_addr(PACKETBUF_ADDR_RECEIVER))) {
79  orchestra_parent_knows_us = 1;
80  }
81  }
82 }
83 /*---------------------------------------------------------------------------*/
84 void
85 orchestra_callback_child_added(const linkaddr_t *addr)
86 {
87  /* Notify all Orchestra rules that a child was added */
88  int i;
89  for(i = 0; i < NUM_RULES; i++) {
90  if(all_rules[i]->child_added != NULL) {
91  all_rules[i]->child_added(addr);
92  }
93  }
94 }
95 /*---------------------------------------------------------------------------*/
96 void
97 orchestra_callback_child_removed(const linkaddr_t *addr)
98 {
99  /* Notify all Orchestra rules that a child was removed */
100  int i;
101  for(i = 0; i < NUM_RULES; i++) {
102  if(all_rules[i]->child_removed != NULL) {
103  all_rules[i]->child_removed(addr);
104  }
105  }
106 }
107 /*---------------------------------------------------------------------------*/
108 void
109 orchestra_callback_packet_ready(void)
110 {
111  int i;
112  /* By default, use any slotframe, any timeslot */
113  uint16_t slotframe = 9;
114  uint16_t timeslot = 0xffff;
115 
116  /* Loop over all rules until finding one able to handle the packet */
117  for(i = 0; i < NUM_RULES; i++) {
118  if(all_rules[i]->select_packet != NULL) {
119  if(all_rules[i]->select_packet(&slotframe, &timeslot)) {
120  break;
121  }
122  }
123  }
124 
125 #if TSCH_WITH_LINK_SELECTOR
126  packetbuf_set_attr(PACKETBUF_ATTR_TSCH_SLOTFRAME, slotframe);
127  packetbuf_set_attr(PACKETBUF_ATTR_TSCH_TIMESLOT, timeslot);
128 #endif
129 }
130 /*---------------------------------------------------------------------------*/
131 void
132 orchestra_callback_new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
133 {
134  /* Orchestra assumes that the time source is also the RPL parent.
135  * This is the case if the following is set:
136  * #define RPL_CALLBACK_PARENT_SWITCH tsch_rpl_callback_parent_switch
137  * */
138 
139  int i;
140  if(new != old) {
141  orchestra_parent_knows_us = 0;
142  }
143  for(i = 0; i < NUM_RULES; i++) {
144  if(all_rules[i]->new_time_source != NULL) {
145  all_rules[i]->new_time_source(old, new);
146  }
147  }
148 }
149 /*---------------------------------------------------------------------------*/
150 void
151 orchestra_init(void)
152 {
153  int i;
154  /* Snoop on packet transmission to know if our parent knows about us
155  * (i.e. has ACKed at one of our DAOs since we decided to use it as a parent) */
156  rime_sniffer_add(&orchestra_sniffer);
157  linkaddr_copy(&orchestra_parent_linkaddr, &linkaddr_null);
158  /* Initialize all Orchestra rules */
159  for(i = 0; i < NUM_RULES; i++) {
160  if(all_rules[i]->init != NULL) {
161  PRINTF("Orchestra: initializing rule %u\n", i);
162  all_rules[i]->init(i);
163  }
164  }
165  PRINTF("Orchestra: initialization done\n");
166 }
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
Orchestra header file
const linkaddr_t linkaddr_null
The null Rime address.
Definition: eth-conf.c:37
#define ICMP6_RPL
RPL.
Definition: uip-icmp6.h:66
Header file for the Rime buffer (packetbuf) management
Header file for the Rime stack
The MAC layer transmission was OK.
Definition: mac.h:79
#define NULL
The null pointer.
Header file for ICMPv6 message and error handing (RFC 4443)
A set of debugging macros for the IP stack
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
Definition: linkaddr.c:66
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60