-
Notifications
You must be signed in to change notification settings - Fork 79
/
sniffer_binary.py
executable file
·60 lines (53 loc) · 1.83 KB
/
sniffer_binary.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
#!/usr/bin/env python3
# UWB binary sniffer decoder
#
# The LPS node should be in sniffer mode already, it will be switched to
# binary mode by this script.
import sys
import struct
import serial
import binascii
import yaml
import time
if len(sys.argv) < 2:
print("usage: {} <sniffer serial port> [format]".format(sys.argv[0]))
print(" Possible format: human (default), csv, yaml")
sys.exit(1)
ser = serial.Serial(sys.argv[1], 9600)
outputFormat = 'human'
if len(sys.argv) > 2:
outputFormat = sys.argv[2].strip()
# Switch to binary mode
ser.write(b'b')
while True:
c = ser.read(1)
if c == b'\xbc':
ts = ser.read(5)
ts += b'\0\0\0'
ts = struct.unpack('<Q', ts)[0]
addrFrom, addrTo = struct.unpack('<BB', ser.read(2))
length = struct.unpack('<H', ser.read(2))[0]
if length > 1024:
continue
data = ser.read(length)
l2 = struct.unpack('<H', ser.read(2))[0]
if length == l2:
now = time.time()
if outputFormat == 'human':
print("@{:010x} from {} to {}: {}".format(ts, addrFrom, addrTo,
binascii.hexlify(data)
.decode('utf8')))
elif outputFormat == 'csv':
print("0x{:010x}, {}, {}, {}".format(ts, addrFrom, addrTo,
binascii.hexlify(data)
.decode('utf8')))
elif outputFormat == 'yaml':
print("---")
print(yaml.dump({'ts': ts, 'from': addrFrom,
'to': addrTo, 'data': data, 'rxSys': now},
Dumper=yaml.Dumper))
else:
sys.stderr.write("Error: Uknown output format: {}\n".format(
outputFormat))
else:
print("Out of sync!")