-
Notifications
You must be signed in to change notification settings - Fork 1
/
decoder.py
280 lines (215 loc) · 7.77 KB
/
decoder.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
import sys
import numpy as np
import bitstruct
from bitstring import BitArray as ba
PREAMBLE = 0x55
STOP = 0x35
SYNC = [0xFF, 0x00, 0x33, 0x55, 0x53]
RAW_BYTE_LENGTH = 10
MANCH_ENC = [0xAA, 0xA9, 0xA6, 0xA5, 0x9A, 0x99, 0x96, 0x95,
0x6A, 0x69, 0x66, 0x65, 0x5A, 0x59, 0x56, 0x55]
HEADER_FLAGS = [0x0F, 0x0C, 0x0D, 0x0B, 0x27, 0x24, 0x25, 0x23,
0x47, 0x44, 0x45, 0x43, 0x17, 0x14, 0x15, 0x13]
CMD_CO2_PPM = 0x1212
class PacketException(Exception):
"""Base class for other exceptions"""
pass
class PreambleError(PacketException):
"""Raised when the preamble is not valid"""
pass
class SyncError(PacketException):
"""Raised when the sync byes are not valid"""
pass
class EndOfPacketError(PacketException):
"""Raised when the preamble is not valid"""
pass
class ManchesterDecodeError(PacketException):
"""Raised when the preamble is not valid"""
pass
class ChecksumError(PacketException):
"""Raised when the preamble is not valid"""
pass
class RF15Decoder:
raw_message = ''
decoded_message = []
header = 0
flags = 0
type = ''
addr0 = 0
addr1 = 0
addr2 = 0
param0 = 0
param1 = 0
cmd = 0
pkt_length = 0
payload = []
def __init__(self):
pass
def _hex_list_to_int(self, fmt, hexlist):
return bitstruct.unpack('fmt', bytearray(hexlist))
def _pairwise(self, it):
it = iter(it)
while True:
try:
yield next(it), next(it)
except StopIteration:
return
def _printf(self, format, *args):
sys.stdout.write(format % args)
sys.stdout.flush()
def _is_information(self, flags):
return flags & 0x20
def _is_request(self, flags):
return flags & 0x08
def _is_response(self, flags):
return flags & 0x10
def _is_write(self, flags):
return flags & 0x40
def _has_addr0(self, flags):
return flags & 0x01
def _has_addr1(self, flags):
return flags & 0x02
def _has_addr2(self, flags):
return flags & 0x04
def _has_param0(self, header):
return header & 0x02
def _has_param1(self, header):
return header & 0x01
def prepare_message(self, rawmessage) -> list:
"""
Parse the raw bitstring to decoded raw bytes with start stop bits removed and endianness fixed
:param rawmessage:
:return: list of bytes
"""
if type(rawmessage) == ba:
packet = rawmessage
else:
packet = ba(bin=rawmessage)
try:
# Save raw bitstream
self.raw_message = packet.bin
# Sync on preamble
counter = 0
bit = packet[0]
while packet[counter] == bit:
bit = not bit
counter += 1
packet = packet << (counter - 2)
# Split bin string to raw packets
message = [packet[i:i + RAW_BYTE_LENGTH] for i in range(0, len(packet), RAW_BYTE_LENGTH)]
# Remove start and stop bytes
message = [byte[1:9] for byte in message]
# Reverse bit order (Endianess is LSB)
message = [byte[::-1] for byte in message]
return message
except Exception:
raise PreambleError()
def extract_message(self, prepared_message: list) -> list:
"""
Remove preamble, syncwords and end word
:param prepared_message:
:return:
"""
# Remove sync words
for i, x in enumerate(SYNC):
if prepared_message[i].uint != x:
raise SyncError()
prepared_message = prepared_message[len(SYNC):len(prepared_message)]
# Remove stop and rest of bogus data in packet
index = prepared_message.index(ba(int=STOP, length=8))
prepared_message = prepared_message[:index]
# Convert to numpy uint8 datatype
prepared_message = [np.uint8(x.uint) for x in prepared_message]
return prepared_message
def man_decode_message(self, extracted_message: list) -> list:
"""
Manchester decode message
:param extracted_message:
:return:
"""
decoded = []
try:
for a, b in self._pairwise(extracted_message):
decoded.append((MANCH_ENC.index(a) << 4) + MANCH_ENC.index(b))
except ValueError:
raise ManchesterDecodeError()
self.decoded_message = decoded
return decoded
def parse_message(self, decoded_message: list):
# Verify checksum (sum of all data bytes + checksum byte should be 0)
checksum = 0
for x in decoded_message:
checksum = (checksum + x) & 0xFF
if checksum != 0:
raise ChecksumError()
# Parse Header
self.header = decoded_message[0]
self.flags = HEADER_FLAGS[(self.header >> 2) & 0x0F]
del decoded_message[0]
if self._is_information(self.flags):
self._printf("--- INF --- ")
self.type = 'INF'
if self._is_request(self.flags):
self._printf("--- REQ --- ")
self.type = 'REQ'
if self._is_response(self.flags):
self._printf("--- RSP --- ")
self.type = 'RSP'
if self._is_write(self.flags):
self._printf("--- WRT --- ")
self.type = 'WRT'
# Parse optional fields
if self._has_addr0(self.flags):
self.addr0 = (decoded_message[0] << 16) | (decoded_message[0] << 8) | decoded_message[0]
self._printf('%02hu:%06lu ', np.uint8(self.addr0 >> 18) & 0x3F, self.addr0 & 0x3FFFF)
decoded_message = decoded_message[3:]
else:
self._printf('--:------ ')
if self._has_addr1(self.flags):
self.addr1 = (decoded_message[0] << 16) | (decoded_message[0] << 8) | decoded_message[0]
self._printf('%02hu:%06lu ', np.uint8(self.addr1 >> 18) & 0x3F, self.addr1 & 0x3FFFF)
decoded_message = decoded_message[3:]
else:
self._printf('--:------ ')
if self._has_addr2(self.flags):
self.addr2 = (decoded_message[0] << 16) | (decoded_message[0] << 8) | decoded_message[0]
self._printf('%02hu:%06lu ', np.uint8(self.addr2 >> 18) & 0x3F, self.addr2 & 0x3FFFF)
decoded_message = decoded_message[3:]
else:
self._printf('--:------ ')
if self._has_param0(self.header):
param0 = decoded_message[0]
self._printf('%03hu ', param0)
decoded_message = decoded_message[1:]
else:
self._printf('--- ')
if self._has_param1(self.header):
param1 = decoded_message[0]
self._printf('%03hu ', param1)
decoded_message = decoded_message[1:]
else:
self._printf('--- ')
# Parse Command
self.cmd = (decoded_message[0] << 8) | decoded_message[0]
self._printf('0x%04X ', self.cmd)
decoded_message = decoded_message[2:]
# Parse Length
self.pkt_length = decoded_message[0]
self._printf('%03hu ', self.pkt_length)
decoded_message = decoded_message[1:]
# Get Payload
self.payload = decoded_message[0:self.pkt_length]
for x in self.payload:
self._printf('%02hX', x)
# End of packet
self._printf('\n')
def decode(self, message):
message = self.prepare_message(message)
message = self.extract_message(message)
message = self.man_decode_message(message)
self.parse_message(message)
try:
if self.cmd == CMD_CO2_PPM:
self._printf('CO2 PPM: %i\n', self._hex_list_to_int('u24', self.payload))
except Exception as e:
pass