What a load of BS
[tpope-extra.git] / perl / mobile-phone-monitor
1 #!/usr/bin/perl
2 # $Id$
3 # -*- perl -*- vim:set ft=perl sw=4 sts=4:
4
5 # Monitors a bluetooth mobile phone for calls.  You'll need to bind rfcomm1 to
6 # your mobile phone's SP service.  You'll also need my Device::Nokia module
7 # (the name is misleading, it's not Nokia specific).
8
9 use strict;
10 use Device::Nokia;
11 my $number;
12 my %status;
13 my $quiton;
14 my $daemonize;
15 my $last=0;
16 my $gsm = new Device::Nokia( port => '/dev/rfcomm1', log => 'file,/dev/null');
17
18 $SIG{INT}  = 'death';
19 $SIG{TERM} = 'death';
20 $SIG{QUIT} = 'death';
21
22 sub initialize {
23     my $gsm=shift;
24     $gsm->connect() || return 0;
25     $gsm->atsend( 'AT+CLIP=1' . Device::Modem::CR );
26     $gsm->answer();
27     return 1;
28 }
29
30 initialize($gsm) || die "Could not initialize modem: $!";
31
32 while($_=shift) {
33     $daemonize=1 if ($_ eq "-d");
34     $quiton=1 if ($_ eq "-b");
35 }
36
37 daemonize() if $daemonize;
38
39 $|=1;
40 while(1) {
41 %status=("is_active"=>1);
42 while($status{"is_active"} ne 0) {
43     for(my $i=0;$i<3;$i++) {
44         sleep 1;
45         $number = wait_for_ring();
46         if($number) {
47             $status{"number"} = $number;
48             output_status(%status);
49             ring($number);
50             last;
51         } elsif(!defined($number)) { $status{"is_active"}=0; last; };
52     }
53     my %newstatus=gather_data($gsm);
54     print $newstatus{'call'}." ".$status{'call'}."\n";
55     ring() if($newstatus{"call"}!=0 && $status{"call"}==0 && !$status{"number"});
56     foreach (keys %newstatus) {
57         $status{$_} = $newstatus{$_} if(defined ($newstatus{$_}));
58     }
59     undef $status{"number"} if (!$number && $status{"callsetup"}==0 && $status{"call"}==0);
60     output_status(%status);
61     if ($status{"source"}==0 && $status{"callsetup"}==0 && $status{"call"}==0 && $quiton) {
62         death() if(time-$last>600);
63     } elsif ($quiton) {
64         $last=time;
65     }
66 }
67 $gsm->disconnect();
68 output_status(is_active => 0);
69 do {sleep (60-30*$quiton);}
70 until(initialize($gsm));
71 }
72
73 sub wait_for_ring {
74     local $_;
75     eval {
76         local $SIG{ALRM} = sub { die "alarm\n" };
77         alarm 5;
78         $_ = $gsm->answer("\\+CLIP: \"[0-9]+\",[0-9]+\$", 30);
79         alarm 0
80     };
81     if($@) {
82         die unless $@ eq "alarm\n";
83         return undef;
84     } else {
85         m/CLIP: "([0-9]+)",/;
86         return "$1";
87     }
88 }
89
90 sub ring {
91     system($ENV{"HOME"} . "/bin/phone-call " . $_[0]);
92 }
93
94 sub gather_data {
95   my %data;
96   my $gsm=shift;
97   eval {
98   local $SIG{ALRM} = sub { die "alarm\n" };
99   alarm 15;
100   %data=$gsm->indicators();
101   my ($first, $second) = $gsm->battery_strength();
102   $data{"source"}=$first;
103   #$data{"battery"}=$second;
104   ($first, $second) = $gsm->signal_quality();
105   $data{"signal"}=$first;
106   alarm 0;
107   };
108     if($@) {
109         die unless $@ eq "alarm\n";
110         return (is_active => 0);
111     } else {
112         $data{"is_active"}=$gsm->is_active() && $data{"signal"} && 1 || 0;
113         return %data;
114     }
115 }
116
117 sub output_status {
118     my %status = @_;
119     open STATUS, ">/tmp/.phone-status.new" || die $!;
120     foreach my $indicator (sort keys %status) {
121         print STATUS "$indicator=" . $status{$indicator} . "\n";
122     };
123     close STATUS;
124     rename "/tmp/.phone-status.new", "/tmp/.phone-status" || die $!;
125 }
126
127 sub death {
128     $gsm->disconnect() if($gsm);
129     unlink "/tmp/.phone-status.new", "/tmp/.phone-status";
130     exit(0);
131 }