Dernière activité 1 month ago

Sik Radio / RTBBOX Packet Decoder

racerxdl's Avatar Lucas Teske a révisé ce gist 7 years ago. Aller à la révision

1 file changed, 241 insertions

decoder-with-manchester.py(fichier créé)

@@ -0,0 +1,241 @@
1 + #!/usr/bin/env python
2 +
3 + # 64 bit preamble (16 bytes)
4 + # Reserved Reserved txdtrtscale enphpwdn manppol enmaninv enmanch enwhite
5 + # EZRADIOPRO_MODULATION_MODE_CONTROL_1 = 0x0D = 0b00 001101
6 + # trclk[1] trclk[0] dtmod[1] dtmod[0] eninv fd[8] modtyp[1] modtyp[0]
7 + # EZRADIOPRO_HEADER_CONTROL_2IOPRO_MODULATION_MODE_CONTROL_2 = 0x23 = 0b00100011
8 + # EZRADIOPRO_HEADER_CONTROL_2 = EZRADIOPRO_HDLEN_2BYTE | EZRADIOPRO_SYNCLEN_2BYTE
9 + # EZRADIOPRO_DATA_ACCESS_CONTROL = EZRADIOPRO_ENPACTX | EZRADIOPRO_ENPACRX | EZRADIOPRO_ENCRC | EZRADIOPRO_CRC_16
10 + # Sync Word: 2A D4
11 + # id = 25
12 + # register_write(EZRADIOPRO_TRANSMIT_HEADER_3, id >> 8);
13 + # register_write(EZRADIOPRO_TRANSMIT_HEADER_2, id & 0xFF);
14 +
15 + # txdtrtscale 0
16 + # enphpwdn 0
17 + # manppol 1
18 + # enmaninv 1
19 + # enmanch 1
20 + # enwhite 1
21 +
22 + import struct, crc16, sys
23 +
24 + from binascii import hexlify
25 +
26 + SYNCB0 = 0x2D
27 + SYNCB1 = 0xD4
28 +
29 + f = open("output.bin")
30 + data = f.read()
31 + f.close()
32 +
33 + pn9 = [
34 + 0x0f, 0x70, 0xb3, 0x6f, 0x43, 0x98, 0x48, 0xae,
35 + 0xbc, 0x97, 0x38, 0x1d, 0xd3, 0xd4, 0xa0, 0x55,
36 + 0x7d, 0x68, 0x37, 0x6d, 0x60, 0xbb, 0xe3, 0xcd,
37 + 0x35, 0xc6, 0x8b, 0xfa, 0x58, 0xa6, 0x30, 0x19,
38 + 0x95, 0x93, 0xf6, 0x92, 0x6f, 0xcb, 0x50, 0xa2,
39 + 0x76, 0x5e, 0xc3, 0x54, 0xe4, 0x31, 0x08, 0x04,
40 + 0x46, 0x47, 0x56, 0xc7, 0x12, 0xa3, 0x67, 0xcf,
41 + 0x16, 0xe5, 0x20, 0x99, 0xd1, 0xf7, 0x83, 0xfe,
42 + 0x1e, 0xe1, 0x66, 0xde, 0x87, 0x30, 0x91, 0x5d,
43 + 0x79, 0x2e, 0x70, 0x3b, 0xa7, 0xa9, 0x40, 0xaa,
44 + 0xfa, 0xd0, 0x6e, 0xda, 0xc1, 0x77, 0xc7, 0x9a,
45 + 0x6b, 0x8d, 0x17, 0xf4, 0xb1, 0x4c, 0x60, 0x33,
46 + 0x2b, 0x27, 0xed, 0x24, 0xdf, 0x96, 0xa1, 0x44,
47 + 0xec, 0xbd, 0x86, 0xa9, 0xc8, 0x62, 0x10, 0x08,
48 + 0x8c, 0x8e, 0xad, 0x8e, 0x25, 0x46, 0xcf, 0x9e,
49 + 0x2d, 0xca, 0x41, 0x33, 0xa3, 0xef, 0x07, 0xfc,
50 + 0x3d, 0xc2, 0xcd, 0xbd, 0x0e, 0x61, 0x22, 0xba,
51 + 0xf2, 0x5c, 0xe0, 0x77, 0x4f, 0x52, 0x81, 0x55,
52 + 0xf5, 0xa0, 0xdd, 0xb5, 0x82, 0xef, 0x8f, 0x34,
53 + 0xd7, 0x1a, 0x2f, 0xe9, 0x62, 0x98, 0xc0, 0x66,
54 + 0x56, 0x4f, 0xda, 0x49, 0xbf, 0x2d, 0x42, 0x89,
55 + 0xd9, 0x7b, 0x0d, 0x53, 0x90, 0xc4, 0x20, 0x11,
56 + 0x19, 0x1d, 0x5b, 0x1c, 0x4a, 0x8d, 0x9f, 0x3c,
57 + 0x5b, 0x94, 0x82, 0x67, 0x47, 0xde, 0x0f, 0xf8,
58 + 0x7b, 0x85, 0x9b, 0x7a, 0x1c, 0xc2, 0x45, 0x75,
59 + 0xe4, 0xb9, 0xc0, 0xee, 0x9e, 0xa5, 0x02, 0xab,
60 + 0xeb, 0x41, 0xbb, 0x6b, 0x05, 0xdf, 0x1e, 0x69,
61 + 0xae, 0x34, 0x5f, 0xd2, 0xc5, 0x31, 0x80, 0xcc,
62 + 0xac, 0x9f, 0xb4, 0x93, 0x7e, 0x5a, 0x85, 0x13,
63 + 0xb2, 0xf6, 0x1a, 0xa7, 0x21, 0x88, 0x40, 0x22,
64 + 0x32, 0x3a, 0xb6, 0x38, 0x95, 0x1b, 0x3e, 0x78,
65 + 0xb7, 0x29, 0x04, 0xce, 0x8f, 0xbc
66 + ]
67 +
68 + #define POLY_IEC_16 (0x5B93)
69 + #define POLY_BAICHEVA (0x90D9)
70 + #define POLY_CRC_16 (0x8005)
71 + #define POLY_CCITT_16 (0x1021)
72 +
73 + manchTable = {
74 + "00": 'E',
75 + "01": 0,
76 + "10": 1,
77 + "11": 'E',
78 + }
79 +
80 + manchInvTable = {
81 + "00": 'E',
82 + "01": 1,
83 + "10": 0,
84 + "11": 'E',
85 + }
86 +
87 + def parseTDMControl(u16):
88 + '''
89 + window: 13
90 + command: 1
91 + bonus: 1
92 + resend: 1
93 + '''
94 + u16 = struct.unpack(">H", u16)[0]
95 + window = u16 & 0xFFF8
96 + command = (u16 & 4) > 0
97 + bonus = (u16 & 2) > 0
98 + resend = (u16 & 1) > 0
99 +
100 + return window, command, bonus, resend
101 +
102 + def reverseBits(n):
103 + result = 0
104 + for i in range(8):
105 + result <<= 1
106 + result |= n & 1
107 + n >>= 1
108 + return result
109 +
110 + def updatecrc16(acc, input):
111 + POLY = 0x8005
112 + acc = (acc ^ (input << 8)) & 0xFFFF
113 + for i in range(8):
114 + if acc & 0x8000 == 0x8000:
115 + acc = (acc << 1) & 0xFFFF
116 + acc ^= POLY
117 + else:
118 + acc = (acc << 1) & 0xFFFF
119 + return acc & 0xFFFF
120 +
121 + def crc16(data, init=0):
122 + data = bytearray(data)
123 + acc = init
124 + for i in range(len(data)):
125 + acc = updatecrc16(acc, data[i])
126 +
127 + return acc
128 +
129 + def bitsToByte(bits):
130 + return int("0b" + "".join(str(x) for x in bits), 2)
131 +
132 + def manchesterDecode(data):
133 + out = []
134 + #print data
135 + for i in range(len(data)/2):
136 + b0 = str(data[i*2])
137 + b1 = str(data[(i*2)+1])
138 + out.append(manchTable["%s%s" %(b0, b1)])
139 + # out.append(manchInvTable["%s%s" %(b0, b1)])
140 + return out
141 +
142 + def derandomize(data, offset=0):
143 + data = bytearray(data)
144 + for i in range(len(data) - offset):
145 + data[i+offset] = data[i+offset] ^ pn9[i%len(pn9)]
146 + return data
147 +
148 + def parseFrame(bits):
149 + s = bits[8*2:8*5]
150 + data = bytearray(" " * 3)
151 + for i in range(3):
152 + data[i] = bitsToByte(s[8*i:8*(i+1)]) ^ pn9[i]
153 +
154 + netId, size = struct.unpack(">HB", data)
155 + bitSize = size * 8
156 + if len(bits) - 5*8 < bitSize:
157 + print "Not enough bits."
158 + exit(1)
159 +
160 + consumed = bitSize + 8*5
161 +
162 + frameBits = bits[8*5:]
163 + frame = bytearray(" " * size)
164 +
165 + crcframe = bytearray(" " * (size + 3 + 2))
166 + crcFrameBits = bits[8*2:]
167 +
168 + for i in range(size + 3 + 2):
169 + crcframe[i] = bitsToByte(crcFrameBits[8*i:8*(i+1)]) ^ pn9[i % len(pn9)]
170 +
171 + crc = struct.unpack(">H", crcframe[len(crcframe)-2:])[0]
172 + frame = crcframe[:len(crcframe)-2]
173 + crc = crc16(frame) == crc
174 + frame = frame[3:]
175 +
176 + window, command, bonus, resend = parseTDMControl(frame[len(frame)-2:])
177 + frame = frame[:len(frame)-2]
178 +
179 + return netId, size, consumed, frame, crc, window, command, bonus, resend
180 +
181 +
182 + preamble = [ int(x) for x in ("10" * 4 * 4)]
183 +
184 + bits = [ ord(x) for x in data ]
185 + offset = 0
186 + syncWord = False
187 +
188 + skipHeads = 0
189 + skipped = 0
190 +
191 + while len(bits) > 0:
192 + if len(bits) <= len(preamble):
193 + #print "None found"
194 + exit(1)
195 + for i in range(len(bits) - len(preamble)):
196 + found = True
197 + for z in range(len(preamble)):
198 + if bits[i+z] != preamble[z]:
199 + found = False
200 + break
201 + if found:
202 + offset = i
203 + break
204 +
205 + #print "Preamble start at %s" %offset
206 +
207 + offset += len(preamble)
208 + bits = bits[offset:]
209 + offset = 0
210 +
211 + #print bits[:64]
212 +
213 + #print "Searching for sync word"
214 + searchLen = 512
215 + for i in range(len(bits)-32):
216 + b0 = manchesterDecode(bits[i:i+16])
217 + b1 = manchesterDecode(bits[i+16:i+32])
218 +
219 + if 'E' in b0 or 'E' in b1: # Manchester Unaligned
220 + continue
221 +
222 + if bitsToByte(b0) == SYNCB0 and bitsToByte(b1) == SYNCB1:
223 + partial = bits[i:128*1024]
224 + partial = manchesterDecode(partial)
225 + netId, size, consumed, frame, crc, window, command, bonus, resend = parseFrame(partial)
226 + if crc:
227 + # print "------------------------------"
228 + # print " HexFrame: %s" %hexlify(frame)
229 + # print " Frame: %s" %frame
230 + # print " Window: %s"%window
231 + # print " Command: %s"%command
232 + # print " Bonus: %s"%bonus
233 + # print " Resend: %s"%resend
234 + # print "------------------------------"
235 + if command:
236 + sys.stdout.write(frame)
237 + bits = bits[consumed:]
238 + break
239 +
240 + if i > searchLen:
241 + break

