]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/chips/tda5250/mac/RedMacP.nc
added SpeckMac
[tinyos-2.x.git] / tos / chips / tda5250 / mac / RedMacP.nc
index 5ea29ebe7fc0b0c57216910f974454944dc40b80..41739aa300136fbeb28e6e7e8bd67967c4d7fb5f 100644 (file)
@@ -46,7 +46,6 @@ module RedMacP {
         interface MacSend;
         interface MacReceive;
         interface Packet;
-        interface LocalTime<T32khz> as LocalTime32khz;
         interface Sleeptime;
         interface Teamgeist;
         interface ChannelCongestion;
@@ -72,7 +71,10 @@ module RedMacP {
         
         interface Alarm<T32khz, uint16_t> as Timer;
         interface Alarm<T32khz, uint16_t> as SampleTimer;
-        interface Counter<T32khz,uint16_t> as Counter32khz16;
+        interface LocalTime<T32khz> as LocalTime32kHz;
+
+        interface Duplicate;
+        
         async command am_addr_t amAddress();
 /*
         interface GeneralIO as Led0;
@@ -137,8 +139,8 @@ implementation
         // DEFAULT_SLEEP_TIME=3250,
         // DEFAULT_SLEEP_TIME=6500,
         // DEFAULT_SLEEP_TIME=8192,
-        DEFAULT_SLEEP_TIME=16384,
-        // DEFAULT_SLEEP_TIME=32768U,
+        // DEFAULT_SLEEP_TIME=16384,
+        DEFAULT_SLEEP_TIME=32768U,
         // DEFAULT_SLEEP_TIME=65535U,
         DATA_DETECT_TIME=17,
         RX_SETUP_TIME=102,    // time to set up receiver
@@ -150,8 +152,6 @@ implementation
         ACK_DURATION = SUB_HEADER_TIME + SUB_FOOTER_TIME,
         MAX_SHORT_RETRY=9,
         MAX_LONG_RETRY=3,
-        MAX_AGE=0xff,
-        MSG_TABLE_ENTRIES=16,
         TOKEN_ACK_FLAG = 64,
         TOKEN_ACK_MASK = 0x3f,
         INVALID_SNR = 0xffff,
@@ -162,15 +162,6 @@ implementation
     };
     
     /**************** Module Global Variables  *****************/
-    typedef union 
-    {
-        uint32_t op;
-        struct {
-            uint16_t lo;
-            uint16_t hi;
-        };
-    } ui32parts_t;
-    
     /* flags */
     typedef enum {
         SWITCHING = 1,
@@ -183,14 +174,6 @@ implementation
         TEAMGEIST_ACTIVE=128
     } flags_t;
 
-        /* duplicate suppression */
-    typedef struct knownMessage_t {
-        am_addr_t src;
-        uint8_t token;
-        uint8_t age;
-    } knownMessage_t;
-    
-    knownMessage_t knownMsgTable[MSG_TABLE_ENTRIES];
     uint8_t flags = 0;
     uint8_t checkCounter = 0;
     uint8_t shortRetryCounter = 0;
@@ -199,8 +182,7 @@ implementation
     uint16_t localSleeptime = DEFAULT_SLEEP_TIME;
     uint16_t rssiValue = 0;
     uint32_t restLaufzeit = 0;
-
-    uint16_t counter2sec = 127;
+    
     uint32_t rxTime = 0;
 
     am_id_t teamgeistType = 0;
@@ -215,23 +197,6 @@ implementation
 
     uint16_t MIN_BACKOFF_MASK;
 
-#ifdef REDMAC_DEBUG
-    uint8_t dupOldest;
-    task void dumpLast() {
-        knownMessage_t mem;
-        unsigned i;
-        atomic {
-            i = dupOldest;
-            mem = knownMsgTable[i];
-        }
-        sdDebug(5000);
-        sdDebug(i);
-        sdDebug(mem.src);
-        sdDebug(mem.token);
-        sdDebug(mem.age);
-    }
-#endif
-    
     /****** Secure switching of radio modes ***/
     void interruptBackoffTimer();
     
@@ -319,16 +284,7 @@ implementation
         }
     }
 
-    /**************** Helper functions ************************/
-    task void ageMsgsTask() {
-        unsigned i;
-        atomic {
-            for(i = 0; i < MSG_TABLE_ENTRIES; i++) {
-                if(knownMsgTable[i].age < MAX_AGE) ++knownMsgTable[i].age;
-            }
-        }
-    }
-    
+    /**************** Helper functions ************************/    
     void computeBackoff();
     
     void checkSend() {
@@ -402,7 +358,7 @@ implementation
             sT = networkSleeptime;
         }
         if(msg == NULL) return;
