Improve meshtrace/beacontrace

This commit is contained in:
Stefan `Sec` Zehl 2012-01-28 14:23:27 +01:00
parent d0be542bbe
commit 709c402f29
3 changed files with 150 additions and 39 deletions

View File

@ -5,28 +5,28 @@
use strict; use strict;
use Curses; use Curses;
use POSIX qw(strftime); use POSIX qw(strftime);
use Time::HiRes qw(time);
use lib '.'; use lib '.';
use r0ket; use r0ket;
$|=1; $|=1;
my $ser="<undef>"; r0ket::r0ket_init();
do {$ser=$_ if ( -e $_ ) } for qw(/dev/ttyS3 /dev/ttyACM0); # Default openbeacon settings.
r0ket::set_txmac(pack("H*","0102030201"));
if ($ARGV[0] eq "-s"){ r0ket::set_rxmac(pack("H*","0102030201"));
shift; r0ket::set_channel(81);
$ser=shift; r0ket::set_rxlen(16);
};
open(SER, "+<",$ser) || die "open serial: $!";
#r0ket::readbeacon(); #r0ket::readbeacon();
my $str; my $str;
my %bdata; my %bdata;
sub do_str;
initscr; initscr;
END{endwin;} END{endwin;}
use constant WIDTH => 80; use constant WIDTH => 80;
@ -40,25 +40,79 @@ $win_top->addstr(1,0,"-"x20);
$win_top->refresh; $win_top->refresh;
my $beaconctr=0; my $beaconctr=0;
use constant CLEAN => 10;
use constant UPDATE => 0.3;
my $lasttime;
my $lastcleantime;
my $clean;
my $crcerr=0;
while(1){ while(1){
$str=r0ket::get_packet(\*SER); $str=r0ket::get_packet();
my $p=r0ket::nice_beacon($str); my $p=r0ket::nice_beacon($str);
if(!$bdata{$p->{beacon}}){ if($p->{crc} ne "ok"){
$bdata{$p->{beacon}}=++$beaconctr; $crcerr++;
}; next;
if($p->{type} eq "beacon"){
$win->addstr($bdata{$p->{beacon}},0,
sprintf "%s | bt=%s str=%s idx=%8s | %s",
$p->{beacon},
$p->{button},
$p->{strength},
$p->{idx},
r0ket::getbeacon($p->{beacon})
);
}else{
$win->addstr($bdata{$p->{beacon}},40,$p->{nick});
}; };
$win->refresh; if($p->{type} eq "beacon"){
$bdata{$p->{beacon}}{seen}=time;
$bdata{$p->{beacon}}{beacon}=$p;
$bdata{$p->{beacon}}{stats}{$p->{strength}}++;
if(!defined($bdata{$p->{beacon}}{stats}{first})){
$bdata{$p->{beacon}}{stats}{first}=time;
};
}elsif($p->{type} eq "nick"){
$bdata{$p->{beacon}}{nick}=$p->{nick};
}else{ #unknown
;
};
if(time>$lastcleantime+CLEAN){
$clean=1;
$lastcleantime=time;
}else{
$clean=0;
};
my $line=0;
if($clean){
$win->clear;
for my $b (sort keys %bdata){
if($bdata{$b}{seen}+10<time){
delete $bdata{$b}
};
};
};
if(time>$lasttime+UPDATE){
for my $b (sort keys %bdata){
$win->addstr($line++,0,
sprintf "%s | bt=%s idx=%8s | %s | %s",
$b,
$bdata{$b}{beacon}->{button},
$bdata{$b}{beacon}->{idx},
do_str($bdata{$b}{stats}),
$bdata{$b}{nick}." "
);
};
$win_top->addstr(1,20,sprintf" cnt=%2d, crc=%d",scalar(keys %bdata),$crcerr);
$win_top->refresh;
$win->refresh;
$lasttime=time;
};
}; };
r0ket::rest(); r0ket::rest();
sub do_str{
my $hr=shift;
my $df=time()-$hr->{first};
$df=1 if $df==0;
my $out="";
# for(sort keys %$hr){
for(qw(00 55 aa ff)){
next if $_ eq "first";
$out.=sprintf("%3d% ",($hr->{$_}/$df)*100/2);
};
return $out;
};

View File