racerxdl's Avatar Lucas Teske a révisé ce gist 7 years ago. Aller à la révision

1 file changed, 179 insertions

decoder.py(fichier créé)

@@ -0,0 +1,179 @@
1 + #!/usr/bin/env python
2 +
3 + # 64 bit preamble (16 bytes)
4 + # Reserved Reserved txdtrtscale enphpwdn manppol enmaninv enmanch enwhite
5 + # EZRADIOPRO_MODULATION_MODE_CONTROL_1 = 0x0D = 0b00 001101
6 + # trclk[1] trclk[0] dtmod[1] dtmod[0] eninv fd[8] modtyp[1] modtyp[0]
7 + # EZRADIOPRO_HEADER_CONTROL_2IOPRO_MODULATION_MODE_CONTROL_2 = 0x23 = 0b00100011
8 + # EZRADIOPRO_HEADER_CONTROL_2 = EZRADIOPRO_HDLEN_2BYTE | EZRADIOPRO_SYNCLEN_2BYTE
9 + # EZRADIOPRO_DATA_ACCESS_CONTROL = EZRADIOPRO_ENPACTX | EZRADIOPRO_ENPACRX | EZRADIOPRO_ENCRC | EZRADIOPRO_CRC_16
10 + # Sync Word: 2A D4
11 + # id = 25
12 + # register_write(EZRADIOPRO_TRANSMIT_HEADER_3, id >> 8);
13 + # register_write(EZRADIOPRO_TRANSMIT_HEADER_2, id & 0xFF);
14 +
15 + # txdtrtscale 0
16 + # enphpwdn 0
17 + # manppol 1
18 + # enmaninv 1
19 + # enmanch 0
20 + # enwhite 1
21 +
22 + import struct, crc16
23 +
24 + from binascii import hexlify
25 +
26 + SYNCB0 = 0x2D
27 + SYNCB1 = 0xD4
28 +
29 + f = open("output.bin")
30 + data = f.read()
31 + f.close()
32 +
33 + pn9 = [
34 + 0x0f, 0x70, 0xb3, 0x6f, 0x43, 0x98, 0x48, 0xae,
35 + 0xbc, 0x97, 0x38, 0x1d, 0xd3, 0xd4, 0xa0, 0x55,
36 + 0x7d, 0x68, 0x37, 0x6d, 0x60, 0xbb, 0xe3, 0xcd,
37 + 0x35, 0xc6, 0x8b, 0xfa, 0x58, 0xa6, 0x30, 0x19,
38 + 0x95, 0x93, 0xf6, 0x92, 0x6f, 0xcb, 0x50, 0xa2,
39 + 0x76, 0x5e, 0xc3, 0x54, 0xe4, 0x31, 0x08, 0x04,
40 + 0x46, 0x47, 0x56, 0xc7, 0x12, 0xa3, 0x67, 0xcf,
41 + 0x16, 0xe5, 0x20, 0x99, 0xd1, 0xf7, 0x83, 0xfe,
42 + 0x1e, 0xe1, 0x66, 0xde, 0x87, 0x30, 0x91, 0x5d,
43 + 0x79, 0x2e, 0x70, 0x3b, 0xa7, 0xa9, 0x40, 0xaa,
44 + 0xfa, 0xd0, 0x6e, 0xda, 0xc1, 0x77, 0xc7, 0x9a,
45 + 0x6b, 0x8d, 0x17, 0xf4, 0xb1, 0x4c, 0x60, 0x33,
46 + 0x2b, 0x27, 0xed, 0x24, 0xdf, 0x96, 0xa1, 0x44,
47 + 0xec, 0xbd, 0x86, 0xa9, 0xc8, 0x62, 0x10, 0x08,
48 + 0x8c, 0x8e, 0xad, 0x8e, 0x25, 0x46, 0xcf, 0x9e,
49 + 0x2d, 0xca, 0x41, 0x33, 0xa3, 0xef, 0x07, 0xfc,
50 + 0x3d, 0xc2, 0xcd, 0xbd, 0x0e, 0x61, 0x22, 0xba,
51 + 0xf2, 0x5c, 0xe0, 0x77, 0x4f, 0x52, 0x81, 0x55,
52 + 0xf5, 0xa0, 0xdd, 0xb5, 0x82, 0xef, 0x8f, 0x34,
53 + 0xd7, 0x1a, 0x2f, 0xe9, 0x62, 0x98, 0xc0, 0x66,
54 + 0x56, 0x4f, 0xda, 0x49, 0xbf, 0x2d, 0x42, 0x89,
55 + 0xd9, 0x7b, 0x0d, 0x53, 0x90, 0xc4, 0x20, 0x11,
56 + 0x19, 0x1d, 0x5b, 0x1c, 0x4a, 0x8d, 0x9f, 0x3c,
57 + 0x5b, 0x94, 0x82, 0x67, 0x47, 0xde, 0x0f, 0xf8,
58 + 0x7b, 0x85, 0x9b, 0x7a, 0x1c, 0xc2, 0x45, 0x75,
59 + 0xe4, 0xb9, 0xc0, 0xee, 0x9e, 0xa5, 0x02, 0xab,
60 + 0xeb, 0x41, 0xbb, 0x6b, 0x05, 0xdf, 0x1e, 0x69,
61 + 0xae, 0x34, 0x5f, 0xd2, 0xc5, 0x31, 0x80, 0xcc,
62 + 0xac, 0x9f, 0xb4, 0x93, 0x7e, 0x5a, 0x85, 0x13,
63 + 0xb2, 0xf6, 0x1a, 0xa7, 0x21, 0x88, 0x40, 0x22,
64 + 0x32, 0x3a, 0xb6, 0x38, 0x95, 0x1b, 0x3e, 0x78,
65 + 0xb7, 0x29, 0x04, 0xce, 0x8f, 0xbc
66 + ]
67 +
68 + def reverseBits(n):
69 + result = 0
70 + for i in range(8):
71 + result <<= 1
72 + result |= n & 1
73 + n >>= 1
74 + return result
75 +
76 + def updatecrc16(acc, input):
77 + POLY = 0x8005
78 + acc = (acc ^ (input << 8)) & 0xFFFF
79 + for i in range(8):
80 + if acc & 0x8000 == 0x8000:
81 + acc = (acc << 1) & 0xFFFF
82 + acc ^= POLY
83 + else:
84 + acc = (acc << 1) & 0xFFFF
85 + return acc & 0xFFFF
86 +
87 + def crc16(data, init=0):
88 + data = bytearray(data)
89 + acc = init
90 + for i in range(len(data)):
91 + acc = updatecrc16(acc, data[i])
92 +
93 + return acc
94 +
95 + def bitsToByte(bits):
96 + return int("0b" + "".join(str(x) for x in bits), 2)
97 +
98 + def derandomize(data, offset=0):
99 + data = bytearray(data)
100 + for i in range(len(data) - offset):
101 + data[i+offset] = data[i+offset] ^ pn9[i%len(pn9)]
102 + return data
103 +
104 + def parseFrame(bits):
105 + s = bits[8*2:8*5]
106 + data = bytearray(" " * 3)
107 + for i in range(3):
108 + data[i] = bitsToByte(s[8*i:8*(i+1)]) ^ pn9[i]
109 +
110 + netId, size = struct.unpack(">HB", data)
111 + bitSize = size * 8
112 + if len(bits) - 5*8 < bitSize:
113 + print "Not enough bits."
114 + exit(1)
115 +
116 + consumed = bitSize + 8*5
117 +
118 + frameBits = bits[8*5:]
119 + frame = bytearray(" " * size)
120 +
121 + crcframe = bytearray(" " * (size + 3 + 2))
122 + crcFrameBits = bits[8*2:]
123 +
124 + for i in range(size + 3 + 2):
125 + crcframe[i] = bitsToByte(crcFrameBits[8*i:8*(i+1)]) ^ pn9[i % len(pn9)]
126 +
127 + crc = struct.unpack(">H", crcframe[len(crcframe)-2:])[0]
128 + frame = crcframe[:len(crcframe)-2]
129 + crc = crc16(frame) == crc
130 + frame = crcframe[3:]
131 +
132 + return netId, size, consumed, frame, crc
133 +
134 +
135 + preamble = [ int(x) for x in ("10" * 4 * 4)]
136 +
137 + bits = [ ord(x) for x in data ]
138 + offset = 0
139 + syncWord = False
140 +
141 + skipHeads = 0
142 + skipped = 0
143 +
144 + while len(bits) > 0:
145 + if len(bits) <= len(preamble):
146 + #print "None found"
147 + exit(1)
148 + for i in range(len(bits) - len(preamble)):
149 + found = True
150 + for z in range(len(preamble)):
151 + if bits[i+z] != preamble[z]:
152 + found = False
153 + break
154 + if found:
155 + offset = i
156 + break
157 +
158 + #print "Preamble start at %s" %offset
159 +
160 + offset += len(preamble)
161 + bits = bits[offset:]
162 + offset = 0
163 +
164 + #print "Searching for sync word"
165 + searchLen = 512
166 + for i in range(len(bits)-16):
167 + b0 = bits[i:i+8]
168 + b1 = bits[i+8:i+16]
169 + if bitsToByte(b0) == SYNCB0 and bitsToByte(b1) == SYNCB1:
170 + #print "FOUND"
171 + netId, size, consumed, frame, crc = parseFrame(bits[i:])
172 + if crc:
173 + print " HexFrame: %s" %hexlify(frame)
174 + print " Frame: %s" %frame
175 + bits = bits[consumed:]
176 + break
177 +
178 + if i > searchLen:
179 + break
Plus récent Plus ancien