Skip to content

Commit 89daab1

Browse files
committed
LibUSBHIDAPI Transport: Validate params outside of the lock.
1 parent 8ef5e71 commit 89daab1

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/StreamDeck/Transport/LibUSBHIDAPI.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ def open_device(self, path):
213213
:rtype: Handle
214214
:return: Device handle if opened successfully, None if open failed.
215215
"""
216-
217216
with self.mutex:
218217
if type(path) is not bytes:
219218
path = bytes(path, 'utf-8')
@@ -250,10 +249,10 @@ def send_feature_report(self, handle, data):
250249
:rtype: int
251250
:return: Number of bytes successfully sent to the device.
252251
"""
253-
with self.mutex:
254-
if not handle:
255-
raise TransportError("No HID device.")
252+
if not handle:
253+
raise TransportError("No HID device.")
256254

255+
with self.mutex:
257256
result = self.hidapi.hid_send_feature_report(handle, bytes(data), len(data))
258257

259258
if result < 0:
@@ -274,6 +273,8 @@ def get_feature_report(self, handle, report_id, length):
274273
first byte of the report will be the Report ID of the
275274
report that was read.
276275
"""
276+
if not handle:
277+
raise TransportError("No HID device.")
277278

278279
# We may need to oversize our read due a bug in some versions of
279280
# HIDAPI. Only applied on Mac systems, as this will cause other
@@ -284,9 +285,6 @@ def get_feature_report(self, handle, report_id, length):
284285
data[0] = report_id
285286

286287
with self.mutex:
287-
if not handle:
288-
raise TransportError("No HID device.")
289-
290288
result = self.hidapi.hid_get_feature_report(handle, data, len(data))
291289

292290
if result < 0:
@@ -313,10 +311,10 @@ def write(self, handle, data):
313311
:rtype: int
314312
:return: Number of bytes successfully sent to the device.
315313
"""
316-
with self.mutex:
317-
if not handle:
318-
raise TransportError("No HID device.")
314+
if not handle:
315+
raise TransportError("No HID device.")
319316

317+
with self.mutex:
320318
result = self.hidapi.hid_write(handle, bytes(data), len(data))
321319

322320
if result < 0:
@@ -336,13 +334,12 @@ def read(self, handle, length):
336334
first byte of the report will be the Report ID of the
337335
report that was read.
338336
"""
337+
if not handle:
338+
raise TransportError("No HID device.")
339339

340340
data = ctypes.create_string_buffer(length)
341341

342342
with self.mutex:
343-
if not handle:
344-
raise TransportError("No HID device.")
345-
346343
result = self.hidapi.hid_read(handle, data, len(data))
347344

348345
if result < 0:

0 commit comments

Comments
 (0)