2007-04-07 07:43:31 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007, Swedish Institute of Computer Science.
|
|
|
|
* 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-06-14 08:52:41 +02:00
|
|
|
* $Id: xmac.c,v 1.57 2010/06/14 06:52:41 adamdunkels Exp $
|
2007-04-07 07:43:31 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* A simple power saving MAC protocol based on X-MAC [SenSys 2006]
|
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
2009-10-19 22:53:56 +02:00
|
|
|
* Niclas Finne <nfi@sics.se>
|
|
|
|
* Joakim Eriksson <joakime@sics.se>
|
2007-04-07 07:43:31 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dev/leds.h"
|
|
|
|
#include "dev/radio.h"
|
2008-02-24 22:07:28 +01:00
|
|
|
#include "dev/watchdog.h"
|
2007-11-12 23:29:37 +01:00
|
|
|
#include "lib/random.h"
|
A work-in-progress rework of the Contiki MAC and radio layers. The
main ideas are:
* Separates the Contiki low-layer network stack into four layers:
network (e.g. sicslowpan / rime), Medium Access Control MAC
(e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
that specify what mechanism/protocol/driver to use at the four
layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
the fate of a transmitted packet: if the packet was not possible to
transmit, the cause of the failure is reported, and if the packets
was successfully transmitted, the number of tries before it was
finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
efficient radio duty cycling protocols: channel check, pending
packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
#defines.
2010-02-18 22:48:39 +01:00
|
|
|
#include "net/netstack.h"
|
2009-10-19 22:53:56 +02:00
|
|
|
#include "net/mac/xmac.h"
|
|
|
|
#include "net/rime.h"
|
|
|
|
#include "net/rime/timesynch.h"
|
2009-03-01 21:37:16 +01:00
|
|
|
#include "sys/compower.h"
|
2009-10-19 22:53:56 +02:00
|
|
|
#include "sys/pt.h"
|
|
|
|
#include "sys/rtimer.h"
|
2008-01-23 16:00:54 +01:00
|
|
|
|
2007-05-25 10:06:44 +02:00
|
|
|
#include "contiki-conf.h"
|
|
|
|
|
2009-11-02 12:58:56 +01:00
|
|
|
#ifdef EXPERIMENT_SETUP
|
|
|
|
#include "experiment-setup.h"
|
|
|
|
#endif
|
|
|
|
|
2009-02-15 23:05:06 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2009-12-06 14:16:59 +01:00
|
|
|
#ifndef WITH_ACK_OPTIMIZATION
|
2010-04-30 09:32:39 +02:00
|
|
|
#define WITH_ACK_OPTIMIZATION 0
|
2009-12-06 14:16:59 +01:00
|
|
|
#endif
|
|
|
|
#ifndef WITH_ENCOUNTER_OPTIMIZATION
|
2009-10-18 15:19:25 +02:00
|
|
|
#define WITH_ENCOUNTER_OPTIMIZATION 1
|
2009-12-06 14:16:59 +01:00
|
|
|
#endif
|
|
|
|
#ifndef WITH_STREAMING
|
2009-11-04 17:02:45 +01:00
|
|
|
#define WITH_STREAMING 1
|
2009-12-06 14:16:59 +01:00
|
|
|
#endif
|
|
|
|
#ifndef WITH_STROBE_BROADCAST
|
|
|
|
#define WITH_STROBE_BROADCAST 0
|
|
|
|
#endif
|
2007-05-25 10:06:44 +02:00
|
|
|
|
2009-02-14 21:35:03 +01: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
|
|
|
|
|
|
|
|
/* The structure of the announcement messages. */
|
|
|
|
struct announcement_msg {
|
|
|
|
uint16_t num;
|
|
|
|
struct announcement_data data[ANNOUNCEMENT_MAX];
|
|
|
|
};
|
|
|
|
|
2009-04-28 16:00:53 +02:00
|
|
|
/* The length of the header of the announcement message, i.e., the
|
|
|
|
"num" field in the struct. */
|
|
|
|
#define ANNOUNCEMENT_MSG_HEADERLEN (sizeof (uint16_t))
|
|
|
|
|
2009-10-19 22:53:56 +02:00
|
|
|
#define DISPATCH 0
|
|
|
|
#define TYPE_STROBE 0x10
|
|
|
|
/* #define TYPE_DATA 0x11 */
|
|
|
|
#define TYPE_ANNOUNCEMENT 0x12
|
|
|
|
#define TYPE_STROBE_ACK 0x13
|
2009-02-14 21:35:03 +01:00
|
|
|
|
2007-04-07 07:43:31 +02:00
|
|
|
struct xmac_hdr {
|
2009-10-19 22:53:56 +02:00
|
|
|
uint8_t dispatch;
|
|
|
|
uint8_t type;
|
2007-04-07 07:43:31 +02:00
|
|
|
};
|
|
|
|
|
2009-10-19 23:27:02 +02:00
|
|
|
#define MAX_STROBE_SIZE 50
|
2009-10-19 22:53:56 +02:00
|
|
|
|
2007-05-22 22:55:24 +02:00
|
|
|
#ifdef XMAC_CONF_ON_TIME
|
2007-11-12 23:29:37 +01:00
|
|
|
#define DEFAULT_ON_TIME (XMAC_CONF_ON_TIME)
|
2007-05-15 10:07:07 +02:00
|
|
|
#else
|
2009-10-19 22:53:56 +02:00
|
|
|
#define DEFAULT_ON_TIME (RTIMER_ARCH_SECOND / 160)
|
2007-05-15 10:07:07 +02:00
|
|
|
#endif
|
2007-04-07 07:43:31 +02:00
|
|
|
|
2007-05-22 22:55:24 +02:00
|
|
|
#ifdef XMAC_CONF_OFF_TIME
|
2007-11-12 23:29:37 +01:00
|
|
|
#define DEFAULT_OFF_TIME (XMAC_CONF_OFF_TIME)
|
2007-05-15 10:07:07 +02:00
|
|
|
#else
|
2010-02-03 00:28:58 +01:00
|
|
|
#define DEFAULT_OFF_TIME (RTIMER_ARCH_SECOND / MAC_CHANNEL_CHECK_RATE - DEFAULT_ON_TIME)
|
2007-05-15 10:07:07 +02:00
|
|
|
#endif
|
|
|
|
|
2009-10-18 15:19:25 +02:00
|
|
|
#define DEFAULT_PERIOD (DEFAULT_OFF_TIME + DEFAULT_ON_TIME)
|
|
|
|
|
2009-12-06 14:16:59 +01:00
|
|
|
#define WAIT_TIME_BEFORE_STROBE_ACK RTIMER_ARCH_SECOND / 1000
|
|
|
|
|
2009-10-19 08:39:29 +02:00
|
|
|
/* On some platforms, we may end up with a DEFAULT_PERIOD that is 0
|
|
|
|
which will make compilation fail due to a modulo operation in the
|
|
|
|
code. To ensure that DEFAULT_PERIOD is greater than zero, we use
|
|
|
|
the construct below. */
|
|
|
|
#if DEFAULT_PERIOD == 0
|
|
|
|
#undef DEFAULT_PERIOD
|
|
|
|
#define DEFAULT_PERIOD 1
|
|
|
|
#endif
|
|
|
|
|
2009-02-14 21:35:03 +01:00
|
|
|
/* The cycle time for announcements. */
|
|
|
|
#define ANNOUNCEMENT_PERIOD 4 * CLOCK_SECOND
|
|
|
|
|
|
|
|
/* The time before sending an announcement within one announcement
|
|
|
|
cycle. */
|
2009-02-15 23:05:06 +01:00
|
|
|
#define ANNOUNCEMENT_TIME (random_rand() % (ANNOUNCEMENT_PERIOD))
|
2009-02-14 21:35:03 +01:00
|
|
|
|
2008-02-24 22:07:28 +01:00
|
|
|
#define DEFAULT_STROBE_WAIT_TIME (7 * DEFAULT_ON_TIME / 8)
|
2008-01-14 10:26:42 +01:00
|
|
|
|
2007-11-12 23:29:37 +01:00
|
|
|
struct xmac_config xmac_config = {
|
2008-01-14 10:26:42 +01:00
|
|
|
DEFAULT_ON_TIME,
|
|
|
|
DEFAULT_OFF_TIME,
|
2009-12-06 14:16:59 +01:00
|
|
|
4 * DEFAULT_ON_TIME + DEFAULT_OFF_TIME,
|
2007-11-12 23:29:37 +01:00
|
|
|
DEFAULT_STROBE_WAIT_TIME
|
|
|
|
};
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
static struct rtimer rt;
|
|
|
|
static struct pt pt;
|
2007-04-07 07:43:31 +02:00
|
|
|
|
2009-12-07 00:29:38 +01:00
|
|
|
static volatile uint8_t xmac_is_on = 0;
|
2007-12-05 14:23:17 +01:00
|
|
|
|
2008-01-23 16:00:54 +01:00
|
|
|
static volatile unsigned char waiting_for_packet = 0;
|
2007-05-15 10:07:07 +02:00
|
|
|
static volatile unsigned char someone_is_sending = 0;
|
|
|
|
static volatile unsigned char we_are_sending = 0;
|
2007-04-07 07:43:31 +02:00
|
|
|
static volatile unsigned char radio_is_on = 0;
|
|
|
|
|
|
|
|
#undef LEDS_ON
|
|
|
|
#undef LEDS_OFF
|
|
|
|
#undef LEDS_TOGGLE
|
|
|
|
|
|
|
|
#define LEDS_ON(x) leds_on(x)
|
|
|
|
#define LEDS_OFF(x) leds_off(x)
|
|
|
|
#define LEDS_TOGGLE(x) leds_toggle(x)
|
2007-11-12 23:29:37 +01:00
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
2007-05-15 10:07:07 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
2009-10-19 22:53:56 +02:00
|
|
|
#define PRINTDEBUG(...) printf(__VA_ARGS__)
|
2007-04-07 07:43:31 +02:00
|
|
|
#else
|
2009-11-04 17:02:45 +01:00
|
|
|
#undef LEDS_ON
|
2007-11-17 11:12:19 +01:00
|
|
|
#undef LEDS_OFF
|
|
|
|
#undef LEDS_TOGGLE
|
2007-04-07 07:43:31 +02:00
|
|
|
#define LEDS_ON(x)
|
|
|
|
#define LEDS_OFF(x)
|
2009-11-04 17:02:45 +01:00
|
|
|
#define LEDS_TOGGLE(x)
|
2007-05-15 10:07:07 +02:00
|
|
|
#define PRINTF(...)
|
2009-10-19 22:53:56 +02:00
|
|
|
#define PRINTDEBUG(...)
|
2007-04-07 07:43:31 +02:00
|
|
|
#endif
|
|
|
|
|
2009-02-14 21:35:03 +01:00
|
|
|
#if XMAC_CONF_ANNOUNCEMENTS
|
|
|
|
/* Timers for keeping track of when to send announcements. */
|
|
|
|
static struct ctimer announcement_cycle_ctimer, announcement_ctimer;
|
2009-03-23 22:06:26 +01:00
|
|
|
|
|
|
|
static int announcement_radio_txpower;
|
2009-02-14 21:35:03 +01:00
|
|
|
#endif /* XMAC_CONF_ANNOUNCEMENTS */
|
|
|
|
|
|
|
|
/* Flag that is used to keep track of whether or not we are listening
|
|
|
|
for announcements from neighbors. */
|
|
|
|
static uint8_t is_listening;
|
|
|
|
|
2009-03-01 21:37:16 +01:00
|
|
|
#if XMAC_CONF_COMPOWER
|
|
|
|
static struct compower_activity current_packet;
|
|
|
|
#endif /* XMAC_CONF_COMPOWER */
|
|
|
|
|
2009-10-18 15:19:25 +02:00
|
|
|
#if WITH_ENCOUNTER_OPTIMIZATION
|
|
|
|
|
|
|
|
#include "lib/list.h"
|
|
|
|
#include "lib/memb.h"
|
|
|
|
|
|
|
|
struct encounter {
|
|
|
|
struct encounter *next;
|
|
|
|
rimeaddr_t neighbor;
|
|
|
|
rtimer_clock_t time;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MAX_ENCOUNTERS 4
|
|
|
|
LIST(encounter_list);
|
|
|
|
MEMB(encounter_memb, struct encounter, MAX_ENCOUNTERS);
|
|
|
|
#endif /* WITH_ENCOUNTER_OPTIMIZATION */
|
2009-10-19 23:27:02 +02:00
|
|
|
|
2009-11-02 12:58:56 +01:00
|
|
|
static uint8_t is_streaming;
|
|
|
|
static rimeaddr_t is_streaming_to, is_streaming_to_too;
|
|
|
|
static rtimer_clock_t stream_until;
|
|
|
|
#define DEFAULT_STREAM_TIME (RTIMER_ARCH_SECOND)
|
|
|
|
|
2009-10-19 23:27:02 +02:00
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a, b) ((a) < (b)? (a) : (b))
|
|
|
|
#endif /* MIN */
|
|
|
|
|
2010-06-14 08:52:41 +02:00
|
|
|
struct seqno {
|
|
|
|
rimeaddr_t sender;
|
|
|
|
uint8_t seqno;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MAX_SEQNOS 8
|
|
|
|
static struct seqno received_seqnos[MAX_SEQNOS];
|
|
|
|
|
|
|
|
|
2007-04-07 07:43:31 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-01-23 16:00:54 +01:00
|
|
|
static void
|
|
|
|
on(void)
|
|
|
|
{
|
2008-02-24 22:07:28 +01:00
|
|
|
if(xmac_is_on && radio_is_on == 0) {
|
2008-01-23 16:00:54 +01:00
|
|
|
radio_is_on = 1;
|
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
|
|
|
NETSTACK_RADIO.on();
|
2008-01-23 16:00:54 +01:00
|
|
|
LEDS_ON(LEDS_RED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
off(void)
|
|
|
|
{
|
2009-11-02 12:58:56 +01:00
|
|
|
if(xmac_is_on && radio_is_on != 0 && is_listening == 0 &&
|
|
|
|
is_streaming == 0) {
|
2008-01-23 16:00:54 +01:00
|
|
|
radio_is_on = 0;
|
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
|
|
|
NETSTACK_RADIO.off();
|
2008-01-23 16:00:54 +01:00
|
|
|
LEDS_OFF(LEDS_RED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-12-06 14:16:59 +01:00
|
|
|
static char powercycle(struct rtimer *t, void *ptr);
|
|
|
|
static void
|
|
|
|
schedule_powercycle(struct rtimer *t, rtimer_clock_t time)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
if(xmac_is_on) {
|
|
|
|
r = rtimer_set(t, RTIMER_TIME(t) + time, 1,
|
|
|
|
(void (*)(struct rtimer *, void *))powercycle, NULL);
|
|
|
|
if(r) {
|
|
|
|
PRINTF("schedule_powercycle: could not set rtimer\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
powercycle_turn_radio_off(void)
|
|
|
|
{
|
|
|
|
if(we_are_sending == 0 &&
|
|
|
|
waiting_for_packet == 0) {
|
|
|
|
off();
|
|
|
|
}
|
|
|
|
#if XMAC_CONF_COMPOWER
|
|
|
|
compower_accumulate(&compower_idle_activity);
|
|
|
|
#endif /* XMAC_CONF_COMPOWER */
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
powercycle_turn_radio_on(void)
|
|
|
|
{
|
|
|
|
if(we_are_sending == 0 &&
|
|
|
|
waiting_for_packet == 0) {
|
|
|
|
on();
|
|
|
|
}
|
|
|
|
}
|
2007-05-19 15:12:00 +02:00
|
|
|
static char
|
2007-04-07 07:43:31 +02:00
|
|
|
powercycle(struct rtimer *t, void *ptr)
|
|
|
|
{
|
2009-11-02 12:58:56 +01:00
|
|
|
if(is_streaming) {
|
|
|
|
if(!RTIMER_CLOCK_LT(RTIMER_NOW(), stream_until)) {
|
|
|
|
is_streaming = 0;
|
|
|
|
rimeaddr_copy(&is_streaming_to, &rimeaddr_null);
|
|
|
|
rimeaddr_copy(&is_streaming_to_too, &rimeaddr_null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-07 07:43:31 +02:00
|
|
|
PT_BEGIN(&pt);
|
|
|
|
|
|
|
|
while(1) {
|
2007-11-12 23:29:37 +01:00
|
|
|
/* Only wait for some cycles to pass for someone to start sending */
|
|
|
|
if(someone_is_sending > 0) {
|
|
|
|
someone_is_sending--;
|
2007-04-07 07:43:31 +02:00
|
|
|
}
|
2007-11-12 23:29:37 +01:00
|
|
|
|
2009-12-06 14:16:59 +01:00
|
|
|
/* If there were a strobe in the air, turn radio on */
|
|
|
|
powercycle_turn_radio_on();
|
|
|
|
schedule_powercycle(t, xmac_config.on_time);
|
|
|
|
PT_YIELD(&pt);
|
|
|
|
|
2010-04-30 09:32:39 +02:00
|
|
|
if(xmac_config.off_time > 0 && !NETSTACK_RADIO.receiving_packet()) {
|
2009-12-06 14:16:59 +01:00
|
|
|
powercycle_turn_radio_off();
|
|
|
|
if(waiting_for_packet != 0) {
|
|
|
|
waiting_for_packet++;
|
|
|
|
if(waiting_for_packet > 2) {
|
|
|
|
/* We should not be awake for more than two consecutive
|
|
|
|
power cycles without having heard a packet, so we turn off
|
|
|
|
the radio. */
|
|
|
|
waiting_for_packet = 0;
|
|
|
|
powercycle_turn_radio_off();
|
2007-11-12 23:29:37 +01:00
|
|
|
}
|
|
|
|
}
|
2009-12-06 14:16:59 +01:00
|
|
|
schedule_powercycle(t, xmac_config.off_time);
|
2007-11-12 23:29:37 +01:00
|
|
|
PT_YIELD(&pt);
|
|
|
|
}
|
2007-04-07 07:43:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PT_END(&pt);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-02-14 21:35:03 +01:00
|
|
|
#if XMAC_CONF_ANNOUNCEMENTS
|
|
|
|
static int
|
2009-10-19 22:53:56 +02:00
|
|
|
parse_announcements(const rimeaddr_t *from)
|
2009-02-14 21:35:03 +01:00
|
|
|
{
|
|
|
|
/* Parse incoming announcements */
|
2009-10-19 23:27:02 +02:00
|
|
|
struct announcement_msg adata;
|
2009-02-14 21:35:03 +01:00
|
|
|
int i;
|
2009-10-19 23:27:02 +02:00
|
|
|
|
|
|
|
memcpy(&adata, packetbuf_dataptr(), MIN(packetbuf_datalen(), sizeof(adata)));
|
2009-11-27 15:27:50 +01:00
|
|
|
|
2009-04-28 16:00:53 +02:00
|
|
|
/* printf("%d.%d: probe from %d.%d with %d announcements\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
from->u8[0], from->u8[1], adata->num);*/
|
|
|
|
/* for(i = 0; i < packetbuf_datalen(); ++i) {
|
|
|
|
printf("%02x ", ((uint8_t *)packetbuf_dataptr())[i]);
|
|
|
|
}
|
|
|
|
printf("\n");*/
|
2009-11-27 15:27:50 +01:00
|
|
|
|
2009-10-19 23:27:02 +02:00
|
|
|
for(i = 0; i < adata.num; ++i) {
|
2009-04-28 16:00:53 +02:00
|
|
|
/* printf("%d.%d: announcement %d: %d\n",
|
2009-02-14 21:35:03 +01:00
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
adata->data[i].id,
|
2009-04-28 16:00:53 +02:00
|
|
|
adata->data[i].value);*/
|
2009-11-27 15:27:50 +01:00
|
|
|
|
2009-02-14 21:35:03 +01:00
|
|
|
announcement_heard(from,
|
2009-10-19 23:27:02 +02:00
|
|
|
adata.data[i].id,
|
|
|
|
adata.data[i].value);
|
2009-02-14 21:35:03 +01:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
format_announcement(char *hdr)
|
|
|
|
{
|
2009-11-27 13:49:27 +01:00
|
|
|
struct announcement_msg adata;
|
2009-02-14 21:35:03 +01:00
|
|
|
struct announcement *a;
|
2009-11-27 15:27:50 +01:00
|
|
|
|
2009-02-14 21:35:03 +01:00
|
|
|
/* Construct the announcements */
|
2009-11-27 13:49:27 +01:00
|
|
|
/* adata = (struct announcement_msg *)hdr;*/
|
2009-11-27 15:27:50 +01:00
|
|
|
|
2009-11-27 13:49:27 +01:00
|
|
|
adata.num = 0;
|
2009-02-14 21:35:03 +01:00
|
|
|
for(a = announcement_list();
|
2009-11-27 13:49:27 +01:00
|
|
|
a != NULL && adata.num < ANNOUNCEMENT_MAX;
|
2009-02-14 21:35:03 +01:00
|
|
|
a = a->next) {
|
2009-11-27 15:27:50 +01:00
|
|
|
adata.data[adata.num].id = a->id;
|
|
|
|
adata.data[adata.num].value = a->value;
|
2009-11-27 13:49:27 +01:00
|
|
|
adata.num++;
|
2009-02-14 21:35:03 +01:00
|
|
|
}
|
|
|
|
|
2009-11-27 13:49:27 +01:00
|
|
|
memcpy(hdr, &adata, sizeof(struct announcement_msg));
|
2009-11-27 15:27:50 +01:00
|
|
|
|
2009-11-27 13:49:27 +01:00
|
|
|
if(adata.num > 0) {
|
2009-03-23 22:00:25 +01:00
|
|
|
return ANNOUNCEMENT_MSG_HEADERLEN +
|
2009-11-27 13:49:27 +01:00
|
|
|
sizeof(struct announcement_data) * adata.num;
|
2009-03-23 22:00:25 +01:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-02-14 21:35:03 +01:00
|
|
|
}
|
|
|
|
#endif /* XMAC_CONF_ANNOUNCEMENTS */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-10-18 15:19:25 +02:00
|
|
|
#if WITH_ENCOUNTER_OPTIMIZATION
|
|
|
|
static void
|
2009-10-19 22:53:56 +02:00
|
|
|
register_encounter(const rimeaddr_t *neighbor, rtimer_clock_t time)
|
2009-10-18 15:19:25 +02:00
|
|
|
{
|
|
|
|
struct encounter *e;
|
|
|
|
|
|
|
|
/* If we have an entry for this neighbor already, we renew it. */
|
|
|
|
for(e = list_head(encounter_list); e != NULL; e = e->next) {
|
|
|
|
if(rimeaddr_cmp(neighbor, &e->neighbor)) {
|
|
|
|
e->time = time;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-10-19 22:53:56 +02:00
|
|
|
/* No matching encounter was found, so we allocate a new one. */
|
2009-10-18 15:19:25 +02:00
|
|
|
if(e == NULL) {
|
|
|
|
e = memb_alloc(&encounter_memb);
|
|
|
|
if(e == NULL) {
|
|
|
|
/* We could not allocate memory for this encounter, so we just drop it. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rimeaddr_copy(&e->neighbor, neighbor);
|
|
|
|
e->time = time;
|
|
|
|
list_add(encounter_list, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* WITH_ENCOUNTER_OPTIMIZATION */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-25 10:06:44 +02:00
|
|
|
static int
|
2007-12-23 15:56:54 +01:00
|
|
|
send_packet(void)
|
2007-04-07 07:43:31 +02:00
|
|
|
{
|
2007-11-12 23:29:37 +01:00
|
|
|
rtimer_clock_t t0;
|
|
|
|
rtimer_clock_t t;
|
2009-10-19 22:53:56 +02:00
|
|
|
rtimer_clock_t encounter_time = 0;
|
2007-04-07 07:43:31 +02:00
|
|
|
int strobes;
|
2009-10-19 22:53:56 +02:00
|
|
|
struct xmac_hdr *hdr;
|
2009-03-05 20:37:52 +01:00
|
|
|
int got_strobe_ack = 0;
|
2009-10-19 22:53:56 +02:00
|
|
|
uint8_t strobe[MAX_STROBE_SIZE];
|
|
|
|
int strobe_len, len;
|
2008-06-30 10:10:02 +02:00
|
|
|
int is_broadcast = 0;
|
2009-10-19 22:53:56 +02:00
|
|
|
int is_reliable;
|
2009-10-18 15:19:25 +02:00
|
|
|
struct encounter *e;
|
2009-10-19 22:53:56 +02:00
|
|
|
struct queuebuf *packet;
|
2009-11-02 12:58:56 +01:00
|
|
|
int is_already_streaming = 0;
|
2009-12-06 14:16:59 +01:00
|
|
|
uint8_t collisions;
|
2009-11-02 12:58:56 +01:00
|
|
|
|
2009-10-19 22:53:56 +02:00
|
|
|
/* Create the X-MAC header for the data packet. */
|
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &rimeaddr_node_addr);
|
|
|
|
if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &rimeaddr_null)) {
|
|
|
|
is_broadcast = 1;
|
|
|
|
PRINTDEBUG("xmac: send broadcast\n");
|
|
|
|
} else {
|
|
|
|
#if UIP_CONF_IPV6
|
|
|
|
PRINTDEBUG("xmac: 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]);
|
|
|
|
#else
|
|
|
|
PRINTDEBUG("xmac: 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-01 14:30:21 +01:00
|
|
|
len = NETSTACK_FRAMER.create();
|
2009-10-19 22:53:56 +02:00
|
|
|
strobe_len = len + sizeof(struct xmac_hdr);
|
|
|
|
if(len == 0 || strobe_len > sizeof(strobe)) {
|
2009-11-27 15:27:50 +01:00
|
|
|
/* Failed to send */
|
2009-11-02 12:58:56 +01:00
|
|
|
PRINTF("xmac: send failed, too large header\n");
|
2009-12-06 14:16:59 +01:00
|
|
|
return MAC_TX_ERR_FATAL;
|
2009-10-19 22:53:56 +02:00
|
|
|
}
|
|
|
|
memcpy(strobe, packetbuf_hdrptr(), len);
|
|
|
|
strobe[len] = DISPATCH; /* dispatch */
|
|
|
|
strobe[len + 1] = TYPE_STROBE; /* type */
|
|
|
|
|
|
|
|
packetbuf_compact();
|
|
|
|
packet = queuebuf_new_from_packetbuf();
|
|
|
|
if(packet == NULL) {
|
|
|
|
/* No buffer available */
|
|
|
|
PRINTF("xmac: send failed, no queue buffer available (of %u)\n",
|
|
|
|
QUEUEBUF_CONF_NUM);
|
2009-12-06 14:16:59 +01:00
|
|
|
return MAC_TX_ERR;
|
2009-10-19 22:53:56 +02:00
|
|
|
}
|
|
|
|
|
2009-11-02 12:58:56 +01:00
|
|
|
#if WITH_STREAMING
|
|
|
|
if(is_streaming == 1 &&
|
|
|
|
(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
|
|
|
&is_streaming_to) ||
|
|
|
|
rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
|
|
|
&is_streaming_to_too))) {
|
|
|
|
is_already_streaming = 1;
|
|
|
|
}
|
|
|
|
if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
|
|
|
|
PACKETBUF_ATTR_PACKET_TYPE_STREAM) {
|
|
|
|
is_streaming = 1;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
#endif /* WITH_STREAMING */
|
|
|
|
|
2008-01-23 16:00:54 +01:00
|
|
|
off();
|
2007-11-12 23:29:37 +01:00
|
|
|
|
2009-10-18 15:19:25 +02:00
|
|
|
#if WITH_ENCOUNTER_OPTIMIZATION
|
|
|
|
/* We go through the list of encounters to find if we have recorded
|
|
|
|
an encounter with this particular neighbor. If so, we can compute
|
|
|
|
the time for the next expected encounter and setup a ctimer to
|
|
|
|
switch on the radio just before the encounter. */
|
|
|
|
for(e = list_head(encounter_list); e != NULL; e = e->next) {
|
|
|
|
const rimeaddr_t *neighbor = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
|
|
|
|
|
|
|
|
if(rimeaddr_cmp(neighbor, &e->neighbor)) {
|
|
|
|
rtimer_clock_t wait, now, expected;
|
|
|
|
|
|
|
|
/* We expect encounters to happen every DEFAULT_PERIOD time
|
|
|
|
units. The next expected encounter is at time e->time +
|
|
|
|
DEFAULT_PERIOD. To compute a relative offset, we subtract
|
|
|
|
with clock_time(). Because we are only interested in turning
|
|
|
|
on the radio within the DEFAULT_PERIOD period, we compute the
|
|
|
|
waiting time with modulo DEFAULT_PERIOD. */
|
|
|
|
|
|
|
|
now = RTIMER_NOW();
|
|
|
|
wait = ((rtimer_clock_t)(e->time - now)) % (DEFAULT_PERIOD);
|
2009-12-06 14:16:59 +01:00
|
|
|
expected = now + wait - 2 * DEFAULT_ON_TIME;
|
2009-10-18 15:19:25 +02:00
|
|
|
|
|
|
|
#if WITH_ACK_OPTIMIZATION
|
|
|
|
/* Wait until the receiver is expected to be awake */
|
|
|
|
if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) !=
|
2009-11-02 12:58:56 +01:00
|
|
|
PACKETBUF_ATTR_PACKET_TYPE_ACK &&
|
|
|
|
is_streaming == 0) {
|
2009-10-18 15:19:25 +02:00
|
|
|
/* Do not wait if we are sending an ACK, because then the
|
|
|
|
receiver will already be awake. */
|
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), expected));
|
|
|
|
}
|
|
|
|
#else /* WITH_ACK_OPTIMIZATION */
|
|
|
|
/* Wait until the receiver is expected to be awake */
|
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), expected));
|
|
|
|
#endif /* WITH_ACK_OPTIMIZATION */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* WITH_ENCOUNTER_OPTIMIZATION */
|
|
|
|
|
2009-12-06 18:05:45 +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;
|
|
|
|
|
2007-04-07 07:43:31 +02:00
|
|
|
t0 = RTIMER_NOW();
|
|
|
|
strobes = 0;
|
|
|
|
|
2008-01-23 16:00:54 +01:00
|
|
|
LEDS_ON(LEDS_BLUE);
|
2007-11-12 23:29:37 +01:00
|
|
|
|
2008-01-23 16:00:54 +01:00
|
|
|
/* Send a train of strobes until the receiver answers with an ACK. */
|
2007-04-07 07:43:31 +02:00
|
|
|
|
2008-06-30 10:10:02 +02:00
|
|
|
/* Turn on the radio to listen for the strobe ACK. */
|
2009-12-06 14:16:59 +01:00
|
|
|
on();
|
|
|
|
collisions = 0;
|
2009-11-02 12:58:56 +01:00
|
|
|
if(!is_already_streaming) {
|
2009-12-06 14:16:59 +01:00
|
|
|
watchdog_stop();
|
|
|
|
got_strobe_ack = 0;
|
2007-04-07 07:43:31 +02:00
|
|
|
t = RTIMER_NOW();
|
2009-12-06 14:16:59 +01:00
|
|
|
for(strobes = 0, collisions = 0;
|
|
|
|
got_strobe_ack == 0 && collisions == 0 &&
|
|
|
|
RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + xmac_config.strobe_time);
|
|
|
|
strobes++) {
|
|
|
|
|
|
|
|
while(got_strobe_ack == 0 &&
|
|
|
|
RTIMER_CLOCK_LT(RTIMER_NOW(), t + xmac_config.strobe_wait_time)) {
|
|
|
|
rtimer_clock_t now = RTIMER_NOW();
|
|
|
|
/* See if we got an ACK */
|
|
|
|
packetbuf_clear();
|
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
|
|
|
len = NETSTACK_RADIO.read(packetbuf_dataptr(), PACKETBUF_SIZE);
|
2009-05-06 17:05:28 +02:00
|
|
|
if(len > 0) {
|
2009-12-06 14:16:59 +01:00
|
|
|
packetbuf_set_datalen(len);
|
2010-03-01 14:30:21 +01:00
|
|
|
if(NETSTACK_FRAMER.parse()) {
|
2009-12-06 14:16:59 +01:00
|
|
|
hdr = packetbuf_dataptr();
|
|
|
|
if(hdr->dispatch == DISPATCH && hdr->type == TYPE_STROBE_ACK) {
|
|
|
|
if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
|
|
|
&rimeaddr_node_addr)) {
|
|
|
|
/* We got an ACK from the receiver, so we can immediately send
|
|
|
|
the packet. */
|
|
|
|
got_strobe_ack = 1;
|
|
|
|
encounter_time = now;
|
|
|
|
} else {
|
|
|
|
PRINTDEBUG("xmac: strobe ack for someone else\n");
|
|
|
|
}
|
|
|
|
} else /*if(hdr->dispatch == DISPATCH && hdr->type == TYPE_STROBE)*/ {
|
|
|
|
PRINTDEBUG("xmac: strobe from someone else\n");
|
|
|
|
collisions++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PRINTF("xmac: send failed to parse %u\n", len);
|
|
|
|
}
|
|
|
|
}
|
2007-05-15 10:07:07 +02:00
|
|
|
}
|
2007-11-12 23:29:37 +01:00
|
|
|
|
2009-12-06 14:16:59 +01:00
|
|
|
t = RTIMER_NOW();
|
|
|
|
/* Send the strobe packet. */
|
|
|
|
if(got_strobe_ack == 0 && collisions == 0) {
|
2007-11-12 23:29:37 +01:00
|
|
|
|
2009-12-06 14:16:59 +01:00
|
|
|
if(is_broadcast) {
|
|
|
|
#if WITH_STROBE_BROADCAST
|
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
|
|
|
NETSTACK_RADIO.send(strobe, strobe_len);
|
2009-12-06 14:16:59 +01:00
|
|
|
#else
|
|
|
|
/* restore the packet to send */
|
|
|
|
queuebuf_to_packetbuf(packet);
|
A work-in-progress rework of the Contiki MAC and radio layers. The
main ideas are:
* Separates the Contiki low-layer network stack into four layers:
network (e.g. sicslowpan / rime), Medium Access Control MAC
(e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
that specify what mechanism/protocol/driver to use at the four
layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
the fate of a transmitted packet: if the packet was not possible to
transmit, the cause of the failure is reported, and if the packets
was successfully transmitted, the number of tries before it was
finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
efficient radio duty cycling protocols: channel check, pending
packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
#defines.
2010-02-18 22:48:39 +01:00
|
|
|
NETSTACK_RADIO.send(packetbuf_hdrptr(), packetbuf_totlen());
|
2009-12-06 14:16:59 +01:00
|
|
|
#endif
|
|
|
|
off();
|
|
|
|
} else {
|
|
|
|
rtimer_clock_t wt;
|
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
|
|
|
NETSTACK_RADIO.send(strobe, strobe_len);
|
2009-12-06 14:16:59 +01:00
|
|
|
#if 1
|
|
|
|
/* Turn off the radio for a while to let the other side
|
|
|
|
respond. We don't need to keep our radio on when we know
|
|
|
|
that the other side needs some time to produce a reply. */
|
|
|
|
off();
|
|
|
|
wt = RTIMER_NOW();
|
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + WAIT_TIME_BEFORE_STROBE_ACK));
|
|
|
|
#endif /* 0 */
|
|
|
|
on();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-02 12:58:56 +01:00
|
|
|
}
|
2008-01-23 16:00:54 +01:00
|
|
|
|
2009-11-02 12:58:56 +01:00
|
|
|
#if WITH_ACK_OPTIMIZATION
|
2009-03-05 20:37:52 +01:00
|
|
|
/* If we have received the strobe ACK, and we are sending a packet
|
|
|
|
that will need an upper layer ACK (as signified by the
|
2009-03-12 22:58:20 +01:00
|
|
|
PACKETBUF_ATTR_RELIABLE packet attribute), we keep the radio on. */
|
2009-04-28 16:00:53 +02:00
|
|
|
if(got_strobe_ack && (packetbuf_attr(PACKETBUF_ATTR_RELIABLE) ||
|
2009-11-02 12:58:56 +01:00
|
|
|
packetbuf_attr(PACKETBUF_ATTR_ERELIABLE) ||
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
|
|
|
|
PACKETBUF_ATTR_PACKET_TYPE_STREAM)) {
|
2009-03-05 20:37:52 +01:00
|
|
|
on(); /* Wait for ACK packet */
|
|
|
|
waiting_for_packet = 1;
|
|
|
|
} else {
|
2009-11-02 12:58:56 +01:00
|
|
|
off();
|
2008-01-23 16:00:54 +01:00
|
|
|
}
|
2009-11-02 12:58:56 +01:00
|
|
|
#else /* WITH_ACK_OPTIMIZATION */
|
|
|
|
off();
|
|
|
|
#endif /* WITH_ACK_OPTIMIZATION */
|
2008-01-23 16:00:54 +01:00
|
|
|
|
2009-10-19 22:53:56 +02:00
|
|
|
/* restore the packet to send */
|
|
|
|
queuebuf_to_packetbuf(packet);
|
|
|
|
queuebuf_free(packet);
|
|
|
|
|
2008-06-30 10:10:02 +02:00
|
|
|
/* Send the data packet. */
|
2009-12-06 14:16:59 +01:00
|
|
|
if((is_broadcast || got_strobe_ack || is_streaming) && collisions == 0) {
|
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
|
|
|
NETSTACK_RADIO.send(packetbuf_hdrptr(), packetbuf_totlen());
|
2008-01-23 16:00:54 +01:00
|
|
|
}
|
2009-10-18 15:19:25 +02:00
|
|
|
|
|
|
|
#if WITH_ENCOUNTER_OPTIMIZATION
|
2009-11-02 12:58:56 +01:00
|
|
|
if(got_strobe_ack && !is_streaming) {
|
2009-10-19 22:53:56 +02:00
|
|
|
register_encounter(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), encounter_time);
|
2009-10-18 15:19:25 +02:00
|
|
|
}
|
|
|
|
#endif /* WITH_ENCOUNTER_OPTIMIZATION */
|
2008-02-24 22:07:28 +01:00
|
|
|
watchdog_start();
|
2007-05-15 10:07:07 +02:00
|
|
|
|
2007-11-12 23:29:37 +01:00
|
|
|
PRINTF("xmac: send (strobes=%u,len=%u,%s), done\n", strobes,
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_totlen(), got_strobe_ack ? "ack" : "no ack");
|
2007-11-12 23:29:37 +01:00
|
|
|
|
2009-03-01 21:37:16 +01:00
|
|
|
#if XMAC_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 /* XMAC_CONF_COMPOWER */
|
2009-11-27 15:27:50 +01:00
|
|
|
|
2007-11-12 23:29:37 +01:00
|
|
|
we_are_sending = 0;
|
2009-05-06 17:05:28 +02:00
|
|
|
|
2008-01-23 16:00:54 +01:00
|
|
|
LEDS_OFF(LEDS_BLUE);
|
2009-12-06 14:16:59 +01:00
|
|
|
if(collisions == 0) {
|
2010-02-03 02:17:32 +01:00
|
|
|
if(!is_broadcast && !got_strobe_ack) {
|
|
|
|
return MAC_TX_NOACK;
|
|
|
|
} else {
|
|
|
|
return MAC_TX_OK;
|
|
|
|
}
|
2009-12-06 14:16:59 +01:00
|
|
|
} else {
|
|
|
|
someone_is_sending++;
|
|
|
|
return MAC_TX_COLLISION;
|
|
|
|
}
|
2007-05-15 10:07:07 +02:00
|
|
|
|
2007-04-07 07:43:31 +02: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
|
|
|
static void
|
|
|
|
qsend_packet(mac_callback_t sent, void *ptr)
|
2007-04-07 07:43:31 +02: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
|
|
|
int ret;
|
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
if(someone_is_sending) {
|
|
|
|
PRINTF("xmac: should queue packet, now just dropping %d %d %d %d.\n",
|
2008-01-23 16:00:54 +01:00
|
|
|
waiting_for_packet, someone_is_sending, we_are_sending, radio_is_on);
|
2009-12-06 14:16:59 +01:00
|
|
|
RIMESTATS_ADD(sendingdrop);
|
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
|
|
|
ret = MAC_TX_COLLISION;
|
2007-05-15 10:07:07 +02:00
|
|
|
} else {
|
|
|
|
PRINTF("xmac: send immediately.\n");
|
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
|
|
|
ret = send_packet();
|
2007-05-15 10:07:07 +02: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
|
|
|
|
|
|
|
mac_call_sent_callback(sent, ptr, ret, 1);
|
2007-04-07 07:43:31 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-15 10:07:07 +02:00
|
|
|
static void
|
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
|
|
|
input_packet(void)
|
2007-04-07 07:43:31 +02:00
|
|
|
{
|
2007-05-15 10:07:07 +02:00
|
|
|
struct xmac_hdr *hdr;
|
2008-01-23 16:00:54 +01:00
|
|
|
|
2010-03-01 14:30:21 +01:00
|
|
|
if(NETSTACK_FRAMER.parse()) {
|
2009-10-19 22:53:56 +02:00
|
|
|
hdr = packetbuf_dataptr();
|
2009-02-14 21:35:03 +01:00
|
|
|
|
2009-10-19 22:53:56 +02:00
|
|
|
if(hdr->dispatch != DISPATCH) {
|
2007-05-15 10:07:07 +02:00
|
|
|
someone_is_sending = 0;
|
2009-10-19 22:53:56 +02:00
|
|
|
if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
|
|
|
&rimeaddr_node_addr) ||
|
|
|
|
rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
|
|
|
&rimeaddr_null)) {
|
2008-01-23 16:00:54 +01:00
|
|
|
/* This is a regular packet that is destined to us or to the
|
|
|
|
broadcast address. */
|
2009-10-19 22:53:56 +02:00
|
|
|
|
2008-01-23 16:00:54 +01:00
|
|
|
/* We have received the final packet, so we can go back to being
|
|
|
|
asleep. */
|
|
|
|
off();
|
2009-03-01 21:37:16 +01:00
|
|
|
|
2010-06-14 08:52:41 +02:00
|
|
|
/* Check for duplicate packet by comparing the sequence number
|
|
|
|
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. */
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2009-03-01 21:37:16 +01:00
|
|
|
#if XMAC_CONF_COMPOWER
|
|
|
|
/* 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);
|
2009-10-19 22:53:56 +02:00
|
|
|
|
2009-03-01 21:37:16 +01:00
|
|
|
/* Clear the accumulated power consumption so that it is ready
|
|
|
|
for the next packet. */
|
|
|
|
compower_clear(¤t_packet);
|
|
|
|
#endif /* XMAC_CONF_COMPOWER */
|
2009-10-19 22:53:56 +02:00
|
|
|
|
2008-01-23 16:00:54 +01:00
|
|
|
waiting_for_packet = 0;
|
2009-10-19 22:53:56 +02:00
|
|
|
|
|
|
|
PRINTDEBUG("xmac: data(%u)\n", packetbuf_datalen());
|
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
|
|
|
NETSTACK_MAC.input();
|
|
|
|
return;
|
2009-10-19 22:53:56 +02:00
|
|
|
} else {
|
|
|
|
PRINTDEBUG("xmac: data not for us\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if(hdr->type == TYPE_STROBE) {
|
|
|
|
someone_is_sending = 2;
|
2009-11-27 15:27:50 +01:00
|
|
|
|
2009-10-19 22:53:56 +02:00
|
|
|
if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
|
|
|
&rimeaddr_node_addr)) {
|
|
|
|
/* This is a strobe packet for us. */
|
|
|
|
|
|
|
|
/* If the sender address is someone else, we should
|
|
|
|
acknowledge the strobe and wait for the packet. By using
|
|
|
|
the same address as both sender and receiver, we flag the
|
|
|
|
message is a strobe ack. */
|
|
|
|
hdr->type = TYPE_STROBE_ACK;
|
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER,
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &rimeaddr_node_addr);
|
|
|
|
packetbuf_compact();
|
2010-03-01 14:30:21 +01:00
|
|
|
if(NETSTACK_FRAMER.create()) {
|
2009-10-19 22:53:56 +02:00
|
|
|
/* We turn on the radio in anticipation of the incoming
|
|
|
|
packet. */
|
|
|
|
someone_is_sending = 1;
|
|
|
|
waiting_for_packet = 1;
|
|
|
|
on();
|
A work-in-progress rework of the Contiki MAC and radio layers. The
main ideas are:
* Separates the Contiki low-layer network stack into four layers:
network (e.g. sicslowpan / rime), Medium Access Control MAC
(e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
that specify what mechanism/protocol/driver to use at the four
layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
the fate of a transmitted packet: if the packet was not possible to
transmit, the cause of the failure is reported, and if the packets
was successfully transmitted, the number of tries before it was
finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
efficient radio duty cycling protocols: channel check, pending
packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
#defines.
2010-02-18 22:48:39 +01:00
|
|
|
NETSTACK_RADIO.send(packetbuf_hdrptr(), packetbuf_totlen());
|
2009-10-19 22:53:56 +02:00
|
|
|
PRINTDEBUG("xmac: send strobe ack %u\n", packetbuf_totlen());
|
|
|
|
} else {
|
|
|
|
PRINTF("xmac: failed to send strobe ack\n");
|
|
|
|
}
|
|
|
|
} else if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
|
|
|
|
&rimeaddr_null)) {
|
|
|
|
/* If the receiver address is null, the strobe is sent to
|
|
|
|
prepare for an incoming broadcast packet. If this is the
|
|
|
|
case, we turn on the radio and wait for the incoming
|
|
|
|
broadcast packet. */
|
|
|
|
waiting_for_packet = 1;
|
|
|
|
on();
|
|
|
|
} else {
|
|
|
|
PRINTDEBUG("xmac: strobe not for us\n");
|
2007-05-22 22:55:24 +02:00
|
|
|
}
|
2009-10-19 22:53:56 +02:00
|
|
|
|
|
|
|
/* We are done processing the strobe and we therefore return
|
|
|
|
to the caller. */
|
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
|
|
|
return;
|
2009-02-14 21:35:03 +01:00
|
|
|
#if XMAC_CONF_ANNOUNCEMENTS
|
|
|
|
} else if(hdr->type == TYPE_ANNOUNCEMENT) {
|
2009-10-19 22:53:56 +02:00
|
|
|
packetbuf_hdrreduce(sizeof(struct xmac_hdr));
|
|
|
|
parse_announcements(packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
2009-02-14 21:35:03 +01:00
|
|
|
#endif /* XMAC_CONF_ANNOUNCEMENTS */
|
2009-10-19 22:53:56 +02:00
|
|
|
} else if(hdr->type == TYPE_STROBE_ACK) {
|
|
|
|
PRINTDEBUG("xmac: stray strobe ack\n");
|
|
|
|
} else {
|
|
|
|
PRINTF("xmac: unknown type %u (%u/%u)\n", hdr->type,
|
|
|
|
packetbuf_datalen(), len);
|
2007-05-15 10:07:07 +02:00
|
|
|
}
|
2009-10-19 22:53:56 +02:00
|
|
|
} else {
|
|
|
|
PRINTF("xmac: failed to parse (%u)\n", packetbuf_totlen());
|
2007-04-07 07:43:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-02-14 21:35:03 +01:00
|
|
|
#if XMAC_CONF_ANNOUNCEMENTS
|
|
|
|
static void
|
|
|
|
send_announcement(void *ptr)
|
|
|
|
{
|
|
|
|
struct xmac_hdr *hdr;
|
|
|
|
int announcement_len;
|
2009-11-27 15:27:50 +01:00
|
|
|
|
2009-02-14 21:35:03 +01:00
|
|
|
/* Set up the probe header. */
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_clear();
|
|
|
|
hdr = packetbuf_dataptr();
|
2009-02-14 21:35:03 +01:00
|
|
|
|
|
|
|
announcement_len = format_announcement((char *)hdr +
|
|
|
|
sizeof(struct xmac_hdr));
|
|
|
|
|
2009-03-23 22:00:25 +01:00
|
|
|
if(announcement_len > 0) {
|
|
|
|
packetbuf_set_datalen(sizeof(struct xmac_hdr) + announcement_len);
|
2009-10-19 22:53:56 +02:00
|
|
|
hdr->dispatch = DISPATCH;
|
|
|
|
hdr->type = TYPE_ANNOUNCEMENT;
|
2009-03-23 22:06:26 +01:00
|
|
|
|
2009-10-19 22:53:56 +02:00
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &rimeaddr_node_addr);
|
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, &rimeaddr_null);
|
2009-03-23 22:06:26 +01:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_RADIO_TXPOWER, announcement_radio_txpower);
|
2010-03-01 14:30:21 +01:00
|
|
|
if(NETSTACK_FRAMER.create()) {
|
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
|
|
|
NETSTACK_RADIO.send(packetbuf_hdrptr(), packetbuf_totlen());
|
2009-10-19 22:53:56 +02:00
|
|
|
}
|
2009-03-23 22:00:25 +01:00
|
|
|
}
|
2009-02-14 21:35:03 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
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);
|
|
|
|
if(is_listening > 0) {
|
|
|
|
is_listening--;
|
2009-04-28 16:00:53 +02:00
|
|
|
/* printf("is_listening %d\n", is_listening);*/
|
2009-02-14 21:35:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
listen_callback(int periods)
|
|
|
|
{
|
|
|
|
is_listening = periods + 1;
|
|
|
|
}
|
|
|
|
#endif /* XMAC_CONF_ANNOUNCEMENTS */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-03-23 22:06:26 +01:00
|
|
|
void
|
|
|
|
xmac_set_announcement_radio_txpower(int txpower)
|
|
|
|
{
|
|
|
|
#if XMAC_CONF_ANNOUNCEMENTS
|
|
|
|
announcement_radio_txpower = txpower;
|
|
|
|
#endif /* XMAC_CONF_ANNOUNCEMENTS */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
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
|
|
|
static void
|
|
|
|
init(void)
|
2007-11-12 23:29:37 +01:00
|
|
|
{
|
|
|
|
radio_is_on = 0;
|
2008-01-23 16:00:54 +01:00
|
|
|
waiting_for_packet = 0;
|
2007-11-12 23:29:37 +01:00
|
|
|
PT_INIT(&pt);
|
|
|
|
rtimer_set(&rt, RTIMER_NOW() + xmac_config.off_time, 1,
|
|
|
|
(void (*)(struct rtimer *, void *))powercycle, NULL);
|
|
|
|
|
2007-12-05 14:23:17 +01:00
|
|
|
xmac_is_on = 1;
|
2007-11-12 23:29:37 +01:00
|
|
|
|
2009-10-18 15:19:25 +02:00
|
|
|
#if WITH_ENCOUNTER_OPTIMIZATION
|
|
|
|
list_init(encounter_list);
|
|
|
|
memb_init(&encounter_memb);
|
|
|
|
#endif /* WITH_ENCOUNTER_OPTIMIZATION */
|
2009-11-27 15:27:50 +01:00
|
|
|
|
2009-02-14 21:35:03 +01:00
|
|
|
#if XMAC_CONF_ANNOUNCEMENTS
|
|
|
|
announcement_register_listen_callback(listen_callback);
|
|
|
|
ctimer_set(&announcement_cycle_ctimer, ANNOUNCEMENT_TIME,
|
|
|
|
cycle_announcement, NULL);
|
|
|
|
#endif /* XMAC_CONF_ANNOUNCEMENTS */
|
2007-11-12 23:29:37 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-25 10:06:44 +02:00
|
|
|
static int
|
2008-01-23 16:00:54 +01:00
|
|
|
turn_on(void)
|
2007-05-25 10:06:44 +02:00
|
|
|
{
|
2007-12-05 14:23:17 +01:00
|
|
|
xmac_is_on = 1;
|
2009-04-29 13:42:13 +02:00
|
|
|
rtimer_set(&rt, RTIMER_NOW() + xmac_config.off_time, 1,
|
|
|
|
(void (*)(struct rtimer *, void *))powercycle, NULL);
|
2007-12-05 14:23:17 +01:00
|
|
|
return 1;
|
2007-05-25 10:06:44 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2008-02-24 22:07:28 +01:00
|
|
|
turn_off(int keep_radio_on)
|
2007-05-25 10:06:44 +02:00
|
|
|
{
|
2007-12-05 14:23:17 +01:00
|
|
|
xmac_is_on = 0;
|
2008-02-24 22:07:28 +01:00
|
|
|
if(keep_radio_on) {
|
A work-in-progress rework of the Contiki MAC and radio layers. The
main ideas are:
* Separates the Contiki low-layer network stack into four layers:
network (e.g. sicslowpan / rime), Medium Access Control MAC
(e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
that specify what mechanism/protocol/driver to use at the four
layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
the fate of a transmitted packet: if the packet was not possible to
transmit, the cause of the failure is reported, and if the packets
was successfully transmitted, the number of tries before it was
finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
efficient radio duty cycling protocols: channel check, pending
packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
#defines.
2010-02-18 22:48:39 +01:00
|
|
|
return NETSTACK_RADIO.on();
|
2008-02-24 22:07:28 +01:00
|
|
|
} else {
|
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
|
|
|
return NETSTACK_RADIO.off();
|
2008-02-24 22:07:28 +01:00
|
|
|
}
|
2007-05-25 10:06:44 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-01-25 12:43:32 +01:00
|
|
|
static unsigned short
|
|
|
|
channel_check_interval(void)
|
|
|
|
{
|
|
|
|
return (1ul * CLOCK_SECOND * DEFAULT_PERIOD) / RTIMER_ARCH_SECOND;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-02-23 21:09:11 +01:00
|
|
|
const struct rdc_driver xmac_driver =
|
2007-05-25 10:06:44 +02:00
|
|
|
{
|
2008-06-30 10:10:02 +02:00
|
|
|
"X-MAC",
|
A work-in-progress rework of the Contiki MAC and radio layers. The
main ideas are:
* Separates the Contiki low-layer network stack into four layers:
network (e.g. sicslowpan / rime), Medium Access Control MAC
(e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
that specify what mechanism/protocol/driver to use at the four
layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
the fate of a transmitted packet: if the packet was not possible to
transmit, the cause of the failure is reported, and if the packets
was successfully transmitted, the number of tries before it was
finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
efficient radio duty cycling protocols: channel check, pending
packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
#defines.
2010-02-18 22:48:39 +01:00
|
|
|
init,
|
2007-12-23 15:56:54 +01:00
|
|
|
qsend_packet,
|
A work-in-progress rework of the Contiki MAC and radio layers. The
main ideas are:
* Separates the Contiki low-layer network stack into four layers:
network (e.g. sicslowpan / rime), Medium Access Control MAC
(e.g. CSMA), Radio Duty Cycling RDC (e.g. ContikiMAC, X-MAC), and
radio (e.g. cc2420).
* Introduces a new way to configure the network stack. Four #defines
that specify what mechanism/protocol/driver to use at the four
layers: NETSTACK_CONF_NETWORK, NETSTACK_CONF_MAC, NETSTACK_CONF_RDC,
NETSTACK_CONF_RADIO.
* Adds a callback mechanism to inform the MAC and network layers about
the fate of a transmitted packet: if the packet was not possible to
transmit, the cause of the failure is reported, and if the packets
was successfully transmitted, the number of tries before it was
finally transmitted is reported.
* NULL-protocols at both the MAC and RDC layers: nullmac and nullrdc,
which can be used when MAC and RDC functionality is not needed.
* Extends the radio API with three new functions that enable more
efficient radio duty cycling protocols: channel check, pending
packet, and receiving packet.
* New initialization mechanism, which takes advantage of the NETSTACK
#defines.
2010-02-18 22:48:39 +01:00
|
|
|
input_packet,
|
2008-01-23 16:00:54 +01:00
|
|
|
turn_on,
|
2010-01-25 12:43:32 +01:00
|
|
|
turn_off,
|
|
|
|
channel_check_interval,
|
2007-05-25 10:06:44 +02:00
|
|
|
};
|