2007-08-31 15:42:22 +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.
|
|
|
|
*
|
2009-03-12 22:58:20 +01:00
|
|
|
* $Id: tdma_mac.c,v 1.6 2009/03/12 21:58:20 adamdunkels Exp $
|
2007-08-31 15:42:22 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "net/mac/tdma_mac.h"
|
2009-03-12 22:58:20 +01:00
|
|
|
#include "net/rime/packetbuf.h"
|
2007-08-31 15:42:22 +02:00
|
|
|
#include "net/uip-fw.h"
|
2007-09-18 12:37:17 +02:00
|
|
|
#include "sys/rtimer.h"
|
|
|
|
#include "net/rime.h"
|
2007-08-31 15:42:22 +02:00
|
|
|
#include "lib/memb.h"
|
|
|
|
#include "lib/list.h"
|
2007-09-18 12:37:17 +02:00
|
|
|
#include "dev/leds.h"
|
2007-08-31 15:42:22 +02:00
|
|
|
#include "node-id.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#define DEBUG 1
|
|
|
|
#if DEBUG
|
2007-09-18 12:37:17 +02:00
|
|
|
#define DLEDS_ON(x) leds_on(x)
|
|
|
|
#define DLEDS_OFF(x) leds_off(x)
|
|
|
|
#define DLEDS_TOGGLE(x) leds_toggle(x)
|
2007-08-31 15:42:22 +02:00
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
2007-09-18 12:37:17 +02:00
|
|
|
#define DLEDS_ON(x)
|
|
|
|
#define DLEDS_OFF(x)
|
|
|
|
#define DLEDS_TOGGLE(x)
|
2007-08-31 15:42:22 +02:00
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* TDMA configuration */
|
2007-09-18 12:37:17 +02:00
|
|
|
#define NR_SLOTS 3
|
|
|
|
#define SLOT_LENGTH (RTIMER_SECOND/3)
|
|
|
|
#define GUARD_PERIOD (RTIMER_SECOND/12)
|
2007-08-31 15:42:22 +02:00
|
|
|
|
|
|
|
#define MY_SLOT (node_id % NR_SLOTS)
|
2007-09-18 12:37:17 +02:00
|
|
|
#define PERIOD_LENGTH RTIMER_SECOND
|
2007-08-31 15:42:22 +02:00
|
|
|
|
2007-09-18 12:37:17 +02:00
|
|
|
/* Buffers */
|
|
|
|
#define NUM_PACKETS 8
|
2008-02-24 23:10:30 +01:00
|
|
|
uint8_t lastqueued = 0;
|
|
|
|
uint8_t nextsend = 0;
|
|
|
|
uint8_t freeslot = 0;
|
2007-09-18 12:37:17 +02:00
|
|
|
struct queuebuf* data[NUM_PACKETS];
|
|
|
|
int id[NUM_PACKETS];
|
2007-08-31 15:42:22 +02:00
|
|
|
|
2007-09-18 12:37:17 +02:00
|
|
|
static struct rtimer rtimer;
|
2008-02-24 23:10:30 +01:00
|
|
|
uint8_t timer_on = 0;
|
2007-08-31 15:42:22 +02:00
|
|
|
|
2007-09-18 12:37:17 +02:00
|
|
|
static const struct radio_driver *radio;
|
|
|
|
static void (* receiver_callback)(const struct mac_driver *);
|
|
|
|
static int id_counter = 0;
|
|
|
|
static int sent_counter = 0;
|
2007-08-31 15:42:22 +02:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-09-18 12:37:17 +02:00
|
|
|
static char
|
|
|
|
transmitter(struct rtimer *t, void *ptr)
|
2007-08-31 15:42:22 +02:00
|
|
|
{
|
2007-09-18 12:37:17 +02:00
|
|
|
int r;
|
|
|
|
rtimer_clock_t now, rest, period_start, slot_start;
|
|
|
|
|
|
|
|
/* Calculate slot start time */
|
|
|
|
now = RTIMER_NOW();
|
|
|
|
rest = now % PERIOD_LENGTH;
|
|
|
|
period_start = now - rest;
|
|
|
|
slot_start = period_start + MY_SLOT*SLOT_LENGTH;
|
|
|
|
|
|
|
|
/* Check if we are inside our slot */
|
2007-11-26 16:31:32 +01:00
|
|
|
if(now < slot_start ||
|
|
|
|
now > slot_start + SLOT_LENGTH - GUARD_PERIOD) {
|
2007-09-18 12:37:17 +02:00
|
|
|
PRINTF("TIMER We are outside our slot: %u != [%u,%u]\n", now, slot_start, slot_start + SLOT_LENGTH);
|
2007-11-26 16:31:32 +01:00
|
|
|
while(now > slot_start + SLOT_LENGTH - GUARD_PERIOD) {
|
2007-09-18 12:37:17 +02:00
|
|
|
slot_start += PERIOD_LENGTH;
|
|
|
|
}
|
2008-11-12 13:42:41 +01:00
|
|
|
|
2007-09-18 12:37:17 +02:00
|
|
|
PRINTF("TIMER Rescheduling until %u\n", slot_start);
|
|
|
|
r = rtimer_set(&rtimer, slot_start, 1,
|
|
|
|
(void (*)(struct rtimer *, void *))transmitter, NULL);
|
2007-11-26 16:31:32 +01:00
|
|
|
if(r) {
|
2007-09-18 12:37:17 +02:00
|
|
|
PRINTF("TIMER Error #1: %d\n", r);
|
2007-08-31 15:42:22 +02:00
|
|
|
}
|
2007-09-18 12:37:17 +02:00
|
|
|
|
|
|
|
return 1;
|
2007-08-31 15:42:22 +02:00
|
|
|
}
|
2007-09-18 12:37:17 +02:00
|
|
|
|
|
|
|
/* Transmit queued packets */
|
2007-11-26 16:31:32 +01:00
|
|
|
while(nextsend != freeslot) {
|
2007-09-18 12:37:17 +02:00
|
|
|
PRINTF("RADIO Transmitting packet #%i\n", id[nextsend]);
|
2007-11-26 16:31:32 +01:00
|
|
|
if(!radio->send(queuebuf_dataptr(data[nextsend]),
|
|
|
|
queuebuf_datalen(data[nextsend]))) {
|
2007-09-18 12:37:17 +02:00
|
|
|
sent_counter++;
|
|
|
|
PRINTF("RADIO Transmit OK for #%i, total=%i\n", id[nextsend], sent_counter);
|
|
|
|
DLEDS_TOGGLE(LEDS_GREEN);
|
2007-11-26 16:31:32 +01:00
|
|
|
} else {
|
2007-09-18 12:37:17 +02:00
|
|
|
PRINTF("RADIO Transmit failed for #%i, total=%i\n", id[nextsend], sent_counter);
|
|
|
|
DLEDS_TOGGLE(LEDS_RED);
|
|
|
|
}
|
|
|
|
|
|
|
|
nextsend = (nextsend + 1) % NUM_PACKETS;
|
|
|
|
|
|
|
|
/* Recalculate new slot */
|
2007-11-26 16:31:32 +01:00
|
|
|
if(RTIMER_NOW() > slot_start + SLOT_LENGTH - GUARD_PERIOD) {
|
2007-09-18 12:37:17 +02:00
|
|
|
PRINTF("TIMER No more time to transmit\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate time of our next slot */
|
|
|
|
slot_start += PERIOD_LENGTH;
|
|
|
|
PRINTF("TIMER Rescheduling until %u\n", slot_start);
|
|
|
|
r = rtimer_set(&rtimer, slot_start, 1,
|
|
|
|
(void (*)(struct rtimer *, void *))transmitter, NULL);
|
2007-11-26 16:31:32 +01:00
|
|
|
if(r) {
|
2007-09-18 12:37:17 +02:00
|
|
|
PRINTF("TIMER Error #2: %d\n", r);
|
|
|
|
}
|
2008-11-12 13:42:41 +01:00
|
|
|
|
2007-09-18 12:37:17 +02:00
|
|
|
return 0;
|
2007-08-31 15:42:22 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
send(void)
|
|
|
|
{
|
2007-09-18 12:37:17 +02:00
|
|
|
int r;
|
|
|
|
id_counter++;
|
2008-11-12 13:42:41 +01:00
|
|
|
|
2007-09-18 12:37:17 +02:00
|
|
|
/* Clean up already sent packets */
|
2007-11-26 16:31:32 +01:00
|
|
|
while(lastqueued != nextsend) {
|
2007-09-18 12:37:17 +02:00
|
|
|
PRINTF("BUFFER Cleaning up packet #%i\n", id[lastqueued]);
|
|
|
|
queuebuf_free(data[lastqueued]);
|
|
|
|
data[lastqueued] = NULL;
|
|
|
|
|
|
|
|
lastqueued = (lastqueued + 1) % NUM_PACKETS;
|
|
|
|
}
|
2008-11-12 13:42:41 +01:00
|
|
|
|
2007-11-26 16:31:32 +01:00
|
|
|
if((freeslot + 1) % NUM_PACKETS == lastqueued) {
|
2007-09-18 12:37:17 +02:00
|
|
|
PRINTF("BUFFER Buffer full, dropping packet #%i\n", (id_counter+1));
|
2007-08-31 15:42:22 +02:00
|
|
|
return UIP_FW_DROPPED;
|
|
|
|
}
|
|
|
|
|
2007-09-18 12:37:17 +02:00
|
|
|
/* Allocate queue buf for packet */
|
2009-03-12 22:58:20 +01:00
|
|
|
data[freeslot] = queuebuf_new_from_packetbuf();
|
2007-09-18 12:37:17 +02:00
|
|
|
id[freeslot] = id_counter;
|
2007-11-26 16:31:32 +01:00
|
|
|
if(data[freeslot] == NULL) {
|
2007-09-18 12:37:17 +02:00
|
|
|
PRINTF("BUFFER Queuebuffer full, dropping packet #%i\n", id[freeslot]);
|
2007-08-31 15:42:22 +02:00
|
|
|
return UIP_FW_DROPPED;
|
|
|
|
}
|
2007-09-18 12:37:17 +02:00
|
|
|
PRINTF("BUFFER Wrote packet #%i to buffer \n", id[freeslot]);
|
|
|
|
|
|
|
|
freeslot = (freeslot + 1) % NUM_PACKETS;
|
2008-11-12 13:42:41 +01:00
|
|
|
|
2007-11-26 16:31:32 +01:00
|
|
|
if(!timer_on) {
|
2007-09-18 12:37:17 +02:00
|
|
|
PRINTF("TIMER Starting timer\n");
|
|
|
|
r = rtimer_set(&rtimer, RTIMER_NOW() + RTIMER_SECOND, 1,
|
|
|
|
(void (*)(struct rtimer *, void *))transmitter, NULL);
|
2007-11-26 16:31:32 +01:00
|
|
|
if(r) {
|
2007-09-18 12:37:17 +02:00
|
|
|
PRINTF("TIMER Error #3: %d\n", r);
|
2007-11-26 16:31:32 +01:00
|
|
|
} else {
|
2007-09-18 12:37:17 +02:00
|
|
|
timer_on = 1;
|
|
|
|
}
|
|
|
|
}
|
2007-08-31 15:42:22 +02:00
|
|
|
|
|
|
|
return UIP_FW_OK; /* TODO Return what? */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
input(const struct radio_driver *d)
|
|
|
|
{
|
|
|
|
receiver_callback(&tdma_mac_driver);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
read(void)
|
|
|
|
{
|
|
|
|
int len;
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_clear();
|
|
|
|
len = radio->read(packetbuf_dataptr(), PACKETBUF_SIZE);
|
|
|
|
packetbuf_set_datalen(len);
|
2007-08-31 15:42:22 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
set_receive_function(void (* recv)(const struct mac_driver *))
|
|
|
|
{
|
|
|
|
receiver_callback = recv;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
on(void)
|
|
|
|
{
|
|
|
|
return radio->on();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
off(void)
|
|
|
|
{
|
|
|
|
return radio->off();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-11-12 13:42:41 +01:00
|
|
|
const struct mac_driver *
|
2007-08-31 15:42:22 +02:00
|
|
|
tdma_mac_init(const struct radio_driver *d)
|
|
|
|
{
|
2007-09-18 12:37:17 +02:00
|
|
|
int i;
|
2007-11-26 16:31:32 +01:00
|
|
|
for(i = 0; i < NUM_PACKETS; i++) {
|
2007-09-18 12:37:17 +02:00
|
|
|
data[i] = NULL;
|
|
|
|
}
|
2007-08-31 15:42:22 +02:00
|
|
|
|
|
|
|
radio = d;
|
|
|
|
radio->set_receive_function(input);
|
|
|
|
radio->on();
|
2008-11-12 13:42:41 +01:00
|
|
|
|
|
|
|
return &tdma_mac_driver;
|
2007-08-31 15:42:22 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
const struct mac_driver tdma_mac_driver = {
|
2008-11-12 13:42:41 +01:00
|
|
|
"TDMA MAC",
|
|
|
|
send,
|
|
|
|
read,
|
|
|
|
set_receive_function,
|
|
|
|
on,
|
|
|
|
off,
|
2007-08-31 15:42:22 +02:00
|
|
|
};
|