Add support for IEEE 802.15.4e Information Elements (IEs). All IEs used by TSCH are currently included.

ico
Simon Duquennoy 2015-09-21 18:49:31 +02:00
parent 5cf9871d5b
commit ecba49c282
5 changed files with 1088 additions and 0 deletions

View File

@ -0,0 +1,585 @@
/*
* 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
* IEEE 802.15.4e Information Element (IE) creation and parsing.
* \author
* Simon Duquennoy <simonduq@sics.se>
*/
#include <string.h>
#include "net/mac/frame802154e-ie.h"
#define DEBUG DEBUG_NONE
#include "net/ip/uip-debug.h"
/* c.f. IEEE 802.15.4e Table 4b */
enum ieee802154e_header_ie_id {
HEADER_IE_LE_CSL = 0x1a,
HEADER_IE_LE_RIT,
HEADER_IE_DSME_PAN_DESCRIPTOR,
HEADER_IE_RZ_TIME,
HEADER_IE_ACK_NACK_TIME_CORRECTION,
HEADER_IE_GACK,
HEADER_IE_LOW_LATENCY_NETWORK_INFO,
HEADER_IE_LIST_TERMINATION_1 = 0x7e,
HEADER_IE_LIST_TERMINATION_2 = 0x7f,
};
/* c.f. IEEE 802.15.4e Table 4c */
enum ieee802154e_payload_ie_id {
PAYLOAD_IE_ESDU = 0,
PAYLOAD_IE_MLME,
PAYLOAD_IE_LIST_TERMINATION = 0xf,
};
/* c.f. IEEE 802.15.4e Table 4d */
enum ieee802154e_mlme_short_subie_id {
MLME_SHORT_IE_TSCH_SYNCHRONIZATION = 0x1a,
MLME_SHORT_IE_TSCH_SLOFTRAME_AND_LINK,
MLME_SHORT_IE_TSCH_TIMESLOT,
MLME_SHORT_IE_TSCH_HOPPING_TIMING,
MLME_SHORT_IE_TSCH_EB_FILTER,
MLME_SHORT_IE_TSCH_MAC_METRICS_1,
MLME_SHORT_IE_TSCH_MAC_METRICS_2,
};
/* c.f. IEEE 802.15.4e Table 4e */
enum ieee802154e_mlme_long_subie_id {
MLME_LONG_IE_TSCH_CHANNEL_HOPPING_SEQUENCE = 0x9,
};
#define WRITE16(buf, val) \
do { ((uint8_t *)(buf))[0] = (val) & 0xff; \
((uint8_t *)(buf))[1] = ((val) >> 8) & 0xff; } while(0);
#define READ16(buf, var) \
(var) = ((uint8_t *)(buf))[0] | ((uint8_t *)(buf))[1] << 8
/* Create a header IE 2-byte descriptor */
static void
create_header_ie_descriptor(uint8_t *buf, uint8_t element_id, int ie_len)
{
uint16_t ie_desc;
/* Header IE descriptor: b0-6: len, b7-14: element id:, b15: type: 0 */
ie_desc = (ie_len & 0x7f) + ((element_id & 0xff) << 7);
WRITE16(buf, ie_desc);
}
/* Create a payload IE 2-byte descriptor */
static void
create_payload_ie_descriptor(uint8_t *buf, uint8_t group_id, int ie_len)
{
uint16_t ie_desc;
/* MLME Long IE descriptor: b0-10: len, b11-14: group id:, b15: type: 1 */
ie_desc = (ie_len & 0x07ff) + ((group_id & 0x0f) << 11) + (1 << 15);
WRITE16(buf, ie_desc);
}
/* Create a MLME short IE 2-byte descriptor */
static void
create_mlme_short_ie_descriptor(uint8_t *buf, uint8_t sub_id, int ie_len)
{
uint16_t ie_desc;
/* MLME Short IE descriptor: b0-7: len, b8-14: sub id:, b15: type: 0 */
ie_desc = (ie_len & 0xff) + ((sub_id & 0x7f) << 8);
WRITE16(buf, ie_desc);
}
/* Create a MLME long IE 2-byte descriptor */
static void
create_mlme_long_ie_descriptor(uint8_t *buf, uint8_t sub_id, int ie_len)
{
uint16_t ie_desc;
/* MLME Long IE descriptor: b0-10: len, b11-14: sub id:, b15: type: 1 */
ie_desc = (ie_len & 0x07ff) + ((sub_id & 0x0f) << 11) + (1 << 15);
WRITE16(buf, ie_desc);
}
/* Header IE. ACK/NACK time correction. Used in enhanced ACKs */
int
frame80215e_create_ie_header_ack_nack_time_correction(uint8_t *buf, int len,
struct ieee802154_ies *ies)
{
int ie_len = 2;
if(len >= 2 + ie_len && ies != NULL) {
int16_t drift_us;
uint16_t time_sync_field;
drift_us = ies->ie_time_correction;
time_sync_field = drift_us & 0x0fff;
if(ies->ie_is_nack) {
time_sync_field |= 0x8000;
}
WRITE16(buf+2, time_sync_field);
create_header_ie_descriptor(buf, HEADER_IE_ACK_NACK_TIME_CORRECTION, ie_len);
return 2 + ie_len;
} else {
return -1;
}
}
/* Header IE. List termination 1 (Signals the end of the Header IEs when
* followed by payload IEs) */
int
frame80215e_create_ie_header_list_termination_1(uint8_t *buf, int len,
struct ieee802154_ies *ies)
{
int ie_len = 0;
if(len >= 2 + ie_len && ies != NULL) {
create_header_ie_descriptor(buf, HEADER_IE_LIST_TERMINATION_1, 0);
return 2 + ie_len;
} else {
return -1;
}
}
/* Header IE. List termination 2 (Signals the end of the Header IEs when
* followed by an unformatted payload) */
int
frame80215e_create_ie_header_list_termination_2(uint8_t *buf, int len,
struct ieee802154_ies *ies)
{
int ie_len = 0;
if(len >= 2 + ie_len && ies != NULL) {
create_header_ie_descriptor(buf, HEADER_IE_LIST_TERMINATION_2, 0);
return 2 + ie_len;
} else {
return -1;
}
}
/* Payload IE. List termination */
int
frame80215e_create_ie_payload_list_termination(uint8_t *buf, int len,
struct ieee802154_ies *ies)
{
int ie_len = 0;
if(len >= 2 + ie_len && ies != NULL) {
create_payload_ie_descriptor(buf, PAYLOAD_IE_LIST_TERMINATION, 0);
return 2 + ie_len;
} else {
return -1;
}
}
/* Payload IE. MLME. Used to nest sub-IEs */
int
frame80215e_create_ie_mlme(uint8_t *buf, int len,
struct ieee802154_ies *ies)
{
int ie_len = 0;
if(len >= 2 + ie_len && ies != NULL) {
/* The length of the outer MLME IE is the total length of sub-IEs */
create_payload_ie_descriptor(buf, PAYLOAD_IE_MLME, ies->ie_mlme_len);
return 2 + ie_len;
} else {
return -1;
}
}
/* MLME sub-IE. TSCH synchronization. Used in EBs: ASN and join priority */
int
frame80215e_create_ie_tsch_synchronization(uint8_t *buf, int len,
struct ieee802154_ies *ies)
{
int ie_len = 6;
if(len >= 2 + ie_len && ies != NULL) {
buf[2] = ies->ie_asn.ls4b;
buf[3] = ies->ie_asn.ls4b >> 8;
buf[4] = ies->ie_asn.ls4b >> 16;
buf[5] = ies->ie_asn.ls4b >> 24;
buf[6] = ies->ie_asn.ms1b;
buf[7] = ies->ie_join_priority;
create_mlme_short_ie_descriptor(buf, MLME_SHORT_IE_TSCH_SYNCHRONIZATION, ie_len);
return 2 + ie_len;
} else {
return -1;
}
}
/* MLME sub-IE. TSCH slotframe and link. Used in EBs: initial schedule */
int
frame80215e_create_ie_tsch_slotframe_and_link(uint8_t *buf, int len,
struct ieee802154_ies *ies)
{
if(ies != NULL) {
int i;
int num_slotframes = ies->ie_tsch_slotframe_and_link.num_slotframes;
int num_links = ies->ie_tsch_slotframe_and_link.num_links;
int ie_len = 1 + num_slotframes * (4 + 5 * num_links);
if(num_slotframes > 1 || num_links > FRAME802154E_IE_MAX_LINKS
|| len < 2 + ie_len) {
/* We support only 0 or 1 slotframe in this IE and a predefined maximum number of links */
return -1;
}
/* Insert IE */
buf[2] = num_slotframes;
/* Insert slotframe */
if(num_slotframes == 1) {
buf[2 + 1] = ies->ie_tsch_slotframe_and_link.slotframe_handle;
WRITE16(buf + 2 + 2, ies->ie_tsch_slotframe_and_link.slotframe_size);
buf[2 + 4] = num_links;
/* Loop over links */
for(i = 0; i < num_links; i++) {
/* Insert links */
WRITE16(buf + 2 + 5 + i * 5, ies->ie_tsch_slotframe_and_link.links[i].timeslot);
WRITE16(buf + 2 + 5 + i * 5 + 2, ies->ie_tsch_slotframe_and_link.links[i].channel_offset);
buf[2 + 5 + i * 5 + 4] = ies->ie_tsch_slotframe_and_link.links[i].link_options;
}
}
create_mlme_short_ie_descriptor(buf, MLME_SHORT_IE_TSCH_SLOFTRAME_AND_LINK, ie_len);
return 2 + ie_len;
} else {
return -1;
}
}
/* MLME sub-IE. TSCH timeslot. Used in EBs: timeslot template (timing) */
int
frame80215e_create_ie_tsch_timeslot(uint8_t *buf, int len,
struct ieee802154_ies *ies)
{
int ie_len;
if(ies == NULL) {
return -1;
}
/* Only ID if ID == 0, else full timing description */
ie_len = ies->ie_tsch_timeslot_id == 0 ? 1 : 25;
if(len >= 2 + ie_len) {
buf[2] = ies->ie_tsch_timeslot_id;
if(ies->ie_tsch_timeslot_id != 0) {
int i;
for(i = 0; i < tsch_ts_elements_count; i++) {
WRITE16(buf + 3 + 2 * i, ies->ie_tsch_timeslot[i]);
}
}
create_mlme_short_ie_descriptor(buf, MLME_SHORT_IE_TSCH_TIMESLOT, ie_len);
return 2 + ie_len;
} else {
return -1;
}
}
/* MLME sub-IE. TSCH channel hopping sequence. Used in EBs: hopping sequence */
int
frame80215e_create_ie_tsch_channel_hopping_sequence(uint8_t *buf, int len,
struct ieee802154_ies *ies)
{
int ie_len;
if(ies == NULL || ies->ie_hopping_sequence_len > sizeof(ies->ie_hopping_sequence_list)) {
return -1;
}
ie_len = ies->ie_channel_hopping_sequence_id == 0 ? 1 : 12 + ies->ie_hopping_sequence_len;
if(len >= 2 + ie_len && ies != NULL) {
buf[2] = ies->ie_channel_hopping_sequence_id;
buf[3] = 0; /* channel page */
WRITE16(buf + 4, 0); /* number of channels */
WRITE16(buf + 6, 0); /* phy configuration */
WRITE16(buf + 8, 0);
/* Extended bitmap. Size: 0 */
WRITE16(buf + 10, ies->ie_hopping_sequence_len); /* sequence len */
memcpy(buf + 12, ies->ie_hopping_sequence_list, ies->ie_hopping_sequence_len); /* sequence list */
WRITE16(buf + 12 + ies->ie_hopping_sequence_len, 0); /* current hop */
create_mlme_long_ie_descriptor(buf, MLME_LONG_IE_TSCH_CHANNEL_HOPPING_SEQUENCE, ie_len);
return 2 + ie_len;
} else {
return -1;
}
}
/* Parse a header IE */
static int
frame802154e_parse_header_ie(const uint8_t *buf, int len,
uint8_t element_id, struct ieee802154_ies *ies)
{
switch(element_id) {
case HEADER_IE_ACK_NACK_TIME_CORRECTION:
if(len == 2) {
if(ies != NULL) {
/* If the originator was a time source neighbor, the receiver adjust
* its own clock by incorporating the received drift correction */
uint16_t time_sync_field = 0;
int16_t drift_us = 0;
/* Extract drift correction from Sync-IE, cast from 12 to 16-bit,
* and convert it to RTIMER ticks.
* See page 88 in IEEE Std 802.15.4e-2012. */
READ16(buf, time_sync_field);
/* First extract NACK */
ies->ie_is_nack = (time_sync_field & (uint16_t)0x8000) ? 1 : 0;
/* Then cast from 12 to 16 bit signed */
if(time_sync_field & 0x0800) { /* Negative integer */
drift_us = time_sync_field | 0xf000;
} else { /* Positive integer */
drift_us = time_sync_field & 0x0fff;
}
/* Convert to RTIMER ticks */
ies->ie_time_correction = drift_us;
}
return len;
}
break;
}
return -1;
}
/* Parse a MLME short IE */
static int
frame802154e_parse_mlme_short_ie(const uint8_t *buf, int len,
uint8_t sub_id, struct ieee802154_ies *ies)
{
switch(sub_id) {
case MLME_SHORT_IE_TSCH_SLOFTRAME_AND_LINK:
if(len >= 1) {
int i;
int num_slotframes = buf[0];
int num_links = buf[4];
if(num_slotframes == 0) {
return len;
}
if(num_slotframes <= 1 && num_links <= FRAME802154E_IE_MAX_LINKS
&& len == 1 + num_slotframes * (4 + 5 * num_links)) {
if(ies != NULL) {
/* We support only 0 or 1 slotframe in this IE and a predefined maximum number of links */
ies->ie_tsch_slotframe_and_link.num_slotframes = buf[0];
ies->ie_tsch_slotframe_and_link.slotframe_handle = buf[1];
READ16(buf + 2, ies->ie_tsch_slotframe_and_link.slotframe_size);
ies->ie_tsch_slotframe_and_link.num_links = buf[4];
for(i = 0; i < num_links; i++) {
READ16(buf + 5 + i * 5, ies->ie_tsch_slotframe_and_link.links[i].timeslot);
READ16(buf + 5 + i * 5 + 2, ies->ie_tsch_slotframe_and_link.links[i].channel_offset);
ies->ie_tsch_slotframe_and_link.links[i].link_options = buf[5 + i * 5 + 4];
}
}
return len;
}
}
break;
case MLME_SHORT_IE_TSCH_SYNCHRONIZATION:
if(len == 6) {
if(ies != NULL) {
ies->ie_asn.ls4b = (uint32_t)buf[0];
ies->ie_asn.ls4b |= (uint32_t)buf[1] << 8;
ies->ie_asn.ls4b |= (uint32_t)buf[2] << 16;
ies->ie_asn.ls4b |= (uint32_t)buf[3] << 24;
ies->ie_asn.ms1b = (uint8_t)buf[4];
ies->ie_join_priority = (uint8_t)buf[5];
}
return len;
}
break;
case MLME_SHORT_IE_TSCH_TIMESLOT:
if(len == 1 || len == 25) {
if(ies != NULL) {
ies->ie_tsch_timeslot_id = buf[0];
if(len == 25) {
int i;
for(i = 0; i < tsch_ts_elements_count; i++) {
READ16(buf+1+2*i, ies->ie_tsch_timeslot[i]);
}
}
}
return len;
}
break;
}
return -1;
}
/* Parse a MLME long IE */
static int
frame802154e_parse_mlme_long_ie(const uint8_t *buf, int len,
uint8_t sub_id, struct ieee802154_ies *ies)
{
switch(sub_id) {
case MLME_LONG_IE_TSCH_CHANNEL_HOPPING_SEQUENCE:
if(len > 0) {
if(ies != NULL) {
ies->ie_channel_hopping_sequence_id = buf[0];
if(len > 1) {
READ16(buf+8, ies->ie_hopping_sequence_len); /* sequence len */
if(ies->ie_hopping_sequence_len <= sizeof(ies->ie_hopping_sequence_list)
&& len == 12 + ies->ie_hopping_sequence_len) {
memcpy(ies->ie_hopping_sequence_list, buf+10, ies->ie_hopping_sequence_len); /* sequence list */
}
}
}
return len;
}
break;
}
return -1;
}
/* Parse all IEEE 802.15.4e Information Elements (IE) from a frame */
int
frame802154e_parse_information_elements(const uint8_t *buf, uint8_t buf_size,
struct ieee802154_ies *ies)
{
const uint8_t *start = buf;
uint16_t ie_desc;
uint8_t type;
uint8_t id;
uint16_t len = 0;
int nested_mlme_len = 0;
enum {PARSING_HEADER_IE, PARSING_PAYLOAD_IE, PARSING_MLME_SUBIE} parsing_state;
if(ies == NULL) {
return -1;
}
/* Always look for a header IE first (at least "list termination 1") */
parsing_state = PARSING_HEADER_IE;
ies->ie_payload_ie_offset = 0;
/* Loop over all IEs */
while(buf_size > 0) {
if(buf_size < 2) { /* Not enough space for IE descriptor */
return -1;
}
READ16(buf, ie_desc);
buf_size -= 2;
buf += 2;
type = ie_desc & 0x8000 ? 1 : 0; /* b15 */
PRINTF("frame802154e: ie type %u, current state %u\n", type, parsing_state);
switch(parsing_state) {
case PARSING_HEADER_IE:
if(type != 0) {
PRINTF("frame802154e: wrong type %04x\n", ie_desc);
return -1;
}
/* Header IE: 2 bytes descriptor, c.f. fig 48n in IEEE 802.15.4e */
len = ie_desc & 0x007f; /* b0-b6 */
id = (ie_desc & 0x7f80) >> 7; /* b7-b14 */
PRINTF("frame802154e: header ie len %u id %x\n", len, id);
switch(id) {
case HEADER_IE_LIST_TERMINATION_1:
if(len == 0) {
/* End of payload IE list, now expect payload IEs */
parsing_state = PARSING_PAYLOAD_IE;
ies->ie_payload_ie_offset = buf - start; /* Save IE header len */
PRINTF("frame802154e: list termination 1, look for payload IEs\n");
} else {
PRINTF("frame802154e: list termination 1, wrong len %u\n", len);
return -1;
}
break;
case HEADER_IE_LIST_TERMINATION_2:
/* End of IE parsing */
if(len == 0) {
ies->ie_payload_ie_offset = buf - start; /* Save IE header len */
PRINTF("frame802154e: list termination 2\n");
return buf + len - start;
} else {
PRINTF("frame802154e: list termination 2, wrong len %u\n", len);
return -1;
}
default:
if(len > buf_size || frame802154e_parse_header_ie(buf, len, id, ies) == -1) {
PRINTF("frame802154e: failed to parse\n");
return -1;
}
break;
}
break;
case PARSING_PAYLOAD_IE:
if(type != 1) {
PRINTF("frame802154e: wrong type %04x\n", ie_desc);
return -1;
}
/* Payload IE: 2 bytes descriptor, c.f. fig 48o in IEEE 802.15.4e */
len = ie_desc & 0x7ff; /* b0-b10 */
id = (ie_desc & 0x7800) >> 11; /* b11-b14 */
PRINTF("frame802154e: payload ie len %u id %x\n", len, id);
switch(id) {
case PAYLOAD_IE_MLME:
/* Now expect 'len' bytes of MLME sub-IEs */
parsing_state = PARSING_MLME_SUBIE;
nested_mlme_len = len;
len = 0; /* Reset len as we want to read subIEs and not jump over them */
PRINTF("frame802154e: entering MLME ie with len %u\n", nested_mlme_len);
break;
case PAYLOAD_IE_LIST_TERMINATION:
PRINTF("frame802154e: payload ie list termination %u\n", len);
return (len == 0) ? buf + len - start : -1;
default:
PRINTF("frame802154e: non-supported payload ie\n");
return -1;
}
break;
case PARSING_MLME_SUBIE:
/* MLME sub-IE: 2 bytes descriptor, c.f. fig 48q in IEEE 802.15.4e */
/* type == 0 means short sub-IE, type == 1 means long sub-IE */
if(type == 0) {
/* Short sub-IE, c.f. fig 48r in IEEE 802.15.4e */
len = ie_desc & 0x00ff; /* b0-b7 */
id = (ie_desc & 0x7f00) >> 8; /* b8-b14 */
PRINTF("frame802154e: short mlme ie len %u id %x\n", len, id);
if(len > buf_size || frame802154e_parse_mlme_short_ie(buf, len, id, ies) == -1) {
PRINTF("frame802154e: failed to parse ie\n");
return -1;
}
} else {
/* Long sub-IE, c.f. fig 48s in IEEE 802.15.4e */
len = ie_desc & 0x7ff; /* b0-b10 */
id = (ie_desc & 0x7800) >> 11; /* b11-b14 */
PRINTF("frame802154e: long mlme ie len %u id %x\n", len, id);
if(len > buf_size || frame802154e_parse_mlme_long_ie(buf, len, id, ies) == -1) {
PRINTF("frame802154e: failed to parse ie\n");
return -1;
}
}
/* Update remaining nested MLME len */
nested_mlme_len -= 2 + len;
if(nested_mlme_len < 0) {
PRINTF("frame802154e: found more sub-IEs than initially advertised\n");
/* We found more sub-IEs than initially advertised */
return -1;
}
if(nested_mlme_len == 0) {
PRINTF("frame802154e: end of MLME IE parsing\n");
/* End of IE parsing, look for another payload IE */
parsing_state = PARSING_PAYLOAD_IE;
}
break;
}
buf += len;
buf_size -= len;
}
if(parsing_state == PARSING_HEADER_IE) {
ies->ie_payload_ie_offset = buf - start; /* Save IE header len */
}
return buf - start;
}

