Skip to content

Instantly share code, notes, and snippets.

@vdudouyt
Last active January 1, 2016 09:09
Show Gist options
  • Save vdudouyt/8122643 to your computer and use it in GitHub Desktop.
Save vdudouyt/8122643 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# Generates PyUSB equivalent from the capture log
#
# Synopsis:
# ./gen_pyusb.pl <test_unit_ready.cap --asserts
# dev.write(2, hex_to_list('55534243707ec2810000000000000600000000000000000000000000000000'))
# assert(list(dev.read(0x81, 26)) == hex_to_list('55534253707ec2810000000000'))
#
# .vimrc:
# :au BufEnter,BufNew *.cap vmap <buffer> <Enter> :!/usr/lib/gen_pyusb.pl<Enter>
use strict;
use USBSniff::Parser;
use Getopt::Long;
use Switch;
my %opts;
my $result = GetOptions(\%opts, "asserts");
for my $packet (USBSniff::Parser::parse(join '', <STDIN>)) {
switch ($packet->get_direction) {
case 'OUT' {
printf("dev.write(%d, hex_to_list('%s'))\n", $packet->get_endpoint, $packet->get_data);
}
case 'IN' {
my $code = sprintf("dev.read(0x%02x, %d)", $packet->get_endpoint | 0x80, int(length($packet->get_data) / 2));
$code = sprintf("assert(list(%s) == hex_to_list('%s'))", $code, $packet->get_data) if $opts{asserts};
print "$code\n";
}
else {
die("Improper format");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment