2010-02-18 21:58:59 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Common functionality for phase optimization in duty cycling radio protocols
|
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "net/mac/phase.h"
|
2010-06-14 21:19:16 +02:00
|
|
|
#include "net/packetbuf.h"
|
2010-02-28 21:19:47 +01:00
|
|
|
#include "sys/clock.h"
|
2010-06-14 09:34:36 +02:00
|
|
|
#include "sys/ctimer.h"
|
2010-06-14 21:19:16 +02:00
|
|
|
#include "net/queuebuf.h"
|
2013-07-29 18:49:21 +02:00
|
|
|
#include "net/nbr-table.h"
|
2013-07-03 19:14:10 +02:00
|
|
|
|
|
|
|
#if PHASE_CONF_DRIFT_CORRECT
|
|
|
|
#define PHASE_DRIFT_CORRECT PHASE_CONF_DRIFT_CORRECT
|
|
|
|
#else
|
|
|
|
#define PHASE_DRIFT_CORRECT 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct phase {
|
|
|
|
rtimer_clock_t time;
|
|
|
|
#if PHASE_DRIFT_CORRECT
|
|
|
|
rtimer_clock_t drift;
|
|
|
|
#endif
|
|
|
|
uint8_t noacks;
|
|
|
|
struct timer noacks_timer;
|
|
|
|
};
|
2010-02-28 21:19:47 +01:00
|
|
|
|
|
|
|
struct phase_queueitem {
|
|
|
|
struct ctimer timer;
|
|
|
|
mac_callback_t mac_callback;
|
|
|
|
void *mac_callback_ptr;
|
|
|
|
struct queuebuf *q;
|
2011-09-27 16:05:30 +02:00
|
|
|
struct rdc_buf_list *buf_list;
|
2010-02-28 21:19:47 +01:00
|
|
|
};
|
|
|
|
|
2010-04-03 15:28:30 +02:00
|
|
|
#define PHASE_DEFER_THRESHOLD 1
|
2010-03-14 23:59:23 +01:00
|
|
|
#define PHASE_QUEUESIZE 8
|
|
|
|
|
2010-09-13 15:39:05 +02:00
|
|
|
#define MAX_NOACKS 16
|
|
|
|
|
2010-12-16 23:43:07 +01:00
|
|
|
#define MAX_NOACKS_TIME CLOCK_SECOND * 30
|
2010-02-28 21:19:47 +01:00
|
|
|
|
2010-04-04 23:02:09 +02:00
|
|
|
MEMB(queued_packets_memb, struct phase_queueitem, PHASE_QUEUESIZE);
|
2013-07-29 18:49:21 +02:00
|
|
|
NBR_TABLE(struct phase, nbr_phase);
|
2010-02-18 21:58:59 +01:00
|
|
|
|
2010-04-06 23:37:54 +02:00
|
|
|
#define DEBUG 0
|
2010-03-14 23:59:23 +01:00
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#define PRINTDEBUG(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#define PRINTDEBUG(...)
|
|
|
|
#endif
|
2010-02-18 21:58:59 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-03 15:28:30 +02:00
|
|
|
void
|
2013-12-12 23:58:52 +01:00
|
|
|
phase_update(const linkaddr_t *neighbor, rtimer_clock_t time,
|
2010-03-14 23:59:23 +01:00
|
|
|
int mac_status)
|
2010-02-18 21:58:59 +01:00
|
|
|
{
|
|
|
|
struct phase *e;
|
|
|
|
|
|
|
|
/* If we have an entry for this neighbor already, we renew it. */
|
2013-07-03 19:14:10 +02:00
|
|
|
e = nbr_table_get_from_lladdr(nbr_phase, neighbor);
|
2010-04-03 15:28:30 +02:00
|
|
|
if(e != NULL) {
|
|
|
|
if(mac_status == MAC_TX_OK) {
|
2011-12-01 16:02:37 +01:00
|
|
|
#if PHASE_DRIFT_CORRECT
|
|
|
|
e->drift = time-e->time;
|
|
|
|
#endif
|
2010-04-03 15:28:30 +02:00
|
|
|
e->time = time;
|
|
|
|
}
|
|
|
|
/* If the neighbor didn't reply to us, it may have switched
|
|
|
|
phase (rebooted). We try a number of transmissions to it
|
|
|
|
before we drop it from the phase list. */
|
|
|
|
if(mac_status == MAC_TX_NOACK) {
|
|
|
|
PRINTF("phase noacks %d to %d.%d\n", e->noacks, neighbor->u8[0], neighbor->u8[1]);
|
|
|
|
e->noacks++;
|
2010-09-13 15:39:05 +02:00
|
|
|
if(e->noacks == 1) {
|
|
|
|
timer_set(&e->noacks_timer, MAX_NOACKS_TIME);
|
|
|
|
}
|
|
|
|
if(e->noacks >= MAX_NOACKS || timer_expired(&e->noacks_timer)) {
|
2010-12-18 23:12:53 +01:00
|
|
|
PRINTF("drop %d\n", neighbor->u8[0]);
|
2013-07-03 19:14:10 +02:00
|
|
|
nbr_table_remove(nbr_phase, e);
|
2010-04-03 15:28:30 +02:00
|
|
|
return;
|
2010-03-14 23:59:23 +01:00
|
|
|
}
|
2010-04-03 15:28:30 +02:00
|
|
|
} else if(mac_status == MAC_TX_OK) {
|
|
|
|
e->noacks = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* No matching phase was found, so we allocate a new one. */
|
|
|
|
if(mac_status == MAC_TX_OK && e == NULL) {
|
2015-09-20 21:02:30 +02:00
|
|
|
e = nbr_table_add_lladdr(nbr_phase, neighbor, NBR_TABLE_REASON_MAC, NULL);
|
2013-07-03 19:14:10 +02:00
|
|
|
if(e) {
|
|
|
|
e->time = time;
|
2011-12-01 16:02:37 +01:00
|
|
|
#if PHASE_DRIFT_CORRECT
|
|
|
|
e->drift = 0;
|
|
|
|
#endif
|
2010-04-03 15:28:30 +02:00
|
|
|
e->noacks = 0;
|
2013-07-03 19:14:10 +02:00
|
|
|
}
|
2010-02-18 21:58:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-02-28 21:19:47 +01:00
|
|
|
static void
|
|
|
|
send_packet(void *ptr)
|
|
|
|
{
|
|
|
|
struct phase_queueitem *p = ptr;
|
|
|
|
|
2011-09-27 16:05:30 +02:00
|
|
|
if(p->buf_list == NULL) {
|
|
|
|
queuebuf_to_packetbuf(p->q);
|
|
|
|
queuebuf_free(p->q);
|
|
|
|
NETSTACK_RDC.send(p->mac_callback, p->mac_callback_ptr);
|
|
|
|
} else {
|
|
|
|
NETSTACK_RDC.send_list(p->mac_callback, p->mac_callback_ptr, p->buf_list);
|
|
|
|
}
|
|
|
|
|
2010-04-04 23:02:09 +02:00
|
|
|
memb_free(&queued_packets_memb, p);
|
2010-02-28 21:19:47 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
phase_status_t
|
2013-12-12 23:58:52 +01:00
|
|
|
phase_wait(const linkaddr_t *neighbor, rtimer_clock_t cycle_time,
|
2010-12-16 23:43:07 +01:00
|
|
|
rtimer_clock_t guard_time,
|
2011-09-27 16:05:30 +02:00
|
|
|
mac_callback_t mac_callback, void *mac_callback_ptr,
|
2011-10-04 10:29:40 +02:00
|
|
|
struct rdc_buf_list *buf_list)
|
2010-02-18 21:58:59 +01:00
|
|
|
{
|
|
|
|
struct phase *e;
|
2013-12-12 23:58:52 +01:00
|
|
|
// const linkaddr_t *neighbor = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
|
2010-03-29 23:50:01 +02:00
|
|
|
/* We go through the list of phases to find if we have recorded a
|
|
|
|
phase for this particular neighbor. If so, we can compute the
|
|
|
|
time for the next expected phase and setup a ctimer to switch on
|
|
|
|
the radio just before the phase. */
|
2013-07-03 19:14:10 +02:00
|
|
|
e = nbr_table_get_from_lladdr(nbr_phase, neighbor);
|
2011-10-04 10:29:40 +02:00
|
|
|
if(e != NULL) {
|
2011-09-27 16:05:30 +02:00
|
|
|
rtimer_clock_t wait, now, expected, sync;
|
2010-04-03 15:28:30 +02:00
|
|
|
clock_time_t ctimewait;
|
|
|
|
|
|
|
|
/* We expect phases to happen every CYCLE_TIME time
|
|
|
|
units. The next expected phase is at time e->time +
|
|
|
|
CYCLE_TIME. To compute a relative offset, we subtract
|
|
|
|
with clock_time(). Because we are only interested in turning
|
|
|
|
on the radio within the CYCLE_TIME period, we compute the
|
|
|
|
waiting time with modulo CYCLE_TIME. */
|
|
|
|
|
|
|
|
/* printf("neighbor phase 0x%02x (cycle 0x%02x)\n", e->time & (cycle_time - 1),
|
|
|
|
cycle_time);*/
|
2010-12-16 23:43:07 +01:00
|
|
|
|
2010-04-03 15:28:30 +02:00
|
|
|
/* if(e->noacks > 0) {
|
|
|
|
printf("additional wait %d\n", additional_wait);
|
|
|
|
}*/
|
|
|
|
|
|
|
|
now = RTIMER_NOW();
|
2011-09-27 16:05:30 +02:00
|
|
|
|
|
|
|
sync = (e == NULL) ? now : e->time;
|
2011-12-01 16:02:37 +01:00
|
|
|
|
|
|
|
#if PHASE_DRIFT_CORRECT
|
2011-12-05 16:28:04 +01:00
|
|
|
{
|
|
|
|
int32_t s;
|
|
|
|
if(e->drift > cycle_time) {
|
|
|
|
s = e->drift % cycle_time / (e->drift / cycle_time); /* drift per cycle */
|
|
|
|
s = s * (now - sync) / cycle_time; /* estimated drift to now */
|
|
|
|
sync += s; /* add it in */
|
|
|
|
}
|
|
|
|
}
|
2011-12-01 16:02:37 +01:00
|
|
|
#endif
|
|
|
|
|
2011-12-05 16:28:04 +01:00
|
|
|
/* Check if cycle_time is a power of two */
|
|
|
|
if(!(cycle_time & (cycle_time - 1))) {
|
|
|
|
/* Faster if cycle_time is a power of two */
|
|
|
|
wait = (rtimer_clock_t)((sync - now) & (cycle_time - 1));
|
|
|
|
} else {
|
|
|
|
/* Works generally */
|
|
|
|
wait = cycle_time - (rtimer_clock_t)((now - sync) % cycle_time);
|
|
|
|
}
|
2011-12-01 16:02:37 +01:00
|
|
|
|
2010-12-16 23:43:07 +01:00
|
|
|
if(wait < guard_time) {
|
2010-04-03 15:28:30 +02:00
|
|
|
wait += cycle_time;
|
|
|
|
}
|
2011-09-27 16:05:30 +02:00
|
|
|
|
2010-12-16 23:43:07 +01:00
|
|
|
ctimewait = (CLOCK_SECOND * (wait - guard_time)) / RTIMER_ARCH_SECOND;
|
2011-09-27 16:05:30 +02:00
|
|
|
|
2011-10-04 10:29:40 +02:00
|
|
|
if(ctimewait > PHASE_DEFER_THRESHOLD) {
|
2010-04-03 15:28:30 +02:00
|
|
|
struct phase_queueitem *p;
|
2010-02-28 21:19:47 +01:00
|
|
|
|
2010-04-04 23:02:09 +02:00
|
|
|
p = memb_alloc(&queued_packets_memb);
|
2010-04-03 15:28:30 +02:00
|
|
|
if(p != NULL) {
|
2011-09-27 16:05:30 +02:00
|
|
|
if(buf_list == NULL) {
|
2014-04-26 11:39:51 +02:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_IS_CREATED_AND_SECURED, 1);
|
2011-09-27 16:05:30 +02:00
|
|
|
p->q = queuebuf_new_from_packetbuf();
|
2014-04-22 15:41:36 +02:00
|
|
|
if(p->q == NULL) {
|
|
|
|
/* memory allocation failed */
|
|
|
|
memb_free(&queued_packets_memb, p);
|
|
|
|
return PHASE_UNKNOWN;
|
|
|
|
}
|
2010-02-28 21:19:47 +01:00
|
|
|
}
|
2011-09-27 16:05:30 +02:00
|
|
|
p->mac_callback = mac_callback;
|
|
|
|
p->mac_callback_ptr = mac_callback_ptr;
|
|
|
|
p->buf_list = buf_list;
|
|
|
|
ctimer_set(&p->timer, ctimewait, send_packet, p);
|
|
|
|
return PHASE_DEFERRED;
|
2010-02-28 21:19:47 +01:00
|
|
|
}
|
2010-04-03 15:28:30 +02:00
|
|
|
}
|
2011-09-27 16:05:30 +02:00
|
|
|
|
2010-12-16 23:43:07 +01:00
|
|
|
expected = now + wait - guard_time;
|
2010-04-03 15:28:30 +02:00
|
|
|
if(!RTIMER_CLOCK_LT(expected, now)) {
|
|
|
|
/* Wait until the receiver is expected to be awake */
|
2010-12-16 23:43:07 +01:00
|
|
|
while(RTIMER_CLOCK_LT(RTIMER_NOW(), expected));
|
2010-02-18 21:58:59 +01:00
|
|
|
}
|
2010-04-03 15:28:30 +02:00
|
|
|
return PHASE_SEND_NOW;
|
2010-02-18 21:58:59 +01:00
|
|
|
}
|
2010-03-14 23:59:23 +01:00
|
|
|
return PHASE_UNKNOWN;
|
2010-02-18 21:58:59 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2013-07-03 19:14:10 +02:00
|
|
|
phase_init(void)
|
2010-02-18 21:58:59 +01:00
|
|
|
{
|
2010-04-04 23:02:09 +02:00
|
|
|
memb_init(&queued_packets_memb);
|
2013-07-03 19:14:10 +02:00
|
|
|
nbr_table_register(nbr_phase, NULL);
|
2010-02-18 21:58:59 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|