@ -11,16 +11,13 @@ use r0ket;
$|=1; $|=1;
my $ser="<undef>"; r0ket::r0ket_init();
do {$ser=$_ if ( -e $_ ) } for qw(/dev/ttyS3 /dev/ttyACM0); # Default mesh settings.
r0ket::set_txmac("ORBIT");
if ($ARGV[0] eq "-s"){ r0ket::set_rxmac("ORBIT");
shift; r0ket::set_channel(83);
$ser=shift; r0ket::set_rxlen(32);
};
open(SER, "+<",$ser) || die "open serial: $!";
r0ket::readbeacon(); r0ket::readbeacon();
@ -41,7 +38,7 @@ $win_top->refresh;
my $beaconctr=0; my $beaconctr=0;
while(1){ while(1){
$str=r0ket::get_packet(\*SER); $str=r0ket::get_packet();
my $p=r0ket::pkt_beauty($str); my $p=r0ket::pkt_beauty($str);
if(!$bdata{$p->{beacon}}){ if(!$bdata{$p->{beacon}}){
$bdata{$p->{beacon}}=++$beaconctr; $bdata{$p->{beacon}}=++$beaconctr;

View File

@ -10,6 +10,8 @@ package r0ket;
use Digest::CRC qw(crcccitt); use Digest::CRC qw(crcccitt);
use POSIX qw(strftime); use POSIX qw(strftime);
our $verbose=1;
our $bridge; # Open device
### Utility ### Utility
sub sprint{ sub sprint{
@ -68,11 +70,9 @@ sub writebeacon{
our $buffer; our $buffer;
sub get_packet{ sub get_packet{
my $dev=shift;
sub _get_bytes{ sub _get_bytes{
my $rr; my $rr;
sysread($dev,$rr,1024); sysread($bridge,$rr,1024);
if(length($rr)<=1){ if(length($rr)<=1){
select(undef,undef,undef,0.1); select(undef,undef,undef,0.1);
}; };
@ -80,7 +80,7 @@ sub get_packet{
}; };
my $cnt=0; my $cnt=0;
while(++$cnt<10){ while(++$cnt<50){
if(length($buffer)<2){ if(length($buffer)<2){
_get_bytes(); _get_bytes();
}elsif($buffer !~ /^\\[12]/){ }elsif($buffer !~ /^\\[12]/){
@ -180,6 +180,17 @@ sub nice_beacon{
}else{ }else{
$out->{string}="<?:".unpack("H*",$pkt).">"; $out->{string}="<?:".unpack("H*",$pkt).">";
}; };
my $pkt_crc= unpack("n",substr($pkt,length($pkt)-2,2));
my $calc_crc= crcccitt(substr($pkt,0,length($pkt)-2));
if ($pkt_crc eq $calc_crc){
$out->{crc}="ok";
}else{
$out->{crc}="fail";
$out->{string}.= " CRCFAIL";
};
return $out; return $out;
}; };
@ -202,5 +213,54 @@ sub pkt_beauty{
return $out; return $out;
} }
sub r0ket_init{
my $ser;
if ($ARGV[0] eq "-s"){
shift;
$ser=shift;
};
if(!defined $ser){
if (defined $ENV{R0KETBRIDGE} && -e $ENV{R0KETBRIDGE}){
$ser=$ENV{R0KETBRIDGE}
};
};
if(!defined $ser){
do {$ser=$_ if ( -e $_ ) } for qw(/dev/ttyS3 /dev/ttyACM0);
};
open($bridge, "+<",$ser) || die "open serial: $!";
if($verbose){
print "using: $ser\n";
};
};
sub send_raw {
if($verbose){
print "send: ",unpack("H*",$_[0]),"\n";
};
syswrite($bridge,shift);
};
sub send_pkt_num {
my $pkt=shift;
$pkt=~s/\\/\\\\/;
send_raw('\\'.shift().$pkt.'\0');
};
sub send_pkt {
send_pkt_num(shift,1);
};
sub set_txmac {
send_pkt_num(shift,3);
};
sub set_rxmac {
send_pkt_num(shift,4);
};
sub set_channel {
send_pkt_num(pack("C",shift),5);
};
sub set_rxlen {
send_pkt_num(pack("C",shift),6);
};
1; 1;