]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/net/ctp/CtpForwardingEngineP.nc
<= will detect loops in which all the nodes have the same metric to the root
[tinyos-2.x.git] / tos / lib / net / ctp / CtpForwardingEngineP.nc
index f62ac97e16b184f5aa8d2a26d19719d7349398fa..eac244e8822948bdb8653586b9a376accdfc6e5b 100644 (file)
@@ -181,6 +181,9 @@ implementation {
   /* Tracks our parent's congestion state. */
   bool parentCongested = FALSE;
 
+  /* Threshold for congestion */
+  uint8_t congestionThreshold;
+
   /* Keeps track of whether the routing layer is running; if not,
    * it will not send packets. */
   bool running = FALSE;
@@ -234,6 +237,7 @@ implementation {
       clientPtrs[i] = clientEntries + i;
       dbg("Forwarder", "clientPtrs[%hhu] = %p\n", i, clientPtrs[i]);
     }
+    congestionThreshold = (call SendQueue.maxSize()) >> 1;
     loopbackMsgPtr = &loopbackMsg;
     lastParent = call AMPacket.address();
     seqno = 0;
@@ -288,7 +292,7 @@ implementation {
   }
 
   ctp_data_header_t* getHeader(message_t* m) {
-    return (ctp_data_header_t*)call SubPacket.getPayload(m, NULL);
+    return (ctp_data_header_t*)call SubPacket.getPayload(m, sizeof(ctp_data_header_t));
   }
  
   /*
@@ -354,8 +358,8 @@ implementation {
     return call Packet.maxPayloadLength();
   }
 
-  command void* Send.getPayload[uint8_t client](message_t* msg) {
-    return call Packet.getPayload(msg, NULL);
+  command void* Send.getPayload[uint8_t client](message_t* msg, uint8_t len) {
+    return call Packet.getPayload(msg, len);
   }
 
   /*
@@ -436,6 +440,10 @@ implementation {
       if (call SentCache.lookup(qe->msg)) {
         call CollectionDebug.logEvent(NET_C_FE_DUPLICATE_CACHE_AT_SEND);
         call SendQueue.dequeue();
+       if (call MessagePool.put(qe->msg) != SUCCESS)
+         call CollectionDebug.logEvent(NET_C_FE_PUT_MSGPOOL_ERR);
+       if (call QEntryPool.put(qe) != SUCCESS)
+         call CollectionDebug.logEvent(NET_C_FE_PUT_QEPOOL_ERR);
         post sendTask();
         return;
       }
@@ -455,8 +463,8 @@ implementation {
        
         dbg("Forwarder", "%s: I'm a root, so loopback and signal receive.\n", __FUNCTION__);
         loopbackMsgPtr = signal Receive.receive[collectid](loopbackMsgPtr,
-                                                         call Packet.getPayload(loopbackMsgPtr, NULL), 
-                                                         call Packet.payloadLength(loopbackMsgPtr));
+                                                          call Packet.getPayload(loopbackMsgPtr, call Packet.payloadLength(loopbackMsgPtr)), 
+                                                          call Packet.payloadLength(loopbackMsgPtr));
         signal SubSend.sendDone(qe->msg, SUCCESS);
         return;
       }
@@ -619,7 +627,6 @@ implementation {
         call CollectionDebug.logEvent(NET_C_FE_PUT_QEPOOL_ERR);
       sending = FALSE;
       startRetxmitTimer(SENDDONE_OK_WINDOW, SENDDONE_OK_OFFSET);
-      call Leds.led2Toggle();
     }
     else {
       dbg("Forwarder", "%s: BUG: we have a pool entry, but the pool is full, client is %hhu.\n", __FUNCTION__, qe->client);
@@ -677,9 +684,11 @@ implementation {
         // Loop-detection code:
         if (call CtpInfo.getEtx(&gradient) == SUCCESS) {
           // We only check for loops if we know our own metric
-          if (call CtpPacket.getEtx(m) < gradient) {
-            // The incoming packet's metric (gradient) is less than our
-            // own gradient.  Trigger a route update and backoff.
+          if (call CtpPacket.getEtx(m) <= gradient) {
+            // If our etx metric is less than or equal to the etx value
+           // on the packet (etx of the previous hop node), then we believe
+           // we are in a loop.
+           // Trigger a route update and backoff.
             call CtpInfo.triggerImmediateRouteUpdate();
             startRetxmitTimer(LOOPY_WINDOW, LOOPY_OFFSET);
             call CollectionDebug.logEventMsg(NET_C_FE_LOOP_DETECTED,
@@ -724,7 +733,6 @@ implementation {
    */ 
   event message_t* 
   SubReceive.receive(message_t* msg, void* payload, uint8_t len) {
-    uint8_t netlen;
     collection_id_t collectid;
     bool duplicate = FALSE;
     fe_queue_entry_t* qe;
@@ -742,7 +750,7 @@ implementation {
     call CollectionDebug.logEventMsg(NET_C_FE_RCV_MSG,
                                         call CollectionPacket.getSequenceNumber(msg), 
                                         call CollectionPacket.getOrigin(msg), 
-                                         call AMPacket.destination(msg));
+                                    thl--);
     if (len > call SubSend.maxPayloadLength()) {
       return msg;
     }
@@ -772,13 +780,13 @@ implementation {
     // If I'm the root, signal receive. 
     else if (call RootControl.isRoot())
       return signal Receive.receive[collectid](msg, 
-                        call Packet.getPayload(msg, &netlen), 
-                        call Packet.payloadLength(msg));
+                                              call Packet.getPayload(msg, call Packet.payloadLength(msg)), 
+                                              call Packet.payloadLength(msg));
     // I'm on the routing path and Intercept indicates that I
     // should not forward the packet.
     else if (!signal Intercept.forward[collectid](msg, 
-                        call Packet.getPayload(msg, &netlen), 
-                        call Packet.payloadLength(msg)))
+                                                 call Packet.getPayload(msg, call Packet.payloadLength(msg)), 
+                                                 call Packet.payloadLength(msg)))
       return msg;
     else {
       dbg("Route", "Forwarding packet from %hu.\n", getHeader(msg)->origin);
@@ -786,25 +794,6 @@ implementation {
     }
   }
 
