diff --git a/examples/6tisch/6p-packet/test-sf.c b/examples/6tisch/6p-packet/test-sf.c index 054186984..1a9830144 100644 --- a/examples/6tisch/6p-packet/test-sf.c +++ b/examples/6tisch/6p-packet/test-sf.c @@ -30,7 +30,7 @@ #include #include -#include +#include #include #include #include diff --git a/examples/6tisch/etsi-plugtest-2017/node.c b/examples/6tisch/etsi-plugtest-2017/node.c index 3e16666dd..3046bb291 100644 --- a/examples/6tisch/etsi-plugtest-2017/node.c +++ b/examples/6tisch/etsi-plugtest-2017/node.c @@ -41,7 +41,6 @@ #include "sys/log.h" #include "net/ipv6/uip-ds6-route.h" #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-log.h" #include "net/routing/routing.h" #include "serial-shell.h" #include "sf-plugtest.h" diff --git a/examples/6tisch/simple-node/node.c b/examples/6tisch/simple-node/node.c index 4fa416290..d2f75ae33 100644 --- a/examples/6tisch/simple-node/node.c +++ b/examples/6tisch/simple-node/node.c @@ -42,7 +42,6 @@ #include "net/ipv6/uip-ds6-route.h" #include "net/ipv6/uip-sr.h" #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-log.h" #include "net/routing/routing.h" #define DEBUG DEBUG_PRINT diff --git a/examples/6tisch/sixtop/node-sixtop.c b/examples/6tisch/sixtop/node-sixtop.c index 73e9fab2e..67bee378a 100755 --- a/examples/6tisch/sixtop/node-sixtop.c +++ b/examples/6tisch/sixtop/node-sixtop.c @@ -42,8 +42,6 @@ #include "sys/log.h" #include "net/ipv6/uip-ds6-route.h" #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-log.h" -#include "net/mac/tsch/tsch-schedule.h" #include "net/mac/tsch/sixtop/sixtop.h" #include "net/routing/routing.h" diff --git a/examples/6tisch/sixtop/sf-simple.c b/examples/6tisch/sixtop/sf-simple.c index e0ba44f21..2b2844dd2 100644 --- a/examples/6tisch/sixtop/sf-simple.c +++ b/examples/6tisch/sixtop/sf-simple.c @@ -40,7 +40,7 @@ #include "contiki-lib.h" #include "lib/assert.h" -#include "net/mac/tsch/tsch-schedule.h" +#include "net/mac/tsch/tsch.h" #include "net/mac/tsch/sixtop/sixtop.h" #include "net/mac/tsch/sixtop/sixp.h" #include "net/mac/tsch/sixtop/sixp-nbr.h" diff --git a/examples/platform-specific/jn516x/tsch/simple-sensor-network/node/node.c b/examples/platform-specific/jn516x/tsch/simple-sensor-network/node/node.c index 12350a5f4..42fa11fb7 100644 --- a/examples/platform-specific/jn516x/tsch/simple-sensor-network/node/node.c +++ b/examples/platform-specific/jn516x/tsch/simple-sensor-network/node/node.c @@ -34,11 +34,8 @@ #include "contiki.h" #include "net/netstack.h" -#include "net/mac/tsch/tsch-schedule.h" #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-private.h" #include "net/routing/routing.h" -#include "net/mac/tsch/tsch-schedule.h" #include "net/ipv6/uip-debug.h" #include "lib/random.h" #include "node-id.h" diff --git a/examples/platform-specific/jn516x/tsch/tx-power-verification/rpl-border-router/rpl-border-router.c b/examples/platform-specific/jn516x/tsch/tx-power-verification/rpl-border-router/rpl-border-router.c index 642e370d1..cf88ea467 100644 --- a/examples/platform-specific/jn516x/tsch/tx-power-verification/rpl-border-router/rpl-border-router.c +++ b/examples/platform-specific/jn516x/tsch/tx-power-verification/rpl-border-router/rpl-border-router.c @@ -39,7 +39,6 @@ #include "net/routing/routing.h" #include "simple-udp.h" #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-schedule.h" #include "net/netstack.h" #include "dev/slip.h" #include "coap-engine.h" diff --git a/os/net/mac/framer/frame802154e-ie.h b/os/net/mac/framer/frame802154e-ie.h index eac9045f3..2df163d7d 100644 --- a/os/net/mac/framer/frame802154e-ie.h +++ b/os/net/mac/framer/frame802154e-ie.h @@ -41,9 +41,13 @@ #define FRAME_802154E_H #include "contiki.h" -/* We need definitions from tsch-private.h for TSCH-specific information elements */ -#include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-private.h" +#include "net/mac/mac.h" +#include "net/linkaddr.h" +/* We need definitions from tsch.h for TSCH-specific information elements */ +#include "net/mac/tsch/tsch-conf.h" +#include "net/mac/tsch/tsch-const.h" +#include "net/mac/tsch/tsch-types.h" +#include "net/mac/tsch/tsch-asn.h" #define FRAME802154E_IE_MAX_LINKS 4 diff --git a/os/net/mac/tsch/tsch-adaptive-timesync.c b/os/net/mac/tsch/tsch-adaptive-timesync.c index 186d8d7ba..a23a4f1b0 100644 --- a/os/net/mac/tsch/tsch-adaptive-timesync.c +++ b/os/net/mac/tsch/tsch-adaptive-timesync.c @@ -44,9 +44,6 @@ */ #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-conf.h" -#include "net/mac/tsch/tsch-adaptive-timesync.h" -#include "net/mac/tsch/tsch-log.h" #include #if TSCH_ADAPTIVE_TIMESYNC diff --git a/os/net/mac/tsch/tsch-adaptive-timesync.h b/os/net/mac/tsch/tsch-adaptive-timesync.h index e8398fdc2..be61b9514 100644 --- a/os/net/mac/tsch/tsch-adaptive-timesync.h +++ b/os/net/mac/tsch/tsch-adaptive-timesync.h @@ -41,44 +41,6 @@ /********** Includes **********/ #include "contiki.h" -#include "net/mac/tsch/tsch-private.h" - -/******** Configuration *******/ - -/* Use SFD timestamp for synchronization? By default we merely rely on rtimer and busy wait - * until SFD is high, which we found to provide greater accuracy on JN516x and CC2420. - * Note: for association, however, we always use SFD timestamp to know the time of arrival - * of the EB (because we do not busy-wait for the whole scanning process) - * */ -#ifdef TSCH_CONF_RESYNC_WITH_SFD_TIMESTAMPS -#define TSCH_RESYNC_WITH_SFD_TIMESTAMPS TSCH_CONF_RESYNC_WITH_SFD_TIMESTAMPS -#else -#define TSCH_RESYNC_WITH_SFD_TIMESTAMPS 0 -#endif - -/* If enabled, remove jitter due to measurement errors */ -#ifdef TSCH_CONF_TIMESYNC_REMOVE_JITTER -#define TSCH_TIMESYNC_REMOVE_JITTER TSCH_CONF_TIMESYNC_REMOVE_JITTER -#else -#define TSCH_TIMESYNC_REMOVE_JITTER TSCH_RESYNC_WITH_SFD_TIMESTAMPS -#endif - -/* The jitter to remove in ticks. - * This should be the sum of measurement errors on Tx and Rx nodes. - * */ -#define TSCH_TIMESYNC_MEASUREMENT_ERROR US_TO_RTIMERTICKS(32) - -/* Base drift value. - * Used to compensate locally know inaccuracies, such as - * the effect of having a binary 32.768 kHz timer as the TSCH time base. */ -#ifdef TSCH_CONF_BASE_DRIFT_PPM -#define TSCH_BASE_DRIFT_PPM TSCH_CONF_BASE_DRIFT_PPM -#else -#define TSCH_BASE_DRIFT_PPM 0 -#endif - -/* The approximate number of slots per second */ -#define TSCH_SLOTS_PER_SECOND (1000000 / TSCH_DEFAULT_TS_TIMESLOT_LENGTH) /***** External Variables *****/ diff --git a/os/net/mac/tsch/tsch-conf.h b/os/net/mac/tsch/tsch-conf.h index 2934738b8..8c94fbc9d 100644 --- a/os/net/mac/tsch/tsch-conf.h +++ b/os/net/mac/tsch/tsch-conf.h @@ -49,19 +49,91 @@ #include "contiki.h" -/******** Configuration *******/ +/******** Configuration: synchronization *******/ -/* Default IEEE 802.15.4e hopping sequences, obtained from https://gist.github.com/twatteyne/2e22ee3c1a802b685695 */ -/* 16 channels, sequence length 16 */ -#define TSCH_HOPPING_SEQUENCE_16_16 (uint8_t[]){ 16, 17, 23, 18, 26, 15, 25, 22, 19, 11, 12, 13, 24, 14, 20, 21 } -/* 4 channels, sequence length 16 */ -#define TSCH_HOPPING_SEQUENCE_4_16 (uint8_t[]){ 20, 26, 25, 26, 15, 15, 25, 20, 26, 15, 26, 25, 20, 15, 20, 25 } -/* 4 channels, sequence length 4 */ -#define TSCH_HOPPING_SEQUENCE_4_4 (uint8_t[]){ 15, 25, 26, 20 } -/* 2 channels, sequence length 2 */ -#define TSCH_HOPPING_SEQUENCE_2_2 (uint8_t[]){ 20, 25 } -/* 1 channel, sequence length 1 */ -#define TSCH_HOPPING_SEQUENCE_1_1 (uint8_t[]){ 20 } +/* Max time before sending a unicast keep-alive message to the time source */ +#ifdef TSCH_CONF_KEEPALIVE_TIMEOUT +#define TSCH_KEEPALIVE_TIMEOUT TSCH_CONF_KEEPALIVE_TIMEOUT +#else +/* Time to desynch assuming a drift of 40 PPM (80 PPM between two nodes) and guard time of +/-1ms: 12.5s. */ +#define TSCH_KEEPALIVE_TIMEOUT (12 * CLOCK_SECOND) +#endif + +/* With TSCH_ADAPTIVE_TIMESYNC enabled: keep-alive timeout used after reaching + * accurate drift compensation. */ +#ifdef TSCH_CONF_MAX_KEEPALIVE_TIMEOUT +#define TSCH_MAX_KEEPALIVE_TIMEOUT TSCH_CONF_MAX_KEEPALIVE_TIMEOUT +#else +#define TSCH_MAX_KEEPALIVE_TIMEOUT (60 * CLOCK_SECOND) +#endif + +/* Max time without synchronization before leaving the PAN */ +#ifdef TSCH_CONF_DESYNC_THRESHOLD +#define TSCH_DESYNC_THRESHOLD TSCH_CONF_DESYNC_THRESHOLD +#else +#define TSCH_DESYNC_THRESHOLD (2 * TSCH_MAX_KEEPALIVE_TIMEOUT) +#endif + +/* Period between two consecutive EBs */ +#ifdef TSCH_CONF_EB_PERIOD +#define TSCH_EB_PERIOD TSCH_CONF_EB_PERIOD +#else +#define TSCH_EB_PERIOD (16 * CLOCK_SECOND) +#endif + +/* Max Period between two consecutive EBs */ +#ifdef TSCH_CONF_MAX_EB_PERIOD +#define TSCH_MAX_EB_PERIOD TSCH_CONF_MAX_EB_PERIOD +#else +#define TSCH_MAX_EB_PERIOD (16 * CLOCK_SECOND) +#endif + +/* Use SFD timestamp for synchronization? By default we merely rely on rtimer and busy wait + * until SFD is high, which we found to provide greater accuracy on JN516x and CC2420. + * Note: for association, however, we always use SFD timestamp to know the time of arrival + * of the EB (because we do not busy-wait for the whole scanning process) + * */ +#ifdef TSCH_CONF_RESYNC_WITH_SFD_TIMESTAMPS +#define TSCH_RESYNC_WITH_SFD_TIMESTAMPS TSCH_CONF_RESYNC_WITH_SFD_TIMESTAMPS +#else +#define TSCH_RESYNC_WITH_SFD_TIMESTAMPS 0 +#endif + +/* If enabled, remove jitter due to measurement errors */ +#ifdef TSCH_CONF_TIMESYNC_REMOVE_JITTER +#define TSCH_TIMESYNC_REMOVE_JITTER TSCH_CONF_TIMESYNC_REMOVE_JITTER +#else +#define TSCH_TIMESYNC_REMOVE_JITTER TSCH_RESYNC_WITH_SFD_TIMESTAMPS +#endif + +/* Base drift value. + * Used to compensate locally know inaccuracies, such as + * the effect of having a binary 32.768 kHz timer as the TSCH time base. */ +#ifdef TSCH_CONF_BASE_DRIFT_PPM +#define TSCH_BASE_DRIFT_PPM TSCH_CONF_BASE_DRIFT_PPM +#else +#define TSCH_BASE_DRIFT_PPM 0 +#endif + +/* Estimate the drift of the time-source neighbor and compensate for it? */ +#ifdef TSCH_CONF_ADAPTIVE_TIMESYNC +#define TSCH_ADAPTIVE_TIMESYNC TSCH_CONF_ADAPTIVE_TIMESYNC +#else +#define TSCH_ADAPTIVE_TIMESYNC 1 +#endif + +/* An ad-hoc mechanism to have TSCH select its time source without the + * help of an upper-layer, simply by collecting statistics on received + * EBs and their join priority. Disabled by default as we recomment + * mapping the time source on the RPL preferred parent + * (via tsch_rpl_callback_parent_switch) */ +#ifdef TSCH_CONF_AUTOSELECT_TIME_SOURCE +#define TSCH_AUTOSELECT_TIME_SOURCE TSCH_CONF_AUTOSELECT_TIME_SOURCE +#else +#define TSCH_AUTOSELECT_TIME_SOURCE 0 +#endif /* TSCH_CONF_EB_AUTOSELECT */ + +/******** Configuration: channel hopping *******/ /* Default hopping sequence, used in case hopping sequence ID == 0 */ #ifdef TSCH_CONF_DEFAULT_HOPPING_SEQUENCE @@ -85,86 +157,195 @@ #define TSCH_HOPPING_SEQUENCE_MAX_LEN 16 #endif -/* Timeslot timing */ - -#ifndef TSCH_CONF_DEFAULT_TIMESLOT_LENGTH -#define TSCH_CONF_DEFAULT_TIMESLOT_LENGTH 10000 -#endif /* TSCH_CONF_DEFAULT_TIMESLOT_LENGTH */ - -/* Configurable Rx guard time is micro-seconds */ -#ifndef TSCH_CONF_RX_WAIT -#define TSCH_CONF_RX_WAIT 2200 -#endif /* TSCH_CONF_RX_WAIT */ - -/* The default timeslot timing in the standard is a guard time of - * 2200 us, a Tx offset of 2120 us and a Rx offset of 1120 us. - * As a result, the listening device has a guard time not centered - * on the expected Tx time. This is to be fixed in the next iteration - * of the standard. This can be enabled with: - * #define TSCH_DEFAULT_TS_TX_OFFSET 2120 - * #define TSCH_DEFAULT_TS_RX_OFFSET 1120 - * #define TSCH_DEFAULT_TS_RX_WAIT 2200 - * - * Instead, we align the Rx guard time on expected Tx time. The Rx - * guard time is user-configurable with TSCH_CONF_RX_WAIT. - - * (TS_TX_OFFSET - (TS_RX_WAIT / 2)) instead */ - -#if TSCH_CONF_DEFAULT_TIMESLOT_LENGTH == 10000 -/* Default timeslot timing as per IEEE 802.15.4e */ - -#define TSCH_DEFAULT_TS_CCA_OFFSET 1800 -#define TSCH_DEFAULT_TS_CCA 128 -#define TSCH_DEFAULT_TS_TX_OFFSET 2120 -#define TSCH_DEFAULT_TS_RX_OFFSET (TSCH_DEFAULT_TS_TX_OFFSET - (TSCH_CONF_RX_WAIT / 2)) -#define TSCH_DEFAULT_TS_RX_ACK_DELAY 800 -#define TSCH_DEFAULT_TS_TX_ACK_DELAY 1000 -#define TSCH_DEFAULT_TS_RX_WAIT TSCH_CONF_RX_WAIT -#define TSCH_DEFAULT_TS_ACK_WAIT 400 -#define TSCH_DEFAULT_TS_RX_TX 192 -#define TSCH_DEFAULT_TS_MAX_ACK 2400 -#define TSCH_DEFAULT_TS_MAX_TX 4256 -#define TSCH_DEFAULT_TS_TIMESLOT_LENGTH 10000 - -#elif TSCH_CONF_DEFAULT_TIMESLOT_LENGTH == 15000 -/* Default timeslot timing for platforms requiring 15ms slots */ - -#define TSCH_DEFAULT_TS_CCA_OFFSET 1800 -#define TSCH_DEFAULT_TS_CCA 128 -#define TSCH_DEFAULT_TS_TX_OFFSET 4000 -#define TSCH_DEFAULT_TS_RX_OFFSET (TSCH_DEFAULT_TS_TX_OFFSET - (TSCH_CONF_RX_WAIT / 2)) -#define TSCH_DEFAULT_TS_RX_ACK_DELAY 3600 -#define TSCH_DEFAULT_TS_TX_ACK_DELAY 4000 -#define TSCH_DEFAULT_TS_RX_WAIT TSCH_CONF_RX_WAIT -#define TSCH_DEFAULT_TS_ACK_WAIT 800 -#define TSCH_DEFAULT_TS_RX_TX 2072 -#define TSCH_DEFAULT_TS_MAX_ACK 2400 -#define TSCH_DEFAULT_TS_MAX_TX 4256 -#define TSCH_DEFAULT_TS_TIMESLOT_LENGTH 15000 - -#elif TSCH_CONF_DEFAULT_TIMESLOT_LENGTH == 65000U -/* 65ms timeslot, i.e. nearly the max length allowed by standard (16-bit unsigned in micro-seconds). - * Useful for running link-layer security on sky in Cooja, where only S/W security is supported. - * Note: this slot timing would require a total of 120ms. If a slot overlaps with the next active slot, - * the latter will be skipped. - * This configuration is mostly a work-around to test link-layer security in Cooja, it is recommended - * to use it with a 6TiSCH minimal schedule of length >= 2. */ - -#define TSCH_DEFAULT_TS_CCA_OFFSET 1800 -#define TSCH_DEFAULT_TS_CCA 128 -#define TSCH_DEFAULT_TS_TX_OFFSET 52000 -#define TSCH_DEFAULT_TS_RX_OFFSET (TSCH_DEFAULT_TS_TX_OFFSET - (TSCH_CONF_RX_WAIT / 2)) -#define TSCH_DEFAULT_TS_RX_ACK_DELAY 58600 -#define TSCH_DEFAULT_TS_TX_ACK_DELAY 59000 -#define TSCH_DEFAULT_TS_RX_WAIT TSCH_CONF_RX_WAIT -#define TSCH_DEFAULT_TS_ACK_WAIT 800 -#define TSCH_DEFAULT_TS_RX_TX 2072 -#define TSCH_DEFAULT_TS_MAX_ACK 2400 -#define TSCH_DEFAULT_TS_MAX_TX 4256 -#define TSCH_DEFAULT_TS_TIMESLOT_LENGTH 65000 +/******** Configuration: association *******/ +/* Start TSCH automatically after init? If not, the upper layers + * must call NETSTACK_MAC.on() to start it. Useful when the + * application needs to control when the nodes are to start + * scanning or advertising.*/ +#ifdef TSCH_CONF_AUTOSTART +#define TSCH_AUTOSTART TSCH_CONF_AUTOSTART #else -#error "TSCH: Unsupported default timeslot length" +#define TSCH_AUTOSTART 1 +#endif + +/* Max acceptable join priority */ +#ifdef TSCH_CONF_MAX_JOIN_PRIORITY +#define TSCH_MAX_JOIN_PRIORITY TSCH_CONF_MAX_JOIN_PRIORITY +#else +#define TSCH_MAX_JOIN_PRIORITY 32 +#endif + +/* Join only secured networks? (discard EBs with security disabled) */ +#ifdef TSCH_CONF_JOIN_SECURED_ONLY +#define TSCH_JOIN_SECURED_ONLY TSCH_CONF_JOIN_SECURED_ONLY +#else +/* By default, set if LLSEC802154_ENABLED is also non-zero */ +#define TSCH_JOIN_SECURED_ONLY LLSEC802154_ENABLED +#endif + +/* By default, join any PAN ID. Otherwise, wait for an EB from IEEE802154_PANID */ +#ifdef TSCH_CONF_JOIN_MY_PANID_ONLY +#define TSCH_JOIN_MY_PANID_ONLY TSCH_CONF_JOIN_MY_PANID_ONLY +#else +#define TSCH_JOIN_MY_PANID_ONLY 1 +#endif + +/* The radio polling frequency (in Hz) during association process */ +#ifdef TSCH_CONF_ASSOCIATION_POLL_FREQUENCY +#define TSCH_ASSOCIATION_POLL_FREQUENCY TSCH_CONF_ASSOCIATION_POLL_FREQUENCY +#else +#define TSCH_ASSOCIATION_POLL_FREQUENCY 100 +#endif + +/* When associating, check ASN against our own uptime (time in minutes).. + * Useful to force joining only with nodes started roughly at the same time. + * Set to the max number of minutes acceptable. */ +#ifdef TSCH_CONF_CHECK_TIME_AT_ASSOCIATION +#define TSCH_CHECK_TIME_AT_ASSOCIATION TSCH_CONF_CHECK_TIME_AT_ASSOCIATION +#else +#define TSCH_CHECK_TIME_AT_ASSOCIATION 0 +#endif + +/* By default: initialize schedule from EB when associating, using the + * slotframe and links Information Element */ +#ifdef TSCH_CONF_INIT_SCHEDULE_FROM_EB +#define TSCH_INIT_SCHEDULE_FROM_EB TSCH_CONF_INIT_SCHEDULE_FROM_EB +#else +#define TSCH_INIT_SCHEDULE_FROM_EB 1 +#endif + +/* How long to scan each channel in the scanning phase */ +#ifdef TSCH_CONF_CHANNEL_SCAN_DURATION +#define TSCH_CHANNEL_SCAN_DURATION TSCH_CONF_CHANNEL_SCAN_DURATION +#else +#define TSCH_CHANNEL_SCAN_DURATION CLOCK_SECOND +#endif + +/* TSCH EB: include timeslot timing Information Element? */ +#ifdef TSCH_PACKET_CONF_EB_WITH_TIMESLOT_TIMING +#define TSCH_PACKET_EB_WITH_TIMESLOT_TIMING TSCH_PACKET_CONF_EB_WITH_TIMESLOT_TIMING +#else +#define TSCH_PACKET_EB_WITH_TIMESLOT_TIMING 0 +#endif + +/* TSCH EB: include hopping sequence Information Element? */ +#ifdef TSCH_PACKET_CONF_EB_WITH_HOPPING_SEQUENCE +#define TSCH_PACKET_EB_WITH_HOPPING_SEQUENCE TSCH_PACKET_CONF_EB_WITH_HOPPING_SEQUENCE +#else +#define TSCH_PACKET_EB_WITH_HOPPING_SEQUENCE 0 +#endif + +/* TSCH EB: include slotframe and link Information Element? */ +#ifdef TSCH_PACKET_CONF_EB_WITH_SLOTFRAME_AND_LINK +#define TSCH_PACKET_EB_WITH_SLOTFRAME_AND_LINK TSCH_PACKET_CONF_EB_WITH_SLOTFRAME_AND_LINK +#else +#define TSCH_PACKET_EB_WITH_SLOTFRAME_AND_LINK 0 +#endif + +/******** Configuration: queues *******/ + +/* Size of the ring buffer storing dequeued outgoing packets (only an array of pointers). + * Must be power of two, and greater or equal to QUEUEBUF_NUM */ +#ifdef TSCH_CONF_DEQUEUED_ARRAY_SIZE +#define TSCH_DEQUEUED_ARRAY_SIZE TSCH_CONF_DEQUEUED_ARRAY_SIZE +#else +/* By default, round QUEUEBUF_CONF_NUM to next power of two + * (in the range [4;256]) */ +#if QUEUEBUF_CONF_NUM <= 4 +#define TSCH_DEQUEUED_ARRAY_SIZE 4 +#elif QUEUEBUF_CONF_NUM <= 8 +#define TSCH_DEQUEUED_ARRAY_SIZE 8 +#elif QUEUEBUF_CONF_NUM <= 16 +#define TSCH_DEQUEUED_ARRAY_SIZE 16 +#elif QUEUEBUF_CONF_NUM <= 32 +#define TSCH_DEQUEUED_ARRAY_SIZE 32 +#elif QUEUEBUF_CONF_NUM <= 64 +#define TSCH_DEQUEUED_ARRAY_SIZE 64 +#elif QUEUEBUF_CONF_NUM <= 128 +#define TSCH_DEQUEUED_ARRAY_SIZE 128 +#else +#define TSCH_DEQUEUED_ARRAY_SIZE 256 +#endif +#endif + +/* Size of the ring buffer storing incoming packets. + * Must be power of two */ +#ifdef TSCH_CONF_MAX_INCOMING_PACKETS +#define TSCH_MAX_INCOMING_PACKETS TSCH_CONF_MAX_INCOMING_PACKETS +#else +#define TSCH_MAX_INCOMING_PACKETS 4 +#endif + +/* The maximum number of outgoing packets towards each neighbor + * Must be power of two to enable atomic ringbuf operations. + * Note: the total number of outgoing packets in the system (for + * all neighbors) is defined via QUEUEBUF_CONF_NUM */ +#ifdef TSCH_QUEUE_CONF_NUM_PER_NEIGHBOR +#define TSCH_QUEUE_NUM_PER_NEIGHBOR TSCH_QUEUE_CONF_NUM_PER_NEIGHBOR +#else +/* By default, round QUEUEBUF_CONF_NUM to next power of two + * (in the range [4;256]) */ +#if QUEUEBUF_CONF_NUM <= 4 +#define TSCH_QUEUE_NUM_PER_NEIGHBOR 4 +#elif QUEUEBUF_CONF_NUM <= 8 +#define TSCH_QUEUE_NUM_PER_NEIGHBOR 8 +#elif QUEUEBUF_CONF_NUM <= 16 +#define TSCH_QUEUE_NUM_PER_NEIGHBOR 16 +#elif QUEUEBUF_CONF_NUM <= 32 +#define TSCH_QUEUE_NUM_PER_NEIGHBOR 32 +#elif QUEUEBUF_CONF_NUM <= 64 +#define TSCH_QUEUE_NUM_PER_NEIGHBOR 64 +#elif QUEUEBUF_CONF_NUM <= 128 +#define TSCH_QUEUE_NUM_PER_NEIGHBOR 128 +#else +#define TSCH_QUEUE_NUM_PER_NEIGHBOR 256 +#endif +#endif + +/* The number of neighbor queues. There are two queues allocated at all times: + * one for EBs, one for broadcasts. Other queues are for unicast to neighbors */ +#ifdef TSCH_QUEUE_CONF_MAX_NEIGHBOR_QUEUES +#define TSCH_QUEUE_MAX_NEIGHBOR_QUEUES TSCH_QUEUE_CONF_MAX_NEIGHBOR_QUEUES +#else +#define TSCH_QUEUE_MAX_NEIGHBOR_QUEUES ((NBR_TABLE_CONF_MAX_NEIGHBORS) + 2) +#endif + +/******** Configuration: scheduling *******/ + +/* Initializes TSCH with a 6TiSCH minimal schedule */ +#ifdef TSCH_SCHEDULE_CONF_WITH_6TISCH_MINIMAL +#define TSCH_SCHEDULE_WITH_6TISCH_MINIMAL TSCH_SCHEDULE_CONF_WITH_6TISCH_MINIMAL +#else +#define TSCH_SCHEDULE_WITH_6TISCH_MINIMAL (!(BUILD_WITH_ORCHESTRA)) +#endif + +/* 6TiSCH Minimal schedule slotframe length */ +#ifdef TSCH_SCHEDULE_CONF_DEFAULT_LENGTH +#define TSCH_SCHEDULE_DEFAULT_LENGTH TSCH_SCHEDULE_CONF_DEFAULT_LENGTH +#else +#define TSCH_SCHEDULE_DEFAULT_LENGTH 7 +#endif + +/* Max number of TSCH slotframes */ +#ifdef TSCH_SCHEDULE_CONF_MAX_SLOTFRAMES +#define TSCH_SCHEDULE_MAX_SLOTFRAMES TSCH_SCHEDULE_CONF_MAX_SLOTFRAMES +#else +#define TSCH_SCHEDULE_MAX_SLOTFRAMES 4 +#endif + +/* Max number of links */ +#ifdef TSCH_SCHEDULE_CONF_MAX_LINKS +#define TSCH_SCHEDULE_MAX_LINKS TSCH_SCHEDULE_CONF_MAX_LINKS +#else +#define TSCH_SCHEDULE_MAX_LINKS 32 +#endif + +/* To include Sixtop Implementation */ +#ifdef TSCH_CONF_WITH_SIXTOP +#define TSCH_WITH_SIXTOP TSCH_CONF_WITH_SIXTOP +#else +#define TSCH_WITH_SIXTOP 0 #endif /* A custom feature allowing upper layers to assign packets to @@ -175,12 +356,44 @@ #define TSCH_WITH_LINK_SELECTOR (BUILD_WITH_ORCHESTRA) #endif /* TSCH_CONF_WITH_LINK_SELECTOR */ -/* Estimate the drift of the time-source neighbor and compensate for it? */ -#ifdef TSCH_CONF_ADAPTIVE_TIMESYNC -#define TSCH_ADAPTIVE_TIMESYNC TSCH_CONF_ADAPTIVE_TIMESYNC +/******** Configuration: CSMA *******/ + +/* TSCH CSMA-CA parameters, see IEEE 802.15.4e-2012 */ +/* Min backoff exponent */ +#ifdef TSCH_CONF_MAC_MIN_BE +#define TSCH_MAC_MIN_BE TSCH_CONF_MAC_MIN_BE #else -#define TSCH_ADAPTIVE_TIMESYNC 1 +#define TSCH_MAC_MIN_BE 1 #endif +/* Max backoff exponent */ +#ifdef TSCH_CONF_MAC_MAX_BE +#define TSCH_MAC_MAX_BE TSCH_CONF_MAC_MAX_BE +#else +#define TSCH_MAC_MAX_BE 5 +#endif +/* Max number of re-transmissions */ +#ifdef TSCH_CONF_MAC_MAX_FRAME_RETRIES +#define TSCH_MAC_MAX_FRAME_RETRIES TSCH_CONF_MAC_MAX_FRAME_RETRIES +#else +#define TSCH_MAC_MAX_FRAME_RETRIES 7 +#endif + +/* Include source address in ACK? */ +#ifdef TSCH_PACKET_CONF_EACK_WITH_SRC_ADDR +#define TSCH_PACKET_EACK_WITH_SRC_ADDR TSCH_PACKET_CONF_EACK_WITH_SRC_ADDR +#else +#define TSCH_PACKET_EACK_WITH_SRC_ADDR 0 +#endif + +/* Include destination address in ACK? */ +#ifdef TSCH_PACKET_CONF_EACK_WITH_DEST_ADDR +#define TSCH_PACKET_EACK_WITH_DEST_ADDR TSCH_PACKET_CONF_EACK_WITH_DEST_ADDR +#else +#define TSCH_PACKET_EACK_WITH_DEST_ADDR 1 /* Include destination address +by default, useful in case of duplicate seqno */ +#endif + +/******** Configuration: hardware-specific settings *******/ /* HW frame filtering enabled */ #ifdef TSCH_CONF_HW_FRAME_FILTERING @@ -196,12 +409,17 @@ #define TSCH_RADIO_ON_DURING_TIMESLOT 0 #endif -/* How long to scan each channel in the scanning phase */ -#ifdef TSCH_CONF_CHANNEL_SCAN_DURATION -#define TSCH_CHANNEL_SCAN_DURATION TSCH_CONF_CHANNEL_SCAN_DURATION -#else -#define TSCH_CHANNEL_SCAN_DURATION CLOCK_SECOND -#endif + + +/* Timeslot timing */ +#ifndef TSCH_CONF_DEFAULT_TIMESLOT_LENGTH +#define TSCH_CONF_DEFAULT_TIMESLOT_LENGTH 10000 +#endif /* TSCH_CONF_DEFAULT_TIMESLOT_LENGTH */ + +/* Configurable Rx guard time is micro-seconds */ +#ifndef TSCH_CONF_RX_WAIT +#define TSCH_CONF_RX_WAIT 2200 +#endif /* TSCH_CONF_RX_WAIT */ #endif /* __TSCH_CONF_H__ */ /** @} */ diff --git a/os/net/mac/tsch/tsch-const.h b/os/net/mac/tsch/tsch-const.h new file mode 100644 index 000000000..607bb3506 --- /dev/null +++ b/os/net/mac/tsch/tsch-const.h @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2015, SICS Swedish ICT. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * TSCH configuration + * \author + * Simon Duquennoy + */ + +/** + * \addtogroup tsch + * @{ +*/ + +#ifndef __TSCH_CONST_H__ +#define __TSCH_CONST_H__ + +/********** Includes **********/ +#include "net/packetbuf.h" +#include "net/mac/tsch/tsch-conf.h" /* Required for timestlot timing consts */ + +/********** Constants **********/ + +/* Link options */ +#define LINK_OPTION_TX 1 +#define LINK_OPTION_RX 2 +#define LINK_OPTION_SHARED 4 +#define LINK_OPTION_TIME_KEEPING 8 + +/* Default IEEE 802.15.4e hopping sequences, obtained from https://gist.github.com/twatteyne/2e22ee3c1a802b685695 */ +/* 16 channels, sequence length 16 */ +#define TSCH_HOPPING_SEQUENCE_16_16 (uint8_t[]){ 16, 17, 23, 18, 26, 15, 25, 22, 19, 11, 12, 13, 24, 14, 20, 21 } +/* 4 channels, sequence length 16 */ +#define TSCH_HOPPING_SEQUENCE_4_16 (uint8_t[]){ 20, 26, 25, 26, 15, 15, 25, 20, 26, 15, 26, 25, 20, 15, 20, 25 } +/* 4 channels, sequence length 4 */ +#define TSCH_HOPPING_SEQUENCE_4_4 (uint8_t[]){ 15, 25, 26, 20 } +/* 2 channels, sequence length 2 */ +#define TSCH_HOPPING_SEQUENCE_2_2 (uint8_t[]){ 20, 25 } +/* 1 channel, sequence length 1 */ +#define TSCH_HOPPING_SEQUENCE_1_1 (uint8_t[]){ 20 } + +/* Max TSCH packet lenght */ +#define TSCH_PACKET_MAX_LEN MIN(127, PACKETBUF_SIZE) + +/* The jitter to remove in ticks. + * This should be the sum of measurement errors on Tx and Rx nodes. + * */ +#define TSCH_TIMESYNC_MEASUREMENT_ERROR US_TO_RTIMERTICKS(32) + +/* The approximate number of slots per second */ +#define TSCH_SLOTS_PER_SECOND (1000000 / TSCH_DEFAULT_TS_TIMESLOT_LENGTH) + +/* Calculate packet tx/rx duration in rtimer ticks based on sent + * packet len in bytes with 802.15.4 250kbps data rate. + * One byte = 32us. Add two bytes for CRC and one for len field */ +#define TSCH_PACKET_DURATION(len) US_TO_RTIMERTICKS(32 * ((len) + 3)) + +/* Convert rtimer ticks to clock and vice versa */ +#define TSCH_CLOCK_TO_TICKS(c) (((c) * RTIMER_SECOND) / CLOCK_SECOND) +#define TSCH_CLOCK_TO_SLOTS(c, timeslot_length) (TSCH_CLOCK_TO_TICKS(c) / timeslot_length) + +/* The default timeslot timing in the standard is a guard time of + * 2200 us, a Tx offset of 2120 us and a Rx offset of 1120 us. + * As a result, the listening device has a guard time not centered + * on the expected Tx time. This is to be fixed in the next iteration + * of the standard. This can be enabled with: + * #define TSCH_DEFAULT_TS_TX_OFFSET 2120 + * #define TSCH_DEFAULT_TS_RX_OFFSET 1120 + * #define TSCH_DEFAULT_TS_RX_WAIT 2200 + * + * Instead, we align the Rx guard time on expected Tx time. The Rx + * guard time is user-configurable with TSCH_CONF_RX_WAIT. + + * (TS_TX_OFFSET - (TS_RX_WAIT / 2)) instead */ + +#if TSCH_CONF_DEFAULT_TIMESLOT_LENGTH == 10000 +/* Default timeslot timing as per IEEE 802.15.4e */ + +#define TSCH_DEFAULT_TS_CCA_OFFSET 1800 +#define TSCH_DEFAULT_TS_CCA 128 +#define TSCH_DEFAULT_TS_TX_OFFSET 2120 +#define TSCH_DEFAULT_TS_RX_OFFSET (TSCH_DEFAULT_TS_TX_OFFSET - (TSCH_CONF_RX_WAIT / 2)) +#define TSCH_DEFAULT_TS_RX_ACK_DELAY 800 +#define TSCH_DEFAULT_TS_TX_ACK_DELAY 1000 +#define TSCH_DEFAULT_TS_RX_WAIT TSCH_CONF_RX_WAIT +#define TSCH_DEFAULT_TS_ACK_WAIT 400 +#define TSCH_DEFAULT_TS_RX_TX 192 +#define TSCH_DEFAULT_TS_MAX_ACK 2400 +#define TSCH_DEFAULT_TS_MAX_TX 4256 +#define TSCH_DEFAULT_TS_TIMESLOT_LENGTH 10000 + +#elif TSCH_CONF_DEFAULT_TIMESLOT_LENGTH == 15000 +/* Default timeslot timing for platforms requiring 15ms slots */ + +#define TSCH_DEFAULT_TS_CCA_OFFSET 1800 +#define TSCH_DEFAULT_TS_CCA 128 +#define TSCH_DEFAULT_TS_TX_OFFSET 4000 +#define TSCH_DEFAULT_TS_RX_OFFSET (TSCH_DEFAULT_TS_TX_OFFSET - (TSCH_CONF_RX_WAIT / 2)) +#define TSCH_DEFAULT_TS_RX_ACK_DELAY 3600 +#define TSCH_DEFAULT_TS_TX_ACK_DELAY 4000 +#define TSCH_DEFAULT_TS_RX_WAIT TSCH_CONF_RX_WAIT +#define TSCH_DEFAULT_TS_ACK_WAIT 800 +#define TSCH_DEFAULT_TS_RX_TX 2072 +#define TSCH_DEFAULT_TS_MAX_ACK 2400 +#define TSCH_DEFAULT_TS_MAX_TX 4256 +#define TSCH_DEFAULT_TS_TIMESLOT_LENGTH 15000 + +#elif TSCH_CONF_DEFAULT_TIMESLOT_LENGTH == 65000U +/* 65ms timeslot, i.e. nearly the max length allowed by standard (16-bit unsigned in micro-seconds). + * Useful for running link-layer security on sky in Cooja, where only S/W security is supported. + * Note: this slot timing would require a total of 120ms. If a slot overlaps with the next active slot, + * the latter will be skipped. + * This configuration is mostly a work-around to test link-layer security in Cooja, it is recommended + * to use it with a 6TiSCH minimal schedule of length >= 2. */ + +#define TSCH_DEFAULT_TS_CCA_OFFSET 1800 +#define TSCH_DEFAULT_TS_CCA 128 +#define TSCH_DEFAULT_TS_TX_OFFSET 52000 +#define TSCH_DEFAULT_TS_RX_OFFSET (TSCH_DEFAULT_TS_TX_OFFSET - (TSCH_CONF_RX_WAIT / 2)) +#define TSCH_DEFAULT_TS_RX_ACK_DELAY 58600 +#define TSCH_DEFAULT_TS_TX_ACK_DELAY 59000 +#define TSCH_DEFAULT_TS_RX_WAIT TSCH_CONF_RX_WAIT +#define TSCH_DEFAULT_TS_ACK_WAIT 800 +#define TSCH_DEFAULT_TS_RX_TX 2072 +#define TSCH_DEFAULT_TS_MAX_ACK 2400 +#define TSCH_DEFAULT_TS_MAX_TX 4256 +#define TSCH_DEFAULT_TS_TIMESLOT_LENGTH 65000 + +#else +#error "TSCH: Unsupported default timeslot length" +#endif + +#endif /* __TSCH_CONST_H__ */ +/** @} */ diff --git a/os/net/mac/tsch/tsch-log.c b/os/net/mac/tsch/tsch-log.c index fdaaf3b53..d27dac064 100644 --- a/os/net/mac/tsch/tsch-log.c +++ b/os/net/mac/tsch/tsch-log.c @@ -48,12 +48,6 @@ #include "contiki.h" #include #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-queue.h" -#include "net/mac/tsch/tsch-private.h" -#include "net/mac/tsch/tsch-log.h" -#include "net/mac/tsch/tsch-packet.h" -#include "net/mac/tsch/tsch-schedule.h" -#include "net/mac/tsch/tsch-slot-operation.h" #include "lib/ringbufindex.h" #include "sys/log.h" diff --git a/os/net/mac/tsch/tsch-log.h b/os/net/mac/tsch/tsch-log.h index 06888e24f..198304b43 100644 --- a/os/net/mac/tsch/tsch-log.h +++ b/os/net/mac/tsch/tsch-log.h @@ -42,7 +42,6 @@ #include "contiki.h" #include "sys/rtimer.h" -#include "net/mac/tsch/tsch-private.h" /******** Configuration *******/ diff --git a/os/net/mac/tsch/tsch-packet.c b/os/net/mac/tsch/tsch-packet.c index 2db212dac..e0e253a3f 100644 --- a/os/net/mac/tsch/tsch-packet.c +++ b/os/net/mac/tsch/tsch-packet.c @@ -46,10 +46,6 @@ #include "contiki.h" #include "net/packetbuf.h" #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-packet.h" -#include "net/mac/tsch/tsch-private.h" -#include "net/mac/tsch/tsch-schedule.h" -#include "net/mac/tsch/tsch-security.h" #include "net/mac/framer/frame802154.h" #include "net/mac/framer/framer-802154.h" #include "net/netstack.h" diff --git a/os/net/mac/tsch/tsch-packet.h b/os/net/mac/tsch/tsch-packet.h index 717e0fdda..4dfbcbe72 100644 --- a/os/net/mac/tsch/tsch-packet.h +++ b/os/net/mac/tsch/tsch-packet.h @@ -41,54 +41,9 @@ /********** Includes **********/ #include "contiki.h" -#include "net/packetbuf.h" -#include "net/mac/tsch/tsch-private.h" #include "net/mac/framer/frame802154.h" #include "net/mac/framer/frame802154e-ie.h" -/******** Configuration *******/ - -/* TSCH EB: include timeslot timing Information Element? */ -#ifdef TSCH_PACKET_CONF_EB_WITH_TIMESLOT_TIMING -#define TSCH_PACKET_EB_WITH_TIMESLOT_TIMING TSCH_PACKET_CONF_EB_WITH_TIMESLOT_TIMING -#else -#define TSCH_PACKET_EB_WITH_TIMESLOT_TIMING 0 -#endif - -/* TSCH EB: include hopping sequence Information Element? */ -#ifdef TSCH_PACKET_CONF_EB_WITH_HOPPING_SEQUENCE -#define TSCH_PACKET_EB_WITH_HOPPING_SEQUENCE TSCH_PACKET_CONF_EB_WITH_HOPPING_SEQUENCE -#else -#define TSCH_PACKET_EB_WITH_HOPPING_SEQUENCE 0 -#endif - -/* TSCH EB: include slotframe and link Information Element? */ -#ifdef TSCH_PACKET_CONF_EB_WITH_SLOTFRAME_AND_LINK -#define TSCH_PACKET_EB_WITH_SLOTFRAME_AND_LINK TSCH_PACKET_CONF_EB_WITH_SLOTFRAME_AND_LINK -#else -#define TSCH_PACKET_EB_WITH_SLOTFRAME_AND_LINK 0 -#endif - -/* Include source address in ACK? */ -#ifdef TSCH_PACKET_CONF_EACK_WITH_SRC_ADDR -#define TSCH_PACKET_EACK_WITH_SRC_ADDR TSCH_PACKET_CONF_EACK_WITH_SRC_ADDR -#else -#define TSCH_PACKET_EACK_WITH_SRC_ADDR 0 -#endif - -/* Include destination address in ACK? */ -#ifdef TSCH_PACKET_CONF_EACK_WITH_DEST_ADDR -#define TSCH_PACKET_EACK_WITH_DEST_ADDR TSCH_PACKET_CONF_EACK_WITH_DEST_ADDR -#else -#define TSCH_PACKET_EACK_WITH_DEST_ADDR 1 /* Include destination address -by default, useful in case of duplicate seqno */ -#endif - -/********** Constants *********/ - -/* Max TSCH packet lenght */ -#define TSCH_PACKET_MAX_LEN MIN(127,PACKETBUF_SIZE) - /********** Functions *********/ /* Construct enhanced ACK packet and return ACK length */ diff --git a/os/net/mac/tsch/tsch-private.h b/os/net/mac/tsch/tsch-private.h deleted file mode 100644 index fc5559b38..000000000 --- a/os/net/mac/tsch/tsch-private.h +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright (c) 2014, SICS Swedish ICT. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * This file is part of the Contiki operating system. - * - */ - -/** - * \file - * Private TSCH definitions - * (meant for use by TSCH implementation files only) - * \author - * Simon Duquennoy - * Beshr Al Nahas - */ - -/** - * \addtogroup tsch - * @{ -*/ - -#ifndef __TSCH_PRIVATE_H__ -#define __TSCH_PRIVATE_H__ - -/********** Includes **********/ - -#include "contiki.h" -#include "net/linkaddr.h" -#include "net/mac/tsch/tsch-asn.h" -#include "net/mac/tsch/tsch-conf.h" -#if CONTIKI_TARGET_COOJA -#include "lib/simEnvChange.h" -#include "sys/cooja_mt.h" -#endif /* CONTIKI_TARGET_COOJA */ - -/************ Types ***********/ - -/* TSCH timeslot timing elements. Used to index timeslot timing - * of different units, such as rtimer tick or micro-second */ -enum tsch_timeslot_timing_elements { - tsch_ts_cca_offset, - tsch_ts_cca, - tsch_ts_tx_offset, - tsch_ts_rx_offset, - tsch_ts_rx_ack_delay, - tsch_ts_tx_ack_delay, - tsch_ts_rx_wait, - tsch_ts_ack_wait, - tsch_ts_rx_tx, - tsch_ts_max_ack, - tsch_ts_max_tx, - tsch_ts_timeslot_length, - tsch_ts_elements_count, /* Not a timing element */ -}; - -/***** External Variables *****/ - -/* 802.15.4 broadcast MAC address */ -extern const linkaddr_t tsch_broadcast_address; -/* The address we use to identify EB queue */ -extern const linkaddr_t tsch_eb_address; -/* The current Absolute Slot Number (ASN) */ -extern struct tsch_asn_t tsch_current_asn; -extern uint8_t tsch_join_priority; -extern struct tsch_link *current_link; -/* TSCH channel hopping sequence */ -extern uint8_t tsch_hopping_sequence[TSCH_HOPPING_SEQUENCE_MAX_LEN]; -extern struct tsch_asn_divisor_t tsch_hopping_sequence_length; -/* TSCH timeslot timing (in rtimer ticks) */ -extern rtimer_clock_t tsch_timing[tsch_ts_elements_count]; -/* Statistics on the current session */ -extern unsigned long tx_count; -extern unsigned long rx_count; -extern unsigned long sync_count; - -/* TSCH processes */ -PROCESS_NAME(tsch_process); -PROCESS_NAME(tsch_send_eb_process); -PROCESS_NAME(tsch_pending_events_process); - -/********** Functions *********/ - -/* Set TSCH to send a keepalive message after TSCH_KEEPALIVE_TIMEOUT */ -void tsch_schedule_keepalive(void); -/* Set TSCH to send a keepalive message immediately */ -void tsch_schedule_keepalive_immediately(void); -/* Leave the TSCH network */ -void tsch_disassociate(void); - -/************ Macros **********/ - -/* Calculate packet tx/rx duration in rtimer ticks based on sent - * packet len in bytes with 802.15.4 250kbps data rate. - * One byte = 32us. Add two bytes for CRC and one for len field */ -#define TSCH_PACKET_DURATION(len) US_TO_RTIMERTICKS(32 * ((len) + 3)) - -/* Convert rtimer ticks to clock and vice versa */ -#define TSCH_CLOCK_TO_TICKS(c) (((c) * RTIMER_SECOND) / CLOCK_SECOND) -#define TSCH_CLOCK_TO_SLOTS(c, timeslot_length) (TSCH_CLOCK_TO_TICKS(c) / timeslot_length) - -/* Wait for a condition with timeout t0+offset. */ -#if CONTIKI_TARGET_COOJA -#define BUSYWAIT_UNTIL_ABS(cond, t0, offset) \ - while(!(cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), (t0) + (offset))) { \ - simProcessRunValue = 1; \ - cooja_mt_yield(); \ - }; -#else -#define BUSYWAIT_UNTIL_ABS(cond, t0, offset) \ - while(!(cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), (t0) + (offset))) ; -#endif /* CONTIKI_TARGET_COOJA */ -#endif /* __TSCH_PRIVATE_H__ */ -/** @} */ diff --git a/os/net/mac/tsch/tsch-queue.c b/os/net/mac/tsch/tsch-queue.c index 7aee0d66a..4ef5130fb 100644 --- a/os/net/mac/tsch/tsch-queue.c +++ b/os/net/mac/tsch/tsch-queue.c @@ -53,11 +53,6 @@ #include "lib/random.h" #include "net/queuebuf.h" #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-private.h" -#include "net/mac/tsch/tsch-queue.h" -#include "net/mac/tsch/tsch-schedule.h" -#include "net/mac/tsch/tsch-slot-operation.h" -#include "net/mac/tsch/tsch-log.h" #include /* Log configuration */ diff --git a/os/net/mac/tsch/tsch-queue.h b/os/net/mac/tsch/tsch-queue.h index 0d77a6105..8c7dceb37 100644 --- a/os/net/mac/tsch/tsch-queue.h +++ b/os/net/mac/tsch/tsch-queue.h @@ -43,123 +43,8 @@ #include "contiki.h" #include "lib/ringbufindex.h" #include "net/linkaddr.h" -#include "net/mac/tsch/tsch-schedule.h" #include "net/mac/mac.h" -/******** Configuration *******/ - -/* The maximum number of outgoing packets towards each neighbor - * Must be power of two to enable atomic ringbuf operations. - * Note: the total number of outgoing packets in the system (for - * all neighbors) is defined via QUEUEBUF_CONF_NUM */ -#ifdef TSCH_QUEUE_CONF_NUM_PER_NEIGHBOR -#define TSCH_QUEUE_NUM_PER_NEIGHBOR TSCH_QUEUE_CONF_NUM_PER_NEIGHBOR -#else -/* By default, round QUEUEBUF_CONF_NUM to next power of two - * (in the range [4;256]) */ -#if QUEUEBUF_CONF_NUM <= 4 -#define TSCH_QUEUE_NUM_PER_NEIGHBOR 4 -#elif QUEUEBUF_CONF_NUM <= 8 -#define TSCH_QUEUE_NUM_PER_NEIGHBOR 8 -#elif QUEUEBUF_CONF_NUM <= 16 -#define TSCH_QUEUE_NUM_PER_NEIGHBOR 16 -#elif QUEUEBUF_CONF_NUM <= 32 -#define TSCH_QUEUE_NUM_PER_NEIGHBOR 32 -#elif QUEUEBUF_CONF_NUM <= 64 -#define TSCH_QUEUE_NUM_PER_NEIGHBOR 64 -#elif QUEUEBUF_CONF_NUM <= 128 -#define TSCH_QUEUE_NUM_PER_NEIGHBOR 128 -#else -#define TSCH_QUEUE_NUM_PER_NEIGHBOR 256 -#endif -#endif - -/* The number of neighbor queues. There are two queues allocated at all times: - * one for EBs, one for broadcasts. Other queues are for unicast to neighbors */ -#ifdef TSCH_QUEUE_CONF_MAX_NEIGHBOR_QUEUES -#define TSCH_QUEUE_MAX_NEIGHBOR_QUEUES TSCH_QUEUE_CONF_MAX_NEIGHBOR_QUEUES -#else -#define TSCH_QUEUE_MAX_NEIGHBOR_QUEUES ((NBR_TABLE_CONF_MAX_NEIGHBORS) + 2) -#endif - -/* TSCH CSMA-CA parameters, see IEEE 802.15.4e-2012 */ -/* Min backoff exponent */ -#ifdef TSCH_CONF_MAC_MIN_BE -#define TSCH_MAC_MIN_BE TSCH_CONF_MAC_MIN_BE -#else -#define TSCH_MAC_MIN_BE 1 -#endif -/* Max backoff exponent */ -#ifdef TSCH_CONF_MAC_MAX_BE -#define TSCH_MAC_MAX_BE TSCH_CONF_MAC_MAX_BE -#else -#define TSCH_MAC_MAX_BE 5 -#endif -/* Max number of re-transmissions */ -#ifdef TSCH_CONF_MAC_MAX_FRAME_RETRIES -#define TSCH_MAC_MAX_FRAME_RETRIES TSCH_CONF_MAC_MAX_FRAME_RETRIES -#else -#define TSCH_MAC_MAX_FRAME_RETRIES 7 -#endif - -/*********** Callbacks *********/ - -#if BUILD_WITH_ORCHESTRA - -#ifndef TSCH_CALLBACK_NEW_TIME_SOURCE -#define TSCH_CALLBACK_NEW_TIME_SOURCE orchestra_callback_new_time_source -#endif /* TSCH_CALLBACK_NEW_TIME_SOURCE */ - -#ifndef TSCH_CALLBACK_PACKET_READY -#define TSCH_CALLBACK_PACKET_READY orchestra_callback_packet_ready -#endif /* TSCH_CALLBACK_PACKET_READY */ - -#endif /* BUILD_WITH_ORCHESTRA */ - -/* Called by TSCH when switching time source */ -#ifdef TSCH_CALLBACK_NEW_TIME_SOURCE -struct tsch_neighbor; -void TSCH_CALLBACK_NEW_TIME_SOURCE(const struct tsch_neighbor *old, const struct tsch_neighbor *new); -#endif - -/* Called by TSCH every time a packet is ready to be added to the send queue */ -#ifdef TSCH_CALLBACK_PACKET_READY -void TSCH_CALLBACK_PACKET_READY(void); -#endif - -/************ Types ***********/ - -/* TSCH packet information */ -struct tsch_packet { - struct queuebuf *qb; /* pointer to the queuebuf to be sent */ - mac_callback_t sent; /* callback for this packet */ - void *ptr; /* MAC callback parameter */ - uint8_t transmissions; /* #transmissions performed for this packet */ - uint8_t max_transmissions; /* maximal number of Tx before dropping the packet */ - uint8_t ret; /* status -- MAC return code */ - uint8_t header_len; /* length of header and header IEs (needed for link-layer security) */ - uint8_t tsch_sync_ie_offset; /* Offset within the frame used for quick update of EB ASN and join priority */ -}; - -/* TSCH neighbor information */ -struct tsch_neighbor { - /* Neighbors are stored as a list: "next" must be the first field */ - struct tsch_neighbor *next; - linkaddr_t addr; /* MAC address of the neighbor */ - uint8_t is_broadcast; /* is this neighbor a virtual neighbor used for broadcast (of data packets or EBs) */ - uint8_t is_time_source; /* is this neighbor a time source? */ - uint8_t backoff_exponent; /* CSMA backoff exponent */ - uint8_t backoff_window; /* CSMA backoff window (number of slots to skip) */ - uint8_t last_backoff_window; /* Last CSMA backoff window */ - uint8_t tx_links_count; /* How many links do we have to this neighbor? */ - uint8_t dedicated_tx_links_count; /* How many dedicated links do we have to this neighbor? */ - /* Array for the ringbuf. Contains pointers to packets. - * Its size must be a power of two to allow for atomic put */ - struct tsch_packet *tx_array[TSCH_QUEUE_NUM_PER_NEIGHBOR]; - /* Circular buffer of pointers to packet. */ - struct ringbufindex tx_ringbuf; -}; - /***** External Variables *****/ /* Broadcast and EB virtual neighbors */ diff --git a/os/net/mac/tsch/tsch-rpl.c b/os/net/mac/tsch/tsch-rpl.c index fedb4ce58..951648d91 100644 --- a/os/net/mac/tsch/tsch-rpl.c +++ b/os/net/mac/tsch/tsch-rpl.c @@ -46,10 +46,6 @@ #include "net/routing/routing.h" #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-private.h" -#include "net/mac/tsch/tsch-schedule.h" -#include "net/mac/tsch/tsch-log.h" -#include "net/mac/tsch/tsch-rpl.h" #include "net/link-stats.h" #if ROUTING_CONF_RPL_LITE diff --git a/os/net/mac/tsch/tsch-rpl.h b/os/net/mac/tsch/tsch-rpl.h index 41fa17e91..64e31166c 100644 --- a/os/net/mac/tsch/tsch-rpl.h +++ b/os/net/mac/tsch/tsch-rpl.h @@ -39,7 +39,6 @@ /********** Includes **********/ #include "net/routing/routing.h" -#include "net/mac/tsch/tsch-queue.h" #if ROUTING_CONF_RPL_LITE #include "net/routing/rpl-lite/rpl.h" #elif ROUTING_CONF_RPL_CLASSIC diff --git a/os/net/mac/tsch/tsch-schedule.c b/os/net/mac/tsch/tsch-schedule.c index 193827f44..70a6b92ae 100644 --- a/os/net/mac/tsch/tsch-schedule.c +++ b/os/net/mac/tsch/tsch-schedule.c @@ -50,11 +50,6 @@ #include "net/packetbuf.h" #include "net/queuebuf.h" #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-queue.h" -#include "net/mac/tsch/tsch-private.h" -#include "net/mac/tsch/tsch-packet.h" -#include "net/mac/tsch/tsch-schedule.h" -#include "net/mac/tsch/tsch-log.h" #include "net/mac/framer/frame802154.h" #include "sys/process.h" #include "sys/rtimer.h" diff --git a/os/net/mac/tsch/tsch-schedule.h b/os/net/mac/tsch/tsch-schedule.h index 12699b0e3..2a2cf5e25 100644 --- a/os/net/mac/tsch/tsch-schedule.h +++ b/os/net/mac/tsch/tsch-schedule.h @@ -41,94 +41,8 @@ /********** Includes **********/ #include "contiki.h" -#include "lib/list.h" -#include "net/mac/tsch/tsch-private.h" -#include "net/mac/tsch/tsch-queue.h" -#include "net/mac/tsch/tsch-slot-operation.h" #include "net/linkaddr.h" -/******** Configuration *******/ - -/* Initializes TSCH with a 6TiSCH minimal schedule */ -#ifdef TSCH_SCHEDULE_CONF_WITH_6TISCH_MINIMAL -#define TSCH_SCHEDULE_WITH_6TISCH_MINIMAL TSCH_SCHEDULE_CONF_WITH_6TISCH_MINIMAL -#else -#define TSCH_SCHEDULE_WITH_6TISCH_MINIMAL (!(BUILD_WITH_ORCHESTRA)) -#endif - -/* 6TiSCH Minimal schedule slotframe length */ -#ifdef TSCH_SCHEDULE_CONF_DEFAULT_LENGTH -#define TSCH_SCHEDULE_DEFAULT_LENGTH TSCH_SCHEDULE_CONF_DEFAULT_LENGTH -#else -#define TSCH_SCHEDULE_DEFAULT_LENGTH 7 -#endif - -/* Max number of TSCH slotframes */ -#ifdef TSCH_SCHEDULE_CONF_MAX_SLOTFRAMES -#define TSCH_SCHEDULE_MAX_SLOTFRAMES TSCH_SCHEDULE_CONF_MAX_SLOTFRAMES -#else -#define TSCH_SCHEDULE_MAX_SLOTFRAMES 4 -#endif - -/* Max number of links */ -#ifdef TSCH_SCHEDULE_CONF_MAX_LINKS -#define TSCH_SCHEDULE_MAX_LINKS TSCH_SCHEDULE_CONF_MAX_LINKS -#else -#define TSCH_SCHEDULE_MAX_LINKS 32 -#endif - -/********** Constants *********/ - -/* Link options */ -#define LINK_OPTION_TX 1 -#define LINK_OPTION_RX 2 -#define LINK_OPTION_SHARED 4 -#define LINK_OPTION_TIME_KEEPING 8 - -/************ Types ***********/ - -/* 802.15.4e link types. - * LINK_TYPE_ADVERTISING_ONLY is an extra one: for EB-only links. */ -enum link_type { LINK_TYPE_NORMAL, LINK_TYPE_ADVERTISING, LINK_TYPE_ADVERTISING_ONLY }; - -struct tsch_link { - /* Links are stored as a list: "next" must be the first field */ - struct tsch_link *next; - /* Unique identifier */ - uint16_t handle; - /* MAC address of neighbor */ - linkaddr_t addr; - /* Slotframe identifier */ - uint16_t slotframe_handle; - /* Identifier of Slotframe to which this link belongs - * Unused. */ - /* uint8_t handle; */ - /* Timeslot for this link */ - uint16_t timeslot; - /* Channel offset for this link */ - uint16_t channel_offset; - /* A bit string that defines - * b0 = Transmit, b1 = Receive, b2 = Shared, b3 = Timekeeping, b4 = reserved */ - uint8_t link_options; - /* Type of link. NORMAL = 0. ADVERTISING = 1, and indicates - the link may be used to send an Enhanced beacon. */ - enum link_type link_type; - /* Any other data for upper layers */ - void *data; -}; - -struct tsch_slotframe { - /* Slotframes are stored as a list: "next" must be the first field */ - struct tsch_slotframe *next; - /* Unique identifier */ - uint16_t handle; - /* Number of timeslots in the slotframe. - * Stored as struct asn_divisor_t because we often need ASN%size */ - struct tsch_asn_divisor_t size; - /* List of links belonging to this slotframe */ - LIST_STRUCT(links_list); -}; - /********** Functions *********/ /* Module initialization, call only once at startup. Returns 1 is success, 0 if failure. */ diff --git a/os/net/mac/tsch/tsch-security.c b/os/net/mac/tsch/tsch-security.c index d53dd9307..1c4266626 100644 --- a/os/net/mac/tsch/tsch-security.c +++ b/os/net/mac/tsch/tsch-security.c @@ -44,11 +44,6 @@ #include "contiki.h" #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-packet.h" -#include "net/mac/tsch/tsch-private.h" -#include "net/mac/tsch/tsch-schedule.h" -#include "net/mac/tsch/tsch-security.h" -#include "net/mac/tsch/tsch-log.h" #include "net/mac/framer/frame802154.h" #include "net/mac/framer/framer-802154.h" #include "net/netstack.h" diff --git a/os/net/mac/tsch/tsch-security.h b/os/net/mac/tsch/tsch-security.h index 4ca4be07e..683bc6316 100644 --- a/os/net/mac/tsch/tsch-security.h +++ b/os/net/mac/tsch/tsch-security.h @@ -41,18 +41,16 @@ /********** Includes **********/ #include "contiki.h" -#include "net/mac/tsch/tsch-asn.h" -#include "net/mac/tsch/tsch-private.h" #include "net/mac/framer/frame802154.h" #include "net/mac/framer/frame802154e-ie.h" #include "net/mac/llsec802154.h" -/******** Configuration *******/ +/********** Configurarion *********/ /* To enable TSCH security: - * - set LLSEC802154_CONF_ENABLED - * - set LLSEC802154_CONF_USES_EXPLICIT_KEYS - * */ +* - set LLSEC802154_CONF_ENABLED +* - set LLSEC802154_CONF_USES_EXPLICIT_KEYS +* */ #if LLSEC802154_ENABLED && !LLSEC802154_USES_EXPLICIT_KEYS #error LLSEC802154_ENABLED set but LLSEC802154_USES_EXPLICIT_KEYS unset @@ -114,8 +112,9 @@ #define TSCH_SECURITY_KEY_SEC_LEVEL_OTHER 5 /* Encryption + MIC-32, as per 6TiSCH minimal */ #endif -/************ Types ***********/ +/********** Data types *********/ +/* AES-128 key */ typedef uint8_t aes_key[16]; /********** Functions *********/ diff --git a/os/net/mac/tsch/tsch-slot-operation.c b/os/net/mac/tsch/tsch-slot-operation.c index 5fcd969d3..c4bb25e88 100644 --- a/os/net/mac/tsch/tsch-slot-operation.c +++ b/os/net/mac/tsch/tsch-slot-operation.c @@ -52,13 +52,6 @@ #include "net/queuebuf.h" #include "net/mac/framer/framer-802154.h" #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-slot-operation.h" -#include "net/mac/tsch/tsch-queue.h" -#include "net/mac/tsch/tsch-private.h" -#include "net/mac/tsch/tsch-log.h" -#include "net/mac/tsch/tsch-packet.h" -#include "net/mac/tsch/tsch-security.h" -#include "net/mac/tsch/tsch-adaptive-timesync.h" #if CONTIKI_TARGET_COOJA #include "lib/simEnvChange.h" #include "sys/cooja_mt.h" diff --git a/os/net/mac/tsch/tsch-slot-operation.h b/os/net/mac/tsch/tsch-slot-operation.h index 0c0ae60b6..1702ce199 100644 --- a/os/net/mac/tsch/tsch-slot-operation.h +++ b/os/net/mac/tsch/tsch-slot-operation.h @@ -42,61 +42,6 @@ #include "contiki.h" #include "lib/ringbufindex.h" -#include "net/mac/tsch/tsch-packet.h" -#include "net/mac/tsch/tsch-private.h" - -/******** Configuration *******/ - -/* Size of the ring buffer storing dequeued outgoing packets (only an array of pointers). - * Must be power of two, and greater or equal to QUEUEBUF_NUM */ -#ifdef TSCH_CONF_DEQUEUED_ARRAY_SIZE -#define TSCH_DEQUEUED_ARRAY_SIZE TSCH_CONF_DEQUEUED_ARRAY_SIZE -#else -/* By default, round QUEUEBUF_CONF_NUM to next power of two - * (in the range [4;256]) */ -#if QUEUEBUF_CONF_NUM <= 4 -#define TSCH_DEQUEUED_ARRAY_SIZE 4 -#elif QUEUEBUF_CONF_NUM <= 8 -#define TSCH_DEQUEUED_ARRAY_SIZE 8 -#elif QUEUEBUF_CONF_NUM <= 16 -#define TSCH_DEQUEUED_ARRAY_SIZE 16 -#elif QUEUEBUF_CONF_NUM <= 32 -#define TSCH_DEQUEUED_ARRAY_SIZE 32 -#elif QUEUEBUF_CONF_NUM <= 64 -#define TSCH_DEQUEUED_ARRAY_SIZE 64 -#elif QUEUEBUF_CONF_NUM <= 128 -#define TSCH_DEQUEUED_ARRAY_SIZE 128 -#else -#define TSCH_DEQUEUED_ARRAY_SIZE 256 -#endif -#endif - -/* Size of the ring buffer storing incoming packets. - * Must be power of two */ -#ifdef TSCH_CONF_MAX_INCOMING_PACKETS -#define TSCH_MAX_INCOMING_PACKETS TSCH_CONF_MAX_INCOMING_PACKETS -#else -#define TSCH_MAX_INCOMING_PACKETS 4 -#endif - -/*********** Callbacks *********/ - -/* Called by TSCH form interrupt after receiving a frame, enabled upper-layer to decide - * whether to ACK or NACK */ -#ifdef TSCH_CALLBACK_DO_NACK -int TSCH_CALLBACK_DO_NACK(struct tsch_link *link, linkaddr_t *src, linkaddr_t *dst); -#endif - -/************ Types ***********/ - -/* Stores data about an incoming packet */ -struct input_packet { - uint8_t payload[TSCH_PACKET_MAX_LEN]; /* Packet payload */ - struct tsch_asn_t rx_asn; /* ASN when the packet was received */ - int len; /* Packet len */ - int16_t rssi; /* RSSI for this packet */ - uint8_t channel; /* Channel we received the packet on */ -}; /***** External Variables *****/ diff --git a/os/net/mac/tsch/tsch-types.h b/os/net/mac/tsch/tsch-types.h new file mode 100644 index 000000000..6bc8895a2 --- /dev/null +++ b/os/net/mac/tsch/tsch-types.h @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2015, SICS Swedish ICT. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +/** + * \file + * TSCH types + * \author + * Simon Duquennoy + */ + +/** + * \addtogroup tsch + * @{ +*/ + +#ifndef __TSCH_TYPES_H__ +#define __TSCH_TYPES_H__ + +/********** Includes **********/ + +#include "net/mac/tsch/tsch-asn.h" +#include "lib/list.h" +#include "lib/ringbufindex.h" + +/********** Data types **********/ + +/* 802.15.4e link types. + * LINK_TYPE_ADVERTISING_ONLY is an extra one: for EB-only links. */ +enum link_type { LINK_TYPE_NORMAL, LINK_TYPE_ADVERTISING, LINK_TYPE_ADVERTISING_ONLY }; + +struct tsch_link { + /* Links are stored as a list: "next" must be the first field */ + struct tsch_link *next; + /* Unique identifier */ + uint16_t handle; + /* MAC address of neighbor */ + linkaddr_t addr; + /* Slotframe identifier */ + uint16_t slotframe_handle; + /* Identifier of Slotframe to which this link belongs + * Unused. */ + /* uint8_t handle; */ + /* Timeslot for this link */ + uint16_t timeslot; + /* Channel offset for this link */ + uint16_t channel_offset; + /* A bit string that defines + * b0 = Transmit, b1 = Receive, b2 = Shared, b3 = Timekeeping, b4 = reserved */ + uint8_t link_options; + /* Type of link. NORMAL = 0. ADVERTISING = 1, and indicates + the link may be used to send an Enhanced beacon. */ + enum link_type link_type; + /* Any other data for upper layers */ + void *data; +}; + +/* 802.15.4e slotframe (contains links) */ +struct tsch_slotframe { + /* Slotframes are stored as a list: "next" must be the first field */ + struct tsch_slotframe *next; + /* Unique identifier */ + uint16_t handle; + /* Number of timeslots in the slotframe. + * Stored as struct asn_divisor_t because we often need ASN%size */ + struct tsch_asn_divisor_t size; + /* List of links belonging to this slotframe */ + LIST_STRUCT(links_list); +}; + +/* TSCH packet information */ +struct tsch_packet { + struct queuebuf *qb; /* pointer to the queuebuf to be sent */ + mac_callback_t sent; /* callback for this packet */ + void *ptr; /* MAC callback parameter */ + uint8_t transmissions; /* #transmissions performed for this packet */ + uint8_t max_transmissions; /* maximal number of Tx before dropping the packet */ + uint8_t ret; /* status -- MAC return code */ + uint8_t header_len; /* length of header and header IEs (needed for link-layer security) */ + uint8_t tsch_sync_ie_offset; /* Offset within the frame used for quick update of EB ASN and join priority */ +}; + +/* TSCH neighbor information */ +struct tsch_neighbor { + /* Neighbors are stored as a list: "next" must be the first field */ + struct tsch_neighbor *next; + linkaddr_t addr; /* MAC address of the neighbor */ + uint8_t is_broadcast; /* is this neighbor a virtual neighbor used for broadcast (of data packets or EBs) */ + uint8_t is_time_source; /* is this neighbor a time source? */ + uint8_t backoff_exponent; /* CSMA backoff exponent */ + uint8_t backoff_window; /* CSMA backoff window (number of slots to skip) */ + uint8_t last_backoff_window; /* Last CSMA backoff window */ + uint8_t tx_links_count; /* How many links do we have to this neighbor? */ + uint8_t dedicated_tx_links_count; /* How many dedicated links do we have to this neighbor? */ + /* Array for the ringbuf. Contains pointers to packets. + * Its size must be a power of two to allow for atomic put */ + struct tsch_packet *tx_array[TSCH_QUEUE_NUM_PER_NEIGHBOR]; + /* Circular buffer of pointers to packet. */ + struct ringbufindex tx_ringbuf; +}; + +/* TSCH timeslot timing elements. Used to index timeslot timing + * of different units, such as rtimer tick or micro-second */ +enum tsch_timeslot_timing_elements { + tsch_ts_cca_offset, + tsch_ts_cca, + tsch_ts_tx_offset, + tsch_ts_rx_offset, + tsch_ts_rx_ack_delay, + tsch_ts_tx_ack_delay, + tsch_ts_rx_wait, + tsch_ts_ack_wait, + tsch_ts_rx_tx, + tsch_ts_max_ack, + tsch_ts_max_tx, + tsch_ts_timeslot_length, + tsch_ts_elements_count, /* Not a timing element */ +}; + +/* Stores data about an incoming packet */ +struct input_packet { + uint8_t payload[TSCH_PACKET_MAX_LEN]; /* Packet payload */ + struct tsch_asn_t rx_asn; /* ASN when the packet was received */ + int len; /* Packet len */ + int16_t rssi; /* RSSI for this packet */ + uint8_t channel; /* Channel we received the packet on */ +}; + +#endif /* __TSCH_CONF_H__ */ +/** @} */ diff --git a/os/net/mac/tsch/tsch.c b/os/net/mac/tsch/tsch.c index 1dc0bbc7c..dea750120 100644 --- a/os/net/mac/tsch/tsch.c +++ b/os/net/mac/tsch/tsch.c @@ -54,15 +54,6 @@ #include "net/link-stats.h" #include "net/mac/framer/framer-802154.h" #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-slot-operation.h" -#include "net/mac/tsch/tsch-queue.h" -#include "net/mac/tsch/tsch-private.h" -#include "net/mac/tsch/tsch-log.h" -#include "net/mac/tsch/tsch-packet.h" -#include "net/mac/tsch/tsch-security.h" -#if UIP_CONF_IPV6_RPL -#include "net/mac/tsch/tsch-rpl.h" -#endif /* UIP_CONF_IPV6_RPL */ #include "net/mac/mac-sequence.h" #include "lib/random.h" #include "net/routing/routing.h" diff --git a/os/net/mac/tsch/tsch.h b/os/net/mac/tsch/tsch.h index 9d7661cf8..5f2289c66 100644 --- a/os/net/mac/tsch/tsch.h +++ b/os/net/mac/tsch/tsch.h @@ -46,120 +46,40 @@ frequency hopping for enhanced reliability. #include "contiki.h" #include "net/mac/mac.h" +#include "net/linkaddr.h" + +#include "net/mac/tsch/tsch-conf.h" +#include "net/mac/tsch/tsch-const.h" +#include "net/mac/tsch/tsch-types.h" +#include "net/mac/tsch/tsch-adaptive-timesync.h" +#include "net/mac/tsch/tsch-slot-operation.h" +#include "net/mac/tsch/tsch-queue.h" +#include "net/mac/tsch/tsch-log.h" +#include "net/mac/tsch/tsch-packet.h" #include "net/mac/tsch/tsch-security.h" +#include "net/mac/tsch/tsch-schedule.h" +#if UIP_CONF_IPV6_RPL +#include "net/mac/tsch/tsch-rpl.h" +#endif /* UIP_CONF_IPV6_RPL */ -/******** Configuration *******/ +#if CONTIKI_TARGET_COOJA +#include "lib/simEnvChange.h" +#include "sys/cooja_mt.h" +#endif /* CONTIKI_TARGET_COOJA */ -/* Max time before sending a unicast keep-alive message to the time source */ -#ifdef TSCH_CONF_KEEPALIVE_TIMEOUT -#define TSCH_KEEPALIVE_TIMEOUT TSCH_CONF_KEEPALIVE_TIMEOUT +/*********** Macros *********/ + +/* Wait for a condition with timeout t0+offset. */ +#if CONTIKI_TARGET_COOJA +#define BUSYWAIT_UNTIL_ABS(cond, t0, offset) \ + while(!(cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), (t0) + (offset))) { \ + simProcessRunValue = 1; \ + cooja_mt_yield(); \ + }; #else -/* Time to desynch assuming a drift of 40 PPM (80 PPM between two nodes) and guard time of +/-1ms: 12.5s. */ -#define TSCH_KEEPALIVE_TIMEOUT (12 * CLOCK_SECOND) -#endif - -/* With TSCH_ADAPTIVE_TIMESYNC enabled: keep-alive timeout used after reaching - * accurate drift compensation. */ -#ifdef TSCH_CONF_MAX_KEEPALIVE_TIMEOUT -#define TSCH_MAX_KEEPALIVE_TIMEOUT TSCH_CONF_MAX_KEEPALIVE_TIMEOUT -#else -#define TSCH_MAX_KEEPALIVE_TIMEOUT (60 * CLOCK_SECOND) -#endif - -/* Max time without synchronization before leaving the PAN */ -#ifdef TSCH_CONF_DESYNC_THRESHOLD -#define TSCH_DESYNC_THRESHOLD TSCH_CONF_DESYNC_THRESHOLD -#else -#define TSCH_DESYNC_THRESHOLD (2 * TSCH_MAX_KEEPALIVE_TIMEOUT) -#endif - -/* Period between two consecutive EBs */ -#ifdef TSCH_CONF_EB_PERIOD -#define TSCH_EB_PERIOD TSCH_CONF_EB_PERIOD -#else -#define TSCH_EB_PERIOD (16 * CLOCK_SECOND) -#endif - -/* Max Period between two consecutive EBs */ -#ifdef TSCH_CONF_MAX_EB_PERIOD -#define TSCH_MAX_EB_PERIOD TSCH_CONF_MAX_EB_PERIOD -#else -#define TSCH_MAX_EB_PERIOD (16 * CLOCK_SECOND) -#endif - -/* Max acceptable join priority */ -#ifdef TSCH_CONF_MAX_JOIN_PRIORITY -#define TSCH_MAX_JOIN_PRIORITY TSCH_CONF_MAX_JOIN_PRIORITY -#else -#define TSCH_MAX_JOIN_PRIORITY 32 -#endif - -/* Start TSCH automatically after init? If not, the upper layers - * must call NETSTACK_MAC.on() to start it. Useful when the - * application needs to control when the nodes are to start - * scanning or advertising.*/ -#ifdef TSCH_CONF_AUTOSTART -#define TSCH_AUTOSTART TSCH_CONF_AUTOSTART -#else -#define TSCH_AUTOSTART 1 -#endif - -/* Join only secured networks? (discard EBs with security disabled) */ -#ifdef TSCH_CONF_JOIN_SECURED_ONLY -#define TSCH_JOIN_SECURED_ONLY TSCH_CONF_JOIN_SECURED_ONLY -#else -/* By default, set if LLSEC802154_ENABLED is also non-zero */ -#define TSCH_JOIN_SECURED_ONLY LLSEC802154_ENABLED -#endif - -/* By default, join any PAN ID. Otherwise, wait for an EB from IEEE802154_PANID */ -#ifdef TSCH_CONF_JOIN_MY_PANID_ONLY -#define TSCH_JOIN_MY_PANID_ONLY TSCH_CONF_JOIN_MY_PANID_ONLY -#else -#define TSCH_JOIN_MY_PANID_ONLY 1 -#endif - -/* The radio polling frequency (in Hz) during association process */ -#ifdef TSCH_CONF_ASSOCIATION_POLL_FREQUENCY -#define TSCH_ASSOCIATION_POLL_FREQUENCY TSCH_CONF_ASSOCIATION_POLL_FREQUENCY -#else -#define TSCH_ASSOCIATION_POLL_FREQUENCY 100 -#endif - -/* When associating, check ASN against our own uptime (time in minutes).. - * Useful to force joining only with nodes started roughly at the same time. - * Set to the max number of minutes acceptable. */ -#ifdef TSCH_CONF_CHECK_TIME_AT_ASSOCIATION -#define TSCH_CHECK_TIME_AT_ASSOCIATION TSCH_CONF_CHECK_TIME_AT_ASSOCIATION -#else -#define TSCH_CHECK_TIME_AT_ASSOCIATION 0 -#endif - -/* By default: initialize schedule from EB when associating, using the - * slotframe and links Information Element */ -#ifdef TSCH_CONF_INIT_SCHEDULE_FROM_EB -#define TSCH_INIT_SCHEDULE_FROM_EB TSCH_CONF_INIT_SCHEDULE_FROM_EB -#else -#define TSCH_INIT_SCHEDULE_FROM_EB 1 -#endif - -/* An ad-hoc mechanism to have TSCH select its time source without the - * help of an upper-layer, simply by collecting statistics on received - * EBs and their join priority. Disabled by default as we recomment - * mapping the time source on the RPL preferred parent - * (via tsch_rpl_callback_parent_switch) */ -#ifdef TSCH_CONF_AUTOSELECT_TIME_SOURCE -#define TSCH_AUTOSELECT_TIME_SOURCE TSCH_CONF_AUTOSELECT_TIME_SOURCE -#else -#define TSCH_AUTOSELECT_TIME_SOURCE 0 -#endif /* TSCH_CONF_EB_AUTOSELECT */ - -/* To include Sixtop Implementation */ -#ifdef TSCH_CONF_WITH_SIXTOP -#define TSCH_WITH_SIXTOP TSCH_CONF_WITH_SIXTOP -#else -#define TSCH_WITH_SIXTOP 0 -#endif /* TSCH_CONF_EB_AUTOSELECT */ +#define BUSYWAIT_UNTIL_ABS(cond, t0, offset) \ + while(!(cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), (t0) + (offset))) ; +#endif /* CONTIKI_TARGET_COOJA */ /*********** Callbacks *********/ @@ -180,6 +100,18 @@ frequency hopping for enhanced reliability. #endif /* UIP_CONF_IPV6_RPL */ +#if BUILD_WITH_ORCHESTRA + +#ifndef TSCH_CALLBACK_NEW_TIME_SOURCE +#define TSCH_CALLBACK_NEW_TIME_SOURCE orchestra_callback_new_time_source +#endif /* TSCH_CALLBACK_NEW_TIME_SOURCE */ + +#ifndef TSCH_CALLBACK_PACKET_READY +#define TSCH_CALLBACK_PACKET_READY orchestra_callback_packet_ready +#endif /* TSCH_CALLBACK_PACKET_READY */ + +#endif /* BUILD_WITH_ORCHESTRA */ + /* Called by TSCH when joining a network */ #ifdef TSCH_CALLBACK_JOINING_NETWORK void TSCH_CALLBACK_JOINING_NETWORK(); @@ -190,6 +122,28 @@ void TSCH_CALLBACK_JOINING_NETWORK(); void TSCH_CALLBACK_LEAVING_NETWORK(); #endif +/* Called by TSCH after sending a keep-alive */ +#ifdef TSCH_CALLBACK_KA_SENT +void TSCH_CALLBACK_KA_SENT(); +#endif + +/* Called by TSCH form interrupt after receiving a frame, enabled upper-layer to decide + * whether to ACK or NACK */ +#ifdef TSCH_CALLBACK_DO_NACK +int TSCH_CALLBACK_DO_NACK(struct tsch_link *link, linkaddr_t *src, linkaddr_t *dst); +#endif + +/* Called by TSCH when switching time source */ +#ifdef TSCH_CALLBACK_NEW_TIME_SOURCE +struct tsch_neighbor; +void TSCH_CALLBACK_NEW_TIME_SOURCE(const struct tsch_neighbor *old, const struct tsch_neighbor *new); +#endif + +/* Called by TSCH every time a packet is ready to be added to the send queue */ +#ifdef TSCH_CALLBACK_PACKET_READY +void TSCH_CALLBACK_PACKET_READY(void); +#endif + /***** External Variables *****/ /* Are we coordinator of the TSCH network? */ @@ -200,6 +154,29 @@ extern int tsch_is_associated; extern int tsch_is_pan_secured; /* The TSCH MAC driver */ extern const struct mac_driver tschmac_driver; +/* 802.15.4 broadcast MAC address */ +extern const linkaddr_t tsch_broadcast_address; +/* The address we use to identify EB queue */ +extern const linkaddr_t tsch_eb_address; +/* The current Absolute Slot Number (ASN) */ +extern struct tsch_asn_t tsch_current_asn; +extern uint8_t tsch_join_priority; +extern struct tsch_link *current_link; +/* TSCH channel hopping sequence */ +extern uint8_t tsch_hopping_sequence[TSCH_HOPPING_SEQUENCE_MAX_LEN]; +extern struct tsch_asn_divisor_t tsch_hopping_sequence_length; +/* TSCH timeslot timing (in rtimer ticks) */ +extern rtimer_clock_t tsch_timing[tsch_ts_elements_count]; +/* Statistics on the current session */ +extern unsigned long tx_count; +extern unsigned long rx_count; +extern unsigned long sync_count; + +/* TSCH processes */ +PROCESS_NAME(tsch_process); +PROCESS_NAME(tsch_send_eb_process); +PROCESS_NAME(tsch_pending_events_process); + /********** Functions *********/ @@ -213,6 +190,12 @@ void tsch_set_ka_timeout(uint32_t timeout); void tsch_set_coordinator(int enable); /* Set the pan as secured or not */ void tsch_set_pan_secured(int enable); +/* Set TSCH to send a keepalive message after TSCH_KEEPALIVE_TIMEOUT */ +void tsch_schedule_keepalive(void); +/* Set TSCH to send a keepalive message immediately */ +void tsch_schedule_keepalive_immediately(void); +/* Leave the TSCH network */ +void tsch_disassociate(void); #endif /* __TSCH_H__ */ /** @} */ diff --git a/os/services/orchestra/orchestra.h b/os/services/orchestra/orchestra.h index b948a2985..32450466d 100644 --- a/os/services/orchestra/orchestra.h +++ b/os/services/orchestra/orchestra.h @@ -39,8 +39,6 @@ #define __ORCHESTRA_H__ #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-conf.h" -#include "net/mac/tsch/tsch-schedule.h" #include "orchestra-conf.h" /* The structure of an Orchestra rule */ diff --git a/os/services/shell/shell-commands.c b/os/services/shell/shell-commands.c index f8ff63660..2068bf249 100644 --- a/os/services/shell/shell-commands.c +++ b/os/services/shell/shell-commands.c @@ -53,10 +53,6 @@ #include "net/ipv6/uip-ds6.h" #if MAC_CONF_WITH_TSCH #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-adaptive-timesync.h" -#include "net/mac/tsch/tsch-queue.h" -#include "net/mac/tsch/tsch-log.h" -#include "net/mac/tsch/tsch-private.h" #endif /* MAC_CONF_WITH_TSCH */ #include "net/routing/routing.h" #include "net/mac/llsec802154.h" diff --git a/tests/13-ieee802154/code-6tisch/test-sixp-pkt.c b/tests/13-ieee802154/code-6tisch/test-sixp-pkt.c index 5aa518d46..11bc339b5 100644 --- a/tests/13-ieee802154/code-6tisch/test-sixp-pkt.c +++ b/tests/13-ieee802154/code-6tisch/test-sixp-pkt.c @@ -35,7 +35,7 @@ #include "contiki-lib.h" #include "lib/assert.h" -#include "net/mac/tsch/tsch-packet.h" +#include "net/mac/tsch/tsch.h" #include "net/mac/tsch/sixtop/sixp-pkt.h" #include "unit-test/unit-test.h" diff --git a/tests/13-ieee802154/code-flush-nbr-queue/test-flush-nbr-queue.c b/tests/13-ieee802154/code-flush-nbr-queue/test-flush-nbr-queue.c index d8be606ac..57324f26f 100644 --- a/tests/13-ieee802154/code-flush-nbr-queue/test-flush-nbr-queue.c +++ b/tests/13-ieee802154/code-flush-nbr-queue/test-flush-nbr-queue.c @@ -38,7 +38,6 @@ #include "net/linkaddr.h" #include "net/mac/tsch/tsch.h" -#include "net/mac/tsch/tsch-queue.h" #include "unit-test/unit-test.h" #include "common.h" diff --git a/tools/viewconf.c b/tools/viewconf.c index 8be720d56..5e6ee3d5e 100644 --- a/tools/viewconf.c +++ b/tools/viewconf.c @@ -1,8 +1,6 @@ #include "contiki.h" #include "os/net/mac/framer/frame802154.h" #include "os/net/mac/tsch/tsch.h" -#include "os/net/mac/tsch/tsch-conf.h" -#include "os/net/mac/tsch/tsch-schedule.h" #include "os/net/ipv6/uip-nd6.h" #include "os/net/ipv6/uipopt.h" #include "os/net/queuebuf.h"