Contiki-NG
tsch.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, SICS Swedish ICT.
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  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34 * \ingroup link-layer
35 * \defgroup tsch 802.15.4 TSCH
36 The IEEE 802.15.4-2015 TimeSlotted Channel Hopping (TSCH) protocol. Provides
37 scheduled communication on top of a globally-synchronized network. Performs
38 frequency hopping for enhanced reliability.
39 * @{
40 * \file
41 * Main API declarations for TSCH.
42 */
43 
44 #ifndef __TSCH_H__
45 #define __TSCH_H__
46 
47 /********** Includes **********/
48 
49 #include "contiki.h"
50 #include "net/mac/mac.h"
51 #include "net/linkaddr.h"
52 
53 #include "net/mac/tsch/tsch-conf.h"
59 #include "net/mac/tsch/tsch-log.h"
64 #include "net/mac/tsch/tsch-roots.h"
65 #if UIP_CONF_IPV6_RPL
66 #include "net/mac/tsch/tsch-rpl.h"
67 #endif /* UIP_CONF_IPV6_RPL */
68 
69 
70 /* Include Arch-Specific conf */
71 #ifdef TSCH_CONF_ARCH_HDR_PATH
72 #include TSCH_CONF_ARCH_HDR_PATH
73 #endif /* TSCH_CONF_ARCH_HDR_PATH */
74 
75 /*********** Callbacks *********/
76 
77 /* Link callbacks to RPL in case RPL is enabled */
78 #if UIP_CONF_IPV6_RPL
79 
80 #ifndef TSCH_CALLBACK_JOINING_NETWORK
81 #define TSCH_CALLBACK_JOINING_NETWORK tsch_rpl_callback_joining_network
82 #endif /* TSCH_CALLBACK_JOINING_NETWORK */
83 
84 #ifndef TSCH_CALLBACK_LEAVING_NETWORK
85 #define TSCH_CALLBACK_LEAVING_NETWORK tsch_rpl_callback_leaving_network
86 #endif /* TSCH_CALLBACK_LEAVING_NETWORK */
87 
88 #ifndef TSCH_CALLBACK_KA_SENT
89 #define TSCH_CALLBACK_KA_SENT tsch_rpl_callback_ka_sent
90 #endif /* TSCH_CALLBACK_KA_SENT */
91 
92 #ifndef TSCH_RPL_CHECK_DODAG_JOINED
93 #define TSCH_RPL_CHECK_DODAG_JOINED tsch_rpl_check_dodag_joined
94 #endif /* TSCH_RPL_CHECK_DODAG_JOINED */
95 
96 #endif /* UIP_CONF_IPV6_RPL */
97 
98 #if BUILD_WITH_ORCHESTRA
99 
100 #ifndef TSCH_CALLBACK_NEW_TIME_SOURCE
101 #define TSCH_CALLBACK_NEW_TIME_SOURCE orchestra_callback_new_time_source
102 #endif /* TSCH_CALLBACK_NEW_TIME_SOURCE */
103 
104 #ifndef TSCH_CALLBACK_PACKET_READY
105 #define TSCH_CALLBACK_PACKET_READY orchestra_callback_packet_ready
106 #endif /* TSCH_CALLBACK_PACKET_READY */
107 
108 #ifndef TSCH_CALLBACK_ROOT_NODE_UPDATED
109 #define TSCH_CALLBACK_ROOT_NODE_UPDATED orchestra_callback_root_node_updated
110 #endif /* TSCH_CALLBACK_ROOT_NODE_UPDATED */
111 
112 #endif /* BUILD_WITH_ORCHESTRA */
113 
114 /* Called by TSCH when joining a network */
115 #ifdef TSCH_CALLBACK_JOINING_NETWORK
116 void TSCH_CALLBACK_JOINING_NETWORK();
117 #endif
118 
119 /* Called by TSCH when leaving a network */
120 #ifdef TSCH_CALLBACK_LEAVING_NETWORK
121 void TSCH_CALLBACK_LEAVING_NETWORK();
122 #endif
123 
124 /* Called by TSCH after sending a keep-alive */
125 #ifdef TSCH_CALLBACK_KA_SENT
126 void TSCH_CALLBACK_KA_SENT();
127 #endif
128 
129 /* Called by TSCH before sending a EB */
130 #ifdef TSCH_RPL_CHECK_DODAG_JOINED
131 int TSCH_RPL_CHECK_DODAG_JOINED();
132 #endif
133 
134 /* Called by TSCH form interrupt after receiving a frame, enabled upper-layer to decide
135  * whether to ACK or NACK */
136 #ifdef TSCH_CALLBACK_DO_NACK
137 int TSCH_CALLBACK_DO_NACK(struct tsch_link *link, linkaddr_t *src, linkaddr_t *dst);
138 #endif
139 
140 /* Called by TSCH when switching time source */
141 #ifdef TSCH_CALLBACK_NEW_TIME_SOURCE
142 struct tsch_neighbor;
143 void TSCH_CALLBACK_NEW_TIME_SOURCE(const struct tsch_neighbor *old, const struct tsch_neighbor *new);
144 #endif
145 
146 /* Called by TSCH every time a packet is ready to be added to the send queue */
147 #ifdef TSCH_CALLBACK_PACKET_READY
148 int TSCH_CALLBACK_PACKET_READY(void);
149 #endif
150 
151 /* Called when a new root node, including the local node, is detected to be added or removed */
152 #ifdef TSCH_CALLBACK_ROOT_NODE_UPDATED
153 void TSCH_CALLBACK_ROOT_NODE_UPDATED(const linkaddr_t *, uint8_t is_added);
154 #endif /* TSCH_CALLBACK_ROOT_NODE_UPDATED */
155 
156 
157 /***** External Variables *****/
158 
159 /* Are we coordinator of the TSCH network? */
160 extern int tsch_is_coordinator;
161 /* Are we associated to a TSCH network? */
162 extern int tsch_is_associated;
163 /* Is the PAN running link-layer security? */
164 extern int tsch_is_pan_secured;
165 /* The TSCH MAC driver */
166 extern const struct mac_driver tschmac_driver;
167 /* 802.15.4 broadcast MAC address */
168 extern const linkaddr_t tsch_broadcast_address;
169 /* The address we use to identify EB queue */
170 extern const linkaddr_t tsch_eb_address;
171 /* The current Absolute Slot Number (ASN) */
172 extern struct tsch_asn_t tsch_current_asn;
173 extern uint8_t tsch_join_priority;
174 extern struct tsch_link *current_link;
175 /* If we are inside a slot, these tell the current channel and channel offset */
176 extern uint8_t tsch_current_channel;
177 extern uint8_t tsch_current_channel_offset;
178 /* TSCH channel hopping sequence */
179 extern uint8_t tsch_hopping_sequence[TSCH_HOPPING_SEQUENCE_MAX_LEN];
180 extern struct tsch_asn_divisor_t tsch_hopping_sequence_length;
181 /* TSCH timeslot timing (in micro-second) */
182 extern tsch_timeslot_timing_usec tsch_timing_us;
183 /* TSCH timeslot timing (in rtimer ticks) */
184 extern tsch_timeslot_timing_ticks tsch_timing;
185 /* Statistics on the current session */
186 extern unsigned long tx_count;
187 extern unsigned long rx_count;
188 extern unsigned long sync_count;
189 extern int32_t min_drift_seen;
190 extern int32_t max_drift_seen;
191 /* The TSCH standard 10ms timeslot timing */
193 
194 /* TSCH processes */
195 PROCESS_NAME(tsch_process);
196 PROCESS_NAME(tsch_send_eb_process);
197 PROCESS_NAME(tsch_pending_events_process);
198 
199 
200 /********** Functions *********/
201 
202 /**
203  * Set the TSCH join priority (JP)
204  *
205  * \param jp the new join priority
206  */
207 void tsch_set_join_priority(uint8_t jp);
208 /**
209  * Set the period at wich TSCH enhanced beacons (EBs) are sent. The period can
210  * not be set to exceed TSCH_MAX_EB_PERIOD. Set to 0 to stop sending EBs.
211  * Actual transmissions are jittered, spaced by a random number within
212  * [period*0.75, period[
213  * If RPL is used, the period will be automatically reset by RPL
214  * equal to the DIO period whenever the DIO period changes.
215  * Hence, calling `tsch_set_eb_period(0)` is NOT sufficient to disable sending EB!
216  * To do that, either configure the node in RPL leaf mode, or
217  * use static config for TSCH (`define TSCH_CONF_EB_PERIOD 0`).
218  *
219  * \param period The period in Clock ticks.
220  */
221 void tsch_set_eb_period(uint32_t period);
222 /**
223  * Set the desynchronization timeout after which a node sends a unicasst
224  * keep-alive (KA) to its time source. Set to 0 to stop sending KAs. The
225  * actual timeout is a random number within [timeout*0.9, timeout[
226  * Can be called from an interrupt.
227  *
228  * \param timeout The timeout in Clock ticks.
229  */
230 void tsch_set_ka_timeout(uint32_t timeout);
231 /**
232  * Set the node as PAN coordinator
233  *
234  * \param enable 1 to be coordinator, 0 to be a node
235  */
236 void tsch_set_coordinator(int enable);
237 /**
238  * Enable/disable security. If done at the coordinator, the Information
239  * will be included in EBs, and all nodes will adopt the same security level.
240  * Enabling requires compilation with LLSEC802154_ENABLED set.
241  * Note: when LLSEC802154_ENABLED is set, nodes boot with security enabled.
242  *
243  * \param enable 1 to enable security, 0 to disable it
244  */
245 void tsch_set_pan_secured(int enable);
246 /**
247  * Schedule a keep-alive transmission within [timeout*0.9, timeout[
248  * Can be called from an interrupt.
249  * @see tsch_set_ka_timeout
250  *
251  * \param immediate send immediately when 1, schedule using current timeout when 0
252  */
253 void tsch_schedule_keepalive(int immediate);
254 /**
255  * Get the time, in clock ticks, since the TSCH network was started.
256  *
257  * \return The network uptime, or -1 if the node is not part of a TSCH network.
258  */
259 uint64_t tsch_get_network_uptime_ticks(void);
260 /**
261  * Leave the TSCH network we are currently in
262  */
263 void tsch_disassociate(void);
264 
265 #endif /* __TSCH_H__ */
266 /** @} */
TSCH constants.
TSCH configuration.
TSCH queues.
TSCH types.
TSCH-RPL interaction.
The structure of a MAC protocol driver in Contiki.
Definition: mac.h:62
void tsch_set_join_priority(uint8_t jp)
Set the TSCH join priority (JP)
Definition: tsch.c:185
Header file for the link-layer address representation
TSCH neighbor information.
Definition: tsch-types.h:109
void tsch_set_ka_timeout(uint32_t timeout)
Set the desynchronization timeout after which a node sends a unicasst keep-alive (KA) to its time sou...
Definition: tsch.c:191
TSCH runtime operation within timeslots.
rtimer_clock_t tsch_timeslot_timing_ticks[tsch_ts_elements_count]
TSCH timeslot timing elements in rtimer ticks.
Definition: tsch-types.h:143
For quick modulo operation on ASN.
Definition: tsch-asn.h:54
void tsch_set_eb_period(uint32_t period)
Set the period at wich TSCH enhanced beacons (EBs) are sent.
Definition: tsch.c:198
void tsch_schedule_keepalive(int immediate)
Schedule a keep-alive transmission within [timeout*0.9, timeout[ Can be called from an interrupt...
Definition: tsch.c:334
#define PROCESS_NAME(name)
Declare the name of a process.
Definition: process.h:286
TSCH adaptive time synchronization.
TSCH security.
uint64_t tsch_get_network_uptime_ticks(void)
Get the time, in clock ticks, since the TSCH network was started.
Header file for TSCH statistics
uint16_t tsch_timeslot_timing_usec[tsch_ts_elements_count]
TSCH timeslot timing elements in micro-seconds.
Definition: tsch-types.h:146
void tsch_disassociate(void)
Leave the TSCH network we are currently in.
Definition: tsch.c:577
void tsch_set_pan_secured(int enable)
Enable/disable security.
Definition: tsch.c:179
TSCH packet parsing and creation.
TSCH scheduling engine.
The ASN is an absolute slot number over 5 bytes.
Definition: tsch-asn.h:48
const tsch_timeslot_timing_usec tsch_timeslot_timing_us_10000
TSCH timing attributes and description.
MAC driver header file
void tsch_set_coordinator(int enable)
Set the node as PAN coordinator.
Definition: tsch.c:168
TSCH per-slot logging.