X-Git-Url: https://oss.titaniummirror.com/gitweb/?a=blobdiff_plain;f=tos%2Flib%2Fftsp%2FTimeSyncP.nc;h=3c3affe83d12528d0d7ef1ece43dfbe316742875;hb=c3864fde9bec0c02c0c6b4221ddde0801a82c980;hp=ba96e92d1f797af0cb04e0c3180e8e2f937becef;hpb=5ddfa2501c180219ecfb6ca374e4cdbfcf08c5ae;p=tinyos-2.x.git diff --git a/tos/lib/ftsp/TimeSyncP.nc b/tos/lib/ftsp/TimeSyncP.nc index ba96e92d..3c3affe8 100644 --- a/tos/lib/ftsp/TimeSyncP.nc +++ b/tos/lib/ftsp/TimeSyncP.nc @@ -46,6 +46,12 @@ generic module TimeSyncP(typedef precision_tag) interface Leds; interface TimeSyncPacket; interface LocalTime as LocalTime; + + +#ifdef LOW_POWER_LISTENING + interface LowPowerListening; +#endif + } } implementation @@ -61,7 +67,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 @@ -150,6 +156,8 @@ implementation float newSkew = skew; uint32_t newLocalAverage; int32_t newOffsetAverage; + int32_t localAverageRest; + int32_t offsetAverageRest; int64_t localSum; int64_t offsetSum; @@ -169,16 +177,23 @@ implementation newOffsetAverage = table[i].timeOffset; localSum = 0; + localAverageRest = 0; offsetSum = 0; + offsetAverageRest = 0; while( ++i < MAX_ENTRIES ) if( table[i].state == ENTRY_FULL ) { + /* + This only works because C ISO 1999 defines the signe for modulo the same as for the Dividend! + */ localSum += (int32_t)(table[i].localTime - newLocalAverage) / tableEntries; + localAverageRest += (table[i].localTime - newLocalAverage) % tableEntries; offsetSum += (int32_t)(table[i].timeOffset - newOffsetAverage) / tableEntries; + offsetAverageRest += (table[i].timeOffset - newOffsetAverage) % tableEntries; } - newLocalAverage += localSum; - newOffsetAverage += offsetSum; + newLocalAverage += localSum + localAverageRest / tableEntries; + newOffsetAverage += offsetSum + offsetAverageRest / tableEntries; localSum = offsetSum = 0; for(i = 0; i < MAX_ENTRIES; ++i) @@ -218,8 +233,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)); @@ -229,10 +242,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; @@ -265,7 +279,7 @@ implementation void task processMsg() { - TimeSyncMsg* msg = (TimeSyncMsg*)(processedMsg->data); + TimeSyncMsg* msg = (TimeSyncMsg*)(call Send.getPayload(processedMsg, sizeof(TimeSyncMsg))); if( msg->rootID < outgoingMsg->rootID && // jw: after becoming the root ignore other roots messages (in send period) @@ -302,11 +316,12 @@ implementation if( diff < -16 || diff > 16 ) return msg; #endif - if( (state & STATE_PROCESSING) == 0 ) { + if( (state & STATE_PROCESSING) == 0 + && call TimeSyncPacket.isValid(msg)) { message_t* old = processedMsg; processedMsg = msg; - ((TimeSyncMsg*)(processedMsg->data))->localTime = call TimeSyncPacket.eventTime(msg); + ((TimeSyncMsg*)(payload))->localTime = call TimeSyncPacket.eventTime(msg); state |= STATE_PROCESSING; post processMsg(); @@ -344,6 +359,9 @@ implementation outgoingMsg->globalTime = globalTime; +#ifdef LOW_POWER_LISTENING + call LowPowerListening.setRxSleepInterval(&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; @@ -399,7 +417,7 @@ implementation if (mode == mode_) return SUCCESS; - if (mode_ == TS_USER_MODE){ + if (mode_ == TS_TIMER_MODE){ call Timer.startPeriodic((uint32_t)1000 * BEACON_RATE); } else