]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/net/ctp/CtpRoutingEngineP.nc
Make timing the optimal values.
[tinyos-2.x.git] / tos / lib / net / ctp / CtpRoutingEngineP.nc
index 6c16ed9b278f8d1a179135e002ea6c16d075cb68..f0ef1f60b6ab3a71ecf4bba28c7afdaf77fe7de7 100644 (file)
@@ -93,7 +93,7 @@
  *  @see Net2-WG
  */
 
-generic module CtpRoutingEngineP(uint8_t routingTableSize, uint16_t minInterval, uint16_t maxInterval) {
+generic module CtpRoutingEngineP(uint8_t routingTableSize, uint32_t minInterval, uint32_t maxInterval) {
     provides {
         interface UnicastNameFreeRouting as Routing;
         interface RootControl;
@@ -115,7 +115,6 @@ generic module CtpRoutingEngineP(uint8_t routingTableSize, uint16_t minInterval,
         interface CtpCongestion;
 
        interface CompareBit;
-       interface CC2420Packet;
 
     }
 }
@@ -165,13 +164,13 @@ implementation {
     error_t routingTableUpdateEntry(am_addr_t, am_addr_t , uint16_t);
     error_t routingTableEvict(am_addr_t neighbor);
 
-    uint16_t currentInterval = minInterval;
+    uint32_t currentInterval = minInterval;
     uint32_t t; 
     bool tHasPassed;
 
     void chooseAdvertiseTime() {
        t = currentInterval;
-       t *= 512; // * 1024 / 2
+       t /= 2;
        t += call Random.rand32() % t;
        tHasPassed = FALSE;
        call BeaconTimer.stop();
@@ -184,21 +183,18 @@ implementation {
     }
 
     void decayInterval() {
-      if (!state_is_root) {
         currentInterval *= 2;
         if (currentInterval > maxInterval) {
           currentInterval = maxInterval;
         }
-      }
       chooseAdvertiseTime();
     }
 
     void remainingInterval() {
        uint32_t remaining = currentInterval;
-       remaining *= 1024;
        remaining -= t;
        tHasPassed = TRUE;
-       call BeaconTimer.startPeriodic(t);
+       call BeaconTimer.startOneShot(remaining);
     }
 
     command error_t Init.init() {
@@ -260,7 +256,7 @@ implementation {
 
     /* Converts the output of the link estimator to path metric
      * units, that can be *added* to form path metric measures */
-    uint16_t evaluateEtx(uint8_t quality) {
+    uint16_t evaluateEtx(uint16_t quality) {
         //dbg("TreeRouting","%s %d -> %d\n",__FUNCTION__,quality, quality+10);
         return (quality + 10);
     }
@@ -301,8 +297,8 @@ implementation {
             /* Compute this neighbor's path metric */
             linkEtx = evaluateEtx(call LinkEstimator.getLinkQuality(entry->neighbor));
             dbg("TreeRouting", 
-                "routingTable[%d]: neighbor: [id: %d parent: %d etx: %d]\n",  
-                i, entry->neighbor, entry->info.parent, linkEtx);
+                "routingTable[%d]: neighbor: [id: %d parent: %d etx: %d retx: %d]\n",  
+                i, entry->neighbor, entry->info.parent, linkEtx, entry->info.etx);
             pathEtx = linkEtx + entry->info.etx;
             /* Operations specific to the current parent */
             if (entry->neighbor == routeInfo.parent) {
@@ -325,6 +321,7 @@ implementation {
             }
             
             if (pathEtx < minEtx) {
+             dbg("TreeRouting", "   best is %d, setting to %d\n", pathEtx, entry->neighbor);
                 minEtx = pathEtx;
                 best = entry;
             }  
@@ -352,17 +349,23 @@ implementation {
                 // we need it: i. when choosing a parent (here); 
                 //            ii. when choosing a next hop
                 parentChanges++;
-                resetInterval();
+
                 dbg("TreeRouting","Changed parent. from %d to %d\n", routeInfo.parent, best->neighbor);
-                call CollectionDebug.logEventRoute(NET_C_TREE_NEW_PARENT, best->neighbor, 0, best->info.etx); 
+                call CollectionDebug.logEventDbg(NET_C_TREE_NEW_PARENT, best->neighbor, best->info.etx, minEtx);
                 call LinkEstimator.unpinNeighbor(routeInfo.parent);
                 call LinkEstimator.pinNeighbor(best->neighbor);
                 call LinkEstimator.clearDLQ(best->neighbor);
-                atomic {
+
+               atomic {
                     routeInfo.parent = best->neighbor;
                     routeInfo.etx = best->info.etx;
                     routeInfo.congested = best->info.congested;
                 }
+               /* If we follow the CTP paper this should be in here.
+               if (currentEtx - minEtx > 20) {
+                 call CtpInfo.triggerRouteUpdate();
+               }
+               */
             }
         }    
 
@@ -457,7 +460,7 @@ implementation {
     }
 
 
-    ctp_routing_header_t* getHeader(message_t* m) {
+    ctp_routing_header_t* getHeader(message_t* ONE m) {
       return (ctp_routing_header_t*)call BeaconSend.getPayload(m, call BeaconSend.maxPayloadLength());
     }
     
@@ -508,7 +511,6 @@ implementation {
         if (call CtpRoutingPacket.getOption(msg, CTP_OPT_PULL)) {
               resetInterval();
         }
-        //post updateRouteTask();
         return msg;
     }
 
@@ -549,7 +551,12 @@ implementation {
             return FAIL;
         if (routeInfo.parent == INVALID_ADDR)    
             return FAIL;
-        *etx = routeInfo.etx;
+       if (state_is_root == 1) {
+         *etx = 0;
+       } else {
+         // path etx = etx(parent) + etx(link to the parent)
+         *etx = routeInfo.etx + evaluateEtx(call LinkEstimator.getLinkQuality(routeInfo.parent));
+       }
         return SUCCESS;
     }
 
@@ -558,24 +565,11 @@ implementation {
     }
 
     command void CtpInfo.triggerRouteUpdate() {
-      // Random time in interval 64-127ms
-      uint16_t beaconDelay = call Random.rand16();
-      beaconDelay &= 0x3f; 
-      beaconDelay += 64;
-      if (call BeaconTimer.gett0() + call BeaconTimer.getdt() -
-                                     call BeaconTimer.getNow() >= beaconDelay) {
-         call BeaconTimer.stop();
-         call BeaconTimer.startOneShot(beaconDelay);
-        }
+      resetInterval();
      }
 
     command void CtpInfo.triggerImmediateRouteUpdate() {
-      // Random time in interval 4-11ms
-      uint16_t beaconDelay = call Random.rand16();
-      beaconDelay &= 0x7; 
-      beaconDelay += 4;
-      call BeaconTimer.stop();
-      call BeaconTimer.startOneShot(beaconDelay);
+      resetInterval();
     }
 
     command void CtpInfo.setNeighborCongested(am_addr_t n, bool congested) {