2012-04-04 17:22:00 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# vim:set ts=4 sw=4:
|
|
|
|
|
|
|
|
use strict;
|
2012-04-05 12:54:12 +00:00
|
|
|
use warnings;
|
2012-04-04 17:22:00 +00:00
|
|
|
use POSIX qw(strftime);
|
2012-04-04 18:56:52 +00:00
|
|
|
#use Time::HiRes qw(time);
|
|
|
|
use Digest::CRC qw(crcccitt);
|
2012-04-04 17:22:00 +00:00
|
|
|
|
2012-04-05 12:08:54 +00:00
|
|
|
use FindBin;
|
|
|
|
use lib "$FindBin::Bin/lib";
|
2012-04-04 17:22:00 +00:00
|
|
|
use r0ket;
|
|
|
|
|
|
|
|
$|=1;
|
|
|
|
|
2012-04-05 12:54:12 +00:00
|
|
|
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";
|
2012-04-05 13:00:27 +00:00
|
|
|
my $ser = undef;
|
2012-04-05 12:54:12 +00:00
|
|
|
my $help = 0;
|
|
|
|
my $intvl = 2;
|
|
|
|
my $lintvl = 60;
|
|
|
|
GetOptions (
|
|
|
|
"server=s" => \$server,
|
|
|
|
"port=n" => \$port,
|
|
|
|
"id=n" => \$id,
|
2012-04-05 13:00:27 +00:00
|
|
|
"dev=s" => \$ser,
|
2012-04-05 12:54:12 +00:00
|
|
|
"fast" => \$fast,
|
|
|
|
"verbose" => \$verbose,
|
|
|
|
"channel=n" => \$channel,
|
|
|
|
"mac=s" => \$mac,
|
|
|
|
"help" => \$help,
|
|
|
|
);
|
|
|
|
|
|
|
|
if($help){
|
|
|
|
die "Currently no help. Please check the source\n";
|
|
|
|
};
|
|
|
|
|
2012-04-05 13:00:27 +00:00
|
|
|
$ser=r0ket::r0ket_init($ser);
|
2012-04-04 17:22:00 +00:00
|
|
|
|
|
|
|
# Default openbeacon settings.
|
2012-04-05 12:54:12 +00:00
|
|
|
r0ket::set_txmac(pack("H*",$mac)); # Not really needed.
|
|
|
|
r0ket::set_rxmac(pack("H*",$mac));
|
|
|
|
r0ket::set_channel($channel);
|
2012-04-04 17:22:00 +00:00
|
|
|
r0ket::set_rxlen(16);
|
2012-04-04 18:56:52 +00:00
|
|
|
$r0ket::quiet=1; # Hackety-hack :)
|
2012-04-04 17:22:00 +00:00
|
|
|
|
|
|
|
my %bdata;
|
|
|
|
|
|
|
|
use Socket;
|
|
|
|
use Sys::Hostname;
|
|
|
|
|
2012-04-05 12:54:12 +00:00
|
|
|
my($iaddr,$proto,$paddr);
|
2012-04-04 17:22:00 +00:00
|
|
|
$iaddr = gethostbyname(hostname());
|
|
|
|
$proto = getprotobyname('udp');
|
|
|
|
$paddr = sockaddr_in(0, $iaddr); # 0 means let kernel pick
|
|
|
|
|
|
|
|
socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) || die "socket: $!";
|
|
|
|
bind(SOCKET, $paddr) || die "bind: $!";
|
|
|
|
|
2012-04-05 12:54:12 +00:00
|
|
|
my $hisiaddr = inet_aton($server) || die "unknown server name";
|
2012-04-04 17:22:00 +00:00
|
|
|
my $hispaddr = sockaddr_in($port, $hisiaddr);
|
|
|
|
|
|
|
|
###send(SOCKET, 0, 0, $hispaddr);
|
|
|
|
|
|
|
|
|
|
|
|
my $crcerr=0;
|
2012-04-05 12:54:12 +00:00
|
|
|
my $errors=0;
|
2012-04-04 17:22:00 +00:00
|
|
|
my $ctr=0;
|
2012-04-05 12:54:12 +00:00
|
|
|
my($lcrcerr,$lctr,$lerrors)=(0,0,0);
|
|
|
|
if($verbose){
|
2012-04-05 13:00:27 +00:00
|
|
|
my($dev)=$ser;
|
|
|
|
if(!defined $dev){
|
|
|
|
$dev="<undef>";
|
|
|
|
}else{
|
|
|
|
$dev=~s!/dev/!!;
|
|
|
|
};
|
|
|
|
print "OpenBeacon Reader $id sending [$dev] to [$server:$port]\n";
|
2012-04-05 12:54:12 +00:00
|
|
|
print "\n";
|
|
|
|
};
|
|
|
|
my $lasttime=time;
|
|
|
|
my $llasttime=time;
|
2012-04-04 18:56:52 +00:00
|
|
|
my $pkt;
|
2012-04-05 12:54:12 +00:00
|
|
|
my $donl=0;
|
|
|
|
|
|
|
|
my($typenick,$typebeacon,$typeunknown)=(0,0,0);
|
|
|
|
my($ltypenick,$ltypebeacon,$ltypeunknown)=(0,0,0);
|
2012-04-04 17:22:00 +00:00
|
|
|
while(1){
|
2012-04-04 18:56:52 +00:00
|
|
|
$pkt=r0ket::get_packet();
|
2012-04-05 12:54:12 +00:00
|
|
|
|
|
|
|
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";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-04-04 18:56:52 +00:00
|
|
|
next if($pkt eq "ack"); # in-band signalling.
|
2012-04-05 12:54:12 +00:00
|
|
|
if(length($pkt) != 16){ # Sanity check
|
|
|
|
$errors++;
|
|
|
|
next;
|
|
|
|
};
|
2012-04-04 17:22:00 +00:00
|
|
|
$ctr++;
|
2012-04-04 18:56:52 +00:00
|
|
|
|
|
|
|
my $hdr= pack("CCnnNN",
|
|
|
|
1, # proto (BEACONLOG_SIGHTING)
|
|
|
|
0, # interface (we only have one antenna per "reader")
|
2012-04-05 12:54:12 +00:00
|
|
|
$id, # readerid
|
|
|
|
length($pkt)+16, # size
|
2012-04-04 18:56:52 +00:00
|
|
|
$ctr, # sequence
|
|
|
|
time # timestamp
|
2012-04-04 17:22:00 +00:00
|
|
|
);
|
2012-04-04 18:56:52 +00:00
|
|
|
my $crc=pack("n",0xffff ^ crcccitt($hdr.$pkt));
|
2012-04-04 17:22:00 +00:00
|
|
|
|
2012-04-04 18:56:52 +00:00
|
|
|
send(SOCKET, $crc.$hdr.$pkt,0,$hispaddr);
|
|
|
|
|
2012-04-05 12:54:12 +00:00
|
|
|
next if($fast);
|
|
|
|
|
2012-04-04 18:56:52 +00:00
|
|
|
my $p=r0ket::nice_beacon($pkt);
|
2012-04-04 17:22:00 +00:00
|
|
|
if($p->{crc} ne "ok"){
|
|
|
|
$crcerr++;
|
|
|
|
next;
|
|
|
|
};
|
2012-04-05 12:54:12 +00:00
|
|
|
if($p->{type} eq "beacon"){
|
|
|
|
$typebeacon++;
|
|
|
|
}elsif($p->{type} eq "nick"){
|
|
|
|
$typenick++;
|
|
|
|
}else{
|
|
|
|
$typeunknown++;
|
|
|
|
};
|
2012-04-04 17:22:00 +00:00
|
|
|
};
|
|
|
|
r0ket::rest();
|