2010-02-18 22:26:15 +01: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 22:48:39 +01:00
|
|
|
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
2010-02-18 22:26:15 +01: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
|
2011-09-02 13:37:46 +02:00
|
|
|
* Implementation of the ContikiMAC power-saving radio duty cycling protocol
|
2010-02-18 22:26:15 +01:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
* Niclas Finne <nfi@sics.se>
|
|
|
|
* Joakim Eriksson <joakime@sics.se>
|
|
|
|
*/
|
|
|
|
|
2011-09-02 14:06:27 +02:00
|
|
|
#include "contiki-conf.h"
|
2010-02-18 22:26:15 +01:00
|
|
|
#include "dev/leds.h"
|
|
|
|
#include "dev/radio.h"
|
|
|
|
#include "dev/watchdog.h"
|
|
|
|
#include "lib/random.h"
|
2013-11-15 19:21:41 +01:00
|
|
|
#include "net/mac/mac-sequence.h"
|
2013-11-26 17:23:10 +01:00
|
|
|
#include "net/mac/contikimac/contikimac.h"
|
2011-09-02 14:06:27 +02:00
|
|
|
#include "net/netstack.h"
|
2013-12-12 20:50:07 +01:00
|
|
|
#include "net/rime/rime.h"
|
2010-02-18 22:26:15 +01:00
|
|
|
#include "sys/compower.h"
|
|
|
|
#include "sys/pt.h"
|
|
|
|
#include "sys/rtimer.h"
|
|
|
|
|
2011-09-02 13:37:46 +02:00
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2011-11-14 18:02:47 +01:00
|
|
|
/* TX/RX cycles are synchronized with neighbor wake periods */
|
2013-03-17 23:34:05 +01:00
|
|
|
#ifdef CONTIKIMAC_CONF_WITH_PHASE_OPTIMIZATION
|
|
|
|
#define WITH_PHASE_OPTIMIZATION CONTIKIMAC_CONF_WITH_PHASE_OPTIMIZATION
|
|
|
|
#else /* CONTIKIMAC_CONF_WITH_PHASE_OPTIMIZATION */
|
2010-02-18 22:26:15 +01:00
|
|
|
#define WITH_PHASE_OPTIMIZATION 1
|
2013-03-17 23:34:05 +01:00
|
|
|
#endif /* CONTIKIMAC_CONF_WITH_PHASE_OPTIMIZATION */
|
2011-11-14 18:02:47 +01:00
|
|
|
/* More aggressive radio sleeping when channel is busy with other traffic */
|
2010-12-16 23:43:07 +01:00
|
|
|
#ifndef WITH_FAST_SLEEP
|
|
|
|
#define WITH_FAST_SLEEP 1
|
|
|
|
#endif
|
2011-11-14 18:02:47 +01:00
|
|
|
/* Radio does CSMA and autobackoff */
|
|
|
|
#ifndef RDC_CONF_HARDWARE_CSMA
|
|
|
|
#define RDC_CONF_HARDWARE_CSMA 0
|
|
|
|
#endif
|
|
|
|
/* Radio returns TX_OK/TX_NOACK after autoack wait */
|
|
|
|
#ifndef RDC_CONF_HARDWARE_ACK
|
|
|
|
#define RDC_CONF_HARDWARE_ACK 0
|
|
|
|
#endif
|
|
|
|
/* MCU can sleep during radio off */
|
|
|
|
#ifndef RDC_CONF_MCU_SLEEP
|
|
|
|
#define RDC_CONF_MCU_SLEEP 0
|
|
|
|
#endif
|
2010-02-18 22:26:15 +01:00
|
|
|
|
2011-01-18 17:03:19 +01:00
|
|
|
#if NETSTACK_RDC_CHANNEL_CHECK_RATE >= 64
|
|
|
|
#undef WITH_PHASE_OPTIMIZATION
|
|
|
|
#define WITH_PHASE_OPTIMIZATION 0
|
|
|
|
#endif
|
|
|
|
|
2012-01-10 18:48:39 +01:00
|
|
|
/* CYCLE_TIME for channel cca checks, in rtimer ticks. */
|
2010-02-18 22:26:15 +01:00
|
|
|
#ifdef CONTIKIMAC_CONF_CYCLE_TIME
|
|
|
|
#define CYCLE_TIME (CONTIKIMAC_CONF_CYCLE_TIME)
|
|
|
|
#else
|
2010-10-03 22:39:24 +02:00
|
|
|
#define CYCLE_TIME (RTIMER_ARCH_SECOND / NETSTACK_RDC_CHANNEL_CHECK_RATE)
|
2010-02-18 22:26:15 +01:00
|
|
|
#endif
|
|
|
|
|
2012-01-10 18:48:39 +01:00
|
|
|
/* CHANNEL_CHECK_RATE is enforced to be a power of two.
|
|
|
|
* If RTIMER_ARCH_SECOND is not also a power of two, there will be an inexact
|
|
|
|
* number of channel checks per second due to the truncation of CYCLE_TIME.
|
|
|
|
* This will degrade the effectiveness of phase optimization with neighbors that
|
|
|
|
* do not have the same truncation error.
|
|
|
|
* Define SYNC_CYCLE_STARTS to ensure an integral number of checks per second.
|
|
|
|
*/
|
|
|
|
#if RTIMER_ARCH_SECOND & (RTIMER_ARCH_SECOND - 1)
|
|
|
|
#define SYNC_CYCLE_STARTS 1
|
|
|
|
#endif
|
|
|
|
|
2011-09-27 16:05:30 +02:00
|
|
|
/* Are we currently receiving a burst? */
|
|
|
|
static int we_are_receiving_burst = 0;
|
|
|
|
|
2013-03-17 23:34:05 +01:00
|
|
|
/* INTER_PACKET_DEADLINE is the maximum time a receiver waits for the
|
2011-09-27 16:05:30 +02:00
|
|
|
next packet of a burst when FRAME_PENDING is set. */
|
2015-11-23 13:57:11 +01:00
|
|
|
#ifdef CONTIKIMAC_CONF_INTER_PACKET_DEADLINE
|
2015-11-24 10:21:23 +01:00
|
|
|
#define INTER_PACKET_DEADLINE CONTIKIMAC_CONF_INTER_PACKET_DEADLINE
|
2015-11-23 13:57:11 +01:00
|
|
|
#else
|
2011-09-27 16:05:30 +02:00
|
|
|
#define INTER_PACKET_DEADLINE CLOCK_SECOND / 32
|
2015-11-23 13:57:11 +01:00
|
|
|
#endif
|
2010-03-14 23:59:23 +01:00
|
|
|
|
2011-01-09 22:07:01 +01:00
|
|
|
/* ContikiMAC performs periodic channel checks. Each channel check
|
|
|
|
consists of two or more CCA checks. CCA_COUNT_MAX is the number of
|
|
|
|
CCAs to be done for each periodic channel check. The default is
|
|
|
|
two.*/
|
2013-03-17 23:34:05 +01:00
|
|
|
#ifdef CONTIKIMAC_CONF_CCA_COUNT_MAX
|
|
|
|
#define CCA_COUNT_MAX (CONTIKIMAC_CONF_CCA_COUNT_MAX)
|
|
|
|
#else
|
2010-04-30 09:25:51 +02:00
|
|
|
#define CCA_COUNT_MAX 2
|
2013-03-17 23:34:05 +01:00
|
|
|
#endif
|
2011-01-09 22:07:01 +01:00
|
|
|
|
2011-09-27 16:05:30 +02:00
|
|
|
/* Before starting a transmission, Contikimac checks the availability
|
|
|
|
of the channel with CCA_COUNT_MAX_TX consecutive CCAs */
|
2013-03-17 23:34:05 +01:00
|
|
|
#ifdef CONTIKIMAC_CONF_CCA_COUNT_MAX_TX
|
|
|
|
#define CCA_COUNT_MAX_TX (CONTIKIMAC_CONF_CCA_COUNT_MAX_TX)
|
|
|
|
#else
|
2011-09-27 16:05:30 +02:00
|
|
|
#define CCA_COUNT_MAX_TX 6
|
2013-03-17 23:34:05 +01:00
|
|
|
#endif
|
2011-09-27 16:05:30 +02:00
|
|
|
|
2011-01-09 22:07:01 +01:00
|
|
|
/* CCA_CHECK_TIME is the time it takes to perform a CCA check. */
|
2011-11-14 18:02:47 +01:00
|
|
|
/* Note this may be zero. AVRs have 7612 ticks/sec, but block until cca is done */
|
2013-03-17 23:34:05 +01:00
|
|
|
#ifdef CONTIKIMAC_CONF_CCA_CHECK_TIME
|
|
|
|
#define CCA_CHECK_TIME (CONTIKIMAC_CONF_CCA_CHECK_TIME)
|
|
|
|
#else
|
2010-04-30 09:25:51 +02:00
|
|
|
#define CCA_CHECK_TIME RTIMER_ARCH_SECOND / 8192
|
2013-03-17 23:34:05 +01:00
|
|
|
#endif
|
2011-01-09 22:07:01 +01:00
|
|
|
|
|
|
|
/* CCA_SLEEP_TIME is the time between two successive CCA checks. */
|
2011-11-14 18:02:47 +01:00
|
|
|
/* Add 1 when rtimer ticks are coarse */
|
2015-07-09 03:51:38 +02:00
|
|
|
#ifdef CONTIKIMAC_CONF_CCA_SLEEP_TIME
|
|
|
|
#define CCA_SLEEP_TIME CONTIKIMAC_CONF_CCA_SLEEP_TIME
|
|
|
|
#else
|
2011-11-14 18:02:47 +01:00
|
|
|
#if RTIMER_ARCH_SECOND > 8000
|
2010-04-30 09:25:51 +02:00
|
|
|
#define CCA_SLEEP_TIME RTIMER_ARCH_SECOND / 2000
|
2011-11-14 18:02:47 +01:00
|
|
|
#else
|
|
|
|
#define CCA_SLEEP_TIME (RTIMER_ARCH_SECOND / 2000) + 1
|
2015-07-09 03:51:38 +02:00
|
|
|
#endif /* RTIMER_ARCH_SECOND > 8000 */
|
|
|
|
#endif /* CONTIKIMAC_CONF_CCA_SLEEP_TIME */
|
2011-01-09 22:07:01 +01:00
|
|
|
|
|
|
|
/* CHECK_TIME is the total time it takes to perform CCA_COUNT_MAX
|
|
|
|
CCAs. */
|
2010-03-14 23:59:23 +01:00
|
|
|
#define CHECK_TIME (CCA_COUNT_MAX * (CCA_CHECK_TIME + CCA_SLEEP_TIME))
|
|
|
|
|
2011-09-27 16:05:30 +02:00
|
|
|
/* CHECK_TIME_TX is the total time it takes to perform CCA_COUNT_MAX_TX
|
|
|
|
CCAs. */
|
|
|
|
#define CHECK_TIME_TX (CCA_COUNT_MAX_TX * (CCA_CHECK_TIME + CCA_SLEEP_TIME))
|
|
|
|
|
2011-01-09 22:07:01 +01:00
|
|
|
/* LISTEN_TIME_AFTER_PACKET_DETECTED is the time that we keep checking
|
|
|
|
for activity after a potential packet has been detected by a CCA
|
|
|
|
check. */
|
2015-07-09 03:51:38 +02:00
|
|
|
#ifdef CONTIKIMAC_CONF_LISTEN_TIME_AFTER_PACKET_DETECTED
|
|
|
|
#define LISTEN_TIME_AFTER_PACKET_DETECTED CONTIKIMAC_CONF_LISTEN_TIME_AFTER_PACKET_DETECTED
|
|
|
|
#else
|
2011-01-09 22:07:01 +01:00
|
|
|
#define LISTEN_TIME_AFTER_PACKET_DETECTED RTIMER_ARCH_SECOND / 80
|
2015-07-09 03:51:38 +02:00
|
|
|
#endif
|
2011-01-09 22:07:01 +01:00
|
|
|
|
|
|
|
/* MAX_SILENCE_PERIODS is the maximum amount of periods (a period is
|
|
|
|
CCA_CHECK_TIME + CCA_SLEEP_TIME) that we allow to be silent before
|
|
|
|
we turn of the radio. */
|
2015-11-23 13:57:11 +01:00
|
|
|
#ifdef CONTIKIMAC_CONF_MAX_SILENCE_PERIODS
|
|
|
|
#define MAX_SILENCE_PERIODS CONTIKIMAC_CONF_MAX_SILENCE_PERIODS
|
|
|
|
#else
|
2011-01-09 22:07:01 +01:00
|
|
|
#define MAX_SILENCE_PERIODS 5
|
2015-11-23 13:57:11 +01:00
|
|
|
#endif
|
2011-01-09 22:07:01 +01:00
|
|
|
|
|
|
|
/* MAX_NONACTIVITY_PERIODS is the maximum number of periods we allow
|
|
|
|
the radio to be turned on without any packet being received, when
|
|
|
|
WITH_FAST_SLEEP is enabled. */
|
2015-11-23 13:57:11 +01:00
|
|
|
#ifdef CONTIKIMAC_CONF_MAX_NONACTIVITY_PERIODS
|
|
|
|
#define MAX_NONACTIVITY_PERIODS CONTIKIMAC_CONF_MAX_NONACTIVITY_PERIODS
|
|
|
|
#else
|
2011-01-09 22:07:01 +01:00
|
|
|
#define MAX_NONACTIVITY_PERIODS 10
|
2015-11-23 13:57:11 +01:00
|
|
|
#endif
|
|
|
|
|
2010-03-31 22:27:15 +02:00
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
|
2011-01-09 22:07:01 +01:00
|
|
|
|
|
|
|
/* STROBE_TIME is the maximum amount of time a transmitted packet
|
|
|
|
should be repeatedly transmitted as part of a transmission. */
|
|
|
|
#define STROBE_TIME (CYCLE_TIME + 2 * CHECK_TIME)
|
|
|
|
|
|
|
|
/* GUARD_TIME is the time before the expected phase of a neighbor that
|
|
|
|
a transmitted should begin transmitting packets. */
|
2015-11-23 13:57:11 +01:00
|
|
|
#ifdef CONTIKIMAC_CONF_GUARD_TIME
|
|
|
|
#define GUARD_TIME CONTIKIMAC_CONF_GUARD_TIME
|
|
|
|
#else
|
2011-09-27 16:05:30 +02:00
|
|
|
#define GUARD_TIME 10 * CHECK_TIME + CHECK_TIME_TX
|
2015-11-23 13:57:11 +01:00
|
|
|
#endif
|
2010-03-29 23:51:36 +02:00
|
|
|
|
2011-01-09 22:07:01 +01:00
|
|
|
/* INTER_PACKET_INTERVAL is the interval between two successive packet transmissions */
|
2013-03-17 23:34:05 +01:00
|
|
|
#ifdef CONTIKIMAC_CONF_INTER_PACKET_INTERVAL
|
|
|
|
#define INTER_PACKET_INTERVAL CONTIKIMAC_CONF_INTER_PACKET_INTERVAL
|
|
|
|
#else
|
2013-05-15 19:17:51 +02:00
|
|
|
#define INTER_PACKET_INTERVAL RTIMER_ARCH_SECOND / 2500
|
2013-03-17 23:34:05 +01:00
|
|
|
#endif
|
2011-01-09 22:07:01 +01:00
|
|
|
|
|
|
|
/* AFTER_ACK_DETECTECT_WAIT_TIME is the time to wait after a potential
|
|
|
|
ACK packet has been detected until we can read it out from the
|
|
|
|
radio. */
|
2013-03-17 23:34:05 +01:00
|
|
|
#ifdef CONTIKIMAC_CONF_AFTER_ACK_DETECTECT_WAIT_TIME
|
|
|
|
#define AFTER_ACK_DETECTECT_WAIT_TIME CONTIKIMAC_CONF_AFTER_ACK_DETECTECT_WAIT_TIME
|
|
|
|
#else
|
2010-03-09 14:19:05 +01:00
|
|
|
#define AFTER_ACK_DETECTECT_WAIT_TIME RTIMER_ARCH_SECOND / 1500
|
2013-03-17 23:34:05 +01:00
|
|
|
#endif
|
2010-02-18 22:26:15 +01:00
|
|
|
|
2011-01-09 22:07:01 +01:00
|
|
|
/* MAX_PHASE_STROBE_TIME is the time that we transmit repeated packets
|
|
|
|
to a neighbor for which we have a phase lock. */
|
2015-11-23 13:57:11 +01:00
|
|
|
#ifdef CONTIKIMAC_CONF_MAX_PHASE_STROBE_TIME
|
|
|
|
#define MAX_PHASE_STROBE_TIME CONTIKIMAC_CONF_MAX_PHASE_STROBE_TIME
|
|
|
|
#else
|
2011-01-09 22:07:01 +01:00
|
|
|
#define MAX_PHASE_STROBE_TIME RTIMER_ARCH_SECOND / 60
|
2015-11-23 13:57:11 +01:00
|
|
|
#endif
|
2011-01-09 22:07:01 +01:00
|
|
|
|
2015-07-09 02:46:58 +02:00
|
|
|
#ifdef CONTIKIMAC_CONF_SEND_SW_ACK
|
|
|
|
#define CONTIKIMAC_SEND_SW_ACK CONTIKIMAC_CONF_SEND_SW_ACK
|
|
|
|
#else
|
|
|
|
#define CONTIKIMAC_SEND_SW_ACK 0
|
|
|
|
#endif
|
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
#define ACK_LEN 3
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
static struct rtimer rt;
|
|
|
|
static struct pt pt;
|
|
|
|
|
|
|
|
static volatile uint8_t contikimac_is_on = 0;
|
2010-12-06 10:08:22 +01:00
|
|
|
static volatile uint8_t contikimac_keep_radio_on = 0;
|
2010-02-18 22:26:15 +01:00
|
|
|
|
|
|
|
static volatile unsigned char we_are_sending = 0;
|
|
|
|
static volatile unsigned char radio_is_on = 0;
|
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#define PRINTDEBUG(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#define PRINTDEBUG(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONTIKIMAC_CONF_COMPOWER
|
|
|
|
static struct compower_activity current_packet;
|
|
|
|
#endif /* CONTIKIMAC_CONF_COMPOWER */
|
|
|
|
|
|
|
|
#if WITH_PHASE_OPTIMIZATION
|
|
|
|
|
|
|
|
#include "net/mac/phase.h"
|
|
|
|
|
|
|
|
#endif /* WITH_PHASE_OPTIMIZATION */
|
|
|
|
|
2011-02-08 21:57:46 +01:00
|
|
|
#define DEFAULT_STREAM_TIME (4 * CYCLE_TIME)
|
2010-02-18 22:26:15 +01:00
|
|
|
|
2010-04-06 13:57:43 +02:00
|
|
|
#if CONTIKIMAC_CONF_BROADCAST_RATE_LIMIT
|
|
|
|
static struct timer broadcast_rate_timer;
|
|
|
|
static int broadcast_rate_counter;
|
|
|
|
#endif /* CONTIKIMAC_CONF_BROADCAST_RATE_LIMIT */
|
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
on(void)
|
|
|
|
{
|
|
|
|
if(contikimac_is_on && radio_is_on == 0) {
|
|
|
|
radio_is_on = 1;
|
|
|
|
NETSTACK_RADIO.on();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
off(void)
|
|
|
|
{
|
2011-09-27 16:05:30 +02:00
|
|
|
if(contikimac_is_on && radio_is_on != 0 &&
|
2011-09-02 14:06:27 +02:00
|
|
|
contikimac_keep_radio_on == 0) {
|
2010-02-18 22:26:15 +01:00
|
|
|
radio_is_on = 0;
|
|
|
|
NETSTACK_RADIO.off();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-09-01 16:34:27 +02:00
|
|
|
static void powercycle_wrapper(struct rtimer *t, void *ptr);
|
2010-02-18 22:26:15 +01:00
|
|
|
static char powercycle(struct rtimer *t, void *ptr);
|
2015-07-10 23:44:46 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static volatile rtimer_clock_t cycle_start;
|
|
|
|
#if SYNC_CYCLE_STARTS
|
|
|
|
static volatile rtimer_clock_t sync_cycle_start;
|
|
|
|
static volatile uint8_t sync_cycle_phase;
|
|
|
|
#endif
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-02-18 22:26:15 +01:00
|
|
|
static void
|
|
|
|
schedule_powercycle(struct rtimer *t, rtimer_clock_t time)
|
|
|
|
{
|
|
|
|
int r;
|
2015-08-18 15:30:20 +02:00
|
|
|
rtimer_clock_t now;
|
2010-02-18 22:26:15 +01:00
|
|
|
|
|
|
|
if(contikimac_is_on) {
|
2010-02-23 19:49:05 +01:00
|
|
|
|
2015-08-18 15:30:20 +02:00
|
|
|
time += RTIMER_TIME(t);
|
|
|
|
now = RTIMER_NOW();
|
|
|
|
if(RTIMER_CLOCK_LT(time, now + RTIMER_GUARD_TIME)) {
|
|
|
|
time = now + RTIMER_GUARD_TIME;
|
2010-02-28 09:33:21 +01:00
|
|
|
}
|
2010-04-04 14:28:29 +02:00
|
|
|
|
2015-08-18 15:30:20 +02:00
|
|
|
r = rtimer_set(t, time, 1, powercycle_wrapper, NULL);
|
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
if(r != RTIMER_OK) {
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("schedule_powercycle: could not set rtimer\n");
|
2010-02-18 22:26:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-02 14:06:27 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-02-18 22:26:15 +01:00
|
|
|
static void
|
2010-04-04 14:28:29 +02:00
|
|
|
schedule_powercycle_fixed(struct rtimer *t, rtimer_clock_t fixed_time)
|
|
|
|
{
|
|
|
|
int r;
|
2015-08-18 15:30:20 +02:00
|
|
|
rtimer_clock_t now;
|
2010-04-04 14:28:29 +02:00
|
|
|
|
|
|
|
if(contikimac_is_on) {
|
|
|
|
|
2015-08-18 15:30:20 +02:00
|
|
|
now = RTIMER_NOW();
|
|
|
|
if(RTIMER_CLOCK_LT(fixed_time, now + RTIMER_GUARD_TIME)) {
|
|
|
|
fixed_time = now + RTIMER_GUARD_TIME;
|
2010-04-04 14:28:29 +02:00
|
|
|
}
|
|
|
|
|
2015-09-01 16:34:27 +02:00
|
|
|
r = rtimer_set(t, fixed_time, 1, powercycle_wrapper, NULL);
|
2010-04-04 14:28:29 +02:00
|
|
|
if(r != RTIMER_OK) {
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("schedule_powercycle: could not set rtimer\n");
|
2010-04-04 14:28:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-02 14:06:27 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-04 14:28:29 +02:00
|
|
|
static void
|
2010-02-18 22:26:15 +01:00
|
|
|
powercycle_turn_radio_off(void)
|
|
|
|
{
|
2011-05-18 17:22:25 +02:00
|
|
|
#if CONTIKIMAC_CONF_COMPOWER
|
2011-04-13 14:10:02 +02:00
|
|
|
uint8_t was_on = radio_is_on;
|
2011-05-18 17:22:25 +02:00
|
|
|
#endif /* CONTIKIMAC_CONF_COMPOWER */
|
2015-07-10 23:44:46 +02:00
|
|
|
|
2011-09-27 16:05:30 +02:00
|
|
|
if(we_are_sending == 0 && we_are_receiving_burst == 0) {
|
2010-02-18 22:26:15 +01:00
|
|
|
off();
|
2011-04-13 14:10:02 +02:00
|
|
|
#if CONTIKIMAC_CONF_COMPOWER
|
|
|
|
if(was_on && !radio_is_on) {
|
|
|
|
compower_accumulate(&compower_idle_activity);
|
|
|
|
}
|
|
|
|
#endif /* CONTIKIMAC_CONF_COMPOWER */
|
2010-02-18 22:26:15 +01:00
|
|
|
}
|
|
|
|
}
|
2011-09-02 14:06:27 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-02-18 22:26:15 +01:00
|
|
|
static void
|
|
|
|
powercycle_turn_radio_on(void)
|
|
|
|
{
|
2011-09-27 16:05:30 +02:00
|
|
|
if(we_are_sending == 0 && we_are_receiving_burst == 0) {
|
2010-02-18 22:26:15 +01:00
|
|
|
on();
|
|
|
|
}
|
|
|
|
}
|
2011-09-02 14:06:27 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-09-15 09:52:06 +02:00
|
|
|
volatile uint8_t mcusleepcycle=16;
|
|
|
|
|
2015-09-01 16:34:27 +02:00
|
|
|
static void
|
|
|
|
powercycle_wrapper(struct rtimer *t, void *ptr)
|
|
|
|
{
|
|
|
|
powercycle(t, ptr);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-07-10 23:44:46 +02:00
|
|
|
static void
|
|
|
|
advance_cycle_start(void)
|
|
|
|
{
|
|
|
|
#if SYNC_CYCLE_STARTS
|
|
|
|
|
|
|
|
/* Compute cycle start when RTIMER_ARCH_SECOND is not a multiple
|
|
|
|
of CHANNEL_CHECK_RATE */
|
|
|
|
if(sync_cycle_phase++ == NETSTACK_RDC_CHANNEL_CHECK_RATE) {
|
|
|
|
sync_cycle_phase = 0;
|
|
|
|
sync_cycle_start += RTIMER_ARCH_SECOND;
|
|
|
|
cycle_start = sync_cycle_start;
|
|
|
|
} else if( (RTIMER_ARCH_SECOND * NETSTACK_RDC_CHANNEL_CHECK_RATE) > 65535) {
|
|
|
|
uint32_t phase_time = sync_cycle_phase*RTIMER_ARCH_SECOND;
|
|
|
|
|
|
|
|
cycle_start = sync_cycle_start + phase_time/NETSTACK_RDC_CHANNEL_CHECK_RATE;
|
|
|
|
} else {
|
|
|
|
unsigned phase_time = sync_cycle_phase*RTIMER_ARCH_SECOND;
|
|
|
|
|
|
|
|
cycle_start = sync_cycle_start + phase_time/NETSTACK_RDC_CHANNEL_CHECK_RATE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cycle_start += CYCLE_TIME;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-02-18 22:26:15 +01:00
|
|
|
static char
|
|
|
|
powercycle(struct rtimer *t, void *ptr)
|
|
|
|
{
|
2012-03-08 16:28:04 +01:00
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
PT_BEGIN(&pt);
|
|
|
|
|
2012-01-10 18:48:39 +01:00
|
|
|
#if SYNC_CYCLE_STARTS
|
|
|
|
sync_cycle_start = RTIMER_NOW();
|
|
|
|
#else
|
2010-12-10 14:36:06 +01:00
|
|
|
cycle_start = RTIMER_NOW();
|
2012-01-10 18:48:39 +01:00
|
|
|
#endif
|
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
while(1) {
|
|
|
|
static uint8_t packet_seen;
|
2010-03-16 19:11:13 +01:00
|
|
|
static uint8_t count;
|
2010-02-18 22:26:15 +01:00
|
|
|
|
|
|
|
packet_seen = 0;
|
2010-03-16 19:11:13 +01:00
|
|
|
|
2011-09-27 16:05:30 +02:00
|
|
|
for(count = 0; count < CCA_COUNT_MAX; ++count) {
|
|
|
|
if(we_are_sending == 0 && we_are_receiving_burst == 0) {
|
|
|
|
powercycle_turn_radio_on();
|
|
|
|
/* Check if a packet is seen in the air. If so, we keep the
|
2010-03-14 23:59:23 +01:00
|
|
|
radio on for a while (LISTEN_TIME_AFTER_PACKET_DETECTED) to
|
|
|
|
be able to receive the packet. We also continuously check
|
|
|
|
the radio medium to make sure that we wasn't woken up by a
|
|
|
|
false positive: a spurious radio interference that was not
|
|
|
|
caused by an incoming packet. */
|
2011-09-27 16:05:30 +02:00
|
|
|
if(NETSTACK_RADIO.channel_clear() == 0) {
|
|
|
|
packet_seen = 1;
|
|
|
|
break;
|
2010-02-18 22:26:15 +01:00
|
|
|
}
|
2011-09-27 16:05:30 +02:00
|
|
|
powercycle_turn_radio_off();
|
2010-02-18 22:26:15 +01:00
|
|
|
}
|
2011-09-27 16:05:30 +02:00
|
|
|
schedule_powercycle_fixed(t, RTIMER_NOW() + CCA_SLEEP_TIME);
|
|
|
|
PT_YIELD(&pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(packet_seen) {
|
|
|
|
static rtimer_clock_t start;
|
|
|
|
static uint8_t silence_periods, periods;
|
|
|
|
start = RTIMER_NOW();
|
|
|
|
|
|
|
|
periods = silence_periods = 0;
|
|
|
|
while(we_are_sending == 0 && radio_is_on &&
|
|
|
|
RTIMER_CLOCK_LT(RTIMER_NOW(),
|
|
|
|
(start + LISTEN_TIME_AFTER_PACKET_DETECTED))) {
|
2011-09-02 14:06:27 +02:00
|
|
|
|
2011-09-27 16:05:30 +02:00
|
|
|
/* Check for a number of consecutive periods of
|
2010-03-14 23:59:23 +01:00
|
|
|
non-activity. If we see two such periods, we turn the
|
|
|
|
radio off. Also, if a packet has been successfully
|
|
|
|
received (as indicated by the
|
|
|
|
NETSTACK_RADIO.pending_packet() function), we stop
|
2010-04-01 12:02:04 +02:00
|
|
|
snooping. */
|
2011-11-14 18:02:47 +01:00
|
|
|
#if !RDC_CONF_HARDWARE_CSMA
|
|
|
|
/* A cca cycle will disrupt rx on some radios, e.g. mc1322x, rf230 */
|
|
|
|
/*TODO: Modify those drivers to just return the internal RSSI when already in rx mode */
|
2011-09-27 16:05:30 +02:00
|
|
|
if(NETSTACK_RADIO.channel_clear()) {
|
|
|
|
++silence_periods;
|
|
|
|
} else {
|
|
|
|
silence_periods = 0;
|
2010-02-23 19:49:05 +01:00
|
|
|
}
|
2011-11-14 18:02:47 +01:00
|
|
|
#endif
|
2011-09-27 16:05:30 +02:00
|
|
|
|
|
|
|
++periods;
|
|
|
|
|
|
|
|
if(NETSTACK_RADIO.receiving_packet()) {
|
|
|
|
silence_periods = 0;
|
|
|
|
}
|
|
|
|
if(silence_periods > MAX_SILENCE_PERIODS) {
|
|
|
|
powercycle_turn_radio_off();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(WITH_FAST_SLEEP &&
|
|
|
|
periods > MAX_NONACTIVITY_PERIODS &&
|
|
|
|
!(NETSTACK_RADIO.receiving_packet() ||
|
|
|
|
NETSTACK_RADIO.pending_packet())) {
|
|
|
|
powercycle_turn_radio_off();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(NETSTACK_RADIO.pending_packet()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-10-16 22:02:54 +02:00
|
|
|
// schedule_powercycle(t, CCA_CHECK_TIME + CCA_SLEEP_TIME);
|
|
|
|
// PT_YIELD(&pt);
|
2011-09-27 16:05:30 +02:00
|
|
|
}
|
|
|
|
if(radio_is_on) {
|
|
|
|
if(!(NETSTACK_RADIO.receiving_packet() ||
|
|
|
|
NETSTACK_RADIO.pending_packet()) ||
|
2011-01-09 22:07:01 +01:00
|
|
|
!RTIMER_CLOCK_LT(RTIMER_NOW(),
|
2011-09-27 16:05:30 +02:00
|
|
|
(start + LISTEN_TIME_AFTER_PACKET_DETECTED))) {
|
|
|
|
powercycle_turn_radio_off();
|
2010-02-23 19:49:05 +01:00
|
|
|
}
|
|
|
|
}
|
2011-09-27 16:05:30 +02:00
|
|
|
}
|
2010-04-03 15:28:30 +02:00
|
|
|
|
2015-07-10 23:44:46 +02:00
|
|
|
advance_cycle_start();
|
|
|
|
|
|
|
|
if(RTIMER_CLOCK_LT(RTIMER_NOW() , cycle_start - CHECK_TIME * 4)) {
|
2013-03-17 23:29:58 +01:00
|
|
|
/* Schedule the next powercycle interrupt, or sleep the mcu
|
2015-07-10 23:44:46 +02:00
|
|
|
until then. Sleeping will not exit from this interrupt, so
|
|
|
|
ensure an occasional wake cycle or foreground processing will
|
|
|
|
be blocked until a packet is detected */
|
2011-11-14 18:02:47 +01:00
|
|
|
#if RDC_CONF_MCU_SLEEP
|
|
|
|
static uint8_t sleepcycle;
|
2016-09-23 14:59:30 +02:00
|
|
|
if((sleepcycle++ < mcusleepcycle) && !we_are_sending && !radio_is_on && !(NETSTACK_RADIO.receiving_packet() || NETSTACK_RADIO.pending_packet())) {
|
|
|
|
rtimer_arch_sleep(cycle_start - RTIMER_NOW());
|
2011-11-14 18:02:47 +01:00
|
|
|
} else {
|
|
|
|
sleepcycle = 0;
|
2014-12-09 16:42:45 +01:00
|
|
|
#ifndef RDC_CONF_PT_YIELD_OFF
|
2015-07-10 23:44:46 +02:00
|
|
|
schedule_powercycle_fixed(t, cycle_start);
|
2011-11-14 18:02:47 +01:00
|
|
|
PT_YIELD(&pt);
|
2014-12-09 16:42:45 +01:00
|
|
|
#endif
|
2011-11-14 18:02:47 +01:00
|
|
|
}
|
|
|
|
#else
|
2015-07-10 23:44:46 +02:00
|
|
|
schedule_powercycle_fixed(t, cycle_start);
|
2010-02-18 22:26:15 +01:00
|
|
|
PT_YIELD(&pt);
|
2011-11-14 18:02:47 +01:00
|
|
|
#endif
|
2010-02-18 22:26:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PT_END(&pt);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2010-04-06 13:57:43 +02:00
|
|
|
broadcast_rate_drop(void)
|
|
|
|
{
|
|
|
|
#if CONTIKIMAC_CONF_BROADCAST_RATE_LIMIT
|
|
|
|
if(!timer_expired(&broadcast_rate_timer)) {
|
|
|
|
broadcast_rate_counter++;
|
|
|
|
if(broadcast_rate_counter < CONTIKIMAC_CONF_BROADCAST_RATE_LIMIT) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
timer_set(&broadcast_rate_timer, CLOCK_SECOND);
|
|
|
|
broadcast_rate_counter = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else /* CONTIKIMAC_CONF_BROADCAST_RATE_LIMIT */
|
|
|
|
return 0;
|
|
|
|
#endif /* CONTIKIMAC_CONF_BROADCAST_RATE_LIMIT */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2013-03-17 23:29:58 +01:00
|
|
|
send_packet(mac_callback_t mac_callback, void *mac_callback_ptr,
|
|
|
|
struct rdc_buf_list *buf_list,
|
|
|
|
int is_receiver_awake)
|
2010-02-18 22:26:15 +01:00
|
|
|
{
|
|
|
|
rtimer_clock_t t0;
|
2015-06-02 01:38:11 +02:00
|
|
|
#if WITH_PHASE_OPTIMIZATION
|
2012-03-26 22:49:28 +02:00
|
|
|
rtimer_clock_t encounter_time = 0;
|
2016-04-22 12:34:16 +02:00
|
|
|
uint8_t is_known_receiver = 0;
|
2015-06-02 01:38:11 +02:00
|
|
|
#endif
|
2010-02-18 22:26:15 +01:00
|
|
|
int strobes;
|
2010-03-14 23:59:23 +01:00
|
|
|
uint8_t got_strobe_ack = 0;
|
|
|
|
uint8_t is_broadcast = 0;
|
2010-02-18 22:26:15 +01:00
|
|
|
uint8_t collisions;
|
|
|
|
int transmit_len;
|
2010-03-14 23:59:23 +01:00
|
|
|
int ret;
|
2010-12-06 10:08:22 +01:00
|
|
|
uint8_t contikimac_was_on;
|
2015-07-16 14:42:25 +02:00
|
|
|
#if !RDC_CONF_HARDWARE_ACK
|
|
|
|
int len;
|
2011-09-27 16:05:30 +02:00
|
|
|
uint8_t seqno;
|
2015-07-16 14:42:25 +02:00
|
|
|
#endif
|
2015-07-10 23:44:46 +02:00
|
|
|
|
2013-03-17 23:29:58 +01:00
|
|
|
/* Exit if RDC and radio were explicitly turned off */
|
|
|
|
if(!contikimac_is_on && !contikimac_keep_radio_on) {
|
2012-03-02 22:01:12 +01:00
|
|
|
PRINTF("contikimac: radio is turned off\n");
|
|
|
|
return MAC_TX_ERR_FATAL;
|
|
|
|
}
|
2015-07-10 23:44:46 +02:00
|
|
|
|
2010-03-09 14:19:05 +01:00
|
|
|
if(packetbuf_totlen() == 0) {
|
|
|
|
PRINTF("contikimac: send_packet data len 0\n");
|
|
|
|
return MAC_TX_ERR_FATAL;
|
|
|
|
}
|
2010-03-29 23:51:36 +02:00
|
|
|
|
2011-03-21 22:25:55 +01:00
|
|
|
#if !NETSTACK_CONF_BRIDGE_MODE
|
|
|
|
/* If NETSTACK_CONF_BRIDGE_MODE is set, assume PACKETBUF_ADDR_SENDER is already set. */
|
2013-12-12 23:58:52 +01:00
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &linkaddr_node_addr);
|
2011-03-21 22:25:55 +01:00
|
|
|
#endif
|
2014-08-05 13:49:05 +02:00
|
|
|
if(packetbuf_holds_broadcast()) {
|
2010-02-18 22:26:15 +01:00
|
|
|
is_broadcast = 1;
|
|
|
|
PRINTDEBUG("contikimac: send broadcast\n");
|
2010-04-06 13:57:43 +02:00
|
|
|
|
|
|
|
if(broadcast_rate_drop()) {
|
|
|
|
return MAC_TX_COLLISION;
|
|
|
|
}
|
2010-02-18 22:26:15 +01:00
|
|
|
} else {
|
2014-12-01 21:02:57 +01:00
|
|
|
#if NETSTACK_CONF_WITH_IPV6
|
2010-02-18 22:26:15 +01:00
|
|
|
PRINTDEBUG("contikimac: send unicast to %02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[0],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[1],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[2],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[3],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[4],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[5],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[6],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[7]);
|
2014-12-01 21:02:57 +01:00
|
|
|
#else /* NETSTACK_CONF_WITH_IPV6 */
|
2010-02-18 22:26:15 +01:00
|
|
|
PRINTDEBUG("contikimac: send unicast to %u.%u\n",
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[0],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[1]);
|
2014-12-01 21:02:57 +01:00
|
|
|
#endif /* NETSTACK_CONF_WITH_IPV6 */
|
2010-02-18 22:26:15 +01:00
|
|
|
}
|
2011-02-08 21:57:46 +01:00
|
|
|
|
2014-04-26 11:39:51 +02:00
|
|
|
if(!packetbuf_attr(PACKETBUF_ATTR_IS_CREATED_AND_SECURED)) {
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAC_ACK, 1);
|
2015-06-27 18:21:08 +02:00
|
|
|
if(NETSTACK_FRAMER.create() < 0) {
|
2014-04-26 11:39:51 +02:00
|
|
|
PRINTF("contikimac: framer failed\n");
|
|
|
|
return MAC_TX_ERR_FATAL;
|
|
|
|
}
|
2010-03-29 23:51:36 +02:00
|
|
|
}
|
2015-07-10 23:44:46 +02:00
|
|
|
|
2012-11-24 17:18:59 +01:00
|
|
|
transmit_len = packetbuf_totlen();
|
2010-04-26 19:46:21 +02:00
|
|
|
NETSTACK_RADIO.prepare(packetbuf_hdrptr(), transmit_len);
|
2015-07-10 23:44:46 +02:00
|
|
|
|
2011-09-27 16:05:30 +02:00
|
|
|
if(!is_broadcast && !is_receiver_awake) {
|
2010-03-14 23:59:23 +01:00
|
|
|
#if WITH_PHASE_OPTIMIZATION
|
2013-07-03 19:14:10 +02:00
|
|
|
ret = phase_wait(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
2010-12-16 23:43:07 +01:00
|
|
|
CYCLE_TIME, GUARD_TIME,
|
2011-10-04 10:29:40 +02:00
|
|
|
mac_callback, mac_callback_ptr, buf_list);
|
2010-12-16 23:43:07 +01:00
|
|
|
if(ret == PHASE_DEFERRED) {
|
|
|
|
return MAC_TX_DEFERRED;
|
|
|
|
}
|
|
|
|
if(ret != PHASE_UNKNOWN) {
|
|
|
|
is_known_receiver = 1;
|
2010-03-14 23:59:23 +01:00
|
|
|
}
|
2015-07-10 23:44:46 +02:00
|
|
|
#endif /* WITH_PHASE_OPTIMIZATION */
|
2010-03-14 23:59:23 +01:00
|
|
|
}
|
2015-07-10 23:44:46 +02:00
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* By setting we_are_sending to one, we ensure that the rtimer
|
|
|
|
powercycle interrupt do not interfere with us sending the packet. */
|
|
|
|
we_are_sending = 1;
|
|
|
|
|
|
|
|
/* If we have a pending packet in the radio, we should not send now,
|
|
|
|
because we will trash the received packet. Instead, we signal
|
|
|
|
that we have a collision, which lets the packet be received. This
|
|
|
|
packet will be retransmitted later by the MAC protocol
|
|
|
|
instread. */
|
|
|
|
if(NETSTACK_RADIO.receiving_packet() || NETSTACK_RADIO.pending_packet()) {
|
|
|
|
we_are_sending = 0;
|
2010-02-28 09:33:21 +01:00
|
|
|
PRINTF("contikimac: collision receiving %d, pending %d\n",
|
|
|
|
NETSTACK_RADIO.receiving_packet(), NETSTACK_RADIO.pending_packet());
|
2010-02-18 22:26:15 +01:00
|
|
|
return MAC_TX_COLLISION;
|
|
|
|
}
|
2015-07-10 23:44:46 +02:00
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
/* Switch off the radio to ensure that we didn't start sending while
|
|
|
|
the radio was doing a channel check. */
|
|
|
|
off();
|
|
|
|
|
|
|
|
|
|
|
|
strobes = 0;
|
|
|
|
|
|
|
|
/* Send a train of strobes until the receiver answers with an ACK. */
|
|
|
|
collisions = 0;
|
|
|
|
|
|
|
|
got_strobe_ack = 0;
|
|
|
|
|
2010-12-06 10:08:22 +01:00
|
|
|
/* Set contikimac_is_on to one to allow the on() and off() functions
|
|
|
|
to control the radio. We restore the old value of
|
|
|
|
contikimac_is_on when we are done. */
|
|
|
|
contikimac_was_on = contikimac_is_on;
|
|
|
|
contikimac_is_on = 1;
|
2011-09-27 16:05:30 +02:00
|
|
|
|
2011-11-14 18:02:47 +01:00
|
|
|
#if !RDC_CONF_HARDWARE_CSMA
|
2010-02-18 22:26:15 +01:00
|
|
|
/* Check if there are any transmissions by others. */
|
2011-11-14 18:02:47 +01:00
|
|
|
/* TODO: why does this give collisions before sending with the mc1322x? */
|
|
|
|
if(is_receiver_awake == 0) {
|
2013-03-17 23:29:58 +01:00
|
|
|
int i;
|
2011-09-27 16:05:30 +02:00
|
|
|
for(i = 0; i < CCA_COUNT_MAX_TX; ++i) {
|
2010-02-18 22:26:15 +01:00
|
|
|
t0 = RTIMER_NOW();
|
|
|
|
on();
|
2011-11-14 18:02:47 +01:00
|
|
|
#if CCA_CHECK_TIME > 0
|
2010-02-18 22:26:15 +01:00
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + CCA_CHECK_TIME)) { }
|
2011-11-14 18:02:47 +01:00
|
|
|
#endif
|
2010-02-18 22:26:15 +01:00
|
|
|
if(NETSTACK_RADIO.channel_clear() == 0) {
|
|
|
|
collisions++;
|
|
|
|
off();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
off();
|
2010-10-21 00:25:24 +02:00
|
|
|
t0 = RTIMER_NOW();
|
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + CCA_SLEEP_TIME)) { }
|
2010-02-18 22:26:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(collisions > 0) {
|
|
|
|
we_are_sending = 0;
|
2010-02-23 19:49:05 +01:00
|
|
|
off();
|
2010-03-09 14:19:05 +01:00
|
|
|
PRINTF("contikimac: collisions before sending\n");
|
2010-12-06 10:08:22 +01:00
|
|
|
contikimac_is_on = contikimac_was_on;
|
2010-02-18 22:26:15 +01:00
|
|
|
return MAC_TX_COLLISION;
|
|
|
|
}
|
2011-11-14 18:02:47 +01:00
|
|
|
#endif /* RDC_CONF_HARDWARE_CSMA */
|
2010-02-18 22:26:15 +01:00
|
|
|
|
2011-11-14 18:02:47 +01:00
|
|
|
#if !RDC_CONF_HARDWARE_ACK
|
2010-03-14 23:59:23 +01:00
|
|
|
if(!is_broadcast) {
|
2013-03-17 23:29:58 +01:00
|
|
|
/* Turn radio on to receive expected unicast ack. Not necessary
|
|
|
|
with hardware ack detection, and may trigger an unnecessary cca
|
|
|
|
or rx cycle */
|
2011-11-14 18:02:47 +01:00
|
|
|
on();
|
2010-03-14 23:59:23 +01:00
|
|
|
}
|
2015-07-16 14:42:25 +02:00
|
|
|
seqno = packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO);
|
2011-11-14 18:02:47 +01:00
|
|
|
#endif
|
2011-09-27 16:05:30 +02:00
|
|
|
|
2010-10-03 22:39:24 +02:00
|
|
|
watchdog_periodic();
|
2010-02-18 22:26:15 +01:00
|
|
|
t0 = RTIMER_NOW();
|
|
|
|
for(strobes = 0, collisions = 0;
|
|
|
|
got_strobe_ack == 0 && collisions == 0 &&
|
|
|
|
RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + STROBE_TIME); strobes++) {
|
2010-03-19 14:24:58 +01:00
|
|
|
|
|
|
|
watchdog_periodic();
|
2011-09-27 16:05:30 +02:00
|
|
|
|
2013-11-28 09:43:59 +01:00
|
|
|
#if WITH_PHASE_OPTIMIZATION
|
2013-03-17 23:29:58 +01:00
|
|
|
if(!is_broadcast && (is_receiver_awake || is_known_receiver) &&
|
|
|
|
!RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + MAX_PHASE_STROBE_TIME)) {
|
2010-12-16 23:43:07 +01:00
|
|
|
PRINTF("miss to %d\n", packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[0]);
|
2010-03-14 23:59:23 +01:00
|
|
|
break;
|
2010-04-04 09:49:30 +02:00
|
|
|
}
|
2013-11-28 09:43:59 +01:00
|
|
|
#endif /* WITH_PHASE_OPTIMIZATION */
|
2011-09-27 16:05:30 +02:00
|
|
|
|
2015-07-16 14:42:25 +02:00
|
|
|
#if !RDC_CONF_HARDWARE_ACK
|
2010-02-18 22:26:15 +01:00
|
|
|
len = 0;
|
2015-07-16 14:42:25 +02:00
|
|
|
#endif
|
2010-02-18 22:26:15 +01:00
|
|
|
|
2010-03-14 23:59:23 +01:00
|
|
|
{
|
2010-02-18 22:26:15 +01:00
|
|
|
rtimer_clock_t wt;
|
2015-06-02 01:38:11 +02:00
|
|
|
#if WITH_PHASE_OPTIMIZATION
|
2015-05-06 16:56:19 +02:00
|
|
|
rtimer_clock_t txtime = RTIMER_NOW();
|
2015-06-02 01:38:11 +02:00
|
|
|
#endif
|
2015-05-06 16:56:19 +02:00
|
|
|
#if RDC_CONF_HARDWARE_ACK
|
|
|
|
int ret = NETSTACK_RADIO.transmit(transmit_len);
|
|
|
|
#else
|
|
|
|
NETSTACK_RADIO.transmit(transmit_len);
|
|
|
|
#endif
|
2010-04-04 09:49:30 +02:00
|
|
|
|
2011-11-14 18:02:47 +01:00
|
|
|
#if RDC_CONF_HARDWARE_ACK
|
2013-03-17 23:29:58 +01:00
|
|
|
/* For radios that block in the transmit routine and detect the
|
|
|
|
ACK in hardware */
|
2011-11-14 18:02:47 +01:00
|
|
|
if(ret == RADIO_TX_OK) {
|
|
|
|
if(!is_broadcast) {
|
|
|
|
got_strobe_ack = 1;
|
2015-06-02 01:38:11 +02:00
|
|
|
#if WITH_PHASE_OPTIMIZATION
|
2012-03-26 22:49:28 +02:00
|
|
|
encounter_time = txtime;
|
2015-06-02 01:38:11 +02:00
|
|
|
#endif
|
2011-11-14 18:02:47 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (ret == RADIO_TX_NOACK) {
|
|
|
|
} else if (ret == RADIO_TX_COLLISION) {
|
|
|
|
PRINTF("contikimac: collisions while sending\n");
|
|
|
|
collisions++;
|
|
|
|
}
|
2013-03-17 23:29:58 +01:00
|
|
|
wt = RTIMER_NOW();
|
2011-11-14 18:02:47 +01:00
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + INTER_PACKET_INTERVAL)) { }
|
2013-03-17 23:29:58 +01:00
|
|
|
#else /* RDC_CONF_HARDWARE_ACK */
|
2011-11-14 18:02:47 +01:00
|
|
|
/* Wait for the ACK packet */
|
2010-02-18 22:26:15 +01:00
|
|
|
wt = RTIMER_NOW();
|
2010-03-14 13:13:54 +01:00
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + INTER_PACKET_INTERVAL)) { }
|
2011-04-13 14:10:02 +02:00
|
|
|
|
2010-02-28 09:33:21 +01:00
|
|
|
if(!is_broadcast && (NETSTACK_RADIO.receiving_packet() ||
|
|
|
|
NETSTACK_RADIO.pending_packet() ||
|
|
|
|
NETSTACK_RADIO.channel_clear() == 0)) {
|
2010-02-18 22:26:15 +01:00
|
|
|
uint8_t ackbuf[ACK_LEN];
|
|
|
|
wt = RTIMER_NOW();
|
2010-03-14 13:13:54 +01:00
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + AFTER_ACK_DETECTECT_WAIT_TIME)) { }
|
2011-04-13 14:10:02 +02:00
|
|
|
|
2010-02-28 09:33:21 +01:00
|
|
|
len = NETSTACK_RADIO.read(ackbuf, ACK_LEN);
|
2013-03-17 23:29:58 +01:00
|
|
|
if(len == ACK_LEN && seqno == ackbuf[ACK_LEN - 1]) {
|
2010-02-28 09:33:21 +01:00
|
|
|
got_strobe_ack = 1;
|
2015-06-02 01:38:11 +02:00
|
|
|
#if WITH_PHASE_OPTIMIZATION
|
2012-03-26 22:49:28 +02:00
|
|
|
encounter_time = txtime;
|
2015-06-02 01:38:11 +02:00
|
|
|
#endif
|
2010-03-14 23:59:23 +01:00
|
|
|
break;
|
2010-02-18 22:26:15 +01:00
|
|
|
} else {
|
2010-03-09 14:19:05 +01:00
|
|
|
PRINTF("contikimac: collisions while sending\n");
|
2010-02-28 09:33:21 +01:00
|
|
|
collisions++;
|
2010-02-18 22:26:15 +01:00
|
|
|
}
|
|
|
|
}
|
2011-11-14 18:02:47 +01:00
|
|
|
#endif /* RDC_CONF_HARDWARE_ACK */
|
2010-02-18 22:26:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-16 23:43:07 +01:00
|
|
|
off();
|
2010-02-18 22:26:15 +01:00
|
|
|
|
2010-03-14 23:59:23 +01:00
|
|
|
PRINTF("contikimac: send (strobes=%u, len=%u, %s, %s), done\n", strobes,
|
|
|
|
packetbuf_totlen(),
|
|
|
|
got_strobe_ack ? "ack" : "no ack",
|
|
|
|
collisions ? "collision" : "no collision");
|
2010-02-18 22:26:15 +01:00
|
|
|
|
|
|
|
#if CONTIKIMAC_CONF_COMPOWER
|
|
|
|
/* Accumulate the power consumption for the packet transmission. */
|
|
|
|
compower_accumulate(¤t_packet);
|
|
|
|
|
|
|
|
/* Convert the accumulated power consumption for the transmitted
|
|
|
|
packet to packet attributes so that the higher levels can keep
|
|
|
|
track of the amount of energy spent on transmitting the
|
|
|
|
packet. */
|
|
|
|
compower_attrconv(¤t_packet);
|
|
|
|
|
|
|
|
/* Clear the accumulated power consumption so that it is ready for
|
|
|
|
the next packet. */
|
|
|
|
compower_clear(¤t_packet);
|
|
|
|
#endif /* CONTIKIMAC_CONF_COMPOWER */
|
|
|
|
|
2010-12-06 10:08:22 +01:00
|
|
|
contikimac_is_on = contikimac_was_on;
|
2010-02-18 22:26:15 +01:00
|
|
|
we_are_sending = 0;
|
|
|
|
|
2010-03-14 23:59:23 +01:00
|
|
|
/* Determine the return value that we will return from the
|
|
|
|
function. We must pass this value to the phase module before we
|
|
|
|
return from the function. */
|
2010-02-23 19:49:05 +01:00
|
|
|
if(collisions > 0) {
|
2010-03-14 23:59:23 +01:00
|
|
|
ret = MAC_TX_COLLISION;
|
2010-10-03 22:39:24 +02:00
|
|
|
} else if(!is_broadcast && !got_strobe_ack) {
|
2010-03-14 23:59:23 +01:00
|
|
|
ret = MAC_TX_NOACK;
|
2010-02-23 19:49:05 +01:00
|
|
|
} else {
|
2010-03-14 23:59:23 +01:00
|
|
|
ret = MAC_TX_OK;
|
2010-02-23 19:49:05 +01:00
|
|
|
}
|
2010-03-14 23:59:23 +01:00
|
|
|
|
|
|
|
#if WITH_PHASE_OPTIMIZATION
|
2010-12-16 23:43:07 +01:00
|
|
|
if(is_known_receiver && got_strobe_ack) {
|
2013-03-17 23:29:58 +01:00
|
|
|
PRINTF("no miss %d wake-ups %d\n",
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[0],
|
2010-12-16 23:43:07 +01:00
|
|
|
strobes);
|
|
|
|
}
|
2011-02-08 21:57:46 +01:00
|
|
|
|
2010-12-16 23:43:07 +01:00
|
|
|
if(!is_broadcast) {
|
2011-09-27 16:05:30 +02:00
|
|
|
if(collisions == 0 && is_receiver_awake == 0) {
|
2013-07-03 19:14:10 +02:00
|
|
|
phase_update(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
2013-03-17 23:29:58 +01:00
|
|
|
encounter_time, ret);
|
2010-03-14 23:59:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* WITH_PHASE_OPTIMIZATION */
|
|
|
|
|
|
|
|
return ret;
|
2010-02-18 22:26:15 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
qsend_packet(mac_callback_t sent, void *ptr)
|
|
|
|
{
|
2013-03-17 23:35:39 +01:00
|
|
|
int ret = send_packet(sent, ptr, NULL, 0);
|
2010-03-09 21:37:02 +01:00
|
|
|
if(ret != MAC_TX_DEFERRED) {
|
|
|
|
mac_call_sent_callback(sent, ptr, ret, 1);
|
|
|
|
}
|
2010-02-18 22:26:15 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-10-03 15:17:30 +02:00
|
|
|
static void
|
2011-09-27 16:05:30 +02:00
|
|
|
qsend_list(mac_callback_t sent, void *ptr, struct rdc_buf_list *buf_list)
|
|
|
|
{
|
2014-04-26 11:39:51 +02:00
|
|
|
struct rdc_buf_list *curr;
|
2011-09-27 16:05:30 +02:00
|
|
|
struct rdc_buf_list *next;
|
|
|
|
int ret;
|
2013-03-17 23:35:39 +01:00
|
|
|
int is_receiver_awake;
|
2015-07-14 15:53:10 +02:00
|
|
|
int pending;
|
2015-07-10 23:44:46 +02:00
|
|
|
|
2014-04-26 11:39:51 +02:00
|
|
|
if(buf_list == NULL) {
|
2011-09-27 16:05:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Do not send during reception of a burst */
|
|
|
|
if(we_are_receiving_burst) {
|
2011-11-15 16:59:12 +01:00
|
|
|
/* Prepare the packetbuf for callback */
|
2014-04-26 11:39:51 +02:00
|
|
|
queuebuf_to_packetbuf(buf_list->buf);
|
2011-10-04 10:29:40 +02:00
|
|
|
/* Return COLLISION so the MAC may try again later */
|
|
|
|
mac_call_sent_callback(sent, ptr, MAC_TX_COLLISION, 1);
|
2011-09-27 16:05:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-07-10 23:44:46 +02:00
|
|
|
|
2014-04-26 11:39:51 +02:00
|
|
|
/* Create and secure frames in advance */
|
|
|
|
curr = buf_list;
|
|
|
|
do {
|
|
|
|
next = list_item_next(curr);
|
|
|
|
queuebuf_to_packetbuf(curr->buf);
|
|
|
|
if(!packetbuf_attr(PACKETBUF_ATTR_IS_CREATED_AND_SECURED)) {
|
|
|
|
/* create and secure this frame */
|
|
|
|
if(next != NULL) {
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_PENDING, 1);
|
|
|
|
}
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAC_ACK, 1);
|
2015-06-27 18:21:08 +02:00
|
|
|
if(NETSTACK_FRAMER.create() < 0) {
|
2014-04-26 11:39:51 +02:00
|
|
|
PRINTF("contikimac: framer failed\n");
|
|
|
|
mac_call_sent_callback(sent, ptr, MAC_TX_ERR_FATAL, 1);
|
|
|
|
return;
|
|
|
|
}
|
2015-07-10 23:44:46 +02:00
|
|
|
|
2014-04-26 11:39:51 +02:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_IS_CREATED_AND_SECURED, 1);
|
|
|
|
queuebuf_update_from_packetbuf(curr->buf);
|
|
|
|
}
|
|
|
|
curr = next;
|
|
|
|
} while(next != NULL);
|
2015-07-10 23:44:46 +02:00
|
|
|
|
2011-09-27 16:05:30 +02:00
|
|
|
/* The receiver needs to be awoken before we send */
|
|
|
|
is_receiver_awake = 0;
|
2014-04-26 11:39:51 +02:00
|
|
|
curr = buf_list;
|
2011-09-27 16:05:30 +02:00
|
|
|
do { /* A loop sending a burst of packets from buf_list */
|
|
|
|
next = list_item_next(curr);
|
|
|
|
|
|
|
|
/* Prepare the packetbuf */
|
|
|
|
queuebuf_to_packetbuf(curr->buf);
|
2015-07-14 15:53:10 +02:00
|
|
|
|
|
|
|
pending = packetbuf_attr(PACKETBUF_ATTR_PENDING);
|
|
|
|
|
2011-09-27 16:05:30 +02:00
|
|
|
/* Send the current packet */
|
2013-03-17 23:35:39 +01:00
|
|
|
ret = send_packet(sent, ptr, curr, is_receiver_awake);
|
2011-09-27 16:05:30 +02:00
|
|
|
if(ret != MAC_TX_DEFERRED) {
|
|
|
|
mac_call_sent_callback(sent, ptr, ret, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ret == MAC_TX_OK) {
|
|
|
|
if(next != NULL) {
|
|
|
|
/* We're in a burst, no need to wake the receiver up again */
|
|
|
|
is_receiver_awake = 1;
|
|
|
|
curr = next;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* The transmission failed, we stop the burst */
|
|
|
|
next = NULL;
|
|
|
|
}
|
2015-07-14 15:53:10 +02:00
|
|
|
} while((next != NULL) && pending);
|
2011-09-27 16:05:30 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-03-17 23:29:58 +01:00
|
|
|
/* Timer callback triggered when receiving a burst, after having
|
|
|
|
waited for a next packet for a too long time. Turns the radio off
|
|
|
|
and leaves burst reception mode */
|
2010-02-18 22:26:15 +01:00
|
|
|
static void
|
2011-09-27 16:05:30 +02:00
|
|
|
recv_burst_off(void *ptr)
|
2010-02-18 22:26:15 +01:00
|
|
|
{
|
|
|
|
off();
|
2011-09-27 16:05:30 +02:00
|
|
|
we_are_receiving_burst = 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
input_packet(void)
|
|
|
|
{
|
|
|
|
static struct ctimer ct;
|
2015-07-09 02:46:58 +02:00
|
|
|
int duplicate = 0;
|
|
|
|
|
|
|
|
#if CONTIKIMAC_SEND_SW_ACK
|
|
|
|
int original_datalen;
|
|
|
|
uint8_t *original_dataptr;
|
|
|
|
|
|
|
|
original_datalen = packetbuf_datalen();
|
|
|
|
original_dataptr = packetbuf_dataptr();
|
|
|
|
#endif
|
|
|
|
|
2011-09-27 16:05:30 +02:00
|
|
|
if(!we_are_receiving_burst) {
|
|
|
|
off();
|
|
|
|
}
|
2010-02-18 22:26:15 +01:00
|
|
|
|
2015-07-09 02:46:58 +02:00
|
|
|
if(packetbuf_datalen() == ACK_LEN) {
|
|
|
|
/* Ignore ack packets */
|
|
|
|
PRINTF("ContikiMAC: ignored ack\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-29 23:51:36 +02:00
|
|
|
/* printf("cycle_start 0x%02x 0x%02x\n", cycle_start, cycle_start % CYCLE_TIME);*/
|
2012-11-24 17:18:59 +01:00
|
|
|
|
2012-01-01 03:00:21 +01:00
|
|
|
if(packetbuf_totlen() > 0 && NETSTACK_FRAMER.parse() >= 0) {
|
2010-02-23 19:49:05 +01:00
|
|
|
if(packetbuf_datalen() > 0 &&
|
|
|
|
packetbuf_totlen() > 0 &&
|
2013-12-12 23:58:52 +01:00
|
|
|
(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
|
|
|
&linkaddr_node_addr) ||
|
2014-08-05 13:49:05 +02:00
|
|
|
packetbuf_holds_broadcast())) {
|
2010-02-23 19:49:05 +01:00
|
|
|
/* This is a regular packet that is destined to us or to the
|
|
|
|
broadcast address. */
|
2010-02-18 22:26:15 +01:00
|
|
|
|
2011-09-27 16:05:30 +02:00
|
|
|
/* If FRAME_PENDING is set, we are receiving a packets in a burst */
|
|
|
|
we_are_receiving_burst = packetbuf_attr(PACKETBUF_ATTR_PENDING);
|
|
|
|
if(we_are_receiving_burst) {
|
|
|
|
on();
|
2013-03-17 23:29:58 +01:00
|
|
|
/* Set a timer to turn the radio off in case we do not receive
|
|
|
|
a next packet */
|
2011-09-27 16:05:30 +02:00
|
|
|
ctimer_set(&ct, INTER_PACKET_DEADLINE, recv_burst_off, NULL);
|
|
|
|
} else {
|
|
|
|
off();
|
|
|
|
ctimer_stop(&ct);
|
2010-04-03 15:28:30 +02:00
|
|
|
}
|
2010-04-05 21:28:07 +02:00
|
|
|
|
2014-01-12 13:58:34 +01:00
|
|
|
#if RDC_WITH_DUPLICATE_DETECTION
|
2013-11-15 19:21:41 +01:00
|
|
|
/* Check for duplicate packet. */
|
2015-07-09 02:46:58 +02:00
|
|
|
duplicate = mac_sequence_is_duplicate();
|
|
|
|
if(duplicate) {
|
2013-11-15 19:21:41 +01:00
|
|
|
/* Drop the packet. */
|
2015-07-09 02:46:58 +02:00
|
|
|
PRINTF("contikimac: Drop duplicate\n");
|
|
|
|
} else {
|
|
|
|
mac_sequence_register_seqno();
|
2010-04-05 21:28:07 +02:00
|
|
|
}
|
2014-01-12 13:58:34 +01:00
|
|
|
#endif /* RDC_WITH_DUPLICATE_DETECTION */
|
2010-04-05 21:28:07 +02:00
|
|
|
|
2010-02-18 22:26:15 +01:00
|
|
|
#if CONTIKIMAC_CONF_COMPOWER
|
2010-02-23 19:49:05 +01:00
|
|
|
/* Accumulate the power consumption for the packet reception. */
|
|
|
|
compower_accumulate(¤t_packet);
|
|
|
|
/* Convert the accumulated power consumption for the received
|
|
|
|
packet to packet attributes so that the higher levels can
|
|
|
|
keep track of the amount of energy spent on receiving the
|
|
|
|
packet. */
|
|
|
|
compower_attrconv(¤t_packet);
|
|
|
|
|
|
|
|
/* Clear the accumulated power consumption so that it is ready
|
|
|
|
for the next packet. */
|
|
|
|
compower_clear(¤t_packet);
|
2010-02-18 22:26:15 +01:00
|
|
|
#endif /* CONTIKIMAC_CONF_COMPOWER */
|
|
|
|
|
2010-03-16 19:11:13 +01:00
|
|
|
PRINTDEBUG("contikimac: data (%u)\n", packetbuf_datalen());
|
2015-07-09 02:46:58 +02:00
|
|
|
|
|
|
|
#if CONTIKIMAC_SEND_SW_ACK
|
|
|
|
{
|
|
|
|
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[ACK_LEN] = {0, 0, 0};
|
|
|
|
|
|
|
|
we_are_sending = 1;
|
|
|
|
ackdata[0] = FRAME802154_ACKFRAME;
|
|
|
|
ackdata[1] = 0;
|
|
|
|
ackdata[2] = info154.seq;
|
|
|
|
NETSTACK_RADIO.send(ackdata, ACK_LEN);
|
|
|
|
we_are_sending = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONTIKIMAC_SEND_SW_ACK */
|
|
|
|
|
|
|
|
if(!duplicate) {
|
|
|
|
NETSTACK_MAC.input();
|
|
|
|
}
|
2010-02-23 19:49:05 +01:00
|
|
|
return;
|
2010-02-18 22:26:15 +01:00
|
|
|
} else {
|
2010-02-23 19:49:05 +01:00
|
|
|
PRINTDEBUG("contikimac: data not for us\n");
|
2010-02-18 22:26:15 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PRINTF("contikimac: failed to parse (%u)\n", packetbuf_totlen());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
init(void)
|
|
|
|
{
|
|
|
|
radio_is_on = 0;
|
|
|
|
PT_INIT(&pt);
|
2011-09-27 16:05:30 +02:00
|
|
|
|
2015-09-01 16:34:27 +02:00
|
|
|
rtimer_set(&rt, RTIMER_NOW() + CYCLE_TIME, 1, powercycle_wrapper, NULL);
|
2010-02-18 22:26:15 +01:00
|
|
|
|
|
|
|
contikimac_is_on = 1;
|
|
|
|
|
|
|
|
#if WITH_PHASE_OPTIMIZATION
|
2013-07-03 19:14:10 +02:00
|
|
|
phase_init();
|
2010-02-18 22:26:15 +01:00
|
|
|
#endif /* WITH_PHASE_OPTIMIZATION */
|
|
|
|
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
turn_on(void)
|
|
|
|
{
|
2010-04-04 09:49:30 +02:00
|
|
|
if(contikimac_is_on == 0) {
|
|
|
|
contikimac_is_on = 1;
|
2010-12-08 00:12:54 +01:00
|
|
|
contikimac_keep_radio_on = 0;
|
2015-09-01 16:34:27 +02:00
|
|
|
rtimer_set(&rt, RTIMER_NOW() + CYCLE_TIME, 1, powercycle_wrapper, NULL);
|
2010-04-04 09:49:30 +02:00
|
|
|
}
|
2010-02-18 22:26:15 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
turn_off(int keep_radio_on)
|
|
|
|
{
|
|
|
|
contikimac_is_on = 0;
|
2010-12-06 10:08:22 +01:00
|
|
|
contikimac_keep_radio_on = keep_radio_on;
|
2010-02-18 22:26:15 +01:00
|
|
|
if(keep_radio_on) {
|
2010-12-06 10:08:22 +01:00
|
|
|
radio_is_on = 1;
|
2010-02-18 22:26:15 +01:00
|
|
|
return NETSTACK_RADIO.on();
|
|
|
|
} else {
|
2010-12-06 10:08:22 +01:00
|
|
|
radio_is_on = 0;
|
2010-02-18 22:26:15 +01:00
|
|
|
return NETSTACK_RADIO.off();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static unsigned short
|
|
|
|
duty_cycle(void)
|
|
|
|
{
|
|
|
|
return (1ul * CLOCK_SECOND * CYCLE_TIME) / RTIMER_ARCH_SECOND;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-02-23 21:09:11 +01:00
|
|
|
const struct rdc_driver contikimac_driver = {
|
2010-02-18 22:26:15 +01:00
|
|
|
"ContikiMAC",
|
|
|
|
init,
|
|
|
|
qsend_packet,
|
2011-09-27 16:05:30 +02:00
|
|
|
qsend_list,
|
2010-02-18 22:26:15 +01:00
|
|
|
input_packet,
|
|
|
|
turn_on,
|
|
|
|
turn_off,
|
|
|
|
duty_cycle,
|
|
|
|
};
|
2010-02-23 19:49:05 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uint16_t
|
|
|
|
contikimac_debug_print(void)
|
|
|
|
{
|
2010-02-28 09:33:21 +01:00
|
|
|
return 0;
|
2010-02-23 19:49:05 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|