View File

@ -0,0 +1,120 @@
/*
* 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
* IEEE 802.15.4e Information Element (IE) creation and parsing.
* \author
* Simon Duquennoy <simonduq@sics.se>
*/
#ifndef FRAME_802154E_H
#define FRAME_802154E_H
#include "contiki.h"
/* We need definitions from tsch-private.h for TSCH-specific information elements */
#include "net/mac/tsch/tsch-private.h"
#define FRAME802154E_IE_MAX_LINKS 4
/* Structures used for the Slotframe and Links information element */
struct tsch_slotframe_and_links_link {
uint16_t timeslot;
uint16_t channel_offset;
uint8_t link_options;
};
struct tsch_slotframe_and_links {
uint8_t num_slotframes; /* We support only 0 or 1 slotframe in this IE */
uint8_t slotframe_handle;
uint16_t slotframe_size;
uint8_t num_links;
struct tsch_slotframe_and_links_link links[FRAME802154E_IE_MAX_LINKS];
};
/* The information elements that we currently support */
struct ieee802154_ies {
/* Header IEs */
int16_t ie_time_correction;
uint8_t ie_is_nack;
/* Payload MLME */
uint8_t ie_payload_ie_offset;
uint16_t ie_mlme_len;
/* Payload Short MLME IEs */
uint8_t ie_tsch_synchronization_offset;
struct asn_t ie_asn;
uint8_t ie_join_priority;
uint8_t ie_tsch_timeslot_id;
uint16_t ie_tsch_timeslot[tsch_ts_elements_count];
struct tsch_slotframe_and_links ie_tsch_slotframe_and_link;
/* Payload Long MLME IEs */
uint8_t ie_channel_hopping_sequence_id;
/* We include and parse only the sequence len and list and omit unused fields */
uint16_t ie_hopping_sequence_len;
uint8_t ie_hopping_sequence_list[TSCH_HOPPING_SEQUENCE_MAX_LEN];
};
/** Insert various Information Elements **/
/* Header IE. ACK/NACK time correction. Used in enhanced ACKs */
int frame80215e_create_ie_header_ack_nack_time_correction(uint8_t *buf, int len,
struct ieee802154_ies *ies);
/* Header IE. List termination 1 (Signals the end of the Header IEs when
* followed by payload IEs) */
int frame80215e_create_ie_header_list_termination_1(uint8_t *buf, int len,
struct ieee802154_ies *ies);
/* Header IE. List termination 2 (Signals the end of the Header IEs when
* followed by an unformatted payload) */
int frame80215e_create_ie_header_list_termination_2(uint8_t *buf, int len,
struct ieee802154_ies *ies);
/* Payload IE. List termination */
int frame80215e_create_ie_payload_list_termination(uint8_t *buf, int len,
struct ieee802154_ies *ies);
/* Payload IE. MLME. Used to nest sub-IEs */
int frame80215e_create_ie_mlme(uint8_t *buf, int len,
struct ieee802154_ies *ies);
/* MLME sub-IE. TSCH synchronization. Used in EBs: ASN and join priority */
int frame80215e_create_ie_tsch_synchronization(uint8_t *buf, int len,
struct ieee802154_ies *ies);
/* MLME sub-IE. TSCH slotframe and link. Used in EBs: initial schedule */
int frame80215e_create_ie_tsch_slotframe_and_link(uint8_t *buf, int len,
struct ieee802154_ies *ies);
/* MLME sub-IE. TSCH timeslot. Used in EBs: timeslot template (timing) */
int frame80215e_create_ie_tsch_timeslot(uint8_t *buf, int len,
struct ieee802154_ies *ies);
/* MLME sub-IE. TSCH channel hopping sequence. Used in EBs: hopping sequence */
int frame80215e_create_ie_tsch_channel_hopping_sequence(uint8_t *buf, int len,
struct ieee802154_ies *ies);
/* Parse all Information Elements of a frame */
int frame802154e_parse_information_elements(const uint8_t *buf, uint8_t buf_size,
struct ieee802154_ies *ies);
#endif /* FRAME_802154E_H */