-        macHdr = (red_mac_header_t *)call SubPacket.getPayload(msg, NULL);
+        macHdr = (red_mac_header_t *)call SubPacket.getPayload(msg, sizeof(red_mac_header_t) + length);
         macHdr->repetitionCounter = sT/(length * BYTE_TIME + SUB_HEADER_TIME + SUB_FOOTER_TIME + 
                                         TX_GAP_TIME) + 1;
         atomic {
@@ -435,6 +391,20 @@ implementation
         }
     }
 
+    void storeStrength(message_t *m) {
+        if(rssiValue != INVALID_SNR) {
+            (getMetadata(txBufPtr))->strength = rssiValue;
+        }
+        else {
+            if(call RssiAdcResource.isOwner()) {
+                (getMetadata(txBufPtr))->strength = call ChannelMonitorData.readSnr();
+            }
+            else {
+                (getMetadata(txBufPtr))->strength = 1;
+            }
+        }
+    }
+
     bool prepareRepetition() {
         bool repeat;
         atomic {
@@ -448,7 +418,7 @@ implementation
         }
         return repeat;
     }
-
+    
     void signalSendDone(error_t error) {
         message_t *m;
         error_t e = error;
@@ -464,12 +434,7 @@ implementation
 #endif
             longRetryCounter = 0;
             shortRetryCounter = 0;
-            if(rssiValue != INVALID_SNR) {
-                (getMetadata(m))->strength = rssiValue;
-            }
-            else {
-                (getMetadata(m))->strength = call ChannelMonitorData.readSnr();
-            }
+            storeStrength(m);
             if(isFlagSet(&flags, CANCEL_SEND)) {
                 e = ECANCEL;
             }
@@ -559,42 +524,11 @@ implementation
     }
     
     bool isNewMsg(message_t* msg) {
-        bool rVal = TRUE;
-        uint8_t i;
-        for(i=0; i < MSG_TABLE_ENTRIES; i++) {
-            if((knownMsgTable[i].age < MAX_AGE) &&
-               (getHeader(msg)->src == knownMsgTable[i].src) &&
-               (((getHeader(msg)->token) & TOKEN_ACK_MASK) == knownMsgTable[i].token)) {
-                knownMsgTable[i].age = 0;
-                rVal = FALSE;
-                break;
-            }
-        }
-        return rVal;
-    }
-
-    unsigned findOldest() {
-        unsigned i;
-        unsigned oldIndex = 0;
-        unsigned age = knownMsgTable[oldIndex].age;
-        for(i = 1; i < MSG_TABLE_ENTRIES; i++) {
-            if(age < knownMsgTable[i].age) {
-                oldIndex = i;
-                age = knownMsgTable[i].age;
-            }
-        }
-        return oldIndex;
-    }
+        return call Duplicate.isNew(getHeader(msg)->src, (getHeader(msg)->token) & TOKEN_ACK_MASK);
+   }
 
     void rememberMsg(message_t* msg) {
-        unsigned oldest = findOldest();
-        knownMsgTable[oldest].src = getHeader(msg)->src;
-        knownMsgTable[oldest].token = (getHeader(msg)->token) & TOKEN_ACK_MASK;
-        knownMsgTable[oldest].age = 0;
-#ifdef REDMAC_DEBUG
-        dupOldest = oldest;
-        post dumpLast();
-#endif
+        call Duplicate.remember(getHeader(msg)->src, (getHeader(msg)->token) & TOKEN_ACK_MASK);
     }
 
     void prepareAck(message_t* msg) {
@@ -605,24 +539,20 @@ implementation
         getHeader(&ackMsg)->dest = getHeader(msg)->src;
         getHeader(&ackMsg)->type = getHeader(msg)->type;
 #ifdef REDMAC_DEBUG
-        repCounter = ((red_mac_header_t *)call SubPacket.getPayload(msg, NULL))->repetitionCounter;
+        repCounter = ((red_mac_header_t *)
+                      call SubPacket.getPayload(msg, sizeof(red_mac_header_t)))->repetitionCounter;
 #endif
     }
     
     uint32_t calcGeneratedTime(red_mac_header_t *m) {
         return rxTime - m->time - TIME_CORRECTION;
     }
