-
Notifications
You must be signed in to change notification settings - Fork 0
/
lrf_traffic_tracer.py
executable file
·523 lines (372 loc) · 12.5 KB
/
lrf_traffic_tracer.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#!/usr/bin/python3
# PYTHON_ARGCOMPLETE_OK
"""Noptel LRF rangefinder sampler for the Flipper Zero
Version: 2.0
Companion utility to connect to the Flipper Zero CLI and decode LRF serial
traffic traces generated by the USB serial passthrough function
The utility needs the USB serial passthrough to be configured on channel 1, so
the Flipper Zero CLI is still available on channel 0
If the LRF class is not present or can't be imported, the utility performs basic
recognition of LRF commands sent to the LRF and responses sent back by the LRF
(standalone decoder)
If the LRF class is present and imported, the utility decodes all the frames in
full and displays the decoded values
Usage:
python lrf_traffic_tracer.py /dev/ttyACMx [-r|-s] (Linux)
python lrf_traffic_tracer.py COMx [-r|-s] (Windows)
/dev/ttyACMx or COMx is the COM port corresponding to the Flipper Zero's CLI
-r turns off the decoder and displays the traffic bytes raw instead
-s forces the use of the standalone decoder even if the LRF class is available
"""
## Parameters
#
cli_serial_device_baudrate = 921600 #bps
max_line_width = 79 #characters
## Modules
#
import re
import sys
import argparse
from time import sleep
from serial import Serial
import serial.tools.list_ports
# Optional bash completion support
try:
import argcomplete
has_argcomplete = True
except:
has_argcomplete = False
# Optional LRF class
try:
from lrfclass.full.lrf import LRF
has_lrfclass = True
except:
try:
from lrfclass.lrf import LRF
has_lrfclass = True
except:
has_lrfclass = False
## Defines
#
TO_LRF = 0
FROM_LRF = 1
DOT = b"."[0]
## Classes
#
class StandaloneBasicLRFTrafficDecoder:
"""Standalone basic LRF traffic decoder, for use when the LRF class is not
available
"""
## LRF command and response frame patterns
lrf_frame_patterns = (
# Command frame patterns
{
"SMM": b"\xc3.",
"SMM_WITH_STATUS": b"\x12.",
"SMM_LOW_VIS": b"\xd3.",
"QUICK_SMM": b"\xdd..",
"CMM": b"\xda..",
"CMM_BREAK": b"\xc6.",
"EXEC_RANGE_MEAS": b"\xcc....",
"STATUS_QUERY": b"\xc7.",
"SET_PTR_MODE": b"\xc5..",
"ASK_RANGE_WIN": b"\x30.",
"SET_MIN_RANGE": b"\x31...",
"SET_MAX_RANGE": b"\x32...",
"SET_BAUDRATE": b"\xc8..",
"SEND_ID_FRAME": b"\xc0.",
"SEND_INFO_FRAME": b"\xc2.",
"RST_RS_ERR_CTR": b"\xcb."
},
# Response frame patterns
{
"RESP_SMM": b"\x59\xc3.......",
"RESP_SMM_WITH_STATUS": b"\x59\x12........",
"RESP_SMM_LOW_VIS": b"\x59\xd3.......",
"RESP_QUICK_SMM": b"\x59\xdd.......",
"RESP_CMM": b"\x59\xda.......",
"RESP_CMM_BREAK": b"\x59\xc6\x3c.",
"RESP_EXEC_RANGE_MEAS": b"\x59\xcc....................",
"RESP_STATUS_QUERY": b"\x59\xc7....",
"RESP_SET_PTR_MODE": b"\x59\xc5\x3c.",
"RESP_ASK_RANGE_WIN": b"\x59\x30.....",
"RESP_SET_MIN_RANGE": b"\x59\x31\x3c.",
"RESP_SET_MAX_RANGE": b"\x59\x32\x3c.",
"RESP_SET_BAUDRATE": b"\x59\xc8\x3c.",
"RESP_SEND_ID_FRAME": b"\x59\xc0...............\x0d\x0a..............."
b"\x0d\x0a..........\x0d\x0a............"
b"\x0d\x0a..\x3a..\x3a..\x0d\x0a.",
"RESP_SEND_INFO_FRAME": b"\x59\xc2.......\x01........"
b"......................",
"RESP_RST_RS_ERR_CTR": b"\x59\xcb\x3c."
})
def __init__(self):
"""Initialize the decoder
"""
self.lrf_frame_patterns_li = [{k: len(self.lrf_frame_patterns[d][k]) - 1 \
for k in self.lrf_frame_patterns[d]} \
for d in (TO_LRF, FROM_LRF)]
self.bytes_buf = [[], []]
self.potential_frame_matches = [set(self.lrf_frame_patterns[d]) \
for d in (TO_LRF, FROM_LRF)]
self.frame_i = [0, 0]
self.chars_in_line = 0
def print_decoded_traffic(self, timestamp, d, data):
"""Decode traffic bytes and display the decoded frame names
"""
# Decode the bytes
for b in data:
# Add the byte to the frame buffer
self.bytes_buf[d].append(b)
# Check potential matching frames for this byte
for f in set(self.potential_frame_matches[d]):
# The potential matching frame is too small
if self.lrf_frame_patterns_li[d][f] < self.frame_i[d]:
self.potential_frame_matches[d].remove(f)
# Non-matching character in the potential matching frame
elif self.lrf_frame_patterns[d][f][self.frame_i[d]] not in (DOT, b):
self.potential_frame_matches[d].remove(f)
# Matching frame
elif self.lrf_frame_patterns_li[d][f] == self.frame_i[d]:
# The frame is an LRF response but the checksum is incorrect
if d == FROM_LRF and self._checksum(self.bytes_buf[d][:-1]) != \
self.bytes_buf[d][-1]:
self.potential_frame_matches[d].remove(f)
continue
# Print the name of the matching frame
if self.chars_in_line:
print()
print("{}: {}LRF: {}". format(timestamp, "<>"[d], f))
self.chars_in_line = 0
# Reset the bytes buffer and the set of potential matching frames
self.bytes_buf[d].clear()
self.frame_i[d] = 0
self.potential_frame_matches[d] = set(self.lrf_frame_patterns[d])
break
# No potential matches found
else:
# No potential matches left
if not self.potential_frame_matches[d]:
# Print the raw bytes
for b in self.bytes_buf[d]:
sb = " {:02x}".format(b)
lb = len(sb)
if not self.chars_in_line or \
self.chars_in_line + lb > max_line_width:
if self.chars_in_line:
print()
sh = "{}: {}LRF:".format(timestamp, "<>"[d])
print(sh, end = "")
self.chars_in_line = len(sh)
print(sb, end = "")
self.chars_in_line += lb
# Reset the bytes buffer and the set of potential matching frames
self.bytes_buf[d] = []
self.frame_i[d] = 0
self.potential_frame_matches[d] = set(self.lrf_frame_patterns[d])
continue
self.frame_i[d] += 1
def _checksum(self, data):
"""Calculate the checksum of a block of data
"""
return (sum([c for c in data]) & 0xff) ^ 0x50
class FullLRFTrafficDecoder:
"""LRF traffic decoder based on the LRF class that decodes all the frame types
and all the values they contain
"""
def __init__(self):
"""Initialize the decoder
"""
self.buf = [b"", b""]
self.nb_bytes_read = [0, 0]
self.direction = None
self.lrf = LRF()
self.lrf.last_command_read_memory_pagecnt = 0
self.chars_in_line = True
def _sim_read(self, nb_bytes, timeout):
"""Simulated serial read function
"""
offset = self.nb_bytes_read[self.direction]
self.nb_bytes_read[self.direction] += nb_bytes
return self.buf[self.direction][offset : self.nb_bytes_read[self.direction]]
def print_decoded_traffic(self, timestamp, d, data):
"""Decode traffic bytes and display the decoded frames
"""
self.direction = d
self.buf[d] += data
get_frame = self.lrf.get_ext_command if d == TO_LRF else \
self.lrf.get_lrf_response
# Try to decode frames until we run out of bytes to decode
while True:
try:
r = get_frame(altreadfct = self._sim_read)
# We don't have all the bytes yet
except TimeoutError:
self.nb_bytes_read[d] = 0
break
# The decoder threw an error: print the offending bytes and the error
except ValueError as e:
print("{}: {}LRF: {}: {}".
format(timestamp, "<>"[d], " ".join(
["{:02x}".format(v) \
for v in self.buf[d][:self.nb_bytes_read[d]]]),
e))
r = None
# If we got a decoded frame, print it
if r is not None:
print("{}: {}LRF: {}".format(timestamp, "<>"[d], r))
# Remove the read bytes from the buffer
self.buf[d] = self.buf[d][self.nb_bytes_read[d]:]
self.nb_bytes_read[d] = 0
class DummyLRFTrafficDecoder:
"""Decoder that doesn't decode anything, only prints the raw bytes
"""
def __init__(self):
"""Initialize the "decoder"
"""
self.chars_in_line = True
def print_decoded_traffic(self, timestamp, d, data):
"""Decode traffic bytes and display the decoded frame names
"""
# Print the raw bytes
for b in data:
sb = " {:02x}".format(b)
lb = len(sb)
if not self.chars_in_line or \
self.chars_in_line + lb > max_line_width:
if self.chars_in_line:
print()
sh = "{}: {}LRF:".format(timestamp, "<>"[d])
print(sh, end = "")
self.chars_in_line = len(sh)
print(sb, end = "")
self.chars_in_line += lb
## Routines
#
def device_completer(**kwargs):
"""Argcomplete completer that returns all serial port device names
"""
return sorted([p[0] for p in serial.tools.list_ports.comports(
include_links = True)])
## Main routine
#
def main():
# Parse the command line arguments
argparser = argparse.ArgumentParser()
argparser.add_argument(
"cli_serial_device",
help = "Serial device corresponding to the Flipper Zero CLI "
"(e.g. /dev/ttyACM0 on Linux/Unix, COM1 on Windows)",
type = str
).completer = device_completer
mutexargs = argparser.add_mutually_exclusive_group(required = False)
mutexargs.add_argument(
"-s", "--standalone-decoder",
help = "Use the standalone decoder even if the LRF class is "
"available",
action = "store_true"
)
mutexargs.add_argument(
"-r", "--raw",
help = "Don't decode the LRF traffic and simply display the "
"raw bytes",
action = "store_true"
)
if has_argcomplete:
argcomplete.autocomplete(argparser, always_complete_options = False)
args = argparser.parse_args()
print()
print("LRF traffic tracer")
print("------------------")
print()
# Initialize the correct traffic decoder
if args.raw:
decoder = DummyLRFTrafficDecoder()
elif has_lrfclass and not args.standalone_decoder:
decoder = FullLRFTrafficDecoder()
print("Using the full LRF frame decoder")
else:
decoder = StandaloneBasicLRFTrafficDecoder()
print("Using the standalone LRF frame decoder")
# Precompiled regex for a trace log line from the USB serial passthrough
re_passthru_log_line = re.compile("([0-9]+) .*\[noptel_lrf_sampler\] .*"
"([<>])LRF:((?: [0-9a-zA-Z]{2})+)")
print("Ctrl-C to exit...")
print()
clidev = None
errcode = 0
try:
# Run until stopped by Ctrl-C
while True:
# Do we arrive here from a previous error? */
if errcode:
# Close the Flipper Zero's CLI device and wait a bit before trying to
# open it again
try:
clidev.close()
except:
pass
clidev = None
print("Retrying in 2 seconds...")
print()
sleep(2)
# Try to open the Flipper Zero's CLI device
if clidev is None:
try:
clidev = Serial(args.cli_serial_device, cli_serial_device_baudrate,
timeout = 0.2)
except Exception as e:
print("Error opening {}: {}".format(args.cli_serial_device, e))
errcode = -1
continue
print("{} opened at {} bps".
format(args.cli_serial_device, cli_serial_device_baudrate))
errcode = 0
try:
# Read one line from the CLI
while True:
l = clidev.readline().rstrip().decode("ascii")
if l:
break
# Timeout: send LF to the console if the curent line is not empty
if decoder.chars_in_line:
print()
decoder.chars_in_line = 0
except Exception as e:
print("Error reading from {}: {}".format(args.cli_serial_device, e))
errcode = -1
continue
# Did the CLI start or restart?
if l.startswith("Welcome"):
# Enable trace logging
try:
clidev.write(b"log trace\r")
print("Trace log started")
print()
except Exception as e:
print("Error writing to {}: {}".format(args.cli_serial_device, e))
errcode = -1
continue
# Catch LRF serial traffic lines from the USB serial passthrough
m = re_passthru_log_line.match(l)
if m:
timestamp = int(m[1])
d = TO_LRF if m[2] == ">" else FROM_LRF
data = bytes([int(v, 16) for v in m[3].split()])
# Decode the LRF traffic bytes and print the decoded information
decoder.print_decoded_traffic(timestamp, d, data);
except KeyboardInterrupt:
print()
print("Interrupted")
finally:
# Close the Flipper Zero's CLI
if clidev is not None:
try:
clidev.close()
except:
pass
return errcode
## Main program
#
if __name__ == "__main__":
sys.exit(main())