]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - apps/tests/TestNetwork/TestNetworkC.nc
explain test procedure and how to interpret results; change code to use leds to indic...
[tinyos-2.x.git] / apps / tests / TestNetwork / TestNetworkC.nc
index 4ed48a2e76aec960a1b5ed6d80cbf7d9cc1b9c03..05e44d2fbf2ca1b41a5fc246a7d4864d233e1e0c 100644 (file)
 
 #include <Timer.h>
 #include "TestNetwork.h"
+#include "CtpDebugMsg.h"
 
 module TestNetworkC {
   uses interface Boot;
   uses interface SplitControl as RadioControl;
   uses interface SplitControl as SerialControl;
   uses interface StdControl as RoutingControl;
-  uses interface DisseminationValue<uint16_t> as DisseminationPeriod;
+  uses interface StdControl as DisseminationControl;
+  uses interface DisseminationValue<uint32_t> as DisseminationPeriod;
   uses interface Send;
   uses interface Leds;
   uses interface Read<uint16_t> as ReadSensor;
@@ -27,8 +29,14 @@ module TestNetworkC {
   uses interface Receive;
   uses interface AMSend as UARTSend;
   uses interface CollectionPacket;
-  uses interface TreeRoutingInspect;
+  uses interface CtpInfo;
+  uses interface CtpCongestion;
   uses interface Random;
+  uses interface Queue<message_t*>;
+  uses interface Pool<message_t>;
+  uses interface CollectionDebug;
+  uses interface AMPacket;
+  uses interface Packet as RadioPacket;
 }
 implementation {
   task void uartEchoTask();
@@ -36,10 +44,16 @@ implementation {
   message_t uartpacket;
   message_t* recvPtr = &uartpacket;
   uint8_t msglen;
-  bool busy = FALSE, uartbusy = FALSE;
+  bool sendBusy = FALSE;
+  bool uartbusy = FALSE;
   bool firstTimer = TRUE;
   uint16_t seqno;
-  
+  enum {
+    SEND_INTERVAL = 8192
+  };
+
+  event void ReadSensor.readDone(error_t err, uint16_t val) { }  
+
   event void Boot.booted() {
     call SerialControl.start();
   }
@@ -51,110 +65,158 @@ implementation {
       call RadioControl.start();
     }
     else {
+      call DisseminationControl.start();
       call RoutingControl.start();
       if (TOS_NODE_ID % 500 == 0) {
        call RootControl.setRoot();
       }
       seqno = 0;
-      call Timer.startOneShot(call Random.rand16() & 0x1ff);
+        call Timer.startOneShot(call Random.rand16() & 0x1ff);
     }
   }
 
   event void RadioControl.stopDone(error_t err) {}
   event void SerialControl.stopDone(error_t err) {}    
-  
-  event void Timer.fired() {
-    call Leds.led0Toggle();
-    dbg("TestNetworkC", "TestNetworkC: Timer fired.\n");
-    if (firstTimer) {
-      firstTimer = FALSE;
-      call Timer.startPeriodic(1024);
-    }
-    if (busy || call ReadSensor.read() != SUCCESS) {
-      signal ReadSensor.readDone(SUCCESS, 0);
-      return;
-    }
-    busy = TRUE;
-  }
 
   void failedSend() {
     dbg("App", "%s: Send failed.\n", __FUNCTION__);
+    call CollectionDebug.logEvent(NET_C_DBG_1);
   }
-  
-  event void ReadSensor.readDone(error_t err, uint16_t val) {
-    TestNetworkMsg* msg = (TestNetworkMsg*)call Send.getPayload(&packet);
-    uint8_t hopcount;
+
+   
+  void sendMessage() {
+    TestNetworkMsg* msg = (TestNetworkMsg*)call Send.getPayload(&packet, sizeof(TestNetworkMsg));
     uint16_t metric;
-    am_addr_t parent;
+    am_addr_t parent = 0;
 
-    call TreeRoutingInspect.getParent(&parent);
-    call TreeRoutingInspect.getHopcount(&hopcount);
-    call TreeRoutingInspect.getMetric(&metric);
+    call CtpInfo.getParent(&parent);
+    call CtpInfo.getEtx(&metric);
 
     msg->source = TOS_NODE_ID;
     msg->seqno = seqno;
-    msg->data = val;
+    msg->data = 0xCAFE;
     msg->parent = parent;
-    msg->hopcount = hopcount;
+    msg->hopcount = 0;
     msg->metric = metric;
 
-    if (err != SUCCESS) {
-      dbg("App", "%s: read done failed.\n", __FUNCTION__);
-      busy = FALSE;
-    }
     if (call Send.send(&packet, sizeof(TestNetworkMsg)) != SUCCESS) {
       failedSend();
       call Leds.led0On();
       dbg("TestNetworkC", "%s: Transmission failed.\n", __FUNCTION__);
     }
     else {
+      sendBusy = TRUE;
       seqno++; 
       dbg("TestNetworkC", "%s: Transmission succeeded.\n", __FUNCTION__);
-
     }
   }
 
+  event void Timer.fired() {
+    uint32_t nextInt;
+    dbg("TestNetworkC", "TestNetworkC: Timer fired.\n");
+    nextInt = call Random.rand32() % SEND_INTERVAL;
+    nextInt += SEND_INTERVAL >> 1;
+    call Timer.startOneShot(nextInt);
+    if (!sendBusy)
+       sendMessage();
+  }
+
   event void Send.sendDone(message_t* m, error_t err) {
     if (err != SUCCESS) {
-       //      call Leds.led0On();
-    }
-    else {
-      busy = FALSE;
+      call Leds.led0On();
     }
+    sendBusy = FALSE;
     dbg("TestNetworkC", "Send completed.\n");
   }
   
   event void DisseminationPeriod.changed() {
-    const uint16_t* newVal = call DisseminationPeriod.get();
+    const uint32_t* newVal = call DisseminationPeriod.get();
     call Timer.stop();
     call Timer.startPeriodic(*newVal);
   }
 
+
+  uint8_t prevSeq = 0;
+  uint8_t firstMsg = 0;
+
   event message_t* 
   Receive.receive(message_t* msg, void* payload, uint8_t len) {
     dbg("TestNetworkC", "Received packet at %s from node %hhu.\n", sim_time_string(), call CollectionPacket.getOrigin(msg));
-    call Leds.led1Toggle();    
-    if (!uartbusy) {
-      message_t* tmp = recvPtr;
-      recvPtr = msg;
-      uartbusy = TRUE;
-      msglen = len + 4;
-      post uartEchoTask();
-      call Leds.led2Toggle();
+    call Leds.led1Toggle();
+
+    if (call CollectionPacket.getOrigin(msg) == 1) {
+      if (firstMsg == 1) {
+       if (call CollectionPacket.getSequenceNumber(msg) - prevSeq > 1) {
+         call Leds.led2On();
+       }
+      } else {
+       firstMsg = 1;
+      }
+      prevSeq = call CollectionPacket.getSequenceNumber(msg);
+    }
+
+    if (!call Pool.empty() && call Queue.size() < call Queue.maxSize()) {
+      message_t* tmp = call Pool.get();
+      call Queue.enqueue(msg);
+      if (!uartbusy) {
+        post uartEchoTask();
+      }
       return tmp;
     }
     return msg;
 }
+ }
 
 task void uartEchoTask() {
+ task void uartEchoTask() {
     dbg("Traffic", "Sending packet to UART.\n");
-    if (call UARTSend.send(0xffff, recvPtr, msglen) != SUCCESS) {
-      uartbusy = FALSE;
-    }
-  }
+   if (call Queue.empty()) {
+     return;
+   }
+   else if (!uartbusy) {
+     message_t* msg = call Queue.dequeue();
+     dbg("Traffic", "Sending packet to UART.\n");
+     if (call UARTSend.send(0xffff, msg, call RadioPacket.payloadLength(msg)) == SUCCESS) {
+       uartbusy = TRUE;
+     }
+     else {
+      call CollectionDebug.logEventMsg(NET_C_DBG_2,
+                                      call CollectionPacket.getSequenceNumber(msg),
+                                      call CollectionPacket.getOrigin(msg),
+                                      call AMPacket.destination(msg));
+     }
+   }
+ }
 
   event void UARTSend.sendDone(message_t *msg, error_t error) {
     dbg("Traffic", "UART send done.\n");
     uartbusy = FALSE;
+    call Pool.put(msg);
+    if (!call Queue.empty()) {
+      post uartEchoTask();
+    } 
+    else {
+      //        call CtpCongestion.setClientCongested(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 metric) {
+        return SUCCESS;
+    }
 }