/** \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)); } }
owerror_t openserial_print_str(char* buffer, uint8_t length) { #ifdef OPENSERIAL_PRINTF uint8_t i; uint8_t asn[5]; // retrieve ASN ieee154e_getAsn(asn); outputHdlcOpen(); outputHdlcWrite(SERFRAME_MOTE2PC_PRINTF); outputHdlcWrite(TYPE_STR); outputHdlcWrite(idmanager_getMyID(ADDR_16B)->addr_16b[0]); outputHdlcWrite(idmanager_getMyID(ADDR_16B)->addr_16b[1]); for(i=0; i<5;i++) { outputHdlcWrite(asn[i]); } for (i=0;i<length;i++){ outputHdlcWrite(buffer[i]); } outputHdlcClose(); // start TX'ing openserial_flush(); #endif return E_SUCCESS; }
owerror_t openserial_printData(uint8_t* buffer, uint8_t length) { uint8_t i; uint8_t asn[5]; INTERRUPT_DECLARATION(); // retrieve ASN ieee154e_getAsn(asn);// byte01,byte23,byte4 DISABLE_INTERRUPTS(); openserial_vars.outputBufFilled = TRUE; outputHdlcOpen(); outputHdlcWrite(SERFRAME_MOTE2PC_DATA); outputHdlcWrite(idmanager_getMyID(ADDR_16B)->addr_16b[1]); outputHdlcWrite(idmanager_getMyID(ADDR_16B)->addr_16b[0]); outputHdlcWrite(asn[0]); outputHdlcWrite(asn[1]); outputHdlcWrite(asn[2]); outputHdlcWrite(asn[3]); outputHdlcWrite(asn[4]); for (i=0;i<length;i++){ outputHdlcWrite(buffer[i]); } outputHdlcClose(); ENABLE_INTERRUPTS(); return E_SUCCESS; }
owerror_t openserial_printData(uint8_t* buffer, uint8_t length) { uint8_t i; uint8_t asn[5]; // retrieve ASN ieee154e_getAsn(asn); outputHdlcOpen(); outputHdlcWrite(SERFRAME_MOTE2PC_DATA); outputHdlcWrite(idmanager_getMyID(ADDR_16B)->addr_16b[0]); outputHdlcWrite(idmanager_getMyID(ADDR_16B)->addr_16b[1]); outputHdlcWrite(asn[0]); outputHdlcWrite(asn[1]); outputHdlcWrite(asn[2]); outputHdlcWrite(asn[3]); outputHdlcWrite(asn[4]); for (i=0;i<length;i++){ outputHdlcWrite(buffer[i]); } outputHdlcClose(); // start TX'ing openserial_flush(); return E_SUCCESS; }