From b43f7830b36e7426c02ad97c71ed58a894aad5a8 Mon Sep 17 00:00:00 2001 From: adamdunkels Date: Wed, 31 Mar 2010 20:27:15 +0000 Subject: [PATCH] Fine-tuned the timing and phase lock optimization --- core/net/mac/contikimac.c | 19 +++++++++++-------- core/net/mac/phase.c | 23 +++++++++++++++-------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/core/net/mac/contikimac.c b/core/net/mac/contikimac.c index f1b162a44..510f59451 100644 --- a/core/net/mac/contikimac.c +++ b/core/net/mac/contikimac.c @@ -28,7 +28,7 @@ * * This file is part of the Contiki operating system. * - * $Id: contikimac.c,v 1.20 2010/03/31 11:54:38 adamdunkels Exp $ + * $Id: contikimac.c,v 1.21 2010/03/31 20:27:15 adamdunkels Exp $ */ /** @@ -99,13 +99,15 @@ struct announcement_msg { #define CYCLE_TIME (RTIMER_ARCH_SECOND / MAC_CHANNEL_CHECK_RATE) #endif -#define MAX_PHASE_STROBES 12 +#define MAX_PHASE_STROBE_TIME RTIMER_ARCH_SECOND / 20 #define CCA_COUNT_MAX 2 #define CCA_CHECK_TIME RTIMER_ARCH_SECOND / 8192 #define CCA_SLEEP_TIME RTIMER_ARCH_SECOND / 2000 //+ CCA_CHECK_TIME #define CHECK_TIME (CCA_COUNT_MAX * (CCA_CHECK_TIME + CCA_SLEEP_TIME)) +#define STROBE_TIME (CYCLE_TIME + 2 * CHECK_TIME) + #define STREAM_CCA_COUNT (CYCLE_TIME / (CCA_SLEEP_TIME + CCA_CHECK_TIME) - CCA_COUNT_MAX) #define GUARD_TIME 7 * CHECK_TIME @@ -130,9 +132,6 @@ struct announcement_msg { cycle. */ #define ANNOUNCEMENT_TIME (random_rand() % (ANNOUNCEMENT_PERIOD)) -#define STROBE_WAIT_TIME (RTIMER_ARCH_SECOND / 600) - -#define STROBE_TIME (CYCLE_TIME + 2 * CHECK_TIME) #define ACK_LEN 3 @@ -415,6 +414,12 @@ parse_announcements(void) } printf("\n"); */ + if(adata.num / sizeof(struct announcement_data) > sizeof(struct announcement_msg)) { + /* Sanity check. The number of announcements is too large - + corrupt packet has been received. */ + return 0; + } + for(i = 0; i < adata.num; ++i) { /* printf("%d.%d: announcement %d: %d\n", rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], @@ -657,7 +662,7 @@ send_packet(mac_callback_t mac_callback, void *mac_callback_ptr) watchdog_periodic(); - if(is_known_receiver && strobes > MAX_PHASE_STROBES) { + if(is_known_receiver && !RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + MAX_PHASE_STROBE_TIME)) { break; } @@ -692,7 +697,6 @@ send_packet(mac_callback_t mac_callback, void *mac_callback_ptr) #endif len = NETSTACK_RADIO.read(ackbuf, ACK_LEN); if(len == ACK_LEN) { - leds_on(LEDS_BLUE); got_strobe_ack = 1; // encounter_time = last_transmission_time; encounter_time = now; @@ -775,7 +779,6 @@ send_packet(mac_callback_t mac_callback, void *mac_callback_ptr) } #endif /* WITH_PHASE_OPTIMIZATION */ - leds_off(LEDS_BLUE); return ret; } /*---------------------------------------------------------------------------*/ diff --git a/core/net/mac/phase.c b/core/net/mac/phase.c index 7368fcd5c..ee355a3ab 100644 --- a/core/net/mac/phase.c +++ b/core/net/mac/phase.c @@ -28,7 +28,7 @@ * * This file is part of the Contiki operating system. * - * $Id: phase.c,v 1.6 2010/03/31 11:54:38 adamdunkels Exp $ + * $Id: phase.c,v 1.7 2010/03/31 20:27:15 adamdunkels Exp $ */ /** @@ -61,7 +61,7 @@ struct phase_queueitem { MEMB(phase_memb, struct phase_queueitem, PHASE_QUEUESIZE); -#define DEBUG 1 +#define DEBUG 0 #if DEBUG #include #define PRINTF(...) printf(__VA_ARGS__) @@ -89,7 +89,7 @@ phase_update(const struct phase_list *list, 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]); + PRINTF("phase noacks %d to %d.%d\n", e->noacks, neighbor->u8[0], neighbor->u8[1]); e->noacks++; if(e->noacks >= MAX_NOACKS) { list_remove(*list->list, e); @@ -149,7 +149,7 @@ phase_wait(struct phase_list *list, const rimeaddr_t *neighbor = packetbuf_addr(PACKETBUF_ADDR_RECEIVER); if(rimeaddr_cmp(neighbor, &e->neighbor)) { - rtimer_clock_t wait, now, expected; + rtimer_clock_t wait, now, expected, additional_wait; clock_time_t ctimewait; /* We expect phases to happen every CYCLE_TIME time @@ -161,14 +161,21 @@ phase_wait(struct phase_list *list, /* printf("neighbor phase 0x%02x (cycle 0x%02x)\n", e->time & (cycle_time - 1), cycle_time);*/ + + additional_wait = 2 * e->noacks * wait_before; + + /* if(e->noacks > 0) { + printf("additional wait %d\n", additional_wait); + }*/ + now = RTIMER_NOW(); - wait = (rtimer_clock_t)((e->time - now - e->noacks * wait_before) & + wait = (rtimer_clock_t)((e->time - now) & (cycle_time - 1)); - if(wait < wait_before) { + if(wait < wait_before + additional_wait) { wait += cycle_time; } - ctimewait = (CLOCK_SECOND * (wait - wait_before)) / RTIMER_ARCH_SECOND; + ctimewait = (CLOCK_SECOND * (wait - wait_before - additional_wait)) / RTIMER_ARCH_SECOND; if(ctimewait > PHASE_DEFER_THRESHOLD) { struct phase_queueitem *p; @@ -187,7 +194,7 @@ phase_wait(struct phase_list *list, } } - expected = now + wait - wait_before; + expected = now + wait - wait_before - additional_wait; if(!RTIMER_CLOCK_LT(expected, now)) { /* Wait until the receiver is expected to be awake */ while(RTIMER_CLOCK_LT(RTIMER_NOW(), expected)) {