X-Git-Url: https://oss.titaniummirror.com/gitweb/?a=blobdiff_plain;f=tos%2Flib%2Fftsp%2FTimeSyncP.nc;h=6a17381db172e17fc18e04081fc29f3bca3ad3e8;hb=54d0ab54d6a8e3b5eb563efc5587982dc4dabf4d;hp=6aed03b86a379ec579b28a6f18dc7274c122a551;hpb=cc933d1a7bccb13de00fc430b0203827f5463797;p=tinyos-2.x.git diff --git a/tos/lib/ftsp/TimeSyncP.nc b/tos/lib/ftsp/TimeSyncP.nc index 6aed03b8..6a17381d 100644 --- a/tos/lib/ftsp/TimeSyncP.nc +++ b/tos/lib/ftsp/TimeSyncP.nc @@ -43,9 +43,16 @@ generic module TimeSyncP(typedef precision_tag) interface TimeSyncAMSend as Send; interface Receive; interface Timer; + interface Random; interface Leds; interface TimeSyncPacket; interface LocalTime as LocalTime; + + +#ifdef LOW_POWER_LISTENING + interface LowPowerListening; +#endif + } } implementation @@ -61,7 +68,7 @@ implementation IGNORE_ROOT_MSG = 4, // after becoming the root ignore other roots messages (in send period) ENTRY_VALID_LIMIT = 4, // number of entries to become synchronized ENTRY_SEND_LIMIT = 3, // number of entries to send sync messages - ENTRY_THROWOUT_LIMIT = 100, // if time sync error is bigger than this clear the table + ENTRY_THROWOUT_LIMIT = 500, // if time sync error is bigger than this clear the table }; typedef struct TableItem @@ -227,8 +234,6 @@ implementation uint32_t age, oldestTime = 0; int32_t timeError; - tableEntries = 0; - // clear table if the received entry's been inconsistent for some time timeError = msg->localTime; call GlobalTime.local2Global((uint32_t*)(&timeError)); @@ -238,10 +243,11 @@ implementation { if (++numErrors>3) clearTable(); + return; // don't incorporate a bad reading } - else - numErrors = 0; + tableEntries = 0; // don't reset table size unless you're recounting + numErrors = 0; for(i = 0; i < MAX_ENTRIES; ++i) { age = msg->localTime - table[i].localTime; @@ -278,7 +284,7 @@ implementation if( msg->rootID < outgoingMsg->rootID && // jw: after becoming the root ignore other roots messages (in send period) - ~(heartBeats < IGNORE_ROOT_MSG && outgoingMsg->rootID == TOS_NODE_ID) ){ + !(heartBeats < IGNORE_ROOT_MSG && outgoingMsg->rootID == TOS_NODE_ID) ){ outgoingMsg->rootID = msg->rootID; outgoingMsg->seqNum = msg->seqNum; } @@ -311,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; @@ -353,7 +360,9 @@ implementation } outgoingMsg->globalTime = globalTime; - +#ifdef LOW_POWER_LISTENING + call LowPowerListening.setRemoteWakeupInterval(&outgoingMsgBuffer, LPL_INTERVAL); +#endif // we don't send time sync msg, if we don't have enough data if( numEntries < ENTRY_SEND_LIMIT && outgoingMsg->rootID != TOS_NODE_ID ){ ++heartBeats; @@ -406,11 +415,8 @@ implementation } command error_t TimeSyncMode.setMode(uint8_t mode_){ - if (mode == mode_) - return SUCCESS; - - if (mode_ == TS_USER_MODE){ - call Timer.startPeriodic((uint32_t)1000 * BEACON_RATE); + if (mode_ == TS_TIMER_MODE){ + call Timer.startPeriodic((uint32_t)(896U+(call Random.rand16()&0xFF)) * BEACON_RATE); } else call Timer.stop(); @@ -458,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; }