#!/usr/bin/perl -w use DBI; use Device::SerialPort; use Time::Piece; use Proc::Daemon; #use re 'debug'; my $ato = 60*60*12; # 12 hours in seconds my $po; sub qit { local($str) = @_; for ($i = 0; $i < length($str); $i++) { $c = ord(substr($str, $i, 1)); if ($c > 0x1F && $c < 0x7F) { print LOG substr($str, $i, 1); } else { printf LOG "[%02x]",$c; } } print LOG "\n"; } sub sendit { local($tosend, $waitfor) = @_; my $gotit = 0; my $cnt; my $inp; print LOG length($tosend)."->"; qit($tosend); while (!$gotit) { $po->write($tosend); print LOG "."; ($cnt, $inp) = $po->read(255); if ($cnt > 0) { if (length($waitfor) > 0) { $gotit = 1 if ($inp =~ m/$waitfor/); } else { $gotit = 1; } print LOG length($inp)."<-"; qit($inp); } } } sub doit { my $t; my $ampm; my $hr; my $cksum; $po->dtr_active(0); sleep(1); $po->dtr_active(1); $po->rts_active(0); sleep(1); $po->rts_active(1); &sendit("\r\n", "Password"); &sendit("1234\r\n",""); &sendit("ECS OF\r\n", ""); &sendit("PRG\r\n", ""); $t = localtime; if ($t->hour <= 11) { $ampm = 0; if ($t->hour == 0) { $hr = 12; } else { $hr = $t->hour; } } else { $ampm = 1; if ($t->hour == 12) { $hr = $t->hour; } else { $hr = $t->hour - 12; } } $cksum = 0xA + 2 + $t->yy + $t->mon + $t->mday + $t->_wday + $hr + $t->min + $t->sec + $ampm; &sendit(pack("C11", 0xA, 2, $t->yy, $t->mon, $t->mday, $t->_wday, $hr, $t->min, $t->sec, $ampm, $cksum), ""); #&sendit(pack("C15", 0xE, 0, 0x80, 0, 0, 0, 0, 0, 0xFF, 0xE1, 0x7E, 0x81, 0xFF, 0x41, 0xAD), ""); &sendit(pack("C3", 2, 0xB, 0xD), ""); &sendit("BYE\r\n", ""); $po->dtr_active(0); sleep(1); $po->dtr_active(1); $po->rts_active(0); sleep(1); $po->rts_active(1); alarm($ato); } if (@ARGV != 1) { print STDERR "usage: pbx file|dev\n"; exit(1); } Proc::Daemon::Init; $SIG{ALRM} = \&doit; alarm($ato); open (LOG, ">/var/log/pbx.log"); LOG->autoflush(1); $po = new Device::SerialPort($ARGV[0],0, 0) || die "cannot open $ARGV[0]\n"; $po->databits(8); $po->baudrate(9600); $po->stopbits(1); $po->dtr_active(0); sleep(1); $po->dtr_active(1); $po->rts_active(0); sleep(1); $po->rts_active(1); $po->read_char_time(0); $po->read_const_time(1000); REREAD: for (;;) { my $cnt; my $rbuf; my $buf; my $inp; ($cnt, $rbuf) = $po->read(255); next if ($cnt == 0); # print LOG length($rbuf)."<-"; # qit($rbuf); $buf = $buf . $rbuf; while (length($buf) > 0) { $cnt = index($buf, "\r\n", 0); if ($cnt < 0) { next REREAD; # wait for some more } $inp = substr($buf, 0, $cnt); $buf = substr($buf, $cnt+2); next if (length($inp) == 0); # print LOG length($inp)."<-"; # qit($inp); if ($inp =~ m/^(..\/..\/..) (..:...M) (.{3}) (..) (.{13}) (.{10}) (.{4}) (.{8}) (.{10}) (..) $/) { my ($dt,$tm,$ext,$co,$cid,$nm,$rtm,$dur,$acc,$cd) = ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10); print LOG "$dt $tm "; my ($mn,$dy,$yr) = $dt =~ m/(..)\/(..)\/(..)/; my ($hr,$mi) = $tm =~ m/(..):(..).M/; $yr = (localtime(time))[5] + 1900; $dbh = DBI->connect("DBI:mysql:database=pbx", "asteriskuser", "amp109", {'RaiseError' => 1}); if ($tm =~ m/(\d\d):\d\dPM/ && ($1 != 12)) { $hr += 12; } elsif ($tm =~ m/12:\d\dAM/) { $hr = 0; } $dttm = "$yr-$mn-$dy $hr:$mi:00"; if ($cid =~ m/^([\d\#\*]*)\s*$/) { $cid = $1; $add = ""; if ($cd eq "RC") { print LOG ("Ringing "); } elsif ($cd eq "AN") { $rtm =~ s/(.)\'/00:0$1:/; print LOG "Answered "; $add = " $rtm by $ext "; } elsif ($cd eq "NA") { $rtm =~ s/\'/:/; print LOG "Not Answered "; $add = " 00:0$rtm "; } elsif ($cd eq "TR") { $dur =~ s/\'/:/; print LOG "Transfered "; $add = " $dur by $ext"; } else { $rtm =~ s/(.)\'/00:0$1:/; $dur =~ s/\'/:/; print LOG "Completed "; $add = " $rtm $dur by $ext"; } print LOG "Incoming call"; $nm =~ s/\s+$//; if (length($nm) != 0) { print LOG " $nm"; } if (length($cid) != 0) { print LOG " <$cid>"; } print LOG " on CO $co"; if (length($add) != 0) { print LOG $add; } print LOG "\n"; $dbh->do("INSERT into cdr VALUES (?,?,?,?,?,?,?,?,?,?)", undef, $dttm, 1, $ext, $co, $cid, $nm, $rtm, $dur, $acc, $cd); } else { $cid =~ s/([\d\#\*]*)\s*/$1/; $dur =~ s/\'/:/; print LOG "Outgoing call"; if (length($cid) != 0) { print LOG " to $cid"; } print LOG " from $ext on CO $co for $dur\n"; $dbh->do("INSERT into cdr VALUES (?,?,?,?,?,?,?,?,?,?)", undef, $dttm, 0, $ext, $co, $cid, $nm, $rtm, $dur, $acc, $cd); } $dbh->disconnect(); } } } $po->close;