]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/net/lqi/LqiRoutingEngineP.nc
explicit conversion to 32bit multiplication (work around msp430-gcc bug)
[tinyos-2.x.git] / tos / lib / net / lqi / LqiRoutingEngineP.nc
index 8a099e88ac72e49d813092799edfd9c574ad97b3..89ede9dcc265c2340ef94d3030ff24e646953967 100644 (file)
@@ -37,6 +37,7 @@
 
 
 #include "MultiHopLqi.h"
+#include "CollectionDebugMsg.h"
 
 module LqiRoutingEngineP {
 
@@ -58,6 +59,7 @@ module LqiRoutingEngineP {
     interface LqiRouteStats;
     interface CC2420Packet;
     interface Leds;
+    interface CollectionDebug;
   }
 }
 
@@ -87,7 +89,8 @@ implementation {
   uint8_t gLastHeard;
 
   int16_t gCurrentSeqNo;
-
+  int16_t gOriginSeqNo;
+  
   uint16_t gUpdateInterval;
 
   uint8_t gRecentIndex;
@@ -119,7 +122,7 @@ implementation {
     dbg("LQI","MultiHopRSSI Sending route update msg.\n");
 
     if (gbCurrentParent != TOS_BCAST_ADDR) {
-      dbg("LQO","MultiHopRSSI: Parent = %d\n", gbCurrentParent);
+      dbg("LQI","MultiHopRSSI: Parent = %d\n", gbCurrentParent);
     }
     
     if (msgBufBusy) {
@@ -135,7 +138,6 @@ implementation {
       bMsg->cost = 0;
       bMsg->originaddr = TOS_NODE_ID;
       bMsg->hopcount = 0;
-      bMsg->originseqno = gCurrentSeqNo;
       bMsg->seqno = gCurrentSeqNo++;
     }
     else {
@@ -143,12 +145,12 @@ implementation {
       bMsg->cost = gbCurrentParentCost + gbCurrentLinkEst;
       bMsg->originaddr = TOS_NODE_ID;
       bMsg->hopcount = gbCurrentHopCount;
-      bMsg->originseqno = gCurrentSeqNo;
       bMsg->seqno = gCurrentSeqNo++;
     }
     
     if (call AMSend.send(TOS_BCAST_ADDR, &msgBuf, length) == SUCCESS) {
       msgBufBusy = TRUE;
+      call CollectionDebug.logEventRoute(NET_C_TREE_SENT_BEACON, bMsg->parent, 0, bMsg->cost);
     }
   }
 
@@ -186,6 +188,7 @@ implementation {
     gbCurrentHopCount = ROUTE_INVALID;
     gbCurrentCost = 0xfffe;
 
+    gOriginSeqNo = 0;
     gCurrentSeqNo = 0;
     gUpdateInterval = BEACON_PERIOD;
     msgBufBusy = FALSE;
@@ -195,6 +198,7 @@ implementation {
 
   command error_t RootControl.setRoot() {
     call Leds.led2On();
+    call CollectionDebug.logEventRoute(NET_C_TREE_NEW_PARENT, TOS_NODE_ID, 0, 0);
     isRoot = TRUE;
     return SUCCESS;
   }
@@ -229,12 +233,14 @@ implementation {
     if (isRoot) {
       return FAIL;
     }
-    
+
     if (hdr->originaddr != TOS_NODE_ID && resend == 0) {
       // supress duplicate packets
       for (i = 0; i < MHOP_HISTORY_SIZE; i++) {
         if ((gRecentPacketSender[i] == call AMPacket.source(msg)) &&
             (gRecentPacketSeqNo[i] == hdr->seqno)) {
+         call CollectionDebug.logEvent(NET_C_FE_DUPLICATE_CACHE_AT_SEND);
+         dbg("LQI", "%s no route as this is a duplicate!\n", __FUNCTION__);
           return FAIL;
         }
       }
@@ -251,6 +257,7 @@ implementation {
           gbCurrentLinkEst = 0x7fff;
           gbCurrentParent = TOS_BCAST_ADDR;
           gbCurrentHopCount = ROUTE_INVALID;
+         dbg("LQI", "%s no route as we are in a cycle!\n", __FUNCTION__);
           return FAIL;
         }
       }
@@ -258,11 +265,12 @@ implementation {
       gRecentOriginPacketSeqNo[gRecentOriginIndex] = hdr->originseqno;
       gRecentOriginIndex = (gRecentOriginIndex + 1) % MHOP_HISTORY_SIZE;
     }
-    
+
     if (resend == 0) {
       hdr->seqno = gCurrentSeqNo++;
     }
     
+    dbg("LQI", "LQI setting destination to %hu and link quality ?\n", gbCurrentParent);
     call AMPacket.setDestination(msg, gbCurrentParent);
 
     return SUCCESS;
@@ -272,8 +280,9 @@ implementation {
     lqi_header_t* header = (lqi_header_t*)call Packet.getPayload(msg, NULL);
 
     header->originaddr = TOS_NODE_ID;
-    header->originseqno = gCurrentSeqNo;
-
+    header->originseqno = gOriginSeqNo++;
+    header->seqno = gCurrentSeqNo;
+    
     if (isRoot) {
       header->hopcount = 0;
     }
@@ -281,6 +290,7 @@ implementation {
       header->hopcount = gbCurrentHopCount;
     }
 
+    dbg("LQI", "LQI setting hopcount to %hhu\n", gbCurrentHopCount);
     return SUCCESS;
   }
 
@@ -318,19 +328,23 @@ implementation {
 
 
   event void Timer.fired() {
+    call Leds.led0Toggle();
     post TimerTask();
-    call Timer.startOneShot(1024 * gUpdateInterval + 1);
+    call Timer.startOneShot((uint32_t)1024 * gUpdateInterval + 1);
   }
 
   event message_t* Receive.receive(message_t* msg, void* payload, uint8_t len) {
+    lqi_beacon_msg_t* bMsg = (lqi_beacon_msg_t*)payload;
+    am_addr_t source = call AMPacket.source(msg);
+    uint8_t lqi = call CC2420Packet.getLqi(msg);
+    
+    call CollectionDebug.logEventRoute(NET_C_TREE_RCV_BEACON, source, 0, bMsg->cost);
+    
     if (isRoot) {
       return msg;
     }
     else {
-      lqi_beacon_msg_t* bMsg = (lqi_beacon_msg_t*)payload;
-      am_addr_t source = call AMPacket.source(msg);
-      uint8_t lqi = call CC2420Packet.getLqi(msg);
-      
+      dbg("LQI,LQIRoute", "LQI receiving routing beacon from %hu with LQI %hhu that advertises %hu.\n", source, lqi, bMsg->cost);
       if (source == gbCurrentParent) {
        // try to prevent cycles
        if (bMsg->parent != TOS_NODE_ID) {
@@ -338,6 +352,7 @@ implementation {
          gbCurrentParentCost = bMsg->cost;
          gbCurrentLinkEst = adjustLQI(lqi);
          gbCurrentHopCount = bMsg->hopcount + 1;
+         dbg("LQI,LQIRoute", "  -- Not a loop\n");
        }
        else {
          gLastHeard = 0;
@@ -345,6 +360,7 @@ implementation {
          gbCurrentLinkEst = 0x7fff;
          gbCurrentParent = TOS_BCAST_ADDR;
          gbCurrentHopCount = ROUTE_INVALID;
+         dbg("LQI,LQIRoute", "  -- Detected a loop\n");
        }
        
       } else {
@@ -365,9 +381,15 @@ implementation {
          gbCurrentParentCost = bMsg->cost;
          gbCurrentLinkEst = adjustLQI(lqi);    
          gbCurrentHopCount = bMsg->hopcount + 1;
+         call CollectionDebug.logEventRoute(NET_C_TREE_NEW_PARENT, gbCurrentParent, 0, gbCurrentParentCost + gbCurrentLinkEst);
+         dbg("LQI,LQIRoute", "  -- Not a cycle.\n");
+       }
+       else {
+         dbg("LQI,LQIRoute", "  -- CYCLE.\n");
        }
       }
     }
+    dbg("LQI,LQIRoute", "Set my count to %hhu, my link to %hu and my cost to %hu.\n", gbCurrentHopCount, gbCurrentLinkEst, gbCurrentParentCost);
 
     return msg;
   }
@@ -375,6 +397,26 @@ implementation {
   event void AMSend.sendDone(message_t* msg, error_t success) {
     msgBufBusy = FALSE;
   }
+
+    /* Default implementations for CollectionDebug calls.
+     * These allow CollectionDebug not to be wired to anything if debugging
+     * is not desired. */
+
+  default command error_t CollectionDebug.logEvent(uint8_t type) {
+    return SUCCESS;
+  }
+  default command error_t CollectionDebug.logEventSimple(uint8_t type, uint16_t arg) {
+    return SUCCESS;
+  }
+  default command error_t CollectionDebug.logEventDbg(uint8_t type, uint16_t arg1, uint16_t arg2, uint16_t arg3) {
+    return SUCCESS;
+  }
+  default command error_t CollectionDebug.logEventMsg(uint8_t type, uint16_t msg, am_addr_t origin, am_addr_t node) {
+    return SUCCESS;
+  }
+  default command error_t CollectionDebug.logEventRoute(uint8_t type, am_addr_t parent, uint8_t hopcount, uint16_t etx) {
+    return SUCCESS;
+  }
   
 }
-
+