Contiki-NG
orchestra-rule-eb-per-time-source.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 
33  * Orchestra: a slotframe dedicated to transmission of EBs.
34  * Nodes transmit at a timeslot defined as hash(MAC) % ORCHESTRA_EBSF_PERIOD
35  * Nodes listen at a timeslot defined as hash(time_source.MAC) % ORCHESTRA_EBSF_PERIOD
36  * \author Simon Duquennoy <simonduq@sics.se>
37  */
38 
39 #include "contiki.h"
40 #include "orchestra.h"
41 #include "net/packetbuf.h"
42 
43 static uint16_t slotframe_handle = 0;
44 static uint16_t channel_offset = 0;
45 static struct tsch_slotframe *sf_eb;
46 
47 /*---------------------------------------------------------------------------*/
48 static uint16_t
49 get_node_timeslot(const linkaddr_t *addr)
50 {
51 #if ORCHESTRA_EBSF_PERIOD > 0
52  return ORCHESTRA_LINKADDR_HASH(addr) % ORCHESTRA_EBSF_PERIOD;
53 #else
54  return 0xffff;
55 #endif
56 }
57 /*---------------------------------------------------------------------------*/
58 static int
59 select_packet(uint16_t *slotframe, uint16_t *timeslot)
60 {
61  /* Select EBs only */
62  if(packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE) == FRAME802154_BEACONFRAME) {
63  if(slotframe != NULL) {
64  *slotframe = slotframe_handle;
65  }
66  if(timeslot != NULL) {
67  *timeslot = get_node_timeslot(&linkaddr_node_addr);
68  }
69  return 1;
70  }
71  return 0;
72 }
73 /*---------------------------------------------------------------------------*/
74 static void
75 new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
76 {
77  uint16_t old_ts = old != NULL ? get_node_timeslot(&old->addr) : 0xffff;
78  uint16_t new_ts = new != NULL ? get_node_timeslot(&new->addr) : 0xffff;
79 
80  if(new_ts == old_ts) {
81  return;
82  }
83 
84  if(old_ts != 0xffff) {
85  /* Stop listening to the old time source's EBs */
86  if(old_ts == get_node_timeslot(&linkaddr_node_addr)) {
87  /* This was the same timeslot as slot. Reset original link options */
88  tsch_schedule_add_link(sf_eb, LINK_OPTION_TX, LINK_TYPE_ADVERTISING_ONLY,
89  &tsch_broadcast_address, old_ts, 0);
90  } else {
91  /* Remove slot */
93  }
94  }
95  if(new_ts != 0xffff) {
96  uint8_t link_options = LINK_OPTION_RX;
97  if(new_ts == get_node_timeslot(&linkaddr_node_addr)) {
98  /* This is also our timeslot, add necessary flags */
99  link_options |= LINK_OPTION_TX;
100  }
101  /* Listen to the time source's EBs */
102  tsch_schedule_add_link(sf_eb, link_options, LINK_TYPE_ADVERTISING_ONLY,
103  &tsch_broadcast_address, new_ts, 0);
104  }
105 }
106 /*---------------------------------------------------------------------------*/
107 static void
108 init(uint16_t sf_handle)
109 {
110  slotframe_handle = sf_handle;
111  channel_offset = sf_handle;
112  sf_eb = tsch_schedule_add_slotframe(slotframe_handle, ORCHESTRA_EBSF_PERIOD);
113  /* EB link: every neighbor uses its own to avoid contention */
115  LINK_OPTION_TX,
116  LINK_TYPE_ADVERTISING_ONLY, &tsch_broadcast_address,
117  get_node_timeslot(&linkaddr_node_addr), 0);
118 }
119 /*---------------------------------------------------------------------------*/
120 struct orchestra_rule eb_per_time_source = {
121  init,
122  new_time_source,
123  select_packet,
124  NULL,
125  NULL,
126 };
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:116
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:72
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)
Removes a link from a slotframe and timeslot.
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)
Adds a link to a slotframe.
Header file for the Packet buffer (packetbuf) management