-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode.py
272 lines (240 loc) · 7.47 KB
/
decode.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
# LGV3 radio frame decoder
# See LICENSE file
# furrtek 2019/02
# usage: python decode.py [BBD_xxxx_low]
# if no file is provided, a fifo is created for grc
import sys, os, wave, struct
from time import gmtime, strftime
def compute_crc(data):
init = 0x1D0F
remainder = init
polynomial = 0x1021
for byte in data:
remainder ^= (byte << 8)
for bit in range(0, 8):
if (remainder & 0x8000):
remainder = ((remainder << 1) & 0xFFFF) ^ polynomial
else:
remainder = ((remainder << 1) & 0xFFFF)
return remainder ^ 0xFFFF
if len(sys.argv) == 2:
# Wav file provided
file_stub = sys.argv[1]
wav_file = wave.open(file_stub + ".wav", "rb")
sample_rate = wav_file.getframerate()
if (sample_rate != 38400):
print("Sample rate must be 38400Hz !")
exit()
n_samples = wav_file.getnframes()
print("Opened " + file_stub + ".wav - " + str(sample_rate) + "Hz, " + str(n_samples) + " samples")
mode = 0
else:
# Must create FIFO
file_stub = strftime("live_%Y-%m-%d_%H:%M:%S", gmtime())
if os.path.exists("fifo.fifo"):
os.remove("fifo.fifo")
os.mkfifo("fifo.fifo", 0666)
print("Created fifo, you can now run lgv3_rx_live")
fifo_file = open("fifo.fifo", "rb")
mode = 1
text_file = open("decoded_" + file_stub + ".txt", "w")
print("Raw decoded bytes will be written to decoded_" + file_stub + ".txt")
# Wave file isn't buffered because Python throws a memory error if it's too long to fit in waveData :(
#waveData = wav_file.readframes(n_samples)
#data = struct.unpack("<" + str(n_samples) + "h", waveData) # Signed short, little endian
ch = ""
shifter = " "
same_counter = 0
prev_sample = 0
prev_bit = 0
state = 0
tprint = 0
sample_timer = 0
while True:
if (mode == 1):
waveData = fifo_file.read(1000)
n_samples = len(waveData)
for i in range(0, n_samples):
if (mode == 0):
# Wav file mode
# Report progress every 5s of the input wav file
tprint += 1
if (tprint > sample_rate * 5):
tprint = 0
print(str(int(float(i) / n_samples * 100)) + "%")
waveData = wav_file.readframes(1)
data = int(struct.unpack("<h", waveData)[0])
#if int(data[i]) >= 0:
else:
# FIFO mode
data = waveData[i]
if ord(data) >= 0x80:
data = -1
else:
data = 0
if data >= 0:
sample = 1
else:
sample = 0
if sample != prev_sample:
# Got a transition, center of symbol is on next sample
sample_timer = 1
prev_sample = sample
if sample_timer > 0:
sample_timer -= 1
else:
sample_timer = 3 # Center of next symbol is in 4 samples
bit = sample
# Shift left and insert bit
shifter = shifter[1:20] + str(bit)
if state == 0:
# Waiting for preamble
if shifter == "01010101010101010101":
ts = i / float(38400)
print("\033[0;30;47mPreamble get @ sample %u (%.1f s)\033[0m" % (i, ts))
if (mode == 0):
text_file.write(str("%07.2fs" % ts).zfill(6) + " ")
else:
text_file.write(strftime("%H:%M:%S ", gmtime()))
state = 1
elif state == 1:
# Waiting for sync word start
if shifter[-2:] == "00":
#print("Sync get:"),
state = 2
elif state == 2:
# Waiting for sync word end (0x2DD4)
if (shifter[-16:] == "0010110111010100") :
#print(hex(int(shifter[-16:], 2)))
#text_file.write(hex(int(shifter[-16:], 2)) + " ")
# Init Manchester decoder variables
dm = "" # Decoded bitstream
oe = 0 # Odd/even bit flag
print("Raw:"),
state = 3
elif state == 3:
if oe == 1:
# Manchester decode
if prev_bit == 1 and bit == 0:
dm += "1"
elif prev_bit == 0 and bit == 1:
dm += "0"
else:
# Manchester coding error, assume end of packet
n_bytes = len(dm) // 8
pkt = []
for j in range(0, n_bytes):
byte = int(dm[j * 8:j * 8 + 8], 2)
pkt.append(byte)
hex_byte = format(byte, 'X').zfill(2)
print(hex_byte),
text_file.write(hex_byte + " ")
text_file.write("\n")
print("")
# Valid packets should be at least 3 bytes (L CRC CRC)
if len(pkt) >= 3:
packet_length = pkt[0]
crc_pkt = 0
if len(pkt) >= packet_length + 3:
crc_hi = pkt[packet_length + 1]
crc_lo = pkt[packet_length + 2]
crc_pkt = crc_hi * 256 + crc_lo
computed_crc = compute_crc(pkt[0:packet_length + 1])
print("Length: %u, CRC: %04X" % (packet_length, crc_pkt)),
if (crc_pkt == computed_crc):
print("\033[0;32m(GOOD)\033[0m")
else:
print("\033[0;31m(BAD: %04X)\033[0m" % computed_crc)
packet_type = pkt[1]
if (packet_type == 0x96):
if (len(pkt) >= 4):
joueur = pkt[3]
salle = pkt[2]
else:
joueur = 0
salle = 0
print("Report touche joueur %u (salle %u)" % (joueur, salle))
if (len(pkt) >= 12):
seconds = pkt[10] + pkt[11] * 256
minutes = seconds // 60
else:
minutes = 0
seconds = 0
seconds = seconds % 60
if (len(pkt) >= 12):
print("Temps ecoule: %u:%u" % (minutes, seconds))
elif packet_type == 0x0D:
print("Game over salle %u !" % pkt[6])
elif packet_type == 0x01:
print("Requete dump joueur %u ?" % pkt[4])
elif packet_type == 0x41:
if (len(pkt) >= 24):
seconds = pkt[22] + pkt[23] * 256
minutes = seconds // 60
seconds = seconds % 60
else:
minutes = 0
seconds = 0
if (len(pkt) >= 11):
salle = pkt[10]
joueur = pkt[4]
else:
salle = 0
joueur = 0
print("Dump joueur %u ?: salle %u, temps max %u:%u" % (joueur, salle, minutes, seconds))
elif packet_type == 0x02:
pseudo = ""
for t in range(10, 25):
pseudo += chr(pkt[t]).decode('latin1')
equipe = pkt[26]
seconds = pkt[28] + pkt[29] * 256
minutes = seconds // 60
seconds = seconds % 60
print("Config joueur %u: salle %u, pseudo '%s', equipe %u, temps max %u:%u" % (pkt[4], pkt[8], pseudo, equipe, minutes, seconds))
elif packet_type == 0x42:
if (len(pkt) >= 5):
joueur = pkt[4]
else:
joueur = 0
print("Ack config joueur %u" % joueur)
elif packet_type == 0x03:
print("Broadcast noms equipes salle %u:" % pkt[6])
n = (packet_length - 6) // 18
for p in range(0, n):
pseudo = ""
for t in range(2, 17):
pseudo += chr(pkt[7 + 18 * p + t]).decode('latin1') # Accents possibles
print(" %u : '%s'" % (pkt[7 + 18 * p + 1], pseudo))
elif packet_type == 0x0F:
if (len(pkt) >= 5):
joueur = pkt[4]
else:
joueur = 0
print("Req 0F? joueur %u" % joueur)
elif packet_type == 0x4F:
if (len(pkt) >= 5):
joueur = pkt[4]
else:
joueur = 0
print("Ack 0F? joueur %u" % joueur)
elif packet_type == 0x04:
if (len(pkt) >= 7):
salle = pkt[6]
else:
salle = 0
print("Broadcast pseudos salle %u:" % salle)
n = (packet_length - 6) // 18
for p in range(0, n):
s = 7 + 18 * p
pseudo = ""
for t in range(2, 17):
if len(pkt) >= s + t:
pseudo += chr(pkt[s + t]).decode('latin1')
print(" %u : '%s' type %u, equipe %u" % (pkt[s + 1], pseudo, pkt[s + 0], pkt[s + 18]))
print("")
state = 0
oe ^= 1
prev_bit = bit
if (mode == 0):
break
text_file.close()