Остання активність 1 month ago

Sik Radio / RTBBOX Packet Decoder

Версія 51b85c8983945eab234ab9d76b5692f4463962d1

decoder.py Неформатований
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
22import struct, crc16
23
24from binascii import hexlify
25
26SYNCB0 = 0x2D
27SYNCB1 = 0xD4
28
29f = open("output.bin")
30data = f.read()
31f.close()
32
33pn9 = [
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, 0x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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
68def 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
76def 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
87def 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
95def bitsToByte(bits):
96 return int("0b" + "".join(str(x) for x in bits), 2)
97
98def 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
104def 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
135preamble = [ int(x) for x in ("10" * 4 * 4)]
136
137bits = [ ord(x) for x in data ]
138offset = 0
139syncWord = False
140
141skipHeads = 0
142skipped = 0
143
144while 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
180