3 # -*- perl -*- vim:set ft=perl sw=4 sts=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).
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;
41 %status=("is_active"=>1);
42 while($status{"is_active"} ne 0) {
43 for(my $i=0;$i<3;$i++) {
45 $number = wait_for_ring();
47 $status{"number"} = $number;
48 output_status(%status);
51 } elsif(!defined($number)) { $status{"is_active"}=0; last; };
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{$_}));
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);
68 output_status(is_active => 0);
69 do {sleep (60-30*$quiton);}
70 until(initialize($gsm));
76 local $SIG{ALRM} = sub { die "alarm\n" };
78 $_ = $gsm->answer("\\+CLIP: \"[0-9]+\",[0-9]+\$", 30);
82 die unless $@ eq "alarm\n";
91 system($ENV{"HOME"} . "/bin/phone-call " . $_[0]);
98 local $SIG{ALRM} = sub { die "alarm\n" };
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;
109 die unless $@ eq "alarm\n";
110 return (is_active => 0);
112 $data{"is_active"}=$gsm->is_active() && $data{"signal"} && 1 || 0;
119 open STATUS, ">/tmp/.phone-status.new" || die $!;
120 foreach my $indicator (sort keys %status) {
121 print STATUS "$indicator=" . $status{$indicator} . "\n";
124 rename "/tmp/.phone-status.new", "/tmp/.phone-status" || die $!;
128 $gsm->disconnect() if($gsm);
129 unlink "/tmp/.phone-status.new", "/tmp/.phone-status";