3 # -*- perl -*- vim: ft=perl sw=4 sts=4
5 # Monitors a bluetooth mobile phome 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).
16 my $gsm = new Device::Nokia( port => '/dev/rfcomm1', log => 'file,/dev/null');
24 $gsm->connect() || return 0;
25 $gsm->atsend( 'AT+CLIP=1' . Device::Modem::CR );
30 initialize($gsm) || die "Could not initialize modem: $!";
33 $daemonize=1 if ($_ eq "-d");
34 $quiton=1 if ($_ eq "-b");
37 daemonize() if $daemonize;
40 %status=("is_active"=>1);
41 while($status{"is_active"} ne 0) {
42 for(my $i=0;$i<8;$i++) {
44 $number = wait_for_ring();
46 $status{"number"} = $number;
47 output_status(%status);
50 } elsif(!defined($number)) { $status{"is_active"}=0; last; };
52 my %newstatus=gather_data($gsm);
53 ring() if($newstatus{"call"}!=0 && $status{"call"}!=0);
54 foreach (keys %newstatus) {
55 $status{$_} = $newstatus{$_} if(defined ($newstatus{$_}));
57 undef $status{"number"} if (!$number && $status{"callsetup"}==0 && $status{"call"}==0);
58 output_status(%status);
59 death() if ($status{"source"}==0 && $status{"callsetup"}==0 && $status{"call"}==0 && $quiton);
62 #print "Chillin'...\n";
63 output_status(is_active => 0);
65 until(initialize($gsm));
71 local $SIG{ALRM} = sub { die "alarm\n" };
73 $_ = $gsm->answer("\\+CLIP: \"[0-9]+\",[0-9]+\$", 30);
77 die unless $@ eq "alarm\n";
86 system($ENV{"HOME"} . "/bin/phone-call " . $_[0]);
93 local $SIG{ALRM} = sub { die "alarm\n" };
95 %data=$gsm->indicators();
96 my ($first, $second) = $gsm->battery_strength();
97 $data{"source"}=$first;
98 $data{"battery"}=$second;
99 ($first, $second) = $gsm->signal_quality();
100 $data{"signal"}=$first;
104 die unless $@ eq "alarm\n";
105 return (is_active => 0);
107 $data{"is_active"}=$gsm->is_active() && $data{"signal"} && 1 || 0;
114 open STATUS, ">/tmp/.phone-status.new" || die $!;
115 foreach my $indicator (sort keys %status) {
116 print STATUS "$indicator=" . $status{$indicator} . "\n";
119 rename "/tmp/.phone-status.new", "/tmp/.phone-status" || die $!;
139 $gsm->disconnect() if($gsm);
140 unlink "/tmp/.phone-status.new", "/tmp/.phone-status";