]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/net/lqi/LqiForwardingEngineP.nc
Remove bogus 'tab:4' and 'tab:2' markers.
[tinyos-2.x.git] / tos / lib / net / lqi / LqiForwardingEngineP.nc
index 2029bb9672203d4454650d0024ebf337db6914a1..e8120900244243c201a19082fcf84dd21f26d15c 100644 (file)
@@ -1,6 +1,5 @@
 // $Id$
 
-
 /* Copyright (c) 2007 Stanford University.
  * All rights reserved.
  *
@@ -31,7 +30,7 @@
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-/*                                                                     tab:4
+/*
  * "Copyright (c) 2000-2003 The Regents of the University  of California.  
  * All rights reserved.
  *
@@ -93,6 +92,7 @@ module LqiForwardingEngineP {
     interface Packet;
   }
   uses {
+    interface SplitControl;
     interface Receive as SubReceive;
     interface AMSend as SubSend;
     interface AMSend as SubSendMine;
@@ -112,7 +112,8 @@ implementation {
 
   enum {
     FWD_QUEUE_SIZE = MHOP_QUEUE_SIZE, // Forwarding Queue
-    EMPTY = 0xff
+    EMPTY = 0xff,
+    MAX_RETRIES = 5
   };
 
   /* Internal storage and scheduling state */
