Skip to content

Commit 78ac347

Browse files
decoders/can: check SBC parity and annotate warning if invalid
1 parent f2b88cb commit 78ac347

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

decoders/can/pd.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,15 @@ def is_stuff_bit(self):
220220
self.last_bit_was_stuff_bit = True
221221
return True
222222

223+
def is_valid_parity(self, num, given_parity_bit):
224+
calculated_parity_bit = 0
225+
226+
while num:
227+
calculated_parity_bit ^= num & 1
228+
num >>= 1
229+
230+
return calculated_parity_bit == given_parity_bit
231+
223232
def is_valid_crc(self, crc_bits):
224233
return True # TODO
225234

@@ -243,6 +252,12 @@ def decode_frame_end(self, can_rx, bitnum):
243252
sbc_bits = self.bits[x:x + self.last_databit + 4]
244253
p_gray_sbc = bitpack_msb(sbc_bits)
245254

255+
parity = p_gray_sbc & 1
256+
gray_sbc = p_gray_sbc >> 1
257+
258+
if not self.is_valid_parity(gray_sbc, parity):
259+
self.putb([16, ['Parity is invalid']])
260+
246261
gray_sbc = p_gray_sbc >> 1
247262
sbc = gray2num(gray_sbc) # Number of stuff bits modulo 8
248263

0 commit comments

Comments
 (0)