View File

@ -0,0 +1,97 @@
/*
* 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 5-Byte Absolute Slot Number (ASN) management
* \author
* Simon Duquennoy <simonduq@sics.se>
*
*/
#ifndef __TSCH_ASN_H__
#define __TSCH_ASN_H__
/************ Types ***********/
/* The ASN is an absolute slot number over 5 bytes. */
struct asn_t {
uint32_t ls4b; /* least significant 4 bytes */
uint8_t ms1b; /* most significant 1 byte */
};
/* For quick modulo operation on ASN */
struct asn_divisor_t {
uint16_t val; /* Divisor value */
uint16_t asn_ms1b_remainder; /* Remainder of the operation 0x100000000 / val */
};
/************ Macros **********/
/* Initialize ASN */
#define ASN_INIT(asn, ms1b_, ls4b_) do { \
(asn).ms1b = (ms1b_); \
(asn).ls4b = (ls4b_); \
} while(0);
/* Increment an ASN by inc (32 bits) */
#define ASN_INC(asn, inc) do { \
uint32_t new_ls4b = (asn).ls4b + (inc); \
if(new_ls4b < (asn).ls4b) { (asn).ms1b++; } \
(asn).ls4b = new_ls4b; \
} while(0);
/* Decrement an ASN by inc (32 bits) */
#define ASN_DEC(asn, dec) do { \
uint32_t new_ls4b = (asn).ls4b - (dec); \
if(new_ls4b > (asn).ls4b) { (asn).ms1b--; } \
(asn).ls4b = new_ls4b; \
} while(0);
/* Returns the 32-bit diff between asn1 and asn2 */
#define ASN_DIFF(asn1, asn2) \
((asn1).ls4b - (asn2).ls4b)
/* Initialize a struct asn_divisor_t */
#define ASN_DIVISOR_INIT(div, val_) do { \
(div).val = (val_); \
(div).asn_ms1b_remainder = ((0xffffffff % (val_)) + 1) % (val_); \
} while(0);
/* Returns the result (16 bits) of a modulo operation on ASN,
* with divisor being a struct asn_divisor_t */
#define ASN_MOD(asn, div) \
((uint16_t)((asn).ls4b % (div).val) \
+ (uint16_t)((asn).ms1b * (div).asn_ms1b_remainder % (div).val)) \
% (div).val
#endif /* __TSCH_ASN_H__ */

