/** \brief Calculated how many slots have elapsed since last synchronized. \param[in] timeCorrection time to be corrected \param[in] timesource The address of neighbor. \return the number of slots */ void adaptive_sync_preprocess(int16_t timeCorrection, open_addr_t timesource){ uint8_t array[5]; // stop calculating compensation period when compensateThreshold exceeds KATIMEOUT and drift is not changed if( adaptive_sync_vars.compensateThreshold > MAXKAPERIOD && adaptive_sync_vars.driftChanged == FALSE ) { if(timeCorrection > LIMITLARGETIMECORRECTION) { //once I get a large time correction, it means previous calcluated drift is not accurate yet. The clock drift is changed. adaptive_sync_driftChanged(); } return; } // check whether I am synchronized and also check whether it's the same neighbor synchronized to last time? if( adaptive_sync_vars.driftChanged == FALSE && ieee154e_isSynch() && packetfunctions_sameAddress(×ource, &(adaptive_sync_vars.compensationInfo_vars.neighborID)) ) { // only calcluate when asnDiff > compensateThresholdThreshold. (this is used for guaranteeing accuracy ) if(ieee154e_asnDiff(&adaptive_sync_vars.oldASN) > adaptive_sync_vars.compensateThreshold) { // calculate compensation interval adaptive_sync_calculateCompensatedSlots(timeCorrection); // reset compensationtTicks and sumOfTC after calculation adaptive_sync_vars.compensateTicks = 0; adaptive_sync_vars.sumOfTC = 0; // update threshold adaptive_sync_vars.compensateThreshold *= 2; sixtop_setKaPeriod(adaptive_sync_vars.compensateThreshold); // update oldASN ieee154e_getAsn(array); adaptive_sync_vars.oldASN.bytes0and1 = ((uint16_t) array[1] << 8) | ((uint16_t) array[0]); adaptive_sync_vars.oldASN.bytes2and3 = ((uint16_t) array[3] << 8) | ((uint16_t) array[2]); adaptive_sync_vars.oldASN.byte4 = array[4]; } else { // record the timeCorrection, if not calculate. adaptive_sync_vars.sumOfTC += timeCorrection; } } else { adaptive_sync_vars.compensateThreshold = BASIC_COMPENSATION_THRESHOLD; sixtop_setKaPeriod(adaptive_sync_vars.compensateThreshold); // when I joined the network, or changed my time parent, reset adaptive_sync relative variables adaptive_sync_vars.clockState = S_NONE; adaptive_sync_vars.elapsedSlots = 0; adaptive_sync_vars.compensationTimeout = 0; adaptive_sync_vars.compensateTicks = 0; adaptive_sync_vars.sumOfTC = 0; // update oldASN ieee154e_getAsn(array); adaptive_sync_vars.oldASN.bytes0and1 = ((uint16_t) array[1] << 8) | ((uint16_t) array[0]); adaptive_sync_vars.oldASN.bytes2and3 = ((uint16_t) array[3] << 8) | ((uint16_t) array[2]); adaptive_sync_vars.oldASN.byte4 = array[4]; // record this neighbor as my time source memcpy(&(adaptive_sync_vars.compensationInfo_vars.neighborID), ×ource, sizeof(open_addr_t)); } }
/** \brief Calculate the compensation interval, in number of slots. \param[in] timeCorrection time to be corrected \returns compensationSlots the number of slots. */ void adaptive_sync_calculateCompensatedSlots(int16_t timeCorrection) { bool isFirstSync; // is this the first sync after joining network? uint16_t totalTimeCorrectionTicks; // how much error in ticks since last synchronization. if(adaptive_sync_vars.clockState == S_NONE) { isFirstSync = TRUE; } else { isFirstSync = FALSE; } adaptive_sync_vars.elapsedSlots = ieee154e_asnDiff(&adaptive_sync_vars.oldASN); if(isFirstSync) { if(timeCorrection > 1) { adaptive_sync_vars.clockState = S_FASTER; adaptive_sync_vars.compensationInfo_vars.compensationSlots = SYNC_ACCURACY*adaptive_sync_vars.elapsedSlots; adaptive_sync_vars.compensationInfo_vars.compensationSlots /= timeCorrection; } else { if(timeCorrection < -1) { adaptive_sync_vars.clockState = S_SLOWER; adaptive_sync_vars.compensationInfo_vars.compensationSlots = SYNC_ACCURACY*adaptive_sync_vars.elapsedSlots; adaptive_sync_vars.compensationInfo_vars.compensationSlots /= (-timeCorrection); } else { //timeCorrection = {-1,1}, it's not accurate when timeCorrection belongs to {-1,1} //nothing is needed to do with this case. } } } else { if(adaptive_sync_vars.clockState == S_SLOWER) { totalTimeCorrectionTicks = adaptive_sync_vars.compensateTicks; totalTimeCorrectionTicks -= timeCorrection+adaptive_sync_vars.sumOfTC; } else { totalTimeCorrectionTicks = adaptive_sync_vars.compensateTicks; totalTimeCorrectionTicks += timeCorrection+adaptive_sync_vars.sumOfTC; } if(totalTimeCorrectionTicks == 0) { // totalTimeCorrectionTicks should be always positive if drift of clock is constant. if totalTimeCorrectionTIcks become zero, it means the drift changed for some reasons. adaptive_sync_driftChanged(); } else { adaptive_sync_vars.compensationInfo_vars.compensationSlots = SYNC_ACCURACY*adaptive_sync_vars.elapsedSlots; adaptive_sync_vars.compensationInfo_vars.compensationSlots /= totalTimeCorrectionTicks; } } adaptive_sync_vars.compensationTimeout = adaptive_sync_vars.compensationInfo_vars.compensationSlots; }