From 709c402f294f4266d4ce651fcb37a1cc21273168 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Sat, 28 Jan 2012 14:23:27 +0100 Subject: [PATCH] Improve meshtrace/beacontrace --- tools/mesh/beacontrace.pl | 104 +++++++++++++++++++++++++++++--------- tools/mesh/meshtrace.pl | 17 +++---- tools/mesh/r0ket.pm | 68 +++++++++++++++++++++++-- 3 files changed, 150 insertions(+), 39 deletions(-) diff --git a/tools/mesh/beacontrace.pl b/tools/mesh/beacontrace.pl index c23fd8a..fe90f61 100755 --- a/tools/mesh/beacontrace.pl +++ b/tools/mesh/beacontrace.pl @@ -5,28 +5,28 @@ use strict; use Curses; use POSIX qw(strftime); +use Time::HiRes qw(time); use lib '.'; use r0ket; $|=1; -my $ser=""; +r0ket::r0ket_init(); -do {$ser=$_ if ( -e $_ ) } for qw(/dev/ttyS3 /dev/ttyACM0); - -if ($ARGV[0] eq "-s"){ - shift; - $ser=shift; -}; - -open(SER, "+<",$ser) || die "open serial: $!"; +# Default openbeacon settings. +r0ket::set_txmac(pack("H*","0102030201")); +r0ket::set_rxmac(pack("H*","0102030201")); +r0ket::set_channel(81); +r0ket::set_rxlen(16); #r0ket::readbeacon(); my $str; my %bdata; +sub do_str; + initscr; END{endwin;} use constant WIDTH => 80; @@ -40,25 +40,79 @@ $win_top->addstr(1,0,"-"x20); $win_top->refresh; my $beaconctr=0; +use constant CLEAN => 10; +use constant UPDATE => 0.3; +my $lasttime; +my $lastcleantime; +my $clean; +my $crcerr=0; while(1){ - $str=r0ket::get_packet(\*SER); + $str=r0ket::get_packet(); my $p=r0ket::nice_beacon($str); - if(!$bdata{$p->{beacon}}){ - $bdata{$p->{beacon}}=++$beaconctr; - }; - 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}); + if($p->{crc} ne "ok"){ + $crcerr++; + next; }; - $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$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(); + +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; +}; diff --git a/tools/mesh/meshtrace.pl b/tools/mesh/meshtrace.pl index 6dbd2d1..5b94300 100755 --- a/tools/mesh/meshtrace.pl +++ b/tools/mesh/meshtrace.pl @@ -11,16 +11,13 @@ use r0ket; $|=1; -my $ser=""; +r0ket::r0ket_init(); -do {$ser=$_ if ( -e $_ ) } for qw(/dev/ttyS3 /dev/ttyACM0); - -if ($ARGV[0] eq "-s"){ - shift; - $ser=shift; -}; - -open(SER, "+<",$ser) || die "open serial: $!"; +# Default mesh settings. +r0ket::set_txmac("ORBIT"); +r0ket::set_rxmac("ORBIT"); +r0ket::set_channel(83); +r0ket::set_rxlen(32); r0ket::readbeacon(); @@ -41,7 +38,7 @@ $win_top->refresh; my $beaconctr=0; while(1){ - $str=r0ket::get_packet(\*SER); + $str=r0ket::get_packet(); my $p=r0ket::pkt_beauty($str); if(!$bdata{$p->{beacon}}){ $bdata{$p->{beacon}}=++$beaconctr; diff --git a/tools/mesh/r0ket.pm b/tools/mesh/r0ket.pm index 26a94cc..ca97a41 100755 --- a/tools/mesh/r0ket.pm +++ b/tools/mesh/r0ket.pm @@ -10,6 +10,8 @@ package r0ket; use Digest::CRC qw(crcccitt); use POSIX qw(strftime); +our $verbose=1; +our $bridge; # Open device ### Utility sub sprint{ @@ -68,11 +70,9 @@ sub writebeacon{ our $buffer; sub get_packet{ - my $dev=shift; - sub _get_bytes{ my $rr; - sysread($dev,$rr,1024); + sysread($bridge,$rr,1024); if(length($rr)<=1){ select(undef,undef,undef,0.1); }; @@ -80,7 +80,7 @@ sub get_packet{ }; my $cnt=0; - while(++$cnt<10){ + while(++$cnt<50){ if(length($buffer)<2){ _get_bytes(); }elsif($buffer !~ /^\\[12]/){ @@ -180,6 +180,17 @@ sub nice_beacon{ }else{ $out->{string}=""; }; + + 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; }; @@ -202,5 +213,54 @@ sub pkt_beauty{ 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;