Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hid_parser: handle negative numbers in descriptors #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 37 additions & 17 deletions hid_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ class TagGlobal():
REPORT_COUNT = 0b1001
PUSH = 0b1010
POP = 0b1011
# These tags have signed integer data
_signed_tags = (
LOGICAL_MINIMUM,
LOGICAL_MAXIMUM,
PHYSICAL_MINIMUM,
PHYSICAL_MAXIMUM,
UNIT_EXPONENT,
)


class TagLocal():
Expand All @@ -81,6 +89,15 @@ class TagLocal():
DELIMITER = 0b1010


def _item_is_signed(tag: int, typ: int) -> bool:
if typ != Type.GLOBAL:
return False
if tag in TagGlobal._signed_tags:
return True
else:
return False


def _data_bit_shift(data: Sequence[int], offset: int, length: int) -> Sequence[int]:
if not length > 0:
raise ValueError(f'Invalid specified length: {length}')
Expand Down Expand Up @@ -688,33 +705,36 @@ def _iterate_raw(self) -> Iterable[Tuple[int, int, Optional[int]]]:
i = 0
while i < len(self.data):
prefix = self.data[i]
i += 1
tag = (prefix & 0b11110000) >> 4
typ = (prefix & 0b00001100) >> 2
size = prefix & 0b00000011

if size == 3: # 6.2.2.2
size = 4

if size == 0:
data = None
elif size == 1:
if i + 1 >= len(self.data):
raise InvalidReportDescriptor(f'Invalid size: expecting >={i + 1}, got {len(self.data)}')
data = self.data[i+1]
# 6.2.2.4 Main items with empty data have a value of zero
if typ == Type.MAIN:
data = 0
else:
data = None
elif size > 3:
raise ValueError(f'Invalid item size: {size}')
else:
if i + 1 + size >= len(self.data):
raise InvalidReportDescriptor(f'Invalid size: expecting >={i + 1 + size}, got {len(self.data)}')
if size == 2:
pack_type = 'H'
elif size == 4:
pack_type = 'L'
# Pick an unpack format letter by size and signedness
# Per 6.2.2.2, size of 0b11 means 4
if _item_is_signed(tag, typ):
pack_type = '0bhl'[size]
else:
raise ValueError(f'Invalid item size: {size}')
data = struct.unpack(f'<{pack_type}', bytes(self.data[i+1:i+1+size]))[0]
pack_type = '0BHL'[size]
fmt = f'<{pack_type}'
size = struct.calcsize(fmt)
if i + size > len(self.data):
raise InvalidReportDescriptor(f'Invalid size: expecting >={i + size}, got {len(self.data)}')

data, = struct.unpack(fmt, bytes(self.data[i:i+size]))

yield typ, tag, data

i += size + 1
i += size

def _append_item(
self,
Expand Down
4 changes: 2 additions & 2 deletions tests/linux-hidpp-print.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ Collection (Application)
Report Size (1)
Input (Data, Variable, Absolute, No Wrap, Linear, Preferred State, No Null position, Bit Field)
Usage Page (Generic Desktop Controls)
Logical Minimum (32769)
Logical Minimum (-32767)
Logical Maximum (32767)
Report Size (16)
Report Count (2)
Usage (X)
Usage (Y)
Input (Data, Variable, Relative, No Wrap, Linear, Preferred State, No Null position, Bit Field)
Logical Minimum (129)
Logical Minimum (-127)
Logical Maximum (127)
Report Size (8)
Report Count (1)
Expand Down
2 changes: 1 addition & 1 deletion tests/simple-mouse-print.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Collection (Application)
Usage Page (Generic Desktop Controls)
Usage (X)
Usage (Y)
Logical Minimum (129)
Logical Minimum (-127)
Logical Maximum (127)
Report Size (8)
Report Count (2)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,16 @@ def test_simple_mouse_items():
assert int(items[4].size) == 8
assert items[4].usage.page == hid_parser.data.UsagePages.GENERIC_DESKTOP_CONTROLS_PAGE
assert items[4].usage.usage == hid_parser.data.GenericDesktopControls.X
assert items[4].logical_min == -127
assert items[4].logical_max == 127

assert isinstance(items[5], hid_parser.VariableItem)
assert int(items[5].offset) == 8*2
assert int(items[5].size) == 8
assert items[5].usage.page == hid_parser.data.UsagePages.GENERIC_DESKTOP_CONTROLS_PAGE
assert items[5].usage.usage == hid_parser.data.GenericDesktopControls.Y
assert items[5].logical_min == -127
assert items[5].logical_max == 127


def test_linux_hidpp_items():
Expand Down