Skip to content

Commit

Permalink
Refactor reflect() method to function.
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed Mar 21, 2018
1 parent 1bd83e5 commit 7253748
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions pyxcp/checksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__copyright__="""
pySART - Simplified AUTOSAR-Toolkit for Python.
(C) 2009-2017 by Christoph Schueler <cpu12.gems@googlemail.com>
(C) 2009-2018 by Christoph Schueler <cpu12.gems@googlemail.com>
All Rights Reserved
Expand Down Expand Up @@ -111,6 +111,15 @@ class Algorithm(enum.IntEnum):
)


def reflect(data, nBits):
reflection = 0x00000000
for bit in range(nBits):
if (data & 0x01):
reflection |= (1 << ((nBits - 1) - bit))
data = (data >> 1)
return reflection


class Crc16:
WIDTH = 16

Expand All @@ -125,24 +134,16 @@ def __call__(self, frame):
remainder = self.initalRemainder
for ch in frame:
if self.reflectData:
data = (self.reflect(ch, 8) ^ (remainder >> (self.WIDTH - 8))) & 0xff
data = (reflect(ch, 8) ^ (remainder >> (self.WIDTH - 8))) & 0xff
else:
data = (ch ^ (remainder >> (self.WIDTH - 8))) & 0xff
remainder = (self.table[data] ^ (remainder << 8)) & 0xffff
if self.reflectRemainder:
result = (self.reflect(remainder, 16) ^ self.finalXorValue)
result = (reflect(remainder, 16) ^ self.finalXorValue)
else:
result = remainder ^ self.finalXorValue
return result

def reflect(self, data, nBits):
reflection = 0x00000000
for bit in range(nBits):
if (data & 0x01):
reflection |= (1 << ((nBits - 1) - bit))
data = (data >> 1)
return reflection


"""
0x01 XCP_ADD_11 Add BYTE into a BYTE checksum, ignore overflows
Expand Down

0 comments on commit 7253748

Please sign in to comment.