From: xuanthanh18 Date: Thu, 11 Mar 2010 22:34:58 +0000 (+0000) Subject: Clean up code X-Git-Url: https://oss.titaniummirror.com/gitweb/?p=tinyos-2.x.git;a=commitdiff_plain;h=d01afff3db89889b3fd93850b2881baf91307f92 Clean up code --- diff --git a/tos/lib/net/dhv/Dhv.h b/tos/lib/net/dhv/Dhv.h index f6ad31d2..a3568422 100755 --- a/tos/lib/net/dhv/Dhv.h +++ b/tos/lib/net/dhv/Dhv.h @@ -26,7 +26,7 @@ #define DHV_VERSION_LENGTH 4 #define VBIT_LENGTH 8 - +#define INFO_THRESHOLD 1 enum { AM_DHV_TEST_MSG = 0xAB }; diff --git a/tos/lib/net/dhv/DhvDataP.nc b/tos/lib/net/dhv/DhvDataP.nc index b1259dba..5834bda6 100755 --- a/tos/lib/net/dhv/DhvDataP.nc +++ b/tos/lib/net/dhv/DhvDataP.nc @@ -83,7 +83,6 @@ implementation { dhv_version_t msgVer; dhv_data_msg_t* ddmsg; - commRate = commRate + 1; ddmsg = (dhv_data_msg_t*) payload; key = ddmsg->key; msgVer = ddmsg->version; @@ -112,6 +111,7 @@ implementation { //keep quite call DataLogic.unsetItem(key); call VectorLogic.unsetItem(key); + commRate = commRate + 1; //set bindex to 0 } } diff --git a/tos/lib/net/dhv/DhvHSumP.nc b/tos/lib/net/dhv/DhvHSumP.nc index e550f610..32dd414a 100755 --- a/tos/lib/net/dhv/DhvHSumP.nc +++ b/tos/lib/net/dhv/DhvHSumP.nc @@ -78,6 +78,7 @@ implementation{ local_hash = call DhvHelp.computeHash(0, UQCOUNT_DHV, salt); if(rcv_hash == local_hash) { call VBitLogic.setSameSummary(); + commRate = commRate + 1; }else{ call VBitLogic.setVBitState(1); } diff --git a/tos/lib/net/dhv/DhvLogicP.nc b/tos/lib/net/dhv/DhvLogicP.nc index de9d3caf..84cacfc7 100755 --- a/tos/lib/net/dhv/DhvLogicP.nc +++ b/tos/lib/net/dhv/DhvLogicP.nc @@ -18,8 +18,8 @@ module DhvLogicP { provides interface Init; provides interface StdControl; - provides interface DhvLogic as VectorLogic; - provides interface DhvLogic as DataLogic; + provides interface DhvLogic as VectorLogic; + provides interface DhvLogic as DataLogic; provides interface DhvStateLogic; uses interface Boot; @@ -29,11 +29,11 @@ module DhvLogicP { uses interface DhvDecision as DhvDataDecision; uses interface DhvDecision as DhvVectorDecision; uses interface DhvDecision as DhvSummaryDecision; - uses interface DhvDecision as DhvVBitDecision; + uses interface DhvDecision as DhvVBitDecision; uses interface DhvDecision as DhvHSumDecision; - uses interface DhvCache as DhvDataCache; - uses interface DhvCache as DhvVectorCache; + uses interface DhvCache as DhvDataCache; + uses interface DhvCache as DhvVectorCache; uses interface DhvHelp; } @@ -41,10 +41,9 @@ module DhvLogicP { implementation { uint32_t windowSize; uint8_t sendDecision(); - uint8_t getState(); uint32_t bitIndex; uint8_t hsum_status; - uint32_t diffHash; + uint32_t diffHash; command error_t Init.init() { windowSize = DHV_TAU_LOW; @@ -53,7 +52,7 @@ implementation { } event void Boot.booted() { - hsum_status = 0; + hsum_status = 0; bitIndex = 0; } @@ -67,203 +66,190 @@ implementation { } - /*Logic operation on the vector */ - command error_t VectorLogic.setItem(dhv_key_t key){ - call DhvVectorCache.addItem(key); - call DhvTrickleTimer.reset(); - return SUCCESS; - } - - command error_t VectorLogic.setReqItem(dhv_key_t key){ - call DhvVectorCache.addReqItem(key); - call DhvTrickleTimer.reset(); - return SUCCESS; - } - - command error_t VectorLogic.unsetItem(dhv_key_t key){ - call DhvVectorCache.removeItem(key); - call DhvStateLogic.setVBitState(0); - return SUCCESS; - } - - command uint8_t * VectorLogic.allItem(){ - return call DhvVectorCache.allItem(); - } - - command uint8_t VectorLogic.nextItem(){ - return call DhvVectorCache.nextItem(); - } - - /*logic operations on the data*/ - command error_t DataLogic.setItem(dhv_key_t key){ - call DhvDataCache.addItem( key); - call DhvTrickleTimer.reset(); - return SUCCESS; - } - - command error_t DataLogic.setReqItem(dhv_key_t key){ - call DhvDataCache.addReqItem( key); - call DhvTrickleTimer.reset(); - return SUCCESS; - } - - command error_t DataLogic.unsetItem(dhv_key_t key){ - call DhvDataCache.removeItem(key); - call DhvStateLogic.setVBitState(0); - return SUCCESS; - } - - command uint8_t* DataLogic.allItem(){ - return call DhvDataCache.allItem(); - } - - command uint8_t DataLogic.nextItem(){ - return call DhvDataCache.nextItem(); - } + /*Logic operation on the vector */ + command error_t VectorLogic.setItem(dhv_key_t key){ + call DhvVectorCache.addItem(key); + call DhvTrickleTimer.reset(); + return SUCCESS; + } + + command error_t VectorLogic.setReqItem(dhv_key_t key){ + call DhvVectorCache.addReqItem(key); + call DhvTrickleTimer.reset(); + return SUCCESS; + } + + command error_t VectorLogic.unsetItem(dhv_key_t key){ + call DhvVectorCache.removeItem(key); + call DhvStateLogic.setVBitState(0); + return SUCCESS; + } + + command uint8_t * VectorLogic.allItem(){ + return call DhvVectorCache.allItem(); + } + + command uint8_t VectorLogic.nextItem(){ + return call DhvVectorCache.nextItem(); + } + + /*logic operations on the data*/ + command error_t DataLogic.setItem(dhv_key_t key){ + call DhvDataCache.addItem( key); + call DhvTrickleTimer.reset(); + return SUCCESS; + } + + command error_t DataLogic.setReqItem(dhv_key_t key){ + call DhvDataCache.addReqItem( key); + call DhvTrickleTimer.reset(); + return SUCCESS; + } + + command error_t DataLogic.unsetItem(dhv_key_t key){ + call DhvDataCache.removeItem(key); + call DhvStateLogic.setVBitState(0); + return SUCCESS; + } + + command uint8_t* DataLogic.allItem(){ + return call DhvDataCache.allItem(); + } + + command uint8_t DataLogic.nextItem(){ + return call DhvDataCache.nextItem(); + } /*logic operation for the summary and vbit*/ command void DhvStateLogic.setHSumStatus(){ - hsum_status = 1; - call DhvTrickleTimer.reset(); + hsum_status = 1; + call DhvTrickleTimer.reset(); } - + command void DhvStateLogic.unsetHSumStatus(){ - hsum_status = 0; + hsum_status = 0; } command uint8_t DhvStateLogic.getHSumStatus(){ - return hsum_status; - } - - command void DhvStateLogic.setDiffSummary(){ - if(bitIndex == 0){ - bitIndex=1; - } - - call DhvTrickleTimer.reset(); - } - - command void DhvStateLogic.setSameSummary(){ - bitIndex = 0; - hsum_status = 0; - //reset all the vector and data status to avoid flooding - call DhvDataCache.removeAll(); - call DhvVectorCache.removeAll(); - - } - - command void DhvStateLogic.setVBitState(uint32_t state){ - bitIndex = state; + return hsum_status; + } + + command void DhvStateLogic.setDiffSummary(){ + if(bitIndex == 0){ + bitIndex=1; + } + + call DhvTrickleTimer.reset(); + } + + command void DhvStateLogic.setSameSummary(){ + bitIndex = 0; + hsum_status = 0; + //reset all the vector and data status to avoid flooding + call DhvDataCache.removeAll(); + call DhvVectorCache.removeAll(); + + } + + command void DhvStateLogic.setVBitState(uint32_t state){ + bitIndex = state; if(state != 0){ - call DhvTrickleTimer.reset(); - } - } + call DhvTrickleTimer.reset(); + } + } - command uint32_t DhvStateLogic.getVBitState(){ - return bitIndex; - } + command uint32_t DhvStateLogic.getVBitState(){ + return bitIndex; + } - //unset one bit at index location - command void DhvStateLogic.unsetVBitIndex(uint8_t dindex){ + //unset one bit at index location + command void DhvStateLogic.unsetVBitIndex(uint8_t dindex){ uint32_t mask; - mask = 1; + mask = 1; mask = mask << (dindex-1); - dbg("TempDebug", "TempDebug: Before mask dindex bitIndex %d %d %d\n", mask, dindex, bitIndex); - if((bitIndex & mask) != 0){ - bitIndex = bitIndex^mask; - } - dbg("TempDebug", "TempDebug: After bitIndex %d\n", bitIndex); - } - - command void DhvStateLogic.setVBitIndex(uint8_t dindex){ + dbg("TempDebug", "TempDebug: Before mask dindex bitIndex %d %d %d\n", mask, dindex, bitIndex); + if((bitIndex & mask) != 0){ + bitIndex = bitIndex^mask; + } + dbg("TempDebug", "TempDebug: After bitIndex %d\n", bitIndex); + } + + command void DhvStateLogic.setVBitIndex(uint8_t dindex){ uint32_t mask; - mask = 1; + mask = 1; mask = mask << (dindex-1); - - bitIndex = bitIndex | mask; - - call DhvTrickleTimer.reset(); -} -//get the non-zero bit index to extract the vertical bits. -command uint8_t DhvStateLogic.getVBitIndex(){ - - uint32_t mask; - uint8_t i; - uint32_t xor; - - if(bitIndex == 0){ - return 0; - }else - { - mask = 1; - for(i = 1; i <= 32; i++){ - xor = bitIndex & mask; - - dbg("TempDebug", "TempDebug: %d %d %d %d \n", i, bitIndex, mask, xor); - if(xor != 0){ - return i; - } - mask = mask << 1; - } - return 0; - } -} + bitIndex = bitIndex | mask; + + call DhvTrickleTimer.reset(); + } + + //get the non-zero bit index to extract the vertical bits. + command uint8_t DhvStateLogic.getVBitIndex(){ + + uint32_t mask; + uint8_t i; + uint32_t xor; + + if(bitIndex == 0){ + return 0; + }else + { + mask = 1; + for(i = 1; i <= 32; i++){ + xor = bitIndex & mask; + + dbg("TempDebug", "TempDebug: %d %d %d %d \n", i, bitIndex, mask, xor); + if(xor != 0){ + return i; + } + mask = mask << 1; + } + return 0; + } + } command void DisseminationUpdate.change[dhv_key_t key](dhv_data_t* val) { dbg("DhvLogicP","App notified key %x is new\n", key); - - //update data: actual reprogramming job + + //update data: actual reprogramming job call VersionUpdate.change[key](val); //set data - call DhvDataCache.addItem(key); + call DhvDataCache.addItem(key); - //set to advertise its version - call DhvVectorCache.addItem(key); + //set to advertise its version + call DhvVectorCache.addItem(key); - //reset bindex - call DhvStateLogic.setVBitState(0); + //reset bindex + call DhvStateLogic.setVBitState(0); - dbg("DhvLogicP","Reset bindex to 0\n"); - //reset timer + dbg("DhvLogicP","Reset bindex to 0\n"); + //reset timer call DhvTrickleTimer.reset(); } event uint32_t DhvTrickleTimer.requestWindowSize() { //TODO: consider if this is neccessary - uint8_t decision; - - decision = sendDecision(); - - if(decision == ID_DHV_SUMMARY){ - windowSize = windowSize << 1; - if(windowSize > DHV_TAU_HIGH){ - windowSize = DHV_TAU_HIGH; - } - }else{ - if(decision != ID_DHV_INVALID){ - windowSize = DHV_TAU_LOW; - } - } - - /*if(decision == ID_DHV_DATA){ - windowSize = DHV_TAU_LOW; - }else{ - if(decision == ID_DHV_VBIT){ - windowSize = DHV_TAU_LOW; - }else{ - windowSize = windowSize << 1; - if(windowSize > DHV_TAU_HIGH){ - windowSize = DHV_TAU_HIGH; - } - } - } - */ + uint8_t decision; + + decision = sendDecision(); + + if(decision == ID_DHV_SUMMARY){ + windowSize = windowSize << 1; + if(windowSize > DHV_TAU_HIGH){ + windowSize = DHV_TAU_HIGH; + } + }else{ + if(decision != ID_DHV_INVALID){ + windowSize = DHV_TAU_LOW; + } + } + dbg("DhvLogicP", "Time window size requested, give %u : send decision %d \n", windowSize, decision); return windowSize; } @@ -276,127 +262,91 @@ command uint8_t DhvStateLogic.getVBitIndex(){ decision = sendDecision(); switch(decision) { - case ID_DHV_INVALID: - dbg("DhvLogicP", "Decision to SUPPRESS\n"); - break; - case ID_DHV_SUMMARY: - dbg("DhvLogicP", "Decision to SUMMARY\n"); - call DhvSummaryDecision.send(); - break; - case ID_DHV_VECTOR: - dbg("DhvLogicP", "Decision to VECTOR\n"); - call DhvVectorDecision.send(); - break; - case ID_DHV_DATA: - dbg("DhvLogicP", "Decision to DATA\n"); - call DhvDataDecision.send(); - break; - case ID_DHV_VBIT: - dbg("DhvLogicP", "Decision to VSUM\n"); - call DhvVBitDecision.send(); - break; - case ID_DHV_HSUM: - dbg("DhvLogicP", "Decision to HSUM\n"); - call DhvHSumDecision.send(); - break; + case ID_DHV_INVALID: + dbg("DhvLogicP", "Decision to SUPPRESS\n"); + break; + case ID_DHV_SUMMARY: + dbg("DhvLogicP", "Decision to SUMMARY\n"); + call DhvSummaryDecision.send(); + break; + case ID_DHV_VECTOR: + dbg("DhvLogicP", "Decision to VECTOR\n"); + call DhvVectorDecision.send(); + break; + case ID_DHV_DATA: + dbg("DhvLogicP", "Decision to DATA\n"); + call DhvDataDecision.send(); + break; + case ID_DHV_VBIT: + dbg("DhvLogicP", "Decision to VSUM\n"); + call DhvVBitDecision.send(); + break; + case ID_DHV_HSUM: + dbg("DhvLogicP", "Decision to HSUM\n"); + call DhvHSumDecision.send(); + break; } call DhvDataDecision.resetCommRate(); call DhvVectorDecision.resetCommRate(); call DhvSummaryDecision.resetCommRate(); - call DhvVBitDecision.resetCommRate(); - call DhvHSumDecision.resetCommRate(); + call DhvVBitDecision.resetCommRate(); + call DhvHSumDecision.resetCommRate(); - //set bitstate to zero - call DhvStateLogic.setVBitState(0); + //set bitstate to zero + call DhvStateLogic.setVBitState(0); } - - uint8_t getState() { - bool hasItemToSend; - uint32_t bindex; - - hasItemToSend = FALSE; - - hasItemToSend = call DhvDataCache.hasItemToSend(); - if(hasItemToSend){ - dbg("DhvLogicP", "has data to send? %u \n", hasItemToSend); - return ID_DHV_DATA; - }else{ - hasItemToSend = call DhvVectorCache.hasItemToSend(); - dbg("DhvLogicP", "has vector to send? %u \n", hasItemToSend); - if(hasItemToSend){ - return ID_DHV_VECTOR; - }else{ - bindex = call DhvStateLogic.getVBitState(); - dbg("DhvLogicP", "send decision bindex %d \n", bindex); - - if(bindex != 0){ - return ID_DHV_VBIT; - }else{ - if(hsum_status != 0){ - return ID_DHV_HSUM; - }else { - return ID_DHV_SUMMARY; - } - } - } - } - } - - uint8_t sendDecision() { - bool hasItemToSend; - uint32_t bindex; + bool hasItemToSend; + uint32_t bindex; uint8_t dataCommRate; uint8_t vectorCommRate; uint8_t summaryCommRate; - uint8_t vbitCommRate; + uint8_t vbitCommRate; uint8_t hsumCommRate; dataCommRate = call DhvDataDecision.getCommRate(); vectorCommRate = call DhvVectorDecision.getCommRate(); summaryCommRate = call DhvSummaryDecision.getCommRate(); - vbitCommRate = call DhvVBitDecision.getCommRate(); + vbitCommRate = call DhvVBitDecision.getCommRate(); hsumCommRate = call DhvHSumDecision.getCommRate(); - hasItemToSend = FALSE; + if(dataCommRate > INFO_THRESHOLD){ + return ID_DHV_INVALID; + } + + hasItemToSend = FALSE; hasItemToSend = call DhvDataCache.hasItemToSend(); - if(hasItemToSend){ - dbg("DhvLogicP", "has data to send? %u \n", hasItemToSend); - return ID_DHV_DATA; - } - - bindex = call DhvStateLogic.getVBitState(); - dbg("DhvLogicP", "send decision bindex %d \n", bindex); - - if(bindex != 0){ - return ID_DHV_VBIT; - } - - if(hsum_status != 0){ - return ID_DHV_HSUM; - } - - if(dataCommRate > 1) { - dbg("DhvLogicP", "Heard data\n"); - return ID_DHV_INVALID; - } - - - hasItemToSend = call DhvVectorCache.hasItemToSend(); - dbg("DhvLogicP", "has vector to send? %u \n", hasItemToSend); - - if(hasItemToSend){ - return ID_DHV_VECTOR; - } - - // didn't send or hear data at this point - if(vectorCommRate + summaryCommRate + vbitCommRate > 1) { - dbg("DhvLogicP", "Heard an advertisement\n"); - return ID_DHV_INVALID; - } - - return ID_DHV_SUMMARY; - } + if(hasItemToSend){ + dbg("DhvLogicP", "has data to send? %u \n", hasItemToSend); + return ID_DHV_DATA; + } + + // didn't send or hear data at this point + if(dataCommRate + vectorCommRate + summaryCommRate + vbitCommRate + hsumCommRate >= INFO_THRESHOLD) { + dbg("DhvLogicP", "Heard an advertisement\n"); + return ID_DHV_INVALID; + } + + hasItemToSend = call DhvVectorCache.hasItemToSend(); + dbg("DhvLogicP", "has vector to send? %u \n", hasItemToSend); + + if(hasItemToSend){ + return ID_DHV_VECTOR; + } + + bindex = call DhvStateLogic.getVBitState(); + dbg("DhvLogicP", "send decision bindex %d \n", bindex); + + if(bindex != 0){ + return ID_DHV_VBIT; + } + + if(hsum_status != 0){ + return ID_DHV_HSUM; + } + + return ID_DHV_SUMMARY; + } } diff --git a/tos/lib/net/dhv/DhvSummaryP.nc b/tos/lib/net/dhv/DhvSummaryP.nc index eb97dd40..5323fb73 100755 --- a/tos/lib/net/dhv/DhvSummaryP.nc +++ b/tos/lib/net/dhv/DhvSummaryP.nc @@ -60,7 +60,6 @@ implementation { dhv_summary_msg_t* dsmsg; uint32_t salt, myHash; - commRate = commRate + 1; dsmsg = (dhv_summary_msg_t*) payload; salt = dsmsg->salt; @@ -73,6 +72,7 @@ implementation { } else { call StateLogic.setSameSummary(); + commRate = commRate + 1; dbg("DhvSummaryP", "Hashes match\n"); } } diff --git a/tos/lib/net/dhv/DhvVBitP.nc b/tos/lib/net/dhv/DhvVBitP.nc index 9ebac2fa..95e8b825 100755 --- a/tos/lib/net/dhv/DhvVBitP.nc +++ b/tos/lib/net/dhv/DhvVBitP.nc @@ -104,7 +104,7 @@ implementation{ dbg("DhvVBitP", "bindex %d vbit %d: 0x%02x 0x%02x \n",bindex, i, dvbmsg->vbit[i], versionPtr[i]); } - dbg("DhvVBitP", "Sending vbit of index %d size %d \n", bindex, sizeof(dhv_msg_t) + sizeof(dhv_vbit_msg_t) + msg_size ); + //dbg("DhvVBitP", "Sending vbit of index %d size %d \n", bindex, sizeof(dhv_msg_t) + sizeof(dhv_vbit_msg_t) + msg_size ); for(i = 0; i < msg_size; i++){ dbg("DhvVBitP", "vbit to send %d, 0x%02x \n", i, dvbmsg->vbit[i]); @@ -140,7 +140,6 @@ implementation{ uint32_t bitIndexValue; isDiff = FALSE; - commRate = 1; maxDataLength = TOSH_DATA_LENGTH - sizeof(dhv_msg_t) - sizeof(dhv_vbit_msg_t); if(UQCOUNT_DHV != 0) @@ -168,7 +167,7 @@ implementation{ //some duplicates dbg("DhvVBitP", "same summary\n"); call VBitLogic.setSameSummary(); - + commRate = commRate + 1; }else{ vbit = call DhvHelp.getVBits(bindex); if(vindex == numMsg-1){ diff --git a/tos/lib/net/dhv/DhvVectorP.nc b/tos/lib/net/dhv/DhvVectorP.nc index 9a3b5870..e76348af 100755 --- a/tos/lib/net/dhv/DhvVectorP.nc +++ b/tos/lib/net/dhv/DhvVectorP.nc @@ -81,7 +81,7 @@ implementation { dvmsg->unitLen = i; //TODO: need to fix - dbg("DhvVectorP", "Sending vector message out ...unitLen 0x%02x \n", dvmsg->unitLen); + // dbg("DhvVectorP", "Sending vector message out ...unitLen 0x%02x \n", dvmsg->unitLen); status = call VectorSend.send(sizeof(dhv_msg_t) + sizeof(dhv_vector_msg_t) + (i*sizeof(uint32_t))); @@ -124,8 +124,7 @@ implementation { uint32_t vectorver; uint32_t myver; - commRate = commRate + 1; - + commRate = commRate + 1; dmsg = (dhv_msg_t*) payload; type = dmsg->type; @@ -140,23 +139,18 @@ implementation { myver = call DhvHelp.keyToVersion(vectorkey); dbg("DhvVectorP", "key 0x%08x version 0x%08x myver 0x%08x \n", vectorkey, vectorver, myver); // TODO: handle the invalid versions + if(myver < vectorver) { - dbg("DhvVectorP", "I have an older version -> setReqItem \n"); - call VectorLogic.setReqItem(vectorkey); + dbg("DhvVectorP", "I have an older version -> setItem \n"); + call VectorLogic.setItem(vectorkey); } else if(myver > vectorver) { dbg("DhvVectorP", "I have a newer version -> Data.setItem \n"); call DataLogic.setItem(vectorkey); } else{ - if(type == ID_DHV_VECTOR_REQ){ - dbg("DhvVectorP", "Request msg and I have that version -> Data.setItem \n"); - call DataLogic.setItem(vectorkey); - }else{ - dbg("DhvVectorP", "Request msg and I have the same version -> keep quite \n"); - call VectorLogic.unsetItem(vectorkey); - } + call VectorLogic.unsetItem(vectorkey); } } } diff --git a/tos/lib/net/dhv/DhvVersionP.nc b/tos/lib/net/dhv/DhvVersionP.nc index cc89a2f9..0df65c1f 100755 --- a/tos/lib/net/dhv/DhvVersionP.nc +++ b/tos/lib/net/dhv/DhvVersionP.nc @@ -139,7 +139,7 @@ implementation { dhv_index_t i; i = call DhvHelp.keyToIndex(key); - vector_to_send[i] = ID_DHV_REQ; + vector_to_send[i] = ID_DHV_ADS; dbg("DhvVersionP", "Add Item to vector_to_send index %d\n", i); printVectorStatus();