From 90e9a2d81d555bd916f57d7538881966e77543e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Mon, 16 Jan 2017 22:07:42 +0000 Subject: LifeScan devices: move lifescan_common to the support directory. This makes it easier to figure out which files represent drivers, and which ones are per-vendor support modules. --- glucometerutils/support/lifescan.py | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 glucometerutils/support/lifescan.py (limited to 'glucometerutils/support/lifescan.py') diff --git a/glucometerutils/support/lifescan.py b/glucometerutils/support/lifescan.py new file mode 100644 index 0000000..1f8234d --- /dev/null +++ b/glucometerutils/support/lifescan.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +"""Common utility functions for LifeScan meters.""" + +__author__ = 'Diego Elio Pettenò' +__email__ = 'flameeyes@flameeyes.eu' +__copyright__ = 'Copyright © 2013, Diego Elio Pettenò' +__license__ = 'MIT' + +from glucometerutils import exceptions + + +class MissingChecksum(exceptions.InvalidResponse): + """The response misses the expected 4-digits checksum.""" + def __init__(self, response): + self.message = 'Response is missing checksum: %s' % response + + +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 + + +class MalformedCommand(exceptions.InvalidResponse): + def __init__(self, message): + exceptions.InvalidResponse.__init__( + self, 'Malformed command: %s' % message) + + +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) -- cgit v1.2.3