mvillalba / python-ant

[UNMAINTAINED, LOOK INTO THE FORKS] Python implementation of the ANT, ANT+, and ANT-FS protocols (http://www.thisisant.com/).
https://github.com/mvillalba/python-ant
MIT License
160 stars 131 forks source link

Checksum incorrectly calculated... sometimes. #25

Open Zebble opened 9 years ago

Zebble commented 9 years ago

Changed in getChecksum in message.py:

checksum = (checksum ^ ord(byte)) % 0xFF

To:

checksum = (checksum ^ ord(byte))

Wasn't properly creating checksum on some streams. Here's an example. getChecksum would fail on the "A4" checksum in the Read response. This was on a Dynastream ANTUSB-m.

Write:

A4 02 4D 00 54 BF

Read:

A4 07 54 08 08 00 BA 36 00 DF A4

-wade

SamyCookie commented 9 years ago

More precisely, I think the author wanted to write: checksum = (checksum ^ ord(byte)) & 0xFF for preventing some errors with integer overflow superior to 0xFF, however I think that case seems to be impossible with a range of bytes. With the current algorithm, if the first operand return 0xFF, the result will be 0, but the expected result should be 0xFF. This is the only value which fail with the used operator %. 0xAB % 0xFF => 0xAB (Good) 0xFF % 0xFF => 0 (failed, expected 0xFF) with &: 0xAB & 0xFF => 0xAB 0xFF & 0xFF => 0xFF