-
Notifications
You must be signed in to change notification settings - Fork 37
/
usbserial.py
executable file
·205 lines (190 loc) · 6.78 KB
/
usbserial.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python3
from usbrply.printer import Printer, indented, indent_inc, indent_dec
from usbrply.util import add_bool_arg
import usbrply.parsers
import usbrply.serial.parsers as sparsers
import usbrply.serial.printers as sprinters
from usbrply.serial import mpsse
import usbrply.main
from usbrply import parsers
import argparse
def main():
parser = argparse.ArgumentParser(
description='Decode USB-serial data (experimental / alpha quality)')
parser.add_argument('--range', '-r', help='inclusive range like 123:456')
"""
parser.add_argument('-k',
dest='ofmt',
default='libusb-py',
action='store_const',
const='linux',
help='output linux kernel')
"""
parser.add_argument('-l',
dest='ofmt',
action='store_const',
const='libusb-c',
help='output libusb C (WARNING: experimental)')
parser.add_argument('-p',
dest='ofmt',
action='store_const',
const='libusb-py',
help='output libusb python')
parser.add_argument("--json",
'-j',
dest='ofmt',
action='store_const',
const='json',
help='output json')
parser.add_argument("--text",
'-t',
dest='ofmt',
action='store_const',
const='text',
help='output txt')
parser.add_argument('-s', help='allow short')
parser.add_argument('-f', help='custom call')
add_bool_arg(parser,
'--packet-numbers',
default=True,
help='print packet numbers')
parser.add_argument('--verbose', '-v', action='store_true', help='verbose')
parser.add_argument(
'--parser',
default="auto",
help='Which parser engine to use. Choices: auto, lin-pcap, win-pcap')
add_bool_arg(
parser,
'--sleep',
default=False,
help='Insert sleep statements between packets to keep original timing')
add_bool_arg(parser, '--comment', default=False, help='General comments')
add_bool_arg(parser, '--fx2', default=False, help='FX2 comments')
add_bool_arg(parser,
'--define',
default=False,
help='Use defines instead of raw numbers')
add_bool_arg(parser, '--halt', default=True, help='Halt on bad packets')
parser.add_argument('--vid',
type=str,
default="0",
help='Only keep packets for given VID')
parser.add_argument('--pid',
type=str,
default="0",
help='Only keep packets for given PID')
parser.add_argument('--device',
type=int,
default=None,
help='Only keep packets for given device')
add_bool_arg(parser,
'--device-hi',
default=None,
help='Auto detect to highest device number')
add_bool_arg(parser,
'--rel-pkt',
default=False,
help='Only count kept packets')
# http://sourceforge.net/p/libusb/mailman/message/25635949/
add_bool_arg(parser,
'--remoteio',
default=False,
help='Warn on -EREMOTEIO resubmit (default: ignore)')
add_bool_arg(
parser,
'--print-short',
default=False,
help='Print warning when request returns less data than requested')
add_bool_arg(
parser,
'--setup',
default=False,
help='Emit initialization packets like CLEAR_FEATURE, SET_FEATURE')
add_bool_arg(parser,
'--wrapper',
default=False,
help='Emit code to make it a full executable program')
add_bool_arg(parser,
'--mpsee',
default=False,
help='Decode mpsee traffic (highly experimental)')
add_bool_arg(parser, '--serial-emit-adata', default=True, help='')
add_bool_arg(parser, '--serial-keep-raw-data', default=False, help='')
add_bool_arg(parser, '--serial-keep-unused-data', default=False, help='')
add_bool_arg(parser, '--serial-keep-empty-txrx', default=False, help='')
parser.add_argument('fin', help='File name in')
args = parser.parse_args()
argsj = usbrply.main.munge_argsj(args)
if args.verbose:
print("")
print("")
print("")
print("PASS: USB parse")
gen = usbrply.parsers.pcap2json_prepare(args.fin, argsj)
parsed = gen.run()
# HACK: get from json output
filters = []
filters.append("vidpid")
if not args.setup:
filters.append("setup")
if args.comment or args.fx2:
filters.append("commenter")
fobjs, filtered = usbrply.filters.runx(filters,
parsed,
argsj,
verbose=args.verbose)
if args.verbose:
print("")
print("")
print("")
print("PASS: serial parse")
j = parsers.jgen2j(filtered)
fvidpid = fobjs["vidpid"]
"""
print(len(j))
import json
print(json.dumps(j,
sort_keys=True,
indent=4,
separators=(',', ': ')))
# print(fvidpid.keep_device, gen.arg_device)
"""
# FIXME: device-hi is faster, so kind of messy
device = gen.arg_device
vid, pid = fvidpid.device2vidpid[device ]
# print("%04X:%04X" % (vid, pid))
# FIXME: allow user to force parser
if vid == 0x0403:
fparser = sparsers.FT2232CParser
else:
raise Exception("Unknown device" % "%04X:%04X" % (vid, pid))
txtj = fparser(argsj=argsj).run(j)
ofmt = args.ofmt
if ofmt is None:
ofmt = "libusb-py"
printer = {
'text': sprinters.TextFT2232CPrinter,
'libusb-py': sprinters.PythonFT2232CPrinter,
'json': sprinters.JSONSPrinter,
}[ofmt]
if args.verbose:
print("")
print("")
print("")
print("PASS: serial print")
printer(argsj=argsj).run(txtj)
if args.mpsee:
if args.verbose:
print("")
print("")
print("")
print("PASS: MPSSE parse")
mpssej = mpsse.MPSSEParser().run(txtj)
if args.verbose:
print("")
print("")
print("")
print("PASS: MPSSE print")
mpsse.MPSSETextPrinter(args).run(mpssej)
if __name__ == "__main__":
main()