Contiki 3.x
orchestra-rule-unicast-per-neighbor-rpl-storing.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  * \file
32  * Orchestra: a slotframe dedicated to unicast data transmission. Designed for
33  * RPL storing mode only, as this is based on the knowledge of the children (and parent).
34  * If receiver-based:
35  * Nodes listen at a timeslot defined as hash(MAC) % ORCHESTRA_SB_UNICAST_PERIOD
36  * Nodes transmit at: for each nbr in RPL children and RPL preferred parent,
37  * hash(nbr.MAC) % ORCHESTRA_SB_UNICAST_PERIOD
38  * If sender-based: the opposite
39  *
40  * \author Simon Duquennoy <simonduq@sics.se>
41  */
42 
43 #include "contiki.h"
44 #include "orchestra.h"
45 #include "net/ipv6/uip-ds6-route.h"
46 #include "net/packetbuf.h"
47 #include "net/rpl/rpl-conf.h"
48 
49 #if ORCHESTRA_UNICAST_SENDER_BASED && ORCHESTRA_COLLISION_FREE_HASH
50 #define UNICAST_SLOT_SHARED_FLAG ((ORCHESTRA_UNICAST_PERIOD < (ORCHESTRA_MAX_HASH + 1)) ? LINK_OPTION_SHARED : 0)
51 #else
52 #define UNICAST_SLOT_SHARED_FLAG LINK_OPTION_SHARED
53 #endif
54 
55 static uint16_t slotframe_handle = 0;
56 static uint16_t channel_offset = 0;
57 static struct tsch_slotframe *sf_unicast;
58 
59 /*---------------------------------------------------------------------------*/
60 static uint16_t
61 get_node_timeslot(const linkaddr_t *addr)
62 {
63  if(addr != NULL && ORCHESTRA_UNICAST_PERIOD > 0) {
64  return ORCHESTRA_LINKADDR_HASH(addr) % ORCHESTRA_UNICAST_PERIOD;
65  } else {
66  return 0xffff;
67  }
68 }
69 /*---------------------------------------------------------------------------*/
70 static int
71 neighbor_has_uc_link(const linkaddr_t *linkaddr)
72 {
73  if(linkaddr != NULL && !linkaddr_cmp(linkaddr, &linkaddr_null)) {
74  if((orchestra_parent_knows_us || !ORCHESTRA_UNICAST_SENDER_BASED)
75  && linkaddr_cmp(&orchestra_parent_linkaddr, linkaddr)) {
76  return 1;
77  }
78  if(nbr_table_get_from_lladdr(nbr_routes, (linkaddr_t *)linkaddr) != NULL) {
79  return 1;
80  }
81  }
82  return 0;
83 }
84 /*---------------------------------------------------------------------------*/
85 static void
86 add_uc_link(const linkaddr_t *linkaddr)
87 {
88  if(linkaddr != NULL) {
89  uint16_t timeslot = get_node_timeslot(linkaddr);
90  uint8_t link_options = ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_RX : LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG;
91 
92  if(timeslot == get_node_timeslot(&linkaddr_node_addr)) {
93  /* This is also our timeslot, add necessary flags */
94  link_options |= ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG: LINK_OPTION_RX;
95  }
96 
97  /* Add/update link */
98  tsch_schedule_add_link(sf_unicast, link_options, LINK_TYPE_NORMAL, &tsch_broadcast_address,
99  timeslot, channel_offset);
100  }
101 }
102 /*---------------------------------------------------------------------------*/
103 static void
104 remove_uc_link(const linkaddr_t *linkaddr)
105 {
106  uint16_t timeslot;
107  struct tsch_link *l;
108 
109  if(linkaddr == NULL) {
110  return;
111  }
112 
113  timeslot = get_node_timeslot(linkaddr);
114  l = tsch_schedule_get_link_by_timeslot(sf_unicast, timeslot);
115  if(l == NULL) {
116  return;
117  }
118  /* Does our current parent need this timeslot? */
119  if(timeslot == get_node_timeslot(&orchestra_parent_linkaddr)) {
120  /* Yes, this timeslot is being used, return */
121  return;
122  }
123  /* Does any other child need this timeslot?
124  * (lookup all route next hops) */
125  nbr_table_item_t *item = nbr_table_head(nbr_routes);
126  while(item != NULL) {
127  linkaddr_t *addr = nbr_table_get_lladdr(nbr_routes, item);
128  if(timeslot == get_node_timeslot(addr)) {
129  /* Yes, this timeslot is being used, return */
130  return;
131  }
132  item = nbr_table_next(nbr_routes, item);
133  }
134 
135  /* Do we need this timeslot? */
136  if(timeslot == get_node_timeslot(&linkaddr_node_addr)) {
137  /* This is our link, keep it but update the link options */
138  uint8_t link_options = ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG: LINK_OPTION_RX;
139  tsch_schedule_add_link(sf_unicast, link_options, LINK_TYPE_NORMAL, &tsch_broadcast_address,
140  timeslot, channel_offset);
141  } else {
142  /* Remove link */
143  tsch_schedule_remove_link(sf_unicast, l);
144  }
145 }
146 /*---------------------------------------------------------------------------*/
147 static void
148 child_added(const linkaddr_t *linkaddr)
149 {
150  add_uc_link(linkaddr);
151 }
152 /*---------------------------------------------------------------------------*/
153 static void
154 child_removed(const linkaddr_t *linkaddr)
155 {
156  remove_uc_link(linkaddr);
157 }
158 /*---------------------------------------------------------------------------*/
159 static int
160 select_packet(uint16_t *slotframe, uint16_t *timeslot)
161 {
162  /* Select data packets we have a unicast link to */
163  const linkaddr_t *dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
164  if(packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE) == FRAME802154_DATAFRAME
165  && neighbor_has_uc_link(dest)) {
166  if(slotframe != NULL) {
167  *slotframe = slotframe_handle;
168  }
169  if(timeslot != NULL) {
170  *timeslot = ORCHESTRA_UNICAST_SENDER_BASED ? get_node_timeslot(&linkaddr_node_addr) : get_node_timeslot(dest);
171  }
172  return 1;
173  }
174  return 0;
175 }
176 /*---------------------------------------------------------------------------*/
177 static void
178 new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
179 {
180  if(new != old) {
181  const linkaddr_t *old_addr = old != NULL ? &old->addr : NULL;
182  const linkaddr_t *new_addr = new != NULL ? &new->addr : NULL;
183  if(new_addr != NULL) {
184  linkaddr_copy(&orchestra_parent_linkaddr, new_addr);
185  } else {
186  linkaddr_copy(&orchestra_parent_linkaddr, &linkaddr_null);
187  }
188  remove_uc_link(old_addr);
189  add_uc_link(new_addr);
190  }
191 }
192 /*---------------------------------------------------------------------------*/
193 static void
194 init(uint16_t sf_handle)
195 {
196  slotframe_handle = sf_handle;
197  channel_offset = sf_handle;
198  /* Slotframe for unicast transmissions */
199  sf_unicast = tsch_schedule_add_slotframe(slotframe_handle, ORCHESTRA_UNICAST_PERIOD);
200  uint16_t timeslot = get_node_timeslot(&linkaddr_node_addr);
201  tsch_schedule_add_link(sf_unicast,
202  ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG: LINK_OPTION_RX,
203  LINK_TYPE_NORMAL, &tsch_broadcast_address,
204  timeslot, channel_offset);
205 }
206 /*---------------------------------------------------------------------------*/
207 struct orchestra_rule unicast_per_neighbor_rpl_storing = {
208  init,
209  new_time_source,
210  select_packet,
211  child_added,
212  child_removed,
213 };
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
Header file for routing table manipulation.
Orchestra header file
const linkaddr_t linkaddr_null
The null Rime address.
Definition: eth-conf.c:37
Header file for the Rime buffer (packetbuf) management
#define NULL
The null pointer.
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
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48