]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - apps/AntiTheft/Root/AntiTheftRootC.nc
Merge TinyOS 2.1.1 into master.
[tinyos-2.x.git] / apps / AntiTheft / Root / AntiTheftRootC.nc
index 19bc9430303fb3e94069e21bfd8f1a26fd4a0f04..ada387b5ec1a9ba6750b5d8bf5b3b3c0c67996de 100644 (file)
@@ -1,3 +1,21 @@
+// $Id$
+/*
+ * Copyright (c) 2007 Intel Corporation
+ * All rights reserved.
+ *
+ * This file is distributed under the terms in the attached INTEL-LICENSE     
+ * file. If you do not find these files, copies can be found by writing to
+ * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 
+ * 94704.  Attention:  Intel License Inquiry.
+ */
+/**
+ * Root node code for the antitheft demo app, just acts as a bridge with the PC:
+ * - disseminates settings received from the PC
+ * - acts as a root forthe theft alert collection tree
+ * - forwards theft alerts received from the collection tree to the PC
+ *
+ * @author David Gay
+ */
 module AntiTheftRootC
 {
   uses
@@ -11,6 +29,7 @@ module AntiTheftRootC
     interface Receive as SettingsReceive;
 
     interface StdControl as CollectionControl;
+    interface StdControl as DisseminationControl;
     interface RootControl;
     interface Receive as AlertsReceive;
     interface AMSend as AlertsForward;
@@ -20,6 +39,7 @@ module AntiTheftRootC
 }
 implementation
 {
+  /* Start the radio and serial ports when booting */
   event void Boot.booted()
   {
     call SerialControl.start();
@@ -28,16 +48,23 @@ implementation
 
   event void SerialControl.startDone(error_t error) { }
   event void SerialControl.stopDone(error_t error) { }
-  event void RadioControl.startDone(error_t error) { 
+  event void RadioControl.startDone(error_t error) {
+    /* Once the radio has started, we can setup low-power listening, and
+       start the collection and dissemination services. Additionally, we
+       set ourselves as the (sole) root for the theft alert dissemination
+       tree */
     if (error == SUCCESS)
       {
-       call LowPowerListening.setLocalDutyCycle(200);
+       call LowPowerListening.setLocalWakeupInterval(512);
+       call DisseminationControl.start();
        call CollectionControl.start();
        call RootControl.setRoot();
       }
   }
   event void RadioControl.stopDone(error_t error) { }
 
+  /* When we receive new settings from the serial port, we disseminate
+     them by calling the change command */
   event message_t *SettingsReceive.receive(message_t* msg, void* payload, uint8_t len)
   {
     settings_t *newSettings = payload;
@@ -53,7 +80,10 @@ implementation
   message_t fwdMsg;
   bool fwdBusy;
 
-  event message_t *AlertsReceive.receive(message_t* msg, void* payload, uint8_t len)
+  /* When we (as root of the collection tree) receive a new theft alert,
+     we forward it to the PC via the serial port */
+  event message_t *AlertsReceive.receive(message_t* msg, void* payload, 
+                                        uint8_t len)
   {
     alert_t *newAlert = payload;
 
@@ -61,11 +91,14 @@ implementation
 
     if (len == sizeof(*newAlert) && !fwdBusy)
       {
-       alert_t *fwdAlert = call AlertsForward.getPayload(&fwdMsg);
-
-       *fwdAlert = *newAlert;
-       if (call AlertsForward.send(AM_BROADCAST_ADDR, &fwdMsg, sizeof *fwdAlert) == SUCCESS)
-         fwdBusy = TRUE;
+       /* Copy payload (newAlert) from collection system to our serial
+          message buffer (fwdAlert), then send our serial message */
+       alert_t *fwdAlert = call AlertsForward.getPayload(&fwdMsg, sizeof(alert_t));
+       if (fwdAlert != NULL) {
+         *fwdAlert = *newAlert;
+         if (call AlertsForward.send(AM_BROADCAST_ADDR, &fwdMsg, sizeof *fwdAlert) == SUCCESS)
+           fwdBusy = TRUE;
+       }
       }
     return msg;
   }