@@ -26,7 +26,7 @@ class Packet(object):
26
26
PACKET_TYPE_ACK = 0b010
27
27
PACKET_TYPE_CON = 0b100
28
28
29
- MAX_BODY_SEGMENT_LEN = 23
29
+ MAX_BODY_SEGMENT_LEN = 25
30
30
MAX_CON_BODY_SEGMENT_LEN = 30
31
31
32
32
PACKET_TYPE_STRINGS = {
@@ -42,7 +42,6 @@ def __init__(self, data=""):
42
42
self .packet_type = None
43
43
self .body = None
44
44
self .body_len = None
45
- self .message_type = None
46
45
self .crc = None
47
46
if len (data ) < 10 :
48
47
return
@@ -58,17 +57,15 @@ def __init__(self, data=""):
58
57
self .pod_address_2 = data [5 :9 ].encode ("hex" )
59
58
60
59
if (self .packet_type != Packet .PACKET_TYPE_CON and
61
- self .packet_type != Packet .PACKET_TYPE_ACK and len (data ) > 13 ):
60
+ self .packet_type != Packet .PACKET_TYPE_ACK and len (data ) > 11 ):
62
61
self .byte9 = ord (data [9 ])
63
62
self .body_len = ord (data [10 ])
64
- self .message_type = data [11 :13 ]
65
- segment_len = min (Packet .MAX_BODY_SEGMENT_LEN ,self .body_len ,len (data )- 14 )
66
- self .body = data [13 :(13 + segment_len )]
67
- self .crc = ord (data [13 + segment_len ])
63
+ segment_len = min (Packet .MAX_BODY_SEGMENT_LEN ,self .body_len ,len (data )- 12 )
64
+ self .body = data [11 :(11 + segment_len )]
65
+ self .crc = ord (data [11 + segment_len ])
68
66
else :
69
67
self .byte9 = None
70
68
self .body_len = 0
71
- self .message_type = None
72
69
self .body = None
73
70
self .crc = ord (data [9 ])
74
71
@@ -98,6 +95,7 @@ def assign_from_string(self, line):
98
95
self .pod_address_2 = None
99
96
self .byte9 = None
100
97
self .received_at = dateutil .parser .parse (elems [0 ])
98
+ legacy_mtype = ""
101
99
for elem in elems [1 :]:
102
100
(key ,v ) = elem .split (':' )
103
101
if key == "ID1" :
@@ -118,10 +116,10 @@ def assign_from_string(self, line):
118
116
self .byte9 = int (v ,16 )
119
117
if key == "BLEN" :
120
118
self .body_len = int (v )
121
- if key == "MTYPE" :
122
- self . message_type = v .decode ('hex' )
119
+ if key == "MTYPE" : # Legacy format
120
+ legacy_mtype = v .decode ('hex' )
123
121
if key == "BODY" :
124
- self .body = v .decode ('hex' )
122
+ self .body = legacy_mtype + v .decode ('hex' )
125
123
except ValueError :
126
124
self .body = None
127
125
except OverflowError :
@@ -140,7 +138,6 @@ def tx_data(self):
140
138
if self .packet_type != self .PACKET_TYPE_CON and self .body is not None :
141
139
data += chr (self .byte9 )
142
140
data += chr (self .body_len )
143
- data += self .message_type
144
141
data += self .body
145
142
data += chr (self .compute_crc_for (bytearray (data )))
146
143
return data
@@ -154,8 +151,6 @@ def __eq__(self, other):
154
151
return False
155
152
if self .packet_type != other .packet_type :
156
153
return False
157
- if self .message_type != other .message_type :
158
- return False
159
154
if self .body != other .body :
160
155
return False
161
156
if self .byte9 != other .byte9 :
@@ -170,7 +165,6 @@ def __hash__(self):
170
165
hash (self .pod_address_1 ),
171
166
hash (self .pod_address_2 ),
172
167
hash (self .packet_type ),
173
- hash (self .message_type ),
174
168
hash (self .body ),
175
169
hash (self .byte9 ),
176
170
hash (self .sequence )])
@@ -206,12 +200,11 @@ def __str__(self):
206
200
)
207
201
if self .body != None :
208
202
# All other packets with enough bytes to have a body
209
- return "%s ID2:%s B9:%02x BLEN:%s MTYPE:%s BODY:%s CRC:%02x" % (
203
+ return "%s ID2:%s B9:%02x BLEN:%s BODY:%s CRC:%02x" % (
210
204
base_str ,
211
205
self .pod_address_2 ,
212
206
self .byte9 ,
213
207
self .body_len ,
214
- self .message_type .encode ('hex' ),
215
208
self .body .encode ('hex' ),
216
209
crc ,
217
210
)
@@ -242,8 +235,6 @@ def as_dict(self):
242
235
if self .body is not None :
243
236
obj ["body" ] = self .body .encode ('hex' )
244
237
obj ["body_len" ] = self .body_len
245
- if self .message_type is not None :
246
- obj ["message_type" ] = self .message_type .encode ('hex' )
247
238
if self .received_at is not None :
248
239
obj ["received_at" ] = self .received_at
249
240
else :
@@ -264,7 +255,7 @@ def is_valid(self):
264
255
if self .body is None :
265
256
return False
266
257
big_body_ok = self .body_len > Packet .MAX_BODY_SEGMENT_LEN and len (self .body ) == Packet .MAX_BODY_SEGMENT_LEN
267
- small_body_ok = self .body_len == 0 or self .body_len == len (self .body )
258
+ small_body_ok = self .body_len == 0 or self .body_len == len (self .body ) - 2
268
259
body_ok = (big_body_ok or small_body_ok )
269
260
return self .crc_ok () and body_ok
270
261
0 commit comments