2007-05-15 10:07:07 +02:00
|
|
|
/* XXX: TODO: turn radio off when a packet not destined for us is heard. */
|
|
|
|
|
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.
|
|
|
|
*
|
2007-05-15 10:07:07 +02:00
|
|
|
* $Id: xmac.c,v 1.2 2007/05/15 08:07:07 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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sys/pt.h"
|
|
|
|
#include "net/mac/xmac.h"
|
|
|
|
#include "sys/rtimer.h"
|
|
|
|
#include "dev/leds.h"
|
|
|
|
#include "net/rime.h"
|
|
|
|
#include "dev/radio.h"
|
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
#ifdef CHAMELEON
|
|
|
|
#include "net/chameleon/packattr.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern rimeaddr_t uc_receiver;
|
|
|
|
|
2007-04-07 07:43:31 +02:00
|
|
|
struct powercycle {
|
|
|
|
struct pt pt;
|
|
|
|
struct rtimer rt;
|
|
|
|
rtimer_clock_t ontime, offtime;
|
|
|
|
int num_strobes;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct xmac_hdr {
|
2007-05-15 10:07:07 +02:00
|
|
|
rimeaddr_t sender;
|
2007-04-07 07:43:31 +02:00
|
|
|
rimeaddr_t receiver;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct rtimer rt;
|
|
|
|
static struct pt pt;
|
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
#ifdef XMAC_CONF_BASE_TIME
|
|
|
|
#define BASE_TIME XMAC_CONF_BASE_TIME
|
|
|
|
#else
|
2007-04-07 07:43:31 +02:00
|
|
|
#define BASE_TIME (RTIMER_ARCH_SECOND / 100)
|
2007-05-15 10:07:07 +02:00
|
|
|
#endif
|
2007-04-07 07:43:31 +02:00
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
#ifdef XMAC_CONF_DUTYCYCLE
|
|
|
|
#define DUTYCYCLE XMAC_CONF_DUTYCYCLE
|
|
|
|
#else
|
|
|
|
#define DUTYCYCLE 10
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define OFF_TIME BASE_TIME * (DUTYCYCLE)
|
2007-04-07 07:43:31 +02:00
|
|
|
#define ON_TIME BASE_TIME
|
2007-05-15 10:07:07 +02:00
|
|
|
#define STROBE_WAIT_TIME ON_TIME / 2
|
2007-04-07 07:43:31 +02:00
|
|
|
|
|
|
|
static volatile unsigned char should_be_awake = 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;
|
2007-05-15 10:07:07 +02:00
|
|
|
static volatile unsigned char long_off = 0;
|
2007-04-07 07:43:31 +02:00
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
static const struct radio_driver *radio;
|
2007-04-07 07:43:31 +02:00
|
|
|
|
|
|
|
#undef LEDS_ON
|
|
|
|
#undef LEDS_OFF
|
|
|
|
#undef LEDS_TOGGLE
|
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2007-04-07 07:43:31 +02:00
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#define LEDS_ON(x) leds_on(x)
|
|
|
|
#define LEDS_OFF(x) leds_off(x)
|
|
|
|
#define LEDS_TOGGLE(x) leds_toggle(x)
|
2007-05-15 10:07:07 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
2007-04-07 07:43:31 +02:00
|
|
|
#else
|
|
|
|
#define LEDS_ON(x)
|
|
|
|
#define LEDS_OFF(x)
|
|
|
|
#define LEDS_TOGGLE(x)
|
2007-05-15 10:07:07 +02:00
|
|
|
#define PRINTF(...)
|
2007-04-07 07:43:31 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
powercycle(struct rtimer *t, void *ptr)
|
|
|
|
{
|
|
|
|
PT_BEGIN(&pt);
|
|
|
|
|
|
|
|
while(1) {
|
2007-05-15 10:07:07 +02:00
|
|
|
rtimer_clock_t t1, t2;
|
|
|
|
|
|
|
|
t1 = rtimer_arch_now();
|
2007-04-07 07:43:31 +02:00
|
|
|
if(should_be_awake == 0) {
|
2007-05-15 10:07:07 +02:00
|
|
|
radio->off();
|
2007-04-07 07:43:31 +02:00
|
|
|
radio_is_on = 0;
|
|
|
|
LEDS_OFF(LEDS_RED);
|
|
|
|
} else {
|
2007-05-15 10:07:07 +02:00
|
|
|
should_be_awake++;
|
|
|
|
if(should_be_awake > 2) {
|
2007-04-07 07:43:31 +02:00
|
|
|
should_be_awake = 0;
|
2007-05-15 10:07:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(rtimer_set(t, RTIMER_TIME(t) + OFF_TIME, 1, powercycle, ptr)) {
|
|
|
|
PRINTF("xmac: could not set rtimer\n");
|
2007-04-07 07:43:31 +02:00
|
|
|
}
|
2007-05-15 10:07:07 +02:00
|
|
|
t2 = rtimer_arch_now();
|
|
|
|
/* printf("xmac 1 %d\n", t2 - t1); */
|
2007-04-07 07:43:31 +02:00
|
|
|
PT_YIELD(&pt);
|
2007-05-15 10:07:07 +02:00
|
|
|
|
|
|
|
if(we_are_sending) {
|
|
|
|
PRINTF("xmac: we are sending 1, stopping timer\n");
|
|
|
|
PT_YIELD(&pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
t1 = rtimer_arch_now();
|
2007-04-07 07:43:31 +02:00
|
|
|
if(radio_is_on == 0) {
|
2007-05-15 10:07:07 +02:00
|
|
|
radio->on();
|
2007-04-07 07:43:31 +02:00
|
|
|
radio_is_on = 1;
|
|
|
|
LEDS_ON(LEDS_RED);
|
|
|
|
}
|
2007-05-15 10:07:07 +02:00
|
|
|
if(rtimer_set(t, RTIMER_TIME(t) + ON_TIME, 1, powercycle, ptr)) {
|
|
|
|
PRINTF("xmac: could not set rtimer\n");
|
|
|
|
}
|
|
|
|
t2 = rtimer_arch_now();
|
|
|
|
/* printf("xmac 2 %d\n", t2 - t1); */
|
|
|
|
|
2007-04-07 07:43:31 +02:00
|
|
|
PT_YIELD(&pt);
|
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
if(we_are_sending) {
|
|
|
|
PRINTF("xmac: we are sending 2, stopping timer\n");
|
|
|
|
PT_YIELD(&pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(long_off) {
|
|
|
|
|
|
|
|
/* XXX should wait for a complete packet that is not destined to
|
|
|
|
us to swisch past us. */
|
|
|
|
|
|
|
|
long_off = 0;
|
|
|
|
}
|
|
|
|
|
2007-04-07 07:43:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PT_END(&pt);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-15 10:07:07 +02:00
|
|
|
static void
|
|
|
|
send(void)
|
2007-04-07 07:43:31 +02:00
|
|
|
{
|
|
|
|
rtimer_clock_t t0, t1;
|
|
|
|
int strobes;
|
2007-05-15 10:07:07 +02:00
|
|
|
struct xmac_hdr *hdr;
|
|
|
|
int got_ack = 0;
|
2007-04-07 07:43:31 +02:00
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
we_are_sending = 1;
|
|
|
|
|
|
|
|
radio->on();
|
|
|
|
|
2007-04-07 07:43:31 +02:00
|
|
|
/* printf("xmac_send\n");*/
|
2007-05-15 10:07:07 +02:00
|
|
|
PRINTF("xmac: send() len %d\n", rimebuf_totlen());
|
2007-04-07 07:43:31 +02:00
|
|
|
|
|
|
|
rimebuf_hdralloc(sizeof(struct xmac_hdr));
|
2007-05-15 10:07:07 +02:00
|
|
|
hdr = rimebuf_hdrptr();
|
|
|
|
rimeaddr_copy(&hdr->sender, &rimeaddr_node_addr);
|
|
|
|
#if CHAMELEON
|
|
|
|
rimeaddr_copy(&hdr->receiver, packattr_aget(PACKATTR_RECEIVER));
|
|
|
|
#else
|
|
|
|
rimeaddr_copy(&hdr->receiver, &uc_receiver);
|
|
|
|
#endif
|
2007-04-07 07:43:31 +02:00
|
|
|
rimebuf_compact();
|
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
/* should_be_awake = 1;
|
|
|
|
while(!radio_is_on) {}*/
|
2007-04-07 07:43:31 +02:00
|
|
|
|
|
|
|
t0 = RTIMER_NOW();
|
|
|
|
strobes = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
struct xmac_hdr msg;
|
|
|
|
rtimer_clock_t t;
|
2007-05-15 10:07:07 +02:00
|
|
|
int len;
|
2007-04-07 07:43:31 +02:00
|
|
|
|
|
|
|
t = RTIMER_NOW();
|
|
|
|
|
|
|
|
LEDS_TOGGLE(LEDS_GREEN);
|
2007-05-15 10:07:07 +02:00
|
|
|
rimeaddr_copy(&msg.sender, &rimeaddr_node_addr);
|
|
|
|
#if CHAMELEON
|
|
|
|
rimeaddr_copy(&msg.receiver, packattr_aget(PACKATTR_RECEIVER));
|
|
|
|
#else
|
|
|
|
rimeaddr_copy(&msg.receiver, &uc_receiver);
|
|
|
|
#endif
|
2007-04-07 07:43:31 +02:00
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
/* printf("sender %d.%d receiver %d.%d node %d.%d.\n",
|
|
|
|
msg.sender.u8[0],msg.sender.u8[1],
|
|
|
|
msg.receiver.u8[0],msg.receiver.u8[1],
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);*/
|
|
|
|
radio->send((const u8_t *)&msg, sizeof(struct xmac_hdr));
|
|
|
|
|
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), t + STROBE_WAIT_TIME)) {
|
|
|
|
/* See if we got an ACK */
|
|
|
|
/* printf("befor read\n");*/
|
|
|
|
len = radio->read((u8_t *)&msg, sizeof(struct xmac_hdr));
|
|
|
|
/* printf("after read len %d\n", len);*/
|
|
|
|
if(len > 0) {
|
|
|
|
PRINTF("xmac len %d sender %d.%d receiver %d.%d node %d.%d.\n", len,
|
|
|
|
msg.sender.u8[0],msg.sender.u8[1],
|
|
|
|
msg.receiver.u8[0],msg.receiver.u8[1],
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
|
|
|
if(rimeaddr_cmp(&msg.receiver, &rimeaddr_node_addr)) {
|
|
|
|
|
|
|
|
/* We got an ACK from the receiver, so we can immediately send
|
|
|
|
the packet. */
|
|
|
|
got_ack = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PRINTF("xmac Strobe %d got_ack %d\n", strobes, got_ack);*/
|
2007-04-07 07:43:31 +02:00
|
|
|
++strobes;
|
2007-05-15 10:07:07 +02:00
|
|
|
if(got_ack) {
|
|
|
|
break;
|
|
|
|
}
|
2007-04-07 07:43:31 +02:00
|
|
|
} while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + OFF_TIME + ON_TIME));
|
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
radio->send(rimebuf_hdrptr(), rimebuf_totlen());
|
|
|
|
|
|
|
|
PRINTF("xmac: send, done\n");
|
|
|
|
|
|
|
|
/* printf("Strobe %d got_ack %d\n", strobes, got_ack);*/
|
|
|
|
|
2007-04-07 07:43:31 +02:00
|
|
|
LEDS_OFF(LEDS_GREEN);
|
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
PT_INIT(&pt);
|
|
|
|
if(rtimer_set(&rt, RTIMER_NOW() + ON_TIME, 1, powercycle, NULL)) {
|
|
|
|
PRINTF("xmac: could not set rtimer after send\n");
|
|
|
|
}
|
|
|
|
we_are_sending = 0;
|
|
|
|
|
2007-04-07 07:43:31 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-15 10:07:07 +02:00
|
|
|
static void
|
|
|
|
qsend(void)
|
2007-04-07 07:43:31 +02:00
|
|
|
{
|
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",
|
|
|
|
should_be_awake, someone_is_sending, we_are_sending, radio_is_on);
|
|
|
|
} else {
|
|
|
|
PRINTF("xmac: send immediately.\n");
|
|
|
|
send();
|
|
|
|
}
|
|
|
|
|
2007-04-07 07:43:31 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-15 10:07:07 +02:00
|
|
|
static void
|
|
|
|
input(const struct radio_driver *d)
|
2007-04-07 07:43:31 +02:00
|
|
|
{
|
2007-05-15 10:07:07 +02:00
|
|
|
struct xmac_hdr *hdr;
|
|
|
|
u8_t len;
|
2007-04-07 07:43:31 +02:00
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
rimebuf_clear();
|
2007-04-07 07:43:31 +02:00
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
PRINTF("xmac: input ");
|
|
|
|
len = d->read(rimebuf_dataptr(), RIMEBUF_SIZE);
|
|
|
|
PRINTF("%d bytes\n", len);
|
|
|
|
|
|
|
|
if(len > 0) {
|
|
|
|
someone_is_sending = 1;
|
|
|
|
rimebuf_set_datalen(len);
|
|
|
|
hdr = rimebuf_dataptr();
|
2007-04-07 07:43:31 +02:00
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
rimebuf_hdrreduce(sizeof(struct xmac_hdr));
|
|
|
|
if(rimeaddr_cmp(&hdr->receiver, &rimeaddr_node_addr)) {
|
|
|
|
should_be_awake = 1;
|
|
|
|
PRINTF("xmac: for us\n");
|
|
|
|
|
|
|
|
if(!rimeaddr_cmp(&hdr->sender, &rimeaddr_node_addr) &&
|
|
|
|
rimebuf_totlen() == 0) {
|
|
|
|
/* XXX Send an ACK and wait for packet . */
|
|
|
|
PRINTF("xmac: got sender %d.%d receiver %d.%d\n",
|
|
|
|
hdr->sender.u8[0],hdr->sender.u8[1],
|
|
|
|
hdr->receiver.u8[0],hdr->receiver.u8[1]);
|
|
|
|
while(!radio_is_on) {}
|
|
|
|
|
|
|
|
{
|
|
|
|
struct xmac_hdr msg;
|
|
|
|
rimeaddr_copy(&msg.receiver, &hdr->sender);
|
|
|
|
rimeaddr_copy(&msg.sender, &hdr->sender);
|
|
|
|
PRINTF("xmac: sending sender %d.%d receiver %d.%d\n",
|
|
|
|
msg.sender.u8[0],msg.sender.u8[1],
|
|
|
|
msg.receiver.u8[0],msg.receiver.u8[1]);
|
|
|
|
radio->send((const u8_t *)&msg, sizeof(struct xmac_hdr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(rimeaddr_cmp(&hdr->receiver, &rimeaddr_null)) {
|
|
|
|
/* Wait for the actual packet to arrive. */
|
|
|
|
should_be_awake = 1;
|
|
|
|
/* PRINTF("xmac: broadcast\n");*/
|
|
|
|
} else {
|
|
|
|
/* Go back to sleep. XXX should not turn on the radio again
|
|
|
|
until this packet has passed. */
|
|
|
|
should_be_awake = 0;
|
|
|
|
someone_is_sending = 0;
|
|
|
|
long_off = 1;
|
|
|
|
/* XXX set timer to turn someone_is_sending off again and send queued packet. */
|
|
|
|
/* PRINTF("xmac: not for us\n");*/
|
|
|
|
}
|
2007-04-07 07:43:31 +02:00
|
|
|
|
2007-05-15 10:07:07 +02:00
|
|
|
if(rimebuf_totlen() > 0) {
|
|
|
|
|
|
|
|
/* We have received the final packet, so we can go back to being
|
|
|
|
asleep. */
|
|
|
|
someone_is_sending = 0;
|
|
|
|
should_be_awake = 0;
|
|
|
|
PRINTF("xmac: final packet received\n");
|
|
|
|
rime_input();
|
|
|
|
/* XXX send queued packet. */
|
|
|
|
}
|
2007-04-07 07:43:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-15 10:07:07 +02:00
|
|
|
void
|
|
|
|
xmac_init(const struct radio_driver *d)
|
|
|
|
{
|
|
|
|
radio_is_on = 0;
|
|
|
|
should_be_awake = 0;
|
|
|
|
PT_INIT(&pt);
|
|
|
|
rtimer_set(&rt, RTIMER_NOW() + OFF_TIME, 1, powercycle, NULL);
|
|
|
|
|
|
|
|
rime_set_output(qsend);
|
|
|
|
radio = d;
|
|
|
|
radio->set_receive_function(input);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|