View File

@ -0,0 +1,171 @@
/*
* 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 <simonduq@sics.se>
*/
#ifndef __TSCH_CONF_H__
#define __TSCH_CONF_H__
/********** Includes **********/
#include "contiki.h"
/******** Configuration *******/
/* 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 }
/* 1 channel, sequence length 1 */
#define TSCH_HOPPING_SEQUENCE_1_1 (uint8_t[]){ 20 }
/* Default hopping sequence, used in case hopping sequence ID == 0 */
#ifdef TSCH_CONF_DEFAULT_HOPPING_SEQUENCE
#define TSCH_DEFAULT_HOPPING_SEQUENCE TSCH_CONF_DEFAULT_HOPPING_SEQUENCE
#else
#define TSCH_DEFAULT_HOPPING_SEQUENCE TSCH_HOPPING_SEQUENCE_4_4
#endif
/* Hopping sequence used for joining (scan channels) */
#ifdef TSCH_CONF_JOIN_HOPPING_SEQUENCE
#define TSCH_JOIN_HOPPING_SEQUENCE TSCH_CONF_JOIN_HOPPING_SEQUENCE
#else
#define TSCH_JOIN_HOPPING_SEQUENCE TSCH_DEFAULT_HOPPING_SEQUENCE
#endif
/* Maximum length of the TSCH channel hopping sequence. Must be greater or
* equal to the length of TSCH_DEFAULT_HOPPING_SEQUENCE. */
#ifdef TSCH_CONF_HOPPING_SEQUENCE_MAX_LEN
#define TSCH_HOPPING_SEQUENCE_MAX_LEN TSCH_CONF_HOPPING_SEQUENCE_MAX_LEN
#else
#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 platfroms 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 == 65000
/* 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 or z1 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
/* A custom feature allowing upper layers to assign packets to
* a specific slotframe and link */
#ifdef TSCH_CONF_WITH_LINK_SELECTOR
#define TSCH_WITH_LINK_SELECTOR TSCH_CONF_WITH_LINK_SELECTOR
#else /* TSCH_CONF_WITH_LINK_SELECTOR */
#define TSCH_WITH_LINK_SELECTOR 0
#endif /* TSCH_CONF_WITH_LINK_SELECTOR */
#endif /* __TSCH_CONF_H__ */

View File

@ -0,0 +1,115 @@
/*
* 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 <simonduq@sics.se>
* Beshr Al Nahas <beshr@sics.se>
*/
#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"
/************ 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 asn_t 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 asn_divisor_t tsch_hopping_sequence_length;
/* TSCH timeslot timing (in rtimer ticks) */
extern rtimer_clock_t tsch_timing[tsch_ts_elements_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);
/* 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. */
#define BUSYWAIT_UNTIL_ABS(cond, t0, offset) \
while(!(cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), (t0) + (offset))) ;
#endif /* __TSCH_PRIVATE_H__ */