Skip to content

Commit

Permalink
feat: add get_report_descriptor wrapper (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldkv authored Nov 8, 2024
1 parent 02c0847 commit 9dbdd01
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions chid.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ cdef extern from "<hidapi.h>":
int hid_read(hid_device* device, unsigned char* data, int max_length) nogil
int hid_read_timeout(hid_device* device, unsigned char* data, int max_length, int milliseconds) nogil
int hid_set_nonblocking(hid_device* device, int value)
int hid_get_report_descriptor(hid_device* device, unsigned char *data, int length) nogil
int hid_send_feature_report(hid_device* device, unsigned char *data, int length) nogil
int hid_get_feature_report(hid_device* device, unsigned char *data, int length) nogil
int hid_get_input_report(hid_device* device, unsigned char *data, int length) nogil
Expand Down
32 changes: 32 additions & 0 deletions hid.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,38 @@ cdef class device:
result = hid_send_feature_report(c_hid, cbuff, c_buff_len)
return result

def get_report_descriptor(self, int max_length=4096):
"""Return the report descriptor up to max_length bytes.
If max_length is bigger than the actual descriptor, the full descriptor will be returned.
:param max_length: Maximum number of bytes to read, must be positive
:type max_length: int
:return:
:rtype: List[int]
:raises ValueError: If connection is not opened.
:raises IOError: If read error
"""
if self._c_hid == NULL:
raise ValueError('not open')

cdef unsigned char* cbuff
cdef size_t c_descriptor_length = max(1, max_length)
cdef hid_device * c_hid = self._c_hid
cdef int n
result = []
try:
cbuff = <unsigned char *>malloc(max_length)
with nogil:
n = hid_get_report_descriptor(c_hid, cbuff, c_descriptor_length)
if n < 0:
raise IOError('read error')
for i in range(n):
result.append(cbuff[i])
finally:
free(cbuff)
return result

def get_feature_report(self, int report_num, int max_length):
"""Receive feature report.
Expand Down

0 comments on commit 9dbdd01

Please sign in to comment.