]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - apps/tests/TestNetwork/TestNetworkC.nc
Merge over into the trunk.
[tinyos-2.x.git] / apps / tests / TestNetwork / TestNetworkC.nc
index 4ed48a2e76aec960a1b5ed6d80cbf7d9cc1b9c03..a2ba9e11711dcc6dff9d20da514f2a4171a0f7fb 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <Timer.h>
 #include "TestNetwork.h"
+#include "CtpDebugMsg.h"
 
 module TestNetworkC {
   uses interface Boot;
@@ -27,8 +28,13 @@ 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;
 }
 implementation {
   task void uartEchoTask();
@@ -36,10 +42,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();
   }
@@ -56,71 +68,63 @@ implementation {
        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) {
+
+   
+  void sendMessage() {
     TestNetworkMsg* msg = (TestNetworkMsg*)call Send.getPayload(&packet);
-    uint8_t hopcount;
     uint16_t metric;
     am_addr_t parent;
 
-    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() {
+    uint16_t nextInt;
+    call Leds.led0Toggle();
+    dbg("TestNetworkC", "TestNetworkC: Timer fired.\n");
+    nextInt = call Random.rand16() % 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;
-    }
+    sendBusy = FALSE;
     dbg("TestNetworkC", "Send completed.\n");
   }
   
@@ -134,27 +138,70 @@ implementation {
   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();
+    if (!call Pool.size() <= (TEST_NETWORK_QUEUE_SIZE < 4)? 1:3)  {
+      call CtpCongestion.setClientCongested(TRUE);
+    }
+    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, recvPtr, call Receive.payloadLength(msg) + 4) == SUCCESS) {
+       uartbusy = TRUE;
+     }
+     else {
+      call CollectionDebug.logEventMsg(NET_C_DBG_2,
+                                      call CollectionPacket.getSequenceNumber(recvPtr),
+                                      call CollectionPacket.getOrigin(recvPtr),
+                                      call AMPacket.destination(recvPtr));
+     }
+   }
+ }
 
   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;
+    }
 }