diff options
author | Diego Elio Pettenò <flameeyes@flameeyes.eu> | 2016-02-13 01:08:32 +0100 |
---|---|---|
committer | Diego Elio Pettenò <flameeyes@flameeyes.eu> | 2016-02-13 01:08:32 +0100 |
commit | f507bb35dcd72a147f4f795464e034dfac929d05 (patch) | |
tree | 85e12c5fd61b88f02c7dfc9184fc0789cdbe6014 | |
parent | Update documentation, reference the new glucometer-protocols repository. (diff) | |
download | glucometerutils-f507bb35dcd72a147f4f795464e034dfac929d05.tar glucometerutils-f507bb35dcd72a147f4f795464e034dfac929d05.tar.gz glucometerutils-f507bb35dcd72a147f4f795464e034dfac929d05.tar.bz2 glucometerutils-f507bb35dcd72a147f4f795464e034dfac929d05.tar.lz glucometerutils-f507bb35dcd72a147f4f795464e034dfac929d05.tar.xz glucometerutils-f507bb35dcd72a147f4f795464e034dfac929d05.tar.zst glucometerutils-f507bb35dcd72a147f4f795464e034dfac929d05.zip |
Diffstat (limited to '')
-rw-r--r-- | glucometerutils/drivers/lifescan_common.py | 24 | ||||
-rw-r--r-- | glucometerutils/drivers/otultraeasy.py | 15 | ||||
-rw-r--r-- | test/test_otultraeasy.py | 4 |
3 files changed, 27 insertions, 16 deletions
diff --git a/glucometerutils/drivers/lifescan_common.py b/glucometerutils/drivers/lifescan_common.py index 9a60ab6..f37903a 100644 --- a/glucometerutils/drivers/lifescan_common.py +++ b/glucometerutils/drivers/lifescan_common.py @@ -26,3 +26,27 @@ class InvalidSerialNumber(exceptions.Error): """The serial number is not as expected.""" def __init__(self, serial_number): self.message = 'Serial number %s is invalid.' % serial_number + + +def crc_ccitt(data): + """Calculate the CRC-16-CCITT with LifeScan's common seed. + + Args: + data: (bytes) the data to calculate the checksum of + + Returns: + (int) The 16-bit integer value of the CRC-CCITT calculated. + + This function uses the non-default 0xFFFF seed as used by multiple + LifeScan meters. + """ + crc = 0xffff + + for byte in data: + crc = (crc >> 8) & 0xffff | (crc << 8) & 0xffff + crc ^= byte + crc ^= (crc & 0xff) >> 4 + crc ^= (((crc << 8) & 0xffff) << 4) & 0xffff + crc ^= (crc & 0xff) << 5 + + return (crc & 0xffff) diff --git a/glucometerutils/drivers/otultraeasy.py b/glucometerutils/drivers/otultraeasy.py index 422a7c1..0028ff6 100644 --- a/glucometerutils/drivers/otultraeasy.py +++ b/glucometerutils/drivers/otultraeasy.py @@ -68,19 +68,6 @@ def _convert_timestamp(timestamp_bytes): class _Packet(object): _STRUCT = struct.Struct('<H') - @staticmethod - def _crc(cmd): - crc = 0xffff - - for byte in cmd: - crc = (crc >> 8) & 0xffff | (crc << 8) & 0xffff - crc ^= byte - crc ^= (crc & 0xff) >> 4 - crc ^= (((crc << 8) & 0xffff) << 4) & 0xffff - crc ^= (crc & 0xff) << 5 - - return (crc & 0xffff) - def __init__(self): self.cmd = array.array('B') @@ -148,7 +135,7 @@ class _Packet(object): @property def checksum(self): - return self._crc(self.cmd[:_IDX_CHECKSUM].tobytes()) + return lifescan_common.crc_ccitt(self.cmd[:_IDX_CHECKSUM].tobytes()) @property def acknowledge(self): diff --git a/test/test_otultraeasy.py b/test/test_otultraeasy.py index 326aeea..f263f12 100644 --- a/test/test_otultraeasy.py +++ b/test/test_otultraeasy.py @@ -31,12 +31,12 @@ class TestOTUltraMini(unittest.TestCase): def testCrc(self): self.assertEqual( 0x41cd, - otultraeasy._Packet._crc(b'\x02\x06\x06\x03')) + lifescan_common.crc_ccitt(b'\x02\x06\x06\x03')) cmd_array = array.array('B', b'\x02\x06\x08\x03') self.assertEqual( 0x62C2, - otultraeasy._Packet._crc(cmd_array)) + lifescan_common.crc_ccitt(cmd_array)) def testPacketUpdateChecksum(self): packet = otultraeasy._Packet() |