-    
     /**************** Init ************************/
     
     command error_t Init.init(){
-        uint8_t i;
         atomic {
             macState = INIT;
             seqNo = call Random.rand16() % TOKEN_ACK_FLAG;
-            for(i = 0; i < MSG_TABLE_ENTRIES; i++) {
-                knownMsgTable[i].age = MAX_AGE;
-            }
             for(MIN_BACKOFF_MASK = 1; MIN_BACKOFF_MASK < networkSleeptime; ) {
                 MIN_BACKOFF_MASK = (MIN_BACKOFF_MASK << 1) + 1;
             }
@@ -698,11 +628,8 @@ implementation
         return call SubPacket.maxPayloadLength() - sizeof(red_mac_header_t);
     }
     
-    command void* Packet.getPayload(message_t* msg, uint8_t* len) {
-        nx_uint8_t *payload = (nx_uint8_t *)call SubPacket.getPayload(msg, len);
-        if (len != NULL) {
-            *len -= sizeof(red_mac_header_t);
-        }
+    command void* Packet.getPayload(message_t* msg, uint8_t len) {
+        nx_uint8_t *payload = (nx_uint8_t *)call SubPacket.getPayload(msg, len + sizeof(red_mac_header_t));
         return (void*)(payload + sizeof(red_mac_header_t));
     }
     
@@ -828,7 +755,7 @@ implementation
                 if(seqNo >= TOKEN_ACK_FLAG) seqNo = 1;
 #ifdef REDMAC_PERFORMANCE
                 txStat.payloadLength = txLen;
-                txStat.interfaceTime = call LocalTime32khz.get();
+                txStat.interfaceTime = call LocalTime32kHz.get();
 #endif
             }
             else {
@@ -907,7 +834,6 @@ implementation
         if(macState == RX_P) {
             // sdDebug(191);
             if(error == SUCCESS) {
-                post ageMsgsTask();
                 // sdDebug(192);
                 isCnt = isControl(msg);
                 if(msgIsForMe(msg)) {
@@ -918,17 +844,7 @@ implementation
                             rxStat.duplicate = PERF_NEW_MSG;
 #endif
                             // sdDebug(194);
-                            if(rssiValue != INVALID_SNR) {
-                                (getMetadata(m))->strength = rssiValue;
-                            }
-                            else {
-                                if(call RssiAdcResource.isOwner()) {
-                                    (getMetadata(m))->strength = call ChannelMonitorData.readSnr();
-                                }
-                                else {
-                                    (getMetadata(m))->strength = 1;
-                                }
-                            }
+                            storeStrength(msg);
                             getMetadata(msg)->time = calcGeneratedTime((red_mac_header_t*) payload);
                             getMetadata(msg)->ack = WAS_NOT_ACKED;
                             m = signal MacReceive.receiveDone(msg);
@@ -989,17 +905,6 @@ implementation
             if(error == SUCCESS) {
                 if(ackIsForMe(msg)) {
                     // sdDebug(202);
-                    if(rssiValue != INVALID_SNR) {
-                        (getMetadata(txBufPtr))->strength = rssiValue;
-                    }
-                    else {
-                        if(call RssiAdcResource.isOwner()) {
-                            (getMetadata(txBufPtr))->strength = call ChannelMonitorData.readSnr();
-                        }
-                        else {
-                            (getMetadata(txBufPtr))->strength = 1;
-                        }
-                    }
                     getMetadata(txBufPtr)->ack = WAS_ACKED;
                     getMetadata(txBufPtr)->repetitions = txMacHdr->repetitionCounter;
                     if(isFlagSet(&flags, TEAMGEIST_ACTIVE) &&
@@ -1112,7 +1017,7 @@ implementation
     async event void RadioTimeStamping.receivedSFD( uint16_t time ) {
         if(call RssiAdcResource.isOwner()) call ChannelMonitorData.getSnr();
         if(macState == RX_P) {
-            rxTime = call LocalTime32khz.get();
+            rxTime = call LocalTime32kHz.get();
             call ChannelMonitor.rxSuccess();
         }
     }
@@ -1121,7 +1026,7 @@ implementation
         uint32_t now;
         uint32_t mTime;
         if((macState == TX) && (p_msg == txBufPtr)) {
-            now = call LocalTime32khz.get();
+            now = call LocalTime32kHz.get();
             mTime = getMetadata(p_msg)->time;
             if(now >= mTime) {
                 txMacHdr->time = now - mTime;
@@ -1133,21 +1038,6 @@ implementation
         }
     }
     
-    async command uint32_t LocalTime32khz.get() {
-        ui32parts_t time;
-        atomic {
-            time.lo = call Counter32khz16.get();
-            time.hi = counter2sec;
-            if(call Counter32khz16.isOverflowPending()) ++time.hi;
-        }
-        return time.op;
-    }
-    
-    async event void Counter32khz16.overflow() {
-        ++counter2sec;
-    }
-
-
     /****** Timer ******************************/
 
     void checkOnBusy() {
@@ -1194,7 +1084,7 @@ implementation
                 setTxMode();
 #ifdef REDMAC_PERFORMANCE
                 call Performance.macIdleOnCca();
-                txStat.txModeTime = call LocalTime32khz.get();
+                txStat.txModeTime = call LocalTime32kHz.get();
 #endif
             }
         }
@@ -1294,7 +1184,6 @@ implementation
             setRxMode();
             call Timer.stop();
         }
-        post ageMsgsTask();
     }
 
     /***** Sleeptime **********************************/