Add commandline options and verbose mode with on-the-fly statistics

This commit is contained in:
Stefan `Sec` Zehl 2012-04-05 14:54:12 +02:00
parent 72194bf139
commit b7155bbb71
1 changed files with 86 additions and 15 deletions

View File

@ -3,23 +3,49 @@
# vim:set ts=4 sw=4:
use strict;
use warnings;
use POSIX qw(strftime);
#use Time::HiRes qw(time);
use Digest::CRC qw(crcccitt);
use FindBin;
use lib "$FindBin::Bin/lib";
use r0ket;
$|=1;
use Getopt::Long;
my $server = "127.0.0.1";
my $port = 2342;
my $id = 1234;
my $verbose = 0;
my $fast = 0;
my $channel = 81;
my $mac = "0102030201";
my $help = 0;
my $intvl = 2;
my $lintvl = 60;
GetOptions (
"server=s" => \$server,
"port=n" => \$port,
"id=n" => \$id,
"fast" => \$fast,
"verbose" => \$verbose,
"channel=n" => \$channel,
"mac=s" => \$mac,
"help" => \$help,
);
if($help){
die "Currently no help. Please check the source\n";
};
r0ket::r0ket_init();
# Default openbeacon settings.
r0ket::set_txmac(pack("H*","0102030201"));
r0ket::set_rxmac(pack("H*","0102030201"));
r0ket::set_channel(81);
r0ket::set_txmac(pack("H*",$mac)); # Not really needed.
r0ket::set_rxmac(pack("H*",$mac));
r0ket::set_channel($channel);
r0ket::set_rxlen(16);
$r0ket::quiet=1; # Hackety-hack :)
@ -28,38 +54,76 @@ my %bdata;
use Socket;
use Sys::Hostname;
my($iaddr,$proto,$port,$paddr);
my($iaddr,$proto,$paddr);
$iaddr = gethostbyname(hostname());
$proto = getprotobyname('udp');
$port = "2342";
$paddr = sockaddr_in(0, $iaddr); # 0 means let kernel pick
socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) || die "socket: $!";
bind(SOCKET, $paddr) || die "bind: $!";
my $host="127.0.0.1";
my $hisiaddr = inet_aton($host) || die "unknown host";
my $hisiaddr = inet_aton($server) || die "unknown server name";
my $hispaddr = sockaddr_in($port, $hisiaddr);
###send(SOCKET, 0, 0, $hispaddr);
my $beaconctr=0;
my $lasttime;
my $crcerr=0;
my $errors=0;
my $ctr=0;
my $fast=0;
my($lcrcerr,$lctr,$lerrors)=(0,0,0);
if($verbose){
print "OpenBeacon Reader $id sending to [$server:$port]\n";
print "\n";
};
my $lasttime=time;
my $llasttime=time;
my $pkt;
my $donl=0;
my($typenick,$typebeacon,$typeunknown)=(0,0,0);
my($ltypenick,$ltypebeacon,$ltypeunknown)=(0,0,0);
while(1){
$pkt=r0ket::get_packet();
if($verbose){
if(time-$lasttime >= $intvl){
print "\r";
if(time-$llasttime >= $lintvl){
$donl=1;
$llasttime=time;
};
$lasttime=time;
print strftime("%Y-%m-%d %H:%M:%S ",localtime);
printf "[%ds] cnt=%3d [b=%3d, n=%3d, ?=%3d] errs=%3d crcerr=%3d ",
$intvl,
($ctr-$lctr),
($typebeacon-$ltypebeacon),
($typenick-$ltypenick),
($typeunknown-$ltypeunknown),
($errors-$lerrors),
($crcerr-$lcrcerr);
($lctr,$lerrors,$lcrcerr)= ($ctr,$errors,$crcerr);
($ltypenick,$ltypebeacon,$ltypeunknown)= ($typenick,$typebeacon,$typeunknown);
if($donl){
$donl=0;
print "\n";
};
};
};
next if($pkt eq "ack"); # in-band signalling.
if(length($pkt) != 16){ # Sanity check
$errors++;
next;
};
$ctr++;
my $hdr= pack("CCnnNN",
1, # proto (BEACONLOG_SIGHTING)
0, # interface (we only have one antenna per "reader")
1234, # readerid
32, # size
$id, # readerid
length($pkt)+16, # size
$ctr, # sequence
time # timestamp
);
@ -67,12 +131,19 @@ while(1){
send(SOCKET, $crc.$hdr.$pkt,0,$hispaddr);
next if($fast);
next if($fast);
my $p=r0ket::nice_beacon($pkt);
if($p->{crc} ne "ok"){
$crcerr++;
next;
};
if($p->{type} eq "beacon"){
$typebeacon++;
}elsif($p->{type} eq "nick"){
$typenick++;
}else{
$typeunknown++;
};
};
print "received $ctr pkts, $crcerr crc errors\n";
r0ket::rest();