From dea8ec030eda9d9d2c4f22d1491cae5d2b94c79a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Sat, 3 Jun 2017 15:46:35 +0100 Subject: Abbott devices: add support for using hidapi library. This allows these drivers to be used on operating systems other than Linux, as hidapi abstracts away the access. The interface is similar enough to using hidraw that the actual code only needed minimal changes. Maintain the ability to access the devices with the hidraw path too, without forcing the dependency. --- glucometerutils/support/freestyle.py | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'glucometerutils/support/freestyle.py') diff --git a/glucometerutils/support/freestyle.py b/glucometerutils/support/freestyle.py index bb891fd..737eda1 100644 --- a/glucometerutils/support/freestyle.py +++ b/glucometerutils/support/freestyle.py @@ -69,16 +69,39 @@ class FreeStyleHidDevice(object): TEXT_CMD = 0x60 TEXT_REPLY_CMD = 0x60 + USB_VENDOR_ID = 0x1a61 # Abbott Diabetes Care + USB_PRODUCT_ID = None + def __init__(self, device): - if not device: + # If we do not know for sure the device ID, rely on the user providing a + # device path. + if self.USB_PRODUCT_ID is None and not device: raise exceptions.CommandLineError( '--device parameter is required, should point to /dev/hidraw ' 'for the meter') - if not os.path.exists(device): + # If the user passed a device path that does not exist, raise an error. + if device and not os.path.exists(device): raise exceptions.ConnectionFailed( message='Path %s does not exist.' % device) - self.handle_ = open(device, 'w+b') + + # If the user passed a device, try opening it. Note that I have had no + # success on actually opening the /dev/hidraw path but that's a + # different problem. + try: + if device: + self.handle_ = open(device, 'w+b') + else: + try: + import hid + except ImportError: + raise exceptions.ConnectionFailed( + message='Missing requied "hidapi" module.') + self.handle_ = hid.device() + self.handle_.open(self.USB_VENDOR_ID, self.USB_PRODUCT_ID) + except OSError: + raise exceptions.ConnectionFailed( + message='Unable to connect to meter.') def connect(self): """Open connection to the device, starting the knocking sequence.""" @@ -103,8 +126,7 @@ class FreeStyleHidDevice(object): cmdlen = len(command) assert cmdlen <= 62 - # First byte in the written buffer is the report number, on Linux HID - # interface. + # First byte in the written buffer is the report number. usb_packet = b'\x00' + _STRUCT_PREAMBLE.pack( message_type, cmdlen) + command + bytes(62 - cmdlen) @@ -126,7 +148,8 @@ class FreeStyleHidDevice(object): if message_type == 0x22 and message_length == 1: return self._read_response() - return (message_type, message_content) + # hidapi module returns a list of bytes rather than a bytes object. + return (message_type, bytes(message_content)) def _send_text_command(self, command): """Send a command to the device that expects a text reply.""" -- cgit v1.2.3