2010-01-25 11:46:44 +00:00
|
|
|
/*
|
A work-in-progress rework of the Contiki MAC and radio layers. The
main ideas are:
* Separates the Contiki low-layer network stack into four layers:
network (e.g. sicslowpan / rime), Medium Access Control MAC
(e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
that specify what mechanism/protocol/driver to use at the four
layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
the fate of a transmitted packet: if the packet was not possible to
transmit, the cause of the failure is reported, and if the packets
was successfully transmitted, the number of tries before it was
finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
efficient radio duty cycling protocols: channel check, pending
packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
#defines.
2010-02-18 21:48:39 +00:00
|
|
|
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
2010-01-25 11:46:44 +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.
|
|
|
|
* 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
|
2017-05-17 19:47:48 +00:00
|
|
|
* The 802.15.4 standard CSMA protocol (nonbeacon-enabled)
|
2010-01-25 11:46:44 +00:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
2017-06-10 12:35:39 +00:00
|
|
|
* Simon Duquennoy <simon.duquennoy@inria.fr>
|
2010-01-25 11:46:44 +00:00
|
|
|
*/
|
|
|
|
|
2017-05-17 20:00:16 +00:00
|
|
|
#include "net/mac/csma/csma.h"
|
|
|
|
#include "net/mac/csma/csma-output.h"
|
2017-05-17 19:47:48 +00:00
|
|
|
#include "net/mac/mac-sequence.h"
|
2010-06-14 19:19:16 +00:00
|
|
|
#include "net/packetbuf.h"
|
A work-in-progress rework of the Contiki MAC and radio layers. The
main ideas are:
* Separates the Contiki low-layer network stack into four layers:
network (e.g. sicslowpan / rime), Medium Access Control MAC
(e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
that specify what mechanism/protocol/driver to use at the four
layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
the fate of a transmitted packet: if the packet was not possible to
transmit, the cause of the failure is reported, and if the packets
was successfully transmitted, the number of tries before it was
finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
efficient radio duty cycling protocols: channel check, pending
packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
#defines.
2010-02-18 21:48:39 +00:00
|
|
|
#include "net/netstack.h"
|
|
|
|
|
2017-06-20 16:06:30 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "sys/log.h"
|
2017-06-21 14:16:49 +00:00
|
|
|
#define LOG_MODULE "CSMA"
|
2017-07-15 13:19:09 +00:00
|
|
|
#define LOG_LEVEL LOG_LEVEL_MAC
|
A work-in-progress rework of the Contiki MAC and radio layers. The
main ideas are:
* Separates the Contiki low-layer network stack into four layers:
network (e.g. sicslowpan / rime), Medium Access Control MAC
(e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
that specify what mechanism/protocol/driver to use at the four
layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
the fate of a transmitted packet: if the packet was not possible to
transmit, the cause of the failure is reported, and if the packets
was successfully transmitted, the number of tries before it was
finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
efficient radio duty cycling protocols: channel check, pending
packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
#defines.
2010-02-18 21:48:39 +00:00
|
|
|
|
2016-04-22 21:27:43 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2017-05-17 19:47:48 +00:00
|
|
|
send_packet(mac_callback_t sent, void *ptr)
|
2016-04-01 16:41:02 +00:00
|
|
|
{
|
2017-05-17 19:47:48 +00:00
|
|
|
csma_output_packet(sent, ptr);
|
2016-04-01 16:41:02 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2017-05-17 19:47:48 +00:00
|
|
|
input_packet(void)
|
2016-04-01 16:41:02 +00:00
|
|
|
{
|
2017-07-04 09:55:47 +00:00
|
|
|
#if CSMA_SEND_SOFT_ACK
|
2017-05-17 19:47:48 +00:00
|
|
|
int original_datalen;
|
|
|
|
uint8_t *original_dataptr;
|
2016-04-01 16:41:02 +00:00
|
|
|
|
2017-05-17 19:47:48 +00:00
|
|
|
original_datalen = packetbuf_datalen();
|
|
|
|
original_dataptr = packetbuf_dataptr();
|
2017-07-04 09:55:47 +00:00
|
|
|
#endif
|
2016-04-01 16:41:02 +00:00
|
|
|
|
2017-05-17 19:47:48 +00:00
|
|
|
if(packetbuf_datalen() == CSMA_ACK_LEN) {
|
|
|
|
/* Ignore ack packets */
|
2017-06-23 19:12:36 +00:00
|
|
|
LOG_DBG("ignored ack\n");
|
2017-07-04 09:55:47 +00:00
|
|
|
} else if(NETSTACK_FRAMER.parse() < 0) {
|
2017-06-21 14:16:18 +00:00
|
|
|
LOG_ERR("failed to parse %u\n", packetbuf_datalen());
|
2017-05-17 19:47:48 +00:00
|
|
|
} else if(!linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
|
|
|
&linkaddr_node_addr) &&
|
|
|
|
!packetbuf_holds_broadcast()) {
|
2017-06-21 14:16:18 +00:00
|
|
|
LOG_WARN("not for us\n");
|
2016-04-01 16:41:02 +00:00
|
|
|
} else {
|
2017-05-17 19:47:48 +00:00
|
|
|
int duplicate = 0;
|
|
|
|
|
|
|
|
/* Check for duplicate packet. */
|
|
|
|
duplicate = mac_sequence_is_duplicate();
|
|
|
|
if(duplicate) {
|
|
|
|
/* Drop the packet. */
|
2017-06-23 19:12:36 +00:00
|
|
|
LOG_WARN("drop duplicate link layer packet from ");
|
|
|
|
LOG_WARN_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
|
|
|
LOG_WARN_(", seqno %u\n", packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO));
|
2017-05-17 19:47:48 +00:00
|
|
|
} else {
|
|
|
|
mac_sequence_register_seqno();
|
2011-09-27 14:05:30 +00:00
|
|
|
}
|
2017-05-17 19:47:48 +00:00
|
|
|
|
2017-07-04 09:55:47 +00:00
|
|
|
#if CSMA_SEND_SOFT_ACK
|
2017-05-17 19:47:48 +00:00
|
|
|
{
|
|
|
|
frame802154_t info154;
|
|
|
|
frame802154_parse(original_dataptr, original_datalen, &info154);
|
|
|
|
if(info154.fcf.frame_type == FRAME802154_DATAFRAME &&
|
|
|
|
info154.fcf.ack_required != 0 &&
|
|
|
|
linkaddr_cmp((linkaddr_t *)&info154.dest_addr,
|
|
|
|
&linkaddr_node_addr)) {
|
|
|
|
uint8_t ackdata[CSMA_ACK_LEN] = {0, 0, 0};
|
|
|
|
|
|
|
|
ackdata[0] = FRAME802154_ACKFRAME;
|
|
|
|
ackdata[1] = 0;
|
|
|
|
ackdata[2] = info154.seq;
|
|
|
|
NETSTACK_RADIO.send(ackdata, CSMA_ACK_LEN);
|
2013-11-20 12:40:56 +00:00
|
|
|
}
|
A work-in-progress rework of the Contiki MAC and radio layers. The
main ideas are:
* Separates the Contiki low-layer network stack into four layers:
network (e.g. sicslowpan / rime), Medium Access Control MAC
(e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
that specify what mechanism/protocol/driver to use at the four
layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
the fate of a transmitted packet: if the packet was not possible to
transmit, the cause of the failure is reported, and if the packets
was successfully transmitted, the number of tries before it was
finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
efficient radio duty cycling protocols: channel check, pending
packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
#defines.
2010-02-18 21:48:39 +00:00
|
|
|
}
|
2017-07-04 09:55:47 +00:00
|
|
|
#endif /* CSMA_SEND_SOFT_ACK */
|
2017-05-17 19:47:48 +00:00
|
|
|
if(!duplicate) {
|
2018-04-05 12:34:50 +00:00
|
|
|
LOG_INFO("received packet from ");
|
|
|
|
LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
|
|
|
LOG_INFO_(", seqno %u, len %u\n", packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO), packetbuf_datalen());
|
2017-05-17 19:47:48 +00:00
|
|
|
NETSTACK_NETWORK.input();
|
|
|
|
}
|
2010-01-25 11:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
on(void)
|
|
|
|
{
|
2017-05-17 13:09:19 +00:00
|
|
|
return NETSTACK_RADIO.on();
|
2010-01-25 11:46:44 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2017-05-17 13:09:19 +00:00
|
|
|
off(void)
|
2010-01-25 11:46:44 +00:00
|
|
|
{
|
2017-05-17 13:09:19 +00:00
|
|
|
return NETSTACK_RADIO.off();
|
2010-01-25 11:46:44 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
A work-in-progress rework of the Contiki MAC and radio layers. The
main ideas are:
* Separates the Contiki low-layer network stack into four layers:
network (e.g. sicslowpan / rime), Medium Access Control MAC
(e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
that specify what mechanism/protocol/driver to use at the four
layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
the fate of a transmitted packet: if the packet was not possible to
transmit, the cause of the failure is reported, and if the packets
was successfully transmitted, the number of tries before it was
finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
efficient radio duty cycling protocols: channel check, pending
packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
#defines.
2010-02-18 21:48:39 +00:00
|
|
|
static void
|
|
|
|
init(void)
|
|
|
|
{
|
2017-05-17 19:47:48 +00:00
|
|
|
csma_output_init();
|
2017-05-17 13:09:19 +00:00
|
|
|
on();
|
A work-in-progress rework of the Contiki MAC and radio layers. The
main ideas are:
* Separates the Contiki low-layer network stack into four layers:
network (e.g. sicslowpan / rime), Medium Access Control MAC
(e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
that specify what mechanism/protocol/driver to use at the four
layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
the fate of a transmitted packet: if the packet was not possible to
transmit, the cause of the failure is reported, and if the packets
was successfully transmitted, the number of tries before it was
finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
efficient radio duty cycling protocols: channel check, pending
packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
#defines.
2010-02-18 21:48:39 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-01-25 11:46:44 +00:00
|
|
|
const struct mac_driver csma_driver = {
|
2010-02-23 20:42:45 +00:00
|
|
|
"CSMA",
|
A work-in-progress rework of the Contiki MAC and radio layers. The
main ideas are:
* Separates the Contiki low-layer network stack into four layers:
network (e.g. sicslowpan / rime), Medium Access Control MAC
(e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
that specify what mechanism/protocol/driver to use at the four
layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
the fate of a transmitted packet: if the packet was not possible to
transmit, the cause of the failure is reported, and if the packets
was successfully transmitted, the number of tries before it was
finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
efficient radio duty cycling protocols: channel check, pending
packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
#defines.
2010-02-18 21:48:39 +00:00
|
|
|
init,
|
2010-01-25 11:46:44 +00:00
|
|
|
send_packet,
|
A work-in-progress rework of the Contiki MAC and radio layers. The
main ideas are:
* Separates the Contiki low-layer network stack into four layers:
network (e.g. sicslowpan / rime), Medium Access Control MAC
(e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
that specify what mechanism/protocol/driver to use at the four
layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
the fate of a transmitted packet: if the packet was not possible to
transmit, the cause of the failure is reported, and if the packets
was successfully transmitted, the number of tries before it was
finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
efficient radio duty cycling protocols: channel check, pending
packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
#defines.
2010-02-18 21:48:39 +00:00
|
|
|
input_packet,
|
2010-01-25 11:46:44 +00:00
|
|
|
on,
|
2017-05-17 12:34:29 +00:00
|
|
|
off
|
2010-01-25 11:46:44 +00:00
|
|
|
};
|
|
|
|
/*---------------------------------------------------------------------------*/
|