Contiki-NG
orchestra-rule-special-for-root.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, Amber Agriculture
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the Institute nor the names of its contributors
13  * may be used to endorse or promote products derived from this software
14  * without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 /**
30  * \file
31  * Orchestra: a slotframe dedicated to unicast data transmission to the root.
32  * See the paper "TSCH for Long Range Low Data Rate Applications", IEEE Access
33  *
34  * Warning: the root rule should not be used together with the NS rule!
35  * The issue is in the prioritization of the different rules - NS rule is
36  * prioritized over the root rule, because it has a lower slotframe handle.
37  * In the NS rule, all slots potentially are active, and all-but-one slots are for Tx.
38  * This leads to a NS-rule slot is always being selected over a root-rule slot.
39  *
40  * \author Atis Elsts <atis.elsts@gmail.com>
41  */
42 
43 #include "contiki.h"
44 #include "orchestra.h"
45 
46 #include "sys/log.h"
47 #define LOG_MODULE "Orchestra"
48 #define LOG_LEVEL LOG_LEVEL_MAC
49 
50 static struct tsch_slotframe *sf_tx;
51 static struct tsch_slotframe *sf_rx;
52 static uint8_t is_root_rule_used;
53 static uint16_t timeslot_tx;
54 
55 static void set_self_to_root(uint8_t is_root);
56 /*---------------------------------------------------------------------------*/
57 static inline uint8_t
58 self_is_root(void)
59 {
60  /* If this is changed to look at the RPL status instead of tsch_is_coordinator,
61  * multiple roots become possible in a single TSCH network. */
62  return tsch_is_coordinator;
63 }
64 /*---------------------------------------------------------------------------*/
65 uint8_t
66 orchestra_is_root_schedule_active(const linkaddr_t *addr)
67 {
68  return is_root_rule_used && tsch_roots_is_root(addr);
69 }
70 /*---------------------------------------------------------------------------*/
71 static uint16_t
72 get_node_timeslot(const linkaddr_t *addr)
73 {
74  if(addr != NULL && ORCHESTRA_ROOT_PERIOD > 0) {
75  return ORCHESTRA_LINKADDR_HASH(addr) % ORCHESTRA_ROOT_PERIOD;
76  } else {
77  return 0xffff;
78  }
79 }
80 /*---------------------------------------------------------------------------*/
81 static uint16_t
82 get_node_channel_offset(const linkaddr_t *addr)
83 {
84  if(addr != NULL && ORCHESTRA_UNICAST_MAX_CHANNEL_OFFSET >= ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET) {
85  return ORCHESTRA_LINKADDR_HASH(addr) % (ORCHESTRA_UNICAST_MAX_CHANNEL_OFFSET - ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET + 1)
86  + ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET;
87  } else {
88  return 0xffff;
89  }
90 }
91 /*---------------------------------------------------------------------------*/
92 static int
93 select_packet(uint16_t *slotframe, uint16_t *timeslot, uint16_t *channel_offset)
94 {
95  /* Select data packets to a root node, in case we are not the root ourselves */
96  const linkaddr_t *dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
97  if(!self_is_root()
98  && sf_tx != NULL
99  && packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE) == FRAME802154_DATAFRAME
100  && dest != NULL
101  && orchestra_is_root_schedule_active(dest)) {
102  if(slotframe != NULL) {
103  *slotframe = sf_tx->handle;
104  }
105  if(timeslot != NULL) {
106  *timeslot = timeslot_tx;
107  }
108  /* set per-packet channel offset */
109  if(channel_offset != NULL) {
110  *channel_offset = get_node_channel_offset(dest);
111  }
112  /* XXX: this should be removed later when the root rule is better tested */
113  LOG_INFO("use the root rule for root node ");
114  LOG_INFO_LLADDR(dest);
115  LOG_INFO_("\n");
116  return 1;
117  }
118  return 0;
119 }
120 /*---------------------------------------------------------------------------*/
121 static void
122 init(uint16_t sf_handle)
123 {
124  /* signal that the root rule is used */
125  is_root_rule_used = 1;
126 
127  /* Add a slotframe for unicast transmission to (other) root nodes, initially empty */
128  timeslot_tx = get_node_timeslot(&linkaddr_node_addr);
129  sf_tx = tsch_schedule_add_slotframe(sf_handle, ORCHESTRA_ROOT_PERIOD);
130  if(sf_tx == NULL) {
131  LOG_ERR("failed to add a slotframe for transmissions\n");
132  }
133 
134  if(tsch_is_coordinator) {
135  /* perform the deferred addition of the root slotframe */
136  set_self_to_root(1);
137  }
138 }
139 /*---------------------------------------------------------------------------*/
140 static void
141 set_self_to_root(uint8_t is_root)
142 {
143  const uint16_t slotframe_rx_handle = (sf_tx == NULL ? (uint16_t)-1 : (sf_tx->handle | 0x8000));
144 
145  if(is_root_rule_used == 0) {
146  return; /* defer addition until this rule is initialized */
147  }
148 
149  if(is_root) {
150  /* potentially becomes root */
151  if(sf_rx == NULL) {
152  /* Add a 1-slot long slotframe for unicast reception */
153  sf_rx = tsch_schedule_add_slotframe(slotframe_rx_handle, 1);
154  if(sf_rx == NULL) {
155  LOG_ERR("failed to add a slotframe for reception\n");
156  return;
157  }
158  /* Add a Rx link to this slotframe */
160  LINK_OPTION_SHARED | LINK_OPTION_RX,
161  LINK_TYPE_NORMAL, &tsch_broadcast_address,
162  0, get_node_channel_offset(&linkaddr_node_addr), 1);
163  }
164  } else {
165  /* not a root anymore */
166  if(sf_rx != NULL) {
168  sf_rx = NULL;
169  }
170  }
171 }
172 /*---------------------------------------------------------------------------*/
173 static void
174 root_node_updated(const linkaddr_t *root, uint8_t is_added)
175 {
176  if(linkaddr_cmp(root, &linkaddr_node_addr)) {
177  /* special handling in case the local node becomes a root */
178  set_self_to_root(is_added);
179  return;
180  }
181 
182  if(is_added) {
183  /* Note in case multiple roots are used:
184  * if there are multiple roots in a direct reach, a TSCH cell is added to each of them,
185  * at the same timeslot. In each slotframe, the root node will be selected randomly.
186  * This means that the cell's efficiency is reduced to 1/N of its full capacity,
187  * where N is the number of directly reachable roots.
188  */
190  LINK_OPTION_SHARED | LINK_OPTION_TX,
191  LINK_TYPE_NORMAL, root,
192  timeslot_tx, get_node_channel_offset(root), 0);
193  } else {
194  tsch_schedule_remove_link_by_timeslot(sf_tx, timeslot_tx, get_node_channel_offset(root));
196  }
197 }
198 /*---------------------------------------------------------------------------*/
199 struct orchestra_rule special_for_root = {
200  init,
201  NULL,
202  select_packet,
203  NULL,
204  NULL,
205  root_node_updated,
206  "special for root",
207  ORCHESTRA_ROOT_PERIOD,
208 };
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:107
802.15.4e slotframe (contains links)
Definition: tsch-types.h:84
Orchestra header file
struct tsch_slotframe * tsch_schedule_add_slotframe(uint16_t handle, uint16_t size)
Creates and adds a new slotframe.
Definition: tsch-schedule.c:73
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition: linkaddr.c:48
int tsch_schedule_remove_link_by_timeslot(struct tsch_slotframe *slotframe, uint16_t timeslot, uint16_t channel_offset)
Removes a link from a slotframe and timeslot.
int tsch_roots_is_root(const linkaddr_t *address)
Tests whether a given address belongs to a single-hop reachable root node in this network...
Definition: tsch-roots.c:176
struct tsch_link * tsch_schedule_add_link(struct tsch_slotframe *slotframe, uint8_t link_options, enum link_type link_type, const linkaddr_t *address, uint16_t timeslot, uint16_t channel_offset, uint8_t do_remove)
Adds a link to a slotframe.
int tsch_schedule_remove_slotframe(struct tsch_slotframe *slotframe)
Removes a slotframe.
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition: linkaddr.c:69
Header file for the logging system
void tsch_queue_free_packets_to(const linkaddr_t *addr)
Flush packets to a specific address.
Definition: tsch-queue.c:324