Difference between revisions of "Script for theft alarm using HDAPS"

From ThinkWiki
Jump to: navigation, search
(The script: Moved to Code/)
(The script)
Line 41: Line 41:
  
 
===The script===
 
===The script===
<pre>
 
#!/usr/bin/perl
 
# tp-theft v0.1 (http://thinkwiki.org/wiki/Script_for_theft_alarm_using_HDAPS)
 
# This script uses the HDAPS accelerometer found on recent ThinkPad models
 
# to emit an audio alarm when the laptop is tilted. In sufficiently
 
# populated environments, it can be used as a laptop theft deterrent.
 
#
 
# This file is placed in the public domain and may be freely distributed.
 
  
use strict;
+
{{CodeRef|tp-theft-basic}}
use warnings;
 
 
 
##############################
 
# Siren volume and content
 
 
 
# Audio volume (0..100)
 
my $volume = 70;
 
 
 
# Synthesize a siren for 1.0 seconds:
 
my $play_cmd = "sox -t nul /dev/null -t ossdsp /dev/dsp synth 1.0 sine 2000-4000 sine 4000-2000";
 
 
 
# Play a file:
 
# my $play_cmd = "play keep_your_hands_off_me.wav";
 
 
 
##############################
 
# Other tweakables
 
 
 
my $thresh = 0.20;  # tilt threshold (increase value to decrease sensitivity)
 
my $interval = 0.1;  # sampling interval in seconds
 
my $depth = 10;      # number of recent samples to analyze
 
my $pos_file='/sys/devices/platform/hdaps/position';
 
my $verbose = 1;
 
 
 
##############################
 
# Code
 
 
 
sub get_pos {
 
    open(POS,"<",$pos_file) or die "Can't open HDAPS file $pos_file: $!\n";
 
    $_=<POS>;
 
    m/^\((-?\d+),(-?\d+)\)$/ or die "Can't parse $pos_file content\n";
 
    return ($1,$2);
 
}
 
 
 
sub stddev {
 
    my $sum=0;
 
    my $sumsq=0;
 
    my $n=$#_+1;
 
    for my $v (@_) {
 
$sum += $v;
 
$sumsq += $v*$v;
 
    }
 
    return sqrt($n*$sumsq - $sum*$sum)/($n*($n-1));
 
}
 
 
 
my (@XHIST, @YHIST);
 
my ($x,$y) = get_pos;
 
for (1..$depth) {
 
    push(@XHIST,$x);
 
    push(@YHIST,$y);
 
}
 
my $alarm_file; # flags ongoing alarm (and stores saved mixer settings)
 
 
 
while (1) {
 
    my ($x,$y) = get_pos;
 
    shift(@XHIST); push(@XHIST,$x);
 
    shift(@YHIST); push(@YHIST,$y);
 
    my $xdev = stddev(@XHIST);
 
    my $ydev = stddev(@YHIST);
 
 
 
    # Print variance and history
 
    print "X: v=$xdev (".join(',',@XHIST).")  Y: v=$ydev (".join(",",@YHIST).")\n" if $verbose>1;
 
 
 
    my $tilted = $xdev>$thresh || $ydev>$thresh;
 
 
 
    if ($tilted && !(defined($alarm_file) && -f $alarm_file)) {
 
print "ALARM\n" if $verbose>0;
 
$alarm_file = `mktemp /tmp/hdaps-tilt.XXXXXXXX` or die "mktemp: $?";
 
chomp($alarm_file);
 
system('/bin/bash', '-c', <<"EOF")==0 or die "Failed: $?";
 
( trap \"aumix -L -f $alarm_file > /dev/null; rm -f $alarm_file" EXIT HUP QUIT TERM
 
  aumix -S -f $alarm_file &&
 
  aumix -v $volume -w 100 &&
 
  $play_cmd) &
 
EOF
 
    }
 
 
 
    select(undef, undef, undef, $interval); # sleep
 
}
 
</pre>
 
 
 
The [[User:Thinker|author]] of the script disclaims all warranty for this script, and releases it to the public domain.
 
  
 
==Ideas for improvement==
 
==Ideas for improvement==

Revision as of 00:38, 27 July 2006

General

Recent ThinkPad models include a built-in two-axis accelerometer, as part of the HDAPS feature. This accelerometer can be put to another use: as a laptop theft deterrent. The following script detects when the laptop is moved and emits a loud audio alarm. Against a casual laptop-snatcher in a populated environment (e.g., typical office space) this can be an effective deterrent.

Note that the alarm cannot work when the laptop is suspended or powered off. You can buy external (hardware) motion detector alarms for those cases.

ATTENTION!
The audio alarm is played at a very high volume. Never enable the alarm while wearing headphones connected to the laptop's speaker output or when the laptop is connected to a high-power amplifier.

