From 91fb2d40f286c1f3b0af9146d66d424f4e8c5c6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Sat, 3 Aug 2013 22:18:11 +0100 Subject: Implement the correct checksum algorithm. Unlike what I though, OneTouch2 does not use CRC32 Sick algorithm, but a very stupid full 16-bit modulo sum of the bytes in the reply. This is very simple and does not require GPLv3 code at all. --- glucometerutils/drivers/lifescan_common.py | 38 +++++++----------------------- 1 file changed, 8 insertions(+), 30 deletions(-) (limited to 'glucometerutils/drivers') diff --git a/glucometerutils/drivers/lifescan_common.py b/glucometerutils/drivers/lifescan_common.py index ac25d22..5b42c33 100644 --- a/glucometerutils/drivers/lifescan_common.py +++ b/glucometerutils/drivers/lifescan_common.py @@ -6,8 +6,6 @@ __email__ = 'flameeyes@flameeyes.eu' __copyright__ = 'Copyright © 2013, Diego Elio Pettenò' __license__ = 'GPL v3 or later' -import ctypes - from glucometerutils import exceptions @@ -31,40 +29,20 @@ class InvalidSerialNumber(exceptions.Error): def calculate_checksum(bytestring): - """Calculate the "CRC16 Sick" style checksum for LifeScan protocols. + """Calculate the checksum used by OneTouch Ultra and Ultra2 devices Args: bytestring: the string of which the checksum has to be calculated. Returns: - A 16-bit integer that is the checksum for the input. + A string with the hexdecimal representation of the checksum for the input. - Credits for this code go to Christian Navalici, who implemented it in his - library at https://github.com/cristianav/PyCRC/ . + The checksum is a very stupid one: it just sums all the bytes, + modulo 16-bit, without any parity. """ - crcValue = 0x0000 - prev_c = 0x0000 - - for idx, c in enumerate(bytestring): - short_c = 0x00ff & c - - idx_previous = idx - 1 - short_p = ( 0x00ff & prev_c) << 8; - - if ( crcValue & 0x8000 ): - crcValue = ctypes.c_ushort(crcValue << 1).value ^ 0x8005 - else: - crcValue = ctypes.c_ushort(crcValue << 1).value - - crcValue &= 0xffff - crcValue ^= ( short_c | short_p ) - - prev_c = short_c + checksum = 0 - # After processing, the one's complement of the CRC is calcluated and the - # two bytes of the CRC are swapped. - low_byte = (crcValue & 0xff00) >> 8 - high_byte = (crcValue & 0x00ff) << 8 - crcValue = low_byte | high_byte; + for byte in bytestring: + checksum = (checksum + byte) & 0xffff - return crcValue + return checksum -- cgit v1.2.3