@@ -121,11 +122,13 @@ implementation {
   uint8_t FwdBufBusy[FWD_QUEUE_SIZE];
   uint8_t iFwdBufHead, iFwdBufTail;
   uint16_t sendFailures = 0;
-  uint8_t fail_count = 0;
-  int fwdbusy = 0;
-  
+  uint8_t fwd_fail_count = 0;
+  uint8_t my_fail_count = 0;
+  bool fwdbusy = FALSE;
+  bool running = FALSE;
   lqi_header_t* getHeader(message_t* msg) {
-    return (lqi_header_t*) call SubPacket.getPayload(msg, NULL);
+    return (lqi_header_t*) call SubPacket.getPayload(msg, sizeof(lqi_header_t));
   }
   
   /***********************************************************************
@@ -149,8 +152,27 @@ implementation {
     initialize();
     return SUCCESS;
   }
+  message_t* nextMsg();
+  static void forward(message_t* msg);
 
+  event void SplitControl.startDone(error_t err) {
+    message_t* nextToSend;
+    if (err != SUCCESS) {return;}
+    nextToSend = nextMsg();
+    running = TRUE;
+    fwdbusy = FALSE;
+
+    if (nextToSend != NULL) {
+      forward(nextToSend);
+    }
+  }
 
+
+  event void SplitControl.stopDone(error_t err) {
+    if (err != SUCCESS) {return;}
+    running = FALSE;
+  }
   /***********************************************************************
    * Commands and events
    ***********************************************************************/
@@ -162,6 +184,9 @@ implementation {
     if (call RootControl.isRoot()) {
       return FAIL;
     }
+    if (running == FALSE) {
+      return EOFF;
+    }
     call RouteSelect.initializeFields(pMsg);
     
     if (call RouteSelect.selectRoute(pMsg, 0) != SUCCESS) {
@@ -240,8 +265,8 @@ implementation {
   static void forward(message_t* msg) {
     // Failures at the send level do not cause the seq. number space to be 
     // rolled back properly.  This is somewhat broken.
-    if (fwdbusy) {
-      dbg("LQI", "%s forwarding busy, wait for later.\n", __FUNCTION__);
+    if (fwdbusy || running == FALSE) {
+      dbg("LQI", "%s forwarding busy or off, wait for later.\n", __FUNCTION__);
       return;
     }
     else {
@@ -249,21 +274,32 @@ implementation {
       if (call SubSend.send(call AMPacket.destination(msg),
                            msg,
                            call SubPacket.payloadLength(msg)) == SUCCESS) {
+       call CollectionDebug.logEventMsg(NET_C_DBG_1, 
+                                        call CollectionPacket.getSequenceNumber(msg), 
+                                        call CollectionPacket.getOrigin(msg), 
+                                        call AMPacket.destination(msg));
        dbg("LQI", "%s: Send to %hu success.\n", __FUNCTION__, call AMPacket.destination(msg));
+        fwdbusy = TRUE;
       }
-      fwdbusy = TRUE;
     }
   }
 
-  event message_t* SubReceive.receive(message_t* msg, void* payload, uint8_t len) {
+  event message_t* SubReceive.receive(message_t* ONE msg, void* COUNT_NOK(len) payload, uint8_t len) {
     collection_id_t id = call CollectionPacket.getType(msg);
     payload += sizeof(lqi_header_t);
     len -= sizeof(lqi_header_t);
 
+    call CollectionDebug.logEventMsg(NET_C_FE_RCV_MSG, 
+                                    call CollectionPacket.getSequenceNumber(msg), 
+                                    call CollectionPacket.getOrigin(msg), 
+                                    call AMPacket.destination(msg));
     if (call RootControl.isRoot()) {
       dbg("LQI,LQIDeliver", "LQI Root is receiving packet from node %hu @%s\n", getHeader(msg)->originaddr, sim_time_string());
       return signal Receive.receive[id](msg, payload, len);
     }
+    else if (call AMPacket.destination(msg) != call AMPacket.address()) {
+      return msg;
+    }
     else if (signal Intercept.forward[id](msg, payload, len)) {
       dbg("LQI,LQIDeliver", "LQI fwd is forwarding packet from node %hu @%s\n", getHeader(msg)->originaddr, sim_time_string());
       return mForward(msg);
@@ -275,7 +311,7 @@ implementation {
   
   message_t* nextMsg() {
     int i;
-    int inc = call Random.rand16();
+    uint16_t inc = call Random.rand16() & 0xfff;
     for (i = 0; i < FWD_QUEUE_SIZE; i++) {
       int pindex = (i + inc) % FWD_QUEUE_SIZE;
       if (FwdBufBusy[pindex]) {
@@ -290,28 +326,30 @@ implementation {
     message_t* nextToSend;
     if (!call PacketAcknowledgements.wasAcked(msg) &&
        call AMPacket.destination(msg) != TOS_BCAST_ADDR &&
-       fail_count < 5){
+       fwd_fail_count < MAX_RETRIES){
       call RouteSelect.selectRoute(msg, 1);
       call PacketAcknowledgements.requestAck(msg);
       if (call SubSend.send(call AMPacket.destination(msg),
                            msg,
                            call SubPacket.payloadLength(msg)) == SUCCESS) {
-       dbg("LQI", "Packet not acked, retransmit:\t%s\n", fields(msg));
+       dbg("LQI", "Packet not acked, retransmit @%s:\n\t%s\n", sim_time_string(), fields(msg));
         call CollectionDebug.logEventMsg(NET_C_FE_SENDDONE_WAITACK, 
                                         call CollectionPacket.getSequenceNumber(msg), 
                                         call CollectionPacket.getOrigin(msg), 
                                          call AMPacket.destination(msg));
-       fail_count ++;
+       fwd_fail_count ++;
+       return;
       } else {
        call CollectionDebug.logEventMsg(NET_C_FE_SENDDONE_FAIL, 
                                         call CollectionPacket.getSequenceNumber(msg), 
                                         call CollectionPacket.getOrigin(msg), 
                                          call AMPacket.destination(msg));
-       dbg("LQI", "Packet not acked, retransmit fail:\t%s\n", fields(msg));
+       dbg("LQI", "Packet not acked, retransmit fail @%s:\n\t%s\n", sim_time_string(), fields(msg));
        sendFailures++;
+       return;
       }
     }
-    else if (fail_count >= 5) {
+    else if (fwd_fail_count >= MAX_RETRIES) {
       call CollectionDebug.logEventMsg(NET_C_FE_SENDDONE_FAIL_ACK_FWD, 
                                       call CollectionPacket.getSequenceNumber(msg), 
                                       call CollectionPacket.getOrigin(msg), 
@@ -326,7 +364,7 @@ implementation {
                                       call AMPacket.destination(msg));
     }
     
-    fail_count = 0;
+    fwd_fail_count = 0;
     buf = is_ours(msg);
     if (buf != -1) {
       FwdBufBusy[(uint8_t)buf] = 0;
@@ -345,43 +383,47 @@ implementation {
   event void SubSendMine.sendDone(message_t* msg, error_t success) {
     if (!call PacketAcknowledgements.wasAcked(msg) &&
        call AMPacket.destination(msg) != TOS_BCAST_ADDR &&
-       fail_count < 5){
+       my_fail_count < MAX_RETRIES){
       call RouteSelect.selectRoute(msg, 1);
       call PacketAcknowledgements.requestAck(msg);
       if (call SubSendMine.send(call AMPacket.destination(msg),
                            msg,
                            call SubPacket.payloadLength(msg)) == SUCCESS) {
-       dbg("LQI", "Packet not acked, retransmit:\t%s\n", fields(msg));
+       dbg("LQI", "Local packet not acked, retransmit (%hhu) @%s:\n\t%s\n", my_fail_count, sim_time_string(), fields(msg));
        call CollectionDebug.logEventMsg(NET_C_FE_SENDDONE_WAITACK, 
                                         call CollectionPacket.getSequenceNumber(msg), 
                                         call CollectionPacket.getOrigin(msg), 
                                          call AMPacket.destination(msg));
-       fail_count ++;
+       my_fail_count ++;
+       return;
       } else {
        call CollectionDebug.logEventMsg(NET_C_FE_SENDDONE_FAIL, 
                                         call CollectionPacket.getSequenceNumber(msg), 
                                         call CollectionPacket.getOrigin(msg), 
                                          call AMPacket.destination(msg));
-       dbg("LQI", "Packet not acked, retransmit fail:\t%s\n", fields(msg));
+       dbg("LQI", "Local packet not acked, retransmit fail @%s:\n\t%s\n", sim_time_string(), fields(msg));
        sendFailures++;
+       signal Send.sendDone(msg, FAIL);
+       return;
       }
     }
-    else if (fail_count >= 5) {
+    else if (my_fail_count >= MAX_RETRIES) {
       call CollectionDebug.logEventMsg(NET_C_FE_SENDDONE_FAIL_ACK_SEND, 
                                       call CollectionPacket.getSequenceNumber(msg), 
                                       call CollectionPacket.getOrigin(msg), 
                                       call AMPacket.destination(msg));
-      dbg("LQI", "Packet failed:\t%s\n", fields(msg));
+      dbg("LQI", "Local packet failed:\t%s\n", fields(msg));
     }
     else if (call PacketAcknowledgements.wasAcked(msg)) {
-      dbg("LQI", "Packet acked:\t%s\n", fields(msg));
+      dbg("LQI", "Local packet acked:\t%s\n", fields(msg));
       call CollectionDebug.logEventMsg(NET_C_FE_SENT_MSG, 
                                       call CollectionPacket.getSequenceNumber(msg), 
                                       call CollectionPacket.getOrigin(msg), 
                                       call AMPacket.destination(msg));
     }
 
-    fail_count = 0;
+    my_fail_count = 0;
+    dbg("LQI", "Local send done with success %d\n", success);
     signal Send.sendDone(msg, success);
   }
 
@@ -421,8 +463,8 @@ implementation {
     
   }
 
-  command void* Send.getPayload(message_t* m) {
-    return call Packet.getPayload(m, NULL);
+  command void* Send.getPayload(message_t* m, uint8_t len) {
+    return call Packet.getPayload(m, len);
   }
 
   command uint8_t Send.maxPayloadLength() {
@@ -434,22 +476,6 @@ implementation {
   }
 
   
-  command void* Receive.getPayload[collection_id_t id](message_t* msg, uint8_t* len) {
-    return call Packet.getPayload(msg, len);
-  }
-  
-  command uint8_t Receive.payloadLength[collection_id_t id](message_t* m) {
-    return call Packet.payloadLength(m);
-  }
-
-  command void* Snoop.getPayload[collection_id_t id](message_t* msg, uint8_t* len) {
-    return call Packet.getPayload(msg, len);
-  }
-  
-  command uint8_t Snoop.payloadLength[collection_id_t id](message_t* m) {
-    return call Packet.payloadLength(m);
-  }
-  
   command uint8_t Packet.payloadLength(message_t* msg) {
     return call SubPacket.payloadLength(msg) - sizeof(lqi_header_t);
   }
@@ -459,12 +485,11 @@ implementation {
   command uint8_t Packet.maxPayloadLength() {
     return (call SubPacket.maxPayloadLength() - sizeof(lqi_header_t));
   }
-  command void* Packet.getPayload(message_t* msg, uint8_t* len) {
-    void* rval = call SubPacket.getPayload(msg, len);
-    if (len != NULL) {
-      *len -= sizeof(lqi_header_t);
+  command void* Packet.getPayload(message_t* msg, uint8_t len) {
+    void* rval = call SubPacket.getPayload(msg, len + sizeof(lqi_header_t));
+    if (rval != NULL) {
+      rval += sizeof(lqi_header_t);
     }
-    rval += sizeof(lqi_header_t);
     return rval;
   }
 
@@ -495,14 +520,36 @@ implementation {
     lqi_header_t* hdr = getHeader(msg);
     hdr->originseqno = seqno;
   }
+
+
   
  default event void Send.sendDone(message_t* pMsg, error_t success) {}
  default event message_t* Snoop.receive[collection_id_t id](message_t* pMsg, void* payload, uint8_t len) {return pMsg;}
  default event message_t* Receive.receive[collection_id_t id](message_t* pMsg, void* payload, uint8_t len) {
    return pMsg;
  }
- default event bool Intercept.forward[collection_id_t id](message_t* pMsg, void* payload, uint16_t len) {
+ default event bool Intercept.forward[collection_id_t id](message_t* pMsg, void* payload, uint8_t len) {
    return 1;
  }
+
+  /* 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 metric) {
+    return SUCCESS;
+  }
 }