A comprehensive script

This Perl script periodically samples the tilt data reported by the accelerometer, computes the variance over recent samples, and triggers the alarm when the variance exceeds a given threshold.

On a ThinkPad with Active Protection System running a modern Linux installation with the hdaps kernel module loaded, the script should work as is. Just run # tp-theft --arm and see (or rather, hear) what happens when you tilt your laptop.

The volume and alarm sound can be adjusted at the top of the script. On a ThinkPad T43, the synthetic siren at $alarm_volume=100 (up from the default 70) is quite ear-splitting, and combined with $acpi_volume=15 it is dangerously loud.

The script is designed to run continuously in the background, so by default the alarm will be activated only when the KDE screen saver is locked. If you you open the laptop lid (or press the lid button) shortly before or after the beginning of movement, the alarm will be suspended (except for a brief warning) and you will get a few seconds of grace to unlock the screen saver (preferably, using the integrated fingerprint reader!). You can disable this functionality by passing the --arm parameter, by setting $use_kde=0 and $use_lid=0, or by using the simpler script below.

Prerequisites

  • ThinkPad with Active Protection System
  • hdaps kernel module loaded (included in kernel 2.6.14 and later)
  • Optional: ibm_acpi module loaded with the experimental=1 parameter (included in kernel 2.6.14 and later; needed only for full volume control)

The following are included in all modern Linux distributions:

  • ALSA sound system, alsactl, aplay, amixer )
  • sox (SOund eXchange) sound utility

The script

tp-theft (download)

A basic script

This is a simpler version of the above script, which omits the fancier functionality such as KDE screensaver detection, lid detection and state machine.

Prerequisites

  • ThinkPad with Active Protection System
  • hdaps kernel module loaded (included in kernel 2.6.14 and later)
  • aumix mixer control utility (included in all modern Linux distributions)
  • sox (SOund eXchange) sound utility (included in all modern Linux distributions)

The script

tp-theft-basic (download)

Ideas for improvement

Features awaiting contribution:

  • Disable the alarm when headphones are plugged in -- it may cause hearing damage (if the user ignores the initial warning), and won't be effective anyway. Can we detect whether the something is plugged into the headphones/line-out socket?
  • Start out quietly, and increase siren duration and volume if movement persists. Reset after a period of no movement.
  • Gnome and xscreensaver support (similarly to lightwatch.pl?)
  • Report theft via network (if you get a chance to).
  • Monitor AC power and take it into account for alarm activation -- thieves seldom carry a UPS.
  • Monitor proximity to a bluetooth device carried by the owner, and take it into account for alarm activation. I'll implement this if you get me a BMDC-3 Bluetooth card.
  • Don't arm the alarm if movement of similar magnitude was happening also before the screenwaver was auto-locked (the owner might be in a moving vehicle, etc.).
  • Theft attempts may be accompanied by rough handling, especially when the siren kicks in. So when starting an alarm also park the disk heads. Release the parking when a key is pressed (according /sys/bus/platform/drivers/hdaps/hdaps/keyboard_activity) so that the login prompt can start up. This requires kernel support for disk head parking and queue freezing, currently developed for the (original) HDAPS functionality.
  • When the alarm is triggered, also show a visual warning on the display. Override screensaver/powersaving if necessary.
  • Disarm the alarm (or hold it off for a few seconds, as already implemented for lid open) based on voice/sound recognition using the built-in microphone.
  • Implement this functionality in the embedded controller, so that the alarm will work even when the laptop is suspended. It may be possible to do so without IBM/Lenovo's involvement, using the embedded controller disassembly.
  • Disable the volume buttons when the script is running so that a thief can't just turn the volume down. (Not an issue when ibm_acpi volume control is available - see Prerequisites.)


Another Script (plugin-based)

there's another script with the same intention available at http://www.informatik.hu-berlin.de/~pilop/HOWTO_Gentoo_T43/#TheftAlarm

it uses a plugin-architecture for different checks (HDAPS, ethernet, power, lid, ...)

Yet another script (python/gtk based)

You can find yet another version of this script at

http://r3blog.nl/index.php/thinkpad-theft

It has almost the same features as the comprehensive script above, with a few improvements. It uses dbus to query the screensaver status and gconf for storing configuration value. To improve the delay before the alarm sounds, it has a built-in wav player, and it opens the file-descriptor of the wav at startup time (thereby removing the need to spawn an application to play the alarm; imagine someone stealing your laptop while you're doing heavy disk io). Furthermore, it has a trayicon allowing you to manipulate most settings stored in gconf aswell as showing you the current status of the alarm. The 0.2 release features activation on missing presence of a bluetooth or usb device.