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. Does not require
34  * any knowledge of the children. Works only as received-base, and as follows:
35  * Nodes listen at a timeslot defined as hash(MAC) % ORCHESTRA_SB_UNICAST_PERIOD
36  * Nodes transmit at: for any neighbor, hash(nbr.MAC) % ORCHESTRA_SB_UNICAST_PERIOD
37  *
38  * \author Simon Duquennoy <simon.duquennoy@inria.fr>
39  */
40 
41 #include "contiki.h"
42 #include "orchestra.h"
43 #include "net/ipv6/uip-ds6-route.h"
44 #include "net/packetbuf.h"
45 
46 static uint16_t slotframe_handle = 0;
47 static struct tsch_slotframe *sf_unicast;
48 
49 /*---------------------------------------------------------------------------*/
50 static uint16_t
51 get_node_timeslot(const linkaddr_t *addr)
52 {
53  if(addr != NULL && ORCHESTRA_UNICAST_PERIOD > 0) {
54  return ORCHESTRA_LINKADDR_HASH(addr) % ORCHESTRA_UNICAST_PERIOD;
55  } else {
56  return 0xffff;
57  }
58 }
59 /*---------------------------------------------------------------------------*/
60 static uint16_t
61 get_node_channel_offset(const linkaddr_t *addr)
62 {
63  if(addr != NULL && ORCHESTRA_UNICAST_MAX_CHANNEL_OFFSET >= ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET) {
64  return ORCHESTRA_LINKADDR_HASH(addr) % (ORCHESTRA_UNICAST_MAX_CHANNEL_OFFSET - ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET + 1)
65  + ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET;
66  } else {
67  return 0xffff;
68  }
69 }
70 /*---------------------------------------------------------------------------*/
71 static void
72 child_added(const linkaddr_t *linkaddr)
73 {
74 }
75 /*---------------------------------------------------------------------------*/
76 static void
77 child_removed(const linkaddr_t *linkaddr)
78 {
79 }
80 /*---------------------------------------------------------------------------*/
81 static int
82 select_packet(uint16_t *slotframe, uint16_t *timeslot, uint16_t *channel_offset)
83 {
84  /* Select data packets we have a unicast link to */
85  const linkaddr_t *dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
86  if(packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE) == FRAME802154_DATAFRAME
87  && !linkaddr_cmp(dest, &linkaddr_null)) {
88  if(slotframe != NULL) {
89  *slotframe = slotframe_handle;
90  }
91  if(timeslot != NULL) {
92  *timeslot = get_node_timeslot(dest);
93  }
94  /* set per-packet channel offset */
95  if(channel_offset != NULL) {
96  *channel_offset = get_node_channel_offset(dest);
97  }
98  return 1;
99  }
100  return 0;
101 }
102 /*---------------------------------------------------------------------------*/
103 static void
104 new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
105 {
106 }
107 /*---------------------------------------------------------------------------*/
108 static void
109 init(uint16_t sf_handle)
110 {
111  int i;
112  uint16_t rx_timeslot;
113  linkaddr_t *local_addr = &linkaddr_node_addr;
114 
115  slotframe_handle = sf_handle;
116  /* Slotframe for unicast transmissions */
117  sf_unicast = tsch_schedule_add_slotframe(slotframe_handle, ORCHESTRA_UNICAST_PERIOD);
118  rx_timeslot = get_node_timeslot(local_addr);
119  /* Add a Tx link at each available timeslot. Make the link Rx at our own timeslot. */
120  for(i = 0; i < ORCHESTRA_UNICAST_PERIOD; i++) {
121  tsch_schedule_add_link(sf_unicast,
122  LINK_OPTION_SHARED | LINK_OPTION_TX | ( i == rx_timeslot ? LINK_OPTION_RX : 0 ),
123  LINK_TYPE_NORMAL, &tsch_broadcast_address,
124  i, get_node_channel_offset(local_addr), 1);
125  }
126 }
127 /*---------------------------------------------------------------------------*/
128 struct orchestra_rule unicast_per_neighbor_rpl_ns = {
129  init,
130  new_time_source,
131  select_packet,
132  child_added,
133  child_removed,
134  "unicast per neighbor non-storing",
135  ORCHESTRA_UNICAST_PERIOD,
136 };
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:107
TSCH neighbor information.
Definition: tsch-types.h:109
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
const linkaddr_t linkaddr_null
The null link-layer address.
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition: linkaddr.c:48
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 linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition: linkaddr.c:69
Header file for routing table manipulation.
Header file for the Packet buffer (packetbuf) management