+// $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"
configuration AntiTheftAppC { }
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;
+ /* 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.Leds -> LedsC;
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;
- AntiTheftC.RadioControl -> ActiveMessageC;
- AntiTheftC.LowPowerListening -> CC1000CsmaRadioC;
+
+ /* 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;
}
+// $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
{
uses {
implementation
{
enum {
- DARK_THRESHOLD = 200,
+ /* 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,
- ACCEL_SAMPLES = 10
+
+ /* 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;
+ 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;
call Leds.led2On();
}
+ /* Notify user that settings changed */
void settingsLed() {
ledTime = WARNING_TIME;
call Leds.led1On();
}
+ /* Turn on bright red light! (LED) */
void theftLed() {
ledTime = WARNING_TIME;
call Leds.led0On();
}
+ /* Time-out leds. Called every checkInterval */
void updateLeds() {
if (ledTime && !--ledTime)
{
}
}
+ /* Check result code and report error if a problem occurred */
void check(error_t ok) {
if (ok != SUCCESS)
errorLed();
}
+ /* Report theft, based on current settings */
void theft() {
if (settings.alert & ALERT_LEDS)
theftLed();
if (settings.alert & ALERT_SOUND)
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;
}
- void resetTimer() {
- call Check.startPeriodic(settings.checkInterval);
- }
-
+ /* At boot time, start the periodic timer and the radio */
event void Boot.booted() {
errorLed();
settings.alert = DEFAULT_ALERT;
call RadioControl.start();
}
+ /* 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)
{
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();
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();
+ 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(10000);
+ 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();
+ theft(); /* ALERT! ALERT! */
}
+ /* A deferred task to check the acceleration data and detect theft. */
task void checkAcceleration() {
uint8_t i;
uint16_t avg;
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];
avg /= ACCEL_SAMPLES;
}
if (var > 4 * ACCEL_SAMPLES)
- theft();
+ 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)
post checkAcceleration();
errorLed();
}
+ /* 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) { }
}
+// $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
--- /dev/null
+README for AntiTheft
+Author/Contact: tinyos-help@millennium.berkeley.edu
+
+Description:
+
+AntiTheft is a demo "antitheft" application. The accompanying
+tutorial-slides.ppt Powerpoint file is a tutorial for TinyOS that uses
+AntiTheft to introduce various aspects of TinyOS and its services.
+
+AntiTheft can be remotely configured to detect theft by:
+- light level (a dark mote is a mote that has been stolen and placed in
+ a concealed dark place, e.g., a pocket!)
+- acceleration (you have to move a mote to steal it...)
+
+It reports theft by:
+- turning on an alert light (a red LED)
+- beeping a sounder
+- reporting the theft to nodes within broadcast radio range (nodes
+ receiving this message turn on their red LED)
+- reporting the theft to a central node via multihop (collection) routing
+
+The antitheft detection and reporting choices are remotely controllable
+using the java GUI found in the java subdirectory.
+
+Nodes blink their yellow LED when turned on or when an internal error
+occurs, and blink their green LED when new settings are received.
+
+This demo is written for mica2 or micaz motes using the mts300 sensor
+board.
+
+The code in the Nodes directory should be installed on the motes
+detecting theft. Each mote should have a separate id, and a mts300
+sensor board. The code in the Root directory should be installed on a
+mote connected to the PC using a programming board. It talks to the java
+GUI, forwarding settings from the PC to the sensor network, and
+forwarding theft alerts from the sensor network to the PC.
+
+Tools:
+
+The java directory contains a control GUI for the antitheft demo app.
+
+Known bugs/limitations:
+
+None.
+
+
+$Id$
-#include "../AntiTheft/antitheft.h"
+// $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 root-node code for the AntiTheft demo app.
+ * Instantiates the dissemination and collection services, and does all
+ * the necessary wiring.
+ *
+ * @author David Gay
+ */
+#include "../Nodes/antitheft.h"
configuration AntiTheftRootAppC { }
implementation
{
- components AntiTheftRootC, MainC, LedsC, CollectionC,
- new DisseminatorC(settings_t, DIS_SETTINGS),
- ActiveMessageC, SerialActiveMessageC, CC1000CsmaRadioC,
- new SerialAMReceiverC(AM_SETTINGS) as SettingsReceiver,
- new SerialAMSenderC(AM_ALERTS) as AlertsForwarder;
+ /* 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 AntiTheftRootC, MainC, LedsC, ActiveMessageC, SerialActiveMessageC;
+#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
AntiTheftRootC.Boot -> MainC;
AntiTheftRootC.SerialControl -> SerialActiveMessageC;
AntiTheftRootC.RadioControl -> ActiveMessageC;
- AntiTheftRootC.LowPowerListening -> CC1000CsmaRadioC;
- AntiTheftRootC.CollectionControl -> CollectionC;
+ AntiTheftRootC.LowPowerListening -> Radio;
+ AntiTheftRootC.Leds -> LedsC;
+
+ /* Next, instantiate and wire a disseminator (to send settings) and a
+ serial receiver (to receive settings from the PC) */
+ components new DisseminatorC(settings_t, DIS_SETTINGS),
+ new SerialAMReceiverC(AM_SETTINGS) as SettingsReceiver;
AntiTheftRootC.SettingsReceive -> SettingsReceiver;
AntiTheftRootC.SettingsUpdate -> DisseminatorC;
+ /* Finally, instantiate and wire a collector (to receive theft alerts) and
+ a serial sender (to send the alerts to the PC) */
+ components CollectionC, new SerialAMSenderC(AM_ALERTS) as AlertsForwarder;
+
+ AntiTheftRootC.CollectionControl -> CollectionC;
AntiTheftRootC.RootControl -> CollectionC;
AntiTheftRootC.AlertsReceive -> CollectionC.Receive[COL_ALERTS];
AntiTheftRootC.AlertsForward -> AlertsForwarder;
- AntiTheftRootC.Leds -> LedsC;
}
+// $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
}
implementation
{
+ /* Start the radio and serial ports when booting */
event void Boot.booted()
{
call SerialControl.start();
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);
}
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;
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;
if (len == sizeof(*newAlert) && !fwdBusy)
{
+ /* Copy payload (newAlert) from collection system to our serial
+ message buffer (fwdAlert), then send our serial message */
alert_t *fwdAlert = call AlertsForward.getPayload(&fwdMsg);
*fwdAlert = *newAlert;