Add commandline options and verbose mode with on-the-fly statistics
This commit is contained in:
parent
72194bf139
commit
b7155bbb71
|
@ -3,23 +3,49 @@
|
||||||
# vim:set ts=4 sw=4:
|
# vim:set ts=4 sw=4:
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
|
use warnings;
|
||||||
use POSIX qw(strftime);
|
use POSIX qw(strftime);
|
||||||
#use Time::HiRes qw(time);
|
#use Time::HiRes qw(time);
|
||||||
use Digest::CRC qw(crcccitt);
|
use Digest::CRC qw(crcccitt);
|
||||||
|
|
||||||
use FindBin;
|
use FindBin;
|
||||||
use lib "$FindBin::Bin/lib";
|
use lib "$FindBin::Bin/lib";
|
||||||
|
|
||||||
use r0ket;
|
use r0ket;
|
||||||
|
|
||||||
$|=1;
|
$|=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();
|
r0ket::r0ket_init();
|
||||||
|
|
||||||
# Default openbeacon settings.
|
# Default openbeacon settings.
|
||||||
r0ket::set_txmac(pack("H*","0102030201"));
|
r0ket::set_txmac(pack("H*",$mac)); # Not really needed.
|
||||||
r0ket::set_rxmac(pack("H*","0102030201"));
|
r0ket::set_rxmac(pack("H*",$mac));
|
||||||
r0ket::set_channel(81);
|
r0ket::set_channel($channel);
|
||||||
r0ket::set_rxlen(16);
|
r0ket::set_rxlen(16);
|
||||||
$r0ket::quiet=1; # Hackety-hack :)
|
$r0ket::quiet=1; # Hackety-hack :)
|
||||||
|
|
||||||
|
@ -28,38 +54,76 @@ my %bdata;
|
||||||
use Socket;
|
use Socket;
|
||||||
use Sys::Hostname;
|
use Sys::Hostname;
|
||||||
|
|
||||||
my($iaddr,$proto,$port,$paddr);
|
my($iaddr,$proto,$paddr);
|
||||||
$iaddr = gethostbyname(hostname());
|
$iaddr = gethostbyname(hostname());
|
||||||
$proto = getprotobyname('udp');
|
$proto = getprotobyname('udp');
|
||||||
$port = "2342";
|
|
||||||
$paddr = sockaddr_in(0, $iaddr); # 0 means let kernel pick
|
$paddr = sockaddr_in(0, $iaddr); # 0 means let kernel pick
|
||||||
|
|
||||||
socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) || die "socket: $!";
|
socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) || die "socket: $!";
|
||||||
bind(SOCKET, $paddr) || die "bind: $!";
|
bind(SOCKET, $paddr) || die "bind: $!";
|
||||||
|
|
||||||
my $host="127.0.0.1";
|
my $hisiaddr = inet_aton($server) || die "unknown server name";
|
||||||
my $hisiaddr = inet_aton($host) || die "unknown host";
|
|
||||||
my $hispaddr = sockaddr_in($port, $hisiaddr);
|
my $hispaddr = sockaddr_in($port, $hisiaddr);
|
||||||
|
|
||||||
###send(SOCKET, 0, 0, $hispaddr);
|
###send(SOCKET, 0, 0, $hispaddr);
|
||||||
|
|
||||||
|
|
||||||
my $beaconctr=0;
|
|
||||||
my $lasttime;
|
|
||||||
my $crcerr=0;
|
my $crcerr=0;
|
||||||
|
my $errors=0;
|
||||||
my $ctr=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 $pkt;
|
||||||
|
my $donl=0;
|
||||||
|
|
||||||
|
my($typenick,$typebeacon,$typeunknown)=(0,0,0);
|
||||||
|
my($ltypenick,$ltypebeacon,$ltypeunknown)=(0,0,0);
|
||||||
while(1){
|
while(1){
|
||||||
$pkt=r0ket::get_packet();
|
$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.
|
next if($pkt eq "ack"); # in-band signalling.
|
||||||
|
if(length($pkt) != 16){ # Sanity check
|
||||||
|
$errors++;
|
||||||
|
next;
|
||||||
|
};
|
||||||
$ctr++;
|
$ctr++;
|
||||||
|
|
||||||
my $hdr= pack("CCnnNN",
|
my $hdr= pack("CCnnNN",
|
||||||
1, # proto (BEACONLOG_SIGHTING)
|
1, # proto (BEACONLOG_SIGHTING)
|
||||||
0, # interface (we only have one antenna per "reader")
|
0, # interface (we only have one antenna per "reader")
|
||||||
1234, # readerid
|
$id, # readerid
|
||||||
32, # size
|
length($pkt)+16, # size
|
||||||
$ctr, # sequence
|
$ctr, # sequence
|
||||||
time # timestamp
|
time # timestamp
|
||||||
);
|
);
|
||||||
|
@ -67,12 +131,19 @@ while(1){
|
||||||
|
|
||||||
send(SOCKET, $crc.$hdr.$pkt,0,$hispaddr);
|
send(SOCKET, $crc.$hdr.$pkt,0,$hispaddr);
|
||||||
|
|
||||||
next if($fast);
|
next if($fast);
|
||||||
|
|
||||||
my $p=r0ket::nice_beacon($pkt);
|
my $p=r0ket::nice_beacon($pkt);
|
||||||
if($p->{crc} ne "ok"){
|
if($p->{crc} ne "ok"){
|
||||||
$crcerr++;
|
$crcerr++;
|
||||||
next;
|
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();
|
r0ket::rest();
|
||||||
|
|
Loading…
Reference in New Issue