-  command void* 
-  Receive.getPayload[collection_id_t id](message_t* msg, uint8_t* len) {
-    return call Packet.getPayload(msg, NULL);
-  }
-
-  command uint8_t
-  Receive.payloadLength[collection_id_t id](message_t *msg) {
-    return call Packet.payloadLength(msg);
-  }
-
-  command void *
-  Snoop.getPayload[collection_id_t id](message_t *msg, uint8_t *len) {
-    return call Packet.getPayload(msg, NULL);
-  }
-
-  command uint8_t Snoop.payloadLength[collection_id_t id](message_t *msg) {
-    return call Packet.payloadLength(msg);
-  }
-
   event message_t* 
   SubSnoop.receive(message_t* msg, void *payload, uint8_t len) {
     //am_addr_t parent = call UnicastNameFreeRouting.nextHop();
@@ -836,7 +825,7 @@ implementation {
   command bool CtpCongestion.isCongested() {
     // A simple predicate for now to determine congestion state of
     // this node.
-    bool congested = (call SendQueue.size() + 2 >= call SendQueue.maxSize()) ? 
+    bool congested = (call SendQueue.size() > congestionThreshold) ? 
       TRUE : FALSE;
     return ((congested || clientCongested)?TRUE:FALSE);
   }
@@ -867,12 +856,12 @@ implementation {
     return call SubPacket.maxPayloadLength() - sizeof(ctp_data_header_t);
   }
 
-  command void* Packet.getPayload(message_t* msg, uint8_t* len) {
-    uint8_t* payload = call SubPacket.getPayload(msg, len);
-    if (len != NULL) {
-      *len -= sizeof(ctp_data_header_t);
+  command void* Packet.getPayload(message_t* msg, uint8_t len) {
+    uint8_t* payload = call SubPacket.getPayload(msg, len + sizeof(ctp_data_header_t));
+    if (payload != NULL) {
+      payload += sizeof(ctp_data_header_t);
     }
-    return payload + sizeof(ctp_data_header_t);
+    return payload;
   }
 
   command am_addr_t       CollectionPacket.getOrigin(message_t* msg) {return getHeader(msg)->origin;}