#!/usr/bin/perl
#
# Copyright 2005/2006 Markus Rechberger <mrechberger@gmail.com>
# Modified by Pantelis Koukousoulas as a quick hack to test the pvrusb2 streams
#
# this tool parses the outgoing isoc stream from an usbsnoop logfile
# and outputs the binary datastream.
#
# to get it work with a specific endpoint just replace:
# if($isocon==1 && $_=~/USBD_TRANSFER_DIRECTION_OUT/i)
#                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^  
#                      with the endpoint address you're looking for
#                      nevermind if it's outgoing or incoming
#                                                   
#
#[25001 ms]  <<<  URB 785 coming back  <<<
#-- URB_FUNCTION_ISOCH_TRANSFER:
#  PipeHandle           = 8885a664 [endpoint 0x00000081]
#  TransferFlags        = 00000001 (USBD_TRANSFER_DIRECTION_IN, ~USBD_SHORT_TRANSFER_OK
#  TransferBufferLength = 0003cb7c
#  TransferBuffer       = 88484100
#  TransferBufferMDL    = 84f288c0
#  StartFrame           = 00027619
#  NumberOfPackets      = 00000100
#  ErrorCount           = 00000002
#  IsoPacket[0].Offset = 0
#  IsoPacket[0].Length = 0
#  IsoPacket[0].Status = c0000011
#  IsoPacket[1].Offset = 3072
#  IsoPacket[1].Length = 0
#  IsoPacket[1].Status = c0000011
#  IsoPacket[2].Offset = 6144
#  IsoPacket[2].Length = 3072
#  IsoPacket[2].Status = 00000000
#    00000000: ff bf 02 02 ff ff 13 11 14 11 14 12 14 11 14 11
#    00000010: 14 11 14 12 14 12 17 13 1a 16 1a 15 1c 17 1c 16
#    00000020: 1b 14 15 10 10 0e 0f 0d 14 12 19 12 11 0e 0e 0d
#    00000030: 0e 0d 0e 0d 0e 0d 0e 0d 0e 0d 0e 0d 0e 0d 0e 0d
#    00000040: 0e 0d 0e 0d 0e 0d 10 0e 11 0e 10 0e 0f 0d 0e 0d
#    00000050: 0e 0d 0e 0d 0d 0d 0e 0d 0e 0d 0f 0e 0f 0e 0f 0d
#    00000060: 0e 0d 0e 0d 0e 0d 0e 0d 0e 0d 0e 0d 0e 0d 0e 0d
#    00000070: 0e 0d 0f 0d 0e 0d 0f 0e 11 11 16 10 0e 0d 0f 0e
#    00000080: 10 10 1c 15 15 13 1b 15 14 12 1a 15 15 13 1b 15
#    00000090: 15 13 1a 16 14 13 1a 15 12 0f 1a 16 14 13 1b 15
#    000000a0: 13 12 12 11 14 13 1c 16 15 13 1b 15 14 12 0f 0e
#    000000b0: 10 0f 11 10 13 11 16 13 16 14 13 12 16 13 13 11
#    000000c0: 13 13 16 14 16 13 16 13 17 14 17 13 16 13 16 13
#    000000d0: 16 13 15 12 15 13 16 13 14 12 14 12 14 12 14 12
#    000000e0: 14 12 14 12 13 12 16 13 15 13 15 13 16 13 16 14
#    000000f0: 17 14 17 14 17 14 13 11 16 14 12 11 11 10 14 0e
#    00000100: 16 14 1f 1a 1c 15 12 10 12 0f 16 19 21 1d 24 1e
#    00000110: 23 1b 22 1b 22 1a 20 19 20 1a 1f 18 1c 18 1c 17
#    00000120: 1b 17 1a 16 18 14 17 14 17 13 16 13 15 13 15 12
#    00000130: 15 12 13 12 14 12 14 11 13 11 14 11 14 11 12 11
#    00000140: 12 11 12 10 12 11 12 10 13 10 11 10 11 10 12 10
#    00000150: 11 10 11 10 11 10 11 10 12 0f 12 10 11 10 11 0f
#
use strict;

my $isocon=0;
my $payload;
my $packindex;
my @paytmp;
my $dataon;

while(<>){
	if($_=~/^-- /i and $isocon==1){
		$isocon=0;
        $dataon=0;
	}
	if($_=~/-- URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER:/i){
		$isocon=1;
	}
    if($isocon==1 && $_=~/endpoint 0x00000084/i){
        $dataon=1;
    }
	if($dataon==1){
		$payload="";
		if($_=~/    ([0-9a-f]{8}): (.*)/i){
			$payload=$2;
			$packindex=$1;
			chomp($payload);
#			print "$packindex - $payload\n";
			@paytmp=split(/ /,$payload);
			print pack("(H2)*", @paytmp);
		}
	}
}
