From bd818a82164e7a40010adc2e2b9c3c1a2180dd71 Mon Sep 17 00:00:00 2001 From: kusy Date: Mon, 30 Nov 2009 21:20:22 +0000 Subject: [PATCH] randomizing timer to support better sync between far away neighbors --- tos/lib/ftsp/TimeSyncC.nc | 7 +++++-- tos/lib/ftsp/TimeSyncMsg.h | 2 +- tos/lib/ftsp/TimeSyncP.nc | 11 ++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tos/lib/ftsp/TimeSyncC.nc b/tos/lib/ftsp/TimeSyncC.nc index 8022d8a9..61cbcd93 100644 --- a/tos/lib/ftsp/TimeSyncC.nc +++ b/tos/lib/ftsp/TimeSyncC.nc @@ -52,8 +52,8 @@ implementation components TimeSyncMessageC as ActiveMessageC; TimeSyncP.RadioControl -> ActiveMessageC; - TimeSyncP.Send -> ActiveMessageC.TimeSyncAMSendMilli[AM_TIMESYNCMSG]; - TimeSyncP.Receive -> ActiveMessageC.Receive[AM_TIMESYNCMSG]; + TimeSyncP.Send -> ActiveMessageC.TimeSyncAMSendMilli[TIMESYNC_AM_FTSP]; + TimeSyncP.Receive -> ActiveMessageC.Receive[TIMESYNC_AM_FTSP]; TimeSyncP.TimeSyncPacket -> ActiveMessageC; components HilTimerMilliC; @@ -62,6 +62,9 @@ implementation components new TimerMilliC() as TimerC; TimeSyncP.Timer -> TimerC; + components RandomC; + TimeSyncP.Random -> RandomC; + #if defined(TIMESYNC_LEDS) components LedsC; #else diff --git a/tos/lib/ftsp/TimeSyncMsg.h b/tos/lib/ftsp/TimeSyncMsg.h index f45c2b34..74a90197 100644 --- a/tos/lib/ftsp/TimeSyncMsg.h +++ b/tos/lib/ftsp/TimeSyncMsg.h @@ -49,7 +49,7 @@ typedef nx_struct TimeSyncMsg } TimeSyncMsg; enum { - AM_TIMESYNCMSG = 0x3E, + TIMESYNC_AM_FTSP = 0x3E, TIMESYNCMSG_LEN = sizeof(TimeSyncMsg) - sizeof(nx_uint32_t), TS_TIMER_MODE = 0, // see TimeSyncMode interface TS_USER_MODE = 1, // see TimeSyncMode interface diff --git a/tos/lib/ftsp/TimeSyncP.nc b/tos/lib/ftsp/TimeSyncP.nc index db59408b..177c1dff 100644 --- a/tos/lib/ftsp/TimeSyncP.nc +++ b/tos/lib/ftsp/TimeSyncP.nc @@ -43,6 +43,7 @@ generic module TimeSyncP(typedef precision_tag) interface TimeSyncAMSend as Send; interface Receive; interface Timer; + interface Random; interface Leds; interface TimeSyncPacket; interface LocalTime as LocalTime; @@ -316,6 +317,7 @@ implementation if( diff < -16 || diff > 16 ) return msg; #endif + if( (state & STATE_PROCESSING) == 0 && call TimeSyncPacket.isValid(msg)) { message_t* old = processedMsg; @@ -358,7 +360,6 @@ implementation } outgoingMsg->globalTime = globalTime; - #ifdef LOW_POWER_LISTENING call LowPowerListening.setRemoteWakeupInterval(&outgoingMsgBuffer, LPL_INTERVAL); #endif @@ -414,11 +415,8 @@ implementation } command error_t TimeSyncMode.setMode(uint8_t mode_){ - if (mode == mode_) - return SUCCESS; - if (mode_ == TS_TIMER_MODE){ - call Timer.startPeriodic((uint32_t)1000 * BEACON_RATE); + call Timer.startPeriodic((uint32_t)(896U+(call Random.rand16()&0xFF)) * BEACON_RATE); } else call Timer.stop(); @@ -466,10 +464,9 @@ implementation command error_t StdControl.start() { - mode = TS_TIMER_MODE; heartBeats = 0; outgoingMsg->nodeID = TOS_NODE_ID; - call Timer.startPeriodic((uint32_t)1000 * BEACON_RATE); + call TimeSyncMode.setMode(TS_TIMER_MODE); return SUCCESS; } -- 2.39.2