forked from samyk/samytools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialsniff
executable file
·83 lines (65 loc) · 1.47 KB
/
serialsniff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/perl
# Sniffs a serial Connection. Prints all unprintable characters!
# Only tested under Mac OS X 10.8. (should work with earlier versions)
# Uses dtruss, which comes with Mac OS.
#
# Usage:
# $ ./serialsniffer.rb <serial device>
#
# Example:
# $ ./serialsniffer.rb /dev/tty.usbserial-A6008j9P
use strict;
die "usage: $0 </dev/tty...>\n" unless -e $ARGV[0];
my ($l) = grep { /$ARGV[0]/ } `/usr/sbin/lsof -n`;
my ($pid, $fd) = ($l =~ /^\s*\S+\s+(\d+)\s+\S+\s+(\d+)u/);
die "no fds using $ARGV[0] (pid $pid)\n" unless $fd;
my $cmd = "dtruss -p $pid 2>&1";
open(F, "$cmd|") || die "Couldn't run $cmd: $!";
print STDERR "$cmd\n";
my @r;
my $h = unpack("H*", chr($fd));
$h =~ s/^0*/0x/g;
while (<F>)
{
if (/(?:(r)ead|(w)rite)\($h, "(.*?)"/)
{
my ($rw, $o) = ($1 . $2, $3);
$o =~ s/\\(\d\d\d)/chr(oct($1))/eg;
$o =~ s/\\0$/\0/g;
if ($rw eq "r")
{
push @r, map { ord } split //, $o;
}
$o =~ s/(.)/unpack("H2", $1) . " "/eg;
print "$rw: $o\n";
#@r = rr(@r) if @r;
}
}
sub rr
{
my @r = @_;
return @r if !@r;
my $r = shift @r;
if ($r != 0xFE)
{
print "Bad start: $r\n";
return @r;
}
if (@r < $r[0] + 7)
{
print "not enough ($r[0] + 7)\n";
# wait for more data to come in
unshift @r, $r;
return @r;
}
my ($len, $seq, $sid, $cid, $mid) = splice(@r, 0, 5);
my @payload = splice(@r, 0, $len);
my @crc = splice(@r, 0, 2);
print "s/cid=$sid $cid MID=$mid\n";
while (@r)
{
print "rerunning! " . @r . "\n";
@r = rr(@r);
}
return @r;
}