2016-10-21 10:53:08 +00:00
|
|
|
/*
|
2016-11-02 13:12:07 +00:00
|
|
|
* Copyright (c) 2016, Yasuyuki Tanaka
|
2016-10-21 10:53:08 +00:00
|
|
|
* 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.
|
2016-11-02 13:12:07 +00:00
|
|
|
* 3. Neither the name of the copyright holder nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
2016-10-21 10:53:08 +00:00
|
|
|
*
|
2016-11-02 13:12:07 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
|
|
|
* COPYRIGHT HOLDER 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.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* \addtogroup sixtop
|
|
|
|
* @{
|
2016-10-21 10:53:08 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* \file
|
2016-11-02 13:12:07 +00:00
|
|
|
* 6TiSCH Operation Sublayer (6top)
|
|
|
|
*
|
2016-10-21 10:53:08 +00:00
|
|
|
* \author
|
2016-11-02 13:12:07 +00:00
|
|
|
* Yasuyuki Tanaka <yasuyuki.tanaka@inf.ethz.ch>
|
2016-10-21 10:53:08 +00:00
|
|
|
*/
|
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
#include "lib/assert.h"
|
|
|
|
|
2016-10-21 10:53:08 +00:00
|
|
|
#include "net/netstack.h"
|
|
|
|
#include "net/packetbuf.h"
|
2016-11-02 13:12:07 +00:00
|
|
|
#include "net/mac/framer/frame802154.h"
|
|
|
|
#include "net/mac/framer/frame802154e-ie.h"
|
|
|
|
|
2016-10-21 10:53:08 +00:00
|
|
|
#include "sixtop.h"
|
2016-11-02 13:12:07 +00:00
|
|
|
#include "sixtop-conf.h"
|
|
|
|
#include "sixp.h"
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2017-07-11 15:26:40 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "sys/log.h"
|
|
|
|
#define LOG_MODULE "6top"
|
|
|
|
#define LOG_LEVEL LOG_LEVEL_6TOP
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
const sixtop_sf_t *scheduling_functions[SIXTOP_MAX_SCHEDULING_FUNCTIONS];
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
const sixtop_sf_t *sixtop_find_sf(uint8_t sfid);
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
strip_payload_termination_ie(void)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = packetbuf_dataptr();
|
|
|
|
if(ptr[0] == 0x00 && ptr[1] == 0xf8) {
|
|
|
|
/* Payload Termination IE is 2 octets long */
|
|
|
|
packetbuf_hdrreduce(2);
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2016-11-02 13:12:07 +00:00
|
|
|
sixtop_add_sf(const sixtop_sf_t *sf)
|
2016-10-21 10:53:08 +00:00
|
|
|
{
|
2016-11-02 13:12:07 +00:00
|
|
|
int i;
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
assert(sf != NULL);
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2017-07-11 15:26:40 +00:00
|
|
|
LOG_INFO("6top: sixtop_add_sf() is adding a SF [SFID:%u]\n", sf->sfid);
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
if(sixtop_find_sf(sf->sfid) != NULL) {
|
2017-07-11 15:26:40 +00:00
|
|
|
LOG_ERR("6top: sixtop_add_sf() fails because of duplicate SF\n");
|
2016-10-21 10:53:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
for(i = 0; i < SIXTOP_MAX_SCHEDULING_FUNCTIONS; i++) {
|
|
|
|
if(scheduling_functions[i] == NULL) {
|
|
|
|
scheduling_functions[i] = sf;
|
|
|
|
if(sf->init != NULL) {
|
|
|
|
sf->init();
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
2016-11-02 13:12:07 +00:00
|
|
|
break;
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
if(i == SIXTOP_MAX_SCHEDULING_FUNCTIONS) {
|
2017-07-11 15:26:40 +00:00
|
|
|
LOG_ERR("6top: sixtop_add_sf() fails because of no memory\n");
|
2016-10-21 10:53:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
if(sf->init != NULL) {
|
|
|
|
sf->init();
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
2017-07-11 15:26:40 +00:00
|
|
|
LOG_INFO("6top: SF [SFID:%u] has been added and initialized\n", sf->sfid);
|
2016-11-02 13:12:07 +00:00
|
|
|
return 0;
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-11-02 13:12:07 +00:00
|
|
|
const sixtop_sf_t *
|
|
|
|
sixtop_find_sf(uint8_t sfid)
|
2016-10-21 10:53:08 +00:00
|
|
|
{
|
2016-11-02 13:12:07 +00:00
|
|
|
int i;
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
for(i = 0; i < SIXTOP_MAX_SCHEDULING_FUNCTIONS; i++) {
|
|
|
|
if(scheduling_functions[i] != NULL &&
|
|
|
|
scheduling_functions[i]->sfid == sfid) {
|
|
|
|
return (const sixtop_sf_t *)scheduling_functions[i];
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
return NULL;
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2016-11-02 13:12:07 +00:00
|
|
|
sixtop_output(const linkaddr_t *dest_addr, mac_callback_t callback, void *arg)
|
2016-10-21 10:53:08 +00:00
|
|
|
{
|
2016-11-02 13:12:07 +00:00
|
|
|
uint8_t *p;
|
|
|
|
struct ieee802154_ies ies;
|
|
|
|
int len;
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
assert(dest_addr != NULL);
|
|
|
|
if(dest_addr == NULL) {
|
2017-07-11 15:26:40 +00:00
|
|
|
LOG_ERR("6top: sixtop_output() fails because dest_addr is NULL\n");
|
2016-11-02 13:12:07 +00:00
|
|
|
if(callback != NULL) {
|
|
|
|
callback(arg, MAC_TX_ERR_FATAL, 0);
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
2016-11-02 13:12:07 +00:00
|
|
|
return;
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
/* prepend 6top Sub-IE ID */
|
|
|
|
if(packetbuf_hdralloc(1) != 1) {
|
2017-07-11 15:26:40 +00:00
|
|
|
LOG_ERR("6top: sixtop_output() fails because of no room for Sub-IE ID\n");
|
2016-11-02 13:12:07 +00:00
|
|
|
return;
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
2016-11-02 13:12:07 +00:00
|
|
|
p = packetbuf_hdrptr();
|
2017-07-10 02:36:24 +00:00
|
|
|
p[0] = SIXTOP_SUBIE_ID;
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
/*
|
|
|
|
* prepend Payload IE header; 2 octets
|
|
|
|
* only sixtop_ie_content_len matters in frame80215e_create_ie_ietf().
|
|
|
|
*/
|
|
|
|
memset(&ies, 0, sizeof(ies));
|
|
|
|
ies.sixtop_ie_content_len = packetbuf_totlen();
|
|
|
|
if(packetbuf_hdralloc(2) != 1 ||
|
|
|
|
(len = frame80215e_create_ie_ietf(packetbuf_hdrptr(),
|
|
|
|
2,
|
|
|
|
&ies)) < 0) {
|
2017-07-11 15:26:40 +00:00
|
|
|
LOG_ERR("6top: sixtop_output() fails because of Payload IE Header\n");
|
2016-11-02 13:12:07 +00:00
|
|
|
if(callback != NULL) {
|
|
|
|
callback(arg, MAC_TX_ERR_FATAL, 0);
|
|
|
|
}
|
|
|
|
return;
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
#if SIXP_WITH_PAYLOAD_TERMINATION_IE
|
|
|
|
/* append Payload Termination IE to the data field; 2 octets */
|
|
|
|
memset(&ies, 0, sizeof(ies));
|
|
|
|
if((len = frame80215e_create_ie_payload_list_termination(
|
2017-07-11 15:26:40 +00:00
|
|
|
(uint8_t *)packetbuf_dataptr() + packetbuf_datalen(),
|
|
|
|
PACKETBUF_SIZE - packetbuf_totlen(),
|
|
|
|
&ies)) < 0) {
|
|
|
|
LOG_ERR("6top: sixtop_output() fails because of Payload Termination IE\n");
|
2016-11-02 13:12:07 +00:00
|
|
|
callback(arg, MAC_TX_ERR_FATAL, 0);
|
|
|
|
return;
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
2016-11-02 13:12:07 +00:00
|
|
|
packetbuf_set_datalen(packetbuf_datalen() + len);
|
|
|
|
#endif /* SIXP_WITH_PAYLOAD_TERMINATION_IE */
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
/* prepend Termination 1 IE to the header field; 2 octets */
|
|
|
|
memset(&ies, 0, sizeof(ies));
|
|
|
|
if(packetbuf_hdralloc(2) &&
|
|
|
|
frame80215e_create_ie_header_list_termination_1(packetbuf_hdrptr(),
|
|
|
|
2,
|
|
|
|
&ies) < 0) {
|
2017-07-11 15:26:40 +00:00
|
|
|
LOG_ERR("6top: sixtop_output() fails because of Header Termination 1 IE\n");
|
2016-11-02 13:12:07 +00:00
|
|
|
callback(arg, MAC_TX_ERR_FATAL, 0);
|
|
|
|
return;
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
/* specify with PACKETBUF_ATTR_METADATA that packetbuf has IEs */
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAC_METADATA, 1);
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
/* 6P packet is data frame */
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, FRAME802154_DATAFRAME);
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, dest_addr);
|
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &linkaddr_node_addr);
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
NETSTACK_MAC.send(callback, arg);
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2016-11-02 13:12:07 +00:00
|
|
|
sixtop_input(void)
|
2016-10-21 10:53:08 +00:00
|
|
|
{
|
2016-11-02 13:12:07 +00:00
|
|
|
uint8_t *hdr_ptr, *payload_ptr;
|
|
|
|
uint16_t hdr_len, payload_len;
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
frame802154_t frame;
|
|
|
|
struct ieee802154_ies ies;
|
|
|
|
linkaddr_t src_addr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A received *DATA* frame is supposed to be stored in packetbuf by
|
|
|
|
* framer_802154.parse(). packetbuf_dataptr() points at the starting address
|
|
|
|
* of the IE field or Frame Payload field if it's available. FCS should not be
|
|
|
|
* in packetbuf, which is expected to be stripped at a radio.
|
|
|
|
*/
|
|
|
|
|
|
|
|
payload_ptr = packetbuf_dataptr();
|
|
|
|
payload_len = packetbuf_datalen();
|
|
|
|
hdr_len = packetbuf_hdrlen();
|
|
|
|
hdr_ptr = payload_ptr - hdr_len;
|
|
|
|
|
|
|
|
memcpy(&src_addr, packetbuf_addr(PACKETBUF_ADDR_SENDER), sizeof(src_addr));
|
|
|
|
|
|
|
|
if(frame802154_parse(hdr_ptr, hdr_len, &frame) == 0) {
|
|
|
|
/* parse error; should not occur, anyway */
|
2017-07-11 15:26:40 +00:00
|
|
|
LOG_ERR("6top: frame802154_parse error\n");
|
2016-11-02 13:12:07 +00:00
|
|
|
return;
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
/*
|
|
|
|
* We don't need to check the frame version nor frame type. The frame version
|
|
|
|
* is turned out to be 0b10 automatically if the frame has a IE list. The
|
|
|
|
* frame type is supposed to be DATA as mentioned above.
|
|
|
|
*/
|
|
|
|
assert(frame.fcf.frame_version == FRAME802154_IEEE802154_2015);
|
|
|
|
assert(frame.fcf.frame_type == FRAME802154_DATAFRAME);
|
2017-07-10 02:36:24 +00:00
|
|
|
memset(&ies, 0, sizeof(ies));
|
2016-11-02 13:12:07 +00:00
|
|
|
if(frame.fcf.ie_list_present &&
|
|
|
|
frame802154e_parse_information_elements(payload_ptr,
|
|
|
|
payload_len, &ies) >= 0 &&
|
|
|
|
ies.sixtop_ie_content_ptr != NULL &&
|
|
|
|
ies.sixtop_ie_content_len > 0) {
|
|
|
|
|
|
|
|
sixp_input(ies.sixtop_ie_content_ptr, ies.sixtop_ie_content_len,
|
|
|
|
&src_addr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* move payloadbuf_dataptr() to the beginning of the next layer for further
|
|
|
|
* processing
|
|
|
|
*/
|
|
|
|
packetbuf_hdrreduce(ies.sixtop_ie_content_ptr - payload_ptr +
|
|
|
|
ies.sixtop_ie_content_len);
|
|
|
|
strip_payload_termination_ie();
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2016-11-02 13:12:07 +00:00
|
|
|
sixtop_init(void)
|
2016-10-21 10:53:08 +00:00
|
|
|
{
|
2016-11-02 13:12:07 +00:00
|
|
|
int i;
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
sixp_init();
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
for(i = 0; i < SIXTOP_MAX_SCHEDULING_FUNCTIONS; i++) {
|
|
|
|
scheduling_functions[i] = NULL;
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
sixtop_init_sf();
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-11-02 13:12:07 +00:00
|
|
|
void
|
|
|
|
sixtop_init_sf(void)
|
2016-10-21 10:53:08 +00:00
|
|
|
{
|
2016-11-02 13:12:07 +00:00
|
|
|
int i;
|
2016-10-21 10:53:08 +00:00
|
|
|
|
2016-11-02 13:12:07 +00:00
|
|
|
for(i = 0; i < SIXTOP_MAX_SCHEDULING_FUNCTIONS; i++) {
|
|
|
|
if(scheduling_functions[i] != NULL &&
|
|
|
|
scheduling_functions[i]->init != NULL) {
|
|
|
|
scheduling_functions[i]->init();
|
2016-10-21 10:53:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-11-02 13:12:07 +00:00
|
|
|
/** @} */
|