Skip to content

Commit

Permalink
Don't double-open HID devices, add translation of internal HID librar…
Browse files Browse the repository at this point in the history
…y errors to IOError exceptions.
  • Loading branch information
abcminiuser committed Oct 21, 2019
1 parent 2a76fdf commit 0a24f77
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
61 changes: 47 additions & 14 deletions src/StreamDeck/Transport/HID.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,15 @@ def __init__(self, device_info):
:param dict() device_info: Device information dictionary describing
a single unique attached USB HID device.
"""
import hid

self.hid_info = device_info
self.hid = hid.Device(vid=device_info['vendor_id'], pid=device_info['product_id'])
self.hid = None

def __del__(self):
"""
Deletion handler for the HID transport, automatically closing the
device if it is currently open.
"""
try:
self.hid.close()
except (IOError, ValueError):
pass
self.close()

def open(self):
"""
Expand All @@ -59,7 +54,13 @@ def close(self):
.. seealso:: See :func:`~~HID.Device.open` for the corresponding
open method.
"""
self.hid.close()
if not self.hid:
return

try:
self.hid.close()
except Exception: # nosec
pass

def connected(self):
"""
Expand Down Expand Up @@ -97,10 +98,18 @@ def write_feature(self, payload):
:rtype: int
:return: Number of bytes successfully sent to the device.
"""
import hid

if not self.hid:
raise IOError("No HID device!")

if type(payload) is bytearray:
payload = bytes(payload)

return self.hid.send_feature_report(payload)
try:
return self.hid.send_feature_report(payload)
except hid.HIDException as e:
raise IOError(e)

def read_feature(self, report_id, length):
"""
Expand All @@ -114,7 +123,15 @@ def read_feature(self, report_id, length):
first byte of the report will be the Report ID of the
report that was read.
"""
return self.hid.get_feature_report(report_id, length)
import hid

if not self.hid:
raise IOError("No HID device!")

try:
return self.hid.get_feature_report(report_id, length)
except hid.HIDException as e:
raise IOError(e)

def write(self, payload):
"""
Expand All @@ -128,10 +145,18 @@ def write(self, payload):
:rtype: int
:return: Number of bytes successfully sent to the device.
"""
import hid

if not self.hid:
raise IOError("No HID device!")

if type(payload) is bytearray:
payload = bytes(payload)

return self.hid.write(payload)
try:
return self.hid.write(payload)
except hid.HIDException as e:
raise IOError(e)

def read(self, length):
"""
Expand All @@ -144,7 +169,15 @@ def read(self, length):
of the report will be the Report ID of the report that was
read.
"""
return self.hid.read(length)
import hid

if not self.hid:
raise IOError("No HID device!")

try:
return self.hid.read(length)
except hid.HIDException as e:
raise IOError(e)

@staticmethod
def probe():
Expand All @@ -153,8 +186,8 @@ def probe():
expected that probe failures throw exceptions detailing their exact
cause of failure.
"""

import hid

hid.enumerate(vid=0, pid=0)

def enumerate(self, vid, pid):
Expand All @@ -169,14 +202,14 @@ def enumerate(self, vid, pid):
:rtype: list(HID.Device)
:return: List of discovered USB HID devices.
"""
import hid

if vid is None:
vid = 0

if pid is None:
pid = 0

import hid
devices = hid.enumerate(vid=vid, pid=pid)

return [HID.Device(d) for d in devices]
14 changes: 7 additions & 7 deletions src/StreamDeck/Transport/HIDAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ def __del__(self):
Deletion handler for the HIDAPI transport, automatically closing the
device if it is currently open.
"""
try:
self.hid.close()
except (IOError, ValueError):
pass
self.close()

def open(self):
"""
Expand All @@ -57,7 +54,10 @@ def close(self):
.. seealso:: See :func:`~~HIDAPI.Device.open` for the corresponding
open method.
"""
self.hid.close()
try:
self.hid.close()
except Exception: # nosec
pass

def connected(self):
"""
Expand Down Expand Up @@ -145,8 +145,8 @@ def probe():
expected that probe failures throw exceptions detailing their exact
cause of failure.
"""

import hid

hid.enumerate(vid=0, pid=0)

def enumerate(self, vid, pid):
Expand All @@ -161,14 +161,14 @@ def enumerate(self, vid, pid):
:rtype: list(HIDAPI.Device)
:return: List of discovered USB HID devices.
"""
import hid

if vid is None:
vid = 0

if pid is None:
pid = 0

import hid
devices = hid.enumerate(vendor_id=vid, product_id=pid)

return [HIDAPI.Device(d) for d in devices]

0 comments on commit 0a24f77

Please sign in to comment.