#!/usr/bin/perl
# read data from "tshark -V -x -r xxx.pcapng" and write USB data in hex
# A.Daviel March 2015

# for recent builds of Wireshark that understand USB interfaces
# no apologies for ugly - this was written ad-hoc for one particular problem

while (<STDIN>) {
  tr/\r//d ;
  chomp ;
  if (/^Frame[\s]+([\d]+):[\s]+([\d]+)/) {
    $frame = $1 ; $nb = $2 ;
#    print "frame $frame $nb bytes\n" ;
    $indata = $inframe = 0 ;
  }
  if (/URB type: URB_COMPLETE/) {
    $line = <STDIN>;
    $inframe = 1 ; $data = '' ;
    next ;
  }
  if (/Data length \[bytes\]:[\s]+([\d]+)/) {
    $dlen = $1 ;
  }
  unless ($_) {
    if ($indata) {
      print "$frame\t" ;
      $data =~ s/[\s]+$// ;
      @F = split(/ /,$data) ;
      $nf = @F ;
      unless ($nf == $dlen) { print STDERR "Frame $frame Data length $dlen $nf\n" ; }
      foreach $x (@F)  {
        printf ("%3.3d ",hex($x)) ;
      }
      print "\n" ;
#      print "$data\n" ;
    }
  }
  if ($inframe) {
    if (/^0000/ or /^0010/ or /^0020/ or /^0030/) { $indata = 1 ; next ; }
    if (/^0/) { $data .= substr($_,6,48) ; next ; }
  }
}
