Contiki-NG
orchestra-rule-unicast-per-neighbor-rpl-ns.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016, Inria.
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 primarily
33 * for RPL non-storing mode but would work with any mode-of-operation.
34 * Uses uIPv6 DS6 neighhbor tables to obtain knowledge of the children.
35 * Only supports the receiver based mode, and works as follows:
36 * Nodes listen at a timeslot defined as hash(MAC) % ORCHESTRA_SB_UNICAST_PERIOD
37 * Nodes transmit at: for any neighbor, hash(nbr.MAC) % ORCHESTRA_SB_UNICAST_PERIOD
38 *
39 * \author Simon Duquennoy <simon.duquennoy@inria.fr>
40 * Atis Elsts <atis.elsts@edi.lv>
41 */
42
43#include "contiki.h"
44#include "orchestra.h"
46#include "net/packetbuf.h"
47
48static uint16_t slotframe_handle = 0;
49static struct tsch_slotframe *sf_unicast;
50
51/*---------------------------------------------------------------------------*/
52static uint16_t
53get_node_timeslot(const linkaddr_t *addr)
54{
55 if(addr != NULL && ORCHESTRA_UNICAST_PERIOD > 0) {
56 return ORCHESTRA_LINKADDR_HASH(addr) % ORCHESTRA_UNICAST_PERIOD;
57 } else {
58 return 0xffff;
59 }
60}
61/*---------------------------------------------------------------------------*/
62static uint16_t
63get_node_channel_offset(const linkaddr_t *addr)
64{
65 if(addr != NULL && ORCHESTRA_UNICAST_MAX_CHANNEL_OFFSET >= ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET) {
66 return ORCHESTRA_LINKADDR_HASH(addr) % (ORCHESTRA_UNICAST_MAX_CHANNEL_OFFSET - ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET + 1)
67 + ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET;
68 } else {
69 return 0xffff;
70 }
71}
72/*---------------------------------------------------------------------------*/
73static void
74add_uc_link(const linkaddr_t *linkaddr)
75{
76 if(linkaddr != NULL) {
77 uint16_t timeslot = get_node_timeslot(linkaddr);
78
79 /* Add a Tx link to the neighbor; do not replace any existing links
80 * at that cell. The channel offset here does not matter:
81 * select_packet() always sets the right channel offset per packet. */
82 tsch_schedule_add_link(sf_unicast,
83 LINK_OPTION_SHARED | LINK_OPTION_TX,
84 LINK_TYPE_NORMAL, &tsch_broadcast_address,
85 timeslot, 0, 0);
86 }
87}
88/*---------------------------------------------------------------------------*/
89static void
90remove_uc_link(const linkaddr_t *linkaddr)
91{
92 if(linkaddr != NULL) {
93 uint16_t timeslot = get_node_timeslot(linkaddr);
94 tsch_schedule_remove_link_by_offsets(sf_unicast, timeslot, 0);
96 }
97}
98/*---------------------------------------------------------------------------*/
99static void
100neighbor_updated(const linkaddr_t *linkaddr, uint8_t is_added)
101{
102 if(is_added) {
103 add_uc_link(linkaddr);
104 } else {
105 remove_uc_link(linkaddr);
106 }
107}
108/*---------------------------------------------------------------------------*/
109static int
110select_packet(uint16_t *slotframe, uint16_t *timeslot, uint16_t *channel_offset)
111{
112 /* Select data packets we have a unicast link to */
113 const linkaddr_t *dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
114 if(packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE) == FRAME802154_DATAFRAME
115 && !orchestra_is_root_schedule_active(dest)
116 && !linkaddr_cmp(dest, &linkaddr_null)) {
117 if(slotframe != NULL) {
118 *slotframe = slotframe_handle;
119 }
120 if(timeslot != NULL) {
121 *timeslot = get_node_timeslot(dest);
122 }
123 /* set per-packet channel offset */
124 if(channel_offset != NULL) {
125 *channel_offset = get_node_channel_offset(dest);
126 }
127 return 1;
128 }
129 return 0;
130}
131/*---------------------------------------------------------------------------*/
132static void
133new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
134{
135 if(new != old) {
136 const linkaddr_t *old_addr = tsch_queue_get_nbr_address(old);
137 const linkaddr_t *new_addr = tsch_queue_get_nbr_address(new);
138 remove_uc_link(old_addr);
139 add_uc_link(new_addr);
140 }
141}
142/*---------------------------------------------------------------------------*/
143static void
144init(uint16_t sf_handle)
145{
146 uint16_t rx_timeslot;
147 linkaddr_t *local_addr = &linkaddr_node_addr;
148
149 slotframe_handle = sf_handle;
150 /* Slotframe for unicast transmissions */
151 sf_unicast = tsch_schedule_add_slotframe(slotframe_handle, ORCHESTRA_UNICAST_PERIOD);
152 rx_timeslot = get_node_timeslot(local_addr);
153 /* Add a Rx link at our own timeslot. */
154 tsch_schedule_add_link(sf_unicast,
155 LINK_OPTION_RX,
156 LINK_TYPE_NORMAL, &tsch_broadcast_address,
157 rx_timeslot, get_node_channel_offset(local_addr), 1);
158}
159/*---------------------------------------------------------------------------*/
160struct orchestra_rule unicast_per_neighbor_rpl_ns = {
161 init,
162 new_time_source,
163 select_packet,
164 NULL,
165 NULL,
166 neighbor_updated,
167 NULL,
168 "unicast per neighbor non-storing",
169 ORCHESTRA_UNICAST_PERIOD,
170};
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition: linkaddr.c:48
bool linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition: linkaddr.c:69
const linkaddr_t linkaddr_null
The null link-layer address.
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_link_by_offsets(struct tsch_slotframe *slotframe, uint16_t timeslot, uint16_t channel_offset)
Removes a link from a slotframe and timeslot + channel offset.
void tsch_queue_free_packets_to(const linkaddr_t *addr)
Flush packets to a specific address.
Definition: tsch-queue.c:324
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 * tsch_queue_get_nbr_address(const struct tsch_neighbor *n)
Get the address of a neighbor.
Definition: tsch-queue.c:135
Orchestra header file.
Header file for the Packet buffer (packetbuf) management.
TSCH neighbor information.
Definition: tsch-types.h:109
802.15.4e slotframe (contains links)
Definition: tsch-types.h:84
Header file for routing table manipulation.
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:107