2010-02-18 21:26:15 +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-02-18 21:26:15 +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.
|
|
|
|
*
|
2010-10-03 20:39:24 +00:00
|
|
|
* $Id: contikimac.c,v 1.38 2010/10/03 20:39:24 adamdunkels Exp $
|
2010-02-18 21:26:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
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
|
|
|
* The Contiki power-saving MAC protocol (ContikiMAC)
|
2010-02-18 21:26:15 +00:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
* Niclas Finne <nfi@sics.se>
|
|
|
|
* Joakim Eriksson <joakime@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "net/netstack.h"
|
|
|
|
#include "dev/leds.h"
|
|
|
|
#include "dev/radio.h"
|
|
|
|
#include "dev/watchdog.h"
|
|
|
|
#include "lib/random.h"
|
|
|
|
#include "net/mac/contikimac.h"
|
|
|
|
#include "net/rime.h"
|
|
|
|
#include "sys/compower.h"
|
|
|
|
#include "sys/pt.h"
|
|
|
|
#include "sys/rtimer.h"
|
|
|
|
|
2010-04-04 12:31:47 +00:00
|
|
|
/*#include "cooja-debug.h"*/
|
2010-02-18 21:26:15 +00:00
|
|
|
#include "contiki-conf.h"
|
|
|
|
|
|
|
|
#ifdef EXPERIMENT_SETUP
|
|
|
|
#include "experiment-setup.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifndef WITH_ACK_OPTIMIZATION
|
2010-03-19 13:24:58 +00:00
|
|
|
#define WITH_ACK_OPTIMIZATION 0
|
2010-02-18 21:26:15 +00:00
|
|
|
#endif
|
|
|
|
#ifndef WITH_PHASE_OPTIMIZATION
|
|
|
|
#define WITH_PHASE_OPTIMIZATION 1
|
|
|
|
#endif
|
|
|
|
#ifndef WITH_STREAMING
|
|
|
|
#define WITH_STREAMING 1
|
|
|
|
#endif
|
2010-04-26 17:46:21 +00:00
|
|
|
#ifndef WITH_CONTIKIMAC_HEADER
|
|
|
|
#define WITH_CONTIKIMAC_HEADER 1
|
|
|
|
#endif
|
2010-02-18 21:26:15 +00:00
|
|
|
|
|
|
|
struct announcement_data {
|
|
|
|
uint16_t id;
|
|
|
|
uint16_t value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The maximum number of announcements in a single announcement
|
|
|
|
message - may need to be increased in the future. */
|
|
|
|
#define ANNOUNCEMENT_MAX 10
|
|
|
|
|
2010-04-26 17:46:21 +00:00
|
|
|
#if WITH_CONTIKIMAC_HEADER
|
|
|
|
#define CONTIKIMAC_ID 0x00
|
|
|
|
|
|
|
|
struct hdr {
|
|
|
|
uint8_t id;
|
|
|
|
uint8_t len;
|
|
|
|
};
|
|
|
|
#endif /* WITH_CONTIKIMAC_HEADER */
|
|
|
|
|
2010-02-18 21:26:15 +00:00
|
|
|
/* The structure of the announcement messages. */
|
|
|
|
struct announcement_msg {
|
2010-03-16 18:11:13 +00:00
|
|
|
uint8_t announcement_magic[2];
|
2010-02-18 21:26:15 +00:00
|
|
|
uint16_t num;
|
|
|
|
struct announcement_data data[ANNOUNCEMENT_MAX];
|
|
|
|
};
|
|
|
|
|
2010-03-16 18:11:13 +00:00
|
|
|
#define ANNOUNCEMENT_MAGIC1 0xAD
|
|
|
|
#define ANNOUNCEMENT_MAGIC2 0xAD
|
|
|
|
|
2010-02-18 21:26:15 +00:00
|
|
|
/* The length of the header of the announcement message, i.e., the
|
|
|
|
"num" field in the struct. */
|
2010-03-16 18:11:13 +00:00
|
|
|
#define ANNOUNCEMENT_MSG_HEADERLEN (sizeof(uint16_t) * 2)
|
2010-02-18 21:26:15 +00:00
|
|
|
|
|
|
|
#ifdef CONTIKIMAC_CONF_CYCLE_TIME
|
|
|
|
#define CYCLE_TIME (CONTIKIMAC_CONF_CYCLE_TIME)
|
|
|
|
#else
|
2010-10-03 20:39:24 +00:00
|
|
|
#define CYCLE_TIME (RTIMER_ARCH_SECOND / NETSTACK_RDC_CHANNEL_CHECK_RATE)
|
2010-02-18 21:26:15 +00:00
|
|
|
#endif
|
|
|
|
|
2010-03-31 20:27:15 +00:00
|
|
|
#define MAX_PHASE_STROBE_TIME RTIMER_ARCH_SECOND / 20
|
2010-03-14 22:59:23 +00:00
|
|
|
|
2010-04-30 07:25:51 +00:00
|
|
|
#define CCA_COUNT_MAX 2
|
|
|
|
#define CCA_CHECK_TIME RTIMER_ARCH_SECOND / 8192
|
|
|
|
#define CCA_SLEEP_TIME RTIMER_ARCH_SECOND / 2000
|
2010-03-14 22:59:23 +00:00
|
|
|
#define CHECK_TIME (CCA_COUNT_MAX * (CCA_CHECK_TIME + CCA_SLEEP_TIME))
|
|
|
|
|
2010-03-31 20:27:15 +00:00
|
|
|
#define STROBE_TIME (CYCLE_TIME + 2 * CHECK_TIME)
|
|
|
|
|
2010-03-14 22:59:23 +00:00
|
|
|
#define STREAM_CCA_COUNT (CYCLE_TIME / (CCA_SLEEP_TIME + CCA_CHECK_TIME) - CCA_COUNT_MAX)
|
2010-02-18 21:26:15 +00:00
|
|
|
|
2010-04-04 21:01:24 +00:00
|
|
|
#define GUARD_TIME 7 * CHECK_TIME
|
2010-03-29 21:51:36 +00:00
|
|
|
|
2010-03-30 23:01:32 +00:00
|
|
|
#define INTER_PACKET_INTERVAL RTIMER_ARCH_SECOND / 5000
|
2010-03-09 13:19:05 +00:00
|
|
|
#define AFTER_ACK_DETECTECT_WAIT_TIME RTIMER_ARCH_SECOND / 1500
|
2010-02-18 21:26:15 +00:00
|
|
|
|
2010-04-04 07:49:30 +00:00
|
|
|
#define LISTEN_TIME_AFTER_PACKET_DETECTED RTIMER_ARCH_SECOND / 80
|
2010-03-09 13:19:05 +00:00
|
|
|
|
2010-04-03 13:28:30 +00:00
|
|
|
#define SHORTEST_PACKET_SIZE 43
|
2010-03-30 23:01:32 +00:00
|
|
|
|
|
|
|
#define MAX_SILENCE_PERIODS 5
|
2010-04-04 07:49:30 +00:00
|
|
|
#define MAX_NONACTIVITY_PERIODIC 10
|
2010-02-18 21:26:15 +00:00
|
|
|
|
|
|
|
/* The cycle time for announcements. */
|
2010-03-29 21:51:36 +00:00
|
|
|
#ifdef ANNOUNCEMENT_CONF_PERIOD
|
|
|
|
#define ANNOUNCEMENT_PERIOD ANNOUNCEMENT_CONF_PERIOD
|
|
|
|
#else /* ANNOUNCEMENT_CONF_PERIOD */
|
|
|
|
#define ANNOUNCEMENT_PERIOD 1 * CLOCK_SECOND
|
|
|
|
#endif /* ANNOUNCEMENT_CONF_PERIOD */
|
2010-02-18 21:26:15 +00:00
|
|
|
|
|
|
|
/* The time before sending an announcement within one announcement
|
|
|
|
cycle. */
|
|
|
|
#define ANNOUNCEMENT_TIME (random_rand() % (ANNOUNCEMENT_PERIOD))
|
|
|
|
|
|
|
|
|
|
|
|
#define ACK_LEN 3
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
static struct rtimer rt;
|
|
|
|
static struct pt pt;
|
|
|
|
|
|
|
|
static volatile uint8_t contikimac_is_on = 0;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2010-04-20 11:41:16 +00:00
|
|
|
#define DEBUG_LEDS DEBUG
|
|
|
|
#undef LEDS_ON
|
|
|
|
#undef LEDS_OFF
|
|
|
|
#if DEBUG_LEDS
|
|
|
|
#define LEDS_ON(x) leds_on(x)
|
|
|
|
#define LEDS_OFF(x) leds_off(x)
|
|
|
|
#else
|
|
|
|
#define LEDS_ON(x)
|
|
|
|
#define LEDS_OFF(x)
|
|
|
|
#endif
|
|
|
|
|
2010-02-18 21:26:15 +00:00
|
|
|
#if CONTIKIMAC_CONF_ANNOUNCEMENTS
|
|
|
|
/* Timers for keeping track of when to send announcements. */
|
|
|
|
static struct ctimer announcement_cycle_ctimer, announcement_ctimer;
|
|
|
|
|
|
|
|
static int announcement_radio_txpower;
|
|
|
|
#endif /* CONTIKIMAC_CONF_ANNOUNCEMENTS */
|
|
|
|
|
2010-04-01 10:02:04 +00:00
|
|
|
/* Flag that is used to keep track of whether or not we are snooping
|
2010-02-18 21:26:15 +00:00
|
|
|
for announcements from neighbors. */
|
2010-04-01 10:02:04 +00:00
|
|
|
static volatile uint8_t is_snooping;
|
2010-02-18 21:26:15 +00:00
|
|
|
|
|
|
|
#if CONTIKIMAC_CONF_COMPOWER
|
|
|
|
static struct compower_activity current_packet;
|
|
|
|
#endif /* CONTIKIMAC_CONF_COMPOWER */
|
|
|
|
|
|
|
|
#if WITH_PHASE_OPTIMIZATION
|
|
|
|
|
|
|
|
#include "net/mac/phase.h"
|
|
|
|
|
2010-04-01 17:17:36 +00:00
|
|
|
#ifndef MAX_PHASE_NEIGHBORS
|
2010-03-09 13:19:05 +00:00
|
|
|
#define MAX_PHASE_NEIGHBORS 30
|
2010-04-01 17:17:36 +00:00
|
|
|
#endif
|
2010-02-18 21:26:15 +00:00
|
|
|
|
|
|
|
PHASE_LIST(phase_list, MAX_PHASE_NEIGHBORS);
|
|
|
|
|
|
|
|
#endif /* WITH_PHASE_OPTIMIZATION */
|
|
|
|
|
2010-03-14 22:59:23 +00:00
|
|
|
static volatile uint8_t is_streaming;
|
2010-02-18 21:26:15 +00:00
|
|
|
static rimeaddr_t is_streaming_to, is_streaming_to_too;
|
2010-03-14 22:59:23 +00:00
|
|
|
static volatile rtimer_clock_t stream_until;
|
2010-02-18 21:26:15 +00:00
|
|
|
|
2010-04-03 13:28:30 +00:00
|
|
|
#define DEFAULT_STREAM_TIME (1 * CYCLE_TIME)
|
2010-02-18 21:26:15 +00:00
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a, b) ((a) < (b)? (a) : (b))
|
|
|
|
#endif /* MIN */
|
|
|
|
|
2010-04-08 09:32:56 +00:00
|
|
|
struct seqno {
|
|
|
|
rimeaddr_t sender;
|
|
|
|
uint8_t seqno;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MAX_SEQNOS 8
|
|
|
|
static struct seqno received_seqnos[MAX_SEQNOS];
|
2010-03-09 13:19:05 +00:00
|
|
|
|
2010-04-06 11:57:43 +00: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 21:26:15 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
on(void)
|
|
|
|
{
|
|
|
|
if(contikimac_is_on && radio_is_on == 0) {
|
|
|
|
radio_is_on = 1;
|
|
|
|
NETSTACK_RADIO.on();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
off(void)
|
|
|
|
{
|
2010-04-04 07:49:30 +00:00
|
|
|
if(contikimac_is_on && radio_is_on != 0 && is_streaming == 0/* && is_snooping == 0*/) {
|
2010-02-18 21:26:15 +00:00
|
|
|
radio_is_on = 0;
|
|
|
|
NETSTACK_RADIO.off();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-04 12:28:29 +00:00
|
|
|
static volatile rtimer_clock_t cycle_start;
|
2010-02-18 21:26:15 +00:00
|
|
|
static char powercycle(struct rtimer *t, void *ptr);
|
|
|
|
static void
|
|
|
|
schedule_powercycle(struct rtimer *t, rtimer_clock_t time)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if(contikimac_is_on) {
|
2010-02-23 18:49:05 +00:00
|
|
|
|
2010-04-04 21:01:24 +00:00
|
|
|
if(RTIMER_CLOCK_LT(RTIMER_TIME(t) + time, RTIMER_NOW() + 2)) {
|
|
|
|
time = RTIMER_NOW() - RTIMER_TIME(t) + 2;
|
2010-02-28 08:33:21 +00:00
|
|
|
}
|
2010-04-04 12:28:29 +00:00
|
|
|
|
2010-02-18 21:26:15 +00:00
|
|
|
#if NURTIMER
|
|
|
|
r = rtimer_reschedule(t, time, 1);
|
|
|
|
#else
|
|
|
|
r = rtimer_set(t, RTIMER_TIME(t) + time, 1,
|
|
|
|
(void (*)(struct rtimer *, void *))powercycle, NULL);
|
|
|
|
#endif
|
|
|
|
if(r != RTIMER_OK) {
|
|
|
|
printf("schedule_powercycle: could not set rtimer\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void
|
2010-04-04 12:28:29 +00:00
|
|
|
schedule_powercycle_fixed(struct rtimer *t, rtimer_clock_t fixed_time)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if(contikimac_is_on) {
|
|
|
|
|
|
|
|
if(RTIMER_CLOCK_LT(fixed_time, RTIMER_NOW() + 1)) {
|
|
|
|
fixed_time = RTIMER_NOW() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if NURTIMER
|
|
|
|
r = rtimer_reschedule(t, RTIMER_TIME(t) - time, 1);
|
|
|
|
#else
|
|
|
|
r = rtimer_set(t, fixed_time, 1,
|
|
|
|
(void (*)(struct rtimer *, void *))powercycle, NULL);
|
|
|
|
#endif
|
|
|
|
if(r != RTIMER_OK) {
|
|
|
|
printf("schedule_powercycle: could not set rtimer\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void
|
2010-02-18 21:26:15 +00:00
|
|
|
powercycle_turn_radio_off(void)
|
|
|
|
{
|
|
|
|
if(we_are_sending == 0) {
|
|
|
|
off();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
powercycle_turn_radio_on(void)
|
|
|
|
{
|
|
|
|
if(we_are_sending == 0) {
|
|
|
|
on();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static char
|
|
|
|
powercycle(struct rtimer *t, void *ptr)
|
|
|
|
{
|
|
|
|
PT_BEGIN(&pt);
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
static uint8_t packet_seen;
|
2010-03-29 21:51:36 +00:00
|
|
|
static rtimer_clock_t t0;
|
2010-03-16 18:11:13 +00:00
|
|
|
static uint8_t count;
|
2010-02-18 21:26:15 +00:00
|
|
|
|
|
|
|
cycle_start = RTIMER_NOW();
|
|
|
|
|
2010-04-04 12:28:29 +00:00
|
|
|
if(WITH_STREAMING && is_streaming) {
|
|
|
|
#if NURTIMER
|
|
|
|
if(!RTIMER_CLOCK_LT(cycle_start, RTIMER_NOW(), stream_until))
|
|
|
|
#else
|
|
|
|
if(!RTIMER_CLOCK_LT(RTIMER_NOW(), stream_until))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
is_streaming = 0;
|
|
|
|
rimeaddr_copy(&is_streaming_to, &rimeaddr_null);
|
|
|
|
rimeaddr_copy(&is_streaming_to_too, &rimeaddr_null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-18 21:26:15 +00:00
|
|
|
packet_seen = 0;
|
2010-03-16 18:11:13 +00:00
|
|
|
|
2010-03-14 22:59:23 +00:00
|
|
|
do {
|
2010-02-18 21:26:15 +00:00
|
|
|
for(count = 0; count < CCA_COUNT_MAX; ++count) {
|
|
|
|
t0 = RTIMER_NOW();
|
2010-03-14 22:59:23 +00:00
|
|
|
if(we_are_sending == 0) {
|
|
|
|
powercycle_turn_radio_on();
|
2010-04-04 07:49:30 +00:00
|
|
|
#if 0
|
2010-02-18 21:26:15 +00:00
|
|
|
#if NURTIMER
|
2010-03-14 22:59:23 +00:00
|
|
|
while(RTIMER_CLOCK_LT(t0, RTIMER_NOW(), t0 + CCA_CHECK_TIME));
|
2010-02-18 21:26:15 +00:00
|
|
|
#else
|
2010-03-14 22:59:23 +00:00
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + CCA_CHECK_TIME));
|
2010-02-18 21:26:15 +00:00
|
|
|
#endif
|
2010-03-17 16:35:52 +00:00
|
|
|
#endif /* 0 */
|
2010-03-14 22:59:23 +00:00
|
|
|
/* Check if a packet is seen in the air. If so, we keep the
|
|
|
|
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. */
|
|
|
|
if(NETSTACK_RADIO.channel_clear() == 0) {
|
|
|
|
packet_seen = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
powercycle_turn_radio_off();
|
2010-02-18 21:26:15 +00:00
|
|
|
}
|
2010-04-08 18:23:24 +00:00
|
|
|
schedule_powercycle_fixed(t, RTIMER_NOW() + CCA_SLEEP_TIME);
|
2010-04-04 21:01:24 +00:00
|
|
|
/* COOJA_DEBUG_STR("yield\n");*/
|
2010-02-18 21:26:15 +00:00
|
|
|
PT_YIELD(&pt);
|
|
|
|
}
|
2010-03-14 22:59:23 +00:00
|
|
|
|
2010-02-18 21:26:15 +00:00
|
|
|
if(packet_seen) {
|
2010-02-23 18:49:05 +00:00
|
|
|
static rtimer_clock_t start;
|
|
|
|
static uint8_t silence_periods, periods;
|
|
|
|
start = RTIMER_NOW();
|
2010-03-14 22:59:23 +00:00
|
|
|
|
2010-02-23 18:49:05 +00:00
|
|
|
periods = silence_periods = 0;
|
|
|
|
while(we_are_sending == 0 && radio_is_on &&
|
2010-03-09 13:19:05 +00:00
|
|
|
RTIMER_CLOCK_LT(RTIMER_NOW(), (start + LISTEN_TIME_AFTER_PACKET_DETECTED))) {
|
2010-03-14 22:59:23 +00:00
|
|
|
|
|
|
|
/* Check for a number of consecutive periods of
|
|
|
|
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 10:02:04 +00:00
|
|
|
snooping. */
|
2010-02-23 18:49:05 +00:00
|
|
|
if(NETSTACK_RADIO.channel_clear()) {
|
|
|
|
++silence_periods;
|
|
|
|
} else {
|
|
|
|
silence_periods = 0;
|
|
|
|
}
|
|
|
|
|
2010-03-14 22:59:23 +00:00
|
|
|
++periods;
|
|
|
|
|
2010-02-23 18:49:05 +00:00
|
|
|
if(NETSTACK_RADIO.receiving_packet()) {
|
|
|
|
silence_periods = 0;
|
|
|
|
}
|
2010-03-30 23:01:32 +00:00
|
|
|
if(silence_periods > MAX_SILENCE_PERIODS) {
|
2010-04-20 11:41:16 +00:00
|
|
|
LEDS_ON(LEDS_RED);
|
2010-02-23 18:49:05 +00:00
|
|
|
powercycle_turn_radio_off();
|
|
|
|
#if CONTIKIMAC_CONF_COMPOWER
|
|
|
|
compower_accumulate(&compower_idle_activity);
|
|
|
|
#endif /* CONTIKIMAC_CONF_COMPOWER */
|
2010-04-20 11:41:16 +00:00
|
|
|
LEDS_OFF(LEDS_RED);
|
2010-02-23 18:49:05 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-04-03 13:28:30 +00:00
|
|
|
#if 1
|
|
|
|
if(periods > MAX_NONACTIVITY_PERIODIC && !(NETSTACK_RADIO.receiving_packet() ||
|
|
|
|
NETSTACK_RADIO.pending_packet())) {
|
2010-04-20 11:41:16 +00:00
|
|
|
LEDS_ON(LEDS_GREEN);
|
2010-02-23 18:49:05 +00:00
|
|
|
powercycle_turn_radio_off();
|
|
|
|
#if CONTIKIMAC_CONF_COMPOWER
|
|
|
|
compower_accumulate(&compower_idle_activity);
|
|
|
|
#endif /* CONTIKIMAC_CONF_COMPOWER */
|
2010-03-14 22:59:23 +00:00
|
|
|
|
2010-04-20 11:41:16 +00:00
|
|
|
LEDS_OFF(LEDS_GREEN);
|
2010-02-23 18:49:05 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-04-03 13:28:30 +00:00
|
|
|
#endif /* 0 */
|
2010-02-23 18:49:05 +00:00
|
|
|
if(NETSTACK_RADIO.pending_packet()) {
|
|
|
|
break;
|
|
|
|
}
|
2010-03-14 22:59:23 +00:00
|
|
|
|
2010-02-23 18:49:05 +00:00
|
|
|
schedule_powercycle(t, CCA_CHECK_TIME + CCA_SLEEP_TIME);
|
2010-04-20 11:41:16 +00:00
|
|
|
LEDS_ON(LEDS_BLUE);
|
2010-02-23 18:49:05 +00:00
|
|
|
PT_YIELD(&pt);
|
2010-04-20 11:41:16 +00:00
|
|
|
LEDS_OFF(LEDS_BLUE);
|
2010-02-23 18:49:05 +00:00
|
|
|
}
|
2010-03-16 18:11:13 +00:00
|
|
|
if(radio_is_on && !(NETSTACK_RADIO.receiving_packet() ||
|
2010-03-09 13:19:05 +00:00
|
|
|
NETSTACK_RADIO.pending_packet())) {
|
2010-04-20 11:41:16 +00:00
|
|
|
LEDS_ON(LEDS_RED + LEDS_GREEN);
|
2010-02-23 18:49:05 +00:00
|
|
|
powercycle_turn_radio_off();
|
|
|
|
#if CONTIKIMAC_CONF_COMPOWER
|
|
|
|
compower_accumulate(&compower_idle_activity);
|
|
|
|
#endif /* CONTIKIMAC_CONF_COMPOWER */
|
2010-04-20 11:41:16 +00:00
|
|
|
LEDS_OFF(LEDS_RED + LEDS_GREEN);
|
2010-02-23 18:49:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#if CONTIKIMAC_CONF_COMPOWER
|
|
|
|
compower_accumulate(&compower_idle_activity);
|
|
|
|
#endif /* CONTIKIMAC_CONF_COMPOWER */
|
|
|
|
}
|
2010-04-04 07:49:30 +00:00
|
|
|
} while(is_snooping &&
|
|
|
|
RTIMER_CLOCK_LT(RTIMER_NOW() - cycle_start, CYCLE_TIME - CHECK_TIME));
|
2010-04-03 13:28:30 +00:00
|
|
|
|
|
|
|
if(is_snooping) {
|
2010-04-20 11:41:16 +00:00
|
|
|
LEDS_ON(LEDS_RED);
|
2010-04-03 13:28:30 +00:00
|
|
|
}
|
2010-04-04 07:49:30 +00:00
|
|
|
if(RTIMER_CLOCK_LT(RTIMER_NOW() - cycle_start, CYCLE_TIME)) {
|
2010-04-04 12:28:29 +00:00
|
|
|
/* schedule_powercycle(t, CYCLE_TIME - (RTIMER_NOW() - cycle_start));*/
|
|
|
|
schedule_powercycle_fixed(t, CYCLE_TIME + cycle_start);
|
2010-03-29 21:51:36 +00:00
|
|
|
/* printf("cycle_start 0x%02x now 0x%02x wait 0x%02x\n",
|
|
|
|
cycle_start, RTIMER_NOW(), CYCLE_TIME - (RTIMER_NOW() - cycle_start));*/
|
2010-02-18 21:26:15 +00:00
|
|
|
PT_YIELD(&pt);
|
|
|
|
}
|
2010-04-20 11:41:16 +00:00
|
|
|
LEDS_OFF(LEDS_RED);
|
2010-02-18 21:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PT_END(&pt);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#if CONTIKIMAC_CONF_ANNOUNCEMENTS
|
|
|
|
static int
|
2010-03-16 18:11:13 +00:00
|
|
|
parse_announcements(void)
|
2010-02-18 21:26:15 +00:00
|
|
|
{
|
|
|
|
/* Parse incoming announcements */
|
|
|
|
struct announcement_msg adata;
|
2010-03-16 18:11:13 +00:00
|
|
|
const rimeaddr_t *from;
|
2010-02-18 21:26:15 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
memcpy(&adata, packetbuf_dataptr(),
|
|
|
|
MIN(packetbuf_datalen(), sizeof(adata)));
|
2010-03-16 18:11:13 +00:00
|
|
|
from = packetbuf_addr(PACKETBUF_ADDR_SENDER);
|
2010-02-18 21:26:15 +00:00
|
|
|
|
|
|
|
/* printf("%d.%d: probe from %d.%d with %d announcements\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
2010-03-16 18:11:13 +00:00
|
|
|
from->u8[0], from->u8[1], adata.num); */
|
2010-02-18 21:26:15 +00:00
|
|
|
/* for(i = 0; i < packetbuf_datalen(); ++i) {
|
|
|
|
printf("%02x ", ((uint8_t *)packetbuf_dataptr())[i]);
|
|
|
|
}
|
|
|
|
printf("\n"); */
|
|
|
|
|
2010-03-31 20:27:15 +00:00
|
|
|
if(adata.num / sizeof(struct announcement_data) > sizeof(struct announcement_msg)) {
|
|
|
|
/* Sanity check. The number of announcements is too large -
|
|
|
|
corrupt packet has been received. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-18 21:26:15 +00:00
|
|
|
for(i = 0; i < adata.num; ++i) {
|
2010-03-16 18:11:13 +00:00
|
|
|
/* printf("%d.%d: announcement %d: %d\n",
|
2010-02-18 21:26:15 +00:00
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
2010-03-16 18:11:13 +00:00
|
|
|
adata.data[i].id,
|
|
|
|
adata.data[i].value); */
|
2010-02-18 21:26:15 +00:00
|
|
|
|
|
|
|
announcement_heard(from, adata.data[i].id, adata.data[i].value);
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
format_announcement(char *hdr)
|
|
|
|
{
|
|
|
|
struct announcement_msg adata;
|
|
|
|
struct announcement *a;
|
|
|
|
|
|
|
|
/* Construct the announcements */
|
|
|
|
/* adata = (struct announcement_msg *)hdr; */
|
|
|
|
|
2010-03-16 18:11:13 +00:00
|
|
|
adata.announcement_magic[0] = ANNOUNCEMENT_MAGIC1;
|
|
|
|
adata.announcement_magic[1] = ANNOUNCEMENT_MAGIC2;
|
2010-02-18 21:26:15 +00:00
|
|
|
adata.num = 0;
|
|
|
|
for(a = announcement_list();
|
2010-04-03 13:28:30 +00:00
|
|
|
a != NULL && adata.num < ANNOUNCEMENT_MAX;
|
2010-03-29 21:51:36 +00:00
|
|
|
a = a->next) {
|
2010-04-03 13:28:30 +00:00
|
|
|
if(a->has_value) {
|
|
|
|
adata.data[adata.num].id = a->id;
|
|
|
|
adata.data[adata.num].value = a->value;
|
|
|
|
adata.num++;
|
|
|
|
}
|
2010-02-18 21:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(hdr, &adata, sizeof(struct announcement_msg));
|
|
|
|
|
|
|
|
if(adata.num > 0) {
|
|
|
|
return ANNOUNCEMENT_MSG_HEADERLEN +
|
|
|
|
sizeof(struct announcement_data) * adata.num;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONTIKIMAC_CONF_ANNOUNCEMENTS */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2010-04-06 11:57:43 +00: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
|
2010-02-28 20:19:47 +00:00
|
|
|
send_packet(mac_callback_t mac_callback, void *mac_callback_ptr)
|
2010-02-18 21:26:15 +00:00
|
|
|
{
|
|
|
|
rtimer_clock_t t0;
|
|
|
|
rtimer_clock_t t;
|
2010-03-14 22:59:23 +00:00
|
|
|
rtimer_clock_t encounter_time = 0, last_transmission_time = 0;
|
|
|
|
uint8_t first_transmission = 1;
|
2010-02-18 21:26:15 +00:00
|
|
|
int strobes;
|
2010-03-14 22:59:23 +00:00
|
|
|
uint8_t got_strobe_ack = 0;
|
2010-03-29 21:51:36 +00:00
|
|
|
int hdrlen, len;
|
2010-03-14 22:59:23 +00:00
|
|
|
uint8_t is_broadcast = 0;
|
|
|
|
uint8_t is_reliable = 0;
|
|
|
|
uint8_t is_known_receiver = 0;
|
2010-02-18 21:26:15 +00:00
|
|
|
uint8_t collisions;
|
|
|
|
int transmit_len;
|
|
|
|
int i;
|
2010-03-14 22:59:23 +00:00
|
|
|
int ret;
|
2010-04-26 17:46:21 +00:00
|
|
|
#if WITH_CONTIKIMAC_HEADER
|
|
|
|
struct hdr *chdr;
|
|
|
|
#endif /* WITH_CONTIKIMAC_HEADER */
|
2010-02-18 21:26:15 +00:00
|
|
|
|
2010-03-09 13:19:05 +00:00
|
|
|
if(packetbuf_totlen() == 0) {
|
|
|
|
PRINTF("contikimac: send_packet data len 0\n");
|
|
|
|
return MAC_TX_ERR_FATAL;
|
|
|
|
}
|
2010-03-29 21:51:36 +00:00
|
|
|
|
|
|
|
|
2010-02-18 21:26:15 +00:00
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &rimeaddr_node_addr);
|
|
|
|
if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &rimeaddr_null)) {
|
|
|
|
is_broadcast = 1;
|
|
|
|
PRINTDEBUG("contikimac: send broadcast\n");
|
2010-04-06 11:57:43 +00:00
|
|
|
|
|
|
|
if(broadcast_rate_drop()) {
|
|
|
|
return MAC_TX_COLLISION;
|
|
|
|
}
|
2010-02-18 21:26:15 +00:00
|
|
|
} else {
|
|
|
|
#if UIP_CONF_IPV6
|
|
|
|
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]);
|
2010-03-14 22:59:23 +00:00
|
|
|
#else /* UIP_CONF_IPV6 */
|
2010-02-18 21:26:15 +00:00
|
|
|
PRINTDEBUG("contikimac: send unicast to %u.%u\n",
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[0],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[1]);
|
|
|
|
#endif /* UIP_CONF_IPV6 */
|
|
|
|
}
|
|
|
|
is_reliable = packetbuf_attr(PACKETBUF_ATTR_RELIABLE) ||
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_ERELIABLE);
|
2010-03-29 21:51:36 +00:00
|
|
|
|
2010-03-14 22:59:23 +00:00
|
|
|
if(WITH_STREAMING) {
|
|
|
|
if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
|
|
|
|
PACKETBUF_ATTR_PACKET_TYPE_STREAM) {
|
|
|
|
if(rimeaddr_cmp(&is_streaming_to, &rimeaddr_null)) {
|
|
|
|
rimeaddr_copy(&is_streaming_to,
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
|
|
|
|
} else if(!rimeaddr_cmp
|
|
|
|
(&is_streaming_to, packetbuf_addr(PACKETBUF_ADDR_RECEIVER))) {
|
|
|
|
rimeaddr_copy(&is_streaming_to_too,
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
|
|
|
|
}
|
|
|
|
stream_until = RTIMER_NOW() + DEFAULT_STREAM_TIME;
|
2010-03-16 18:11:13 +00:00
|
|
|
is_streaming = 1;
|
2010-04-04 07:49:30 +00:00
|
|
|
} else {
|
|
|
|
is_streaming = 0;
|
2010-03-14 22:59:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-04 21:01:24 +00:00
|
|
|
if(is_streaming) {
|
2010-04-03 13:28:30 +00:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_PENDING, 1);
|
|
|
|
}
|
2010-04-30 07:25:51 +00:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAC_ACK, 1);
|
2010-04-26 17:46:21 +00:00
|
|
|
|
|
|
|
#if WITH_CONTIKIMAC_HEADER
|
|
|
|
hdrlen = packetbuf_totlen();
|
|
|
|
if(packetbuf_hdralloc(sizeof(struct hdr)) == 0) {
|
|
|
|
/* Failed to allocate space for contikimac header */
|
|
|
|
PRINTF("contikimac: send failed, too large header\n");
|
|
|
|
return MAC_TX_ERR_FATAL;
|
|
|
|
}
|
|
|
|
chdr = packetbuf_hdrptr();
|
|
|
|
chdr->id = CONTIKIMAC_ID;
|
|
|
|
chdr->len = hdrlen;
|
|
|
|
|
2010-03-29 21:51:36 +00:00
|
|
|
/* Create the MAC header for the data packet. */
|
|
|
|
hdrlen = NETSTACK_FRAMER.create();
|
|
|
|
if(hdrlen == 0) {
|
|
|
|
/* Failed to send */
|
|
|
|
PRINTF("contikimac: send failed, too large header\n");
|
2010-04-26 17:46:21 +00:00
|
|
|
packetbuf_hdr_remove(sizeof(struct hdr));
|
2010-03-29 21:51:36 +00:00
|
|
|
return MAC_TX_ERR_FATAL;
|
|
|
|
}
|
2010-04-26 17:46:21 +00:00
|
|
|
hdrlen += sizeof(struct hdr);
|
|
|
|
#else /* WITH_CONTIKIMAC_HEADER */
|
|
|
|
/* Create the MAC header for the data packet. */
|
|
|
|
hdrlen = NETSTACK_FRAMER.create();
|
|
|
|
if(hdrlen == 0) {
|
|
|
|
/* Failed to send */
|
|
|
|
PRINTF("contikimac: send failed, too large header\n");
|
|
|
|
return MAC_TX_ERR_FATAL;
|
|
|
|
}
|
|
|
|
#endif /* WITH_CONTIKIMAC_HEADER */
|
2010-03-29 21:51:36 +00:00
|
|
|
|
2010-04-26 17:46:21 +00:00
|
|
|
/* Make sure that the packet is longer or equal to the shortest
|
2010-03-29 21:51:36 +00:00
|
|
|
packet length. */
|
2010-04-26 17:46:21 +00:00
|
|
|
transmit_len = packetbuf_totlen();
|
|
|
|
if(transmit_len < SHORTEST_PACKET_SIZE) {
|
|
|
|
#if 0
|
|
|
|
/* Pad with zeroes */
|
|
|
|
uint8_t *ptr;
|
|
|
|
ptr = packetbuf_dataptr();
|
|
|
|
memset(ptr + packetbuf_datalen(), 0, SHORTEST_PACKET_SIZE - packetbuf_totlen());
|
|
|
|
#endif
|
|
|
|
|
2010-03-29 21:51:36 +00:00
|
|
|
PRINTF("contikimac: shorter than shortest (%d)\n", packetbuf_totlen());
|
2010-04-26 17:46:21 +00:00
|
|
|
transmit_len = SHORTEST_PACKET_SIZE;
|
2010-03-29 21:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
packetbuf_compact();
|
|
|
|
|
2010-04-26 17:46:21 +00:00
|
|
|
NETSTACK_RADIO.prepare(packetbuf_hdrptr(), transmit_len);
|
2010-03-29 21:51:36 +00:00
|
|
|
|
|
|
|
/* Remove the MAC-layer header since it will be recreated next time around. */
|
|
|
|
packetbuf_hdr_remove(hdrlen);
|
|
|
|
|
2010-03-14 22:59:23 +00:00
|
|
|
if(!is_broadcast && !is_streaming) {
|
|
|
|
#if WITH_PHASE_OPTIMIZATION
|
|
|
|
if(WITH_ACK_OPTIMIZATION) {
|
|
|
|
/* Wait until the receiver is expected to be awake */
|
|
|
|
if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) !=
|
|
|
|
PACKETBUF_ATTR_PACKET_TYPE_ACK) {
|
|
|
|
|
|
|
|
ret = phase_wait(&phase_list, packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
2010-03-29 21:51:36 +00:00
|
|
|
CYCLE_TIME, GUARD_TIME,
|
2010-03-14 22:59:23 +00:00
|
|
|
mac_callback, mac_callback_ptr);
|
|
|
|
if(ret == PHASE_DEFERRED) {
|
|
|
|
return MAC_TX_DEFERRED;
|
|
|
|
}
|
|
|
|
if(ret != PHASE_UNKNOWN) {
|
|
|
|
is_known_receiver = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = phase_wait(&phase_list, packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
2010-03-29 21:51:36 +00:00
|
|
|
CYCLE_TIME, GUARD_TIME,
|
2010-03-14 22:59:23 +00:00
|
|
|
mac_callback, mac_callback_ptr);
|
|
|
|
if(ret == PHASE_DEFERRED) {
|
|
|
|
return MAC_TX_DEFERRED;
|
|
|
|
}
|
|
|
|
if(ret != PHASE_UNKNOWN) {
|
|
|
|
is_known_receiver = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* WITH_PHASE_OPTIMIZATION */
|
|
|
|
}
|
|
|
|
|
2010-02-18 21:26:15 +00: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 08:33:21 +00:00
|
|
|
PRINTF("contikimac: collision receiving %d, pending %d\n",
|
|
|
|
NETSTACK_RADIO.receiving_packet(), NETSTACK_RADIO.pending_packet());
|
2010-02-18 21:26:15 +00:00
|
|
|
return MAC_TX_COLLISION;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) !=
|
|
|
|
PACKETBUF_ATTR_PACKET_TYPE_ACK && is_streaming == 0) {
|
|
|
|
/* Check if there are any transmissions by others. */
|
|
|
|
for(i = 0; i < CCA_COUNT_MAX; ++i) {
|
|
|
|
t0 = RTIMER_NOW();
|
|
|
|
on();
|
|
|
|
#if NURTIMER
|
|
|
|
while(RTIMER_CLOCK_LT(t0, RTIMER_NOW(), t0 + CCA_CHECK_TIME));
|
|
|
|
#else
|
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + CCA_CHECK_TIME)) { }
|
|
|
|
#endif
|
|
|
|
if(NETSTACK_RADIO.channel_clear() == 0) {
|
|
|
|
collisions++;
|
|
|
|
off();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
off();
|
|
|
|
#if NURTIMER
|
|
|
|
while(RTIMER_CLOCK_LT(t0, RTIMER_NOW(), t0 + CCA_SLEEP_TIME + CCA_CHECK_TIME));
|
|
|
|
#else
|
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + CCA_SLEEP_TIME + CCA_CHECK_TIME)) { }
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(collisions > 0) {
|
|
|
|
we_are_sending = 0;
|
2010-02-23 18:49:05 +00:00
|
|
|
off();
|
2010-03-09 13:19:05 +00:00
|
|
|
PRINTF("contikimac: collisions before sending\n");
|
2010-02-18 21:26:15 +00:00
|
|
|
return MAC_TX_COLLISION;
|
|
|
|
}
|
|
|
|
|
2010-03-14 22:59:23 +00:00
|
|
|
if(!is_broadcast) {
|
|
|
|
on();
|
|
|
|
}
|
|
|
|
|
2010-10-03 20:39:24 +00:00
|
|
|
watchdog_periodic();
|
2010-02-18 21:26:15 +00:00
|
|
|
t0 = RTIMER_NOW();
|
|
|
|
t = RTIMER_NOW();
|
|
|
|
#if NURTIMER
|
|
|
|
for(strobes = 0, collisions = 0;
|
|
|
|
got_strobe_ack == 0 && collisions == 0 &&
|
|
|
|
RTIMER_CLOCK_LT(t0, RTIMER_NOW(), t0 + STROBE_TIME); strobes++) {
|
|
|
|
#else
|
|
|
|
for(strobes = 0, collisions = 0;
|
|
|
|
got_strobe_ack == 0 && collisions == 0 &&
|
|
|
|
RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + STROBE_TIME); strobes++) {
|
|
|
|
#endif
|
2010-03-19 13:24:58 +00:00
|
|
|
|
|
|
|
watchdog_periodic();
|
2010-03-14 22:59:23 +00:00
|
|
|
|
2010-04-04 07:49:30 +00:00
|
|
|
if(is_known_receiver && !RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + MAX_PHASE_STROBE_TIME)) {
|
2010-03-14 22:59:23 +00:00
|
|
|
break;
|
2010-04-04 07:49:30 +00:00
|
|
|
}
|
2010-03-14 22:59:23 +00:00
|
|
|
|
2010-02-18 21:26:15 +00:00
|
|
|
len = 0;
|
|
|
|
|
|
|
|
t = RTIMER_NOW();
|
|
|
|
|
2010-03-14 22:59:23 +00:00
|
|
|
{
|
2010-02-18 21:26:15 +00:00
|
|
|
rtimer_clock_t wt;
|
|
|
|
rtimer_clock_t now = RTIMER_NOW();
|
2010-04-04 07:49:30 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = NETSTACK_RADIO.transmit(transmit_len);
|
|
|
|
|
2010-02-18 21:26:15 +00:00
|
|
|
wt = RTIMER_NOW();
|
|
|
|
#if NURTIMER
|
2010-03-14 12:13:54 +00:00
|
|
|
while(RTIMER_CLOCK_LT(wt, RTIMER_NOW(), wt + INTER_PACKET_INTERVAL));
|
2010-02-18 21:26:15 +00:00
|
|
|
#else
|
2010-03-14 12:13:54 +00:00
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + INTER_PACKET_INTERVAL)) { }
|
2010-02-18 21:26:15 +00:00
|
|
|
#endif
|
2010-02-28 08:33:21 +00:00
|
|
|
if(!is_broadcast && (NETSTACK_RADIO.receiving_packet() ||
|
|
|
|
NETSTACK_RADIO.pending_packet() ||
|
|
|
|
NETSTACK_RADIO.channel_clear() == 0)) {
|
2010-02-18 21:26:15 +00:00
|
|
|
uint8_t ackbuf[ACK_LEN];
|
|
|
|
wt = RTIMER_NOW();
|
|
|
|
#if NURTIMER
|
2010-03-14 12:13:54 +00:00
|
|
|
while(RTIMER_CLOCK_LT(wt, RTIMER_NOW(), wt + AFTER_ACK_DETECTECT_WAIT_TIME));
|
2010-02-18 21:26:15 +00:00
|
|
|
#else
|
2010-03-14 12:13:54 +00:00
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + AFTER_ACK_DETECTECT_WAIT_TIME)) { }
|
2010-02-18 21:26:15 +00:00
|
|
|
#endif
|
2010-02-28 08:33:21 +00:00
|
|
|
len = NETSTACK_RADIO.read(ackbuf, ACK_LEN);
|
|
|
|
if(len == ACK_LEN) {
|
|
|
|
got_strobe_ack = 1;
|
2010-03-31 11:54:38 +00:00
|
|
|
// encounter_time = last_transmission_time;
|
|
|
|
encounter_time = now;
|
2010-03-14 22:59:23 +00:00
|
|
|
break;
|
2010-02-18 21:26:15 +00:00
|
|
|
} else {
|
2010-03-09 13:19:05 +00:00
|
|
|
PRINTF("contikimac: collisions while sending\n");
|
2010-02-28 08:33:21 +00:00
|
|
|
collisions++;
|
2010-02-18 21:26:15 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-14 22:59:23 +00:00
|
|
|
last_transmission_time = now;
|
|
|
|
first_transmission = 0;
|
2010-02-18 21:26:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-14 22:59:23 +00:00
|
|
|
if(WITH_ACK_OPTIMIZATION) {
|
|
|
|
/* If we have received the strobe ACK, and we are sending a packet
|
|
|
|
that will need an upper layer ACK (as signified by the
|
|
|
|
PACKETBUF_ATTR_RELIABLE packet attribute), we keep the radio on. */
|
|
|
|
if(got_strobe_ack && is_reliable) {
|
|
|
|
on(); /* Wait for ACK packet */
|
|
|
|
} else {
|
|
|
|
off();
|
|
|
|
}
|
2010-02-18 21:26:15 +00:00
|
|
|
} else {
|
|
|
|
off();
|
|
|
|
}
|
|
|
|
|
2010-03-14 22:59:23 +00: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 21:26:15 +00: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 */
|
|
|
|
|
|
|
|
we_are_sending = 0;
|
|
|
|
|
2010-03-14 22:59:23 +00: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 18:49:05 +00:00
|
|
|
if(collisions > 0) {
|
2010-03-14 22:59:23 +00:00
|
|
|
ret = MAC_TX_COLLISION;
|
2010-10-03 20:39:24 +00:00
|
|
|
} else if(!is_broadcast && !got_strobe_ack) {
|
2010-03-14 22:59:23 +00:00
|
|
|
ret = MAC_TX_NOACK;
|
2010-02-23 18:49:05 +00:00
|
|
|
} else {
|
2010-03-14 22:59:23 +00:00
|
|
|
ret = MAC_TX_OK;
|
2010-02-23 18:49:05 +00:00
|
|
|
}
|
2010-03-14 22:59:23 +00:00
|
|
|
|
|
|
|
#if WITH_PHASE_OPTIMIZATION
|
2010-04-04 07:49:30 +00:00
|
|
|
/* if(!first_transmission)*/ {
|
2010-03-29 21:51:36 +00:00
|
|
|
|
|
|
|
/* COOJA_DEBUG_PRINTF("first phase 0x%02x\n", encounter_time % CYCLE_TIME);*/
|
|
|
|
|
2010-03-14 22:59:23 +00:00
|
|
|
if(WITH_ACK_OPTIMIZATION) {
|
|
|
|
if(collisions == 0 && packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) !=
|
|
|
|
PACKETBUF_ATTR_PACKET_TYPE_ACK && is_streaming == 0) {
|
|
|
|
phase_update(&phase_list, packetbuf_addr(PACKETBUF_ADDR_RECEIVER), encounter_time,
|
|
|
|
ret);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(collisions == 0 && is_streaming == 0) {
|
|
|
|
phase_update(&phase_list, packetbuf_addr(PACKETBUF_ADDR_RECEIVER), encounter_time,
|
|
|
|
ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* WITH_PHASE_OPTIMIZATION */
|
|
|
|
|
|
|
|
return ret;
|
2010-02-18 21:26:15 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
qsend_packet(mac_callback_t sent, void *ptr)
|
|
|
|
{
|
2010-02-28 20:19:47 +00:00
|
|
|
int ret = send_packet(sent, ptr);
|
2010-03-09 20:37:02 +00:00
|
|
|
if(ret != MAC_TX_DEFERRED) {
|
|
|
|
mac_call_sent_callback(sent, ptr, ret, 1);
|
|
|
|
}
|
2010-02-18 21:26:15 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
input_packet(void)
|
|
|
|
{
|
|
|
|
/* We have received the packet, so we can go back to being
|
|
|
|
asleep. */
|
|
|
|
off();
|
|
|
|
|
2010-03-29 21:51:36 +00:00
|
|
|
/* printf("cycle_start 0x%02x 0x%02x\n", cycle_start, cycle_start % CYCLE_TIME);*/
|
|
|
|
|
2010-03-16 18:11:13 +00:00
|
|
|
|
2010-03-01 13:30:21 +00:00
|
|
|
if(packetbuf_totlen() > 0 && NETSTACK_FRAMER.parse()) {
|
2010-04-26 17:46:21 +00:00
|
|
|
|
|
|
|
#if WITH_CONTIKIMAC_HEADER
|
|
|
|
struct hdr *chdr;
|
|
|
|
chdr = packetbuf_dataptr();
|
|
|
|
if(chdr->id != CONTIKIMAC_ID) {
|
|
|
|
PRINTF("contikimac: failed to parse hdr (%u)\n", packetbuf_totlen());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
packetbuf_hdrreduce(sizeof(struct hdr));
|
|
|
|
packetbuf_set_datalen(chdr->len);
|
|
|
|
#endif /* WITH_CONTIKIMAC_HEADER */
|
|
|
|
|
2010-02-23 18:49:05 +00:00
|
|
|
if(packetbuf_datalen() > 0 &&
|
|
|
|
packetbuf_totlen() > 0 &&
|
|
|
|
(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
|
|
|
&rimeaddr_node_addr) ||
|
|
|
|
rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
|
|
|
&rimeaddr_null))) {
|
|
|
|
/* This is a regular packet that is destined to us or to the
|
|
|
|
broadcast address. */
|
2010-02-18 21:26:15 +00:00
|
|
|
|
2010-03-16 18:11:13 +00:00
|
|
|
#if CONTIKIMAC_CONF_ANNOUNCEMENTS
|
|
|
|
{
|
|
|
|
struct announcement_msg *hdr = packetbuf_dataptr();
|
|
|
|
uint8_t magic[2];
|
|
|
|
memcpy(magic, hdr->announcement_magic, 2);
|
|
|
|
if(magic[0] == ANNOUNCEMENT_MAGIC1 &&
|
|
|
|
magic[1] == ANNOUNCEMENT_MAGIC2) {
|
|
|
|
parse_announcements();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONTIKIMAC_CONF_ANNOUNCEMENTS */
|
2010-04-03 13:28:30 +00:00
|
|
|
|
|
|
|
#if WITH_PHASE_OPTIMIZATION
|
|
|
|
/* If the sender has set its pending flag, it has its radio
|
|
|
|
turned on and we should drop the phase estimation that we
|
|
|
|
have from before. */
|
|
|
|
if(packetbuf_attr(PACKETBUF_ATTR_PENDING)) {
|
|
|
|
phase_remove(&phase_list, packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
|
|
|
}
|
|
|
|
#endif /* WITH_PHASE_OPTIMIZATION */
|
2010-04-05 19:28:07 +00:00
|
|
|
|
|
|
|
/* Check for duplicate packet by comparing the sequence number
|
2010-04-08 09:32:56 +00:00
|
|
|
of the incoming packet with the last few ones we saw. */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < MAX_SEQNOS; ++i) {
|
|
|
|
if(packetbuf_attr(PACKETBUF_ATTR_PACKET_ID) == received_seqnos[i].seqno &&
|
|
|
|
rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER),
|
|
|
|
&received_seqnos[i].sender)) {
|
|
|
|
/* Drop the packet. */
|
|
|
|
/* printf("Drop duplicate ContikiMAC layer packet\n");*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i = MAX_SEQNOS - 1; i > 0; --i) {
|
|
|
|
memcpy(&received_seqnos[i], &received_seqnos[i - 1],
|
|
|
|
sizeof(struct seqno));
|
|
|
|
}
|
|
|
|
received_seqnos[0].seqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
|
|
|
|
rimeaddr_copy(&received_seqnos[0].sender,
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
2010-04-05 19:28:07 +00:00
|
|
|
}
|
|
|
|
|
2010-02-18 21:26:15 +00:00
|
|
|
#if CONTIKIMAC_CONF_COMPOWER
|
2010-02-23 18:49:05 +00: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 21:26:15 +00:00
|
|
|
#endif /* CONTIKIMAC_CONF_COMPOWER */
|
|
|
|
|
2010-03-16 18:11:13 +00:00
|
|
|
PRINTDEBUG("contikimac: data (%u)\n", packetbuf_datalen());
|
2010-02-23 18:49:05 +00:00
|
|
|
NETSTACK_MAC.input();
|
|
|
|
return;
|
2010-02-18 21:26:15 +00:00
|
|
|
} else {
|
2010-02-23 18:49:05 +00:00
|
|
|
PRINTDEBUG("contikimac: data not for us\n");
|
2010-02-18 21:26:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PRINTF("contikimac: failed to parse (%u)\n", packetbuf_totlen());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#if CONTIKIMAC_CONF_ANNOUNCEMENTS
|
|
|
|
static void
|
|
|
|
send_announcement(void *ptr)
|
|
|
|
{
|
|
|
|
int announcement_len;
|
2010-04-26 17:46:21 +00:00
|
|
|
int transmit_len;
|
|
|
|
#if WITH_CONTIKIMAC_HEADER
|
2010-10-03 20:39:24 +00:00
|
|
|
struct hdr *chdr;
|
2010-04-26 17:46:21 +00:00
|
|
|
#endif /* WITH_CONTIKIMAC_HEADER */
|
2010-10-03 20:39:24 +00:00
|
|
|
|
2010-02-18 21:26:15 +00:00
|
|
|
/* Set up the probe header. */
|
|
|
|
packetbuf_clear();
|
2010-03-16 18:11:13 +00:00
|
|
|
announcement_len = format_announcement(packetbuf_dataptr());
|
2010-02-18 21:26:15 +00:00
|
|
|
|
|
|
|
if(announcement_len > 0) {
|
2010-03-16 18:11:13 +00:00
|
|
|
packetbuf_set_datalen(announcement_len);
|
2010-02-18 21:26:15 +00:00
|
|
|
|
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &rimeaddr_node_addr);
|
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, &rimeaddr_null);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_RADIO_TXPOWER,
|
|
|
|
announcement_radio_txpower);
|
2010-04-26 17:46:21 +00:00
|
|
|
#if WITH_CONTIKIMAC_HEADER
|
|
|
|
transmit_len = packetbuf_totlen();
|
|
|
|
if(packetbuf_hdralloc(sizeof(struct hdr)) == 0) {
|
|
|
|
/* Failed to allocate space for contikimac header */
|
|
|
|
PRINTF("contikimac: send announcement failed, too large header\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chdr = packetbuf_hdrptr();
|
|
|
|
chdr->id = CONTIKIMAC_ID;
|
|
|
|
chdr->len = transmit_len;
|
|
|
|
#endif /* WITH_CONTIKIMAC_HEADER */
|
|
|
|
|
2010-03-01 13:30:21 +00:00
|
|
|
if(NETSTACK_FRAMER.create()) {
|
2010-03-16 18:11:13 +00:00
|
|
|
rtimer_clock_t t;
|
|
|
|
int i, collisions;
|
2010-02-18 21:26:15 +00:00
|
|
|
we_are_sending = 1;
|
2010-03-16 18:11:13 +00:00
|
|
|
|
2010-03-29 21:51:36 +00:00
|
|
|
/* Make sure that the packet is longer or equal to the shorest
|
|
|
|
packet length. */
|
2010-04-26 17:46:21 +00:00
|
|
|
transmit_len = packetbuf_totlen();
|
|
|
|
if(transmit_len < SHORTEST_PACKET_SIZE) {
|
|
|
|
#if 0
|
|
|
|
/* Pad with zeroes */
|
|
|
|
uint8_t *ptr;
|
|
|
|
ptr = packetbuf_dataptr();
|
2010-04-26 17:55:11 +00:00
|
|
|
memset(ptr + packetbuf_datalen(), 0, SHORTEST_PACKET_SIZE - transmit_len);
|
2010-04-26 17:46:21 +00:00
|
|
|
#endif
|
|
|
|
|
2010-03-29 21:51:36 +00:00
|
|
|
PRINTF("contikimac: shorter than shortest (%d)\n", packetbuf_totlen());
|
2010-04-26 17:55:11 +00:00
|
|
|
transmit_len = SHORTEST_PACKET_SIZE;
|
2010-03-29 21:51:36 +00:00
|
|
|
}
|
|
|
|
|
2010-03-16 18:11:13 +00:00
|
|
|
collisions = 0;
|
|
|
|
/* Check for collisions */
|
|
|
|
for(i = 0; i < CCA_COUNT_MAX; ++i) {
|
|
|
|
t = RTIMER_NOW();
|
|
|
|
on();
|
|
|
|
#if NURTIMER
|
|
|
|
while(RTIMER_CLOCK_LT(t, RTIMER_NOW(), t + CCA_CHECK_TIME));
|
|
|
|
#else
|
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), t + CCA_CHECK_TIME));
|
|
|
|
#endif
|
|
|
|
if(NETSTACK_RADIO.channel_clear() == 0) {
|
|
|
|
collisions++;
|
|
|
|
off();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
off();
|
|
|
|
#if NURTIMER
|
|
|
|
while(RTIMER_CLOCK_LT(t0, RTIMER_NOW(), t + CCA_SLEEP_TIME + CCA_CHECK_TIME));
|
|
|
|
#else
|
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), t + CCA_SLEEP_TIME + CCA_CHECK_TIME)) { }
|
|
|
|
#endif
|
|
|
|
}
|
2010-04-03 13:28:30 +00:00
|
|
|
|
2010-03-16 18:11:13 +00:00
|
|
|
if(collisions == 0) {
|
|
|
|
|
2010-04-26 17:46:21 +00:00
|
|
|
NETSTACK_RADIO.prepare(packetbuf_hdrptr(), transmit_len);
|
2010-03-16 18:11:13 +00:00
|
|
|
|
2010-04-26 17:46:21 +00:00
|
|
|
NETSTACK_RADIO.transmit(transmit_len);
|
2010-03-16 18:11:13 +00:00
|
|
|
t = RTIMER_NOW();
|
|
|
|
#if NURTIMER
|
|
|
|
while(RTIMER_CLOCK_LT(t, RTIMER_NOW(), t + INTER_PACKET_INTERVAL));
|
|
|
|
#else
|
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), t + INTER_PACKET_INTERVAL)) { }
|
|
|
|
#endif
|
2010-04-26 17:46:21 +00:00
|
|
|
NETSTACK_RADIO.transmit(transmit_len);
|
2010-03-16 18:11:13 +00:00
|
|
|
}
|
2010-02-18 21:26:15 +00:00
|
|
|
we_are_sending = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
cycle_announcement(void *ptr)
|
|
|
|
{
|
|
|
|
ctimer_set(&announcement_ctimer, ANNOUNCEMENT_TIME,
|
|
|
|
send_announcement, NULL);
|
|
|
|
ctimer_set(&announcement_cycle_ctimer, ANNOUNCEMENT_PERIOD,
|
|
|
|
cycle_announcement, NULL);
|
2010-04-01 10:02:04 +00:00
|
|
|
if(is_snooping > 0) {
|
|
|
|
is_snooping--;
|
|
|
|
/* printf("is_snooping %d\n", is_snooping); */
|
2010-02-18 21:26:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
listen_callback(int periods)
|
|
|
|
{
|
2010-04-04 12:28:29 +00:00
|
|
|
printf("Snoop\n");
|
2010-04-01 10:02:04 +00:00
|
|
|
is_snooping = periods + 1;
|
2010-02-18 21:26:15 +00:00
|
|
|
}
|
|
|
|
#endif /* CONTIKIMAC_CONF_ANNOUNCEMENTS */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
contikimac_set_announcement_radio_txpower(int txpower)
|
|
|
|
{
|
|
|
|
#if CONTIKIMAC_CONF_ANNOUNCEMENTS
|
|
|
|
announcement_radio_txpower = txpower;
|
|
|
|
#endif /* CONTIKIMAC_CONF_ANNOUNCEMENTS */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
init(void)
|
|
|
|
{
|
|
|
|
radio_is_on = 0;
|
|
|
|
PT_INIT(&pt);
|
|
|
|
#if NURTIMER
|
|
|
|
rtimer_setup(&rt, RTIMER_HARD,
|
|
|
|
(void (*)(struct rtimer *, void *, int status))powercycle,
|
|
|
|
NULL);
|
|
|
|
rtimer_schedule(&rt, CYCLE_TIME, 1);
|
|
|
|
#else
|
|
|
|
rtimer_set(&rt, RTIMER_NOW() + CYCLE_TIME, 1,
|
|
|
|
(void (*)(struct rtimer *, void *))powercycle, NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
contikimac_is_on = 1;
|
|
|
|
|
|
|
|
#if WITH_PHASE_OPTIMIZATION
|
|
|
|
phase_init(&phase_list);
|
|
|
|
#endif /* WITH_PHASE_OPTIMIZATION */
|
|
|
|
|
|
|
|
#if CONTIKIMAC_CONF_ANNOUNCEMENTS
|
|
|
|
announcement_register_listen_callback(listen_callback);
|
|
|
|
ctimer_set(&announcement_cycle_ctimer, ANNOUNCEMENT_TIME,
|
|
|
|
cycle_announcement, NULL);
|
|
|
|
#endif /* CONTIKIMAC_CONF_ANNOUNCEMENTS */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
turn_on(void)
|
|
|
|
{
|
2010-04-04 07:49:30 +00:00
|
|
|
if(contikimac_is_on == 0) {
|
|
|
|
contikimac_is_on = 1;
|
2010-02-18 21:26:15 +00:00
|
|
|
#if NURTIMER
|
2010-04-04 07:49:30 +00:00
|
|
|
rtimer_schedule(&rt, CYCLE_TIME, 1);
|
2010-02-18 21:26:15 +00:00
|
|
|
#else
|
2010-04-04 07:49:30 +00:00
|
|
|
rtimer_set(&rt, RTIMER_NOW() + CYCLE_TIME, 1,
|
|
|
|
(void (*)(struct rtimer *, void *))powercycle, NULL);
|
2010-02-18 21:26:15 +00:00
|
|
|
#endif
|
2010-04-04 07:49:30 +00:00
|
|
|
}
|
2010-02-18 21:26:15 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
turn_off(int keep_radio_on)
|
|
|
|
{
|
|
|
|
contikimac_is_on = 0;
|
|
|
|
if(keep_radio_on) {
|
|
|
|
return NETSTACK_RADIO.on();
|
|
|
|
} else {
|
|
|
|
return NETSTACK_RADIO.off();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static unsigned short
|
|
|
|
duty_cycle(void)
|
|
|
|
{
|
|
|
|
return (1ul * CLOCK_SECOND * CYCLE_TIME) / RTIMER_ARCH_SECOND;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-02-23 20:09:11 +00:00
|
|
|
const struct rdc_driver contikimac_driver = {
|
2010-02-18 21:26:15 +00:00
|
|
|
"ContikiMAC",
|
|
|
|
init,
|
|
|
|
qsend_packet,
|
|
|
|
input_packet,
|
|
|
|
turn_on,
|
|
|
|
turn_off,
|
|
|
|
duty_cycle,
|
|
|
|
};
|
2010-02-23 18:49:05 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uint16_t
|
|
|
|
contikimac_debug_print(void)
|
|
|
|
{
|
2010-04-04 12:28:29 +00:00
|
|
|
static rtimer_clock_t one_cycle_start;
|
|
|
|
printf("Drift %d\n", (one_cycle_start - cycle_start) % CYCLE_TIME);
|
|
|
|
one_cycle_start = cycle_start;
|
2010-02-28 08:33:21 +00:00
|
|
|
return 0;
|
2010-02-23 18:49:05 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|