#!/usr/bin/perl -n
#
# Extract the pure IO from a log file produced by SniffUSB 
#
# Copyright (C) 2006  Martin Schewe  <pixma at schewe_com>
#
# You always find the most recent version on http://pixma.schewe.com.
# $Id: Makefile 38 2006-03-25 22:22:36Z ms $
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

sub process_urb {
	unless ($back) {
		$down{$urb} = $dat;
		return;
	}
	
	local $_ = $dat;
	$dat = "";
	
	/URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER/ or return;
	
	$out   = /USBD_TRANSFER_DIRECTION_OUT/;
	($ep)  = /endpoint 0x([\da-f]+)/;
	($len) = /TransferBufferLength *= ([\da-f]+)/;
	($mdl) = /TransferBufferMDL *= ([\da-f]+)/;
	$buf   = join(" ", /^ *[\da-f]+: ([\da-f ]+)/mg) ||
	         join(" ", $down{$urb} =~ /^ *[\da-f]+: ([\da-f ]+)/mg);
	
	hex $len or $buf = "NULL";
	
	delete $down{$urb};

	$verbose and printf "%04i %s ", $urb, $out ? ">" : "<";
	printf "%02x: %s\n", hex $ep, $buf;
}
	
if (/URB (\d+) (going down|coming back)/) {
	process_urb();
	$urb = $1;
	$dat = "";
	$back = $2 eq "coming back";
} else {
	$dat .= $_;
}
