[Tinyos-2-commits] CVS: tinyos-2.x/apps/AntiTheft/Nodes
AntiTheftAppC.nc, 1.1, 1.2 AntiTheftC.nc, 1.1, 1.2 antitheft.h,
1.1, 1.2
David Gay
idgay at users.sourceforge.net
Mon Apr 2 13:38:08 PDT 2007
Update of /cvsroot/tinyos/tinyos-2.x/apps/AntiTheft/Nodes
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv22995/Nodes
Modified Files:
AntiTheftAppC.nc AntiTheftC.nc antitheft.h
Log Message:
comments, etc
Index: AntiTheftAppC.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/apps/AntiTheft/Nodes/AntiTheftAppC.nc,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** AntiTheftAppC.nc 30 Mar 2007 16:59:23 -0000 1.1
--- AntiTheftAppC.nc 2 Apr 2007 20:38:05 -0000 1.2
***************
*** 1,2 ****
--- 1,19 ----
+ // $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.
+ */
+ /**
+ * Top-level configuration for node code for the AntiTheft demo app.
+ * Instantiates the sensors, dissemination and collection services, and
+ * does all the necessary wiring.
+ *
+ * @author David Gay
+ */
#include "antitheft.h"
***************
*** 4,26 ****
implementation
{
! components AntiTheftC, new TimerMilliC() as MyTimer, MainC, LedsC,
! new PhotoC(), new AccelXStreamC(), SounderC,
! ActiveMessageC, CollectionC, CC1000CsmaRadioC,
! new DisseminatorC(settings_t, DIS_SETTINGS),
! new CollectionSenderC(COL_ALERTS) as AlertSender,
! new AMSenderC(AM_THEFT) as SendTheft,
! new AMReceiverC(AM_THEFT) as ReceiveTheft;
AntiTheftC.Boot -> MainC.Boot;
AntiTheftC.Check -> MyTimer;
AntiTheftC.Read -> PhotoC;
AntiTheftC.ReadStream -> AccelXStreamC;
- AntiTheftC.Leds -> LedsC;
AntiTheftC.Mts300Sounder -> SounderC;
AntiTheftC.SettingsValue -> DisseminatorC;
AntiTheftC.AlertRoot -> AlertSender;
AntiTheftC.CollectionControl -> CollectionC;
! AntiTheftC.RadioControl -> ActiveMessageC;
! AntiTheftC.LowPowerListening -> CC1000CsmaRadioC;
AntiTheftC.TheftSend -> SendTheft;
AntiTheftC.TheftReceive -> ReceiveTheft;
--- 21,66 ----
implementation
{
! /* First wire the low-level services (booting, serial port, radio).
! There is no standard name for the actual radio component, so we use
! #ifdef to get the right one for the current platform. */
! components AntiTheftC, ActiveMessageC, MainC, LedsC,
! new TimerMilliC() as MyTimer;
! #if defined(PLATFORM_MICA2)
! components CC1000CsmaRadioC as Radio;
! #elif defined(PLATFORM_MICAZ)
! components CC2420ActiveMessageC as Radio;
! #else
! #error "The AntiTheft application is only supported for mica2 and micaz nodes"
! #endif
AntiTheftC.Boot -> MainC.Boot;
AntiTheftC.Check -> MyTimer;
+ AntiTheftC.Leds -> LedsC;
+ AntiTheftC.RadioControl -> ActiveMessageC;
+ AntiTheftC.LowPowerListening -> Radio;
+
+ /* Instaniate, wire MTS300 sensor board components. */
+ components new PhotoC(), new AccelXStreamC(), SounderC;
+
AntiTheftC.Read -> PhotoC;
AntiTheftC.ReadStream -> AccelXStreamC;
AntiTheftC.Mts300Sounder -> SounderC;
+
+ /* Instantiate and wire our settings dissemination service */
+ components new DisseminatorC(settings_t, DIS_SETTINGS),
+
AntiTheftC.SettingsValue -> DisseminatorC;
+
+ /* Instantiate and wire our collection service for theft alerts */
+ components CollectionC, new CollectionSenderC(COL_ALERTS) as AlertSender;
+
AntiTheftC.AlertRoot -> AlertSender;
AntiTheftC.CollectionControl -> CollectionC;
!
! /* Instantiate and wire our local radio-broadcast theft alert and
! reception services */
! components new AMSenderC(AM_THEFT) as SendTheft,
! new AMReceiverC(AM_THEFT) as ReceiveTheft;
!
AntiTheftC.TheftSend -> SendTheft;
AntiTheftC.TheftReceive -> ReceiveTheft;
Index: AntiTheftC.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/apps/AntiTheft/Nodes/AntiTheftC.nc,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** AntiTheftC.nc 30 Mar 2007 16:59:23 -0000 1.1
--- AntiTheftC.nc 2 Apr 2007 20:38:05 -0000 1.2
***************
*** 1,2 ****
--- 1,17 ----
+ // $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.
+ */
+ /**
+ * Main code for the anti theft demo application.
+ *
+ * @author David Gay
+ */
module AntiTheftC
{
***************
*** 20,33 ****
{
enum {
! DARK_THRESHOLD = 200,
WARNING_TIME = 3,
! ACCEL_SAMPLES = 10
};
settings_t settings;
message_t alertMsg, theftMsg;
! uint16_t ledTime;
uint16_t accelSamples[ACCEL_SAMPLES];
void errorLed() {
ledTime = WARNING_TIME;
--- 35,59 ----
{
enum {
! /* Threshold for considering mote in a dark place */
! DARK_THRESHOLD = 200,
!
! /* Amount of time warning leds should stay on (in checkInterval counts) */
WARNING_TIME = 3,
!
! /* Number of acceleration samples to collect */
! ACCEL_SAMPLES = 10,
!
! /* Interval between acceleration samples (us) */
! ACCEL_INTERVAL = 10000
};
settings_t settings;
message_t alertMsg, theftMsg;
! uint16_t ledTime; /* Time left until leds switched off */
uint16_t accelSamples[ACCEL_SAMPLES];
+ /********* LED handling **********/
+
+ /* Warn that some error occurred */
void errorLed() {
ledTime = WARNING_TIME;
***************
*** 35,38 ****
--- 61,65 ----
}
+ /* Notify user that settings changed */
void settingsLed() {
ledTime = WARNING_TIME;
***************
*** 40,43 ****
--- 67,71 ----
}
+ /* Turn on bright red light! (LED) */
void theftLed() {
ledTime = WARNING_TIME;
***************
*** 45,48 ****
--- 73,77 ----
}
+ /* Time-out leds. Called every checkInterval */
void updateLeds() {
if (ledTime && !--ledTime)
***************
*** 54,57 ****
--- 83,87 ----
}
+ /* Check result code and report error if a problem occurred */
void check(error_t ok) {
if (ok != SUCCESS)
***************
*** 59,62 ****
--- 89,93 ----
}
+ /* Report theft, based on current settings */
void theft() {
if (settings.alert & ALERT_LEDS)
***************
*** 65,89 ****
call Mts300Sounder.beep(100);
if (settings.alert & ALERT_RADIO)
check(call TheftSend.send(AM_BROADCAST_ADDR, &theftMsg, 0));
if (settings.alert & ALERT_ROOT)
{
alert_t *newAlert = call AlertRoot.getPayload(&alertMsg);
newAlert->stolenId = TOS_NODE_ID;
check(call AlertRoot.send(&alertMsg, sizeof *newAlert));
}
}
event void AlertRoot.sendDone(message_t *msg, error_t ok) { }
event void TheftSend.sendDone(message_t *msg, error_t ok) { }
event message_t *TheftReceive.receive(message_t* msg, void* payload, uint8_t len) {
theftLed();
return msg;
}
! void resetTimer() {
! call Check.startPeriodic(settings.checkInterval);
! }
!
event void Boot.booted() {
errorLed();
--- 96,128 ----
call Mts300Sounder.beep(100);
if (settings.alert & ALERT_RADIO)
+ /* A local broadcast with no payload */
check(call TheftSend.send(AM_BROADCAST_ADDR, &theftMsg, 0));
if (settings.alert & ALERT_ROOT)
{
+ /* Report the identity of this node, using the collection protocol */
+
+ /* Get the payload part of alertMsg and fill in our data */
alert_t *newAlert = call AlertRoot.getPayload(&alertMsg);
newAlert->stolenId = TOS_NODE_ID;
+
+ /* and send it... */
check(call AlertRoot.send(&alertMsg, sizeof *newAlert));
}
}
+ /* We have nothing to do after messages are sent */
event void AlertRoot.sendDone(message_t *msg, error_t ok) { }
event void TheftSend.sendDone(message_t *msg, error_t ok) { }
+ /* We've received a theft alert from a neighbour. Turn on the theft warning
+ light! */
event message_t *TheftReceive.receive(message_t* msg, void* payload, uint8_t len) {
theftLed();
+ /* We don't need to hold on to the message buffer, so just return the
+ received buffer */
return msg;
}
! /* At boot time, start the periodic timer and the radio */
event void Boot.booted() {
errorLed();
***************
*** 95,98 ****
--- 134,139 ----
}
+ /* Radio started. Now start the collection protocol and set the
+ radio to a 2% low-power-listening duty cycle */
event void RadioControl.startDone(error_t ok) {
if (ok == SUCCESS)
***************
*** 107,110 ****
--- 148,152 ----
event void RadioControl.stopDone(error_t ok) { }
+ /* New settings received, update our local copy */
event void SettingsValue.changed() {
const settings_t *newSettings = call SettingsValue.get();
***************
*** 112,135 ****
settingsLed();
settings = *newSettings;
call Check.startPeriodic(newSettings->checkInterval);
}
event void Check.fired() {
updateLeds();
if (settings.detect & DETECT_DARK)
! call Read.read();
if (settings.detect & DETECT_ACCEL)
{
call ReadStream.postBuffer(accelSamples, ACCEL_SAMPLES);
! call ReadStream.read(10000);
}
}
event void Read.readDone(error_t ok, uint16_t val) {
if (ok == SUCCESS && val < DARK_THRESHOLD)
! theft();
}
task void checkAcceleration() {
uint8_t i;
--- 154,185 ----
settingsLed();
settings = *newSettings;
+ /* Switch to the new check interval */
call Check.startPeriodic(newSettings->checkInterval);
}
+ /* Every check interval: update leds, check for theft based on current
+ settings */
event void Check.fired() {
updateLeds();
if (settings.detect & DETECT_DARK)
! call Read.read(); /* Initiate light sensor read */
if (settings.detect & DETECT_ACCEL)
{
+ /* To sample acceleration, we first register our buffer
+ (postBuffer). Then we trigger sampling at the desired
+ interval (read) */
call ReadStream.postBuffer(accelSamples, ACCEL_SAMPLES);
! call ReadStream.read(ACCEL_INTERVAL);
}
}
+ /* Light sample completed. Check if it indicates theft */
event void Read.readDone(error_t ok, uint16_t val) {
if (ok == SUCCESS && val < DARK_THRESHOLD)
! theft(); /* ALERT! ALERT! */
}
+ /* A deferred task to check the acceleration data and detect theft. */
task void checkAcceleration() {
uint8_t i;
***************
*** 137,140 ****
--- 187,193 ----
uint32_t var;
+ /* We check for theft by checking whether the variance of the sample
+ (in mysterious acceleration units) is > 4 */
+
for (avg = 0, i = 0; i < ACCEL_SAMPLES; i++)
avg += accelSamples[i];
***************
*** 148,154 ****
if (var > 4 * ACCEL_SAMPLES)
! theft();
}
event void ReadStream.readDone(error_t ok, uint32_t usActualPeriod) {
if (ok == SUCCESS)
--- 201,210 ----
if (var > 4 * ACCEL_SAMPLES)
! theft(); /* ALERT! ALERT! */
}
+ /* The acceleration read completed. Post the task that will check for
+ theft. We defer this somewhat cpu-intensive computation to avoid
+ having the current task run for too long. */
event void ReadStream.readDone(error_t ok, uint32_t usActualPeriod) {
if (ok == SUCCESS)
***************
*** 158,161 ****
--- 214,219 ----
}
+ /* The current sampling buffer is full. If we were using several buffers,
+ we would switch between them here. */
event void ReadStream.bufferDone(error_t ok, uint16_t *buf, uint16_t count) { }
}
Index: antitheft.h
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/apps/AntiTheft/Nodes/antitheft.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** antitheft.h 30 Mar 2007 16:59:23 -0000 1.1
--- antitheft.h 2 Apr 2007 20:38:05 -0000 1.2
***************
*** 1,2 ****
--- 1,16 ----
+ // $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.
+ */
+ /**
+ *
+ * @author David Gay
+ */
#ifndef ANTITHEFT_H
#define ANTITHEFT_H
More information about the Tinyos-2-commits
mailing list