diff --git a/rftestrx2pcap.pl b/rftestrx2pcap.pl new file mode 100755 index 000000000..81bad3727 --- /dev/null +++ b/rftestrx2pcap.pl @@ -0,0 +1,87 @@ +#!/usr/bin/perl -w + +use Device::SerialPort; +use Term::ReadKey; +use Getopt::Long; +use Time::HiRes qw(usleep gettimeofday); + +use strict; + +my $filename = ''; +my $second = ''; +my $term = '/dev/ttyUSB0'; +my $baud = '115200'; +my $verbose; + +GetOptions ( + 'terminal=s' => \$term, + 'baud=s' => \$baud, + ); + +$| = 1; + +# TODO: add help argument +# print "Example usage: rftestrx2pcap.pl -t /dev/ttyS0 -b 9600\n"; +# exit; + +my $ob = Device::SerialPort->new ($term) or die "Can't start $term\n"; + # next test will die at runtime unless $ob + +$ob->baudrate($baud); +$ob->parity('none'); +$ob->databits(8); +$ob->stopbits(1); +$ob->read_const_time(1000); # 1 second per unfulfilled "read" call + +my $str = ''; +my ($sec, $usec, $len); +my @frame; + +my $magic = 0xa1b2c3d4; +my $major = 2; +my $minor = 4; +my $zone = 0; +my $sig = 0; +my $snaplen = 0xffff; +my $network = 195; # 802.15.4 + +print pack('LSSLLLL',($magic,$major,$minor,$zone,$sig,$snaplen,$network)); + +while(1) { + my ($count, $c) = $ob->read(1); + + if (defined($count) && ($count != 0)) { + $str .= $c; + # match if ends in \n or \r and process line + if(($str =~ /\n\r$/) || + ($str =~ /\r\n$/)) { + if($str =~ /^rftest/) { + #new packet + ($sec, $usec) = gettimeofday; +# print "rftestline: $sec $usec $str\n\r"; + } elsif($str =~ /^\s*data/) { + #packet payload + $str =~ /data: 0x\d+ (.+)/; + my @data = split(' ',$1); +# print "dataline: "; + ($len, @data) = @data; +# print "\n\r"; + #write out pcap entry + print pack('LLLL',($sec,$usec,scalar(@data),scalar(@data)+2)); + @frame = @data[0,1]; + print pack ('CC',($frame[1],$frame[0])); + foreach my $data (@data[2..scalar(@data)]) { + print pack ('C',hex($data)); + } + } + + $str = ''; + } + } +} + +$ob -> close or die "Close failed: $!\n"; +ReadMode 0; +undef $ob; # closes port AND frees memory in perl +exit; +